yaffs_mtdif.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. /* XXX U-BOOT XXX */
  14. #include <common.h>
  15. #include "yportenv.h"
  16. #include "yaffs_mtdif.h"
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/types.h>
  19. #include <linux/time.h>
  20. #include <linux/mtd/rawnand.h>
  21. static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
  22. {
  23. oob[0] = spare->tb0;
  24. oob[1] = spare->tb1;
  25. oob[2] = spare->tb2;
  26. oob[3] = spare->tb3;
  27. oob[4] = spare->tb4;
  28. oob[5] = spare->tb5 & 0x3f;
  29. oob[5] |= spare->block_status == 'Y' ? 0 : 0x80;
  30. oob[5] |= spare->page_status == 0 ? 0 : 0x40;
  31. oob[6] = spare->tb6;
  32. oob[7] = spare->tb7;
  33. }
  34. static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
  35. {
  36. struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare;
  37. spare->tb0 = oob[0];
  38. spare->tb1 = oob[1];
  39. spare->tb2 = oob[2];
  40. spare->tb3 = oob[3];
  41. spare->tb4 = oob[4];
  42. spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
  43. spare->block_status = oob[5] & 0x80 ? 0xff : 'Y';
  44. spare->page_status = oob[5] & 0x40 ? 0xff : 0;
  45. spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
  46. spare->tb6 = oob[6];
  47. spare->tb7 = oob[7];
  48. spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
  49. nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
  50. }
  51. int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND,
  52. const u8 *data, const struct yaffs_spare *spare)
  53. {
  54. struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
  55. struct mtd_oob_ops ops;
  56. size_t dummy;
  57. int retval = 0;
  58. loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
  59. u8 spareAsBytes[8]; /* OOB */
  60. if (data && !spare)
  61. retval = mtd_write(mtd, addr, dev->data_bytes_per_chunk,
  62. &dummy, data);
  63. else if (spare) {
  64. if (dev->param.use_nand_ecc) {
  65. translate_spare2oob(spare, spareAsBytes);
  66. ops.mode = MTD_OPS_AUTO_OOB;
  67. ops.ooblen = 8; /* temp hack */
  68. } else {
  69. ops.mode = MTD_OPS_RAW;
  70. ops.ooblen = YAFFS_BYTES_PER_SPARE;
  71. }
  72. ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
  73. ops.datbuf = (u8 *)data;
  74. ops.ooboffs = 0;
  75. ops.oobbuf = spareAsBytes;
  76. retval = mtd_write_oob(mtd, addr, &ops);
  77. }
  78. if (retval == 0)
  79. return YAFFS_OK;
  80. else
  81. return YAFFS_FAIL;
  82. }
  83. int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data,
  84. struct yaffs_spare *spare)
  85. {
  86. struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
  87. struct mtd_oob_ops ops;
  88. size_t dummy;
  89. int retval = 0;
  90. loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
  91. u8 spareAsBytes[8]; /* OOB */
  92. if (data && !spare)
  93. retval = mtd_read(mtd, addr, dev->data_bytes_per_chunk,
  94. &dummy, data);
  95. else if (spare) {
  96. if (dev->param.use_nand_ecc) {
  97. ops.mode = MTD_OPS_AUTO_OOB;
  98. ops.ooblen = 8; /* temp hack */
  99. } else {
  100. ops.mode = MTD_OPS_RAW;
  101. ops.ooblen = YAFFS_BYTES_PER_SPARE;
  102. }
  103. ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
  104. ops.datbuf = data;
  105. ops.ooboffs = 0;
  106. ops.oobbuf = spareAsBytes;
  107. retval = mtd_read_oob(mtd, addr, &ops);
  108. if (dev->param.use_nand_ecc)
  109. translate_oob2spare(spare, spareAsBytes);
  110. }
  111. if (retval == 0)
  112. return YAFFS_OK;
  113. else
  114. return YAFFS_FAIL;
  115. }
  116. int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
  117. {
  118. struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
  119. __u32 addr =
  120. ((loff_t) blockNumber) * dev->data_bytes_per_chunk
  121. * dev->param.chunks_per_block;
  122. struct erase_info ei;
  123. int retval = 0;
  124. ei.mtd = mtd;
  125. ei.addr = addr;
  126. ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
  127. ei.time = 1000;
  128. ei.retries = 2;
  129. ei.callback = NULL;
  130. ei.priv = (u_long) dev;
  131. /* Todo finish off the ei if required */
  132. retval = mtd_erase(mtd, &ei);
  133. if (retval == 0)
  134. return YAFFS_OK;
  135. else
  136. return YAFFS_FAIL;
  137. }
  138. int nandmtd_InitialiseNAND(struct yaffs_dev *dev)
  139. {
  140. return YAFFS_OK;
  141. }