sm_common.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/nand.h>
  11. #include "sm_common.h"
  12. static struct nand_ecclayout nand_oob_sm = {
  13. .eccbytes = 6,
  14. .eccpos = {8, 9, 10, 13, 14, 15},
  15. .oobfree = {
  16. {.offset = 0 , .length = 4}, /* reserved */
  17. {.offset = 6 , .length = 2}, /* LBA1 */
  18. {.offset = 11, .length = 2} /* LBA2 */
  19. }
  20. };
  21. /* NOTE: This layout is is not compatabable with SmartMedia, */
  22. /* because the 256 byte devices have page depenent oob layout */
  23. /* However it does preserve the bad block markers */
  24. /* If you use smftl, it will bypass this and work correctly */
  25. /* If you not, then you break SmartMedia compliance anyway */
  26. static struct nand_ecclayout nand_oob_sm_small = {
  27. .eccbytes = 3,
  28. .eccpos = {0, 1, 2},
  29. .oobfree = {
  30. {.offset = 3 , .length = 2}, /* reserved */
  31. {.offset = 6 , .length = 2}, /* LBA1 */
  32. }
  33. };
  34. static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs)
  35. {
  36. struct mtd_oob_ops ops;
  37. struct sm_oob oob;
  38. int ret, error = 0;
  39. memset(&oob, -1, SM_OOB_SIZE);
  40. oob.block_status = 0x0F;
  41. /* As long as this function is called on erase block boundaries
  42. it will work correctly for 256 byte nand */
  43. ops.mode = MTD_OOB_PLACE;
  44. ops.ooboffs = 0;
  45. ops.ooblen = mtd->oobsize;
  46. ops.oobbuf = (void *)&oob;
  47. ops.datbuf = NULL;
  48. ret = mtd->write_oob(mtd, ofs, &ops);
  49. if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
  50. printk(KERN_NOTICE
  51. "sm_common: can't mark sector at %i as bad\n",
  52. (int)ofs);
  53. error = -EIO;
  54. } else
  55. mtd->ecc_stats.badblocks++;
  56. return error;
  57. }
  58. static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
  59. {"SmartMedia 1MiB 5V", 0x6e, 256, 1, 0x1000, 0},
  60. {"SmartMedia 1MiB 3,3V", 0xe8, 256, 1, 0x1000, 0},
  61. {"SmartMedia 1MiB 3,3V", 0xec, 256, 1, 0x1000, 0},
  62. {"SmartMedia 2MiB 3,3V", 0xea, 256, 2, 0x1000, 0},
  63. {"SmartMedia 2MiB 5V", 0x64, 256, 2, 0x1000, 0},
  64. {"SmartMedia 2MiB 3,3V ROM", 0x5d, 512, 2, 0x2000, NAND_ROM},
  65. {"SmartMedia 4MiB 3,3V", 0xe3, 512, 4, 0x2000, 0},
  66. {"SmartMedia 4MiB 3,3/5V", 0xe5, 512, 4, 0x2000, 0},
  67. {"SmartMedia 4MiB 5V", 0x6b, 512, 4, 0x2000, 0},
  68. {"SmartMedia 4MiB 3,3V ROM", 0xd5, 512, 4, 0x2000, NAND_ROM},
  69. {"SmartMedia 8MiB 3,3V", 0xe6, 512, 8, 0x2000, 0},
  70. {"SmartMedia 8MiB 3,3V ROM", 0xd6, 512, 8, 0x2000, NAND_ROM},
  71. {"SmartMedia 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0},
  72. {"SmartMedia 16MiB 3,3V ROM", 0x57, 512, 16, 0x4000, NAND_ROM},
  73. {"SmartMedia 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0},
  74. {"SmartMedia 32MiB 3,3V ROM", 0x58, 512, 32, 0x4000, NAND_ROM},
  75. {"SmartMedia 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0},
  76. {"SmartMedia 64MiB 3,3V ROM", 0xd9, 512, 64, 0x4000, NAND_ROM},
  77. {"SmartMedia 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0},
  78. {"SmartMedia 128MiB 3,3V ROM", 0xda, 512, 128, 0x4000, NAND_ROM},
  79. {"SmartMedia 256MiB 3,3V", 0x71, 512, 256, 0x4000 },
  80. {"SmartMedia 256MiB 3,3V ROM", 0x5b, 512, 256, 0x4000, NAND_ROM},
  81. {NULL,}
  82. };
  83. #define XD_TYPEM (NAND_NO_AUTOINCR | NAND_BROKEN_XD)
  84. static struct nand_flash_dev nand_xd_flash_ids[] = {
  85. {"xD 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0},
  86. {"xD 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0},
  87. {"xD 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0},
  88. {"xD 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0},
  89. {"xD 256MiB 3,3V", 0x71, 512, 256, 0x4000, XD_TYPEM},
  90. {"xD 512MiB 3,3V", 0xdc, 512, 512, 0x4000, XD_TYPEM},
  91. {"xD 1GiB 3,3V", 0xd3, 512, 1024, 0x4000, XD_TYPEM},
  92. {"xD 2GiB 3,3V", 0xd5, 512, 2048, 0x4000, XD_TYPEM},
  93. {NULL,}
  94. };
  95. int sm_register_device(struct mtd_info *mtd, int smartmedia)
  96. {
  97. struct nand_chip *chip = mtd->priv;
  98. int ret;
  99. chip->options |= NAND_SKIP_BBTSCAN;
  100. /* Scan for card properties */
  101. ret = nand_scan_ident(mtd, 1, smartmedia ?
  102. nand_smartmedia_flash_ids : nand_xd_flash_ids);
  103. if (ret)
  104. return ret;
  105. /* Bad block marker postion */
  106. chip->badblockpos = 0x05;
  107. chip->badblockbits = 7;
  108. chip->block_markbad = sm_block_markbad;
  109. /* ECC layout */
  110. if (mtd->writesize == SM_SECTOR_SIZE)
  111. chip->ecc.layout = &nand_oob_sm;
  112. else if (mtd->writesize == SM_SMALL_PAGE)
  113. chip->ecc.layout = &nand_oob_sm_small;
  114. else
  115. return -ENODEV;
  116. ret = nand_scan_tail(mtd);
  117. if (ret)
  118. return ret;
  119. return add_mtd_device(mtd);
  120. }
  121. EXPORT_SYMBOL_GPL(sm_register_device);
  122. MODULE_LICENSE("GPL");
  123. MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
  124. MODULE_DESCRIPTION("Common SmartMedia/xD functions");