020-mtd-nand-pxa3xx_nand-add-support-for-partial-chunks.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  2. Date: Wed, 10 Feb 2016 14:54:21 +0100
  3. Subject: [PATCH] mtd: nand: pxa3xx_nand: add support for partial chunks
  4. This commit is needed to properly support the 8-bits ECC configuration
  5. with 4KB pages.
  6. When pages larger than 2 KB are used on platforms using the PXA3xx
  7. NAND controller, the reading/programming operations need to be split
  8. in chunks of 2 KBs or less because the controller FIFO is limited to
  9. about 2 KB (i.e a bit more than 2 KB to accommodate OOB data). Due to
  10. this requirement, the data layout on NAND is a bit strange, with ECC
  11. interleaved with data, at the end of each chunk.
  12. When a 4-bits ECC configuration is used with 4 KB pages, the physical
  13. data layout on the NAND looks like this:
  14. | 2048 data | 32 spare | 30 ECC | 2048 data | 32 spare | 30 ECC |
  15. So the data chunks have an equal size, 2080 bytes for each chunk,
  16. which the driver supports properly.
  17. When a 8-bits ECC configuration is used with 4KB pages, the physical
  18. data layout on the NAND looks like this:
  19. | 1024 data | 30 ECC | 1024 data | 30 ECC | 1024 data | 30 ECC | 1024 data | 30 ECC | 64 spare | 30 ECC |
  20. So, the spare area is stored in its own chunk, which has a different
  21. size than the other chunks. Since OOB is not used by UBIFS, the initial
  22. implementation of the driver has chosen to not support reading this
  23. additional "spare" chunk of data.
  24. Unfortunately, Marvell has chosen to store the BBT signature in the
  25. OOB area. Therefore, if the driver doesn't read this spare area, Linux
  26. has no way of finding the BBT. It thinks there is no BBT, and rewrites
  27. one, which U-Boot does not recognize, causing compatibility problems
  28. between the bootloader and the kernel in terms of NAND usage.
  29. To fix this, this commit implements the support for reading a partial
  30. last chunk. This support is currently only useful for the case of 8
  31. bits ECC with 4 KB pages, but it will be useful in the future to
  32. enable other configurations such as 12 bits and 16 bits ECC with 4 KB
  33. pages, or 8 bits ECC with 8 KB pages, etc. All those configurations
  34. have a "last" chunk that doesn't have the same size as the other
  35. chunks.
  36. In order to implement reading of the last chunk, this commit:
  37. - Adds a number of new fields to the pxa3xx_nand_info to describe how
  38. many full chunks and how many chunks we have, the size of full
  39. chunks and partial chunks, both in terms of data area and spare
  40. area.
  41. - Fills in the step_chunk_size and step_spare_size variables to
  42. describe how much data and spare should be read/written for the
  43. current read/program step.
  44. - Reworks the state machine to accommodate doing the additional read
  45. or program step when a last partial chunk is used.
  46. This commit has been tested on a Marvell Armada 398 DB board, with a
  47. 4KB page NAND, tested in both 4 bits ECC and 8 bits ECC
  48. configurations. Robert Jarzmik has tested on some PXA platforms.
  49. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  50. Tested-by: Robert Jarzmik <robert.jarzmik@free.fr>
  51. Acked-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
  52. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
  53. ---
  54. --- a/drivers/mtd/nand/pxa3xx_nand.c
  55. +++ b/drivers/mtd/nand/pxa3xx_nand.c
  56. @@ -228,15 +228,44 @@ struct pxa3xx_nand_info {
  57. int use_spare; /* use spare ? */
  58. int need_wait;
  59. - unsigned int data_size; /* data to be read from FIFO */
  60. - unsigned int chunk_size; /* split commands chunk size */
  61. - unsigned int oob_size;
  62. + /* Amount of real data per full chunk */
  63. + unsigned int chunk_size;
  64. +
  65. + /* Amount of spare data per full chunk */
  66. unsigned int spare_size;
  67. +
  68. + /* Number of full chunks (i.e chunk_size + spare_size) */
  69. + unsigned int nfullchunks;
  70. +
  71. + /*
  72. + * Total number of chunks. If equal to nfullchunks, then there
  73. + * are only full chunks. Otherwise, there is one last chunk of
  74. + * size (last_chunk_size + last_spare_size)
  75. + */
  76. + unsigned int ntotalchunks;
  77. +
  78. + /* Amount of real data in the last chunk */
  79. + unsigned int last_chunk_size;
  80. +
  81. + /* Amount of spare data in the last chunk */
  82. + unsigned int last_spare_size;
  83. +
  84. unsigned int ecc_size;
  85. unsigned int ecc_err_cnt;
  86. unsigned int max_bitflips;
  87. int retcode;
  88. + /*
  89. + * Variables only valid during command
  90. + * execution. step_chunk_size and step_spare_size is the
  91. + * amount of real data and spare data in the current
  92. + * chunk. cur_chunk is the current chunk being
  93. + * read/programmed.
  94. + */
  95. + unsigned int step_chunk_size;
  96. + unsigned int step_spare_size;
  97. + unsigned int cur_chunk;
  98. +
  99. /* cached register value */
  100. uint32_t reg_ndcr;
  101. uint32_t ndtr0cs0;
  102. @@ -531,25 +560,6 @@ static int pxa3xx_nand_init(struct pxa3x
  103. return 0;
  104. }
  105. -/*
  106. - * Set the data and OOB size, depending on the selected
  107. - * spare and ECC configuration.
  108. - * Only applicable to READ0, READOOB and PAGEPROG commands.
  109. - */
  110. -static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
  111. - struct mtd_info *mtd)
  112. -{
  113. - int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
  114. -
  115. - info->data_size = mtd->writesize;
  116. - if (!oob_enable)
  117. - return;
  118. -
  119. - info->oob_size = info->spare_size;
  120. - if (!info->use_ecc)
  121. - info->oob_size += info->ecc_size;
  122. -}
  123. -
  124. /**
  125. * NOTE: it is a must to set ND_RUN firstly, then write
  126. * command buffer, otherwise, it does not work.
  127. @@ -665,28 +675,28 @@ static void drain_fifo(struct pxa3xx_nan
  128. static void handle_data_pio(struct pxa3xx_nand_info *info)
  129. {
  130. - unsigned int do_bytes = min(info->data_size, info->chunk_size);
  131. -
  132. switch (info->state) {
  133. case STATE_PIO_WRITING:
  134. - writesl(info->mmio_base + NDDB,
  135. - info->data_buff + info->data_buff_pos,
  136. - DIV_ROUND_UP(do_bytes, 4));
  137. + if (info->step_chunk_size)
  138. + writesl(info->mmio_base + NDDB,
  139. + info->data_buff + info->data_buff_pos,
  140. + DIV_ROUND_UP(info->step_chunk_size, 4));
  141. - if (info->oob_size > 0)
  142. + if (info->step_spare_size)
  143. writesl(info->mmio_base + NDDB,
  144. info->oob_buff + info->oob_buff_pos,
  145. - DIV_ROUND_UP(info->oob_size, 4));
  146. + DIV_ROUND_UP(info->step_spare_size, 4));
  147. break;
  148. case STATE_PIO_READING:
  149. - drain_fifo(info,
  150. - info->data_buff + info->data_buff_pos,
  151. - DIV_ROUND_UP(do_bytes, 4));
  152. + if (info->step_chunk_size)
  153. + drain_fifo(info,
  154. + info->data_buff + info->data_buff_pos,
  155. + DIV_ROUND_UP(info->step_chunk_size, 4));
  156. - if (info->oob_size > 0)
  157. + if (info->step_spare_size)
  158. drain_fifo(info,
  159. info->oob_buff + info->oob_buff_pos,
  160. - DIV_ROUND_UP(info->oob_size, 4));
  161. + DIV_ROUND_UP(info->step_spare_size, 4));
  162. break;
  163. default:
  164. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  165. @@ -695,9 +705,8 @@ static void handle_data_pio(struct pxa3x
  166. }
  167. /* Update buffer pointers for multi-page read/write */
  168. - info->data_buff_pos += do_bytes;
  169. - info->oob_buff_pos += info->oob_size;
  170. - info->data_size -= do_bytes;
  171. + info->data_buff_pos += info->step_chunk_size;
  172. + info->oob_buff_pos += info->step_spare_size;
  173. }
  174. static void pxa3xx_nand_data_dma_irq(void *data)
  175. @@ -738,8 +747,9 @@ static void start_data_dma(struct pxa3xx
  176. info->state);
  177. BUG();
  178. }
  179. - info->sg.length = info->data_size +
  180. - (info->oob_size ? info->spare_size + info->ecc_size : 0);
  181. + info->sg.length = info->chunk_size;
  182. + if (info->use_spare)
  183. + info->sg.length += info->spare_size + info->ecc_size;
  184. dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
  185. tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
  186. @@ -900,9 +910,11 @@ static void prepare_start_command(struct
  187. /* reset data and oob column point to handle data */
  188. info->buf_start = 0;
  189. info->buf_count = 0;
  190. - info->oob_size = 0;
  191. info->data_buff_pos = 0;
  192. info->oob_buff_pos = 0;
  193. + info->step_chunk_size = 0;
  194. + info->step_spare_size = 0;
  195. + info->cur_chunk = 0;
  196. info->use_ecc = 0;
  197. info->use_spare = 1;
  198. info->retcode = ERR_NONE;
  199. @@ -914,8 +926,6 @@ static void prepare_start_command(struct
  200. case NAND_CMD_READ0:
  201. case NAND_CMD_PAGEPROG:
  202. info->use_ecc = 1;
  203. - case NAND_CMD_READOOB:
  204. - pxa3xx_set_datasize(info, mtd);
  205. break;
  206. case NAND_CMD_PARAM:
  207. info->use_spare = 0;
  208. @@ -974,6 +984,14 @@ static int prepare_set_command(struct px
  209. if (command == NAND_CMD_READOOB)
  210. info->buf_start += mtd->writesize;
  211. + if (info->cur_chunk < info->nfullchunks) {
  212. + info->step_chunk_size = info->chunk_size;
  213. + info->step_spare_size = info->spare_size;
  214. + } else {
  215. + info->step_chunk_size = info->last_chunk_size;
  216. + info->step_spare_size = info->last_spare_size;
  217. + }
  218. +
  219. /*
  220. * Multiple page read needs an 'extended command type' field,
  221. * which is either naked-read or last-read according to the
  222. @@ -985,8 +1003,8 @@ static int prepare_set_command(struct px
  223. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  224. | NDCB0_LEN_OVRD
  225. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  226. - info->ndcb3 = info->chunk_size +
  227. - info->oob_size;
  228. + info->ndcb3 = info->step_chunk_size +
  229. + info->step_spare_size;
  230. }
  231. set_command_address(info, mtd->writesize, column, page_addr);
  232. @@ -1006,8 +1024,6 @@ static int prepare_set_command(struct px
  233. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  234. | addr_cycle
  235. | command;
  236. - /* No data transfer in this case */
  237. - info->data_size = 0;
  238. exec_cmd = 1;
  239. }
  240. break;
  241. @@ -1019,6 +1035,14 @@ static int prepare_set_command(struct px
  242. break;
  243. }
  244. + if (info->cur_chunk < info->nfullchunks) {
  245. + info->step_chunk_size = info->chunk_size;
  246. + info->step_spare_size = info->spare_size;
  247. + } else {
  248. + info->step_chunk_size = info->last_chunk_size;
  249. + info->step_spare_size = info->last_spare_size;
  250. + }
  251. +
  252. /* Second command setting for large pages */
  253. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  254. /*
  255. @@ -1029,14 +1053,14 @@ static int prepare_set_command(struct px
  256. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  257. | NDCB0_LEN_OVRD
  258. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  259. - info->ndcb3 = info->chunk_size +
  260. - info->oob_size;
  261. + info->ndcb3 = info->step_chunk_size +
  262. + info->step_spare_size;
  263. /*
  264. * This is the command dispatch that completes a chunked
  265. * page program operation.
  266. */
  267. - if (info->data_size == 0) {
  268. + if (info->cur_chunk == info->ntotalchunks) {
  269. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  270. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  271. | command;
  272. @@ -1063,7 +1087,7 @@ static int prepare_set_command(struct px
  273. | command;
  274. info->ndcb1 = (column & 0xFF);
  275. info->ndcb3 = INIT_BUFFER_SIZE;
  276. - info->data_size = INIT_BUFFER_SIZE;
  277. + info->step_chunk_size = INIT_BUFFER_SIZE;
  278. break;
  279. case NAND_CMD_READID:
  280. @@ -1073,7 +1097,7 @@ static int prepare_set_command(struct px
  281. | command;
  282. info->ndcb1 = (column & 0xFF);
  283. - info->data_size = 8;
  284. + info->step_chunk_size = 8;
  285. break;
  286. case NAND_CMD_STATUS:
  287. info->buf_count = 1;
  288. @@ -1081,7 +1105,7 @@ static int prepare_set_command(struct px
  289. | NDCB0_ADDR_CYC(1)
  290. | command;
  291. - info->data_size = 8;
  292. + info->step_chunk_size = 8;
  293. break;
  294. case NAND_CMD_ERASE1:
  295. @@ -1220,6 +1244,7 @@ static void nand_cmdfunc_extended(struct
  296. init_completion(&info->dev_ready);
  297. do {
  298. info->state = STATE_PREPARED;
  299. +
  300. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  301. column, page_addr);
  302. if (!exec_cmd) {
  303. @@ -1239,22 +1264,30 @@ static void nand_cmdfunc_extended(struct
  304. break;
  305. }
  306. + /* Only a few commands need several steps */
  307. + if (command != NAND_CMD_PAGEPROG &&
  308. + command != NAND_CMD_READ0 &&
  309. + command != NAND_CMD_READOOB)
  310. + break;
  311. +
  312. + info->cur_chunk++;
  313. +
  314. /* Check if the sequence is complete */
  315. - if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
  316. + if (info->cur_chunk == info->ntotalchunks && command != NAND_CMD_PAGEPROG)
  317. break;
  318. /*
  319. * After a splitted program command sequence has issued
  320. * the command dispatch, the command sequence is complete.
  321. */
  322. - if (info->data_size == 0 &&
  323. + if (info->cur_chunk == (info->ntotalchunks + 1) &&
  324. command == NAND_CMD_PAGEPROG &&
  325. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  326. break;
  327. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  328. /* Last read: issue a 'last naked read' */
  329. - if (info->data_size == info->chunk_size)
  330. + if (info->cur_chunk == info->ntotalchunks - 1)
  331. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  332. else
  333. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  334. @@ -1264,7 +1297,7 @@ static void nand_cmdfunc_extended(struct
  335. * the command dispatch must be issued to complete.
  336. */
  337. } else if (command == NAND_CMD_PAGEPROG &&
  338. - info->data_size == 0) {
  339. + info->cur_chunk == info->ntotalchunks) {
  340. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  341. }
  342. } while (1);
  343. @@ -1514,6 +1547,8 @@ static int pxa_ecc_init(struct pxa3xx_na
  344. int strength, int ecc_stepsize, int page_size)
  345. {
  346. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  347. + info->nfullchunks = 1;
  348. + info->ntotalchunks = 1;
  349. info->chunk_size = 2048;
  350. info->spare_size = 40;
  351. info->ecc_size = 24;
  352. @@ -1522,6 +1557,8 @@ static int pxa_ecc_init(struct pxa3xx_na
  353. ecc->strength = 1;
  354. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  355. + info->nfullchunks = 1;
  356. + info->ntotalchunks = 1;
  357. info->chunk_size = 512;
  358. info->spare_size = 8;
  359. info->ecc_size = 8;
  360. @@ -1535,6 +1572,8 @@ static int pxa_ecc_init(struct pxa3xx_na
  361. */
  362. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  363. info->ecc_bch = 1;
  364. + info->nfullchunks = 1;
  365. + info->ntotalchunks = 1;
  366. info->chunk_size = 2048;
  367. info->spare_size = 32;
  368. info->ecc_size = 32;
  369. @@ -1545,6 +1584,8 @@ static int pxa_ecc_init(struct pxa3xx_na
  370. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  371. info->ecc_bch = 1;
  372. + info->nfullchunks = 2;
  373. + info->ntotalchunks = 2;
  374. info->chunk_size = 2048;
  375. info->spare_size = 32;
  376. info->ecc_size = 32;
  377. @@ -1559,8 +1600,12 @@ static int pxa_ecc_init(struct pxa3xx_na
  378. */
  379. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  380. info->ecc_bch = 1;
  381. + info->nfullchunks = 4;
  382. + info->ntotalchunks = 5;
  383. info->chunk_size = 1024;
  384. info->spare_size = 0;
  385. + info->last_chunk_size = 0;
  386. + info->last_spare_size = 64;
  387. info->ecc_size = 32;
  388. ecc->mode = NAND_ECC_HW;
  389. ecc->size = info->chunk_size;