ssfdc.c 12 KB

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