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 <[email protected]>
  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. int usecount;
  20. unsigned char heads;
  21. unsigned char sectors;
  22. unsigned short cylinders;
  23. int cis_block; /* block n. containing CIS/IDI */
  24. int erase_size; /* phys_block_size */
  25. unsigned short *logic_block_map; /* all zones (max 8192 phys blocks on
  26. the 128MiB) */
  27. int map_len; /* n. phys_blocks on the card */
  28. };
  29. #define SSFDCR_MAJOR 257
  30. #define SSFDCR_PARTN_BITS 3
  31. #define SECTOR_SIZE 512
  32. #define SECTOR_SHIFT 9
  33. #define OOB_SIZE 16
  34. #define MAX_LOGIC_BLK_PER_ZONE 1000
  35. #define MAX_PHYS_BLK_PER_ZONE 1024
  36. #define KiB(x) ( (x) * 1024L )
  37. #define MiB(x) ( KiB(x) * 1024L )
  38. /** CHS Table
  39. 1MiB 2MiB 4MiB 8MiB 16MiB 32MiB 64MiB 128MiB
  40. NCylinder 125 125 250 250 500 500 500 500
  41. NHead 4 4 4 4 4 8 8 16
  42. NSector 4 8 8 16 16 16 32 32
  43. SumSector 2,000 4,000 8,000 16,000 32,000 64,000 128,000 256,000
  44. SectorSize 512 512 512 512 512 512 512 512
  45. **/
  46. typedef struct {
  47. unsigned long size;
  48. unsigned short cyl;
  49. unsigned char head;
  50. unsigned char sec;
  51. } chs_entry_t;
  52. /* Must be ordered by size */
  53. static const chs_entry_t chs_table[] = {
  54. { MiB( 1), 125, 4, 4 },
  55. { MiB( 2), 125, 4, 8 },
  56. { MiB( 4), 250, 4, 8 },
  57. { MiB( 8), 250, 4, 16 },
  58. { MiB( 16), 500, 4, 16 },
  59. { MiB( 32), 500, 8, 16 },
  60. { MiB( 64), 500, 8, 32 },
  61. { MiB(128), 500, 16, 32 },
  62. { 0 },
  63. };
  64. static int get_chs(unsigned long size, unsigned short *cyl, unsigned char *head,
  65. unsigned char *sec)
  66. {
  67. int k;
  68. int found = 0;
  69. k = 0;
  70. while (chs_table[k].size > 0 && size > chs_table[k].size)
  71. k++;
  72. if (chs_table[k].size > 0) {
  73. if (cyl)
  74. *cyl = chs_table[k].cyl;
  75. if (head)
  76. *head = chs_table[k].head;
  77. if (sec)
  78. *sec = chs_table[k].sec;
  79. found = 1;
  80. }
  81. return found;
  82. }
  83. /* These bytes are the signature for the CIS/IDI sector */
  84. static const uint8_t cis_numbers[] = {
  85. 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
  86. };
  87. /* Read and check for a valid CIS sector */
  88. static int get_valid_cis_sector(struct mtd_info *mtd)
  89. {
  90. int ret, k, cis_sector;
  91. size_t retlen;
  92. loff_t offset;
  93. uint8_t *sect_buf;
  94. cis_sector = -1;
  95. sect_buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  96. if (!sect_buf)
  97. goto out;
  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. for (k = 0, offset = 0; k < 4; k++, offset += mtd->erasesize) {
  104. if (mtd_block_isbad(mtd, offset)) {
  105. ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen,
  106. sect_buf);
  107. /* CIS pattern match on the sector buffer */
  108. if (ret < 0 || retlen != SECTOR_SIZE) {
  109. printk(KERN_WARNING
  110. "SSFDC_RO:can't read CIS/IDI sector\n");
  111. } else if (!memcmp(sect_buf, cis_numbers,
  112. sizeof(cis_numbers))) {
  113. /* Found */
  114. cis_sector = (int)(offset >> SECTOR_SHIFT);
  115. } else {
  116. pr_debug("SSFDC_RO: CIS/IDI sector not found"
  117. " on %s (mtd%d)\n", mtd->name,
  118. mtd->index);
  119. }
  120. break;
  121. }
  122. }
  123. kfree(sect_buf);
  124. out:
  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_OPS_RAW;
  145. ops.ooboffs = 0;
  146. ops.ooblen = OOB_SIZE;
  147. ops.oobbuf = buf;
  148. ops.datbuf = NULL;
  149. ret = mtd_read_oob(mtd, offs, &ops);
  150. if (ret < 0 || ops.oobretlen != OOB_SIZE)
  151. return -1;
  152. return 0;
  153. }
  154. /* Parity calculator on a word of n bit size */
  155. static int get_parity(int number, int size)
  156. {
  157. int k;
  158. int parity;
  159. parity = 1;
  160. for (k = 0; k < size; k++) {
  161. parity += (number >> k);
  162. parity &= 1;
  163. }
  164. return parity;
  165. }
  166. /* Read and validate the logical block address field stored in the OOB */
  167. static int get_logical_address(uint8_t *oob_buf)
  168. {
  169. int block_address, parity;
  170. int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */
  171. int j;
  172. int ok = 0;
  173. /*
  174. * Look for the first valid logical address
  175. * Valid address has fixed pattern on most significant bits and
  176. * parity check
  177. */
  178. for (j = 0; j < ARRAY_SIZE(offset); j++) {
  179. block_address = ((int)oob_buf[offset[j]] << 8) |
  180. oob_buf[offset[j]+1];
  181. /* Check for the signature bits in the address field (MSBits) */
  182. if ((block_address & ~0x7FF) == 0x1000) {
  183. parity = block_address & 0x01;
  184. block_address &= 0x7FF;
  185. block_address >>= 1;
  186. if (get_parity(block_address, 10) != parity) {
  187. pr_debug("SSFDC_RO: logical address field%d"
  188. "parity error(0x%04X)\n", j+1,
  189. block_address);
  190. } else {
  191. ok = 1;
  192. break;
  193. }
  194. }
  195. }
  196. if (!ok)
  197. block_address = -2;
  198. pr_debug("SSFDC_RO: get_logical_address() %d\n",
  199. block_address);
  200. return block_address;
  201. }
  202. /* Build the logic block map */
  203. static int build_logical_block_map(struct ssfdcr_record *ssfdc)
  204. {
  205. unsigned long offset;
  206. uint8_t oob_buf[OOB_SIZE];
  207. int ret, block_address, phys_block;
  208. struct mtd_info *mtd = ssfdc->mbd.mtd;
  209. pr_debug("SSFDC_RO: build_block_map() nblks=%d (%luK)\n",
  210. ssfdc->map_len,
  211. (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024);
  212. /* Scan every physical block, skip CIS block */
  213. for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len;
  214. phys_block++) {
  215. offset = (unsigned long)phys_block * ssfdc->erase_size;
  216. if (mtd_block_isbad(mtd, offset))
  217. continue; /* skip bad blocks */
  218. ret = read_raw_oob(mtd, offset, oob_buf);
  219. if (ret < 0) {
  220. pr_debug("SSFDC_RO: mtd read_oob() failed at %lu\n",
  221. offset);
  222. return -1;
  223. }
  224. block_address = get_logical_address(oob_buf);
  225. /* Skip invalid addresses */
  226. if (block_address >= 0 &&
  227. block_address < MAX_LOGIC_BLK_PER_ZONE) {
  228. int zone_index;
  229. zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE;
  230. block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE;
  231. ssfdc->logic_block_map[block_address] =
  232. (unsigned short)phys_block;
  233. pr_debug("SSFDC_RO: build_block_map() phys_block=%d,"
  234. "logic_block_addr=%d, zone=%d\n",
  235. phys_block, block_address, zone_index);
  236. }
  237. }
  238. return 0;
  239. }
  240. static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  241. {
  242. struct ssfdcr_record *ssfdc;
  243. int cis_sector;
  244. /* Check for small page NAND flash */
  245. if (!mtd_type_is_nand(mtd) || mtd->oobsize != OOB_SIZE ||
  246. mtd->size > UINT_MAX)
  247. return;
  248. /* Check for SSDFC format by reading CIS/IDI sector */
  249. cis_sector = get_valid_cis_sector(mtd);
  250. if (cis_sector == -1)
  251. return;
  252. ssfdc = kzalloc(sizeof(struct ssfdcr_record), GFP_KERNEL);
  253. if (!ssfdc)
  254. return;
  255. ssfdc->mbd.mtd = mtd;
  256. ssfdc->mbd.devnum = -1;
  257. ssfdc->mbd.tr = tr;
  258. ssfdc->mbd.readonly = 1;
  259. ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT);
  260. ssfdc->erase_size = mtd->erasesize;
  261. ssfdc->map_len = (u32)mtd->size / mtd->erasesize;
  262. pr_debug("SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n",
  263. ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len,
  264. DIV_ROUND_UP(ssfdc->map_len, MAX_PHYS_BLK_PER_ZONE));
  265. /* Set geometry */
  266. ssfdc->heads = 16;
  267. ssfdc->sectors = 32;
  268. get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors);
  269. ssfdc->cylinders = (unsigned short)(((u32)mtd->size >> SECTOR_SHIFT) /
  270. ((long)ssfdc->sectors * (long)ssfdc->heads));
  271. pr_debug("SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n",
  272. ssfdc->cylinders, ssfdc->heads , ssfdc->sectors,
  273. (long)ssfdc->cylinders * (long)ssfdc->heads *
  274. (long)ssfdc->sectors);
  275. ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders *
  276. (long)ssfdc->sectors;
  277. /* Allocate logical block map */
  278. ssfdc->logic_block_map =
  279. kmalloc_array(ssfdc->map_len,
  280. sizeof(ssfdc->logic_block_map[0]), GFP_KERNEL);
  281. if (!ssfdc->logic_block_map)
  282. goto out_err;
  283. memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) *
  284. ssfdc->map_len);
  285. /* Build logical block map */
  286. if (build_logical_block_map(ssfdc) < 0)
  287. goto out_err;
  288. /* Register device + partitions */
  289. if (add_mtd_blktrans_dev(&ssfdc->mbd))
  290. goto out_err;
  291. printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n",
  292. ssfdc->mbd.devnum + 'a', mtd->index, mtd->name);
  293. return;
  294. out_err:
  295. kfree(ssfdc->logic_block_map);
  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 <[email protected]>");
  372. MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card");