sm_common.c 4.5 KB

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