ssfdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Linux driver for SSFDC Flash Translation Layer (Read only)
  3. * (c) 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/nand.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. DEBUG(MTD_DEBUG_LEVEL1,
  120. "SSFDC_RO: CIS/IDI sector not found"
  121. " on %s (mtd%d)\n", mtd->name,
  122. mtd->index);
  123. }
  124. break;
  125. }
  126. }
  127. kfree(sect_buf);
  128. out:
  129. return cis_sector;
  130. }
  131. /* Read physical sector (wrapper to MTD_READ) */
  132. static int read_physical_sector(struct mtd_info *mtd, uint8_t *sect_buf,
  133. int sect_no)
  134. {
  135. int ret;
  136. size_t retlen;
  137. loff_t offset = (loff_t)sect_no << SECTOR_SHIFT;
  138. ret = mtd->read(mtd, offset, SECTOR_SIZE, &retlen, sect_buf);
  139. if (ret < 0 || retlen != SECTOR_SIZE)
  140. return -1;
  141. return 0;
  142. }
  143. /* Read redundancy area (wrapper to MTD_READ_OOB */
  144. static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf)
  145. {
  146. struct mtd_oob_ops ops;
  147. int ret;
  148. ops.mode = MTD_OOB_RAW;
  149. ops.ooboffs = 0;
  150. ops.ooblen = mtd->oobsize;
  151. ops.len = OOB_SIZE;
  152. ops.oobbuf = buf;
  153. ops.datbuf = NULL;
  154. ret = mtd->read_oob(mtd, offs, &ops);
  155. if (ret < 0 || ops.retlen != OOB_SIZE)
  156. return -1;
  157. return 0;
  158. }
  159. /* Parity calculator on a word of n bit size */
  160. static int get_parity(int number, int size)
  161. {
  162. int k;
  163. int parity;
  164. parity = 1;
  165. for (k = 0; k < size; k++) {
  166. parity += (number >> k);
  167. parity &= 1;
  168. }
  169. return parity;
  170. }
  171. /* Read and validate the logical block address field stored in the OOB */
  172. static int get_logical_address(uint8_t *oob_buf)
  173. {
  174. int block_address, parity;
  175. int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */
  176. int j;
  177. int ok = 0;
  178. /*
  179. * Look for the first valid logical address
  180. * Valid address has fixed pattern on most significant bits and
  181. * parity check
  182. */
  183. for (j = 0; j < ARRAY_SIZE(offset); j++) {
  184. block_address = ((int)oob_buf[offset[j]] << 8) |
  185. oob_buf[offset[j]+1];
  186. /* Check for the signature bits in the address field (MSBits) */
  187. if ((block_address & ~0x7FF) == 0x1000) {
  188. parity = block_address & 0x01;
  189. block_address &= 0x7FF;
  190. block_address >>= 1;
  191. if (get_parity(block_address, 10) != parity) {
  192. DEBUG(MTD_DEBUG_LEVEL0,
  193. "SSFDC_RO: logical address field%d"
  194. "parity error(0x%04X)\n", j+1,
  195. block_address);
  196. } else {
  197. ok = 1;
  198. break;
  199. }
  200. }
  201. }
  202. if (!ok)
  203. block_address = -2;
  204. DEBUG(MTD_DEBUG_LEVEL3, "SSFDC_RO: get_logical_address() %d\n",
  205. block_address);
  206. return block_address;
  207. }
  208. /* Build the logic block map */
  209. static int build_logical_block_map(struct ssfdcr_record *ssfdc)
  210. {
  211. unsigned long offset;
  212. uint8_t oob_buf[OOB_SIZE];
  213. int ret, block_address, phys_block;
  214. struct mtd_info *mtd = ssfdc->mbd.mtd;
  215. DEBUG(MTD_DEBUG_LEVEL1, "SSFDC_RO: build_block_map() nblks=%d (%luK)\n",
  216. ssfdc->map_len,
  217. (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024);
  218. /* Scan every physical block, skip CIS block */
  219. for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len;
  220. phys_block++) {
  221. offset = (unsigned long)phys_block * ssfdc->erase_size;
  222. if (mtd->block_isbad(mtd, offset))
  223. continue; /* skip bad blocks */
  224. ret = read_raw_oob(mtd, offset, oob_buf);
  225. if (ret < 0) {
  226. DEBUG(MTD_DEBUG_LEVEL0,
  227. "SSFDC_RO: mtd read_oob() failed at %lu\n",
  228. offset);
  229. return -1;
  230. }
  231. block_address = get_logical_address(oob_buf);
  232. /* Skip invalid addresses */
  233. if (block_address >= 0 &&
  234. block_address < MAX_LOGIC_BLK_PER_ZONE) {
  235. int zone_index;
  236. zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE;
  237. block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE;
  238. ssfdc->logic_block_map[block_address] =
  239. (unsigned short)phys_block;
  240. DEBUG(MTD_DEBUG_LEVEL2,
  241. "SSFDC_RO: build_block_map() phys_block=%d,"
  242. "logic_block_addr=%d, zone=%d\n",
  243. phys_block, block_address, zone_index);
  244. }
  245. }
  246. return 0;
  247. }
  248. static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  249. {
  250. struct ssfdcr_record *ssfdc;
  251. int cis_sector;
  252. /* Check for small page NAND flash */
  253. if (mtd->type != MTD_NANDFLASH || mtd->oobsize != OOB_SIZE)
  254. return;
  255. /* Check for SSDFC format by reading CIS/IDI sector */
  256. cis_sector = get_valid_cis_sector(mtd);
  257. if (cis_sector == -1)
  258. return;
  259. ssfdc = kzalloc(sizeof(struct ssfdcr_record), GFP_KERNEL);
  260. if (!ssfdc) {
  261. printk(KERN_WARNING
  262. "SSFDC_RO: out of memory for data structures\n");
  263. return;
  264. }
  265. ssfdc->mbd.mtd = mtd;
  266. ssfdc->mbd.devnum = -1;
  267. ssfdc->mbd.blksize = SECTOR_SIZE;
  268. ssfdc->mbd.tr = tr;
  269. ssfdc->mbd.readonly = 1;
  270. ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT);
  271. ssfdc->erase_size = mtd->erasesize;
  272. ssfdc->map_len = mtd->size / mtd->erasesize;
  273. DEBUG(MTD_DEBUG_LEVEL1,
  274. "SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n",
  275. ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len,
  276. (ssfdc->map_len + MAX_PHYS_BLK_PER_ZONE - 1) /
  277. MAX_PHYS_BLK_PER_ZONE);
  278. /* Set geometry */
  279. ssfdc->heads = 16;
  280. ssfdc->sectors = 32;
  281. get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors);
  282. ssfdc->cylinders = (unsigned short)((mtd->size >> SECTOR_SHIFT) /
  283. ((long)ssfdc->sectors * (long)ssfdc->heads));
  284. DEBUG(MTD_DEBUG_LEVEL1, "SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n",
  285. ssfdc->cylinders, ssfdc->heads , ssfdc->sectors,
  286. (long)ssfdc->cylinders * (long)ssfdc->heads *
  287. (long)ssfdc->sectors);
  288. ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders *
  289. (long)ssfdc->sectors;
  290. /* Allocate logical block map */
  291. ssfdc->logic_block_map = kmalloc(sizeof(ssfdc->logic_block_map[0]) *
  292. ssfdc->map_len, GFP_KERNEL);
  293. if (!ssfdc->logic_block_map) {
  294. printk(KERN_WARNING
  295. "SSFDC_RO: out of memory for data structures\n");
  296. goto out_err;
  297. }
  298. memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) *
  299. ssfdc->map_len);
  300. /* Build logical block map */
  301. if (build_logical_block_map(ssfdc) < 0)
  302. goto out_err;
  303. /* Register device + partitions */
  304. if (add_mtd_blktrans_dev(&ssfdc->mbd))
  305. goto out_err;
  306. printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n",
  307. ssfdc->mbd.devnum + 'a', mtd->index, mtd->name);
  308. return;
  309. out_err:
  310. kfree(ssfdc->logic_block_map);
  311. kfree(ssfdc);
  312. }
  313. static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev)
  314. {
  315. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  316. DEBUG(MTD_DEBUG_LEVEL1, "SSFDC_RO: remove_dev (i=%d)\n", dev->devnum);
  317. del_mtd_blktrans_dev(dev);
  318. kfree(ssfdc->logic_block_map);
  319. kfree(ssfdc);
  320. }
  321. static int ssfdcr_readsect(struct mtd_blktrans_dev *dev,
  322. unsigned long logic_sect_no, char *buf)
  323. {
  324. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  325. int sectors_per_block, offset, block_address;
  326. sectors_per_block = ssfdc->erase_size >> SECTOR_SHIFT;
  327. offset = (int)(logic_sect_no % sectors_per_block);
  328. block_address = (int)(logic_sect_no / sectors_per_block);
  329. DEBUG(MTD_DEBUG_LEVEL3,
  330. "SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d,"
  331. " block_addr=%d\n", logic_sect_no, sectors_per_block, offset,
  332. block_address);
  333. if (block_address >= ssfdc->map_len)
  334. BUG();
  335. block_address = ssfdc->logic_block_map[block_address];
  336. DEBUG(MTD_DEBUG_LEVEL3,
  337. "SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n",
  338. block_address);
  339. if (block_address < 0xffff) {
  340. unsigned long sect_no;
  341. sect_no = (unsigned long)block_address * sectors_per_block +
  342. offset;
  343. DEBUG(MTD_DEBUG_LEVEL3,
  344. "SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n",
  345. sect_no);
  346. if (read_physical_sector(ssfdc->mbd.mtd, buf, sect_no) < 0)
  347. return -EIO;
  348. } else {
  349. memset(buf, 0xff, SECTOR_SIZE);
  350. }
  351. return 0;
  352. }
  353. static int ssfdcr_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  354. {
  355. struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
  356. DEBUG(MTD_DEBUG_LEVEL1, "SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n",
  357. ssfdc->cylinders, ssfdc->heads, ssfdc->sectors);
  358. geo->heads = ssfdc->heads;
  359. geo->sectors = ssfdc->sectors;
  360. geo->cylinders = ssfdc->cylinders;
  361. return 0;
  362. }
  363. /****************************************************************************
  364. *
  365. * Module stuff
  366. *
  367. ****************************************************************************/
  368. static struct mtd_blktrans_ops ssfdcr_tr = {
  369. .name = "ssfdc",
  370. .major = SSFDCR_MAJOR,
  371. .part_bits = SSFDCR_PARTN_BITS,
  372. .getgeo = ssfdcr_getgeo,
  373. .readsect = ssfdcr_readsect,
  374. .add_mtd = ssfdcr_add_mtd,
  375. .remove_dev = ssfdcr_remove_dev,
  376. .owner = THIS_MODULE,
  377. };
  378. static int __init init_ssfdcr(void)
  379. {
  380. printk(KERN_INFO "SSFDC read-only Flash Translation layer\n");
  381. return register_mtd_blktrans(&ssfdcr_tr);
  382. }
  383. static void __exit cleanup_ssfdcr(void)
  384. {
  385. deregister_mtd_blktrans(&ssfdcr_tr);
  386. }
  387. module_init(init_ssfdcr);
  388. module_exit(cleanup_ssfdcr);
  389. MODULE_LICENSE("GPL");
  390. MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>");
  391. MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card");