ssfdc.c 11 KB

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