blob: e88d64e1e9632df9e349616af77d204e2f4e4ca3 [file] [log] [blame]
Huang Shijie10a2bca2011-09-08 10:47:09 +08001/*
2 * Freescale GPMI NAND Flash Driver
3 *
4 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
5 * 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 Shijie10a2bca2011-09-08 10:47:09 +080074static irqreturn_t bch_irq(int irq, void *cookie)
75{
76 struct gpmi_nand_data *this = cookie;
77
78 gpmi_clear_bch(this);
79 complete(&this->bch_done);
80 return IRQ_HANDLED;
81}
82
83/*
84 * Calculate the ECC strength by hand:
85 * E : The ECC strength.
86 * G : the length of Galois Field.
87 * N : The chunk count of per page.
88 * O : the oobsize of the NAND chip.
89 * M : the metasize of per page.
90 *
91 * The formula is :
92 * E * G * N
93 * ------------ <= (O - M)
94 * 8
95 *
96 * So, we get E by:
97 * (O - M) * 8
98 * E <= -------------
99 * G * N
100 */
101static inline int get_ecc_strength(struct gpmi_nand_data *this)
102{
103 struct bch_geometry *geo = &this->bch_geometry;
104 struct mtd_info *mtd = &this->mtd;
105 int ecc_strength;
106
107 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
108 / (geo->gf_len * geo->ecc_chunk_count);
109
110 /* We need the minor even number. */
111 return round_down(ecc_strength, 2);
112}
113
Huang Shijie92d0e092013-01-29 09:23:38 +0800114static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
115{
116 struct bch_geometry *geo = &this->bch_geometry;
117
118 /* Do the sanity check. */
119 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
120 /* The mx23/mx28 only support the GF13. */
121 if (geo->gf_len == 14)
122 return false;
Huang Shijie92d0e092013-01-29 09:23:38 +0800123 }
Huang Shijie6189ccc2014-03-21 18:19:39 +0800124 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
Huang Shijie92d0e092013-01-29 09:23:38 +0800125}
126
Huang Shijie2febcdf2013-05-17 11:17:34 +0800127/*
128 * If we can get the ECC information from the nand chip, we do not
129 * need to calculate them ourselves.
130 *
131 * We may have available oob space in this case.
132 */
133static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
134{
135 struct bch_geometry *geo = &this->bch_geometry;
136 struct mtd_info *mtd = &this->mtd;
137 struct nand_chip *chip = mtd->priv;
138 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
139 unsigned int block_mark_bit_offset;
140
141 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
142 return false;
143
144 switch (chip->ecc_step_ds) {
145 case SZ_512:
146 geo->gf_len = 13;
147 break;
148 case SZ_1K:
149 geo->gf_len = 14;
150 break;
151 default:
152 dev_err(this->dev,
153 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
154 chip->ecc_strength_ds, chip->ecc_step_ds);
155 return false;
156 }
157 geo->ecc_chunk_size = chip->ecc_step_ds;
158 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
159 if (!gpmi_check_ecc(this))
160 return false;
161
162 /* Keep the C >= O */
163 if (geo->ecc_chunk_size < mtd->oobsize) {
164 dev_err(this->dev,
165 "unsupported nand chip. ecc size: %d, oob size : %d\n",
166 chip->ecc_step_ds, mtd->oobsize);
167 return false;
168 }
169
170 /* The default value, see comment in the legacy_set_geometry(). */
171 geo->metadata_size = 10;
172
173 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
174
175 /*
176 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
177 *
178 * | P |
179 * |<----------------------------------------------------->|
180 * | |
181 * | (Block Mark) |
182 * | P' | | | |
183 * |<-------------------------------------------->| D | | O' |
184 * | |<---->| |<--->|
185 * V V V V V
186 * +---+----------+-+----------+-+----------+-+----------+-+-----+
187 * | M | data |E| data |E| data |E| data |E| |
188 * +---+----------+-+----------+-+----------+-+----------+-+-----+
189 * ^ ^
190 * | O |
191 * |<------------>|
192 * | |
193 *
194 * P : the page size for BCH module.
195 * E : The ECC strength.
196 * G : the length of Galois Field.
197 * N : The chunk count of per page.
198 * M : the metasize of per page.
199 * C : the ecc chunk size, aka the "data" above.
200 * P': the nand chip's page size.
201 * O : the nand chip's oob size.
202 * O': the free oob.
203 *
204 * The formula for P is :
205 *
206 * E * G * N
207 * P = ------------ + P' + M
208 * 8
209 *
210 * The position of block mark moves forward in the ECC-based view
211 * of page, and the delta is:
212 *
213 * E * G * (N - 1)
214 * D = (---------------- + M)
215 * 8
216 *
217 * Please see the comment in legacy_set_geometry().
218 * With the condition C >= O , we still can get same result.
219 * So the bit position of the physical block mark within the ECC-based
220 * view of the page is :
221 * (P' - D) * 8
222 */
223 geo->page_size = mtd->writesize + geo->metadata_size +
224 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
225
226 /* The available oob size we have. */
227 if (geo->page_size < mtd->writesize + mtd->oobsize) {
228 of->offset = geo->page_size - mtd->writesize;
229 of->length = mtd->oobsize - of->offset;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800230 }
231
232 geo->payload_size = mtd->writesize;
233
234 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
235 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
236 + ALIGN(geo->ecc_chunk_count, 4);
237
238 if (!this->swap_block_mark)
239 return true;
240
241 /* For bit swap. */
242 block_mark_bit_offset = mtd->writesize * 8 -
243 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
244 + geo->metadata_size * 8);
245
246 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
247 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
248 return true;
249}
250
251static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800252{
253 struct bch_geometry *geo = &this->bch_geometry;
254 struct mtd_info *mtd = &this->mtd;
255 unsigned int metadata_size;
256 unsigned int status_size;
257 unsigned int block_mark_bit_offset;
258
259 /*
260 * The size of the metadata can be changed, though we set it to 10
261 * bytes now. But it can't be too large, because we have to save
262 * enough space for BCH.
263 */
264 geo->metadata_size = 10;
265
266 /* The default for the length of Galois Field. */
267 geo->gf_len = 13;
268
Huang Shijie9ff16f02013-01-25 14:04:07 +0800269 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800270 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800271 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800272 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800273 geo->gf_len = 14;
274 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800275
276 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
277
278 /* We use the same ECC strength for all chunks. */
279 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800280 if (!gpmi_check_ecc(this)) {
281 dev_err(this->dev,
282 "We can not support this nand chip."
283 " Its required ecc strength(%d) is beyond our"
284 " capability(%d).\n", geo->ecc_strength,
Huang Shijie6189ccc2014-03-21 18:19:39 +0800285 this->devdata->bch_max_ecc_strength);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800286 return -EINVAL;
287 }
288
289 geo->page_size = mtd->writesize + mtd->oobsize;
290 geo->payload_size = mtd->writesize;
291
292 /*
293 * The auxiliary buffer contains the metadata and the ECC status. The
294 * metadata is padded to the nearest 32-bit boundary. The ECC status
295 * contains one byte for every ECC chunk, and is also padded to the
296 * nearest 32-bit boundary.
297 */
298 metadata_size = ALIGN(geo->metadata_size, 4);
299 status_size = ALIGN(geo->ecc_chunk_count, 4);
300
301 geo->auxiliary_size = metadata_size + status_size;
302 geo->auxiliary_status_offset = metadata_size;
303
304 if (!this->swap_block_mark)
305 return 0;
306
307 /*
308 * We need to compute the byte and bit offsets of
309 * the physical block mark within the ECC-based view of the page.
310 *
311 * NAND chip with 2K page shows below:
312 * (Block Mark)
313 * | |
314 * | D |
315 * |<---->|
316 * V V
317 * +---+----------+-+----------+-+----------+-+----------+-+
318 * | M | data |E| data |E| data |E| data |E|
319 * +---+----------+-+----------+-+----------+-+----------+-+
320 *
321 * The position of block mark moves forward in the ECC-based view
322 * of page, and the delta is:
323 *
324 * E * G * (N - 1)
325 * D = (---------------- + M)
326 * 8
327 *
328 * With the formula to compute the ECC strength, and the condition
329 * : C >= O (C is the ecc chunk size)
330 *
331 * It's easy to deduce to the following result:
332 *
333 * E * G (O - M) C - M C - M
334 * ----------- <= ------- <= -------- < ---------
335 * 8 N N (N - 1)
336 *
337 * So, we get:
338 *
339 * E * G * (N - 1)
340 * D = (---------------- + M) < C
341 * 8
342 *
343 * The above inequality means the position of block mark
344 * within the ECC-based view of the page is still in the data chunk,
345 * and it's NOT in the ECC bits of the chunk.
346 *
347 * Use the following to compute the bit position of the
348 * physical block mark within the ECC-based view of the page:
349 * (page_size - D) * 8
350 *
351 * --Huang Shijie
352 */
353 block_mark_bit_offset = mtd->writesize * 8 -
354 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
355 + geo->metadata_size * 8);
356
357 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
358 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
359 return 0;
360}
361
Huang Shijie2febcdf2013-05-17 11:17:34 +0800362int common_nfc_set_geometry(struct gpmi_nand_data *this)
363{
Huang Shijie89b59e62013-11-07 18:07:38 +0800364 if (of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")
365 && set_geometry_by_ecc_info(this))
366 return 0;
David Woodhouse031e2772013-10-25 15:03:59 +0100367 return legacy_set_geometry(this);
Huang Shijie2febcdf2013-05-17 11:17:34 +0800368}
369
Huang Shijie10a2bca2011-09-08 10:47:09 +0800370struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
371{
Huang Shijiea7c12d02013-08-27 17:29:05 +0800372 /* We use the DMA channel 0 to access all the nand chips. */
373 return this->dma_chans[0];
Huang Shijie10a2bca2011-09-08 10:47:09 +0800374}
375
376/* Can we use the upper's buffer directly for DMA? */
377void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
378{
379 struct scatterlist *sgl = &this->data_sgl;
380 int ret;
381
Huang Shijie10a2bca2011-09-08 10:47:09 +0800382 /* first try to map the upper buffer directly */
Huang Shijie0ff76a92013-12-18 23:41:00 +0800383 if (virt_addr_valid(this->upper_buf) &&
384 !object_is_on_stack(this->upper_buf)) {
385 sg_init_one(sgl, this->upper_buf, this->upper_len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800386 ret = dma_map_sg(this->dev, sgl, 1, dr);
387 if (ret == 0)
Huang Shijie0ff76a92013-12-18 23:41:00 +0800388 goto map_fail;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800389
Huang Shijie0ff76a92013-12-18 23:41:00 +0800390 this->direct_dma_map_ok = true;
391 return;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800392 }
Huang Shijie0ff76a92013-12-18 23:41:00 +0800393
394map_fail:
395 /* We have to use our own DMA buffer. */
396 sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
397
398 if (dr == DMA_TO_DEVICE)
399 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
400
401 dma_map_sg(this->dev, sgl, 1, dr);
402
403 this->direct_dma_map_ok = false;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800404}
405
406/* This will be called after the DMA operation is finished. */
407static void dma_irq_callback(void *param)
408{
409 struct gpmi_nand_data *this = param;
410 struct completion *dma_c = &this->dma_done;
411
Huang Shijie10a2bca2011-09-08 10:47:09 +0800412 switch (this->dma_type) {
413 case DMA_FOR_COMMAND:
414 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
415 break;
416
417 case DMA_FOR_READ_DATA:
418 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
419 if (this->direct_dma_map_ok == false)
420 memcpy(this->upper_buf, this->data_buffer_dma,
421 this->upper_len);
422 break;
423
424 case DMA_FOR_WRITE_DATA:
425 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
426 break;
427
428 case DMA_FOR_READ_ECC_PAGE:
429 case DMA_FOR_WRITE_ECC_PAGE:
430 /* We have to wait the BCH interrupt to finish. */
431 break;
432
433 default:
Huang Shijieda40c162013-11-20 10:09:43 +0800434 dev_err(this->dev, "in wrong DMA operation.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800435 }
Huang Shijie7b3d2fb2013-11-11 12:13:45 +0800436
437 complete(dma_c);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800438}
439
440int start_dma_without_bch_irq(struct gpmi_nand_data *this,
441 struct dma_async_tx_descriptor *desc)
442{
443 struct completion *dma_c = &this->dma_done;
444 int err;
445
446 init_completion(dma_c);
447
448 desc->callback = dma_irq_callback;
449 desc->callback_param = this;
450 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800451 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800452
453 /* Wait for the interrupt from the DMA block. */
454 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
455 if (!err) {
Huang Shijieda40c162013-11-20 10:09:43 +0800456 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
457 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800458 gpmi_dump_info(this);
459 return -ETIMEDOUT;
460 }
461 return 0;
462}
463
464/*
465 * This function is used in BCH reading or BCH writing pages.
466 * It will wait for the BCH interrupt as long as ONE second.
467 * Actually, we must wait for two interrupts :
468 * [1] firstly the DMA interrupt and
469 * [2] secondly the BCH interrupt.
470 */
471int start_dma_with_bch_irq(struct gpmi_nand_data *this,
472 struct dma_async_tx_descriptor *desc)
473{
474 struct completion *bch_c = &this->bch_done;
475 int err;
476
477 /* Prepare to receive an interrupt from the BCH block. */
478 init_completion(bch_c);
479
480 /* start the DMA */
481 start_dma_without_bch_irq(this, desc);
482
483 /* Wait for the interrupt from the BCH block. */
484 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
485 if (!err) {
Huang Shijieda40c162013-11-20 10:09:43 +0800486 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
487 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800488 gpmi_dump_info(this);
489 return -ETIMEDOUT;
490 }
491 return 0;
492}
493
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800494static int acquire_register_block(struct gpmi_nand_data *this,
495 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800496{
497 struct platform_device *pdev = this->pdev;
498 struct resources *res = &this->resources;
499 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800500 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800501
502 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
Huang Shijie87a9d692013-11-14 14:25:48 +0800503 p = devm_ioremap_resource(&pdev->dev, r);
504 if (IS_ERR(p))
505 return PTR_ERR(p);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800506
507 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
508 res->gpmi_regs = p;
509 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
510 res->bch_regs = p;
511 else
Huang Shijieda40c162013-11-20 10:09:43 +0800512 dev_err(this->dev, "unknown resource name : %s\n", res_name);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800513
514 return 0;
515}
516
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800517static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800518{
519 struct platform_device *pdev = this->pdev;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800520 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
521 struct resource *r;
522 int err;
523
524 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
525 if (!r) {
Huang Shijieda40c162013-11-20 10:09:43 +0800526 dev_err(this->dev, "Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200527 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800528 }
529
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800530 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
531 if (err)
532 dev_err(this->dev, "error requesting BCH IRQ\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800533
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800534 return err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800535}
536
Huang Shijie10a2bca2011-09-08 10:47:09 +0800537static void release_dma_channels(struct gpmi_nand_data *this)
538{
539 unsigned int i;
540 for (i = 0; i < DMA_CHANS; i++)
541 if (this->dma_chans[i]) {
542 dma_release_channel(this->dma_chans[i]);
543 this->dma_chans[i] = NULL;
544 }
545}
546
Bill Pemberton06f25512012-11-19 13:23:07 -0500547static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800548{
549 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400550 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400551
552 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800553 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400554 if (!dma_chan) {
Huang Shijieda40c162013-11-20 10:09:43 +0800555 dev_err(this->dev, "Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400556 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800557 }
558
Huang Shijiee10db1f2012-05-04 21:42:05 -0400559 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800560 return 0;
561
562acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800563 release_dma_channels(this);
564 return -EINVAL;
565}
566
Huang Shijieff506172012-07-02 21:39:32 -0400567static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
568 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
569};
570
Bill Pemberton06f25512012-11-19 13:23:07 -0500571static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400572{
573 struct resources *r = &this->resources;
574 char **extra_clks = NULL;
575 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200576 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400577
578 /* The main clock is stored in the first. */
Fabio Estevam554cbc52013-11-07 22:32:38 -0200579 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200580 if (IS_ERR(r->clock[0])) {
581 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400582 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200583 }
Huang Shijieff506172012-07-02 21:39:32 -0400584
585 /* Get extra clocks */
586 if (GPMI_IS_MX6Q(this))
587 extra_clks = extra_clks_for_mx6q;
588 if (!extra_clks)
589 return 0;
590
591 for (i = 1; i < GPMI_CLK_MAX; i++) {
592 if (extra_clks[i - 1] == NULL)
593 break;
594
Fabio Estevam554cbc52013-11-07 22:32:38 -0200595 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200596 if (IS_ERR(clk)) {
597 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400598 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200599 }
Huang Shijieff506172012-07-02 21:39:32 -0400600
601 r->clock[i] = clk;
602 }
603
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800604 if (GPMI_IS_MX6Q(this))
Huang Shijieff506172012-07-02 21:39:32 -0400605 /*
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800606 * Set the default value for the gpmi clock in mx6q:
Huang Shijieff506172012-07-02 21:39:32 -0400607 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800608 * If you want to use the ONFI nand which is in the
609 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400610 */
611 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800612
Huang Shijieff506172012-07-02 21:39:32 -0400613 return 0;
614
615err_clock:
616 dev_dbg(this->dev, "failed in finding the clocks.\n");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200617 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400618}
619
Bill Pemberton06f25512012-11-19 13:23:07 -0500620static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800621{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800622 int ret;
623
624 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
625 if (ret)
626 goto exit_regs;
627
628 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
629 if (ret)
630 goto exit_regs;
631
632 ret = acquire_bch_irq(this, bch_irq);
633 if (ret)
634 goto exit_regs;
635
636 ret = acquire_dma_channels(this);
637 if (ret)
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800638 goto exit_regs;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800639
Huang Shijieff506172012-07-02 21:39:32 -0400640 ret = gpmi_get_clks(this);
641 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800642 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800643 return 0;
644
645exit_clock:
646 release_dma_channels(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800647exit_regs:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800648 return ret;
649}
650
651static void release_resources(struct gpmi_nand_data *this)
652{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800653 release_dma_channels(this);
654}
655
Bill Pemberton06f25512012-11-19 13:23:07 -0500656static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800657{
658 int ret;
659
660 /*
661 * This structure contains the "safe" GPMI timing that should succeed
662 * with any NAND Flash device
663 * (although, with less-than-optimal performance).
664 */
665 struct nand_timing safe_timing = {
666 .data_setup_in_ns = 80,
667 .data_hold_in_ns = 60,
668 .address_setup_in_ns = 25,
669 .gpmi_sample_delay_in_ns = 6,
670 .tREA_in_ns = -1,
671 .tRLOH_in_ns = -1,
672 .tRHOH_in_ns = -1,
673 };
674
675 /* Initialize the hardwares. */
676 ret = gpmi_init(this);
677 if (ret)
678 return ret;
679
680 this->timing = safe_timing;
681 return 0;
682}
683
684static int read_page_prepare(struct gpmi_nand_data *this,
685 void *destination, unsigned length,
686 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
687 void **use_virt, dma_addr_t *use_phys)
688{
689 struct device *dev = this->dev;
690
691 if (virt_addr_valid(destination)) {
692 dma_addr_t dest_phys;
693
694 dest_phys = dma_map_single(dev, destination,
695 length, DMA_FROM_DEVICE);
696 if (dma_mapping_error(dev, dest_phys)) {
697 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800698 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800699 return -ENOMEM;
700 }
701 goto map_failed;
702 }
703 *use_virt = destination;
704 *use_phys = dest_phys;
705 this->direct_dma_map_ok = true;
706 return 0;
707 }
708
709map_failed:
710 *use_virt = alt_virt;
711 *use_phys = alt_phys;
712 this->direct_dma_map_ok = false;
713 return 0;
714}
715
716static inline void read_page_end(struct gpmi_nand_data *this,
717 void *destination, unsigned length,
718 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
719 void *used_virt, dma_addr_t used_phys)
720{
721 if (this->direct_dma_map_ok)
722 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
723}
724
725static inline void read_page_swap_end(struct gpmi_nand_data *this,
726 void *destination, unsigned length,
727 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
728 void *used_virt, dma_addr_t used_phys)
729{
730 if (!this->direct_dma_map_ok)
731 memcpy(destination, alt_virt, length);
732}
733
734static int send_page_prepare(struct gpmi_nand_data *this,
735 const void *source, unsigned length,
736 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
737 const void **use_virt, dma_addr_t *use_phys)
738{
739 struct device *dev = this->dev;
740
741 if (virt_addr_valid(source)) {
742 dma_addr_t source_phys;
743
744 source_phys = dma_map_single(dev, (void *)source, length,
745 DMA_TO_DEVICE);
746 if (dma_mapping_error(dev, source_phys)) {
747 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800748 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800749 return -ENOMEM;
750 }
751 goto map_failed;
752 }
753 *use_virt = source;
754 *use_phys = source_phys;
755 return 0;
756 }
757map_failed:
758 /*
759 * Copy the content of the source buffer into the alternate
760 * buffer and set up the return values accordingly.
761 */
762 memcpy(alt_virt, source, length);
763
764 *use_virt = alt_virt;
765 *use_phys = alt_phys;
766 return 0;
767}
768
769static void send_page_end(struct gpmi_nand_data *this,
770 const void *source, unsigned length,
771 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
772 const void *used_virt, dma_addr_t used_phys)
773{
774 struct device *dev = this->dev;
775 if (used_virt == source)
776 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
777}
778
779static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
780{
781 struct device *dev = this->dev;
782
783 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
784 dma_free_coherent(dev, this->page_buffer_size,
785 this->page_buffer_virt,
786 this->page_buffer_phys);
787 kfree(this->cmd_buffer);
788 kfree(this->data_buffer_dma);
789
790 this->cmd_buffer = NULL;
791 this->data_buffer_dma = NULL;
792 this->page_buffer_virt = NULL;
793 this->page_buffer_size = 0;
794}
795
796/* Allocate the DMA buffers */
797static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
798{
799 struct bch_geometry *geo = &this->bch_geometry;
800 struct device *dev = this->dev;
Huang Shijie06f216c2013-12-18 23:40:59 +0800801 struct mtd_info *mtd = &this->mtd;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800802
803 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800804 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800805 if (this->cmd_buffer == NULL)
806 goto error_alloc;
807
Huang Shijie06f216c2013-12-18 23:40:59 +0800808 /*
809 * [2] Allocate a read/write data buffer.
810 * The gpmi_alloc_dma_buffer can be called twice.
811 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
812 * is called before the nand_scan_ident; and we allocate a buffer
813 * of the real NAND page size when the gpmi_alloc_dma_buffer is
814 * called after the nand_scan_ident.
815 */
816 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
817 GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800818 if (this->data_buffer_dma == NULL)
819 goto error_alloc;
820
821 /*
822 * [3] Allocate the page buffer.
823 *
824 * Both the payload buffer and the auxiliary buffer must appear on
825 * 32-bit boundaries. We presume the size of the payload buffer is a
826 * power of two and is much larger than four, which guarantees the
827 * auxiliary buffer will appear on a 32-bit boundary.
828 */
829 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
830 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
831 &this->page_buffer_phys, GFP_DMA);
832 if (!this->page_buffer_virt)
833 goto error_alloc;
834
835
836 /* Slice up the page buffer. */
837 this->payload_virt = this->page_buffer_virt;
838 this->payload_phys = this->page_buffer_phys;
839 this->auxiliary_virt = this->payload_virt + geo->payload_size;
840 this->auxiliary_phys = this->payload_phys + geo->payload_size;
841 return 0;
842
843error_alloc:
844 gpmi_free_dma_buffer(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800845 return -ENOMEM;
846}
847
848static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
849{
850 struct nand_chip *chip = mtd->priv;
851 struct gpmi_nand_data *this = chip->priv;
852 int ret;
853
854 /*
855 * Every operation begins with a command byte and a series of zero or
856 * more address bytes. These are distinguished by either the Address
857 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
858 * asserted. When MTD is ready to execute the command, it will deassert
859 * both latch enables.
860 *
861 * Rather than run a separate DMA operation for every single byte, we
862 * queue them up and run a single DMA operation for the entire series
863 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
864 */
865 if ((ctrl & (NAND_ALE | NAND_CLE))) {
866 if (data != NAND_CMD_NONE)
867 this->cmd_buffer[this->command_length++] = data;
868 return;
869 }
870
871 if (!this->command_length)
872 return;
873
874 ret = gpmi_send_command(this);
875 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +0800876 dev_err(this->dev, "Chip: %u, Error %d\n",
877 this->current_chip, ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800878
879 this->command_length = 0;
880}
881
882static int gpmi_dev_ready(struct mtd_info *mtd)
883{
884 struct nand_chip *chip = mtd->priv;
885 struct gpmi_nand_data *this = chip->priv;
886
887 return gpmi_is_ready(this, this->current_chip);
888}
889
890static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
891{
892 struct nand_chip *chip = mtd->priv;
893 struct gpmi_nand_data *this = chip->priv;
894
895 if ((this->current_chip < 0) && (chipnr >= 0))
896 gpmi_begin(this);
897 else if ((this->current_chip >= 0) && (chipnr < 0))
898 gpmi_end(this);
899
900 this->current_chip = chipnr;
901}
902
903static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
904{
905 struct nand_chip *chip = mtd->priv;
906 struct gpmi_nand_data *this = chip->priv;
907
Huang Shijiec2325962013-11-20 10:09:44 +0800908 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800909 this->upper_buf = buf;
910 this->upper_len = len;
911
912 gpmi_read_data(this);
913}
914
915static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
916{
917 struct nand_chip *chip = mtd->priv;
918 struct gpmi_nand_data *this = chip->priv;
919
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 = (uint8_t *)buf;
922 this->upper_len = len;
923
924 gpmi_send_data(this);
925}
926
927static uint8_t gpmi_read_byte(struct mtd_info *mtd)
928{
929 struct nand_chip *chip = mtd->priv;
930 struct gpmi_nand_data *this = chip->priv;
931 uint8_t *buf = this->data_buffer_dma;
932
933 gpmi_read_buf(mtd, buf, 1);
934 return buf[0];
935}
936
937/*
938 * Handles block mark swapping.
939 * It can be called in swapping the block mark, or swapping it back,
940 * because the the operations are the same.
941 */
942static void block_mark_swapping(struct gpmi_nand_data *this,
943 void *payload, void *auxiliary)
944{
945 struct bch_geometry *nfc_geo = &this->bch_geometry;
946 unsigned char *p;
947 unsigned char *a;
948 unsigned int bit;
949 unsigned char mask;
950 unsigned char from_data;
951 unsigned char from_oob;
952
953 if (!this->swap_block_mark)
954 return;
955
956 /*
957 * If control arrives here, we're swapping. Make some convenience
958 * variables.
959 */
960 bit = nfc_geo->block_mark_bit_offset;
961 p = payload + nfc_geo->block_mark_byte_offset;
962 a = auxiliary;
963
964 /*
965 * Get the byte from the data area that overlays the block mark. Since
966 * the ECC engine applies its own view to the bits in the page, the
967 * physical block mark won't (in general) appear on a byte boundary in
968 * the data.
969 */
970 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
971
972 /* Get the byte from the OOB. */
973 from_oob = a[0];
974
975 /* Swap them. */
976 a[0] = from_data;
977
978 mask = (0x1 << bit) - 1;
979 p[0] = (p[0] & mask) | (from_oob << bit);
980
981 mask = ~0 << bit;
982 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
983}
984
985static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700986 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800987{
988 struct gpmi_nand_data *this = chip->priv;
989 struct bch_geometry *nfc_geo = &this->bch_geometry;
990 void *payload_virt;
991 dma_addr_t payload_phys;
992 void *auxiliary_virt;
993 dma_addr_t auxiliary_phys;
994 unsigned int i;
995 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -0600996 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800997 int ret;
998
Huang Shijiec2325962013-11-20 10:09:44 +0800999 dev_dbg(this->dev, "page number is : %d\n", page);
Huang Shijie4a57d672014-01-03 11:01:41 +08001000 ret = read_page_prepare(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001001 this->payload_virt, this->payload_phys,
1002 nfc_geo->payload_size,
1003 &payload_virt, &payload_phys);
1004 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001005 dev_err(this->dev, "Inadequate DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001006 ret = -ENOMEM;
1007 return ret;
1008 }
1009 auxiliary_virt = this->auxiliary_virt;
1010 auxiliary_phys = this->auxiliary_phys;
1011
1012 /* go! */
1013 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
Huang Shijie4a57d672014-01-03 11:01:41 +08001014 read_page_end(this, buf, nfc_geo->payload_size,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001015 this->payload_virt, this->payload_phys,
1016 nfc_geo->payload_size,
1017 payload_virt, payload_phys);
1018 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001019 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001020 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001021 }
1022
1023 /* handle the block mark swapping */
1024 block_mark_swapping(this, payload_virt, auxiliary_virt);
1025
1026 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001027 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001028
1029 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1030 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1031 continue;
1032
1033 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001034 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001035 continue;
1036 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001037 mtd->ecc_stats.corrected += *status;
1038 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001039 }
1040
Brian Norris7725cc82012-05-02 10:15:02 -07001041 if (oob_required) {
1042 /*
1043 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1044 * for details about our policy for delivering the OOB.
1045 *
1046 * We fill the caller's buffer with set bits, and then copy the
1047 * block mark to th caller's buffer. Note that, if block mark
1048 * swapping was necessary, it has already been done, so we can
1049 * rely on the first byte of the auxiliary buffer to contain
1050 * the block mark.
1051 */
1052 memset(chip->oob_poi, ~0, mtd->oobsize);
1053 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001054 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001055
Huang Shijie4a57d672014-01-03 11:01:41 +08001056 read_page_swap_end(this, buf, nfc_geo->payload_size,
Sascha Hauer6023813a2012-06-26 17:26:16 +02001057 this->payload_virt, this->payload_phys,
1058 nfc_geo->payload_size,
1059 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001060
1061 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001062}
1063
Huang Shijieb8e29312014-01-03 11:01:42 +08001064/* Fake a virtual small page for the subpage read */
1065static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1066 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1067{
1068 struct gpmi_nand_data *this = chip->priv;
1069 void __iomem *bch_regs = this->resources.bch_regs;
1070 struct bch_geometry old_geo = this->bch_geometry;
1071 struct bch_geometry *geo = &this->bch_geometry;
1072 int size = chip->ecc.size; /* ECC chunk size */
1073 int meta, n, page_size;
1074 u32 r1_old, r2_old, r1_new, r2_new;
1075 unsigned int max_bitflips;
1076 int first, last, marker_pos;
1077 int ecc_parity_size;
1078 int col = 0;
1079
1080 /* The size of ECC parity */
1081 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1082
1083 /* Align it with the chunk size */
1084 first = offs / size;
1085 last = (offs + len - 1) / size;
1086
1087 /*
1088 * Find the chunk which contains the Block Marker. If this chunk is
1089 * in the range of [first, last], we have to read out the whole page.
1090 * Why? since we had swapped the data at the position of Block Marker
1091 * to the metadata which is bound with the chunk 0.
1092 */
1093 marker_pos = geo->block_mark_byte_offset / size;
1094 if (last >= marker_pos && first <= marker_pos) {
1095 dev_dbg(this->dev, "page:%d, first:%d, last:%d, marker at:%d\n",
1096 page, first, last, marker_pos);
1097 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1098 }
1099
1100 meta = geo->metadata_size;
1101 if (first) {
1102 col = meta + (size + ecc_parity_size) * first;
1103 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1104
1105 meta = 0;
1106 buf = buf + first * size;
1107 }
1108
1109 /* Save the old environment */
1110 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1111 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1112
1113 /* change the BCH registers and bch_geometry{} */
1114 n = last - first + 1;
1115 page_size = meta + (size + ecc_parity_size) * n;
1116
1117 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1118 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1119 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1120 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1121 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1122
1123 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1124 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1125 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1126
1127 geo->ecc_chunk_count = n;
1128 geo->payload_size = n * size;
1129 geo->page_size = page_size;
1130 geo->auxiliary_status_offset = ALIGN(meta, 4);
1131
1132 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1133 page, offs, len, col, first, n, page_size);
1134
1135 /* Read the subpage now */
1136 this->swap_block_mark = false;
1137 max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1138
1139 /* Restore */
1140 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1141 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1142 this->bch_geometry = old_geo;
1143 this->swap_block_mark = true;
1144
1145 return max_bitflips;
1146}
1147
Josh Wufdbad98d2012-06-25 18:07:45 +08001148static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001149 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001150{
1151 struct gpmi_nand_data *this = chip->priv;
1152 struct bch_geometry *nfc_geo = &this->bch_geometry;
1153 const void *payload_virt;
1154 dma_addr_t payload_phys;
1155 const void *auxiliary_virt;
1156 dma_addr_t auxiliary_phys;
1157 int ret;
1158
Huang Shijiec2325962013-11-20 10:09:44 +08001159 dev_dbg(this->dev, "ecc write page.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001160 if (this->swap_block_mark) {
1161 /*
1162 * If control arrives here, we're doing block mark swapping.
1163 * Since we can't modify the caller's buffers, we must copy them
1164 * into our own.
1165 */
1166 memcpy(this->payload_virt, buf, mtd->writesize);
1167 payload_virt = this->payload_virt;
1168 payload_phys = this->payload_phys;
1169
1170 memcpy(this->auxiliary_virt, chip->oob_poi,
1171 nfc_geo->auxiliary_size);
1172 auxiliary_virt = this->auxiliary_virt;
1173 auxiliary_phys = this->auxiliary_phys;
1174
1175 /* Handle block mark swapping. */
1176 block_mark_swapping(this,
1177 (void *) payload_virt, (void *) auxiliary_virt);
1178 } else {
1179 /*
1180 * If control arrives here, we're not doing block mark swapping,
1181 * so we can to try and use the caller's buffers.
1182 */
1183 ret = send_page_prepare(this,
1184 buf, mtd->writesize,
1185 this->payload_virt, this->payload_phys,
1186 nfc_geo->payload_size,
1187 &payload_virt, &payload_phys);
1188 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001189 dev_err(this->dev, "Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001190 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001191 }
1192
1193 ret = send_page_prepare(this,
1194 chip->oob_poi, mtd->oobsize,
1195 this->auxiliary_virt, this->auxiliary_phys,
1196 nfc_geo->auxiliary_size,
1197 &auxiliary_virt, &auxiliary_phys);
1198 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001199 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001200 goto exit_auxiliary;
1201 }
1202 }
1203
1204 /* Ask the NFC. */
1205 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1206 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +08001207 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001208
1209 if (!this->swap_block_mark) {
1210 send_page_end(this, chip->oob_poi, mtd->oobsize,
1211 this->auxiliary_virt, this->auxiliary_phys,
1212 nfc_geo->auxiliary_size,
1213 auxiliary_virt, auxiliary_phys);
1214exit_auxiliary:
1215 send_page_end(this, buf, mtd->writesize,
1216 this->payload_virt, this->payload_phys,
1217 nfc_geo->payload_size,
1218 payload_virt, payload_phys);
1219 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001220
1221 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001222}
1223
1224/*
1225 * There are several places in this driver where we have to handle the OOB and
1226 * block marks. This is the function where things are the most complicated, so
1227 * this is where we try to explain it all. All the other places refer back to
1228 * here.
1229 *
1230 * These are the rules, in order of decreasing importance:
1231 *
1232 * 1) Nothing the caller does can be allowed to imperil the block mark.
1233 *
1234 * 2) In read operations, the first byte of the OOB we return must reflect the
1235 * true state of the block mark, no matter where that block mark appears in
1236 * the physical page.
1237 *
1238 * 3) ECC-based read operations return an OOB full of set bits (since we never
1239 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1240 * return).
1241 *
1242 * 4) "Raw" read operations return a direct view of the physical bytes in the
1243 * page, using the conventional definition of which bytes are data and which
1244 * are OOB. This gives the caller a way to see the actual, physical bytes
1245 * in the page, without the distortions applied by our ECC engine.
1246 *
1247 *
1248 * What we do for this specific read operation depends on two questions:
1249 *
1250 * 1) Are we doing a "raw" read, or an ECC-based read?
1251 *
1252 * 2) Are we using block mark swapping or transcription?
1253 *
1254 * There are four cases, illustrated by the following Karnaugh map:
1255 *
1256 * | Raw | ECC-based |
1257 * -------------+-------------------------+-------------------------+
1258 * | Read the conventional | |
1259 * | OOB at the end of the | |
1260 * Swapping | page and return it. It | |
1261 * | contains exactly what | |
1262 * | we want. | Read the block mark and |
1263 * -------------+-------------------------+ return it in a buffer |
1264 * | Read the conventional | full of set bits. |
1265 * | OOB at the end of the | |
1266 * | page and also the block | |
1267 * Transcribing | mark in the metadata. | |
1268 * | Copy the block mark | |
1269 * | into the first byte of | |
1270 * | the OOB. | |
1271 * -------------+-------------------------+-------------------------+
1272 *
1273 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1274 * giving an accurate view of the actual, physical bytes in the page (we're
1275 * overwriting the block mark). That's OK because it's more important to follow
1276 * rule #2.
1277 *
1278 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1279 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1280 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1281 * ECC-based or raw view of the page is implicit in which function it calls
1282 * (there is a similar pair of ECC-based/raw functions for writing).
1283 *
Brian Norris271b874b2012-05-11 13:30:35 -07001284 * FIXME: The following paragraph is incorrect, now that there exist
1285 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1286 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001287 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1288 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1289 * caller wants an ECC-based or raw view of the page is not propagated down to
1290 * this driver.
1291 */
1292static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001293 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001294{
1295 struct gpmi_nand_data *this = chip->priv;
1296
Huang Shijiec2325962013-11-20 10:09:44 +08001297 dev_dbg(this->dev, "page number is %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001298 /* clear the OOB buffer */
1299 memset(chip->oob_poi, ~0, mtd->oobsize);
1300
1301 /* Read out the conventional OOB. */
1302 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1303 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1304
1305 /*
1306 * Now, we want to make sure the block mark is correct. In the
1307 * Swapping/Raw case, we already have it. Otherwise, we need to
1308 * explicitly read it.
1309 */
1310 if (!this->swap_block_mark) {
1311 /* Read the block mark into the first byte of the OOB buffer. */
1312 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1313 chip->oob_poi[0] = chip->read_byte(mtd);
1314 }
1315
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001316 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001317}
1318
1319static int
1320gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1321{
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001322 struct nand_oobfree *of = mtd->ecclayout->oobfree;
1323 int status = 0;
1324
1325 /* Do we have available oob area? */
1326 if (!of->length)
1327 return -EPERM;
1328
1329 if (!nand_is_slc(chip))
1330 return -EPERM;
1331
1332 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1333 chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1334 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1335
1336 status = chip->waitfunc(mtd, chip);
1337 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001338}
1339
1340static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1341{
1342 struct nand_chip *chip = mtd->priv;
1343 struct gpmi_nand_data *this = chip->priv;
Brian Norris5a0edb22013-07-30 17:52:58 -07001344 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001345 uint8_t *block_mark;
1346 int column, page, status, chipnr;
1347
Brian Norris5a0edb22013-07-30 17:52:58 -07001348 chipnr = (int)(ofs >> chip->chip_shift);
1349 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001350
Brian Norris5a0edb22013-07-30 17:52:58 -07001351 column = this->swap_block_mark ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001352
Brian Norris5a0edb22013-07-30 17:52:58 -07001353 /* Write the block mark. */
1354 block_mark = this->data_buffer_dma;
1355 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001356
Brian Norris5a0edb22013-07-30 17:52:58 -07001357 /* Shift to get page */
1358 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001359
Brian Norris5a0edb22013-07-30 17:52:58 -07001360 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1361 chip->write_buf(mtd, block_mark, 1);
1362 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001363
Brian Norris5a0edb22013-07-30 17:52:58 -07001364 status = chip->waitfunc(mtd, chip);
1365 if (status & NAND_STATUS_FAIL)
1366 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001367
Brian Norris5a0edb22013-07-30 17:52:58 -07001368 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001369
1370 return ret;
1371}
1372
Wolfram Sanga78da282012-03-21 19:29:17 +01001373static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001374{
1375 struct boot_rom_geometry *geometry = &this->rom_geometry;
1376
1377 /*
1378 * Set the boot block stride size.
1379 *
1380 * In principle, we should be reading this from the OTP bits, since
1381 * that's where the ROM is going to get it. In fact, we don't have any
1382 * way to read the OTP bits, so we go with the default and hope for the
1383 * best.
1384 */
1385 geometry->stride_size_in_pages = 64;
1386
1387 /*
1388 * Set the search area stride exponent.
1389 *
1390 * In principle, we should be reading this from the OTP bits, since
1391 * that's where the ROM is going to get it. In fact, we don't have any
1392 * way to read the OTP bits, so we go with the default and hope for the
1393 * best.
1394 */
1395 geometry->search_area_stride_exponent = 2;
1396 return 0;
1397}
1398
1399static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001400static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001401{
1402 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1403 struct device *dev = this->dev;
1404 struct mtd_info *mtd = &this->mtd;
1405 struct nand_chip *chip = &this->nand;
1406 unsigned int search_area_size_in_strides;
1407 unsigned int stride;
1408 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001409 uint8_t *buffer = chip->buffers->databuf;
1410 int saved_chip_number;
1411 int found_an_ncb_fingerprint = false;
1412
1413 /* Compute the number of strides in a search area. */
1414 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1415
1416 saved_chip_number = this->current_chip;
1417 chip->select_chip(mtd, 0);
1418
1419 /*
1420 * Loop through the first search area, looking for the NCB fingerprint.
1421 */
1422 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1423
1424 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001425 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001426 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001427
1428 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1429
1430 /*
1431 * Read the NCB fingerprint. The fingerprint is four bytes long
1432 * and starts in the 12th byte of the page.
1433 */
1434 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1435 chip->read_buf(mtd, buffer, strlen(fingerprint));
1436
1437 /* Look for the fingerprint. */
1438 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1439 found_an_ncb_fingerprint = true;
1440 break;
1441 }
1442
1443 }
1444
1445 chip->select_chip(mtd, saved_chip_number);
1446
1447 if (found_an_ncb_fingerprint)
1448 dev_dbg(dev, "\tFound a fingerprint\n");
1449 else
1450 dev_dbg(dev, "\tNo fingerprint found\n");
1451 return found_an_ncb_fingerprint;
1452}
1453
1454/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001455static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001456{
1457 struct device *dev = this->dev;
1458 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1459 struct mtd_info *mtd = &this->mtd;
1460 struct nand_chip *chip = &this->nand;
1461 unsigned int block_size_in_pages;
1462 unsigned int search_area_size_in_strides;
1463 unsigned int search_area_size_in_pages;
1464 unsigned int search_area_size_in_blocks;
1465 unsigned int block;
1466 unsigned int stride;
1467 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001468 uint8_t *buffer = chip->buffers->databuf;
1469 int saved_chip_number;
1470 int status;
1471
1472 /* Compute the search area geometry. */
1473 block_size_in_pages = mtd->erasesize / mtd->writesize;
1474 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1475 search_area_size_in_pages = search_area_size_in_strides *
1476 rom_geo->stride_size_in_pages;
1477 search_area_size_in_blocks =
1478 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1479 block_size_in_pages;
1480
1481 dev_dbg(dev, "Search Area Geometry :\n");
1482 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1483 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1484 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1485
1486 /* Select chip 0. */
1487 saved_chip_number = this->current_chip;
1488 chip->select_chip(mtd, 0);
1489
1490 /* Loop over blocks in the first search area, erasing them. */
1491 dev_dbg(dev, "Erasing the search area...\n");
1492
1493 for (block = 0; block < search_area_size_in_blocks; block++) {
1494 /* Compute the page address. */
1495 page = block * block_size_in_pages;
1496
1497 /* Erase this block. */
1498 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1499 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1500 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1501
1502 /* Wait for the erase to finish. */
1503 status = chip->waitfunc(mtd, chip);
1504 if (status & NAND_STATUS_FAIL)
1505 dev_err(dev, "[%s] Erase failed.\n", __func__);
1506 }
1507
1508 /* Write the NCB fingerprint into the page buffer. */
1509 memset(buffer, ~0, mtd->writesize);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001510 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1511
1512 /* Loop through the first search area, writing NCB fingerprints. */
1513 dev_dbg(dev, "Writing NCB fingerprints...\n");
1514 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001515 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001516 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001517
1518 /* Write the first page of the current stride. */
1519 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1520 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001521 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001522 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1523
1524 /* Wait for the write to finish. */
1525 status = chip->waitfunc(mtd, chip);
1526 if (status & NAND_STATUS_FAIL)
1527 dev_err(dev, "[%s] Write failed.\n", __func__);
1528 }
1529
1530 /* Deselect chip 0. */
1531 chip->select_chip(mtd, saved_chip_number);
1532 return 0;
1533}
1534
Wolfram Sanga78da282012-03-21 19:29:17 +01001535static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001536{
1537 struct device *dev = this->dev;
1538 struct nand_chip *chip = &this->nand;
1539 struct mtd_info *mtd = &this->mtd;
1540 unsigned int block_count;
1541 unsigned int block;
1542 int chipnr;
1543 int page;
1544 loff_t byte;
1545 uint8_t block_mark;
1546 int ret = 0;
1547
1548 /*
1549 * If control arrives here, we can't use block mark swapping, which
1550 * means we're forced to use transcription. First, scan for the
1551 * transcription stamp. If we find it, then we don't have to do
1552 * anything -- the block marks are already transcribed.
1553 */
1554 if (mx23_check_transcription_stamp(this))
1555 return 0;
1556
1557 /*
1558 * If control arrives here, we couldn't find a transcription stamp, so
1559 * so we presume the block marks are in the conventional location.
1560 */
1561 dev_dbg(dev, "Transcribing bad block marks...\n");
1562
1563 /* Compute the number of blocks in the entire medium. */
1564 block_count = chip->chipsize >> chip->phys_erase_shift;
1565
1566 /*
1567 * Loop over all the blocks in the medium, transcribing block marks as
1568 * we go.
1569 */
1570 for (block = 0; block < block_count; block++) {
1571 /*
1572 * Compute the chip, page and byte addresses for this block's
1573 * conventional mark.
1574 */
1575 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1576 page = block << (chip->phys_erase_shift - chip->page_shift);
1577 byte = block << chip->phys_erase_shift;
1578
1579 /* Send the command to read the conventional block mark. */
1580 chip->select_chip(mtd, chipnr);
1581 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1582 block_mark = chip->read_byte(mtd);
1583 chip->select_chip(mtd, -1);
1584
1585 /*
1586 * Check if the block is marked bad. If so, we need to mark it
1587 * again, but this time the result will be a mark in the
1588 * location where we transcribe block marks.
1589 */
1590 if (block_mark != 0xff) {
1591 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1592 ret = chip->block_markbad(mtd, byte);
1593 if (ret)
1594 dev_err(dev, "Failed to mark block bad with "
1595 "ret %d\n", ret);
1596 }
1597 }
1598
1599 /* Write the stamp that indicates we've transcribed the block marks. */
1600 mx23_write_transcription_stamp(this);
1601 return 0;
1602}
1603
Wolfram Sanga78da282012-03-21 19:29:17 +01001604static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001605{
1606 nand_boot_set_geometry(this);
1607
1608 /* This is ROM arch-specific initilization before the BBT scanning. */
1609 if (GPMI_IS_MX23(this))
1610 return mx23_boot_init(this);
1611 return 0;
1612}
1613
Wolfram Sanga78da282012-03-21 19:29:17 +01001614static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001615{
1616 int ret;
1617
1618 /* Free the temporary DMA memory for reading ID. */
1619 gpmi_free_dma_buffer(this);
1620
1621 /* Set up the NFC geometry which is used by BCH. */
1622 ret = bch_set_geometry(this);
1623 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001624 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001625 return ret;
1626 }
1627
1628 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1629 return gpmi_alloc_dma_buffer(this);
1630}
1631
Huang Shijieccce4172013-11-14 14:25:47 +08001632static void gpmi_nand_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001633{
Huang Shijief720e7c2013-08-16 10:10:08 +08001634 nand_release(&this->mtd);
1635 gpmi_free_dma_buffer(this);
1636}
1637
1638static int gpmi_init_last(struct gpmi_nand_data *this)
1639{
1640 struct mtd_info *mtd = &this->mtd;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001641 struct nand_chip *chip = mtd->priv;
Huang Shijief720e7c2013-08-16 10:10:08 +08001642 struct nand_ecc_ctrl *ecc = &chip->ecc;
1643 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001644 int ret;
1645
Huang Shijied7364a272013-11-14 14:25:45 +08001646 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1647 this->swap_block_mark = !GPMI_IS_MX23(this);
1648
1649 /* Set up the medium geometry */
1650 ret = gpmi_set_geometry(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001651 if (ret)
1652 return ret;
1653
Huang Shijief720e7c2013-08-16 10:10:08 +08001654 /* Init the nand_ecc_ctrl{} */
1655 ecc->read_page = gpmi_ecc_read_page;
1656 ecc->write_page = gpmi_ecc_write_page;
1657 ecc->read_oob = gpmi_ecc_read_oob;
1658 ecc->write_oob = gpmi_ecc_write_oob;
1659 ecc->mode = NAND_ECC_HW;
1660 ecc->size = bch_geo->ecc_chunk_size;
1661 ecc->strength = bch_geo->ecc_strength;
1662 ecc->layout = &gpmi_hw_ecclayout;
1663
Huang Shijie995fbbf2012-09-13 14:57:59 +08001664 /*
Huang Shijieb8e29312014-01-03 11:01:42 +08001665 * We only enable the subpage read when:
1666 * (1) the chip is imx6, and
1667 * (2) the size of the ECC parity is byte aligned.
1668 */
1669 if (GPMI_IS_MX6Q(this) &&
1670 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1671 ecc->read_subpage = gpmi_ecc_read_subpage;
1672 chip->options |= NAND_SUBPAGE_READ;
1673 }
1674
1675 /*
Huang Shijie995fbbf2012-09-13 14:57:59 +08001676 * Can we enable the extra features? such as EDO or Sync mode.
1677 *
1678 * We do not check the return value now. That's means if we fail in
1679 * enable the extra features, we still can run in the normal way.
1680 */
1681 gpmi_extra_init(this);
1682
Huang Shijief720e7c2013-08-16 10:10:08 +08001683 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001684}
1685
Huang Shijieccce4172013-11-14 14:25:47 +08001686static int gpmi_nand_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001687{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001688 struct mtd_info *mtd = &this->mtd;
1689 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001690 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001691 int ret;
1692
1693 /* init current chip */
1694 this->current_chip = -1;
1695
1696 /* init the MTD data structures */
1697 mtd->priv = chip;
1698 mtd->name = "gpmi-nand";
1699 mtd->owner = THIS_MODULE;
1700
1701 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1702 chip->priv = this;
1703 chip->select_chip = gpmi_select_chip;
1704 chip->cmd_ctrl = gpmi_cmd_ctrl;
1705 chip->dev_ready = gpmi_dev_ready;
1706 chip->read_byte = gpmi_read_byte;
1707 chip->read_buf = gpmi_read_buf;
1708 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001709 chip->badblock_pattern = &gpmi_bbt_descr;
1710 chip->block_markbad = gpmi_block_markbad;
1711 chip->options |= NAND_NO_SUBPAGE_WRITE;
Huang Shijiec50c6942012-07-03 16:24:32 +08001712 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1713 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001714
Huang Shijief720e7c2013-08-16 10:10:08 +08001715 /*
1716 * Allocate a temporary DMA buffer for reading ID in the
1717 * nand_scan_ident().
1718 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001719 this->bch_geometry.payload_size = 1024;
1720 this->bch_geometry.auxiliary_size = 128;
1721 ret = gpmi_alloc_dma_buffer(this);
1722 if (ret)
1723 goto err_out;
1724
Huang Shijie80bd33a2013-11-07 17:46:37 +08001725 ret = nand_scan_ident(mtd, GPMI_IS_MX6Q(this) ? 2 : 1, NULL);
Huang Shijief720e7c2013-08-16 10:10:08 +08001726 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001727 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001728
1729 ret = gpmi_init_last(this);
1730 if (ret)
1731 goto err_out;
1732
Huang Shijie885d71e2013-11-12 12:23:08 +08001733 chip->options |= NAND_SKIP_BBTSCAN;
Huang Shijief720e7c2013-08-16 10:10:08 +08001734 ret = nand_scan_tail(mtd);
1735 if (ret)
1736 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001737
Huang Shijie885d71e2013-11-12 12:23:08 +08001738 ret = nand_boot_init(this);
1739 if (ret)
1740 goto err_out;
1741 chip->scan_bbt(mtd);
1742
Huang Shijiee10db1f2012-05-04 21:42:05 -04001743 ppdata.of_node = this->pdev->dev.of_node;
1744 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001745 if (ret)
1746 goto err_out;
1747 return 0;
1748
1749err_out:
Huang Shijieccce4172013-11-14 14:25:47 +08001750 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001751 return ret;
1752}
1753
Huang Shijiee10db1f2012-05-04 21:42:05 -04001754static const struct of_device_id gpmi_nand_id_table[] = {
1755 {
1756 .compatible = "fsl,imx23-gpmi-nand",
Huang Shijie6189ccc2014-03-21 18:19:39 +08001757 .data = (void *)&gpmi_devdata_imx23,
Huang Shijiee10db1f2012-05-04 21:42:05 -04001758 }, {
1759 .compatible = "fsl,imx28-gpmi-nand",
Huang Shijie6189ccc2014-03-21 18:19:39 +08001760 .data = (void *)&gpmi_devdata_imx28,
Huang Shijie9013bb42012-05-04 21:42:06 -04001761 }, {
1762 .compatible = "fsl,imx6q-gpmi-nand",
Huang Shijie6189ccc2014-03-21 18:19:39 +08001763 .data = (void *)&gpmi_devdata_imx6q,
Huang Shijiee10db1f2012-05-04 21:42:05 -04001764 }, {}
1765};
1766MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1767
Bill Pemberton06f25512012-11-19 13:23:07 -05001768static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001769{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001770 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001771 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001772 int ret;
1773
Huang Shijie6189ccc2014-03-21 18:19:39 +08001774 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1775 if (!this)
1776 return -ENOMEM;
1777
Huang Shijiee10db1f2012-05-04 21:42:05 -04001778 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1779 if (of_id) {
Huang Shijie6189ccc2014-03-21 18:19:39 +08001780 this->devdata = of_id->data;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001781 } else {
Huang Shijieda40c162013-11-20 10:09:43 +08001782 dev_err(&pdev->dev, "Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02001783 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001784 }
1785
Huang Shijie10a2bca2011-09-08 10:47:09 +08001786 platform_set_drvdata(pdev, this);
1787 this->pdev = pdev;
1788 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001789
1790 ret = acquire_resources(this);
1791 if (ret)
1792 goto exit_acquire_resources;
1793
1794 ret = init_hardware(this);
1795 if (ret)
1796 goto exit_nfc_init;
1797
Huang Shijieccce4172013-11-14 14:25:47 +08001798 ret = gpmi_nand_init(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001799 if (ret)
1800 goto exit_nfc_init;
1801
Fabio Estevam490e2802012-09-05 11:35:24 -03001802 dev_info(this->dev, "driver registered.\n");
1803
Huang Shijie10a2bca2011-09-08 10:47:09 +08001804 return 0;
1805
1806exit_nfc_init:
1807 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001808exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03001809 dev_err(this->dev, "driver registration failed: %d\n", ret);
1810
Huang Shijie10a2bca2011-09-08 10:47:09 +08001811 return ret;
1812}
1813
Bill Pemberton810b7e02012-11-19 13:26:04 -05001814static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001815{
1816 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1817
Huang Shijieccce4172013-11-14 14:25:47 +08001818 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001819 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001820 return 0;
1821}
1822
Huang Shijie10a2bca2011-09-08 10:47:09 +08001823static struct platform_driver gpmi_nand_driver = {
1824 .driver = {
1825 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001826 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001827 },
1828 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001829 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001830};
Fabio Estevam490e2802012-09-05 11:35:24 -03001831module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001832
1833MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1834MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1835MODULE_LICENSE("GPL");