sm_common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2009 - Maxim Levitsky
  4. * Common routines & support for xD format
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/mtd/rawnand.h>
  8. #include <linux/module.h>
  9. #include <linux/sizes.h>
  10. #include "sm_common.h"
  11. static int oob_sm_ooblayout_ecc(struct mtd_info *mtd, int section,
  12. struct mtd_oob_region *oobregion)
  13. {
  14. if (section > 1)
  15. return -ERANGE;
  16. oobregion->length = 3;
  17. oobregion->offset = ((section + 1) * 8) - 3;
  18. return 0;
  19. }
  20. static int oob_sm_ooblayout_free(struct mtd_info *mtd, int section,
  21. struct mtd_oob_region *oobregion)
  22. {
  23. switch (section) {
  24. case 0:
  25. /* reserved */
  26. oobregion->offset = 0;
  27. oobregion->length = 4;
  28. break;
  29. case 1:
  30. /* LBA1 */
  31. oobregion->offset = 6;
  32. oobregion->length = 2;
  33. break;
  34. case 2:
  35. /* LBA2 */
  36. oobregion->offset = 11;
  37. oobregion->length = 2;
  38. break;
  39. default:
  40. return -ERANGE;
  41. }
  42. return 0;
  43. }
  44. static const struct mtd_ooblayout_ops oob_sm_ops = {
  45. .ecc = oob_sm_ooblayout_ecc,
  46. .free = oob_sm_ooblayout_free,
  47. };
  48. /* NOTE: This layout is not compatabable with SmartMedia, */
  49. /* because the 256 byte devices have page depenent oob layout */
  50. /* However it does preserve the bad block markers */
  51. /* If you use smftl, it will bypass this and work correctly */
  52. /* If you not, then you break SmartMedia compliance anyway */
  53. static int oob_sm_small_ooblayout_ecc(struct mtd_info *mtd, int section,
  54. struct mtd_oob_region *oobregion)
  55. {
  56. if (section)
  57. return -ERANGE;
  58. oobregion->length = 3;
  59. oobregion->offset = 0;
  60. return 0;
  61. }
  62. static int oob_sm_small_ooblayout_free(struct mtd_info *mtd, int section,
  63. struct mtd_oob_region *oobregion)
  64. {
  65. switch (section) {
  66. case 0:
  67. /* reserved */
  68. oobregion->offset = 3;
  69. oobregion->length = 2;
  70. break;
  71. case 1:
  72. /* LBA1 */
  73. oobregion->offset = 6;
  74. oobregion->length = 2;
  75. break;
  76. default:
  77. return -ERANGE;
  78. }
  79. return 0;
  80. }
  81. static const struct mtd_ooblayout_ops oob_sm_small_ops = {
  82. .ecc = oob_sm_small_ooblayout_ecc,
  83. .free = oob_sm_small_ooblayout_free,
  84. };
  85. static int sm_block_markbad(struct nand_chip *chip, loff_t ofs)
  86. {
  87. struct mtd_info *mtd = nand_to_mtd(chip);
  88. struct mtd_oob_ops ops = { };
  89. struct sm_oob oob;
  90. int ret;
  91. memset(&oob, -1, SM_OOB_SIZE);
  92. oob.block_status = 0x0F;
  93. /* As long as this function is called on erase block boundaries
  94. it will work correctly for 256 byte nand */
  95. ops.mode = MTD_OPS_PLACE_OOB;
  96. ops.ooboffs = 0;
  97. ops.ooblen = mtd->oobsize;
  98. ops.oobbuf = (void *)&oob;
  99. ops.datbuf = NULL;
  100. ret = mtd_write_oob(mtd, ofs, &ops);
  101. if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
  102. pr_notice("sm_common: can't mark sector at %i as bad\n",
  103. (int)ofs);
  104. return -EIO;
  105. }
  106. return 0;
  107. }
  108. static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
  109. LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM),
  110. LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0),
  111. LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0),
  112. LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0),
  113. LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM),
  114. LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0),
  115. LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM),
  116. LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
  117. LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM),
  118. LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
  119. LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM),
  120. LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
  121. LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM),
  122. LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
  123. LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM),
  124. LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0),
  125. LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM),
  126. {NULL}
  127. };
  128. static struct nand_flash_dev nand_xd_flash_ids[] = {
  129. LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
  130. LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
  131. LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
  132. LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
  133. LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD),
  134. LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD),
  135. LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD),
  136. LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD),
  137. {NULL}
  138. };
  139. static int sm_attach_chip(struct nand_chip *chip)
  140. {
  141. struct mtd_info *mtd = nand_to_mtd(chip);
  142. /* Bad block marker position */
  143. chip->badblockpos = 0x05;
  144. chip->badblockbits = 7;
  145. chip->legacy.block_markbad = sm_block_markbad;
  146. /* ECC layout */
  147. if (mtd->writesize == SM_SECTOR_SIZE)
  148. mtd_set_ooblayout(mtd, &oob_sm_ops);
  149. else if (mtd->writesize == SM_SMALL_PAGE)
  150. mtd_set_ooblayout(mtd, &oob_sm_small_ops);
  151. else
  152. return -ENODEV;
  153. return 0;
  154. }
  155. static const struct nand_controller_ops sm_controller_ops = {
  156. .attach_chip = sm_attach_chip,
  157. };
  158. int sm_register_device(struct mtd_info *mtd, int smartmedia)
  159. {
  160. struct nand_chip *chip = mtd_to_nand(mtd);
  161. struct nand_flash_dev *flash_ids;
  162. int ret;
  163. chip->options |= NAND_SKIP_BBTSCAN;
  164. /* Scan for card properties */
  165. chip->legacy.dummy_controller.ops = &sm_controller_ops;
  166. flash_ids = smartmedia ? nand_smartmedia_flash_ids : nand_xd_flash_ids;
  167. ret = nand_scan_with_ids(chip, 1, flash_ids);
  168. if (ret)
  169. return ret;
  170. ret = mtd_device_register(mtd, NULL, 0);
  171. if (ret)
  172. nand_cleanup(chip);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL_GPL(sm_register_device);
  176. MODULE_LICENSE("GPL");
  177. MODULE_AUTHOR("Maxim Levitsky <[email protected]>");
  178. MODULE_DESCRIPTION("Common SmartMedia/xD functions");