blob: c50523b085efa687a986a12695f3b4302305ad17 [file] [log] [blame]
Huang Shijie10a2bca2011-09-08 10:47:09 +08001/*
2 * Freescale GPMI NAND Flash Driver
3 *
Huang Shijie026918e2015-12-02 16:47:40 -06004 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
Huang Shijie10a2bca2011-09-08 10:47:09 +08005 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21#include <linux/clk.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
Wolfram Sangdf16c862011-11-23 15:57:06 +010024#include <linux/module.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080025#include <linux/mtd/partitions.h>
Huang Shijiee10db1f2012-05-04 21:42:05 -040026#include <linux/of.h>
27#include <linux/of_device.h>
Huang Shijiec50c6942012-07-03 16:24:32 +080028#include <linux/of_mtd.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080029#include "gpmi-nand.h"
Huang Shijieb8e29312014-01-03 11:01:42 +080030#include "bch-regs.h"
Huang Shijie10a2bca2011-09-08 10:47:09 +080031
Huang Shijie5de0b522012-10-13 13:03:29 -040032/* Resource names for the GPMI NAND driver. */
33#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
34#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
35#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
Huang Shijie5de0b522012-10-13 13:03:29 -040036
Huang Shijie10a2bca2011-09-08 10:47:09 +080037/* add our owner bbt descriptor */
38static uint8_t scan_ff_pattern[] = { 0xff };
39static struct nand_bbt_descr gpmi_bbt_descr = {
40 .options = 0,
41 .offs = 0,
42 .len = 1,
43 .pattern = scan_ff_pattern
44};
45
Huang Shijie7a2b89a2013-09-25 14:58:15 +080046/*
47 * We may change the layout if we can get the ECC info from the datasheet,
48 * else we will use all the (page + OOB).
49 */
Boris Brezillon3f158e42016-02-03 20:01:54 +010050static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
51 struct mtd_oob_region *oobregion)
52{
53 struct nand_chip *chip = mtd_to_nand(mtd);
54 struct gpmi_nand_data *this = nand_get_controller_data(chip);
55 struct bch_geometry *geo = &this->bch_geometry;
56
57 if (section)
58 return -ERANGE;
59
60 oobregion->offset = 0;
61 oobregion->length = geo->page_size - mtd->writesize;
62
63 return 0;
64}
65
66static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
67 struct mtd_oob_region *oobregion)
68{
69 struct nand_chip *chip = mtd_to_nand(mtd);
70 struct gpmi_nand_data *this = nand_get_controller_data(chip);
71 struct bch_geometry *geo = &this->bch_geometry;
72
73 if (section)
74 return -ERANGE;
75
76 /* The available oob size we have. */
77 if (geo->page_size < mtd->writesize + mtd->oobsize) {
78 oobregion->offset = geo->page_size - mtd->writesize;
79 oobregion->length = mtd->oobsize - oobregion->offset;
80 }
81
82 return 0;
83}
84
85static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
86 .ecc = gpmi_ooblayout_ecc,
87 .free = gpmi_ooblayout_free,
Huang Shijie10a2bca2011-09-08 10:47:09 +080088};
89
Huang Shijie6189ccc2014-03-21 18:19:39 +080090static const struct gpmi_devdata gpmi_devdata_imx23 = {
91 .type = IS_MX23,
92 .bch_max_ecc_strength = 20,
93 .max_chain_delay = 16,
94};
95
96static const struct gpmi_devdata gpmi_devdata_imx28 = {
97 .type = IS_MX28,
98 .bch_max_ecc_strength = 20,
99 .max_chain_delay = 16,
100};
101
102static const struct gpmi_devdata gpmi_devdata_imx6q = {
103 .type = IS_MX6Q,
104 .bch_max_ecc_strength = 40,
105 .max_chain_delay = 12,
106};
107
Huang Shijie91f54982014-03-27 10:43:22 +0800108static const struct gpmi_devdata gpmi_devdata_imx6sx = {
109 .type = IS_MX6SX,
110 .bch_max_ecc_strength = 62,
111 .max_chain_delay = 12,
112};
113
Huang Shijie10a2bca2011-09-08 10:47:09 +0800114static irqreturn_t bch_irq(int irq, void *cookie)
115{
116 struct gpmi_nand_data *this = cookie;
117
118 gpmi_clear_bch(this);
119 complete(&this->bch_done);
120 return IRQ_HANDLED;
121}
122
123/*
124 * Calculate the ECC strength by hand:
125 * E : The ECC strength.
126 * G : the length of Galois Field.
127 * N : The chunk count of per page.
128 * O : the oobsize of the NAND chip.
129 * M : the metasize of per page.
130 *
131 * The formula is :
132 * E * G * N
133 * ------------ <= (O - M)
134 * 8
135 *
136 * So, we get E by:
137 * (O - M) * 8
138 * E <= -------------
139 * G * N
140 */
141static inline int get_ecc_strength(struct gpmi_nand_data *this)
142{
143 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100144 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800145 int ecc_strength;
146
147 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
148 / (geo->gf_len * geo->ecc_chunk_count);
149
150 /* We need the minor even number. */
151 return round_down(ecc_strength, 2);
152}
153
Huang Shijie92d0e092013-01-29 09:23:38 +0800154static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
155{
156 struct bch_geometry *geo = &this->bch_geometry;
157
158 /* Do the sanity check. */
159 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
160 /* The mx23/mx28 only support the GF13. */
161 if (geo->gf_len == 14)
162 return false;
Huang Shijie92d0e092013-01-29 09:23:38 +0800163 }
Huang Shijie6189ccc2014-03-21 18:19:39 +0800164 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
Huang Shijie92d0e092013-01-29 09:23:38 +0800165}
166
Huang Shijie2febcdf2013-05-17 11:17:34 +0800167/*
168 * If we can get the ECC information from the nand chip, we do not
169 * need to calculate them ourselves.
170 *
171 * We may have available oob space in this case.
172 */
Han Xub8b0e462015-12-02 16:47:43 -0600173static int set_geometry_by_ecc_info(struct gpmi_nand_data *this)
Huang Shijie2febcdf2013-05-17 11:17:34 +0800174{
175 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100176 struct nand_chip *chip = &this->nand;
177 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie2febcdf2013-05-17 11:17:34 +0800178 unsigned int block_mark_bit_offset;
179
180 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
Han Xub8b0e462015-12-02 16:47:43 -0600181 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800182
183 switch (chip->ecc_step_ds) {
184 case SZ_512:
185 geo->gf_len = 13;
186 break;
187 case SZ_1K:
188 geo->gf_len = 14;
189 break;
190 default:
191 dev_err(this->dev,
192 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
193 chip->ecc_strength_ds, chip->ecc_step_ds);
Han Xub8b0e462015-12-02 16:47:43 -0600194 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800195 }
196 geo->ecc_chunk_size = chip->ecc_step_ds;
197 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
198 if (!gpmi_check_ecc(this))
Han Xub8b0e462015-12-02 16:47:43 -0600199 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800200
201 /* Keep the C >= O */
202 if (geo->ecc_chunk_size < mtd->oobsize) {
203 dev_err(this->dev,
204 "unsupported nand chip. ecc size: %d, oob size : %d\n",
205 chip->ecc_step_ds, mtd->oobsize);
Han Xub8b0e462015-12-02 16:47:43 -0600206 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800207 }
208
209 /* The default value, see comment in the legacy_set_geometry(). */
210 geo->metadata_size = 10;
211
212 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
213
214 /*
215 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
216 *
217 * | P |
218 * |<----------------------------------------------------->|
219 * | |
220 * | (Block Mark) |
221 * | P' | | | |
222 * |<-------------------------------------------->| D | | O' |
223 * | |<---->| |<--->|
224 * V V V V V
225 * +---+----------+-+----------+-+----------+-+----------+-+-----+
226 * | M | data |E| data |E| data |E| data |E| |
227 * +---+----------+-+----------+-+----------+-+----------+-+-----+
228 * ^ ^
229 * | O |
230 * |<------------>|
231 * | |
232 *
233 * P : the page size for BCH module.
234 * E : The ECC strength.
235 * G : the length of Galois Field.
236 * N : The chunk count of per page.
237 * M : the metasize of per page.
238 * C : the ecc chunk size, aka the "data" above.
239 * P': the nand chip's page size.
240 * O : the nand chip's oob size.
241 * O': the free oob.
242 *
243 * The formula for P is :
244 *
245 * E * G * N
246 * P = ------------ + P' + M
247 * 8
248 *
249 * The position of block mark moves forward in the ECC-based view
250 * of page, and the delta is:
251 *
252 * E * G * (N - 1)
253 * D = (---------------- + M)
254 * 8
255 *
256 * Please see the comment in legacy_set_geometry().
257 * With the condition C >= O , we still can get same result.
258 * So the bit position of the physical block mark within the ECC-based
259 * view of the page is :
260 * (P' - D) * 8
261 */
262 geo->page_size = mtd->writesize + geo->metadata_size +
263 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
264
Huang Shijie2febcdf2013-05-17 11:17:34 +0800265 geo->payload_size = mtd->writesize;
266
267 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
268 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
269 + ALIGN(geo->ecc_chunk_count, 4);
270
271 if (!this->swap_block_mark)
Han Xub8b0e462015-12-02 16:47:43 -0600272 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800273
274 /* For bit swap. */
275 block_mark_bit_offset = mtd->writesize * 8 -
276 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
277 + geo->metadata_size * 8);
278
279 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
280 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Han Xub8b0e462015-12-02 16:47:43 -0600281 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800282}
283
284static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800285{
286 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100287 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800288 unsigned int metadata_size;
289 unsigned int status_size;
290 unsigned int block_mark_bit_offset;
291
292 /*
293 * The size of the metadata can be changed, though we set it to 10
294 * bytes now. But it can't be too large, because we have to save
295 * enough space for BCH.
296 */
297 geo->metadata_size = 10;
298
299 /* The default for the length of Galois Field. */
300 geo->gf_len = 13;
301
Huang Shijie9ff16f02013-01-25 14:04:07 +0800302 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800303 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800304 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800305 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800306 geo->gf_len = 14;
307 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800308
309 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
310
311 /* We use the same ECC strength for all chunks. */
312 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800313 if (!gpmi_check_ecc(this)) {
314 dev_err(this->dev,
Han Xub8b0e462015-12-02 16:47:43 -0600315 "ecc strength: %d cannot be supported by the controller (%d)\n"
316 "try to use minimum ecc strength that NAND chip required\n",
Lothar Waßmannd8c03722014-06-12 15:20:42 +0200317 geo->ecc_strength,
Huang Shijie6189ccc2014-03-21 18:19:39 +0800318 this->devdata->bch_max_ecc_strength);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800319 return -EINVAL;
320 }
321
322 geo->page_size = mtd->writesize + mtd->oobsize;
323 geo->payload_size = mtd->writesize;
324
325 /*
326 * The auxiliary buffer contains the metadata and the ECC status. The
327 * metadata is padded to the nearest 32-bit boundary. The ECC status
328 * contains one byte for every ECC chunk, and is also padded to the
329 * nearest 32-bit boundary.
330 */
331 metadata_size = ALIGN(geo->metadata_size, 4);
332 status_size = ALIGN(geo->ecc_chunk_count, 4);
333
334 geo->auxiliary_size = metadata_size + status_size;
335 geo->auxiliary_status_offset = metadata_size;
336
337 if (!this->swap_block_mark)
338 return 0;
339
340 /*
341 * We need to compute the byte and bit offsets of
342 * the physical block mark within the ECC-based view of the page.
343 *
344 * NAND chip with 2K page shows below:
345 * (Block Mark)
346 * | |
347 * | D |
348 * |<---->|
349 * V V
350 * +---+----------+-+----------+-+----------+-+----------+-+
351 * | M | data |E| data |E| data |E| data |E|
352 * +---+----------+-+----------+-+----------+-+----------+-+
353 *
354 * The position of block mark moves forward in the ECC-based view
355 * of page, and the delta is:
356 *
357 * E * G * (N - 1)
358 * D = (---------------- + M)
359 * 8
360 *
361 * With the formula to compute the ECC strength, and the condition
362 * : C >= O (C is the ecc chunk size)
363 *
364 * It's easy to deduce to the following result:
365 *
366 * E * G (O - M) C - M C - M
367 * ----------- <= ------- <= -------- < ---------
368 * 8 N N (N - 1)
369 *
370 * So, we get:
371 *
372 * E * G * (N - 1)
373 * D = (---------------- + M) < C
374 * 8
375 *
376 * The above inequality means the position of block mark
377 * within the ECC-based view of the page is still in the data chunk,
378 * and it's NOT in the ECC bits of the chunk.
379 *
380 * Use the following to compute the bit position of the
381 * physical block mark within the ECC-based view of the page:
382 * (page_size - D) * 8
383 *
384 * --Huang Shijie
385 */
386 block_mark_bit_offset = mtd->writesize * 8 -
387 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
388 + geo->metadata_size * 8);
389
390 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
391 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
392 return 0;
393}
394
Huang Shijie2febcdf2013-05-17 11:17:34 +0800395int common_nfc_set_geometry(struct gpmi_nand_data *this)
396{
Han Xub8b0e462015-12-02 16:47:43 -0600397 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
398 || legacy_set_geometry(this))
399 return set_geometry_by_ecc_info(this);
400
401 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800402}
403
Huang Shijie10a2bca2011-09-08 10:47:09 +0800404struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
405{
Huang Shijiea7c12d02013-08-27 17:29:05 +0800406 /* We use the DMA channel 0 to access all the nand chips. */
407 return this->dma_chans[0];
Huang Shijie10a2bca2011-09-08 10:47:09 +0800408}
409
410/* Can we use the upper's buffer directly for DMA? */
411void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
412{
413 struct scatterlist *sgl = &this->data_sgl;
414 int ret;
415
Huang Shijie10a2bca2011-09-08 10:47:09 +0800416 /* first try to map the upper buffer directly */
Huang Shijie0ff76a92013-12-18 23:41:00 +0800417 if (virt_addr_valid(this->upper_buf) &&
418 !object_is_on_stack(this->upper_buf)) {
419 sg_init_one(sgl, this->upper_buf, this->upper_len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800420 ret = dma_map_sg(this->dev, sgl, 1, dr);
421 if (ret == 0)
Huang Shijie0ff76a92013-12-18 23:41:00 +0800422 goto map_fail;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800423
Huang Shijie0ff76a92013-12-18 23:41:00 +0800424 this->direct_dma_map_ok = true;
425 return;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800426 }
Huang Shijie0ff76a92013-12-18 23:41:00 +0800427
428map_fail:
429 /* We have to use our own DMA buffer. */
430 sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
431
432 if (dr == DMA_TO_DEVICE)
433 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
434
435 dma_map_sg(this->dev, sgl, 1, dr);
436
437 this->direct_dma_map_ok = false;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800438}
439
440/* This will be called after the DMA operation is finished. */
441static void dma_irq_callback(void *param)
442{
443 struct gpmi_nand_data *this = param;
444 struct completion *dma_c = &this->dma_done;
445
Huang Shijie10a2bca2011-09-08 10:47:09 +0800446 switch (this->dma_type) {
447 case DMA_FOR_COMMAND:
448 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
449 break;
450
451 case DMA_FOR_READ_DATA:
452 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
453 if (this->direct_dma_map_ok == false)
454 memcpy(this->upper_buf, this->data_buffer_dma,
455 this->upper_len);
456 break;
457
458 case DMA_FOR_WRITE_DATA:
459 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
460 break;
461
462 case DMA_FOR_READ_ECC_PAGE:
463 case DMA_FOR_WRITE_ECC_PAGE:
464 /* We have to wait the BCH interrupt to finish. */
465 break;
466
467 default:
Huang Shijieda40c162013-11-20 10:09:43 +0800468 dev_err(this->dev, "in wrong DMA operation.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800469 }
Huang Shijie7b3d2fb2013-11-11 12:13:45 +0800470
471 complete(dma_c);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800472}
473
474int start_dma_without_bch_irq(struct gpmi_nand_data *this,
475 struct dma_async_tx_descriptor *desc)
476{
477 struct completion *dma_c = &this->dma_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500478 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800479
480 init_completion(dma_c);
481
482 desc->callback = dma_irq_callback;
483 desc->callback_param = this;
484 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800485 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800486
487 /* Wait for the interrupt from the DMA block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500488 timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
489 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800490 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
491 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800492 gpmi_dump_info(this);
493 return -ETIMEDOUT;
494 }
495 return 0;
496}
497
498/*
499 * This function is used in BCH reading or BCH writing pages.
500 * It will wait for the BCH interrupt as long as ONE second.
501 * Actually, we must wait for two interrupts :
502 * [1] firstly the DMA interrupt and
503 * [2] secondly the BCH interrupt.
504 */
505int start_dma_with_bch_irq(struct gpmi_nand_data *this,
506 struct dma_async_tx_descriptor *desc)
507{
508 struct completion *bch_c = &this->bch_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500509 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800510
511 /* Prepare to receive an interrupt from the BCH block. */
512 init_completion(bch_c);
513
514 /* start the DMA */
515 start_dma_without_bch_irq(this, desc);
516
517 /* Wait for the interrupt from the BCH block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500518 timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
519 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800520 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
521 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800522 gpmi_dump_info(this);
523 return -ETIMEDOUT;
524 }
525 return 0;
526}
527
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800528static int acquire_register_block(struct gpmi_nand_data *this,
529 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800530{
531 struct platform_device *pdev = this->pdev;
532 struct resources *res = &this->resources;
533 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800534 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800535
536 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
Huang Shijie87a9d692013-11-14 14:25:48 +0800537 p = devm_ioremap_resource(&pdev->dev, r);
538 if (IS_ERR(p))
539 return PTR_ERR(p);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800540
541 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
542 res->gpmi_regs = p;
543 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
544 res->bch_regs = p;
545 else
Huang Shijieda40c162013-11-20 10:09:43 +0800546 dev_err(this->dev, "unknown resource name : %s\n", res_name);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800547
548 return 0;
549}
550
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800551static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800552{
553 struct platform_device *pdev = this->pdev;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800554 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
555 struct resource *r;
556 int err;
557
558 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
559 if (!r) {
Huang Shijieda40c162013-11-20 10:09:43 +0800560 dev_err(this->dev, "Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200561 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800562 }
563
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800564 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
565 if (err)
566 dev_err(this->dev, "error requesting BCH IRQ\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800567
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800568 return err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800569}
570
Huang Shijie10a2bca2011-09-08 10:47:09 +0800571static void release_dma_channels(struct gpmi_nand_data *this)
572{
573 unsigned int i;
574 for (i = 0; i < DMA_CHANS; i++)
575 if (this->dma_chans[i]) {
576 dma_release_channel(this->dma_chans[i]);
577 this->dma_chans[i] = NULL;
578 }
579}
580
Bill Pemberton06f25512012-11-19 13:23:07 -0500581static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800582{
583 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400584 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400585
586 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800587 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400588 if (!dma_chan) {
Huang Shijieda40c162013-11-20 10:09:43 +0800589 dev_err(this->dev, "Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400590 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800591 }
592
Huang Shijiee10db1f2012-05-04 21:42:05 -0400593 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800594 return 0;
595
596acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800597 release_dma_channels(this);
598 return -EINVAL;
599}
600
Huang Shijieff506172012-07-02 21:39:32 -0400601static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
602 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
603};
604
Bill Pemberton06f25512012-11-19 13:23:07 -0500605static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400606{
607 struct resources *r = &this->resources;
608 char **extra_clks = NULL;
609 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200610 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400611
612 /* The main clock is stored in the first. */
Fabio Estevam554cbc52013-11-07 22:32:38 -0200613 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200614 if (IS_ERR(r->clock[0])) {
615 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400616 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200617 }
Huang Shijieff506172012-07-02 21:39:32 -0400618
619 /* Get extra clocks */
Huang Shijie91f54982014-03-27 10:43:22 +0800620 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400621 extra_clks = extra_clks_for_mx6q;
622 if (!extra_clks)
623 return 0;
624
625 for (i = 1; i < GPMI_CLK_MAX; i++) {
626 if (extra_clks[i - 1] == NULL)
627 break;
628
Fabio Estevam554cbc52013-11-07 22:32:38 -0200629 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200630 if (IS_ERR(clk)) {
631 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400632 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200633 }
Huang Shijieff506172012-07-02 21:39:32 -0400634
635 r->clock[i] = clk;
636 }
637
Huang Shijie91f54982014-03-27 10:43:22 +0800638 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400639 /*
Huang Shijie91f54982014-03-27 10:43:22 +0800640 * Set the default value for the gpmi clock.
Huang Shijieff506172012-07-02 21:39:32 -0400641 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800642 * If you want to use the ONFI nand which is in the
643 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400644 */
645 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800646
Huang Shijieff506172012-07-02 21:39:32 -0400647 return 0;
648
649err_clock:
650 dev_dbg(this->dev, "failed in finding the clocks.\n");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200651 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400652}
653
Bill Pemberton06f25512012-11-19 13:23:07 -0500654static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800655{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800656 int ret;
657
658 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
659 if (ret)
660 goto exit_regs;
661
662 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
663 if (ret)
664 goto exit_regs;
665
666 ret = acquire_bch_irq(this, bch_irq);
667 if (ret)
668 goto exit_regs;
669
670 ret = acquire_dma_channels(this);
671 if (ret)
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800672 goto exit_regs;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800673
Huang Shijieff506172012-07-02 21:39:32 -0400674 ret = gpmi_get_clks(this);
675 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800676 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800677 return 0;
678
679exit_clock:
680 release_dma_channels(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800681exit_regs:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800682 return ret;
683}
684
685static void release_resources(struct gpmi_nand_data *this)
686{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800687 release_dma_channels(this);
688}
689
Bill Pemberton06f25512012-11-19 13:23:07 -0500690static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800691{
692 int ret;
693
694 /*
695 * This structure contains the "safe" GPMI timing that should succeed
696 * with any NAND Flash device
697 * (although, with less-than-optimal performance).
698 */
699 struct nand_timing safe_timing = {
700 .data_setup_in_ns = 80,
701 .data_hold_in_ns = 60,
702 .address_setup_in_ns = 25,
703 .gpmi_sample_delay_in_ns = 6,
704 .tREA_in_ns = -1,
705 .tRLOH_in_ns = -1,
706 .tRHOH_in_ns = -1,
707 };
708
709 /* Initialize the hardwares. */
710 ret = gpmi_init(this);
711 if (ret)
712 return ret;
713
714 this->timing = safe_timing;
715 return 0;
716}
717
718static int read_page_prepare(struct gpmi_nand_data *this,
719 void *destination, unsigned length,
720 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
721 void **use_virt, dma_addr_t *use_phys)
722{
723 struct device *dev = this->dev;
724
725 if (virt_addr_valid(destination)) {
726 dma_addr_t dest_phys;
727
728 dest_phys = dma_map_single(dev, destination,
729 length, DMA_FROM_DEVICE);
730 if (dma_mapping_error(dev, dest_phys)) {
731 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800732 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800733 return -ENOMEM;
734 }
735 goto map_failed;
736 }
737 *use_virt = destination;
738 *use_phys = dest_phys;
739 this->direct_dma_map_ok = true;
740 return 0;
741 }
742
743map_failed:
744 *use_virt = alt_virt;
745 *use_phys = alt_phys;
746 this->direct_dma_map_ok = false;
747 return 0;
748}
749
750static inline void read_page_end(struct gpmi_nand_data *this,
751 void *destination, unsigned length,
752 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
753 void *used_virt, dma_addr_t used_phys)
754{
755 if (this->direct_dma_map_ok)
756 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
757}
758
759static inline void read_page_swap_end(struct gpmi_nand_data *this,
760 void *destination, unsigned length,
761 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
762 void *used_virt, dma_addr_t used_phys)
763{
764 if (!this->direct_dma_map_ok)
765 memcpy(destination, alt_virt, length);
766}
767
768static int send_page_prepare(struct gpmi_nand_data *this,
769 const void *source, unsigned length,
770 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
771 const void **use_virt, dma_addr_t *use_phys)
772{
773 struct device *dev = this->dev;
774
775 if (virt_addr_valid(source)) {
776 dma_addr_t source_phys;
777
778 source_phys = dma_map_single(dev, (void *)source, length,
779 DMA_TO_DEVICE);
780 if (dma_mapping_error(dev, source_phys)) {
781 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800782 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800783 return -ENOMEM;
784 }
785 goto map_failed;
786 }
787 *use_virt = source;
788 *use_phys = source_phys;
789 return 0;
790 }
791map_failed:
792 /*
793 * Copy the content of the source buffer into the alternate
794 * buffer and set up the return values accordingly.
795 */
796 memcpy(alt_virt, source, length);
797
798 *use_virt = alt_virt;
799 *use_phys = alt_phys;
800 return 0;
801}
802
803static void send_page_end(struct gpmi_nand_data *this,
804 const void *source, unsigned length,
805 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
806 const void *used_virt, dma_addr_t used_phys)
807{
808 struct device *dev = this->dev;
809 if (used_virt == source)
810 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
811}
812
813static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
814{
815 struct device *dev = this->dev;
816
817 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
818 dma_free_coherent(dev, this->page_buffer_size,
819 this->page_buffer_virt,
820 this->page_buffer_phys);
821 kfree(this->cmd_buffer);
822 kfree(this->data_buffer_dma);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100823 kfree(this->raw_buffer);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800824
825 this->cmd_buffer = NULL;
826 this->data_buffer_dma = NULL;
Han Xu2cd395d2016-04-04 15:41:29 -0500827 this->raw_buffer = NULL;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800828 this->page_buffer_virt = NULL;
829 this->page_buffer_size = 0;
830}
831
832/* Allocate the DMA buffers */
833static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
834{
835 struct bch_geometry *geo = &this->bch_geometry;
836 struct device *dev = this->dev;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100837 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800838
839 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800840 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800841 if (this->cmd_buffer == NULL)
842 goto error_alloc;
843
Huang Shijie06f216c2013-12-18 23:40:59 +0800844 /*
845 * [2] Allocate a read/write data buffer.
846 * The gpmi_alloc_dma_buffer can be called twice.
847 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
848 * is called before the nand_scan_ident; and we allocate a buffer
849 * of the real NAND page size when the gpmi_alloc_dma_buffer is
850 * called after the nand_scan_ident.
851 */
852 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
853 GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800854 if (this->data_buffer_dma == NULL)
855 goto error_alloc;
856
857 /*
858 * [3] Allocate the page buffer.
859 *
860 * Both the payload buffer and the auxiliary buffer must appear on
861 * 32-bit boundaries. We presume the size of the payload buffer is a
862 * power of two and is much larger than four, which guarantees the
863 * auxiliary buffer will appear on a 32-bit boundary.
864 */
865 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
866 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
867 &this->page_buffer_phys, GFP_DMA);
868 if (!this->page_buffer_virt)
869 goto error_alloc;
870
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100871 this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
872 if (!this->raw_buffer)
873 goto error_alloc;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800874
875 /* Slice up the page buffer. */
876 this->payload_virt = this->page_buffer_virt;
877 this->payload_phys = this->page_buffer_phys;
878 this->auxiliary_virt = this->payload_virt + geo->payload_size;
879 this->auxiliary_phys = this->payload_phys + geo->payload_size;
880 return 0;
881
882error_alloc:
883 gpmi_free_dma_buffer(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800884 return -ENOMEM;
885}
886
887static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
888{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100889 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100890 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800891 int ret;
892
893 /*
894 * Every operation begins with a command byte and a series of zero or
895 * more address bytes. These are distinguished by either the Address
896 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
897 * asserted. When MTD is ready to execute the command, it will deassert
898 * both latch enables.
899 *
900 * Rather than run a separate DMA operation for every single byte, we
901 * queue them up and run a single DMA operation for the entire series
902 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
903 */
904 if ((ctrl & (NAND_ALE | NAND_CLE))) {
905 if (data != NAND_CMD_NONE)
906 this->cmd_buffer[this->command_length++] = data;
907 return;
908 }
909
910 if (!this->command_length)
911 return;
912
913 ret = gpmi_send_command(this);
914 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +0800915 dev_err(this->dev, "Chip: %u, Error %d\n",
916 this->current_chip, ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800917
918 this->command_length = 0;
919}
920
921static int gpmi_dev_ready(struct mtd_info *mtd)
922{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100923 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100924 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800925
926 return gpmi_is_ready(this, this->current_chip);
927}
928
929static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
930{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100931 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100932 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800933
934 if ((this->current_chip < 0) && (chipnr >= 0))
935 gpmi_begin(this);
936 else if ((this->current_chip >= 0) && (chipnr < 0))
937 gpmi_end(this);
938
939 this->current_chip = chipnr;
940}
941
942static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
943{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100944 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100945 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800946
Huang Shijiec2325962013-11-20 10:09:44 +0800947 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800948 this->upper_buf = buf;
949 this->upper_len = len;
950
951 gpmi_read_data(this);
952}
953
954static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
955{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100956 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100957 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800958
Huang Shijiec2325962013-11-20 10:09:44 +0800959 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800960 this->upper_buf = (uint8_t *)buf;
961 this->upper_len = len;
962
963 gpmi_send_data(this);
964}
965
966static uint8_t gpmi_read_byte(struct mtd_info *mtd)
967{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100968 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100969 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800970 uint8_t *buf = this->data_buffer_dma;
971
972 gpmi_read_buf(mtd, buf, 1);
973 return buf[0];
974}
975
976/*
977 * Handles block mark swapping.
978 * It can be called in swapping the block mark, or swapping it back,
979 * because the the operations are the same.
980 */
981static void block_mark_swapping(struct gpmi_nand_data *this,
982 void *payload, void *auxiliary)
983{
984 struct bch_geometry *nfc_geo = &this->bch_geometry;
985 unsigned char *p;
986 unsigned char *a;
987 unsigned int bit;
988 unsigned char mask;
989 unsigned char from_data;
990 unsigned char from_oob;
991
992 if (!this->swap_block_mark)
993 return;
994
995 /*
996 * If control arrives here, we're swapping. Make some convenience
997 * variables.
998 */
999 bit = nfc_geo->block_mark_bit_offset;
1000 p = payload + nfc_geo->block_mark_byte_offset;
1001 a = auxiliary;
1002
1003 /*
1004 * Get the byte from the data area that overlays the block mark. Since
1005 * the ECC engine applies its own view to the bits in the page, the
1006 * physical block mark won't (in general) appear on a byte boundary in
1007 * the data.
1008 */
1009 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1010
1011 /* Get the byte from the OOB. */
1012 from_oob = a[0];
1013
1014 /* Swap them. */
1015 a[0] = from_data;
1016
1017 mask = (0x1 << bit) - 1;
1018 p[0] = (p[0] & mask) | (from_oob << bit);
1019
1020 mask = ~0 << bit;
1021 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1022}
1023
1024static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001025 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001026{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001027 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001028 struct bch_geometry *nfc_geo = &this->bch_geometry;
1029 void *payload_virt;
1030 dma_addr_t payload_phys;
1031 void *auxiliary_virt;
1032 dma_addr_t auxiliary_phys;
1033 unsigned int i;
1034 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -06001035 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001036 int ret;
1037
Huang Shijiec2325962013-11-20 10:09:44 +08001038 dev_dbg(this->dev, "page number is : %d\n", page);
Huang Shijie4a57d672014-01-03 11:01:41 +08001039 ret = read_page_prepare(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001040 this->payload_virt, this->payload_phys,
1041 nfc_geo->payload_size,
1042 &payload_virt, &payload_phys);
1043 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001044 dev_err(this->dev, "Inadequate DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001045 ret = -ENOMEM;
1046 return ret;
1047 }
1048 auxiliary_virt = this->auxiliary_virt;
1049 auxiliary_phys = this->auxiliary_phys;
1050
1051 /* go! */
1052 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
Huang Shijie4a57d672014-01-03 11:01:41 +08001053 read_page_end(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001054 this->payload_virt, this->payload_phys,
1055 nfc_geo->payload_size,
1056 payload_virt, payload_phys);
1057 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001058 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001059 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001060 }
1061
1062 /* handle the block mark swapping */
1063 block_mark_swapping(this, payload_virt, auxiliary_virt);
1064
1065 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001066 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001067
1068 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1069 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1070 continue;
1071
1072 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001073 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001074 continue;
1075 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001076 mtd->ecc_stats.corrected += *status;
1077 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001078 }
1079
Brian Norris7725cc82012-05-02 10:15:02 -07001080 if (oob_required) {
1081 /*
1082 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1083 * for details about our policy for delivering the OOB.
1084 *
1085 * We fill the caller's buffer with set bits, and then copy the
1086 * block mark to th caller's buffer. Note that, if block mark
1087 * swapping was necessary, it has already been done, so we can
1088 * rely on the first byte of the auxiliary buffer to contain
1089 * the block mark.
1090 */
1091 memset(chip->oob_poi, ~0, mtd->oobsize);
1092 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001093 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001094
Huang Shijie4a57d672014-01-03 11:01:41 +08001095 read_page_swap_end(this, buf, nfc_geo->payload_size,
Sascha Hauer6023813a2012-06-26 17:26:16 +02001096 this->payload_virt, this->payload_phys,
1097 nfc_geo->payload_size,
1098 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001099
1100 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001101}
1102
Huang Shijieb8e29312014-01-03 11:01:42 +08001103/* Fake a virtual small page for the subpage read */
1104static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1105 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1106{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001107 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijieb8e29312014-01-03 11:01:42 +08001108 void __iomem *bch_regs = this->resources.bch_regs;
1109 struct bch_geometry old_geo = this->bch_geometry;
1110 struct bch_geometry *geo = &this->bch_geometry;
1111 int size = chip->ecc.size; /* ECC chunk size */
1112 int meta, n, page_size;
1113 u32 r1_old, r2_old, r1_new, r2_new;
1114 unsigned int max_bitflips;
1115 int first, last, marker_pos;
1116 int ecc_parity_size;
1117 int col = 0;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001118 int old_swap_block_mark = this->swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001119
1120 /* The size of ECC parity */
1121 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1122
1123 /* Align it with the chunk size */
1124 first = offs / size;
1125 last = (offs + len - 1) / size;
1126
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001127 if (this->swap_block_mark) {
1128 /*
1129 * Find the chunk which contains the Block Marker.
1130 * If this chunk is in the range of [first, last],
1131 * we have to read out the whole page.
1132 * Why? since we had swapped the data at the position of Block
1133 * Marker to the metadata which is bound with the chunk 0.
1134 */
1135 marker_pos = geo->block_mark_byte_offset / size;
1136 if (last >= marker_pos && first <= marker_pos) {
1137 dev_dbg(this->dev,
1138 "page:%d, first:%d, last:%d, marker at:%d\n",
Huang Shijieb8e29312014-01-03 11:01:42 +08001139 page, first, last, marker_pos);
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001140 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1141 }
Huang Shijieb8e29312014-01-03 11:01:42 +08001142 }
1143
1144 meta = geo->metadata_size;
1145 if (first) {
1146 col = meta + (size + ecc_parity_size) * first;
1147 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1148
1149 meta = 0;
1150 buf = buf + first * size;
1151 }
1152
1153 /* Save the old environment */
1154 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1155 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1156
1157 /* change the BCH registers and bch_geometry{} */
1158 n = last - first + 1;
1159 page_size = meta + (size + ecc_parity_size) * n;
1160
1161 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1162 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1163 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1164 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1165 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1166
1167 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1168 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1169 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1170
1171 geo->ecc_chunk_count = n;
1172 geo->payload_size = n * size;
1173 geo->page_size = page_size;
1174 geo->auxiliary_status_offset = ALIGN(meta, 4);
1175
1176 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1177 page, offs, len, col, first, n, page_size);
1178
1179 /* Read the subpage now */
1180 this->swap_block_mark = false;
1181 max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1182
1183 /* Restore */
1184 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1185 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1186 this->bch_geometry = old_geo;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001187 this->swap_block_mark = old_swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001188
1189 return max_bitflips;
1190}
1191
Josh Wufdbad98d2012-06-25 18:07:45 +08001192static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001193 const uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001194{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001195 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001196 struct bch_geometry *nfc_geo = &this->bch_geometry;
1197 const void *payload_virt;
1198 dma_addr_t payload_phys;
1199 const void *auxiliary_virt;
1200 dma_addr_t auxiliary_phys;
1201 int ret;
1202
Huang Shijiec2325962013-11-20 10:09:44 +08001203 dev_dbg(this->dev, "ecc write page.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001204 if (this->swap_block_mark) {
1205 /*
1206 * If control arrives here, we're doing block mark swapping.
1207 * Since we can't modify the caller's buffers, we must copy them
1208 * into our own.
1209 */
1210 memcpy(this->payload_virt, buf, mtd->writesize);
1211 payload_virt = this->payload_virt;
1212 payload_phys = this->payload_phys;
1213
1214 memcpy(this->auxiliary_virt, chip->oob_poi,
1215 nfc_geo->auxiliary_size);
1216 auxiliary_virt = this->auxiliary_virt;
1217 auxiliary_phys = this->auxiliary_phys;
1218
1219 /* Handle block mark swapping. */
1220 block_mark_swapping(this,
Lothar Waßmann6a760962014-06-12 15:20:41 +02001221 (void *)payload_virt, (void *)auxiliary_virt);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001222 } else {
1223 /*
1224 * If control arrives here, we're not doing block mark swapping,
1225 * so we can to try and use the caller's buffers.
1226 */
1227 ret = send_page_prepare(this,
1228 buf, mtd->writesize,
1229 this->payload_virt, this->payload_phys,
1230 nfc_geo->payload_size,
1231 &payload_virt, &payload_phys);
1232 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001233 dev_err(this->dev, "Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001234 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001235 }
1236
1237 ret = send_page_prepare(this,
1238 chip->oob_poi, mtd->oobsize,
1239 this->auxiliary_virt, this->auxiliary_phys,
1240 nfc_geo->auxiliary_size,
1241 &auxiliary_virt, &auxiliary_phys);
1242 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001243 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001244 goto exit_auxiliary;
1245 }
1246 }
1247
1248 /* Ask the NFC. */
1249 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1250 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +08001251 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001252
1253 if (!this->swap_block_mark) {
1254 send_page_end(this, chip->oob_poi, mtd->oobsize,
1255 this->auxiliary_virt, this->auxiliary_phys,
1256 nfc_geo->auxiliary_size,
1257 auxiliary_virt, auxiliary_phys);
1258exit_auxiliary:
1259 send_page_end(this, buf, mtd->writesize,
1260 this->payload_virt, this->payload_phys,
1261 nfc_geo->payload_size,
1262 payload_virt, payload_phys);
1263 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001264
1265 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001266}
1267
1268/*
1269 * There are several places in this driver where we have to handle the OOB and
1270 * block marks. This is the function where things are the most complicated, so
1271 * this is where we try to explain it all. All the other places refer back to
1272 * here.
1273 *
1274 * These are the rules, in order of decreasing importance:
1275 *
1276 * 1) Nothing the caller does can be allowed to imperil the block mark.
1277 *
1278 * 2) In read operations, the first byte of the OOB we return must reflect the
1279 * true state of the block mark, no matter where that block mark appears in
1280 * the physical page.
1281 *
1282 * 3) ECC-based read operations return an OOB full of set bits (since we never
1283 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1284 * return).
1285 *
1286 * 4) "Raw" read operations return a direct view of the physical bytes in the
1287 * page, using the conventional definition of which bytes are data and which
1288 * are OOB. This gives the caller a way to see the actual, physical bytes
1289 * in the page, without the distortions applied by our ECC engine.
1290 *
1291 *
1292 * What we do for this specific read operation depends on two questions:
1293 *
1294 * 1) Are we doing a "raw" read, or an ECC-based read?
1295 *
1296 * 2) Are we using block mark swapping or transcription?
1297 *
1298 * There are four cases, illustrated by the following Karnaugh map:
1299 *
1300 * | Raw | ECC-based |
1301 * -------------+-------------------------+-------------------------+
1302 * | Read the conventional | |
1303 * | OOB at the end of the | |
1304 * Swapping | page and return it. It | |
1305 * | contains exactly what | |
1306 * | we want. | Read the block mark and |
1307 * -------------+-------------------------+ return it in a buffer |
1308 * | Read the conventional | full of set bits. |
1309 * | OOB at the end of the | |
1310 * | page and also the block | |
1311 * Transcribing | mark in the metadata. | |
1312 * | Copy the block mark | |
1313 * | into the first byte of | |
1314 * | the OOB. | |
1315 * -------------+-------------------------+-------------------------+
1316 *
1317 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1318 * giving an accurate view of the actual, physical bytes in the page (we're
1319 * overwriting the block mark). That's OK because it's more important to follow
1320 * rule #2.
1321 *
1322 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1323 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1324 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1325 * ECC-based or raw view of the page is implicit in which function it calls
1326 * (there is a similar pair of ECC-based/raw functions for writing).
Huang Shijie10a2bca2011-09-08 10:47:09 +08001327 */
1328static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001329 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001330{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001331 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001332
Huang Shijiec2325962013-11-20 10:09:44 +08001333 dev_dbg(this->dev, "page number is %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001334 /* clear the OOB buffer */
1335 memset(chip->oob_poi, ~0, mtd->oobsize);
1336
1337 /* Read out the conventional OOB. */
1338 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1339 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1340
1341 /*
1342 * Now, we want to make sure the block mark is correct. In the
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001343 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1344 * Otherwise, we need to explicitly read it.
Huang Shijie10a2bca2011-09-08 10:47:09 +08001345 */
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001346 if (GPMI_IS_MX23(this)) {
Huang Shijie10a2bca2011-09-08 10:47:09 +08001347 /* Read the block mark into the first byte of the OOB buffer. */
1348 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1349 chip->oob_poi[0] = chip->read_byte(mtd);
1350 }
1351
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001352 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001353}
1354
1355static int
1356gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1357{
Boris Brezillon191a8292016-02-03 20:11:44 +01001358 struct mtd_oob_region of = { };
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001359 int status = 0;
1360
1361 /* Do we have available oob area? */
Boris Brezillon191a8292016-02-03 20:11:44 +01001362 mtd_ooblayout_free(mtd, 0, &of);
1363 if (!of.length)
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001364 return -EPERM;
1365
1366 if (!nand_is_slc(chip))
1367 return -EPERM;
1368
Boris Brezillon191a8292016-02-03 20:11:44 +01001369 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of.offset, page);
1370 chip->write_buf(mtd, chip->oob_poi + of.offset, of.length);
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001371 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1372
1373 status = chip->waitfunc(mtd, chip);
1374 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001375}
1376
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001377/*
1378 * This function reads a NAND page without involving the ECC engine (no HW
1379 * ECC correction).
1380 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1381 * inline (interleaved with payload DATA), and do not align data chunk on
1382 * byte boundaries.
1383 * We thus need to take care moving the payload data and ECC bits stored in the
1384 * page into the provided buffers, which is why we're using gpmi_copy_bits.
1385 *
1386 * See set_geometry_by_ecc_info inline comments to have a full description
1387 * of the layout used by the GPMI controller.
1388 */
1389static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
1390 struct nand_chip *chip, uint8_t *buf,
1391 int oob_required, int page)
1392{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001393 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001394 struct bch_geometry *nfc_geo = &this->bch_geometry;
1395 int eccsize = nfc_geo->ecc_chunk_size;
1396 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1397 u8 *tmp_buf = this->raw_buffer;
1398 size_t src_bit_off;
1399 size_t oob_bit_off;
1400 size_t oob_byte_off;
1401 uint8_t *oob = chip->oob_poi;
1402 int step;
1403
1404 chip->read_buf(mtd, tmp_buf,
1405 mtd->writesize + mtd->oobsize);
1406
1407 /*
1408 * If required, swap the bad block marker and the data stored in the
1409 * metadata section, so that we don't wrongly consider a block as bad.
1410 *
1411 * See the layout description for a detailed explanation on why this
1412 * is needed.
1413 */
1414 if (this->swap_block_mark) {
1415 u8 swap = tmp_buf[0];
1416
1417 tmp_buf[0] = tmp_buf[mtd->writesize];
1418 tmp_buf[mtd->writesize] = swap;
1419 }
1420
1421 /*
1422 * Copy the metadata section into the oob buffer (this section is
1423 * guaranteed to be aligned on a byte boundary).
1424 */
1425 if (oob_required)
1426 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1427
1428 oob_bit_off = nfc_geo->metadata_size * 8;
1429 src_bit_off = oob_bit_off;
1430
1431 /* Extract interleaved payload data and ECC bits */
1432 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1433 if (buf)
1434 gpmi_copy_bits(buf, step * eccsize * 8,
1435 tmp_buf, src_bit_off,
1436 eccsize * 8);
1437 src_bit_off += eccsize * 8;
1438
1439 /* Align last ECC block to align a byte boundary */
1440 if (step == nfc_geo->ecc_chunk_count - 1 &&
1441 (oob_bit_off + eccbits) % 8)
1442 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1443
1444 if (oob_required)
1445 gpmi_copy_bits(oob, oob_bit_off,
1446 tmp_buf, src_bit_off,
1447 eccbits);
1448
1449 src_bit_off += eccbits;
1450 oob_bit_off += eccbits;
1451 }
1452
1453 if (oob_required) {
1454 oob_byte_off = oob_bit_off / 8;
1455
1456 if (oob_byte_off < mtd->oobsize)
1457 memcpy(oob + oob_byte_off,
1458 tmp_buf + mtd->writesize + oob_byte_off,
1459 mtd->oobsize - oob_byte_off);
1460 }
1461
1462 return 0;
1463}
1464
1465/*
1466 * This function writes a NAND page without involving the ECC engine (no HW
1467 * ECC generation).
1468 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1469 * inline (interleaved with payload DATA), and do not align data chunk on
1470 * byte boundaries.
1471 * We thus need to take care moving the OOB area at the right place in the
1472 * final page, which is why we're using gpmi_copy_bits.
1473 *
1474 * See set_geometry_by_ecc_info inline comments to have a full description
1475 * of the layout used by the GPMI controller.
1476 */
1477static int gpmi_ecc_write_page_raw(struct mtd_info *mtd,
1478 struct nand_chip *chip,
1479 const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001480 int oob_required, int page)
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001481{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001482 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001483 struct bch_geometry *nfc_geo = &this->bch_geometry;
1484 int eccsize = nfc_geo->ecc_chunk_size;
1485 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1486 u8 *tmp_buf = this->raw_buffer;
1487 uint8_t *oob = chip->oob_poi;
1488 size_t dst_bit_off;
1489 size_t oob_bit_off;
1490 size_t oob_byte_off;
1491 int step;
1492
1493 /*
1494 * Initialize all bits to 1 in case we don't have a buffer for the
1495 * payload or oob data in order to leave unspecified bits of data
1496 * to their initial state.
1497 */
1498 if (!buf || !oob_required)
1499 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1500
1501 /*
1502 * First copy the metadata section (stored in oob buffer) at the
1503 * beginning of the page, as imposed by the GPMI layout.
1504 */
1505 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1506 oob_bit_off = nfc_geo->metadata_size * 8;
1507 dst_bit_off = oob_bit_off;
1508
1509 /* Interleave payload data and ECC bits */
1510 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1511 if (buf)
1512 gpmi_copy_bits(tmp_buf, dst_bit_off,
1513 buf, step * eccsize * 8, eccsize * 8);
1514 dst_bit_off += eccsize * 8;
1515
1516 /* Align last ECC block to align a byte boundary */
1517 if (step == nfc_geo->ecc_chunk_count - 1 &&
1518 (oob_bit_off + eccbits) % 8)
1519 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1520
1521 if (oob_required)
1522 gpmi_copy_bits(tmp_buf, dst_bit_off,
1523 oob, oob_bit_off, eccbits);
1524
1525 dst_bit_off += eccbits;
1526 oob_bit_off += eccbits;
1527 }
1528
1529 oob_byte_off = oob_bit_off / 8;
1530
1531 if (oob_required && oob_byte_off < mtd->oobsize)
1532 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1533 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1534
1535 /*
1536 * If required, swap the bad block marker and the first byte of the
1537 * metadata section, so that we don't modify the bad block marker.
1538 *
1539 * See the layout description for a detailed explanation on why this
1540 * is needed.
1541 */
1542 if (this->swap_block_mark) {
1543 u8 swap = tmp_buf[0];
1544
1545 tmp_buf[0] = tmp_buf[mtd->writesize];
1546 tmp_buf[mtd->writesize] = swap;
1547 }
1548
1549 chip->write_buf(mtd, tmp_buf, mtd->writesize + mtd->oobsize);
1550
1551 return 0;
1552}
1553
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001554static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1555 int page)
1556{
1557 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1558
1559 return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
1560}
1561
1562static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1563 int page)
1564{
1565 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
1566
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001567 return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001568}
1569
Huang Shijie10a2bca2011-09-08 10:47:09 +08001570static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1571{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001572 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001573 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -07001574 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001575 uint8_t *block_mark;
1576 int column, page, status, chipnr;
1577
Brian Norris5a0edb22013-07-30 17:52:58 -07001578 chipnr = (int)(ofs >> chip->chip_shift);
1579 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001580
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001581 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001582
Brian Norris5a0edb22013-07-30 17:52:58 -07001583 /* Write the block mark. */
1584 block_mark = this->data_buffer_dma;
1585 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001586
Brian Norris5a0edb22013-07-30 17:52:58 -07001587 /* Shift to get page */
1588 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001589
Brian Norris5a0edb22013-07-30 17:52:58 -07001590 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1591 chip->write_buf(mtd, block_mark, 1);
1592 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001593
Brian Norris5a0edb22013-07-30 17:52:58 -07001594 status = chip->waitfunc(mtd, chip);
1595 if (status & NAND_STATUS_FAIL)
1596 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001597
Brian Norris5a0edb22013-07-30 17:52:58 -07001598 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001599
1600 return ret;
1601}
1602
Wolfram Sanga78da282012-03-21 19:29:17 +01001603static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001604{
1605 struct boot_rom_geometry *geometry = &this->rom_geometry;
1606
1607 /*
1608 * Set the boot block stride size.
1609 *
1610 * In principle, we should be reading this from the OTP bits, since
1611 * that's where the ROM is going to get it. In fact, we don't have any
1612 * way to read the OTP bits, so we go with the default and hope for the
1613 * best.
1614 */
1615 geometry->stride_size_in_pages = 64;
1616
1617 /*
1618 * Set the search area stride exponent.
1619 *
1620 * In principle, we should be reading this from the OTP bits, since
1621 * that's where the ROM is going to get it. In fact, we don't have any
1622 * way to read the OTP bits, so we go with the default and hope for the
1623 * best.
1624 */
1625 geometry->search_area_stride_exponent = 2;
1626 return 0;
1627}
1628
1629static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001630static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001631{
1632 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1633 struct device *dev = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001634 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001635 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001636 unsigned int search_area_size_in_strides;
1637 unsigned int stride;
1638 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001639 uint8_t *buffer = chip->buffers->databuf;
1640 int saved_chip_number;
1641 int found_an_ncb_fingerprint = false;
1642
1643 /* Compute the number of strides in a search area. */
1644 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1645
1646 saved_chip_number = this->current_chip;
1647 chip->select_chip(mtd, 0);
1648
1649 /*
1650 * Loop through the first search area, looking for the NCB fingerprint.
1651 */
1652 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1653
1654 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001655 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001656 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001657
1658 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1659
1660 /*
1661 * Read the NCB fingerprint. The fingerprint is four bytes long
1662 * and starts in the 12th byte of the page.
1663 */
1664 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1665 chip->read_buf(mtd, buffer, strlen(fingerprint));
1666
1667 /* Look for the fingerprint. */
1668 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1669 found_an_ncb_fingerprint = true;
1670 break;
1671 }
1672
1673 }
1674
1675 chip->select_chip(mtd, saved_chip_number);
1676
1677 if (found_an_ncb_fingerprint)
1678 dev_dbg(dev, "\tFound a fingerprint\n");
1679 else
1680 dev_dbg(dev, "\tNo fingerprint found\n");
1681 return found_an_ncb_fingerprint;
1682}
1683
1684/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001685static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001686{
1687 struct device *dev = this->dev;
1688 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001689 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001690 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001691 unsigned int block_size_in_pages;
1692 unsigned int search_area_size_in_strides;
1693 unsigned int search_area_size_in_pages;
1694 unsigned int search_area_size_in_blocks;
1695 unsigned int block;
1696 unsigned int stride;
1697 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001698 uint8_t *buffer = chip->buffers->databuf;
1699 int saved_chip_number;
1700 int status;
1701
1702 /* Compute the search area geometry. */
1703 block_size_in_pages = mtd->erasesize / mtd->writesize;
1704 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1705 search_area_size_in_pages = search_area_size_in_strides *
1706 rom_geo->stride_size_in_pages;
1707 search_area_size_in_blocks =
1708 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1709 block_size_in_pages;
1710
1711 dev_dbg(dev, "Search Area Geometry :\n");
1712 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1713 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1714 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1715
1716 /* Select chip 0. */
1717 saved_chip_number = this->current_chip;
1718 chip->select_chip(mtd, 0);
1719
1720 /* Loop over blocks in the first search area, erasing them. */
1721 dev_dbg(dev, "Erasing the search area...\n");
1722
1723 for (block = 0; block < search_area_size_in_blocks; block++) {
1724 /* Compute the page address. */
1725 page = block * block_size_in_pages;
1726
1727 /* Erase this block. */
1728 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1729 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1730 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1731
1732 /* Wait for the erase to finish. */
1733 status = chip->waitfunc(mtd, chip);
1734 if (status & NAND_STATUS_FAIL)
1735 dev_err(dev, "[%s] Erase failed.\n", __func__);
1736 }
1737
1738 /* Write the NCB fingerprint into the page buffer. */
1739 memset(buffer, ~0, mtd->writesize);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001740 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1741
1742 /* Loop through the first search area, writing NCB fingerprints. */
1743 dev_dbg(dev, "Writing NCB fingerprints...\n");
1744 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001745 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001746 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001747
1748 /* Write the first page of the current stride. */
1749 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1750 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001751 chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001752 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1753
1754 /* Wait for the write to finish. */
1755 status = chip->waitfunc(mtd, chip);
1756 if (status & NAND_STATUS_FAIL)
1757 dev_err(dev, "[%s] Write failed.\n", __func__);
1758 }
1759
1760 /* Deselect chip 0. */
1761 chip->select_chip(mtd, saved_chip_number);
1762 return 0;
1763}
1764
Wolfram Sanga78da282012-03-21 19:29:17 +01001765static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001766{
1767 struct device *dev = this->dev;
1768 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001769 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001770 unsigned int block_count;
1771 unsigned int block;
1772 int chipnr;
1773 int page;
1774 loff_t byte;
1775 uint8_t block_mark;
1776 int ret = 0;
1777
1778 /*
1779 * If control arrives here, we can't use block mark swapping, which
1780 * means we're forced to use transcription. First, scan for the
1781 * transcription stamp. If we find it, then we don't have to do
1782 * anything -- the block marks are already transcribed.
1783 */
1784 if (mx23_check_transcription_stamp(this))
1785 return 0;
1786
1787 /*
1788 * If control arrives here, we couldn't find a transcription stamp, so
1789 * so we presume the block marks are in the conventional location.
1790 */
1791 dev_dbg(dev, "Transcribing bad block marks...\n");
1792
1793 /* Compute the number of blocks in the entire medium. */
1794 block_count = chip->chipsize >> chip->phys_erase_shift;
1795
1796 /*
1797 * Loop over all the blocks in the medium, transcribing block marks as
1798 * we go.
1799 */
1800 for (block = 0; block < block_count; block++) {
1801 /*
1802 * Compute the chip, page and byte addresses for this block's
1803 * conventional mark.
1804 */
1805 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1806 page = block << (chip->phys_erase_shift - chip->page_shift);
1807 byte = block << chip->phys_erase_shift;
1808
1809 /* Send the command to read the conventional block mark. */
1810 chip->select_chip(mtd, chipnr);
1811 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1812 block_mark = chip->read_byte(mtd);
1813 chip->select_chip(mtd, -1);
1814
1815 /*
1816 * Check if the block is marked bad. If so, we need to mark it
1817 * again, but this time the result will be a mark in the
1818 * location where we transcribe block marks.
1819 */
1820 if (block_mark != 0xff) {
1821 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1822 ret = chip->block_markbad(mtd, byte);
1823 if (ret)
Lothar Waßmannd8c03722014-06-12 15:20:42 +02001824 dev_err(dev,
1825 "Failed to mark block bad with ret %d\n",
1826 ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001827 }
1828 }
1829
1830 /* Write the stamp that indicates we've transcribed the block marks. */
1831 mx23_write_transcription_stamp(this);
1832 return 0;
1833}
1834
Wolfram Sanga78da282012-03-21 19:29:17 +01001835static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001836{
1837 nand_boot_set_geometry(this);
1838
1839 /* This is ROM arch-specific initilization before the BBT scanning. */
1840 if (GPMI_IS_MX23(this))
1841 return mx23_boot_init(this);
1842 return 0;
1843}
1844
Wolfram Sanga78da282012-03-21 19:29:17 +01001845static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001846{
1847 int ret;
1848
1849 /* Free the temporary DMA memory for reading ID. */
1850 gpmi_free_dma_buffer(this);
1851
1852 /* Set up the NFC geometry which is used by BCH. */
1853 ret = bch_set_geometry(this);
1854 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001855 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001856 return ret;
1857 }
1858
1859 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1860 return gpmi_alloc_dma_buffer(this);
1861}
1862
Huang Shijieccce4172013-11-14 14:25:47 +08001863static void gpmi_nand_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001864{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001865 nand_release(nand_to_mtd(&this->nand));
Huang Shijief720e7c2013-08-16 10:10:08 +08001866 gpmi_free_dma_buffer(this);
1867}
1868
1869static int gpmi_init_last(struct gpmi_nand_data *this)
1870{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001871 struct nand_chip *chip = &this->nand;
Boris Brezillon3f158e42016-02-03 20:01:54 +01001872 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijief720e7c2013-08-16 10:10:08 +08001873 struct nand_ecc_ctrl *ecc = &chip->ecc;
1874 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001875 int ret;
1876
Huang Shijied7364a272013-11-14 14:25:45 +08001877 /* Set up the medium geometry */
1878 ret = gpmi_set_geometry(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001879 if (ret)
1880 return ret;
1881
Huang Shijief720e7c2013-08-16 10:10:08 +08001882 /* Init the nand_ecc_ctrl{} */
1883 ecc->read_page = gpmi_ecc_read_page;
1884 ecc->write_page = gpmi_ecc_write_page;
1885 ecc->read_oob = gpmi_ecc_read_oob;
1886 ecc->write_oob = gpmi_ecc_write_oob;
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001887 ecc->read_page_raw = gpmi_ecc_read_page_raw;
1888 ecc->write_page_raw = gpmi_ecc_write_page_raw;
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001889 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1890 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
Huang Shijief720e7c2013-08-16 10:10:08 +08001891 ecc->mode = NAND_ECC_HW;
1892 ecc->size = bch_geo->ecc_chunk_size;
1893 ecc->strength = bch_geo->ecc_strength;
Boris Brezillon3f158e42016-02-03 20:01:54 +01001894 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
Huang Shijief720e7c2013-08-16 10:10:08 +08001895
Huang Shijie995fbbf2012-09-13 14:57:59 +08001896 /*
Huang Shijieb8e29312014-01-03 11:01:42 +08001897 * We only enable the subpage read when:
1898 * (1) the chip is imx6, and
1899 * (2) the size of the ECC parity is byte aligned.
1900 */
Huang Shijie91f54982014-03-27 10:43:22 +08001901 if (GPMI_IS_MX6(this) &&
Huang Shijieb8e29312014-01-03 11:01:42 +08001902 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1903 ecc->read_subpage = gpmi_ecc_read_subpage;
1904 chip->options |= NAND_SUBPAGE_READ;
1905 }
1906
1907 /*
Huang Shijie995fbbf2012-09-13 14:57:59 +08001908 * Can we enable the extra features? such as EDO or Sync mode.
1909 *
1910 * We do not check the return value now. That's means if we fail in
1911 * enable the extra features, we still can run in the normal way.
1912 */
1913 gpmi_extra_init(this);
1914
Huang Shijief720e7c2013-08-16 10:10:08 +08001915 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001916}
1917
Huang Shijieccce4172013-11-14 14:25:47 +08001918static int gpmi_nand_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001919{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001920 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001921 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001922 int ret;
1923
1924 /* init current chip */
1925 this->current_chip = -1;
1926
1927 /* init the MTD data structures */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001928 mtd->name = "gpmi-nand";
Frans Klaver4dc67b12015-06-10 22:38:49 +02001929 mtd->dev.parent = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001930
1931 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001932 nand_set_controller_data(chip, this);
Brian Norrisa61ae812015-10-30 20:33:25 -07001933 nand_set_flash_node(chip, this->pdev->dev.of_node);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001934 chip->select_chip = gpmi_select_chip;
1935 chip->cmd_ctrl = gpmi_cmd_ctrl;
1936 chip->dev_ready = gpmi_dev_ready;
1937 chip->read_byte = gpmi_read_byte;
1938 chip->read_buf = gpmi_read_buf;
1939 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001940 chip->badblock_pattern = &gpmi_bbt_descr;
1941 chip->block_markbad = gpmi_block_markbad;
1942 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001943
1944 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1945 this->swap_block_mark = !GPMI_IS_MX23(this);
1946
1947 if (of_get_nand_on_flash_bbt(this->dev->of_node)) {
Huang Shijiec50c6942012-07-03 16:24:32 +08001948 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001949
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001950 if (of_property_read_bool(this->dev->of_node,
1951 "fsl,no-blockmark-swap"))
1952 this->swap_block_mark = false;
1953 }
1954 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
1955 this->swap_block_mark ? "en" : "dis");
1956
Huang Shijief720e7c2013-08-16 10:10:08 +08001957 /*
1958 * Allocate a temporary DMA buffer for reading ID in the
1959 * nand_scan_ident().
1960 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001961 this->bch_geometry.payload_size = 1024;
1962 this->bch_geometry.auxiliary_size = 128;
1963 ret = gpmi_alloc_dma_buffer(this);
1964 if (ret)
1965 goto err_out;
1966
Huang Shijie91f54982014-03-27 10:43:22 +08001967 ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
Huang Shijief720e7c2013-08-16 10:10:08 +08001968 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001969 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001970
1971 ret = gpmi_init_last(this);
1972 if (ret)
1973 goto err_out;
1974
Huang Shijie885d71e2013-11-12 12:23:08 +08001975 chip->options |= NAND_SKIP_BBTSCAN;
Huang Shijief720e7c2013-08-16 10:10:08 +08001976 ret = nand_scan_tail(mtd);
1977 if (ret)
1978 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001979
Huang Shijie885d71e2013-11-12 12:23:08 +08001980 ret = nand_boot_init(this);
1981 if (ret)
1982 goto err_out;
Fabio Estevam899b8342015-02-09 19:22:33 -02001983 ret = chip->scan_bbt(mtd);
1984 if (ret)
1985 goto err_out;
Huang Shijie885d71e2013-11-12 12:23:08 +08001986
Brian Norrisa61ae812015-10-30 20:33:25 -07001987 ret = mtd_device_register(mtd, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001988 if (ret)
1989 goto err_out;
1990 return 0;
1991
1992err_out:
Huang Shijieccce4172013-11-14 14:25:47 +08001993 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001994 return ret;
1995}
1996
Huang Shijiee10db1f2012-05-04 21:42:05 -04001997static const struct of_device_id gpmi_nand_id_table[] = {
1998 {
1999 .compatible = "fsl,imx23-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002000 .data = &gpmi_devdata_imx23,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002001 }, {
2002 .compatible = "fsl,imx28-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002003 .data = &gpmi_devdata_imx28,
Huang Shijie9013bb42012-05-04 21:42:06 -04002004 }, {
2005 .compatible = "fsl,imx6q-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002006 .data = &gpmi_devdata_imx6q,
Huang Shijie91f54982014-03-27 10:43:22 +08002007 }, {
2008 .compatible = "fsl,imx6sx-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002009 .data = &gpmi_devdata_imx6sx,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002010 }, {}
2011};
2012MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
2013
Bill Pemberton06f25512012-11-19 13:23:07 -05002014static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08002015{
Huang Shijie10a2bca2011-09-08 10:47:09 +08002016 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002017 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08002018 int ret;
2019
Huang Shijie6189ccc2014-03-21 18:19:39 +08002020 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
2021 if (!this)
2022 return -ENOMEM;
2023
Huang Shijiee10db1f2012-05-04 21:42:05 -04002024 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
2025 if (of_id) {
Huang Shijie6189ccc2014-03-21 18:19:39 +08002026 this->devdata = of_id->data;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002027 } else {
Huang Shijieda40c162013-11-20 10:09:43 +08002028 dev_err(&pdev->dev, "Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02002029 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002030 }
2031
Huang Shijie10a2bca2011-09-08 10:47:09 +08002032 platform_set_drvdata(pdev, this);
2033 this->pdev = pdev;
2034 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08002035
2036 ret = acquire_resources(this);
2037 if (ret)
2038 goto exit_acquire_resources;
2039
2040 ret = init_hardware(this);
2041 if (ret)
2042 goto exit_nfc_init;
2043
Huang Shijieccce4172013-11-14 14:25:47 +08002044 ret = gpmi_nand_init(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002045 if (ret)
2046 goto exit_nfc_init;
2047
Fabio Estevam490e2802012-09-05 11:35:24 -03002048 dev_info(this->dev, "driver registered.\n");
2049
Huang Shijie10a2bca2011-09-08 10:47:09 +08002050 return 0;
2051
2052exit_nfc_init:
2053 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002054exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03002055
Huang Shijie10a2bca2011-09-08 10:47:09 +08002056 return ret;
2057}
2058
Bill Pemberton810b7e02012-11-19 13:26:04 -05002059static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08002060{
2061 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2062
Huang Shijieccce4172013-11-14 14:25:47 +08002063 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002064 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002065 return 0;
2066}
2067
Huang Shijie026918e2015-12-02 16:47:40 -06002068#ifdef CONFIG_PM_SLEEP
2069static int gpmi_pm_suspend(struct device *dev)
2070{
2071 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2072
2073 release_dma_channels(this);
2074 return 0;
2075}
2076
2077static int gpmi_pm_resume(struct device *dev)
2078{
2079 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2080 int ret;
2081
2082 ret = acquire_dma_channels(this);
2083 if (ret < 0)
2084 return ret;
2085
2086 /* re-init the GPMI registers */
2087 this->flags &= ~GPMI_TIMING_INIT_OK;
2088 ret = gpmi_init(this);
2089 if (ret) {
2090 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2091 return ret;
2092 }
2093
2094 /* re-init the BCH registers */
2095 ret = bch_set_geometry(this);
2096 if (ret) {
2097 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2098 return ret;
2099 }
2100
2101 /* re-init others */
2102 gpmi_extra_init(this);
2103
2104 return 0;
2105}
2106#endif /* CONFIG_PM_SLEEP */
2107
2108static const struct dev_pm_ops gpmi_pm_ops = {
2109 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2110};
2111
Huang Shijie10a2bca2011-09-08 10:47:09 +08002112static struct platform_driver gpmi_nand_driver = {
2113 .driver = {
2114 .name = "gpmi-nand",
Huang Shijie026918e2015-12-02 16:47:40 -06002115 .pm = &gpmi_pm_ops,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002116 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002117 },
2118 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05002119 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002120};
Fabio Estevam490e2802012-09-05 11:35:24 -03002121module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002122
2123MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2124MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2125MODULE_LICENSE("GPL");