ssfdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Linux driver for SSFDC Flash Translation Layer (Read only)
  3. * © 2005 Eptar srl
  4. * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com>
  5. *
  6. * Based on NTFL and MTDBLOCK_RO drivers
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/hdreg.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/rawnand.h>
  19. #include <linux/mtd/blktrans.h>
  20. struct ssfdcr_record {
  21. struct mtd_blktrans_dev mbd;
  22. int usecount;
  23. unsigned char heads;
  24. unsigned char sectors;
  25. unsigned short cylinders;
  26. int cis_block; /* block n. containing CIS/IDI */
  27. int erase_size; /* phys_block_size */
  28. unsigned short *logic_block_map; /* all zones (max 8192 phys blocks on
  29. the 128MiB) */
  30. int map_len; /* n. phys_blocks on the card */
  31. };
  32. #define SSFDCR_MAJOR 257
  33. #define SSFDCR_PARTN_BITS 3
  34. #define SECTOR_SIZE 512
  35. #define SECTOR_SHIFT 9
  36. #define OOB_SIZE 16
  37. #define MAX_LOGIC_BLK_PER_ZONE 1000
  38. #define MAX_PHYS_BLK_PER_ZONE 1024
  39. #define KiB(x) ( (x) * 1024L )
  40. #define MiB(x) ( KiB(x) * 1024L )
  41. /** CHS Table
  42. 1MiB 2MiB 4MiB 8MiB 16MiB 32MiB 64MiB 128MiB
  43. NCylinder 125 125 250 250 500 500 500 500
  44. NHead 4 4 4 4 4 8 8 16
  45. NSector 4 8 8 16 16 16 32 32
  46. SumSector 2,000 4,000 8,000 16,000 32,000 64,000 128,000 256,000
  47. SectorSize 512 512 512 512 512 512 512 512
  48. **/
  49. typedef struct {
  50. unsigned long size;
  51. unsigned short cyl;
  52. unsigned char head;
  53. unsigned char sec;
  54. } chs_entry_t;
  55. /* Must be ordered by size */
  56. static const chs_entry_t chs_table[] = {
  57. { MiB( 1), 125, 4, 4 },
  58. { MiB( 2), 125, 4, 8 },
  59. { MiB( 4), 250, 4, 8 },
  60. { MiB( 8), 250, 4, 16 },
  61. { MiB( 16), 500, 4, 16 },
  62. { MiB( 32), 500, 8, 16 },
  63. { MiB( 64), 500, 8, 32 },
  64. { MiB(128), 500, 16, 32 },
  65. { 0 },
  66. };
  67. static int get_chs(unsigned long size, unsigned short *cyl, unsigned char *head,
  68. unsigned char *sec)
  69. {
  70. int k;
  71. int found = 0;
  72. k = 0;
  73. while (chs_table[k].size > 0 && size > chs_table[k].size)
  74. k++;
  75. if (chs_table[k].size > 0) {
  76. if (cyl)
  77. *cyl = chs_table[k].cyl;
  78. if (head)
  79. *head = chs_table[k].head;
  80. if (sec)
  81. *sec = chs_table[k].sec;
  82. found = 1;
  83. }
  84. return found;
  85. }
  86. /* These bytes are the signature for the CIS/IDI sector */
  87. static const uint8_t cis_numbers[] = {
  88. 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
  89. };
  90. /* Read and check for a valid CIS sector */
  91. static int get_valid_cis_sector(struct mtd_info *mtd)
  92. {
  93. int ret, k, cis_sector;
  94. size_t retlen;
  95. loff_t offset;
  96. uint8_t *sect_buf;
  97. cis_sector = -1;
  98. sect_buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  99. if (!sect_buf)
  100. goto out;
  101. /*
  102. * Look for CIS/IDI sector on the first GOOD block (give up after 4 bad
  103. * blocks). If the first good block doesn't contain CIS number the flash
  104. * is not SSFDC formatted
  105. */
  106. for (k = 0, offset = 0; k < 4; k++, offset += mtd->erasesize) {
  107. if (mtd_block_isbad(mtd, offset)) {
  108. ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen,
  109. sect_buf);
  110. /* CIS pattern match on the sector buffer */
  111. if (ret < 0 || retlen != SECTOR_SIZE) {
  112. printk(KERN_WARNING
  113. "SSFDC_RO:can't read CIS/IDI sector\n");
  114. } else if (!memcmp(sect_buf, cis_numbers,
  115. sizeof(cis_numbers))) {
  116. /* Found */
  117. cis_sector = (int)(offset >> SECTOR_SHIFT);
  118. } else {
  119. pr_debug("SSFDC_RO: CIS/IDI sector not found"
  120. " on %s (mtd%d)\n", mtd->name,
  121. mtd->index);
  122. }
  123. break;
  124. }
  125. }
  126. kfree(sect_buf);
  127. out:
  128. return cis_sector;
  129. }
  130. /* Read physical sector (wrapper to MTD_READ) */
  131. static int read_physical_sector(struct mtd_info *mtd, uint8_t *sect_buf,
  132. int sect_no)
  133. {
  134. int ret;
  135. size_t retlen;
  136. loff_t offset = (loff_t)sect_no << SECTOR_SHIFT;
  137. ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen, sect_buf);
  138. if (ret < 0 || retlen != SECTOR_SIZE)
  139. return -1;
  140. return 0;
  141. }
  142. /* Read redundancy area (wrapper to MTD_READ_OOB */
  143. static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf)
  144. {
  145. struct mtd_oob_ops ops;
  146. int ret;
  147. ops.mode = MTD_OPS_RAW;
  148. ops.ooboffs = 0;
  149. ops.ooblen = OOB_SIZE;
  150. ops.oobbuf = buf;
  151. ops.datbuf = NULL;
  152. ret = mtd_read_oob(mtd, offs, &ops);
  153. if (ret < 0 || ops.oobretlen != OOB_SIZE)
  154. return -1;
  155. return 0;
  156. }
  157. /* Parity calculator on a word of n bit size */
  158. static int get_parity(int number, int size)
  159. {
  160. int k;
  161. int parity;
  162. parity = 1;
  163. for (k = 0; k < size; k++) {
  164. parity += (number >> k);
  165. parity &= 1;
  166. }
  167. return parity;
  168. }
  169. /* Read and validate the logical block address field stored in the OOB */
  170. static int get_logical_address(uint8_t *oob_buf)
  171. {
  172. int block_address, parity;
  173. int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */
  174. int j;
  175. int ok = 0;
  176. /*
  177. * Look for the first valid logical address
  178. * Valid address has fixed pattern on most significant bits and
  179. * parity check
  180. */
  181. for (j = 0; j < ARRAY_SIZE(offset); j++) {
  182. block_address = ((int)oob_buf[offset[j]] << 8) |
  183. oob_buf[offset[j]+1];
  184. /* Check for the signature bits in the address field (MSBits) */
  185. if ((block_address & ~0x7FF) == 0x1000) {
  186. parity = block_address & 0x01;
  187. block_address &= 0x7FF;
  188. block_address >>= 1;
  189. if (get_parity(block_address, 10) != parity) {
  190. pr_debug("SSFDC_RO: logical address field%d"
  191. "parity error(0x%04X)\n", j+1,
  192. block_address);
  193. } else {
  194. ok = 1;
  195. break;
  196. }
  197. }
  198. }
  199. if (!ok)
  200. block_address = -2;
  201. pr_debug("SSFDC_RO: get_logical_address() %d\n",
  202. block_address);
  203. return block_address;
  204. }
  205. /* Build the logic block map */
  206. static int build_logical_block_map(struct ssfdcr_record *ssfdc)
  207. {
  208. unsigned long offset;
  209. uint8_t oob_buf[OOB_SIZE];
  210. int ret, block_address, phys_block;
  211. struct mtd_info *mtd = ssfdc->mbd.mtd;
  212. pr_debug("SSFDC_RO: build_block_map() nblks=%d (%luK)\n",
  213. ssfdc->map_len,
  214. (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024);
  215. /* Scan every physical block, skip CIS block */
  216. for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len;
  217. phys_block++) {
  218. offset = (unsigned long)phys_block * ssfdc->erase_size;
  219. if (mtd_block_isbad(mtd, offset))
  220. continue; /* skip bad blocks */
  221. ret = read_raw_oob(mtd, offset, oob_buf);
  222. if (ret < 0) {
  223. pr_debug("SSFDC_RO: mtd read_oob() failed at %lu\n",
  224. offset);
  225. return -1;
  226. }
  227. block_address = get_logical_address(oob_buf);
  228. /* Skip invalid addresses */
  229. if (block_address >= 0 &&
  230. block_address < MAX_LOGIC_BLK_PER_ZONE) {
  231. int zone_index;
  232. zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE;
  233. block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE;
  234. ssfdc->logic_block_map[block_address] =
  235. (unsigned short)phys_block;
  236. pr_debug("SSFDC_RO: build_block_map() phys_block=%d,"
  237. "logic_block_addr=%d, zone=%d\n",
  238. phys_block, block_address, zone_index);
  239. }
  240. }
  241. return 0;
  242. }
  243. static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  244. {
  245. struct ssfdcr_record *ssfdc;
  246. int cis_sector;
  247. /* Check for small page NAND flash */
  248. if (!mtd_type_is_nand(mtd) || mtd->oobsize != OOB_SIZE ||
  249. mtd->size > UINT_MAX)
  250. return;
  251. /* Check for SSDFC format by reading CIS/IDI sector */
  252. cis_sector = get_valid_cis_sector(mtd);
  253. if (cis_sector == -1)
  254. return;
  255. ssfdc = kzalloc(sizeof(struct ssfdcr_record), GFP_KERNEL);
  256. if (!ssfdc)
  257. return;
  258. ssfdc->mbd.mtd = mtd;
  259. ssfdc->mbd.devnum = -1;
  260. ssfdc->mbd.tr = tr;
  261. ssfdc->mbd.readonly = 1;
  262. ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT);
  263. ssfdc->erase_size = mtd->erasesize;
  264. ssfdc->map_len = (u32)mtd->size / mtd->erasesize;
  265. pr_debug("SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n",
  266. ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len,
  267. DIV_ROUND_UP(ssfdc->map_len, MAX_PHYS_BLK_PER_ZONE));
  268. /* Set geometry */
  269. ssfdc->heads = 16;
  270. ssfdc->sectors = 32;
  271. get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors);
  272. ssfdc->cylinders = (unsigned short)(((u32)mtd->size >> SECTOR_SHIFT) /
  273. ((long)ssfdc->sectors * (long)ssfdc->heads));
  274. pr_debug("SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n",
  275. ssfdc->cylinders, ssfdc->heads , ssfdc->sectors,
  276. (long)ssfdc->cylinders * (long)ssfdc->heads *
  277. (long)ssfdc->sectors);
  278. ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders *
  279. (long)ssfdc->sectors;
  280. /* Allocate logical block map */
  281. ssfdc->logic_block_map =
  282. kmalloc_array(ssfdc->map_len,
  283. sizeof(ssfdc->logic_block_map[0]), GFP_KERNEL);
  284. if (!ssfdc->logic_block_map)
  285. goto out_err;
  286. memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) *
  287. ssfdc->map_len);
  288. /* Build logical block map */
  289. if (build_logical_block_map(ssfdc) < 0)
  290. goto out_err;
  291. /* Register device + partitions */
  292. if (add_mtd_blktrans_dev(&ssfdc->mbd))
  293. goto out_err;
  294. printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n",
  295. ssfdc->mbd.devnum + 'a', mtd->index, mtd->name);
  296. return;
  297. out_err:
  298. kfree(ssfdc->logic_block_map);
  299. kfree(ssfdc);
  300. }
  301. static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev)
  302. {
  303. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  304. pr_debug("SSFDC_RO: remove_dev (i=%d)\n", dev->devnum);
  305. del_mtd_blktrans_dev(dev);
  306. kfree(ssfdc->logic_block_map);
  307. }
  308. static int ssfdcr_readsect(struct mtd_blktrans_dev *dev,
  309. unsigned long logic_sect_no, char *buf)
  310. {
  311. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  312. int sectors_per_block, offset, block_address;
  313. sectors_per_block = ssfdc->erase_size >> SECTOR_SHIFT;
  314. offset = (int)(logic_sect_no % sectors_per_block);
  315. block_address = (int)(logic_sect_no / sectors_per_block);
  316. pr_debug("SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d,"
  317. " block_addr=%d\n", logic_sect_no, sectors_per_block, offset,
  318. block_address);
  319. BUG_ON(block_address >= ssfdc->map_len);
  320. block_address = ssfdc->logic_block_map[block_address];
  321. pr_debug("SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n",
  322. block_address);
  323. if (block_address < 0xffff) {
  324. unsigned long sect_no;
  325. sect_no = (unsigned long)block_address * sectors_per_block +
  326. offset;
  327. pr_debug("SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n",
  328. sect_no);
  329. if (read_physical_sector(ssfdc->mbd.mtd, buf, sect_no) < 0)
  330. return -EIO;
  331. } else {
  332. memset(buf, 0xff, SECTOR_SIZE);
  333. }
  334. return 0;
  335. }
  336. static int ssfdcr_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  337. {
  338. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  339. pr_debug("SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n",
  340. ssfdc->cylinders, ssfdc->heads, ssfdc->sectors);
  341. geo->heads = ssfdc->heads;
  342. geo->sectors = ssfdc->sectors;
  343. geo->cylinders = ssfdc->cylinders;
  344. return 0;
  345. }
  346. /****************************************************************************
  347. *
  348. * Module stuff
  349. *
  350. ****************************************************************************/
  351. static struct mtd_blktrans_ops ssfdcr_tr = {
  352. .name = "ssfdc",
  353. .major = SSFDCR_MAJOR,
  354. .part_bits = SSFDCR_PARTN_BITS,
  355. .blksize = SECTOR_SIZE,
  356. .getgeo = ssfdcr_getgeo,
  357. .readsect = ssfdcr_readsect,
  358. .add_mtd = ssfdcr_add_mtd,
  359. .remove_dev = ssfdcr_remove_dev,
  360. .owner = THIS_MODULE,
  361. };
  362. static int __init init_ssfdcr(void)
  363. {
  364. printk(KERN_INFO "SSFDC read-only Flash Translation layer\n");
  365. return register_mtd_blktrans(&ssfdcr_tr);
  366. }
  367. static void __exit cleanup_ssfdcr(void)
  368. {
  369. deregister_mtd_blktrans(&ssfdcr_tr);
  370. }
  371. module_init(init_ssfdcr);
  372. module_exit(cleanup_ssfdcr);
  373. MODULE_LICENSE("GPL");
  374. MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>");
  375. MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card");