yaffs_mtdif2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /* mtd interface for YAFFS2 */
  14. /* XXX U-BOOT XXX */
  15. #include <common.h>
  16. #include <linux/errno.h>
  17. #include "yportenv.h"
  18. #include "yaffs_trace.h"
  19. #include "yaffs_mtdif2.h"
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/types.h>
  22. #include <linux/time.h>
  23. #include "yaffs_trace.h"
  24. #include "yaffs_packedtags2.h"
  25. #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
  26. #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
  27. /* NB For use with inband tags....
  28. * We assume that the data buffer is of size total_bytes_per_chunk so
  29. * that we can also use it to load the tags.
  30. */
  31. int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  32. const u8 *data,
  33. const struct yaffs_ext_tags *tags)
  34. {
  35. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  36. struct mtd_oob_ops ops;
  37. int retval = 0;
  38. loff_t addr;
  39. struct yaffs_packed_tags2 pt;
  40. int packed_tags_size =
  41. dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  42. void *packed_tags_ptr =
  43. dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  44. yaffs_trace(YAFFS_TRACE_MTD,
  45. "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
  46. nand_chunk, data, tags);
  47. addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  48. /* For yaffs2 writing there must be both data and tags.
  49. * If we're using inband tags, then the tags are stuffed into
  50. * the end of the data buffer.
  51. */
  52. if (!data || !tags)
  53. BUG();
  54. else if (dev->param.inband_tags) {
  55. struct yaffs_packed_tags2_tags_only *pt2tp;
  56. pt2tp =
  57. (struct yaffs_packed_tags2_tags_only *)(data +
  58. dev->
  59. data_bytes_per_chunk);
  60. yaffs_pack_tags2_tags_only(pt2tp, tags);
  61. } else {
  62. yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
  63. }
  64. ops.mode = MTD_OPS_AUTO_OOB;
  65. ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
  66. ops.len = dev->param.total_bytes_per_chunk;
  67. ops.ooboffs = 0;
  68. ops.datbuf = (u8 *) data;
  69. ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
  70. retval = mtd_write_oob(mtd, addr, &ops);
  71. if (retval == 0)
  72. return YAFFS_OK;
  73. else
  74. return YAFFS_FAIL;
  75. }
  76. int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
  77. u8 *data, struct yaffs_ext_tags *tags)
  78. {
  79. struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
  80. u8 local_spare[128];
  81. struct mtd_oob_ops ops;
  82. size_t dummy;
  83. int retval = 0;
  84. int local_data = 0;
  85. struct yaffs_packed_tags2 pt;
  86. loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
  87. int packed_tags_size =
  88. dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
  89. void *packed_tags_ptr =
  90. dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
  91. yaffs_trace(YAFFS_TRACE_MTD,
  92. "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
  93. nand_chunk, data, tags);
  94. if (dev->param.inband_tags) {
  95. if (!data) {
  96. local_data = 1;
  97. data = yaffs_get_temp_buffer(dev);
  98. }
  99. }
  100. if (dev->param.inband_tags || (data && !tags))
  101. retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk,
  102. &dummy, data);
  103. else if (tags) {
  104. ops.mode = MTD_OPS_AUTO_OOB;
  105. ops.ooblen = packed_tags_size;
  106. ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
  107. ops.ooboffs = 0;
  108. ops.datbuf = data;
  109. ops.oobbuf = local_spare;
  110. retval = mtd_read_oob(mtd, addr, &ops);
  111. }
  112. if (dev->param.inband_tags) {
  113. if (tags) {
  114. struct yaffs_packed_tags2_tags_only *pt2tp;
  115. pt2tp =
  116. (struct yaffs_packed_tags2_tags_only *)
  117. &data[dev->data_bytes_per_chunk];
  118. yaffs_unpack_tags2_tags_only(tags, pt2tp);
  119. }
  120. } else {
  121. if (tags) {
  122. memcpy(packed_tags_ptr,
  123. local_spare,
  124. packed_tags_size);
  125. yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
  126. }
  127. }
  128. if (local_data)
  129. yaffs_release_temp_buffer(dev, data);
  130. if (tags && retval == -EBADMSG
  131. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  132. tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
  133. dev->n_ecc_unfixed++;
  134. }
  135. if (tags && retval == -EUCLEAN
  136. && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
  137. tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
  138. dev->n_ecc_fixed++;
  139. }
  140. if (retval == 0)
  141. return YAFFS_OK;
  142. else
  143. return YAFFS_FAIL;
  144. }
  145. int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
  146. {
  147. struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
  148. int retval;
  149. yaffs_trace(YAFFS_TRACE_MTD,
  150. "nandmtd2_MarkNANDBlockBad %d", blockNo);
  151. retval =
  152. mtd_block_markbad(mtd,
  153. blockNo * dev->param.chunks_per_block *
  154. dev->data_bytes_per_chunk);
  155. if (retval == 0)
  156. return YAFFS_OK;
  157. else
  158. return YAFFS_FAIL;
  159. }
  160. int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
  161. enum yaffs_block_state *state, u32 *sequenceNumber)
  162. {
  163. struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
  164. int retval;
  165. yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
  166. retval =
  167. mtd_block_isbad(mtd,
  168. blockNo * dev->param.chunks_per_block *
  169. dev->data_bytes_per_chunk);
  170. if (retval) {
  171. yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
  172. *state = YAFFS_BLOCK_STATE_DEAD;
  173. *sequenceNumber = 0;
  174. } else {
  175. struct yaffs_ext_tags t;
  176. nandmtd2_read_chunk_tags(dev,
  177. blockNo *
  178. dev->param.chunks_per_block, NULL,
  179. &t);
  180. if (t.chunk_used) {
  181. *sequenceNumber = t.seq_number;
  182. *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
  183. } else {
  184. *sequenceNumber = 0;
  185. *state = YAFFS_BLOCK_STATE_EMPTY;
  186. }
  187. }
  188. yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
  189. *sequenceNumber, *state);
  190. if (retval == 0)
  191. return YAFFS_OK;
  192. else
  193. return YAFFS_FAIL;
  194. }