blob: 798ed5961e8faf132a5d76fbdd91a4973d404875 [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 Shijie10a2bca2011-09-08 10:47:09 +080028#include "gpmi-nand.h"
Huang Shijieb8e29312014-01-03 11:01:42 +080029#include "bch-regs.h"
Huang Shijie10a2bca2011-09-08 10:47:09 +080030
Huang Shijie5de0b522012-10-13 13:03:29 -040031/* Resource names for the GPMI NAND driver. */
32#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
33#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
34#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
Huang Shijie5de0b522012-10-13 13:03:29 -040035
Huang Shijie10a2bca2011-09-08 10:47:09 +080036/* add our owner bbt descriptor */
37static uint8_t scan_ff_pattern[] = { 0xff };
38static struct nand_bbt_descr gpmi_bbt_descr = {
39 .options = 0,
40 .offs = 0,
41 .len = 1,
42 .pattern = scan_ff_pattern
43};
44
Huang Shijie7a2b89a2013-09-25 14:58:15 +080045/*
46 * We may change the layout if we can get the ECC info from the datasheet,
47 * else we will use all the (page + OOB).
48 */
Boris Brezillon3f158e42016-02-03 20:01:54 +010049static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
50 struct mtd_oob_region *oobregion)
51{
52 struct nand_chip *chip = mtd_to_nand(mtd);
53 struct gpmi_nand_data *this = nand_get_controller_data(chip);
54 struct bch_geometry *geo = &this->bch_geometry;
55
56 if (section)
57 return -ERANGE;
58
59 oobregion->offset = 0;
60 oobregion->length = geo->page_size - mtd->writesize;
61
62 return 0;
63}
64
65static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
66 struct mtd_oob_region *oobregion)
67{
68 struct nand_chip *chip = mtd_to_nand(mtd);
69 struct gpmi_nand_data *this = nand_get_controller_data(chip);
70 struct bch_geometry *geo = &this->bch_geometry;
71
72 if (section)
73 return -ERANGE;
74
75 /* The available oob size we have. */
76 if (geo->page_size < mtd->writesize + mtd->oobsize) {
77 oobregion->offset = geo->page_size - mtd->writesize;
78 oobregion->length = mtd->oobsize - oobregion->offset;
79 }
80
81 return 0;
82}
83
84static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
85 .ecc = gpmi_ooblayout_ecc,
86 .free = gpmi_ooblayout_free,
Huang Shijie10a2bca2011-09-08 10:47:09 +080087};
88
Huang Shijie6189ccc2014-03-21 18:19:39 +080089static const struct gpmi_devdata gpmi_devdata_imx23 = {
90 .type = IS_MX23,
91 .bch_max_ecc_strength = 20,
92 .max_chain_delay = 16,
93};
94
95static const struct gpmi_devdata gpmi_devdata_imx28 = {
96 .type = IS_MX28,
97 .bch_max_ecc_strength = 20,
98 .max_chain_delay = 16,
99};
100
101static const struct gpmi_devdata gpmi_devdata_imx6q = {
102 .type = IS_MX6Q,
103 .bch_max_ecc_strength = 40,
104 .max_chain_delay = 12,
105};
106
Huang Shijie91f54982014-03-27 10:43:22 +0800107static const struct gpmi_devdata gpmi_devdata_imx6sx = {
108 .type = IS_MX6SX,
109 .bch_max_ecc_strength = 62,
110 .max_chain_delay = 12,
111};
112
Huang Shijie10a2bca2011-09-08 10:47:09 +0800113static irqreturn_t bch_irq(int irq, void *cookie)
114{
115 struct gpmi_nand_data *this = cookie;
116
117 gpmi_clear_bch(this);
118 complete(&this->bch_done);
119 return IRQ_HANDLED;
120}
121
122/*
123 * Calculate the ECC strength by hand:
124 * E : The ECC strength.
125 * G : the length of Galois Field.
126 * N : The chunk count of per page.
127 * O : the oobsize of the NAND chip.
128 * M : the metasize of per page.
129 *
130 * The formula is :
131 * E * G * N
132 * ------------ <= (O - M)
133 * 8
134 *
135 * So, we get E by:
136 * (O - M) * 8
137 * E <= -------------
138 * G * N
139 */
140static inline int get_ecc_strength(struct gpmi_nand_data *this)
141{
142 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100143 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800144 int ecc_strength;
145
146 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
147 / (geo->gf_len * geo->ecc_chunk_count);
148
149 /* We need the minor even number. */
150 return round_down(ecc_strength, 2);
151}
152
Huang Shijie92d0e092013-01-29 09:23:38 +0800153static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
154{
155 struct bch_geometry *geo = &this->bch_geometry;
156
157 /* Do the sanity check. */
158 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
159 /* The mx23/mx28 only support the GF13. */
160 if (geo->gf_len == 14)
161 return false;
Huang Shijie92d0e092013-01-29 09:23:38 +0800162 }
Huang Shijie6189ccc2014-03-21 18:19:39 +0800163 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
Huang Shijie92d0e092013-01-29 09:23:38 +0800164}
165
Huang Shijie2febcdf2013-05-17 11:17:34 +0800166/*
167 * If we can get the ECC information from the nand chip, we do not
168 * need to calculate them ourselves.
169 *
170 * We may have available oob space in this case.
171 */
Han Xub8b0e462015-12-02 16:47:43 -0600172static int set_geometry_by_ecc_info(struct gpmi_nand_data *this)
Huang Shijie2febcdf2013-05-17 11:17:34 +0800173{
174 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100175 struct nand_chip *chip = &this->nand;
176 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie2febcdf2013-05-17 11:17:34 +0800177 unsigned int block_mark_bit_offset;
178
179 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
Han Xub8b0e462015-12-02 16:47:43 -0600180 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800181
182 switch (chip->ecc_step_ds) {
183 case SZ_512:
184 geo->gf_len = 13;
185 break;
186 case SZ_1K:
187 geo->gf_len = 14;
188 break;
189 default:
190 dev_err(this->dev,
191 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
192 chip->ecc_strength_ds, chip->ecc_step_ds);
Han Xub8b0e462015-12-02 16:47:43 -0600193 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800194 }
195 geo->ecc_chunk_size = chip->ecc_step_ds;
196 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
197 if (!gpmi_check_ecc(this))
Han Xub8b0e462015-12-02 16:47:43 -0600198 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800199
200 /* Keep the C >= O */
201 if (geo->ecc_chunk_size < mtd->oobsize) {
202 dev_err(this->dev,
203 "unsupported nand chip. ecc size: %d, oob size : %d\n",
204 chip->ecc_step_ds, mtd->oobsize);
Han Xub8b0e462015-12-02 16:47:43 -0600205 return -EINVAL;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800206 }
207
208 /* The default value, see comment in the legacy_set_geometry(). */
209 geo->metadata_size = 10;
210
211 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
212
213 /*
214 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
215 *
216 * | P |
217 * |<----------------------------------------------------->|
218 * | |
219 * | (Block Mark) |
220 * | P' | | | |
221 * |<-------------------------------------------->| D | | O' |
222 * | |<---->| |<--->|
223 * V V V V V
224 * +---+----------+-+----------+-+----------+-+----------+-+-----+
225 * | M | data |E| data |E| data |E| data |E| |
226 * +---+----------+-+----------+-+----------+-+----------+-+-----+
227 * ^ ^
228 * | O |
229 * |<------------>|
230 * | |
231 *
232 * P : the page size for BCH module.
233 * E : The ECC strength.
234 * G : the length of Galois Field.
235 * N : The chunk count of per page.
236 * M : the metasize of per page.
237 * C : the ecc chunk size, aka the "data" above.
238 * P': the nand chip's page size.
239 * O : the nand chip's oob size.
240 * O': the free oob.
241 *
242 * The formula for P is :
243 *
244 * E * G * N
245 * P = ------------ + P' + M
246 * 8
247 *
248 * The position of block mark moves forward in the ECC-based view
249 * of page, and the delta is:
250 *
251 * E * G * (N - 1)
252 * D = (---------------- + M)
253 * 8
254 *
255 * Please see the comment in legacy_set_geometry().
256 * With the condition C >= O , we still can get same result.
257 * So the bit position of the physical block mark within the ECC-based
258 * view of the page is :
259 * (P' - D) * 8
260 */
261 geo->page_size = mtd->writesize + geo->metadata_size +
262 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
263
Huang Shijie2febcdf2013-05-17 11:17:34 +0800264 geo->payload_size = mtd->writesize;
265
266 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
267 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
268 + ALIGN(geo->ecc_chunk_count, 4);
269
270 if (!this->swap_block_mark)
Han Xub8b0e462015-12-02 16:47:43 -0600271 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800272
273 /* For bit swap. */
274 block_mark_bit_offset = mtd->writesize * 8 -
275 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
276 + geo->metadata_size * 8);
277
278 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
279 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
Han Xub8b0e462015-12-02 16:47:43 -0600280 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800281}
282
283static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800284{
285 struct bch_geometry *geo = &this->bch_geometry;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100286 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800287 unsigned int metadata_size;
288 unsigned int status_size;
289 unsigned int block_mark_bit_offset;
290
291 /*
292 * The size of the metadata can be changed, though we set it to 10
293 * bytes now. But it can't be too large, because we have to save
294 * enough space for BCH.
295 */
296 geo->metadata_size = 10;
297
298 /* The default for the length of Galois Field. */
299 geo->gf_len = 13;
300
Huang Shijie9ff16f02013-01-25 14:04:07 +0800301 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800302 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800303 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800304 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800305 geo->gf_len = 14;
306 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800307
308 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
309
310 /* We use the same ECC strength for all chunks. */
311 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800312 if (!gpmi_check_ecc(this)) {
313 dev_err(this->dev,
Han Xub8b0e462015-12-02 16:47:43 -0600314 "ecc strength: %d cannot be supported by the controller (%d)\n"
315 "try to use minimum ecc strength that NAND chip required\n",
Lothar Waßmannd8c03722014-06-12 15:20:42 +0200316 geo->ecc_strength,
Huang Shijie6189ccc2014-03-21 18:19:39 +0800317 this->devdata->bch_max_ecc_strength);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800318 return -EINVAL;
319 }
320
321 geo->page_size = mtd->writesize + mtd->oobsize;
322 geo->payload_size = mtd->writesize;
323
324 /*
325 * The auxiliary buffer contains the metadata and the ECC status. The
326 * metadata is padded to the nearest 32-bit boundary. The ECC status
327 * contains one byte for every ECC chunk, and is also padded to the
328 * nearest 32-bit boundary.
329 */
330 metadata_size = ALIGN(geo->metadata_size, 4);
331 status_size = ALIGN(geo->ecc_chunk_count, 4);
332
333 geo->auxiliary_size = metadata_size + status_size;
334 geo->auxiliary_status_offset = metadata_size;
335
336 if (!this->swap_block_mark)
337 return 0;
338
339 /*
340 * We need to compute the byte and bit offsets of
341 * the physical block mark within the ECC-based view of the page.
342 *
343 * NAND chip with 2K page shows below:
344 * (Block Mark)
345 * | |
346 * | D |
347 * |<---->|
348 * V V
349 * +---+----------+-+----------+-+----------+-+----------+-+
350 * | M | data |E| data |E| data |E| data |E|
351 * +---+----------+-+----------+-+----------+-+----------+-+
352 *
353 * The position of block mark moves forward in the ECC-based view
354 * of page, and the delta is:
355 *
356 * E * G * (N - 1)
357 * D = (---------------- + M)
358 * 8
359 *
360 * With the formula to compute the ECC strength, and the condition
361 * : C >= O (C is the ecc chunk size)
362 *
363 * It's easy to deduce to the following result:
364 *
365 * E * G (O - M) C - M C - M
366 * ----------- <= ------- <= -------- < ---------
367 * 8 N N (N - 1)
368 *
369 * So, we get:
370 *
371 * E * G * (N - 1)
372 * D = (---------------- + M) < C
373 * 8
374 *
375 * The above inequality means the position of block mark
376 * within the ECC-based view of the page is still in the data chunk,
377 * and it's NOT in the ECC bits of the chunk.
378 *
379 * Use the following to compute the bit position of the
380 * physical block mark within the ECC-based view of the page:
381 * (page_size - D) * 8
382 *
383 * --Huang Shijie
384 */
385 block_mark_bit_offset = mtd->writesize * 8 -
386 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
387 + geo->metadata_size * 8);
388
389 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
390 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
391 return 0;
392}
393
Huang Shijie2febcdf2013-05-17 11:17:34 +0800394int common_nfc_set_geometry(struct gpmi_nand_data *this)
395{
Han Xub8b0e462015-12-02 16:47:43 -0600396 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
397 || legacy_set_geometry(this))
398 return set_geometry_by_ecc_info(this);
399
400 return 0;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800401}
402
Huang Shijie10a2bca2011-09-08 10:47:09 +0800403struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
404{
Huang Shijiea7c12d02013-08-27 17:29:05 +0800405 /* We use the DMA channel 0 to access all the nand chips. */
406 return this->dma_chans[0];
Huang Shijie10a2bca2011-09-08 10:47:09 +0800407}
408
409/* Can we use the upper's buffer directly for DMA? */
410void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
411{
412 struct scatterlist *sgl = &this->data_sgl;
413 int ret;
414
Huang Shijie10a2bca2011-09-08 10:47:09 +0800415 /* first try to map the upper buffer directly */
Huang Shijie0ff76a92013-12-18 23:41:00 +0800416 if (virt_addr_valid(this->upper_buf) &&
417 !object_is_on_stack(this->upper_buf)) {
418 sg_init_one(sgl, this->upper_buf, this->upper_len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800419 ret = dma_map_sg(this->dev, sgl, 1, dr);
420 if (ret == 0)
Huang Shijie0ff76a92013-12-18 23:41:00 +0800421 goto map_fail;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800422
Huang Shijie0ff76a92013-12-18 23:41:00 +0800423 this->direct_dma_map_ok = true;
424 return;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800425 }
Huang Shijie0ff76a92013-12-18 23:41:00 +0800426
427map_fail:
428 /* We have to use our own DMA buffer. */
429 sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
430
431 if (dr == DMA_TO_DEVICE)
432 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
433
434 dma_map_sg(this->dev, sgl, 1, dr);
435
436 this->direct_dma_map_ok = false;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800437}
438
439/* This will be called after the DMA operation is finished. */
440static void dma_irq_callback(void *param)
441{
442 struct gpmi_nand_data *this = param;
443 struct completion *dma_c = &this->dma_done;
444
Huang Shijie10a2bca2011-09-08 10:47:09 +0800445 switch (this->dma_type) {
446 case DMA_FOR_COMMAND:
447 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
448 break;
449
450 case DMA_FOR_READ_DATA:
451 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
452 if (this->direct_dma_map_ok == false)
453 memcpy(this->upper_buf, this->data_buffer_dma,
454 this->upper_len);
455 break;
456
457 case DMA_FOR_WRITE_DATA:
458 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
459 break;
460
461 case DMA_FOR_READ_ECC_PAGE:
462 case DMA_FOR_WRITE_ECC_PAGE:
463 /* We have to wait the BCH interrupt to finish. */
464 break;
465
466 default:
Huang Shijieda40c162013-11-20 10:09:43 +0800467 dev_err(this->dev, "in wrong DMA operation.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800468 }
Huang Shijie7b3d2fb2013-11-11 12:13:45 +0800469
470 complete(dma_c);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800471}
472
473int start_dma_without_bch_irq(struct gpmi_nand_data *this,
474 struct dma_async_tx_descriptor *desc)
475{
476 struct completion *dma_c = &this->dma_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500477 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800478
479 init_completion(dma_c);
480
481 desc->callback = dma_irq_callback;
482 desc->callback_param = this;
483 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800484 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800485
486 /* Wait for the interrupt from the DMA block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500487 timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
488 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800489 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
490 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800491 gpmi_dump_info(this);
492 return -ETIMEDOUT;
493 }
494 return 0;
495}
496
497/*
498 * This function is used in BCH reading or BCH writing pages.
499 * It will wait for the BCH interrupt as long as ONE second.
500 * Actually, we must wait for two interrupts :
501 * [1] firstly the DMA interrupt and
502 * [2] secondly the BCH interrupt.
503 */
504int start_dma_with_bch_irq(struct gpmi_nand_data *this,
505 struct dma_async_tx_descriptor *desc)
506{
507 struct completion *bch_c = &this->bch_done;
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500508 unsigned long timeout;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800509
510 /* Prepare to receive an interrupt from the BCH block. */
511 init_completion(bch_c);
512
513 /* start the DMA */
514 start_dma_without_bch_irq(this, desc);
515
516 /* Wait for the interrupt from the BCH block. */
Nicholas Mc Guire706d5b22015-02-08 11:37:33 -0500517 timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
518 if (!timeout) {
Huang Shijieda40c162013-11-20 10:09:43 +0800519 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
520 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800521 gpmi_dump_info(this);
522 return -ETIMEDOUT;
523 }
524 return 0;
525}
526
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800527static int acquire_register_block(struct gpmi_nand_data *this,
528 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800529{
530 struct platform_device *pdev = this->pdev;
531 struct resources *res = &this->resources;
532 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800533 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800534
535 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
Huang Shijie87a9d692013-11-14 14:25:48 +0800536 p = devm_ioremap_resource(&pdev->dev, r);
537 if (IS_ERR(p))
538 return PTR_ERR(p);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800539
540 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
541 res->gpmi_regs = p;
542 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
543 res->bch_regs = p;
544 else
Huang Shijieda40c162013-11-20 10:09:43 +0800545 dev_err(this->dev, "unknown resource name : %s\n", res_name);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800546
547 return 0;
548}
549
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800550static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800551{
552 struct platform_device *pdev = this->pdev;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800553 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
554 struct resource *r;
555 int err;
556
557 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
558 if (!r) {
Huang Shijieda40c162013-11-20 10:09:43 +0800559 dev_err(this->dev, "Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200560 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800561 }
562
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800563 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
564 if (err)
565 dev_err(this->dev, "error requesting BCH IRQ\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800566
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800567 return err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800568}
569
Huang Shijie10a2bca2011-09-08 10:47:09 +0800570static void release_dma_channels(struct gpmi_nand_data *this)
571{
572 unsigned int i;
573 for (i = 0; i < DMA_CHANS; i++)
574 if (this->dma_chans[i]) {
575 dma_release_channel(this->dma_chans[i]);
576 this->dma_chans[i] = NULL;
577 }
578}
579
Bill Pemberton06f25512012-11-19 13:23:07 -0500580static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800581{
582 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400583 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400584
585 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800586 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400587 if (!dma_chan) {
Huang Shijieda40c162013-11-20 10:09:43 +0800588 dev_err(this->dev, "Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400589 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800590 }
591
Huang Shijiee10db1f2012-05-04 21:42:05 -0400592 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800593 return 0;
594
595acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800596 release_dma_channels(this);
597 return -EINVAL;
598}
599
Huang Shijieff506172012-07-02 21:39:32 -0400600static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
601 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
602};
603
Bill Pemberton06f25512012-11-19 13:23:07 -0500604static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400605{
606 struct resources *r = &this->resources;
607 char **extra_clks = NULL;
608 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200609 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400610
611 /* The main clock is stored in the first. */
Fabio Estevam554cbc52013-11-07 22:32:38 -0200612 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200613 if (IS_ERR(r->clock[0])) {
614 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400615 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200616 }
Huang Shijieff506172012-07-02 21:39:32 -0400617
618 /* Get extra clocks */
Huang Shijie91f54982014-03-27 10:43:22 +0800619 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400620 extra_clks = extra_clks_for_mx6q;
621 if (!extra_clks)
622 return 0;
623
624 for (i = 1; i < GPMI_CLK_MAX; i++) {
625 if (extra_clks[i - 1] == NULL)
626 break;
627
Fabio Estevam554cbc52013-11-07 22:32:38 -0200628 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200629 if (IS_ERR(clk)) {
630 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400631 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200632 }
Huang Shijieff506172012-07-02 21:39:32 -0400633
634 r->clock[i] = clk;
635 }
636
Huang Shijie91f54982014-03-27 10:43:22 +0800637 if (GPMI_IS_MX6(this))
Huang Shijieff506172012-07-02 21:39:32 -0400638 /*
Huang Shijie91f54982014-03-27 10:43:22 +0800639 * Set the default value for the gpmi clock.
Huang Shijieff506172012-07-02 21:39:32 -0400640 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800641 * If you want to use the ONFI nand which is in the
642 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400643 */
644 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800645
Huang Shijieff506172012-07-02 21:39:32 -0400646 return 0;
647
648err_clock:
649 dev_dbg(this->dev, "failed in finding the clocks.\n");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200650 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400651}
652
Bill Pemberton06f25512012-11-19 13:23:07 -0500653static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800654{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800655 int ret;
656
657 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
658 if (ret)
659 goto exit_regs;
660
661 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
662 if (ret)
663 goto exit_regs;
664
665 ret = acquire_bch_irq(this, bch_irq);
666 if (ret)
667 goto exit_regs;
668
669 ret = acquire_dma_channels(this);
670 if (ret)
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800671 goto exit_regs;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800672
Huang Shijieff506172012-07-02 21:39:32 -0400673 ret = gpmi_get_clks(this);
674 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800675 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800676 return 0;
677
678exit_clock:
679 release_dma_channels(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800680exit_regs:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800681 return ret;
682}
683
684static void release_resources(struct gpmi_nand_data *this)
685{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800686 release_dma_channels(this);
687}
688
Bill Pemberton06f25512012-11-19 13:23:07 -0500689static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800690{
691 int ret;
692
693 /*
694 * This structure contains the "safe" GPMI timing that should succeed
695 * with any NAND Flash device
696 * (although, with less-than-optimal performance).
697 */
698 struct nand_timing safe_timing = {
699 .data_setup_in_ns = 80,
700 .data_hold_in_ns = 60,
701 .address_setup_in_ns = 25,
702 .gpmi_sample_delay_in_ns = 6,
703 .tREA_in_ns = -1,
704 .tRLOH_in_ns = -1,
705 .tRHOH_in_ns = -1,
706 };
707
708 /* Initialize the hardwares. */
709 ret = gpmi_init(this);
710 if (ret)
711 return ret;
712
713 this->timing = safe_timing;
714 return 0;
715}
716
717static int read_page_prepare(struct gpmi_nand_data *this,
718 void *destination, unsigned length,
719 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
720 void **use_virt, dma_addr_t *use_phys)
721{
722 struct device *dev = this->dev;
723
724 if (virt_addr_valid(destination)) {
725 dma_addr_t dest_phys;
726
727 dest_phys = dma_map_single(dev, destination,
728 length, DMA_FROM_DEVICE);
729 if (dma_mapping_error(dev, dest_phys)) {
730 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800731 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800732 return -ENOMEM;
733 }
734 goto map_failed;
735 }
736 *use_virt = destination;
737 *use_phys = dest_phys;
738 this->direct_dma_map_ok = true;
739 return 0;
740 }
741
742map_failed:
743 *use_virt = alt_virt;
744 *use_phys = alt_phys;
745 this->direct_dma_map_ok = false;
746 return 0;
747}
748
749static inline void read_page_end(struct gpmi_nand_data *this,
750 void *destination, unsigned length,
751 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
752 void *used_virt, dma_addr_t used_phys)
753{
754 if (this->direct_dma_map_ok)
755 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
756}
757
758static inline void read_page_swap_end(struct gpmi_nand_data *this,
759 void *destination, unsigned length,
760 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
761 void *used_virt, dma_addr_t used_phys)
762{
763 if (!this->direct_dma_map_ok)
764 memcpy(destination, alt_virt, length);
765}
766
767static int send_page_prepare(struct gpmi_nand_data *this,
768 const void *source, unsigned length,
769 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
770 const void **use_virt, dma_addr_t *use_phys)
771{
772 struct device *dev = this->dev;
773
774 if (virt_addr_valid(source)) {
775 dma_addr_t source_phys;
776
777 source_phys = dma_map_single(dev, (void *)source, length,
778 DMA_TO_DEVICE);
779 if (dma_mapping_error(dev, source_phys)) {
780 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800781 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800782 return -ENOMEM;
783 }
784 goto map_failed;
785 }
786 *use_virt = source;
787 *use_phys = source_phys;
788 return 0;
789 }
790map_failed:
791 /*
792 * Copy the content of the source buffer into the alternate
793 * buffer and set up the return values accordingly.
794 */
795 memcpy(alt_virt, source, length);
796
797 *use_virt = alt_virt;
798 *use_phys = alt_phys;
799 return 0;
800}
801
802static void send_page_end(struct gpmi_nand_data *this,
803 const void *source, unsigned length,
804 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
805 const void *used_virt, dma_addr_t used_phys)
806{
807 struct device *dev = this->dev;
808 if (used_virt == source)
809 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
810}
811
812static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
813{
814 struct device *dev = this->dev;
815
816 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
817 dma_free_coherent(dev, this->page_buffer_size,
818 this->page_buffer_virt,
819 this->page_buffer_phys);
820 kfree(this->cmd_buffer);
821 kfree(this->data_buffer_dma);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100822 kfree(this->raw_buffer);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800823
824 this->cmd_buffer = NULL;
825 this->data_buffer_dma = NULL;
Han Xu2cd395d2016-04-04 15:41:29 -0500826 this->raw_buffer = NULL;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800827 this->page_buffer_virt = NULL;
828 this->page_buffer_size = 0;
829}
830
831/* Allocate the DMA buffers */
832static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
833{
834 struct bch_geometry *geo = &this->bch_geometry;
835 struct device *dev = this->dev;
Boris BREZILLON2a690b22015-12-10 09:00:07 +0100836 struct mtd_info *mtd = nand_to_mtd(&this->nand);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800837
838 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800839 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800840 if (this->cmd_buffer == NULL)
841 goto error_alloc;
842
Huang Shijie06f216c2013-12-18 23:40:59 +0800843 /*
844 * [2] Allocate a read/write data buffer.
845 * The gpmi_alloc_dma_buffer can be called twice.
846 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
847 * is called before the nand_scan_ident; and we allocate a buffer
848 * of the real NAND page size when the gpmi_alloc_dma_buffer is
849 * called after the nand_scan_ident.
850 */
851 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
852 GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800853 if (this->data_buffer_dma == NULL)
854 goto error_alloc;
855
856 /*
857 * [3] Allocate the page buffer.
858 *
859 * Both the payload buffer and the auxiliary buffer must appear on
860 * 32-bit boundaries. We presume the size of the payload buffer is a
861 * power of two and is much larger than four, which guarantees the
862 * auxiliary buffer will appear on a 32-bit boundary.
863 */
864 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
865 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
866 &this->page_buffer_phys, GFP_DMA);
867 if (!this->page_buffer_virt)
868 goto error_alloc;
869
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +0100870 this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
871 if (!this->raw_buffer)
872 goto error_alloc;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800873
874 /* Slice up the page buffer. */
875 this->payload_virt = this->page_buffer_virt;
876 this->payload_phys = this->page_buffer_phys;
877 this->auxiliary_virt = this->payload_virt + geo->payload_size;
878 this->auxiliary_phys = this->payload_phys + geo->payload_size;
879 return 0;
880
881error_alloc:
882 gpmi_free_dma_buffer(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800883 return -ENOMEM;
884}
885
886static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
887{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100888 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100889 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800890 int ret;
891
892 /*
893 * Every operation begins with a command byte and a series of zero or
894 * more address bytes. These are distinguished by either the Address
895 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
896 * asserted. When MTD is ready to execute the command, it will deassert
897 * both latch enables.
898 *
899 * Rather than run a separate DMA operation for every single byte, we
900 * queue them up and run a single DMA operation for the entire series
901 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
902 */
903 if ((ctrl & (NAND_ALE | NAND_CLE))) {
904 if (data != NAND_CMD_NONE)
905 this->cmd_buffer[this->command_length++] = data;
906 return;
907 }
908
909 if (!this->command_length)
910 return;
911
912 ret = gpmi_send_command(this);
913 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +0800914 dev_err(this->dev, "Chip: %u, Error %d\n",
915 this->current_chip, ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800916
917 this->command_length = 0;
918}
919
920static int gpmi_dev_ready(struct mtd_info *mtd)
921{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100922 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100923 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800924
925 return gpmi_is_ready(this, this->current_chip);
926}
927
928static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
929{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100930 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100931 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800932
933 if ((this->current_chip < 0) && (chipnr >= 0))
934 gpmi_begin(this);
935 else if ((this->current_chip >= 0) && (chipnr < 0))
936 gpmi_end(this);
937
938 this->current_chip = chipnr;
939}
940
941static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
942{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100943 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100944 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800945
Huang Shijiec2325962013-11-20 10:09:44 +0800946 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800947 this->upper_buf = buf;
948 this->upper_len = len;
949
950 gpmi_read_data(this);
951}
952
953static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
954{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100955 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100956 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800957
Huang Shijiec2325962013-11-20 10:09:44 +0800958 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800959 this->upper_buf = (uint8_t *)buf;
960 this->upper_len = len;
961
962 gpmi_send_data(this);
963}
964
965static uint8_t gpmi_read_byte(struct mtd_info *mtd)
966{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100967 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100968 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800969 uint8_t *buf = this->data_buffer_dma;
970
971 gpmi_read_buf(mtd, buf, 1);
972 return buf[0];
973}
974
975/*
976 * Handles block mark swapping.
977 * It can be called in swapping the block mark, or swapping it back,
978 * because the the operations are the same.
979 */
980static void block_mark_swapping(struct gpmi_nand_data *this,
981 void *payload, void *auxiliary)
982{
983 struct bch_geometry *nfc_geo = &this->bch_geometry;
984 unsigned char *p;
985 unsigned char *a;
986 unsigned int bit;
987 unsigned char mask;
988 unsigned char from_data;
989 unsigned char from_oob;
990
991 if (!this->swap_block_mark)
992 return;
993
994 /*
995 * If control arrives here, we're swapping. Make some convenience
996 * variables.
997 */
998 bit = nfc_geo->block_mark_bit_offset;
999 p = payload + nfc_geo->block_mark_byte_offset;
1000 a = auxiliary;
1001
1002 /*
1003 * Get the byte from the data area that overlays the block mark. Since
1004 * the ECC engine applies its own view to the bits in the page, the
1005 * physical block mark won't (in general) appear on a byte boundary in
1006 * the data.
1007 */
1008 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1009
1010 /* Get the byte from the OOB. */
1011 from_oob = a[0];
1012
1013 /* Swap them. */
1014 a[0] = from_data;
1015
1016 mask = (0x1 << bit) - 1;
1017 p[0] = (p[0] & mask) | (from_oob << bit);
1018
1019 mask = ~0 << bit;
1020 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1021}
1022
1023static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001024 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001025{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001026 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001027 struct bch_geometry *nfc_geo = &this->bch_geometry;
1028 void *payload_virt;
1029 dma_addr_t payload_phys;
1030 void *auxiliary_virt;
1031 dma_addr_t auxiliary_phys;
1032 unsigned int i;
1033 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -06001034 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001035 int ret;
1036
Huang Shijiec2325962013-11-20 10:09:44 +08001037 dev_dbg(this->dev, "page number is : %d\n", page);
Huang Shijie4a57d672014-01-03 11:01:41 +08001038 ret = read_page_prepare(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001039 this->payload_virt, this->payload_phys,
1040 nfc_geo->payload_size,
1041 &payload_virt, &payload_phys);
1042 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001043 dev_err(this->dev, "Inadequate DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001044 ret = -ENOMEM;
1045 return ret;
1046 }
1047 auxiliary_virt = this->auxiliary_virt;
1048 auxiliary_phys = this->auxiliary_phys;
1049
1050 /* go! */
1051 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
Huang Shijie4a57d672014-01-03 11:01:41 +08001052 read_page_end(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001053 this->payload_virt, this->payload_phys,
1054 nfc_geo->payload_size,
1055 payload_virt, payload_phys);
1056 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001057 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001058 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001059 }
1060
1061 /* handle the block mark swapping */
1062 block_mark_swapping(this, payload_virt, auxiliary_virt);
1063
1064 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001065 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001066
1067 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1068 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1069 continue;
1070
1071 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001072 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001073 continue;
1074 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001075 mtd->ecc_stats.corrected += *status;
1076 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001077 }
1078
Brian Norris7725cc82012-05-02 10:15:02 -07001079 if (oob_required) {
1080 /*
1081 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1082 * for details about our policy for delivering the OOB.
1083 *
1084 * We fill the caller's buffer with set bits, and then copy the
1085 * block mark to th caller's buffer. Note that, if block mark
1086 * swapping was necessary, it has already been done, so we can
1087 * rely on the first byte of the auxiliary buffer to contain
1088 * the block mark.
1089 */
1090 memset(chip->oob_poi, ~0, mtd->oobsize);
1091 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001092 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001093
Huang Shijie4a57d672014-01-03 11:01:41 +08001094 read_page_swap_end(this, buf, nfc_geo->payload_size,
Sascha Hauer6023813a2012-06-26 17:26:16 +02001095 this->payload_virt, this->payload_phys,
1096 nfc_geo->payload_size,
1097 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001098
1099 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001100}
1101
Huang Shijieb8e29312014-01-03 11:01:42 +08001102/* Fake a virtual small page for the subpage read */
1103static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1104 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1105{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001106 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijieb8e29312014-01-03 11:01:42 +08001107 void __iomem *bch_regs = this->resources.bch_regs;
1108 struct bch_geometry old_geo = this->bch_geometry;
1109 struct bch_geometry *geo = &this->bch_geometry;
1110 int size = chip->ecc.size; /* ECC chunk size */
1111 int meta, n, page_size;
1112 u32 r1_old, r2_old, r1_new, r2_new;
1113 unsigned int max_bitflips;
1114 int first, last, marker_pos;
1115 int ecc_parity_size;
1116 int col = 0;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001117 int old_swap_block_mark = this->swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001118
1119 /* The size of ECC parity */
1120 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1121
1122 /* Align it with the chunk size */
1123 first = offs / size;
1124 last = (offs + len - 1) / size;
1125
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001126 if (this->swap_block_mark) {
1127 /*
1128 * Find the chunk which contains the Block Marker.
1129 * If this chunk is in the range of [first, last],
1130 * we have to read out the whole page.
1131 * Why? since we had swapped the data at the position of Block
1132 * Marker to the metadata which is bound with the chunk 0.
1133 */
1134 marker_pos = geo->block_mark_byte_offset / size;
1135 if (last >= marker_pos && first <= marker_pos) {
1136 dev_dbg(this->dev,
1137 "page:%d, first:%d, last:%d, marker at:%d\n",
Huang Shijieb8e29312014-01-03 11:01:42 +08001138 page, first, last, marker_pos);
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001139 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1140 }
Huang Shijieb8e29312014-01-03 11:01:42 +08001141 }
1142
1143 meta = geo->metadata_size;
1144 if (first) {
1145 col = meta + (size + ecc_parity_size) * first;
1146 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1147
1148 meta = 0;
1149 buf = buf + first * size;
1150 }
1151
1152 /* Save the old environment */
1153 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1154 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1155
1156 /* change the BCH registers and bch_geometry{} */
1157 n = last - first + 1;
1158 page_size = meta + (size + ecc_parity_size) * n;
1159
1160 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1161 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1162 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1163 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1164 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1165
1166 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1167 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1168 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1169
1170 geo->ecc_chunk_count = n;
1171 geo->payload_size = n * size;
1172 geo->page_size = page_size;
1173 geo->auxiliary_status_offset = ALIGN(meta, 4);
1174
1175 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1176 page, offs, len, col, first, n, page_size);
1177
1178 /* Read the subpage now */
1179 this->swap_block_mark = false;
1180 max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1181
1182 /* Restore */
1183 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1184 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1185 this->bch_geometry = old_geo;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001186 this->swap_block_mark = old_swap_block_mark;
Huang Shijieb8e29312014-01-03 11:01:42 +08001187
1188 return max_bitflips;
1189}
1190
Josh Wufdbad98d2012-06-25 18:07:45 +08001191static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001192 const uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001193{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001194 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001195 struct bch_geometry *nfc_geo = &this->bch_geometry;
1196 const void *payload_virt;
1197 dma_addr_t payload_phys;
1198 const void *auxiliary_virt;
1199 dma_addr_t auxiliary_phys;
1200 int ret;
1201
Huang Shijiec2325962013-11-20 10:09:44 +08001202 dev_dbg(this->dev, "ecc write page.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001203 if (this->swap_block_mark) {
1204 /*
1205 * If control arrives here, we're doing block mark swapping.
1206 * Since we can't modify the caller's buffers, we must copy them
1207 * into our own.
1208 */
1209 memcpy(this->payload_virt, buf, mtd->writesize);
1210 payload_virt = this->payload_virt;
1211 payload_phys = this->payload_phys;
1212
1213 memcpy(this->auxiliary_virt, chip->oob_poi,
1214 nfc_geo->auxiliary_size);
1215 auxiliary_virt = this->auxiliary_virt;
1216 auxiliary_phys = this->auxiliary_phys;
1217
1218 /* Handle block mark swapping. */
1219 block_mark_swapping(this,
Lothar Waßmann6a760962014-06-12 15:20:41 +02001220 (void *)payload_virt, (void *)auxiliary_virt);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001221 } else {
1222 /*
1223 * If control arrives here, we're not doing block mark swapping,
1224 * so we can to try and use the caller's buffers.
1225 */
1226 ret = send_page_prepare(this,
1227 buf, mtd->writesize,
1228 this->payload_virt, this->payload_phys,
1229 nfc_geo->payload_size,
1230 &payload_virt, &payload_phys);
1231 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001232 dev_err(this->dev, "Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001233 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001234 }
1235
1236 ret = send_page_prepare(this,
1237 chip->oob_poi, mtd->oobsize,
1238 this->auxiliary_virt, this->auxiliary_phys,
1239 nfc_geo->auxiliary_size,
1240 &auxiliary_virt, &auxiliary_phys);
1241 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001242 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001243 goto exit_auxiliary;
1244 }
1245 }
1246
1247 /* Ask the NFC. */
1248 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1249 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +08001250 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001251
1252 if (!this->swap_block_mark) {
1253 send_page_end(this, chip->oob_poi, mtd->oobsize,
1254 this->auxiliary_virt, this->auxiliary_phys,
1255 nfc_geo->auxiliary_size,
1256 auxiliary_virt, auxiliary_phys);
1257exit_auxiliary:
1258 send_page_end(this, buf, mtd->writesize,
1259 this->payload_virt, this->payload_phys,
1260 nfc_geo->payload_size,
1261 payload_virt, payload_phys);
1262 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001263
1264 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001265}
1266
1267/*
1268 * There are several places in this driver where we have to handle the OOB and
1269 * block marks. This is the function where things are the most complicated, so
1270 * this is where we try to explain it all. All the other places refer back to
1271 * here.
1272 *
1273 * These are the rules, in order of decreasing importance:
1274 *
1275 * 1) Nothing the caller does can be allowed to imperil the block mark.
1276 *
1277 * 2) In read operations, the first byte of the OOB we return must reflect the
1278 * true state of the block mark, no matter where that block mark appears in
1279 * the physical page.
1280 *
1281 * 3) ECC-based read operations return an OOB full of set bits (since we never
1282 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1283 * return).
1284 *
1285 * 4) "Raw" read operations return a direct view of the physical bytes in the
1286 * page, using the conventional definition of which bytes are data and which
1287 * are OOB. This gives the caller a way to see the actual, physical bytes
1288 * in the page, without the distortions applied by our ECC engine.
1289 *
1290 *
1291 * What we do for this specific read operation depends on two questions:
1292 *
1293 * 1) Are we doing a "raw" read, or an ECC-based read?
1294 *
1295 * 2) Are we using block mark swapping or transcription?
1296 *
1297 * There are four cases, illustrated by the following Karnaugh map:
1298 *
1299 * | Raw | ECC-based |
1300 * -------------+-------------------------+-------------------------+
1301 * | Read the conventional | |
1302 * | OOB at the end of the | |
1303 * Swapping | page and return it. It | |
1304 * | contains exactly what | |
1305 * | we want. | Read the block mark and |
1306 * -------------+-------------------------+ return it in a buffer |
1307 * | Read the conventional | full of set bits. |
1308 * | OOB at the end of the | |
1309 * | page and also the block | |
1310 * Transcribing | mark in the metadata. | |
1311 * | Copy the block mark | |
1312 * | into the first byte of | |
1313 * | the OOB. | |
1314 * -------------+-------------------------+-------------------------+
1315 *
1316 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1317 * giving an accurate view of the actual, physical bytes in the page (we're
1318 * overwriting the block mark). That's OK because it's more important to follow
1319 * rule #2.
1320 *
1321 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1322 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1323 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1324 * ECC-based or raw view of the page is implicit in which function it calls
1325 * (there is a similar pair of ECC-based/raw functions for writing).
Huang Shijie10a2bca2011-09-08 10:47:09 +08001326 */
1327static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001328 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001329{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001330 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001331
Huang Shijiec2325962013-11-20 10:09:44 +08001332 dev_dbg(this->dev, "page number is %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001333 /* clear the OOB buffer */
1334 memset(chip->oob_poi, ~0, mtd->oobsize);
1335
1336 /* Read out the conventional OOB. */
1337 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1338 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1339
1340 /*
1341 * Now, we want to make sure the block mark is correct. In the
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001342 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1343 * Otherwise, we need to explicitly read it.
Huang Shijie10a2bca2011-09-08 10:47:09 +08001344 */
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001345 if (GPMI_IS_MX23(this)) {
Huang Shijie10a2bca2011-09-08 10:47:09 +08001346 /* Read the block mark into the first byte of the OOB buffer. */
1347 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1348 chip->oob_poi[0] = chip->read_byte(mtd);
1349 }
1350
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001351 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001352}
1353
1354static int
1355gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1356{
Boris Brezillon191a8292016-02-03 20:11:44 +01001357 struct mtd_oob_region of = { };
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001358 int status = 0;
1359
1360 /* Do we have available oob area? */
Boris Brezillon191a8292016-02-03 20:11:44 +01001361 mtd_ooblayout_free(mtd, 0, &of);
1362 if (!of.length)
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001363 return -EPERM;
1364
1365 if (!nand_is_slc(chip))
1366 return -EPERM;
1367
Boris Brezillon191a8292016-02-03 20:11:44 +01001368 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of.offset, page);
1369 chip->write_buf(mtd, chip->oob_poi + of.offset, of.length);
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001370 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1371
1372 status = chip->waitfunc(mtd, chip);
1373 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001374}
1375
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001376/*
1377 * This function reads a NAND page without involving the ECC engine (no HW
1378 * ECC correction).
1379 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1380 * inline (interleaved with payload DATA), and do not align data chunk on
1381 * byte boundaries.
1382 * We thus need to take care moving the payload data and ECC bits stored in the
1383 * page into the provided buffers, which is why we're using gpmi_copy_bits.
1384 *
1385 * See set_geometry_by_ecc_info inline comments to have a full description
1386 * of the layout used by the GPMI controller.
1387 */
1388static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
1389 struct nand_chip *chip, uint8_t *buf,
1390 int oob_required, int page)
1391{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001392 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001393 struct bch_geometry *nfc_geo = &this->bch_geometry;
1394 int eccsize = nfc_geo->ecc_chunk_size;
1395 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1396 u8 *tmp_buf = this->raw_buffer;
1397 size_t src_bit_off;
1398 size_t oob_bit_off;
1399 size_t oob_byte_off;
1400 uint8_t *oob = chip->oob_poi;
1401 int step;
1402
1403 chip->read_buf(mtd, tmp_buf,
1404 mtd->writesize + mtd->oobsize);
1405
1406 /*
1407 * If required, swap the bad block marker and the data stored in the
1408 * metadata section, so that we don't wrongly consider a block as bad.
1409 *
1410 * See the layout description for a detailed explanation on why this
1411 * is needed.
1412 */
1413 if (this->swap_block_mark) {
1414 u8 swap = tmp_buf[0];
1415
1416 tmp_buf[0] = tmp_buf[mtd->writesize];
1417 tmp_buf[mtd->writesize] = swap;
1418 }
1419
1420 /*
1421 * Copy the metadata section into the oob buffer (this section is
1422 * guaranteed to be aligned on a byte boundary).
1423 */
1424 if (oob_required)
1425 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1426
1427 oob_bit_off = nfc_geo->metadata_size * 8;
1428 src_bit_off = oob_bit_off;
1429
1430 /* Extract interleaved payload data and ECC bits */
1431 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1432 if (buf)
1433 gpmi_copy_bits(buf, step * eccsize * 8,
1434 tmp_buf, src_bit_off,
1435 eccsize * 8);
1436 src_bit_off += eccsize * 8;
1437
1438 /* Align last ECC block to align a byte boundary */
1439 if (step == nfc_geo->ecc_chunk_count - 1 &&
1440 (oob_bit_off + eccbits) % 8)
1441 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1442
1443 if (oob_required)
1444 gpmi_copy_bits(oob, oob_bit_off,
1445 tmp_buf, src_bit_off,
1446 eccbits);
1447
1448 src_bit_off += eccbits;
1449 oob_bit_off += eccbits;
1450 }
1451
1452 if (oob_required) {
1453 oob_byte_off = oob_bit_off / 8;
1454
1455 if (oob_byte_off < mtd->oobsize)
1456 memcpy(oob + oob_byte_off,
1457 tmp_buf + mtd->writesize + oob_byte_off,
1458 mtd->oobsize - oob_byte_off);
1459 }
1460
1461 return 0;
1462}
1463
1464/*
1465 * This function writes a NAND page without involving the ECC engine (no HW
1466 * ECC generation).
1467 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1468 * inline (interleaved with payload DATA), and do not align data chunk on
1469 * byte boundaries.
1470 * We thus need to take care moving the OOB area at the right place in the
1471 * final page, which is why we're using gpmi_copy_bits.
1472 *
1473 * See set_geometry_by_ecc_info inline comments to have a full description
1474 * of the layout used by the GPMI controller.
1475 */
1476static int gpmi_ecc_write_page_raw(struct mtd_info *mtd,
1477 struct nand_chip *chip,
1478 const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001479 int oob_required, int page)
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001480{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001481 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001482 struct bch_geometry *nfc_geo = &this->bch_geometry;
1483 int eccsize = nfc_geo->ecc_chunk_size;
1484 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1485 u8 *tmp_buf = this->raw_buffer;
1486 uint8_t *oob = chip->oob_poi;
1487 size_t dst_bit_off;
1488 size_t oob_bit_off;
1489 size_t oob_byte_off;
1490 int step;
1491
1492 /*
1493 * Initialize all bits to 1 in case we don't have a buffer for the
1494 * payload or oob data in order to leave unspecified bits of data
1495 * to their initial state.
1496 */
1497 if (!buf || !oob_required)
1498 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1499
1500 /*
1501 * First copy the metadata section (stored in oob buffer) at the
1502 * beginning of the page, as imposed by the GPMI layout.
1503 */
1504 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1505 oob_bit_off = nfc_geo->metadata_size * 8;
1506 dst_bit_off = oob_bit_off;
1507
1508 /* Interleave payload data and ECC bits */
1509 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1510 if (buf)
1511 gpmi_copy_bits(tmp_buf, dst_bit_off,
1512 buf, step * eccsize * 8, eccsize * 8);
1513 dst_bit_off += eccsize * 8;
1514
1515 /* Align last ECC block to align a byte boundary */
1516 if (step == nfc_geo->ecc_chunk_count - 1 &&
1517 (oob_bit_off + eccbits) % 8)
1518 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1519
1520 if (oob_required)
1521 gpmi_copy_bits(tmp_buf, dst_bit_off,
1522 oob, oob_bit_off, eccbits);
1523
1524 dst_bit_off += eccbits;
1525 oob_bit_off += eccbits;
1526 }
1527
1528 oob_byte_off = oob_bit_off / 8;
1529
1530 if (oob_required && oob_byte_off < mtd->oobsize)
1531 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1532 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1533
1534 /*
1535 * If required, swap the bad block marker and the first byte of the
1536 * metadata section, so that we don't modify the bad block marker.
1537 *
1538 * See the layout description for a detailed explanation on why this
1539 * is needed.
1540 */
1541 if (this->swap_block_mark) {
1542 u8 swap = tmp_buf[0];
1543
1544 tmp_buf[0] = tmp_buf[mtd->writesize];
1545 tmp_buf[mtd->writesize] = swap;
1546 }
1547
1548 chip->write_buf(mtd, tmp_buf, mtd->writesize + mtd->oobsize);
1549
1550 return 0;
1551}
1552
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001553static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1554 int page)
1555{
1556 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1557
1558 return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
1559}
1560
1561static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1562 int page)
1563{
1564 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
1565
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001566 return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001567}
1568
Huang Shijie10a2bca2011-09-08 10:47:09 +08001569static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1570{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001571 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001572 struct gpmi_nand_data *this = nand_get_controller_data(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -07001573 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001574 uint8_t *block_mark;
1575 int column, page, status, chipnr;
1576
Brian Norris5a0edb22013-07-30 17:52:58 -07001577 chipnr = (int)(ofs >> chip->chip_shift);
1578 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001579
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001580 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001581
Brian Norris5a0edb22013-07-30 17:52:58 -07001582 /* Write the block mark. */
1583 block_mark = this->data_buffer_dma;
1584 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001585
Brian Norris5a0edb22013-07-30 17:52:58 -07001586 /* Shift to get page */
1587 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001588
Brian Norris5a0edb22013-07-30 17:52:58 -07001589 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1590 chip->write_buf(mtd, block_mark, 1);
1591 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001592
Brian Norris5a0edb22013-07-30 17:52:58 -07001593 status = chip->waitfunc(mtd, chip);
1594 if (status & NAND_STATUS_FAIL)
1595 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001596
Brian Norris5a0edb22013-07-30 17:52:58 -07001597 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001598
1599 return ret;
1600}
1601
Wolfram Sanga78da282012-03-21 19:29:17 +01001602static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001603{
1604 struct boot_rom_geometry *geometry = &this->rom_geometry;
1605
1606 /*
1607 * Set the boot block stride size.
1608 *
1609 * In principle, we should be reading this from the OTP bits, since
1610 * that's where the ROM is going to get it. In fact, we don't have any
1611 * way to read the OTP bits, so we go with the default and hope for the
1612 * best.
1613 */
1614 geometry->stride_size_in_pages = 64;
1615
1616 /*
1617 * Set the search area stride exponent.
1618 *
1619 * In principle, we should be reading this from the OTP bits, since
1620 * that's where the ROM is going to get it. In fact, we don't have any
1621 * way to read the OTP bits, so we go with the default and hope for the
1622 * best.
1623 */
1624 geometry->search_area_stride_exponent = 2;
1625 return 0;
1626}
1627
1628static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001629static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001630{
1631 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1632 struct device *dev = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001633 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001634 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001635 unsigned int search_area_size_in_strides;
1636 unsigned int stride;
1637 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001638 uint8_t *buffer = chip->buffers->databuf;
1639 int saved_chip_number;
1640 int found_an_ncb_fingerprint = false;
1641
1642 /* Compute the number of strides in a search area. */
1643 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1644
1645 saved_chip_number = this->current_chip;
1646 chip->select_chip(mtd, 0);
1647
1648 /*
1649 * Loop through the first search area, looking for the NCB fingerprint.
1650 */
1651 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1652
1653 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001654 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001655 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001656
1657 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1658
1659 /*
1660 * Read the NCB fingerprint. The fingerprint is four bytes long
1661 * and starts in the 12th byte of the page.
1662 */
1663 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1664 chip->read_buf(mtd, buffer, strlen(fingerprint));
1665
1666 /* Look for the fingerprint. */
1667 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1668 found_an_ncb_fingerprint = true;
1669 break;
1670 }
1671
1672 }
1673
1674 chip->select_chip(mtd, saved_chip_number);
1675
1676 if (found_an_ncb_fingerprint)
1677 dev_dbg(dev, "\tFound a fingerprint\n");
1678 else
1679 dev_dbg(dev, "\tNo fingerprint found\n");
1680 return found_an_ncb_fingerprint;
1681}
1682
1683/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001684static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001685{
1686 struct device *dev = this->dev;
1687 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001688 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001689 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001690 unsigned int block_size_in_pages;
1691 unsigned int search_area_size_in_strides;
1692 unsigned int search_area_size_in_pages;
1693 unsigned int search_area_size_in_blocks;
1694 unsigned int block;
1695 unsigned int stride;
1696 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001697 uint8_t *buffer = chip->buffers->databuf;
1698 int saved_chip_number;
1699 int status;
1700
1701 /* Compute the search area geometry. */
1702 block_size_in_pages = mtd->erasesize / mtd->writesize;
1703 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1704 search_area_size_in_pages = search_area_size_in_strides *
1705 rom_geo->stride_size_in_pages;
1706 search_area_size_in_blocks =
1707 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1708 block_size_in_pages;
1709
1710 dev_dbg(dev, "Search Area Geometry :\n");
1711 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1712 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1713 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1714
1715 /* Select chip 0. */
1716 saved_chip_number = this->current_chip;
1717 chip->select_chip(mtd, 0);
1718
1719 /* Loop over blocks in the first search area, erasing them. */
1720 dev_dbg(dev, "Erasing the search area...\n");
1721
1722 for (block = 0; block < search_area_size_in_blocks; block++) {
1723 /* Compute the page address. */
1724 page = block * block_size_in_pages;
1725
1726 /* Erase this block. */
1727 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1728 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1729 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1730
1731 /* Wait for the erase to finish. */
1732 status = chip->waitfunc(mtd, chip);
1733 if (status & NAND_STATUS_FAIL)
1734 dev_err(dev, "[%s] Erase failed.\n", __func__);
1735 }
1736
1737 /* Write the NCB fingerprint into the page buffer. */
1738 memset(buffer, ~0, mtd->writesize);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001739 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1740
1741 /* Loop through the first search area, writing NCB fingerprints. */
1742 dev_dbg(dev, "Writing NCB fingerprints...\n");
1743 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001744 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001745 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001746
1747 /* Write the first page of the current stride. */
1748 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1749 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001750 chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001751 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1752
1753 /* Wait for the write to finish. */
1754 status = chip->waitfunc(mtd, chip);
1755 if (status & NAND_STATUS_FAIL)
1756 dev_err(dev, "[%s] Write failed.\n", __func__);
1757 }
1758
1759 /* Deselect chip 0. */
1760 chip->select_chip(mtd, saved_chip_number);
1761 return 0;
1762}
1763
Wolfram Sanga78da282012-03-21 19:29:17 +01001764static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001765{
1766 struct device *dev = this->dev;
1767 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001768 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001769 unsigned int block_count;
1770 unsigned int block;
1771 int chipnr;
1772 int page;
1773 loff_t byte;
1774 uint8_t block_mark;
1775 int ret = 0;
1776
1777 /*
1778 * If control arrives here, we can't use block mark swapping, which
1779 * means we're forced to use transcription. First, scan for the
1780 * transcription stamp. If we find it, then we don't have to do
1781 * anything -- the block marks are already transcribed.
1782 */
1783 if (mx23_check_transcription_stamp(this))
1784 return 0;
1785
1786 /*
1787 * If control arrives here, we couldn't find a transcription stamp, so
1788 * so we presume the block marks are in the conventional location.
1789 */
1790 dev_dbg(dev, "Transcribing bad block marks...\n");
1791
1792 /* Compute the number of blocks in the entire medium. */
1793 block_count = chip->chipsize >> chip->phys_erase_shift;
1794
1795 /*
1796 * Loop over all the blocks in the medium, transcribing block marks as
1797 * we go.
1798 */
1799 for (block = 0; block < block_count; block++) {
1800 /*
1801 * Compute the chip, page and byte addresses for this block's
1802 * conventional mark.
1803 */
1804 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1805 page = block << (chip->phys_erase_shift - chip->page_shift);
1806 byte = block << chip->phys_erase_shift;
1807
1808 /* Send the command to read the conventional block mark. */
1809 chip->select_chip(mtd, chipnr);
1810 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1811 block_mark = chip->read_byte(mtd);
1812 chip->select_chip(mtd, -1);
1813
1814 /*
1815 * Check if the block is marked bad. If so, we need to mark it
1816 * again, but this time the result will be a mark in the
1817 * location where we transcribe block marks.
1818 */
1819 if (block_mark != 0xff) {
1820 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1821 ret = chip->block_markbad(mtd, byte);
1822 if (ret)
Lothar Waßmannd8c03722014-06-12 15:20:42 +02001823 dev_err(dev,
1824 "Failed to mark block bad with ret %d\n",
1825 ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001826 }
1827 }
1828
1829 /* Write the stamp that indicates we've transcribed the block marks. */
1830 mx23_write_transcription_stamp(this);
1831 return 0;
1832}
1833
Wolfram Sanga78da282012-03-21 19:29:17 +01001834static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001835{
1836 nand_boot_set_geometry(this);
1837
1838 /* This is ROM arch-specific initilization before the BBT scanning. */
1839 if (GPMI_IS_MX23(this))
1840 return mx23_boot_init(this);
1841 return 0;
1842}
1843
Wolfram Sanga78da282012-03-21 19:29:17 +01001844static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001845{
1846 int ret;
1847
1848 /* Free the temporary DMA memory for reading ID. */
1849 gpmi_free_dma_buffer(this);
1850
1851 /* Set up the NFC geometry which is used by BCH. */
1852 ret = bch_set_geometry(this);
1853 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001854 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001855 return ret;
1856 }
1857
1858 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1859 return gpmi_alloc_dma_buffer(this);
1860}
1861
Huang Shijieccce4172013-11-14 14:25:47 +08001862static void gpmi_nand_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001863{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001864 nand_release(nand_to_mtd(&this->nand));
Huang Shijief720e7c2013-08-16 10:10:08 +08001865 gpmi_free_dma_buffer(this);
1866}
1867
1868static int gpmi_init_last(struct gpmi_nand_data *this)
1869{
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001870 struct nand_chip *chip = &this->nand;
Boris Brezillon3f158e42016-02-03 20:01:54 +01001871 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijief720e7c2013-08-16 10:10:08 +08001872 struct nand_ecc_ctrl *ecc = &chip->ecc;
1873 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001874 int ret;
1875
Huang Shijied7364a272013-11-14 14:25:45 +08001876 /* Set up the medium geometry */
1877 ret = gpmi_set_geometry(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001878 if (ret)
1879 return ret;
1880
Huang Shijief720e7c2013-08-16 10:10:08 +08001881 /* Init the nand_ecc_ctrl{} */
1882 ecc->read_page = gpmi_ecc_read_page;
1883 ecc->write_page = gpmi_ecc_write_page;
1884 ecc->read_oob = gpmi_ecc_read_oob;
1885 ecc->write_oob = gpmi_ecc_write_oob;
Boris BREZILLONda3bc42c2014-11-30 19:10:29 +01001886 ecc->read_page_raw = gpmi_ecc_read_page_raw;
1887 ecc->write_page_raw = gpmi_ecc_write_page_raw;
Boris BREZILLON7ca94e02014-11-30 19:10:30 +01001888 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1889 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
Huang Shijief720e7c2013-08-16 10:10:08 +08001890 ecc->mode = NAND_ECC_HW;
1891 ecc->size = bch_geo->ecc_chunk_size;
1892 ecc->strength = bch_geo->ecc_strength;
Boris Brezillon3f158e42016-02-03 20:01:54 +01001893 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
Huang Shijief720e7c2013-08-16 10:10:08 +08001894
Huang Shijie995fbbf2012-09-13 14:57:59 +08001895 /*
Huang Shijieb8e29312014-01-03 11:01:42 +08001896 * We only enable the subpage read when:
1897 * (1) the chip is imx6, and
1898 * (2) the size of the ECC parity is byte aligned.
1899 */
Huang Shijie91f54982014-03-27 10:43:22 +08001900 if (GPMI_IS_MX6(this) &&
Huang Shijieb8e29312014-01-03 11:01:42 +08001901 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1902 ecc->read_subpage = gpmi_ecc_read_subpage;
1903 chip->options |= NAND_SUBPAGE_READ;
1904 }
1905
1906 /*
Huang Shijie995fbbf2012-09-13 14:57:59 +08001907 * Can we enable the extra features? such as EDO or Sync mode.
1908 *
1909 * We do not check the return value now. That's means if we fail in
1910 * enable the extra features, we still can run in the normal way.
1911 */
1912 gpmi_extra_init(this);
1913
Huang Shijief720e7c2013-08-16 10:10:08 +08001914 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001915}
1916
Huang Shijieccce4172013-11-14 14:25:47 +08001917static int gpmi_nand_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001918{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001919 struct nand_chip *chip = &this->nand;
Boris BREZILLON2a690b22015-12-10 09:00:07 +01001920 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001921 int ret;
1922
1923 /* init current chip */
1924 this->current_chip = -1;
1925
1926 /* init the MTD data structures */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001927 mtd->name = "gpmi-nand";
Frans Klaver4dc67b12015-06-10 22:38:49 +02001928 mtd->dev.parent = this->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001929
1930 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001931 nand_set_controller_data(chip, this);
Brian Norrisa61ae812015-10-30 20:33:25 -07001932 nand_set_flash_node(chip, this->pdev->dev.of_node);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001933 chip->select_chip = gpmi_select_chip;
1934 chip->cmd_ctrl = gpmi_cmd_ctrl;
1935 chip->dev_ready = gpmi_dev_ready;
1936 chip->read_byte = gpmi_read_byte;
1937 chip->read_buf = gpmi_read_buf;
1938 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001939 chip->badblock_pattern = &gpmi_bbt_descr;
1940 chip->block_markbad = gpmi_block_markbad;
1941 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lothar Waßmann2a500af2014-03-28 11:35:06 +01001942
1943 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1944 this->swap_block_mark = !GPMI_IS_MX23(this);
1945
Huang Shijief720e7c2013-08-16 10:10:08 +08001946 /*
1947 * Allocate a temporary DMA buffer for reading ID in the
1948 * nand_scan_ident().
1949 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001950 this->bch_geometry.payload_size = 1024;
1951 this->bch_geometry.auxiliary_size = 128;
1952 ret = gpmi_alloc_dma_buffer(this);
1953 if (ret)
1954 goto err_out;
1955
Huang Shijie91f54982014-03-27 10:43:22 +08001956 ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
Huang Shijief720e7c2013-08-16 10:10:08 +08001957 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001958 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001959
Boris Brezillonf05f6a12016-04-01 14:54:26 +02001960 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1961 chip->bbt_options |= NAND_BBT_NO_OOB;
1962
1963 if (of_property_read_bool(this->dev->of_node,
1964 "fsl,no-blockmark-swap"))
1965 this->swap_block_mark = false;
1966 }
1967 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
1968 this->swap_block_mark ? "en" : "dis");
1969
Huang Shijief720e7c2013-08-16 10:10:08 +08001970 ret = gpmi_init_last(this);
1971 if (ret)
1972 goto err_out;
1973
Huang Shijie885d71e2013-11-12 12:23:08 +08001974 chip->options |= NAND_SKIP_BBTSCAN;
Huang Shijief720e7c2013-08-16 10:10:08 +08001975 ret = nand_scan_tail(mtd);
1976 if (ret)
1977 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001978
Huang Shijie885d71e2013-11-12 12:23:08 +08001979 ret = nand_boot_init(this);
1980 if (ret)
1981 goto err_out;
Fabio Estevam899b8342015-02-09 19:22:33 -02001982 ret = chip->scan_bbt(mtd);
1983 if (ret)
1984 goto err_out;
Huang Shijie885d71e2013-11-12 12:23:08 +08001985
Brian Norrisa61ae812015-10-30 20:33:25 -07001986 ret = mtd_device_register(mtd, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001987 if (ret)
1988 goto err_out;
1989 return 0;
1990
1991err_out:
Huang Shijieccce4172013-11-14 14:25:47 +08001992 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001993 return ret;
1994}
1995
Huang Shijiee10db1f2012-05-04 21:42:05 -04001996static const struct of_device_id gpmi_nand_id_table[] = {
1997 {
1998 .compatible = "fsl,imx23-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02001999 .data = &gpmi_devdata_imx23,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002000 }, {
2001 .compatible = "fsl,imx28-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002002 .data = &gpmi_devdata_imx28,
Huang Shijie9013bb42012-05-04 21:42:06 -04002003 }, {
2004 .compatible = "fsl,imx6q-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002005 .data = &gpmi_devdata_imx6q,
Huang Shijie91f54982014-03-27 10:43:22 +08002006 }, {
2007 .compatible = "fsl,imx6sx-gpmi-nand",
Lothar Waßmann6a760962014-06-12 15:20:41 +02002008 .data = &gpmi_devdata_imx6sx,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002009 }, {}
2010};
2011MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
2012
Bill Pemberton06f25512012-11-19 13:23:07 -05002013static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08002014{
Huang Shijie10a2bca2011-09-08 10:47:09 +08002015 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002016 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08002017 int ret;
2018
Huang Shijie6189ccc2014-03-21 18:19:39 +08002019 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
2020 if (!this)
2021 return -ENOMEM;
2022
Huang Shijiee10db1f2012-05-04 21:42:05 -04002023 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
2024 if (of_id) {
Huang Shijie6189ccc2014-03-21 18:19:39 +08002025 this->devdata = of_id->data;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002026 } else {
Huang Shijieda40c162013-11-20 10:09:43 +08002027 dev_err(&pdev->dev, "Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02002028 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04002029 }
2030
Huang Shijie10a2bca2011-09-08 10:47:09 +08002031 platform_set_drvdata(pdev, this);
2032 this->pdev = pdev;
2033 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08002034
2035 ret = acquire_resources(this);
2036 if (ret)
2037 goto exit_acquire_resources;
2038
2039 ret = init_hardware(this);
2040 if (ret)
2041 goto exit_nfc_init;
2042
Huang Shijieccce4172013-11-14 14:25:47 +08002043 ret = gpmi_nand_init(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002044 if (ret)
2045 goto exit_nfc_init;
2046
Fabio Estevam490e2802012-09-05 11:35:24 -03002047 dev_info(this->dev, "driver registered.\n");
2048
Huang Shijie10a2bca2011-09-08 10:47:09 +08002049 return 0;
2050
2051exit_nfc_init:
2052 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002053exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03002054
Huang Shijie10a2bca2011-09-08 10:47:09 +08002055 return ret;
2056}
2057
Bill Pemberton810b7e02012-11-19 13:26:04 -05002058static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08002059{
2060 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2061
Huang Shijieccce4172013-11-14 14:25:47 +08002062 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002063 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002064 return 0;
2065}
2066
Huang Shijie026918e2015-12-02 16:47:40 -06002067#ifdef CONFIG_PM_SLEEP
2068static int gpmi_pm_suspend(struct device *dev)
2069{
2070 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2071
2072 release_dma_channels(this);
2073 return 0;
2074}
2075
2076static int gpmi_pm_resume(struct device *dev)
2077{
2078 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2079 int ret;
2080
2081 ret = acquire_dma_channels(this);
2082 if (ret < 0)
2083 return ret;
2084
2085 /* re-init the GPMI registers */
2086 this->flags &= ~GPMI_TIMING_INIT_OK;
2087 ret = gpmi_init(this);
2088 if (ret) {
2089 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2090 return ret;
2091 }
2092
2093 /* re-init the BCH registers */
2094 ret = bch_set_geometry(this);
2095 if (ret) {
2096 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2097 return ret;
2098 }
2099
2100 /* re-init others */
2101 gpmi_extra_init(this);
2102
2103 return 0;
2104}
2105#endif /* CONFIG_PM_SLEEP */
2106
2107static const struct dev_pm_ops gpmi_pm_ops = {
2108 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2109};
2110
Huang Shijie10a2bca2011-09-08 10:47:09 +08002111static struct platform_driver gpmi_nand_driver = {
2112 .driver = {
2113 .name = "gpmi-nand",
Huang Shijie026918e2015-12-02 16:47:40 -06002114 .pm = &gpmi_pm_ops,
Huang Shijiee10db1f2012-05-04 21:42:05 -04002115 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002116 },
2117 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05002118 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08002119};
Fabio Estevam490e2802012-09-05 11:35:24 -03002120module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08002121
2122MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2123MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2124MODULE_LICENSE("GPL");