blob: 4721008e0289a519bf08f80a6790538f9e8612a3 [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 */
Huang Shijie10a2bca2011-09-08 10:47:09 +080050static struct nand_ecclayout gpmi_hw_ecclayout = {
51 .eccbytes = 0,
52 .eccpos = { 0, },
53 .oobfree = { {.offset = 0, .length = 0} }
54};
55
Huang Shijie6189ccc2014-03-21 18:19:39 +080056static const struct gpmi_devdata gpmi_devdata_imx23 = {
57 .type = IS_MX23,
58 .bch_max_ecc_strength = 20,
59 .max_chain_delay = 16,
60};
61
62static const struct gpmi_devdata gpmi_devdata_imx28 = {
63 .type = IS_MX28,
64 .bch_max_ecc_strength = 20,
65 .max_chain_delay = 16,
66};
67
68static const struct gpmi_devdata gpmi_devdata_imx6q = {
69 .type = IS_MX6Q,
70 .bch_max_ecc_strength = 40,
71 .max_chain_delay = 12,
72};
73
Huang Shijie91f54982014-03-27 10:43:22 +080074static const struct gpmi_devdata gpmi_devdata_imx6sx = {
75 .type = IS_MX6SX,
76 .bch_max_ecc_strength = 62,
77 .max_chain_delay = 12,
78};
79
Huang Shijie10a2bca2011-09-08 10:47:09 +080080static irqreturn_t bch_irq(int irq, void *cookie)
81{
82 struct gpmi_nand_data *this = cookie;
83
84 gpmi_clear_bch(this);
85 complete(&this->bch_done);
86 return IRQ_HANDLED;
87}
88
89/*
90 * Calculate the ECC strength by hand:
91 * E : The ECC strength.
92 * G : the length of Galois Field.
93 * N : The chunk count of per page.
94 * O : the oobsize of the NAND chip.
95 * M : the metasize of per page.
96 *
97 * The formula is :
98 * E * G * N
99 * ------------ <= (O - M)
100 * 8
101 *
102 * So, we get E by:
103 * (O - M) * 8
104 * E <= -------------
105 * G * N
106 */
107static inline int get_ecc_strength(struct gpmi_nand_data *this)
108{
109 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100110 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800111 int ecc_strength;
112
113 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
114 / (geo->gf_len * geo->ecc_chunk_count);
115
116 /* We need the minor even number. */
117 return round_down(ecc_strength, 2);
118}
119
Huang Shijie92d0e092013-01-29 09:23:38 +0800120static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
121{
122 struct bch_geometry *geo = &this->bch_geometry;
123
124 /* Do the sanity check. */
125 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
126 /* The mx23/mx28 only support the GF13. */
127 if (geo->gf_len == 14)
128 return false;
Huang Shijie92d0e092013-01-29 09:23:38 +0800129 }
Huang Shijie6189ccc2014-03-21 18:19:39 +0800130 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
Huang Shijie92d0e092013-01-29 09:23:38 +0800131}
132
Huang Shijie2febcdf2013-05-17 11:17:34 +0800133/*
134 * If we can get the ECC information from the nand chip, we do not
135 * need to calculate them ourselves.
136 *
137 * We may have available oob space in this case.
138 */
Han Xub8b0e462015-12-02 16:47:43 -0600139static int set_geometry_by_ecc_info(struct gpmi_nand_data *this)
Huang Shijie2febcdf2013-05-17 11:17:34 +0800140{
141 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100142 struct nand_chip *chip = &this->nand;
143 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie2febcdf2013-05-17 11:17:34 +0800144 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
145 unsigned int block_mark_bit_offset;
146
147 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
Han Xub8b0e462015-12-02 16:47:43 -0600148 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800149
150 switch (chip->ecc_step_ds) {
151 case SZ_512:
152 geo->gf_len = 13;
153 break;
154 case SZ_1K:
155 geo->gf_len = 14;
156 break;
157 default:
158 dev_err(this->dev,
159 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
160 chip->ecc_strength_ds, chip->ecc_step_ds);
Han Xub8b0e462015-12-02 16:47:43 -0600161 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800162 }
163 geo->ecc_chunk_size = chip->ecc_step_ds;
164 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
165 if (!gpmi_check_ecc(this))
Han Xub8b0e462015-12-02 16:47:43 -0600166 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800167
168 /* Keep the C >= O */
169 if (geo->ecc_chunk_size < mtd->oobsize) {
170 dev_err(this->dev,
171 "unsupported nand chip. ecc size: %d, oob size : %d\n",
172 chip->ecc_step_ds, mtd->oobsize);
Han Xub8b0e462015-12-02 16:47:43 -0600173 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800174 }
175
176 /* The default value, see comment in the legacy_set_geometry(). */
177 geo->metadata_size = 10;
178
179 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
180
181 /*
182 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
183 *
184 * | P |
185 * |<----------------------------------------------------->|
186 * | |
187 * | (Block Mark) |
188 * | P' | | | |
189 * |<-------------------------------------------->| D | | O' |
190 * | |<---->| |<--->|
191 * V V V V V
192 * +---+----------+-+----------+-+----------+-+----------+-+-----+
193 * | M | data |E| data |E| data |E| data |E| |
194 * +---+----------+-+----------+-+----------+-+----------+-+-----+
195 * ^ ^
196 * | O |
197 * |<------------>|
198 * | |
199 *
200 * P : the page size for BCH module.
201 * E : The ECC strength.
202 * G : the length of Galois Field.
203 * N : The chunk count of per page.
204 * M : the metasize of per page.
205 * C : the ecc chunk size, aka the "data" above.
206 * P': the nand chip's page size.
207 * O : the nand chip's oob size.
208 * O': the free oob.
209 *
210 * The formula for P is :
211 *
212 * E * G * N
213 * P = ------------ + P' + M
214 * 8
215 *
216 * The position of block mark moves forward in the ECC-based view
217 * of page, and the delta is:
218 *
219 * E * G * (N - 1)
220 * D = (---------------- + M)
221 * 8
222 *
223 * Please see the comment in legacy_set_geometry().
224 * With the condition C >= O , we still can get same result.
225 * So the bit position of the physical block mark within the ECC-based
226 * view of the page is :
227 * (P' - D) * 8
228 */
229 geo->page_size = mtd->writesize + geo->metadata_size +
230 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
231
232 /* The available oob size we have. */
233 if (geo->page_size < mtd->writesize + mtd->oobsize) {
234 of->offset = geo->page_size - mtd->writesize;
235 of->length = mtd->oobsize - of->offset;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800236 }
237
238 geo->payload_size = mtd->writesize;
239
240 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
241 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
242 + ALIGN(geo->ecc_chunk_count, 4);
243
244 if (!this->swap_block_mark)
Han Xub8b0e462015-12-02 16:47:43 -0600245 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800246
247 /* For bit swap. */
248 block_mark_bit_offset = mtd->writesize * 8 -
249 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
250 + geo->metadata_size * 8);
251
252 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
253 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Han Xub8b0e462015-12-02 16:47:43 -0600254 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800255}
256
257static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800258{
259 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100260 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800261 unsigned int metadata_size;
262 unsigned int status_size;
263 unsigned int block_mark_bit_offset;
264
265 /*
266 * The size of the metadata can be changed, though we set it to 10
267 * bytes now. But it can't be too large, because we have to save
268 * enough space for BCH.
269 */
270 geo->metadata_size = 10;
271
272 /* The default for the length of Galois Field. */
273 geo->gf_len = 13;
274
Huang Shijie9ff16f02013-01-25 14:04:07 +0800275 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800276 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800277 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800278 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800279 geo->gf_len = 14;
280 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800281
282 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
283
284 /* We use the same ECC strength for all chunks. */
285 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800286 if (!gpmi_check_ecc(this)) {
287 dev_err(this->dev,
Han Xub8b0e462015-12-02 16:47:43 -0600288 "ecc strength: %d cannot be supported by the controller (%d)\n"
289 "try to use minimum ecc strength that NAND chip required\n",
Lothar Waßmannd8c03722014-06-12 15:20:42 +0200290 geo->ecc_strength,
Huang Shijie6189ccc2014-03-21 18:19:39 +0800291 this->devdata->bch_max_ecc_strength);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800292 return -EINVAL;
293 }
294
295 geo->page_size = mtd->writesize + mtd->oobsize;
296 geo->payload_size = mtd->writesize;
297
298 /*
299 * The auxiliary buffer contains the metadata and the ECC status. The
300 * metadata is padded to the nearest 32-bit boundary. The ECC status
301 * contains one byte for every ECC chunk, and is also padded to the
302 * nearest 32-bit boundary.
303 */
304 metadata_size = ALIGN(geo->metadata_size, 4);
305 status_size = ALIGN(geo->ecc_chunk_count, 4);
306
307 geo->auxiliary_size = metadata_size + status_size;
308 geo->auxiliary_status_offset = metadata_size;
309
310 if (!this->swap_block_mark)
311 return 0;
312
313 /*
314 * We need to compute the byte and bit offsets of
315 * the physical block mark within the ECC-based view of the page.
316 *
317 * NAND chip with 2K page shows below:
318 * (Block Mark)
319 * | |
320 * | D |
321 * |<---->|
322 * V V
323 * +---+----------+-+----------+-+----------+-+----------+-+
324 * | M | data |E| data |E| data |E| data |E|
325 * +---+----------+-+----------+-+----------+-+----------+-+
326 *
327 * The position of block mark moves forward in the ECC-based view
328 * of page, and the delta is:
329 *
330 * E * G * (N - 1)
331 * D = (---------------- + M)
332 * 8
333 *
334 * With the formula to compute the ECC strength, and the condition
335 * : C >= O (C is the ecc chunk size)
336 *
337 * It's easy to deduce to the following result:
338 *
339 * E * G (O - M) C - M C - M
340 * ----------- <= ------- <= -------- < ---------
341 * 8 N N (N - 1)
342 *
343 * So, we get:
344 *
345 * E * G * (N - 1)
346 * D = (---------------- + M) < C
347 * 8
348 *
349 * The above inequality means the position of block mark
350 * within the ECC-based view of the page is still in the data chunk,
351 * and it's NOT in the ECC bits of the chunk.
352 *
353 * Use the following to compute the bit position of the
354 * physical block mark within the ECC-based view of the page:
355 * (page_size - D) * 8
356 *
357 * --Huang Shijie
358 */
359 block_mark_bit_offset = mtd->writesize * 8 -
360 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
361 + geo->metadata_size * 8);
362
363 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
364 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
365 return 0;
366}
367
Huang Shijie2febcdf2013-05-17 11:17:34 +0800368int common_nfc_set_geometry(struct gpmi_nand_data *this)
369{
Han Xub8b0e462015-12-02 16:47:43 -0600370 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
371 || legacy_set_geometry(this))
372 return set_geometry_by_ecc_info(this);
373
374 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800375}
376
Huang Shijie10a2bca2011-09-08 10:47:09 +0800377struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
378{
Huang Shijiea7c12d02013-08-27 17:29:05 +0800379 /* We use the DMA channel 0 to access all the nand chips. */
380 return this->dma_chans[0];
Huang Shijie10a2bca2011-09-08 10:47:09 +0800381}
382
383/* Can we use the upper's buffer directly for DMA? */
384void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
385{
386 struct scatterlist *sgl = &this->data_sgl;
387 int ret;
388
Huang Shijie10a2bca2011-09-08 10:47:09 +0800389 /* first try to map the upper buffer directly */
Huang Shijie0ff76a92013-12-18 23:41:00 +0800390 if (virt_addr_valid(this->upper_buf) &&
391 !object_is_on_stack(this->upper_buf)) {
392 sg_init_one(sgl, this->upper_buf, this->upper_len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800393 ret = dma_map_sg(this->dev, sgl, 1, dr);
394 if (ret == 0)
Huang Shijie0ff76a92013-12-18 23:41:00 +0800395 goto map_fail;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800396
Huang Shijie0ff76a92013-12-18 23:41:00 +0800397 this->direct_dma_map_ok = true;
398 return;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800399 }
Huang Shijie0ff76a92013-12-18 23:41:00 +0800400
401map_fail:
402 /* We have to use our own DMA buffer. */
403 sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
404
405 if (dr == DMA_TO_DEVICE)
406 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
407
408 dma_map_sg(this->dev, sgl, 1, dr);
409
410 this->direct_dma_map_ok = false;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800411}
412
413/* This will be called after the DMA operation is finished. */
414static void dma_irq_callback(void *param)
415{
416 struct gpmi_nand_data *this = param;
417 struct completion *dma_c = &this->dma_done;
418
Huang Shijie10a2bca2011-09-08 10:47:09 +0800419 switch (this->dma_type) {
420 case DMA_FOR_COMMAND:
421 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
422 break;
423
424 case DMA_FOR_READ_DATA:
425 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
426 if (this->direct_dma_map_ok == false)
427 memcpy(this->upper_buf, this->data_buffer_dma,
428 this->upper_len);
429 break;
430
431 case DMA_FOR_WRITE_DATA:
432 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
433 break;
434
435 case DMA_FOR_READ_ECC_PAGE:
436 case DMA_FOR_WRITE_ECC_PAGE:
437 /* We have to wait the BCH interrupt to finish. */
438 break;
439
440 default:
Huang Shijieda40c162013-11-20 10:09:43 +0800441 dev_err(this->dev, "in wrong DMA operation.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800442 }
Huang Shijie7b3d2fb2013-11-11 12:13:45 +0800443
444 complete(dma_c);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800445}
446
447int start_dma_without_bch_irq(struct gpmi_nand_data *this,
448 struct dma_async_tx_descriptor *desc)
449{
450 struct completion *dma_c = &this->dma_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500451 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800452
453 init_completion(dma_c);
454
455 desc->callback = dma_irq_callback;
456 desc->callback_param = this;
457 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800458 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800459
460 /* Wait for the interrupt from the DMA block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500461 timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
462 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800463 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
464 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800465 gpmi_dump_info(this);
466 return -ETIMEDOUT;
467 }
468 return 0;
469}
470
471/*
472 * This function is used in BCH reading or BCH writing pages.
473 * It will wait for the BCH interrupt as long as ONE second.
474 * Actually, we must wait for two interrupts :
475 * [1] firstly the DMA interrupt and
476 * [2] secondly the BCH interrupt.
477 */
478int start_dma_with_bch_irq(struct gpmi_nand_data *this,
479 struct dma_async_tx_descriptor *desc)
480{
481 struct completion *bch_c = &this->bch_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500482 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800483
484 /* Prepare to receive an interrupt from the BCH block. */
485 init_completion(bch_c);
486
487 /* start the DMA */
488 start_dma_without_bch_irq(this, desc);
489
490 /* Wait for the interrupt from the BCH block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500491 timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
492 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800493 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
494 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800495 gpmi_dump_info(this);
496 return -ETIMEDOUT;
497 }
498 return 0;
499}
500
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800501static int acquire_register_block(struct gpmi_nand_data *this,
502 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800503{
504 struct platform_device *pdev = this->pdev;
505 struct resources *res = &this->resources;
506 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800507 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800508
509 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
Huang Shijie87a9d692013-11-14 14:25:48 +0800510 p = devm_ioremap_resource(&pdev->dev, r);
511 if (IS_ERR(p))
512 return PTR_ERR(p);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800513
514 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
515 res->gpmi_regs = p;
516 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
517 res->bch_regs = p;
518 else
Huang Shijieda40c162013-11-20 10:09:43 +0800519 dev_err(this->dev, "unknown resource name : %s\n", res_name);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800520
521 return 0;
522}
523
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800524static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800525{
526 struct platform_device *pdev = this->pdev;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800527 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
528 struct resource *r;
529 int err;
530
531 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
532 if (!r) {
Huang Shijieda40c162013-11-20 10:09:43 +0800533 dev_err(this->dev, "Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200534 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800535 }
536
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800537 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
538 if (err)
539 dev_err(this->dev, "error requesting BCH IRQ\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800540
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800541 return err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800542}
543
Huang Shijie10a2bca2011-09-08 10:47:09 +0800544static void release_dma_channels(struct gpmi_nand_data *this)
545{
546 unsigned int i;
547 for (i = 0; i < DMA_CHANS; i++)
548 if (this->dma_chans[i]) {
549 dma_release_channel(this->dma_chans[i]);
550 this->dma_chans[i] = NULL;
551 }
552}
553
Bill Pemberton06f25512012-11-19 13:23:07 -0500554static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800555{
556 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400557 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400558
559 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800560 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400561 if (!dma_chan) {
Huang Shijieda40c162013-11-20 10:09:43 +0800562 dev_err(this->dev, "Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400563 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800564 }
565
Huang Shijiee10db1f2012-05-04 21:42:05 -0400566 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800567 return 0;
568
569acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800570 release_dma_channels(this);
571 return -EINVAL;
572}
573
Huang Shijieff506172012-07-02 21:39:32 -0400574static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
575 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
576};
577
Bill Pemberton06f25512012-11-19 13:23:07 -0500578static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400579{
580 struct resources *r = &this->resources;
581 char **extra_clks = NULL;
582 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200583 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400584
585 /* The main clock is stored in the first. */
Fabio Estevam554cbc52013-11-07 22:32:38 -0200586 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200587 if (IS_ERR(r->clock[0])) {
588 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400589 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200590 }
Huang Shijieff506172012-07-02 21:39:32 -0400591
592 /* Get extra clocks */
Huang Shijie91f54982014-03-27 10:43:22 +0800593 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400594 extra_clks = extra_clks_for_mx6q;
595 if (!extra_clks)
596 return 0;
597
598 for (i = 1; i < GPMI_CLK_MAX; i++) {
599 if (extra_clks[i - 1] == NULL)
600 break;
601
Fabio Estevam554cbc52013-11-07 22:32:38 -0200602 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200603 if (IS_ERR(clk)) {
604 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400605 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200606 }
Huang Shijieff506172012-07-02 21:39:32 -0400607
608 r->clock[i] = clk;
609 }
610
Huang Shijie91f54982014-03-27 10:43:22 +0800611 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400612 /*
Huang Shijie91f54982014-03-27 10:43:22 +0800613 * Set the default value for the gpmi clock.
Huang Shijieff506172012-07-02 21:39:32 -0400614 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800615 * If you want to use the ONFI nand which is in the
616 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400617 */
618 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800619
Huang Shijieff506172012-07-02 21:39:32 -0400620 return 0;
621
622err_clock:
623 dev_dbg(this->dev, "failed in finding the clocks.\n");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200624 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400625}
626
Bill Pemberton06f25512012-11-19 13:23:07 -0500627static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800628{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800629 int ret;
630
631 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
632 if (ret)
633 goto exit_regs;
634
635 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
636 if (ret)
637 goto exit_regs;
638
639 ret = acquire_bch_irq(this, bch_irq);
640 if (ret)
641 goto exit_regs;
642
643 ret = acquire_dma_channels(this);
644 if (ret)
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800645 goto exit_regs;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800646
Huang Shijieff506172012-07-02 21:39:32 -0400647 ret = gpmi_get_clks(this);
648 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800649 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800650 return 0;
651
652exit_clock:
653 release_dma_channels(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800654exit_regs:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800655 return ret;
656}
657
658static void release_resources(struct gpmi_nand_data *this)
659{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800660 release_dma_channels(this);
661}
662
Bill Pemberton06f25512012-11-19 13:23:07 -0500663static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800664{
665 int ret;
666
667 /*
668 * This structure contains the "safe" GPMI timing that should succeed
669 * with any NAND Flash device
670 * (although, with less-than-optimal performance).
671 */
672 struct nand_timing safe_timing = {
673 .data_setup_in_ns = 80,
674 .data_hold_in_ns = 60,
675 .address_setup_in_ns = 25,
676 .gpmi_sample_delay_in_ns = 6,
677 .tREA_in_ns = -1,
678 .tRLOH_in_ns = -1,
679 .tRHOH_in_ns = -1,
680 };
681
682 /* Initialize the hardwares. */
683 ret = gpmi_init(this);
684 if (ret)
685 return ret;
686
687 this->timing = safe_timing;
688 return 0;
689}
690
691static int read_page_prepare(struct gpmi_nand_data *this,
692 void *destination, unsigned length,
693 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
694 void **use_virt, dma_addr_t *use_phys)
695{
696 struct device *dev = this->dev;
697
698 if (virt_addr_valid(destination)) {
699 dma_addr_t dest_phys;
700
701 dest_phys = dma_map_single(dev, destination,
702 length, DMA_FROM_DEVICE);
703 if (dma_mapping_error(dev, dest_phys)) {
704 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800705 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800706 return -ENOMEM;
707 }
708 goto map_failed;
709 }
710 *use_virt = destination;
711 *use_phys = dest_phys;
712 this->direct_dma_map_ok = true;
713 return 0;
714 }
715
716map_failed:
717 *use_virt = alt_virt;
718 *use_phys = alt_phys;
719 this->direct_dma_map_ok = false;
720 return 0;
721}
722
723static inline void read_page_end(struct gpmi_nand_data *this,
724 void *destination, unsigned length,
725 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
726 void *used_virt, dma_addr_t used_phys)
727{
728 if (this->direct_dma_map_ok)
729 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
730}
731
732static inline void read_page_swap_end(struct gpmi_nand_data *this,
733 void *destination, unsigned length,
734 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
735 void *used_virt, dma_addr_t used_phys)
736{
737 if (!this->direct_dma_map_ok)
738 memcpy(destination, alt_virt, length);
739}
740
741static int send_page_prepare(struct gpmi_nand_data *this,
742 const void *source, unsigned length,
743 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
744 const void **use_virt, dma_addr_t *use_phys)
745{
746 struct device *dev = this->dev;
747
748 if (virt_addr_valid(source)) {
749 dma_addr_t source_phys;
750
751 source_phys = dma_map_single(dev, (void *)source, length,
752 DMA_TO_DEVICE);
753 if (dma_mapping_error(dev, source_phys)) {
754 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800755 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800756 return -ENOMEM;
757 }
758 goto map_failed;
759 }
760 *use_virt = source;
761 *use_phys = source_phys;
762 return 0;
763 }
764map_failed:
765 /*
766 * Copy the content of the source buffer into the alternate
767 * buffer and set up the return values accordingly.
768 */
769 memcpy(alt_virt, source, length);
770
771 *use_virt = alt_virt;
772 *use_phys = alt_phys;
773 return 0;
774}
775
776static void send_page_end(struct gpmi_nand_data *this,
777 const void *source, unsigned length,
778 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
779 const void *used_virt, dma_addr_t used_phys)
780{
781 struct device *dev = this->dev;
782 if (used_virt == source)
783 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
784}
785
786static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
787{
788 struct device *dev = this->dev;
789
790 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
791 dma_free_coherent(dev, this->page_buffer_size,
792 this->page_buffer_virt,
793 this->page_buffer_phys);
794 kfree(this->cmd_buffer);
795 kfree(this->data_buffer_dma);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100796 kfree(this->raw_buffer);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800797
798 this->cmd_buffer = NULL;
799 this->data_buffer_dma = NULL;
Han Xu2cd395d2016-04-04 15:41:29 -0500800 this->raw_buffer = NULL;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800801 this->page_buffer_virt = NULL;
802 this->page_buffer_size = 0;
803}
804
805/* Allocate the DMA buffers */
806static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
807{
808 struct bch_geometry *geo = &this->bch_geometry;
809 struct device *dev = this->dev;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100810 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800811
812 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800813 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800814 if (this->cmd_buffer == NULL)
815 goto error_alloc;
816
Huang Shijie06f216c2013-12-18 23:40:59 +0800817 /*
818 * [2] Allocate a read/write data buffer.
819 * The gpmi_alloc_dma_buffer can be called twice.
820 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
821 * is called before the nand_scan_ident; and we allocate a buffer
822 * of the real NAND page size when the gpmi_alloc_dma_buffer is
823 * called after the nand_scan_ident.
824 */
825 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
826 GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800827 if (this->data_buffer_dma == NULL)
828 goto error_alloc;
829
830 /*
831 * [3] Allocate the page buffer.
832 *
833 * Both the payload buffer and the auxiliary buffer must appear on
834 * 32-bit boundaries. We presume the size of the payload buffer is a
835 * power of two and is much larger than four, which guarantees the
836 * auxiliary buffer will appear on a 32-bit boundary.
837 */
838 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
839 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
840 &this->page_buffer_phys, GFP_DMA);
841 if (!this->page_buffer_virt)
842 goto error_alloc;
843
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100844 this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
845 if (!this->raw_buffer)
846 goto error_alloc;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800847
848 /* Slice up the page buffer. */
849 this->payload_virt = this->page_buffer_virt;
850 this->payload_phys = this->page_buffer_phys;
851 this->auxiliary_virt = this->payload_virt + geo->payload_size;
852 this->auxiliary_phys = this->payload_phys + geo->payload_size;
853 return 0;
854
855error_alloc:
856 gpmi_free_dma_buffer(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800857 return -ENOMEM;
858}
859
860static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
861{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100862 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100863 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800864 int ret;
865
866 /*
867 * Every operation begins with a command byte and a series of zero or
868 * more address bytes. These are distinguished by either the Address
869 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
870 * asserted. When MTD is ready to execute the command, it will deassert
871 * both latch enables.
872 *
873 * Rather than run a separate DMA operation for every single byte, we
874 * queue them up and run a single DMA operation for the entire series
875 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
876 */
877 if ((ctrl & (NAND_ALE | NAND_CLE))) {
878 if (data != NAND_CMD_NONE)
879 this->cmd_buffer[this->command_length++] = data;
880 return;
881 }
882
883 if (!this->command_length)
884 return;
885
886 ret = gpmi_send_command(this);
887 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +0800888 dev_err(this->dev, "Chip: %u, Error %d\n",
889 this->current_chip, ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800890
891 this->command_length = 0;
892}
893
894static int gpmi_dev_ready(struct mtd_info *mtd)
895{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100896 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100897 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800898
899 return gpmi_is_ready(this, this->current_chip);
900}
901
902static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
903{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100904 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100905 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800906
907 if ((this->current_chip < 0) && (chipnr >= 0))
908 gpmi_begin(this);
909 else if ((this->current_chip >= 0) && (chipnr < 0))
910 gpmi_end(this);
911
912 this->current_chip = chipnr;
913}
914
915static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
916{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100917 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100918 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800919
Huang Shijiec2325962013-11-20 10:09:44 +0800920 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800921 this->upper_buf = buf;
922 this->upper_len = len;
923
924 gpmi_read_data(this);
925}
926
927static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
928{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100929 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100930 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800931
Huang Shijiec2325962013-11-20 10:09:44 +0800932 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800933 this->upper_buf = (uint8_t *)buf;
934 this->upper_len = len;
935
936 gpmi_send_data(this);
937}
938
939static uint8_t gpmi_read_byte(struct mtd_info *mtd)
940{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100941 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100942 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800943 uint8_t *buf = this->data_buffer_dma;
944
945 gpmi_read_buf(mtd, buf, 1);
946 return buf[0];
947}
948
949/*
950 * Handles block mark swapping.
951 * It can be called in swapping the block mark, or swapping it back,
952 * because the the operations are the same.
953 */
954static void block_mark_swapping(struct gpmi_nand_data *this,
955 void *payload, void *auxiliary)
956{
957 struct bch_geometry *nfc_geo = &this->bch_geometry;
958 unsigned char *p;
959 unsigned char *a;
960 unsigned int bit;
961 unsigned char mask;
962 unsigned char from_data;
963 unsigned char from_oob;
964
965 if (!this->swap_block_mark)
966 return;
967
968 /*
969 * If control arrives here, we're swapping. Make some convenience
970 * variables.
971 */
972 bit = nfc_geo->block_mark_bit_offset;
973 p = payload + nfc_geo->block_mark_byte_offset;
974 a = auxiliary;
975
976 /*
977 * Get the byte from the data area that overlays the block mark. Since
978 * the ECC engine applies its own view to the bits in the page, the
979 * physical block mark won't (in general) appear on a byte boundary in
980 * the data.
981 */
982 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
983
984 /* Get the byte from the OOB. */
985 from_oob = a[0];
986
987 /* Swap them. */
988 a[0] = from_data;
989
990 mask = (0x1 << bit) - 1;
991 p[0] = (p[0] & mask) | (from_oob << bit);
992
993 mask = ~0 << bit;
994 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
995}
996
997static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700998 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800999{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001000 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001001 struct bch_geometry *nfc_geo = &this->bch_geometry;
1002 void *payload_virt;
1003 dma_addr_t payload_phys;
1004 void *auxiliary_virt;
1005 dma_addr_t auxiliary_phys;
1006 unsigned int i;
1007 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -06001008 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001009 int ret;
1010
Huang Shijiec2325962013-11-20 10:09:44 +08001011 dev_dbg(this->dev, "page number is : %d\n", page);
Huang Shijie4a57d672014-01-03 11:01:41 +08001012 ret = read_page_prepare(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001013 this->payload_virt, this->payload_phys,
1014 nfc_geo->payload_size,
1015 &payload_virt, &payload_phys);
1016 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001017 dev_err(this->dev, "Inadequate DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001018 ret = -ENOMEM;
1019 return ret;
1020 }
1021 auxiliary_virt = this->auxiliary_virt;
1022 auxiliary_phys = this->auxiliary_phys;
1023
1024 /* go! */
1025 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
Huang Shijie4a57d672014-01-03 11:01:41 +08001026 read_page_end(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001027 this->payload_virt, this->payload_phys,
1028 nfc_geo->payload_size,
1029 payload_virt, payload_phys);
1030 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001031 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001032 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001033 }
1034
1035 /* handle the block mark swapping */
1036 block_mark_swapping(this, payload_virt, auxiliary_virt);
1037
1038 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001039 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001040
1041 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1042 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1043 continue;
1044
1045 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001046 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001047 continue;
1048 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001049 mtd->ecc_stats.corrected += *status;
1050 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001051 }
1052
Brian Norris7725cc82012-05-02 10:15:02 -07001053 if (oob_required) {
1054 /*
1055 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1056 * for details about our policy for delivering the OOB.
1057 *
1058 * We fill the caller's buffer with set bits, and then copy the
1059 * block mark to th caller's buffer. Note that, if block mark
1060 * swapping was necessary, it has already been done, so we can
1061 * rely on the first byte of the auxiliary buffer to contain
1062 * the block mark.
1063 */
1064 memset(chip->oob_poi, ~0, mtd->oobsize);
1065 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001066 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001067
Huang Shijie4a57d672014-01-03 11:01:41 +08001068 read_page_swap_end(this, buf, nfc_geo->payload_size,
Sascha Hauer6023813a2012-06-26 17:26:16 +02001069 this->payload_virt, this->payload_phys,
1070 nfc_geo->payload_size,
1071 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001072
1073 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001074}
1075
Huang Shijieb8e29312014-01-03 11:01:42 +08001076/* Fake a virtual small page for the subpage read */
1077static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1078 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1079{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001080 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijieb8e29312014-01-03 11:01:42 +08001081 void __iomem *bch_regs = this->resources.bch_regs;
1082 struct bch_geometry old_geo = this->bch_geometry;
1083 struct bch_geometry *geo = &this->bch_geometry;
1084 int size = chip->ecc.size; /* ECC chunk size */
1085 int meta, n, page_size;
1086 u32 r1_old, r2_old, r1_new, r2_new;
1087 unsigned int max_bitflips;
1088 int first, last, marker_pos;
1089 int ecc_parity_size;
1090 int col = 0;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001091 int old_swap_block_mark = this->swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001092
1093 /* The size of ECC parity */
1094 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1095
1096 /* Align it with the chunk size */
1097 first = offs / size;
1098 last = (offs + len - 1) / size;
1099
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001100 if (this->swap_block_mark) {
1101 /*
1102 * Find the chunk which contains the Block Marker.
1103 * If this chunk is in the range of [first, last],
1104 * we have to read out the whole page.
1105 * Why? since we had swapped the data at the position of Block
1106 * Marker to the metadata which is bound with the chunk 0.
1107 */
1108 marker_pos = geo->block_mark_byte_offset / size;
1109 if (last >= marker_pos && first <= marker_pos) {
1110 dev_dbg(this->dev,
1111 "page:%d, first:%d, last:%d, marker at:%d\n",
Huang Shijieb8e29312014-01-03 11:01:42 +08001112 page, first, last, marker_pos);
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001113 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1114 }
Huang Shijieb8e29312014-01-03 11:01:42 +08001115 }
1116
1117 meta = geo->metadata_size;
1118 if (first) {
1119 col = meta + (size + ecc_parity_size) * first;
1120 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1121
1122 meta = 0;
1123 buf = buf + first * size;
1124 }
1125
1126 /* Save the old environment */
1127 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1128 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1129
1130 /* change the BCH registers and bch_geometry{} */
1131 n = last - first + 1;
1132 page_size = meta + (size + ecc_parity_size) * n;
1133
1134 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1135 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1136 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1137 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1138 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1139
1140 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1141 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1142 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1143
1144 geo->ecc_chunk_count = n;
1145 geo->payload_size = n * size;
1146 geo->page_size = page_size;
1147 geo->auxiliary_status_offset = ALIGN(meta, 4);
1148
1149 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1150 page, offs, len, col, first, n, page_size);
1151
1152 /* Read the subpage now */
1153 this->swap_block_mark = false;
1154 max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1155
1156 /* Restore */
1157 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1158 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1159 this->bch_geometry = old_geo;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001160 this->swap_block_mark = old_swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001161
1162 return max_bitflips;
1163}
1164
Josh Wufdbad98d2012-06-25 18:07:45 +08001165static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001166 const uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001167{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001168 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001169 struct bch_geometry *nfc_geo = &this->bch_geometry;
1170 const void *payload_virt;
1171 dma_addr_t payload_phys;
1172 const void *auxiliary_virt;
1173 dma_addr_t auxiliary_phys;
1174 int ret;
1175
Huang Shijiec2325962013-11-20 10:09:44 +08001176 dev_dbg(this->dev, "ecc write page.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001177 if (this->swap_block_mark) {
1178 /*
1179 * If control arrives here, we're doing block mark swapping.
1180 * Since we can't modify the caller's buffers, we must copy them
1181 * into our own.
1182 */
1183 memcpy(this->payload_virt, buf, mtd->writesize);
1184 payload_virt = this->payload_virt;
1185 payload_phys = this->payload_phys;
1186
1187 memcpy(this->auxiliary_virt, chip->oob_poi,
1188 nfc_geo->auxiliary_size);
1189 auxiliary_virt = this->auxiliary_virt;
1190 auxiliary_phys = this->auxiliary_phys;
1191
1192 /* Handle block mark swapping. */
1193 block_mark_swapping(this,
Lothar Waßmann6a760962014-06-12 15:20:41 +02001194 (void *)payload_virt, (void *)auxiliary_virt);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001195 } else {
1196 /*
1197 * If control arrives here, we're not doing block mark swapping,
1198 * so we can to try and use the caller's buffers.
1199 */
1200 ret = send_page_prepare(this,
1201 buf, mtd->writesize,
1202 this->payload_virt, this->payload_phys,
1203 nfc_geo->payload_size,
1204 &payload_virt, &payload_phys);
1205 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001206 dev_err(this->dev, "Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001207 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001208 }
1209
1210 ret = send_page_prepare(this,
1211 chip->oob_poi, mtd->oobsize,
1212 this->auxiliary_virt, this->auxiliary_phys,
1213 nfc_geo->auxiliary_size,
1214 &auxiliary_virt, &auxiliary_phys);
1215 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001216 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001217 goto exit_auxiliary;
1218 }
1219 }
1220
1221 /* Ask the NFC. */
1222 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1223 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +08001224 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001225
1226 if (!this->swap_block_mark) {
1227 send_page_end(this, chip->oob_poi, mtd->oobsize,
1228 this->auxiliary_virt, this->auxiliary_phys,
1229 nfc_geo->auxiliary_size,
1230 auxiliary_virt, auxiliary_phys);
1231exit_auxiliary:
1232 send_page_end(this, buf, mtd->writesize,
1233 this->payload_virt, this->payload_phys,
1234 nfc_geo->payload_size,
1235 payload_virt, payload_phys);
1236 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001237
1238 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001239}
1240
1241/*
1242 * There are several places in this driver where we have to handle the OOB and
1243 * block marks. This is the function where things are the most complicated, so
1244 * this is where we try to explain it all. All the other places refer back to
1245 * here.
1246 *
1247 * These are the rules, in order of decreasing importance:
1248 *
1249 * 1) Nothing the caller does can be allowed to imperil the block mark.
1250 *
1251 * 2) In read operations, the first byte of the OOB we return must reflect the
1252 * true state of the block mark, no matter where that block mark appears in
1253 * the physical page.
1254 *
1255 * 3) ECC-based read operations return an OOB full of set bits (since we never
1256 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1257 * return).
1258 *
1259 * 4) "Raw" read operations return a direct view of the physical bytes in the
1260 * page, using the conventional definition of which bytes are data and which
1261 * are OOB. This gives the caller a way to see the actual, physical bytes
1262 * in the page, without the distortions applied by our ECC engine.
1263 *
1264 *
1265 * What we do for this specific read operation depends on two questions:
1266 *
1267 * 1) Are we doing a "raw" read, or an ECC-based read?
1268 *
1269 * 2) Are we using block mark swapping or transcription?
1270 *
1271 * There are four cases, illustrated by the following Karnaugh map:
1272 *
1273 * | Raw | ECC-based |
1274 * -------------+-------------------------+-------------------------+
1275 * | Read the conventional | |
1276 * | OOB at the end of the | |
1277 * Swapping | page and return it. It | |
1278 * | contains exactly what | |
1279 * | we want. | Read the block mark and |
1280 * -------------+-------------------------+ return it in a buffer |
1281 * | Read the conventional | full of set bits. |
1282 * | OOB at the end of the | |
1283 * | page and also the block | |
1284 * Transcribing | mark in the metadata. | |
1285 * | Copy the block mark | |
1286 * | into the first byte of | |
1287 * | the OOB. | |
1288 * -------------+-------------------------+-------------------------+
1289 *
1290 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1291 * giving an accurate view of the actual, physical bytes in the page (we're
1292 * overwriting the block mark). That's OK because it's more important to follow
1293 * rule #2.
1294 *
1295 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1296 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1297 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1298 * ECC-based or raw view of the page is implicit in which function it calls
1299 * (there is a similar pair of ECC-based/raw functions for writing).
Huang Shijie10a2bca2011-09-08 10:47:09 +08001300 */
1301static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001302 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001303{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001304 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001305
Huang Shijiec2325962013-11-20 10:09:44 +08001306 dev_dbg(this->dev, "page number is %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001307 /* clear the OOB buffer */
1308 memset(chip->oob_poi, ~0, mtd->oobsize);
1309
1310 /* Read out the conventional OOB. */
1311 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1312 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1313
1314 /*
1315 * Now, we want to make sure the block mark is correct. In the
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001316 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1317 * Otherwise, we need to explicitly read it.
Huang Shijie10a2bca2011-09-08 10:47:09 +08001318 */
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001319 if (GPMI_IS_MX23(this)) {
Huang Shijie10a2bca2011-09-08 10:47:09 +08001320 /* Read the block mark into the first byte of the OOB buffer. */
1321 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1322 chip->oob_poi[0] = chip->read_byte(mtd);
1323 }
1324
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001325 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001326}
1327
1328static int
1329gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1330{
Boris Brezillon191a8292016-02-03 20:11:44 +01001331 struct mtd_oob_region of = { };
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001332 int status = 0;
1333
1334 /* Do we have available oob area? */
Boris Brezillon191a8292016-02-03 20:11:44 +01001335 mtd_ooblayout_free(mtd, 0, &of);
1336 if (!of.length)
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001337 return -EPERM;
1338
1339 if (!nand_is_slc(chip))
1340 return -EPERM;
1341
Boris Brezillon191a8292016-02-03 20:11:44 +01001342 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of.offset, page);
1343 chip->write_buf(mtd, chip->oob_poi + of.offset, of.length);
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001344 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1345
1346 status = chip->waitfunc(mtd, chip);
1347 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001348}
1349
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001350/*
1351 * This function reads a NAND page without involving the ECC engine (no HW
1352 * ECC correction).
1353 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1354 * inline (interleaved with payload DATA), and do not align data chunk on
1355 * byte boundaries.
1356 * We thus need to take care moving the payload data and ECC bits stored in the
1357 * page into the provided buffers, which is why we're using gpmi_copy_bits.
1358 *
1359 * See set_geometry_by_ecc_info inline comments to have a full description
1360 * of the layout used by the GPMI controller.
1361 */
1362static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
1363 struct nand_chip *chip, uint8_t *buf,
1364 int oob_required, int page)
1365{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001366 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001367 struct bch_geometry *nfc_geo = &this->bch_geometry;
1368 int eccsize = nfc_geo->ecc_chunk_size;
1369 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1370 u8 *tmp_buf = this->raw_buffer;
1371 size_t src_bit_off;
1372 size_t oob_bit_off;
1373 size_t oob_byte_off;
1374 uint8_t *oob = chip->oob_poi;
1375 int step;
1376
1377 chip->read_buf(mtd, tmp_buf,
1378 mtd->writesize + mtd->oobsize);
1379
1380 /*
1381 * If required, swap the bad block marker and the data stored in the
1382 * metadata section, so that we don't wrongly consider a block as bad.
1383 *
1384 * See the layout description for a detailed explanation on why this
1385 * is needed.
1386 */
1387 if (this->swap_block_mark) {
1388 u8 swap = tmp_buf[0];
1389
1390 tmp_buf[0] = tmp_buf[mtd->writesize];
1391 tmp_buf[mtd->writesize] = swap;
1392 }
1393
1394 /*
1395 * Copy the metadata section into the oob buffer (this section is
1396 * guaranteed to be aligned on a byte boundary).
1397 */
1398 if (oob_required)
1399 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1400
1401 oob_bit_off = nfc_geo->metadata_size * 8;
1402 src_bit_off = oob_bit_off;
1403
1404 /* Extract interleaved payload data and ECC bits */
1405 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1406 if (buf)
1407 gpmi_copy_bits(buf, step * eccsize * 8,
1408 tmp_buf, src_bit_off,
1409 eccsize * 8);
1410 src_bit_off += eccsize * 8;
1411
1412 /* Align last ECC block to align a byte boundary */
1413 if (step == nfc_geo->ecc_chunk_count - 1 &&
1414 (oob_bit_off + eccbits) % 8)
1415 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1416
1417 if (oob_required)
1418 gpmi_copy_bits(oob, oob_bit_off,
1419 tmp_buf, src_bit_off,
1420 eccbits);
1421
1422 src_bit_off += eccbits;
1423 oob_bit_off += eccbits;
1424 }
1425
1426 if (oob_required) {
1427 oob_byte_off = oob_bit_off / 8;
1428
1429 if (oob_byte_off < mtd->oobsize)
1430 memcpy(oob + oob_byte_off,
1431 tmp_buf + mtd->writesize + oob_byte_off,
1432 mtd->oobsize - oob_byte_off);
1433 }
1434
1435 return 0;
1436}
1437
1438/*
1439 * This function writes a NAND page without involving the ECC engine (no HW
1440 * ECC generation).
1441 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1442 * inline (interleaved with payload DATA), and do not align data chunk on
1443 * byte boundaries.
1444 * We thus need to take care moving the OOB area at the right place in the
1445 * final page, which is why we're using gpmi_copy_bits.
1446 *
1447 * See set_geometry_by_ecc_info inline comments to have a full description
1448 * of the layout used by the GPMI controller.
1449 */
1450static int gpmi_ecc_write_page_raw(struct mtd_info *mtd,
1451 struct nand_chip *chip,
1452 const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001453 int oob_required, int page)
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001454{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001455 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001456 struct bch_geometry *nfc_geo = &this->bch_geometry;
1457 int eccsize = nfc_geo->ecc_chunk_size;
1458 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1459 u8 *tmp_buf = this->raw_buffer;
1460 uint8_t *oob = chip->oob_poi;
1461 size_t dst_bit_off;
1462 size_t oob_bit_off;
1463 size_t oob_byte_off;
1464 int step;
1465
1466 /*
1467 * Initialize all bits to 1 in case we don't have a buffer for the
1468 * payload or oob data in order to leave unspecified bits of data
1469 * to their initial state.
1470 */
1471 if (!buf || !oob_required)
1472 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1473
1474 /*
1475 * First copy the metadata section (stored in oob buffer) at the
1476 * beginning of the page, as imposed by the GPMI layout.
1477 */
1478 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1479 oob_bit_off = nfc_geo->metadata_size * 8;
1480 dst_bit_off = oob_bit_off;
1481
1482 /* Interleave payload data and ECC bits */
1483 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1484 if (buf)
1485 gpmi_copy_bits(tmp_buf, dst_bit_off,
1486 buf, step * eccsize * 8, eccsize * 8);
1487 dst_bit_off += eccsize * 8;
1488
1489 /* Align last ECC block to align a byte boundary */
1490 if (step == nfc_geo->ecc_chunk_count - 1 &&
1491 (oob_bit_off + eccbits) % 8)
1492 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1493
1494 if (oob_required)
1495 gpmi_copy_bits(tmp_buf, dst_bit_off,
1496 oob, oob_bit_off, eccbits);
1497
1498 dst_bit_off += eccbits;
1499 oob_bit_off += eccbits;
1500 }
1501
1502 oob_byte_off = oob_bit_off / 8;
1503
1504 if (oob_required && oob_byte_off < mtd->oobsize)
1505 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1506 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1507
1508 /*
1509 * If required, swap the bad block marker and the first byte of the
1510 * metadata section, so that we don't modify the bad block marker.
1511 *
1512 * See the layout description for a detailed explanation on why this
1513 * is needed.
1514 */
1515 if (this->swap_block_mark) {
1516 u8 swap = tmp_buf[0];
1517
1518 tmp_buf[0] = tmp_buf[mtd->writesize];
1519 tmp_buf[mtd->writesize] = swap;
1520 }
1521
1522 chip->write_buf(mtd, tmp_buf, mtd->writesize + mtd->oobsize);
1523
1524 return 0;
1525}
1526
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001527static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1528 int page)
1529{
1530 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1531
1532 return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
1533}
1534
1535static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1536 int page)
1537{
1538 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
1539
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001540 return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001541}
1542
Huang Shijie10a2bca2011-09-08 10:47:09 +08001543static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1544{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001545 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001546 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -07001547 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001548 uint8_t *block_mark;
1549 int column, page, status, chipnr;
1550
Brian Norris5a0edb22013-07-30 17:52:58 -07001551 chipnr = (int)(ofs >> chip->chip_shift);
1552 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001553
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001554 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001555
Brian Norris5a0edb22013-07-30 17:52:58 -07001556 /* Write the block mark. */
1557 block_mark = this->data_buffer_dma;
1558 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001559
Brian Norris5a0edb22013-07-30 17:52:58 -07001560 /* Shift to get page */
1561 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001562
Brian Norris5a0edb22013-07-30 17:52:58 -07001563 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1564 chip->write_buf(mtd, block_mark, 1);
1565 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001566
Brian Norris5a0edb22013-07-30 17:52:58 -07001567 status = chip->waitfunc(mtd, chip);
1568 if (status & NAND_STATUS_FAIL)
1569 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001570
Brian Norris5a0edb22013-07-30 17:52:58 -07001571 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001572
1573 return ret;
1574}
1575
Wolfram Sanga78da282012-03-21 19:29:17 +01001576static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001577{
1578 struct boot_rom_geometry *geometry = &this->rom_geometry;
1579
1580 /*
1581 * Set the boot block stride size.
1582 *
1583 * In principle, we should be reading this from the OTP bits, since
1584 * that's where the ROM is going to get it. In fact, we don't have any
1585 * way to read the OTP bits, so we go with the default and hope for the
1586 * best.
1587 */
1588 geometry->stride_size_in_pages = 64;
1589
1590 /*
1591 * Set the search area stride exponent.
1592 *
1593 * In principle, we should be reading this from the OTP bits, since
1594 * that's where the ROM is going to get it. In fact, we don't have any
1595 * way to read the OTP bits, so we go with the default and hope for the
1596 * best.
1597 */
1598 geometry->search_area_stride_exponent = 2;
1599 return 0;
1600}
1601
1602static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001603static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001604{
1605 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1606 struct device *dev = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001607 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001608 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001609 unsigned int search_area_size_in_strides;
1610 unsigned int stride;
1611 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001612 uint8_t *buffer = chip->buffers->databuf;
1613 int saved_chip_number;
1614 int found_an_ncb_fingerprint = false;
1615
1616 /* Compute the number of strides in a search area. */
1617 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1618
1619 saved_chip_number = this->current_chip;
1620 chip->select_chip(mtd, 0);
1621
1622 /*
1623 * Loop through the first search area, looking for the NCB fingerprint.
1624 */
1625 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1626
1627 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001628 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001629 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001630
1631 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1632
1633 /*
1634 * Read the NCB fingerprint. The fingerprint is four bytes long
1635 * and starts in the 12th byte of the page.
1636 */
1637 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1638 chip->read_buf(mtd, buffer, strlen(fingerprint));
1639
1640 /* Look for the fingerprint. */
1641 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1642 found_an_ncb_fingerprint = true;
1643 break;
1644 }
1645
1646 }
1647
1648 chip->select_chip(mtd, saved_chip_number);
1649
1650 if (found_an_ncb_fingerprint)
1651 dev_dbg(dev, "\tFound a fingerprint\n");
1652 else
1653 dev_dbg(dev, "\tNo fingerprint found\n");
1654 return found_an_ncb_fingerprint;
1655}
1656
1657/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001658static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001659{
1660 struct device *dev = this->dev;
1661 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001662 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001663 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001664 unsigned int block_size_in_pages;
1665 unsigned int search_area_size_in_strides;
1666 unsigned int search_area_size_in_pages;
1667 unsigned int search_area_size_in_blocks;
1668 unsigned int block;
1669 unsigned int stride;
1670 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001671 uint8_t *buffer = chip->buffers->databuf;
1672 int saved_chip_number;
1673 int status;
1674
1675 /* Compute the search area geometry. */
1676 block_size_in_pages = mtd->erasesize / mtd->writesize;
1677 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1678 search_area_size_in_pages = search_area_size_in_strides *
1679 rom_geo->stride_size_in_pages;
1680 search_area_size_in_blocks =
1681 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1682 block_size_in_pages;
1683
1684 dev_dbg(dev, "Search Area Geometry :\n");
1685 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1686 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1687 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1688
1689 /* Select chip 0. */
1690 saved_chip_number = this->current_chip;
1691 chip->select_chip(mtd, 0);
1692
1693 /* Loop over blocks in the first search area, erasing them. */
1694 dev_dbg(dev, "Erasing the search area...\n");
1695
1696 for (block = 0; block < search_area_size_in_blocks; block++) {
1697 /* Compute the page address. */
1698 page = block * block_size_in_pages;
1699
1700 /* Erase this block. */
1701 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1702 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1703 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1704
1705 /* Wait for the erase to finish. */
1706 status = chip->waitfunc(mtd, chip);
1707 if (status & NAND_STATUS_FAIL)
1708 dev_err(dev, "[%s] Erase failed.\n", __func__);
1709 }
1710
1711 /* Write the NCB fingerprint into the page buffer. */
1712 memset(buffer, ~0, mtd->writesize);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001713 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1714
1715 /* Loop through the first search area, writing NCB fingerprints. */
1716 dev_dbg(dev, "Writing NCB fingerprints...\n");
1717 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001718 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001719 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001720
1721 /* Write the first page of the current stride. */
1722 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1723 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001724 chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001725 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1726
1727 /* Wait for the write to finish. */
1728 status = chip->waitfunc(mtd, chip);
1729 if (status & NAND_STATUS_FAIL)
1730 dev_err(dev, "[%s] Write failed.\n", __func__);
1731 }
1732
1733 /* Deselect chip 0. */
1734 chip->select_chip(mtd, saved_chip_number);
1735 return 0;
1736}
1737
Wolfram Sanga78da282012-03-21 19:29:17 +01001738static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001739{
1740 struct device *dev = this->dev;
1741 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001742 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001743 unsigned int block_count;
1744 unsigned int block;
1745 int chipnr;
1746 int page;
1747 loff_t byte;
1748 uint8_t block_mark;
1749 int ret = 0;
1750
1751 /*
1752 * If control arrives here, we can't use block mark swapping, which
1753 * means we're forced to use transcription. First, scan for the
1754 * transcription stamp. If we find it, then we don't have to do
1755 * anything -- the block marks are already transcribed.
1756 */
1757 if (mx23_check_transcription_stamp(this))
1758 return 0;
1759
1760 /*
1761 * If control arrives here, we couldn't find a transcription stamp, so
1762 * so we presume the block marks are in the conventional location.
1763 */
1764 dev_dbg(dev, "Transcribing bad block marks...\n");
1765
1766 /* Compute the number of blocks in the entire medium. */
1767 block_count = chip->chipsize >> chip->phys_erase_shift;
1768
1769 /*
1770 * Loop over all the blocks in the medium, transcribing block marks as
1771 * we go.
1772 */
1773 for (block = 0; block < block_count; block++) {
1774 /*
1775 * Compute the chip, page and byte addresses for this block's
1776 * conventional mark.
1777 */
1778 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1779 page = block << (chip->phys_erase_shift - chip->page_shift);
1780 byte = block << chip->phys_erase_shift;
1781
1782 /* Send the command to read the conventional block mark. */
1783 chip->select_chip(mtd, chipnr);
1784 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1785 block_mark = chip->read_byte(mtd);
1786 chip->select_chip(mtd, -1);
1787
1788 /*
1789 * Check if the block is marked bad. If so, we need to mark it
1790 * again, but this time the result will be a mark in the
1791 * location where we transcribe block marks.
1792 */
1793 if (block_mark != 0xff) {
1794 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1795 ret = chip->block_markbad(mtd, byte);
1796 if (ret)
Lothar Waßmannd8c03722014-06-12 15:20:42 +02001797 dev_err(dev,
1798 "Failed to mark block bad with ret %d\n",
1799 ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001800 }
1801 }
1802
1803 /* Write the stamp that indicates we've transcribed the block marks. */
1804 mx23_write_transcription_stamp(this);
1805 return 0;
1806}
1807
Wolfram Sanga78da282012-03-21 19:29:17 +01001808static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001809{
1810 nand_boot_set_geometry(this);
1811
1812 /* This is ROM arch-specific initilization before the BBT scanning. */
1813 if (GPMI_IS_MX23(this))
1814 return mx23_boot_init(this);
1815 return 0;
1816}
1817
Wolfram Sanga78da282012-03-21 19:29:17 +01001818static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001819{
1820 int ret;
1821
1822 /* Free the temporary DMA memory for reading ID. */
1823 gpmi_free_dma_buffer(this);
1824
1825 /* Set up the NFC geometry which is used by BCH. */
1826 ret = bch_set_geometry(this);
1827 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001828 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001829 return ret;
1830 }
1831
1832 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1833 return gpmi_alloc_dma_buffer(this);
1834}
1835
Huang Shijieccce4172013-11-14 14:25:47 +08001836static void gpmi_nand_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001837{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001838 nand_release(nand_to_mtd(&this->nand));
Huang Shijief720e7c2013-08-16 10:10:08 +08001839 gpmi_free_dma_buffer(this);
1840}
1841
1842static int gpmi_init_last(struct gpmi_nand_data *this)
1843{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001844 struct nand_chip *chip = &this->nand;
Huang Shijief720e7c2013-08-16 10:10:08 +08001845 struct nand_ecc_ctrl *ecc = &chip->ecc;
1846 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001847 int ret;
1848
Huang Shijied7364a272013-11-14 14:25:45 +08001849 /* Set up the medium geometry */
1850 ret = gpmi_set_geometry(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001851 if (ret)
1852 return ret;
1853
Huang Shijief720e7c2013-08-16 10:10:08 +08001854 /* Init the nand_ecc_ctrl{} */
1855 ecc->read_page = gpmi_ecc_read_page;
1856 ecc->write_page = gpmi_ecc_write_page;
1857 ecc->read_oob = gpmi_ecc_read_oob;
1858 ecc->write_oob = gpmi_ecc_write_oob;
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001859 ecc->read_page_raw = gpmi_ecc_read_page_raw;
1860 ecc->write_page_raw = gpmi_ecc_write_page_raw;
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001861 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1862 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
Huang Shijief720e7c2013-08-16 10:10:08 +08001863 ecc->mode = NAND_ECC_HW;
1864 ecc->size = bch_geo->ecc_chunk_size;
1865 ecc->strength = bch_geo->ecc_strength;
1866 ecc->layout = &gpmi_hw_ecclayout;
1867
Huang Shijie995fbbf2012-09-13 14:57:59 +08001868 /*
Huang Shijieb8e29312014-01-03 11:01:42 +08001869 * We only enable the subpage read when:
1870 * (1) the chip is imx6, and
1871 * (2) the size of the ECC parity is byte aligned.
1872 */
Huang Shijie91f54982014-03-27 10:43:22 +08001873 if (GPMI_IS_MX6(this) &&
Huang Shijieb8e29312014-01-03 11:01:42 +08001874 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1875 ecc->read_subpage = gpmi_ecc_read_subpage;
1876 chip->options |= NAND_SUBPAGE_READ;
1877 }
1878
1879 /*
Huang Shijie995fbbf2012-09-13 14:57:59 +08001880 * Can we enable the extra features? such as EDO or Sync mode.
1881 *
1882 * We do not check the return value now. That's means if we fail in
1883 * enable the extra features, we still can run in the normal way.
1884 */
1885 gpmi_extra_init(this);
1886
Huang Shijief720e7c2013-08-16 10:10:08 +08001887 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001888}
1889
Huang Shijieccce4172013-11-14 14:25:47 +08001890static int gpmi_nand_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001891{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001892 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001893 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001894 int ret;
1895
1896 /* init current chip */
1897 this->current_chip = -1;
1898
1899 /* init the MTD data structures */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001900 mtd->name = "gpmi-nand";
Frans Klaver4dc67b12015-06-10 22:38:49 +02001901 mtd->dev.parent = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001902
1903 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001904 nand_set_controller_data(chip, this);
Brian Norrisa61ae812015-10-30 20:33:25 -07001905 nand_set_flash_node(chip, this->pdev->dev.of_node);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001906 chip->select_chip = gpmi_select_chip;
1907 chip->cmd_ctrl = gpmi_cmd_ctrl;
1908 chip->dev_ready = gpmi_dev_ready;
1909 chip->read_byte = gpmi_read_byte;
1910 chip->read_buf = gpmi_read_buf;
1911 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001912 chip->badblock_pattern = &gpmi_bbt_descr;
1913 chip->block_markbad = gpmi_block_markbad;
1914 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001915
1916 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1917 this->swap_block_mark = !GPMI_IS_MX23(this);
1918
1919 if (of_get_nand_on_flash_bbt(this->dev->of_node)) {
Huang Shijiec50c6942012-07-03 16:24:32 +08001920 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001921
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001922 if (of_property_read_bool(this->dev->of_node,
1923 "fsl,no-blockmark-swap"))
1924 this->swap_block_mark = false;
1925 }
1926 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
1927 this->swap_block_mark ? "en" : "dis");
1928
Huang Shijief720e7c2013-08-16 10:10:08 +08001929 /*
1930 * Allocate a temporary DMA buffer for reading ID in the
1931 * nand_scan_ident().
1932 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001933 this->bch_geometry.payload_size = 1024;
1934 this->bch_geometry.auxiliary_size = 128;
1935 ret = gpmi_alloc_dma_buffer(this);
1936 if (ret)
1937 goto err_out;
1938
Huang Shijie91f54982014-03-27 10:43:22 +08001939 ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
Huang Shijief720e7c2013-08-16 10:10:08 +08001940 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001941 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001942
1943 ret = gpmi_init_last(this);
1944 if (ret)
1945 goto err_out;
1946
Huang Shijie885d71e2013-11-12 12:23:08 +08001947 chip->options |= NAND_SKIP_BBTSCAN;
Huang Shijief720e7c2013-08-16 10:10:08 +08001948 ret = nand_scan_tail(mtd);
1949 if (ret)
1950 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001951
Huang Shijie885d71e2013-11-12 12:23:08 +08001952 ret = nand_boot_init(this);
1953 if (ret)
1954 goto err_out;
Fabio Estevam899b8342015-02-09 19:22:33 -02001955 ret = chip->scan_bbt(mtd);
1956 if (ret)
1957 goto err_out;
Huang Shijie885d71e2013-11-12 12:23:08 +08001958
Brian Norrisa61ae812015-10-30 20:33:25 -07001959 ret = mtd_device_register(mtd, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001960 if (ret)
1961 goto err_out;
1962 return 0;
1963
1964err_out:
Huang Shijieccce4172013-11-14 14:25:47 +08001965 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001966 return ret;
1967}
1968
Huang Shijiee10db1f2012-05-04 21:42:05 -04001969static const struct of_device_id gpmi_nand_id_table[] = {
1970 {
1971 .compatible = "fsl,imx23-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02001972 .data = &gpmi_devdata_imx23,
Huang Shijiee10db1f2012-05-04 21:42:05 -04001973 }, {
1974 .compatible = "fsl,imx28-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02001975 .data = &gpmi_devdata_imx28,
Huang Shijie9013bb42012-05-04 21:42:06 -04001976 }, {
1977 .compatible = "fsl,imx6q-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02001978 .data = &gpmi_devdata_imx6q,
Huang Shijie91f54982014-03-27 10:43:22 +08001979 }, {
1980 .compatible = "fsl,imx6sx-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02001981 .data = &gpmi_devdata_imx6sx,
Huang Shijiee10db1f2012-05-04 21:42:05 -04001982 }, {}
1983};
1984MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1985
Bill Pemberton06f25512012-11-19 13:23:07 -05001986static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001987{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001988 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001989 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001990 int ret;
1991
Huang Shijie6189ccc2014-03-21 18:19:39 +08001992 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1993 if (!this)
1994 return -ENOMEM;
1995
Huang Shijiee10db1f2012-05-04 21:42:05 -04001996 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1997 if (of_id) {
Huang Shijie6189ccc2014-03-21 18:19:39 +08001998 this->devdata = of_id->data;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001999 } else {
Huang Shijieda40c162013-11-20 10:09:43 +08002000 dev_err(&pdev->dev, "Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02002001 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002002 }
2003
Huang Shijie10a2bca2011-09-08 10:47:09 +08002004 platform_set_drvdata(pdev, this);
2005 this->pdev = pdev;
2006 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08002007
2008 ret = acquire_resources(this);
2009 if (ret)
2010 goto exit_acquire_resources;
2011
2012 ret = init_hardware(this);
2013 if (ret)
2014 goto exit_nfc_init;
2015
Huang Shijieccce4172013-11-14 14:25:47 +08002016 ret = gpmi_nand_init(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002017 if (ret)
2018 goto exit_nfc_init;
2019
Fabio Estevam490e2802012-09-05 11:35:24 -03002020 dev_info(this->dev, "driver registered.\n");
2021
Huang Shijie10a2bca2011-09-08 10:47:09 +08002022 return 0;
2023
2024exit_nfc_init:
2025 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002026exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03002027
Huang Shijie10a2bca2011-09-08 10:47:09 +08002028 return ret;
2029}
2030
Bill Pemberton810b7e02012-11-19 13:26:04 -05002031static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08002032{
2033 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2034
Huang Shijieccce4172013-11-14 14:25:47 +08002035 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002036 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002037 return 0;
2038}
2039
Huang Shijie026918e2015-12-02 16:47:40 -06002040#ifdef CONFIG_PM_SLEEP
2041static int gpmi_pm_suspend(struct device *dev)
2042{
2043 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2044
2045 release_dma_channels(this);
2046 return 0;
2047}
2048
2049static int gpmi_pm_resume(struct device *dev)
2050{
2051 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2052 int ret;
2053
2054 ret = acquire_dma_channels(this);
2055 if (ret < 0)
2056 return ret;
2057
2058 /* re-init the GPMI registers */
2059 this->flags &= ~GPMI_TIMING_INIT_OK;
2060 ret = gpmi_init(this);
2061 if (ret) {
2062 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2063 return ret;
2064 }
2065
2066 /* re-init the BCH registers */
2067 ret = bch_set_geometry(this);
2068 if (ret) {
2069 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2070 return ret;
2071 }
2072
2073 /* re-init others */
2074 gpmi_extra_init(this);
2075
2076 return 0;
2077}
2078#endif /* CONFIG_PM_SLEEP */
2079
2080static const struct dev_pm_ops gpmi_pm_ops = {
2081 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2082};
2083
Huang Shijie10a2bca2011-09-08 10:47:09 +08002084static struct platform_driver gpmi_nand_driver = {
2085 .driver = {
2086 .name = "gpmi-nand",
Huang Shijie026918e2015-12-02 16:47:40 -06002087 .pm = &gpmi_pm_ops,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002088 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002089 },
2090 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05002091 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002092};
Fabio Estevam490e2802012-09-05 11:35:24 -03002093module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002094
2095MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2096MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2097MODULE_LICENSE("GPL");