sm_common.c 4.6 KB

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