sm_common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. printk(KERN_NOTICE
  105. "sm_common: can't mark sector at %i as bad\n",
  106. (int)ofs);
  107. return -EIO;
  108. }
  109. return 0;
  110. }
  111. static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
  112. LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM),
  113. LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0),
  114. LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0),
  115. LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0),
  116. LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM),
  117. LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0),
  118. LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM),
  119. LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
  120. LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM),
  121. LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
  122. LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM),
  123. LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
  124. LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM),
  125. LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
  126. LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM),
  127. LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0),
  128. LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM),
  129. {NULL}
  130. };
  131. static struct nand_flash_dev nand_xd_flash_ids[] = {
  132. LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0),
  133. LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0),
  134. LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0),
  135. LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0),
  136. LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD),
  137. LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD),
  138. LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD),
  139. LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD),
  140. {NULL}
  141. };
  142. int sm_register_device(struct mtd_info *mtd, int smartmedia)
  143. {
  144. struct nand_chip *chip = mtd_to_nand(mtd);
  145. int ret;
  146. chip->options |= NAND_SKIP_BBTSCAN;
  147. /* Scan for card properties */
  148. ret = nand_scan_ident(mtd, 1, smartmedia ?
  149. nand_smartmedia_flash_ids : nand_xd_flash_ids);
  150. if (ret)
  151. return ret;
  152. /* Bad block marker position */
  153. chip->badblockpos = 0x05;
  154. chip->badblockbits = 7;
  155. chip->block_markbad = sm_block_markbad;
  156. /* ECC layout */
  157. if (mtd->writesize == SM_SECTOR_SIZE)
  158. mtd_set_ooblayout(mtd, &oob_sm_ops);
  159. else if (mtd->writesize == SM_SMALL_PAGE)
  160. mtd_set_ooblayout(mtd, &oob_sm_small_ops);
  161. else
  162. return -ENODEV;
  163. ret = nand_scan_tail(mtd);
  164. if (ret)
  165. return ret;
  166. return mtd_device_register(mtd, NULL, 0);
  167. }
  168. EXPORT_SYMBOL_GPL(sm_register_device);
  169. MODULE_LICENSE("GPL");
  170. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  171. MODULE_DESCRIPTION("Common SmartMedia/xD functions");