sm_common.c 5.8 KB

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