113-mtd-nand-add-pst.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. From bec69bb8e85151729014d859106dcc3fe652b1d4 Mon Sep 17 00:00:00 2001
  2. From: Boris BREZILLON <boris.brezillon@free-electrons.com>
  3. Date: Mon, 28 Jul 2014 14:45:40 +0200
  4. Subject: [PATCH] mtd: nand: Add page status table (pst)
  5. Page status table is an byte array storing pages status.
  6. It defines 3 status:
  7. - unknown: the page has not been read yet and we do not know its current
  8. state
  9. - empty: the page contains only FFs
  10. - filled: the page has been filled with data
  11. Care must be taken: an empty page does not mean it can be written, because
  12. it might have already been written with only FFs.
  13. These page status are useful to check wether the controller should try to
  14. correct errors (using ECC) or a derandomize data (using a randomizer
  15. block).
  16. Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
  17. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  18. ---
  19. drivers/mtd/nand/nand_base.c | 154 +++++++++++++++++++++++++++++++++++++++++++
  20. include/linux/mtd/nand.h | 21 ++++++
  21. 2 files changed, 175 insertions(+)
  22. --- a/drivers/mtd/nand/nand_base.c
  23. +++ b/drivers/mtd/nand/nand_base.c
  24. @@ -1102,6 +1102,138 @@ out:
  25. EXPORT_SYMBOL(nand_lock);
  26. /**
  27. + * nand_page_is_empty - check wether a NAND page contains only FFs
  28. + * @mtd: mtd info
  29. + * @data: data buffer
  30. + * @oob: oob buffer
  31. + *
  32. + * Reads the data stored in the databuf buffer and check if it contains only
  33. + * FFs.
  34. + *
  35. + * Return true if it does else return false.
  36. + */
  37. +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob)
  38. +{
  39. + u8 *buf;
  40. + int length;
  41. + u32 pattern = 0xffffffff;
  42. + int bitflips = 0;
  43. + int cnt;
  44. +
  45. + buf = data;
  46. + length = mtd->writesize;
  47. + while (length) {
  48. + cnt = length < sizeof(pattern) ? length : sizeof(pattern);
  49. + if (memcmp(&pattern, buf, cnt)) {
  50. + int i;
  51. + for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
  52. + if (!(buf[i / BITS_PER_BYTE] &
  53. + (1 << (i % BITS_PER_BYTE)))) {
  54. + bitflips++;
  55. + if (bitflips > mtd->ecc_strength)
  56. + return false;
  57. + }
  58. + }
  59. + }
  60. +
  61. + buf += sizeof(pattern);
  62. + length -= sizeof(pattern);
  63. + }
  64. +
  65. + buf = oob;
  66. + length = mtd->oobsize;
  67. + while (length) {
  68. + cnt = length < sizeof(pattern) ? length : sizeof(pattern);
  69. + if (memcmp(&pattern, buf, cnt)) {
  70. + int i;
  71. + for (i = 0; i < cnt * BITS_PER_BYTE; i++) {
  72. + if (!(buf[i / BITS_PER_BYTE] &
  73. + (1 << (i % BITS_PER_BYTE)))) {
  74. + bitflips++;
  75. + if (bitflips > mtd->ecc_strength)
  76. + return false;
  77. + }
  78. + }
  79. + }
  80. +
  81. + buf += sizeof(pattern);
  82. + length -= sizeof(pattern);
  83. + }
  84. +
  85. + return true;
  86. +}
  87. +EXPORT_SYMBOL(nand_page_is_empty);
  88. +
  89. +/**
  90. + * nand_page_get_status - retrieve page status from the page status table (pst)
  91. + * @mtd: mtd info
  92. + * @page: page you want to get status on
  93. + *
  94. + * Return the page status.
  95. + */
  96. +int nand_page_get_status(struct mtd_info *mtd, int page)
  97. +{
  98. + struct nand_chip *chip = mtd->priv;
  99. + u8 shift = (page % 4) * 2;
  100. + uint64_t offset = page / 4;
  101. + int ret = NAND_PAGE_STATUS_UNKNOWN;
  102. +
  103. + if (chip->pst)
  104. + ret = (chip->pst[offset] >> shift) & 0x3;
  105. +
  106. + return ret;
  107. +}
  108. +EXPORT_SYMBOL(nand_page_get_status);
  109. +
  110. +/**
  111. + * nand_page_set_status - assign page status from in the page status table
  112. + * @mtd: mtd info
  113. + * @page: page you want to get status on
  114. + * @status: new status to assign
  115. + */
  116. +void nand_page_set_status(struct mtd_info *mtd, int page,
  117. + enum nand_page_status status)
  118. +{
  119. + struct nand_chip *chip = mtd->priv;
  120. + u8 shift;
  121. + uint64_t offset;
  122. +
  123. + if (!chip->pst)
  124. + return;
  125. +
  126. + shift = (page % 4) * 2;
  127. + offset = page / 4;
  128. + chip->pst[offset] &= ~(0x3 << shift);
  129. + chip->pst[offset] |= (status & 0x3) << shift;
  130. +}
  131. +EXPORT_SYMBOL(nand_page_set_status);
  132. +
  133. +/**
  134. + * nand_pst_create - create a page status table
  135. + * @mtd: mtd info
  136. + *
  137. + * Allocate a page status table and assign it to the mtd device.
  138. + *
  139. + * Returns 0 in case of success or -ERRNO in case of error.
  140. + */
  141. +int nand_pst_create(struct mtd_info *mtd)
  142. +{
  143. + struct nand_chip *chip = mtd->priv;
  144. +
  145. + if (chip->pst)
  146. + return 0;
  147. +
  148. + chip->pst = kzalloc(mtd->size >>
  149. + (chip->page_shift + mtd->subpage_sft + 2),
  150. + GFP_KERNEL);
  151. + if (!chip->pst)
  152. + return -ENOMEM;
  153. +
  154. + return 0;
  155. +}
  156. +EXPORT_SYMBOL(nand_pst_create);
  157. +
  158. +/**
  159. * nand_read_page_raw - [INTERN] read raw page data without ecc
  160. * @mtd: mtd info structure
  161. * @chip: nand chip info structure
  162. @@ -2539,6 +2671,7 @@ static int nand_do_write_ops(struct mtd_
  163. uint8_t *wbuf = buf;
  164. int use_bufpoi;
  165. int part_pagewr = (column || writelen < (mtd->writesize - 1));
  166. + int subpage;
  167. if (part_pagewr)
  168. use_bufpoi = 1;
  169. @@ -2574,6 +2707,14 @@ static int nand_do_write_ops(struct mtd_
  170. if (ret)
  171. break;
  172. + for (subpage = column / chip->subpagesize;
  173. + subpage < (column + writelen) / chip->subpagesize;
  174. + subpage++)
  175. + nand_page_set_status(mtd,
  176. + (page << mtd->subpage_sft) +
  177. + subpage,
  178. + NAND_PAGE_FILLED);
  179. +
  180. writelen -= bytes;
  181. if (!writelen)
  182. break;
  183. @@ -2979,6 +3120,7 @@ int nand_erase_nand(struct mtd_info *mtd
  184. int page, status, pages_per_block, ret, chipnr;
  185. struct nand_chip *chip = mtd->priv;
  186. loff_t len;
  187. + int i;
  188. pr_debug("%s: start = 0x%012llx, len = %llu\n",
  189. __func__, (unsigned long long)instr->addr,
  190. @@ -3051,6 +3193,18 @@ int nand_erase_nand(struct mtd_info *mtd
  191. goto erase_exit;
  192. }
  193. + for (i = 0; i < pages_per_block; i++) {
  194. + int subpage;
  195. + for (subpage = 0;
  196. + subpage < 1 << mtd->subpage_sft;
  197. + subpage++) {
  198. + nand_page_set_status(mtd,
  199. + ((page + i) << mtd->subpage_sft) +
  200. + subpage,
  201. + NAND_PAGE_EMPTY);
  202. + }
  203. + }
  204. +
  205. /* Increment page address and decrement length */
  206. len -= (1ULL << chip->phys_erase_shift);
  207. page += pages_per_block;
  208. --- a/include/linux/mtd/nand.h
  209. +++ b/include/linux/mtd/nand.h
  210. @@ -521,6 +521,24 @@ struct nand_ecc_ctrl {
  211. int page);
  212. };
  213. +/*
  214. + * Constants for page status
  215. + */
  216. +enum nand_page_status {
  217. + NAND_PAGE_STATUS_UNKNOWN,
  218. + NAND_PAGE_EMPTY,
  219. + NAND_PAGE_FILLED,
  220. +};
  221. +
  222. +bool nand_page_is_empty(struct mtd_info *mtd, void *data, void *oob);
  223. +
  224. +int nand_page_get_status(struct mtd_info *mtd, int page);
  225. +
  226. +void nand_page_set_status(struct mtd_info *mtd, int page,
  227. + enum nand_page_status status);
  228. +
  229. +int nand_pst_create(struct mtd_info *mtd);
  230. +
  231. /**
  232. * struct nand_buffers - buffer structure for read/write
  233. * @ecccalc: buffer pointer for calculated ECC, size is oobsize.
  234. @@ -630,6 +648,7 @@ struct nand_buffers {
  235. * @bbt_md: [REPLACEABLE] bad block table mirror descriptor
  236. * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for initial
  237. * bad block scan.
  238. + * @pst: [INTERN] page status table
  239. * @controller: [REPLACEABLE] a pointer to a hardware controller
  240. * structure which is shared among multiple independent
  241. * devices.
  242. @@ -718,6 +737,8 @@ struct nand_chip {
  243. struct nand_bbt_descr *badblock_pattern;
  244. + uint8_t *pst;
  245. +
  246. struct list_head partitions;
  247. struct mutex part_lock;