ssfdc.c 12 KB

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