blob: c46be6c8b2c48d0c44d66feb65f04077fe6650c6 [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/gpmi-nand.h>
26#include <linux/mtd/partitions.h>
Shawn Guo39febc02012-05-06 22:57:41 +080027#include <linux/pinctrl/consumer.h>
Huang Shijiee10db1f2012-05-04 21:42:05 -040028#include <linux/of.h>
29#include <linux/of_device.h>
Huang Shijiec50c6942012-07-03 16:24:32 +080030#include <linux/of_mtd.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080031#include "gpmi-nand.h"
32
33/* add our owner bbt descriptor */
34static uint8_t scan_ff_pattern[] = { 0xff };
35static struct nand_bbt_descr gpmi_bbt_descr = {
36 .options = 0,
37 .offs = 0,
38 .len = 1,
39 .pattern = scan_ff_pattern
40};
41
42/* We will use all the (page + OOB). */
43static struct nand_ecclayout gpmi_hw_ecclayout = {
44 .eccbytes = 0,
45 .eccpos = { 0, },
46 .oobfree = { {.offset = 0, .length = 0} }
47};
48
49static irqreturn_t bch_irq(int irq, void *cookie)
50{
51 struct gpmi_nand_data *this = cookie;
52
53 gpmi_clear_bch(this);
54 complete(&this->bch_done);
55 return IRQ_HANDLED;
56}
57
58/*
59 * Calculate the ECC strength by hand:
60 * E : The ECC strength.
61 * G : the length of Galois Field.
62 * N : The chunk count of per page.
63 * O : the oobsize of the NAND chip.
64 * M : the metasize of per page.
65 *
66 * The formula is :
67 * E * G * N
68 * ------------ <= (O - M)
69 * 8
70 *
71 * So, we get E by:
72 * (O - M) * 8
73 * E <= -------------
74 * G * N
75 */
76static inline int get_ecc_strength(struct gpmi_nand_data *this)
77{
78 struct bch_geometry *geo = &this->bch_geometry;
79 struct mtd_info *mtd = &this->mtd;
80 int ecc_strength;
81
82 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
83 / (geo->gf_len * geo->ecc_chunk_count);
84
85 /* We need the minor even number. */
86 return round_down(ecc_strength, 2);
87}
88
89int common_nfc_set_geometry(struct gpmi_nand_data *this)
90{
91 struct bch_geometry *geo = &this->bch_geometry;
92 struct mtd_info *mtd = &this->mtd;
93 unsigned int metadata_size;
94 unsigned int status_size;
95 unsigned int block_mark_bit_offset;
96
97 /*
98 * The size of the metadata can be changed, though we set it to 10
99 * bytes now. But it can't be too large, because we have to save
100 * enough space for BCH.
101 */
102 geo->metadata_size = 10;
103
104 /* The default for the length of Galois Field. */
105 geo->gf_len = 13;
106
107 /* The default for chunk size. There is no oobsize greater then 512. */
108 geo->ecc_chunk_size = 512;
109 while (geo->ecc_chunk_size < mtd->oobsize)
110 geo->ecc_chunk_size *= 2; /* keep C >= O */
111
112 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
113
114 /* We use the same ECC strength for all chunks. */
115 geo->ecc_strength = get_ecc_strength(this);
116 if (!geo->ecc_strength) {
117 pr_err("We get a wrong ECC strength.\n");
118 return -EINVAL;
119 }
120
121 geo->page_size = mtd->writesize + mtd->oobsize;
122 geo->payload_size = mtd->writesize;
123
124 /*
125 * The auxiliary buffer contains the metadata and the ECC status. The
126 * metadata is padded to the nearest 32-bit boundary. The ECC status
127 * contains one byte for every ECC chunk, and is also padded to the
128 * nearest 32-bit boundary.
129 */
130 metadata_size = ALIGN(geo->metadata_size, 4);
131 status_size = ALIGN(geo->ecc_chunk_count, 4);
132
133 geo->auxiliary_size = metadata_size + status_size;
134 geo->auxiliary_status_offset = metadata_size;
135
136 if (!this->swap_block_mark)
137 return 0;
138
139 /*
140 * We need to compute the byte and bit offsets of
141 * the physical block mark within the ECC-based view of the page.
142 *
143 * NAND chip with 2K page shows below:
144 * (Block Mark)
145 * | |
146 * | D |
147 * |<---->|
148 * V V
149 * +---+----------+-+----------+-+----------+-+----------+-+
150 * | M | data |E| data |E| data |E| data |E|
151 * +---+----------+-+----------+-+----------+-+----------+-+
152 *
153 * The position of block mark moves forward in the ECC-based view
154 * of page, and the delta is:
155 *
156 * E * G * (N - 1)
157 * D = (---------------- + M)
158 * 8
159 *
160 * With the formula to compute the ECC strength, and the condition
161 * : C >= O (C is the ecc chunk size)
162 *
163 * It's easy to deduce to the following result:
164 *
165 * E * G (O - M) C - M C - M
166 * ----------- <= ------- <= -------- < ---------
167 * 8 N N (N - 1)
168 *
169 * So, we get:
170 *
171 * E * G * (N - 1)
172 * D = (---------------- + M) < C
173 * 8
174 *
175 * The above inequality means the position of block mark
176 * within the ECC-based view of the page is still in the data chunk,
177 * and it's NOT in the ECC bits of the chunk.
178 *
179 * Use the following to compute the bit position of the
180 * physical block mark within the ECC-based view of the page:
181 * (page_size - D) * 8
182 *
183 * --Huang Shijie
184 */
185 block_mark_bit_offset = mtd->writesize * 8 -
186 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
187 + geo->metadata_size * 8);
188
189 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
190 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
191 return 0;
192}
193
194struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
195{
196 int chipnr = this->current_chip;
197
198 return this->dma_chans[chipnr];
199}
200
201/* Can we use the upper's buffer directly for DMA? */
202void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
203{
204 struct scatterlist *sgl = &this->data_sgl;
205 int ret;
206
207 this->direct_dma_map_ok = true;
208
209 /* first try to map the upper buffer directly */
210 sg_init_one(sgl, this->upper_buf, this->upper_len);
211 ret = dma_map_sg(this->dev, sgl, 1, dr);
212 if (ret == 0) {
213 /* We have to use our own DMA buffer. */
214 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
215
216 if (dr == DMA_TO_DEVICE)
217 memcpy(this->data_buffer_dma, this->upper_buf,
218 this->upper_len);
219
220 ret = dma_map_sg(this->dev, sgl, 1, dr);
221 if (ret == 0)
222 pr_err("map failed.\n");
223
224 this->direct_dma_map_ok = false;
225 }
226}
227
228/* This will be called after the DMA operation is finished. */
229static void dma_irq_callback(void *param)
230{
231 struct gpmi_nand_data *this = param;
232 struct completion *dma_c = &this->dma_done;
233
234 complete(dma_c);
235
236 switch (this->dma_type) {
237 case DMA_FOR_COMMAND:
238 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
239 break;
240
241 case DMA_FOR_READ_DATA:
242 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
243 if (this->direct_dma_map_ok == false)
244 memcpy(this->upper_buf, this->data_buffer_dma,
245 this->upper_len);
246 break;
247
248 case DMA_FOR_WRITE_DATA:
249 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
250 break;
251
252 case DMA_FOR_READ_ECC_PAGE:
253 case DMA_FOR_WRITE_ECC_PAGE:
254 /* We have to wait the BCH interrupt to finish. */
255 break;
256
257 default:
258 pr_err("in wrong DMA operation.\n");
259 }
260}
261
262int start_dma_without_bch_irq(struct gpmi_nand_data *this,
263 struct dma_async_tx_descriptor *desc)
264{
265 struct completion *dma_c = &this->dma_done;
266 int err;
267
268 init_completion(dma_c);
269
270 desc->callback = dma_irq_callback;
271 desc->callback_param = this;
272 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800273 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800274
275 /* Wait for the interrupt from the DMA block. */
276 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
277 if (!err) {
278 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
279 gpmi_dump_info(this);
280 return -ETIMEDOUT;
281 }
282 return 0;
283}
284
285/*
286 * This function is used in BCH reading or BCH writing pages.
287 * It will wait for the BCH interrupt as long as ONE second.
288 * Actually, we must wait for two interrupts :
289 * [1] firstly the DMA interrupt and
290 * [2] secondly the BCH interrupt.
291 */
292int start_dma_with_bch_irq(struct gpmi_nand_data *this,
293 struct dma_async_tx_descriptor *desc)
294{
295 struct completion *bch_c = &this->bch_done;
296 int err;
297
298 /* Prepare to receive an interrupt from the BCH block. */
299 init_completion(bch_c);
300
301 /* start the DMA */
302 start_dma_without_bch_irq(this, desc);
303
304 /* Wait for the interrupt from the BCH block. */
305 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
306 if (!err) {
307 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
308 gpmi_dump_info(this);
309 return -ETIMEDOUT;
310 }
311 return 0;
312}
313
314static int __devinit
315acquire_register_block(struct gpmi_nand_data *this, const char *res_name)
316{
317 struct platform_device *pdev = this->pdev;
318 struct resources *res = &this->resources;
319 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800320 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800321
322 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
323 if (!r) {
324 pr_err("Can't get resource for %s\n", res_name);
325 return -ENXIO;
326 }
327
328 p = ioremap(r->start, resource_size(r));
329 if (!p) {
330 pr_err("Can't remap %s\n", res_name);
331 return -ENOMEM;
332 }
333
334 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
335 res->gpmi_regs = p;
336 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
337 res->bch_regs = p;
338 else
339 pr_err("unknown resource name : %s\n", res_name);
340
341 return 0;
342}
343
344static void release_register_block(struct gpmi_nand_data *this)
345{
346 struct resources *res = &this->resources;
347 if (res->gpmi_regs)
348 iounmap(res->gpmi_regs);
349 if (res->bch_regs)
350 iounmap(res->bch_regs);
351 res->gpmi_regs = NULL;
352 res->bch_regs = NULL;
353}
354
355static int __devinit
356acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
357{
358 struct platform_device *pdev = this->pdev;
359 struct resources *res = &this->resources;
360 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
361 struct resource *r;
362 int err;
363
364 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
365 if (!r) {
366 pr_err("Can't get resource for %s\n", res_name);
367 return -ENXIO;
368 }
369
370 err = request_irq(r->start, irq_h, 0, res_name, this);
371 if (err) {
372 pr_err("Can't own %s\n", res_name);
373 return err;
374 }
375
376 res->bch_low_interrupt = r->start;
377 res->bch_high_interrupt = r->end;
378 return 0;
379}
380
381static void release_bch_irq(struct gpmi_nand_data *this)
382{
383 struct resources *res = &this->resources;
384 int i = res->bch_low_interrupt;
385
386 for (; i <= res->bch_high_interrupt; i++)
387 free_irq(i, this);
388}
389
390static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
391{
392 struct gpmi_nand_data *this = param;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400393 int dma_channel = (int)this->private;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800394
395 if (!mxs_dma_is_apbh(chan))
396 return false;
397 /*
398 * only catch the GPMI dma channels :
399 * for mx23 : MX23_DMA_GPMI0 ~ MX23_DMA_GPMI3
400 * (These four channels share the same IRQ!)
401 *
402 * for mx28 : MX28_DMA_GPMI0 ~ MX28_DMA_GPMI7
403 * (These eight channels share the same IRQ!)
404 */
Huang Shijiee10db1f2012-05-04 21:42:05 -0400405 if (dma_channel == chan->chan_id) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800406 chan->private = &this->dma_data;
407 return true;
408 }
409 return false;
410}
411
412static void release_dma_channels(struct gpmi_nand_data *this)
413{
414 unsigned int i;
415 for (i = 0; i < DMA_CHANS; i++)
416 if (this->dma_chans[i]) {
417 dma_release_channel(this->dma_chans[i]);
418 this->dma_chans[i] = NULL;
419 }
420}
421
422static int __devinit acquire_dma_channels(struct gpmi_nand_data *this)
423{
424 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400425 struct resource *r_dma;
426 struct device_node *dn;
Huang Shijie513d57e2012-07-17 14:14:02 +0800427 u32 dma_channel;
428 int ret;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400429 struct dma_chan *dma_chan;
430 dma_cap_mask_t mask;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800431
Huang Shijiee10db1f2012-05-04 21:42:05 -0400432 /* dma channel, we only use the first one. */
433 dn = pdev->dev.of_node;
434 ret = of_property_read_u32(dn, "fsl,gpmi-dma-channel", &dma_channel);
435 if (ret) {
436 pr_err("unable to get DMA channel from dt.\n");
437 goto acquire_err;
438 }
439 this->private = (void *)dma_channel;
440
441 /* gpmi dma interrupt */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800442 r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
443 GPMI_NAND_DMA_INTERRUPT_RES_NAME);
Huang Shijiee10db1f2012-05-04 21:42:05 -0400444 if (!r_dma) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800445 pr_err("Can't get resource for DMA\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400446 goto acquire_err;
447 }
448 this->dma_data.chan_irq = r_dma->start;
449
450 /* request dma channel */
451 dma_cap_zero(mask);
452 dma_cap_set(DMA_SLAVE, mask);
453
454 dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
455 if (!dma_chan) {
456 pr_err("dma_request_channel failed.\n");
457 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800458 }
459
Huang Shijiee10db1f2012-05-04 21:42:05 -0400460 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800461 return 0;
462
463acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800464 release_dma_channels(this);
465 return -EINVAL;
466}
467
Huang Shijieff506172012-07-02 21:39:32 -0400468static void gpmi_put_clks(struct gpmi_nand_data *this)
469{
470 struct resources *r = &this->resources;
471 struct clk *clk;
472 int i;
473
474 for (i = 0; i < GPMI_CLK_MAX; i++) {
475 clk = r->clock[i];
476 if (clk) {
477 clk_put(clk);
478 r->clock[i] = NULL;
479 }
480 }
481}
482
483static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
484 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
485};
486
487static int __devinit gpmi_get_clks(struct gpmi_nand_data *this)
488{
489 struct resources *r = &this->resources;
490 char **extra_clks = NULL;
491 struct clk *clk;
492 int i;
493
494 /* The main clock is stored in the first. */
495 r->clock[0] = clk_get(this->dev, "gpmi_io");
496 if (IS_ERR(r->clock[0]))
497 goto err_clock;
498
499 /* Get extra clocks */
500 if (GPMI_IS_MX6Q(this))
501 extra_clks = extra_clks_for_mx6q;
502 if (!extra_clks)
503 return 0;
504
505 for (i = 1; i < GPMI_CLK_MAX; i++) {
506 if (extra_clks[i - 1] == NULL)
507 break;
508
509 clk = clk_get(this->dev, extra_clks[i - 1]);
510 if (IS_ERR(clk))
511 goto err_clock;
512
513 r->clock[i] = clk;
514 }
515
516 if (GPMI_IS_MX6Q(this)) {
517 /*
518 * Set the default values for the clocks in mx6q:
519 * The main clock(enfc) : 22MHz
520 * The others : 44.5MHz
521 *
522 * These are just the default values. If you want to use
523 * the ONFI nand which is in the Synchronous Mode, you should
524 * change the clocks's frequencies as you need.
525 */
526 clk_set_rate(r->clock[0], 22000000);
527 for (i = 1; i < GPMI_CLK_MAX && r->clock[i]; i++)
528 clk_set_rate(r->clock[i], 44500000);
529 }
530 return 0;
531
532err_clock:
533 dev_dbg(this->dev, "failed in finding the clocks.\n");
534 gpmi_put_clks(this);
535 return -ENOMEM;
536}
537
Huang Shijie10a2bca2011-09-08 10:47:09 +0800538static int __devinit acquire_resources(struct gpmi_nand_data *this)
539{
Shawn Guo39febc02012-05-06 22:57:41 +0800540 struct pinctrl *pinctrl;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800541 int ret;
542
543 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
544 if (ret)
545 goto exit_regs;
546
547 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
548 if (ret)
549 goto exit_regs;
550
551 ret = acquire_bch_irq(this, bch_irq);
552 if (ret)
553 goto exit_regs;
554
555 ret = acquire_dma_channels(this);
556 if (ret)
557 goto exit_dma_channels;
558
Shawn Guo3e48b1b2012-05-19 21:06:13 +0800559 pinctrl = devm_pinctrl_get_select_default(&this->pdev->dev);
Shawn Guo39febc02012-05-06 22:57:41 +0800560 if (IS_ERR(pinctrl)) {
561 ret = PTR_ERR(pinctrl);
562 goto exit_pin;
563 }
564
Huang Shijieff506172012-07-02 21:39:32 -0400565 ret = gpmi_get_clks(this);
566 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800567 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800568 return 0;
569
570exit_clock:
Shawn Guo39febc02012-05-06 22:57:41 +0800571exit_pin:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800572 release_dma_channels(this);
573exit_dma_channels:
574 release_bch_irq(this);
575exit_regs:
576 release_register_block(this);
577 return ret;
578}
579
580static void release_resources(struct gpmi_nand_data *this)
581{
Huang Shijieff506172012-07-02 21:39:32 -0400582 gpmi_put_clks(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800583 release_register_block(this);
584 release_bch_irq(this);
585 release_dma_channels(this);
586}
587
588static int __devinit init_hardware(struct gpmi_nand_data *this)
589{
590 int ret;
591
592 /*
593 * This structure contains the "safe" GPMI timing that should succeed
594 * with any NAND Flash device
595 * (although, with less-than-optimal performance).
596 */
597 struct nand_timing safe_timing = {
598 .data_setup_in_ns = 80,
599 .data_hold_in_ns = 60,
600 .address_setup_in_ns = 25,
601 .gpmi_sample_delay_in_ns = 6,
602 .tREA_in_ns = -1,
603 .tRLOH_in_ns = -1,
604 .tRHOH_in_ns = -1,
605 };
606
607 /* Initialize the hardwares. */
608 ret = gpmi_init(this);
609 if (ret)
610 return ret;
611
612 this->timing = safe_timing;
613 return 0;
614}
615
616static int read_page_prepare(struct gpmi_nand_data *this,
617 void *destination, unsigned length,
618 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
619 void **use_virt, dma_addr_t *use_phys)
620{
621 struct device *dev = this->dev;
622
623 if (virt_addr_valid(destination)) {
624 dma_addr_t dest_phys;
625
626 dest_phys = dma_map_single(dev, destination,
627 length, DMA_FROM_DEVICE);
628 if (dma_mapping_error(dev, dest_phys)) {
629 if (alt_size < length) {
630 pr_err("Alternate buffer is too small\n");
631 return -ENOMEM;
632 }
633 goto map_failed;
634 }
635 *use_virt = destination;
636 *use_phys = dest_phys;
637 this->direct_dma_map_ok = true;
638 return 0;
639 }
640
641map_failed:
642 *use_virt = alt_virt;
643 *use_phys = alt_phys;
644 this->direct_dma_map_ok = false;
645 return 0;
646}
647
648static inline void read_page_end(struct gpmi_nand_data *this,
649 void *destination, unsigned length,
650 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
651 void *used_virt, dma_addr_t used_phys)
652{
653 if (this->direct_dma_map_ok)
654 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
655}
656
657static inline void read_page_swap_end(struct gpmi_nand_data *this,
658 void *destination, unsigned length,
659 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
660 void *used_virt, dma_addr_t used_phys)
661{
662 if (!this->direct_dma_map_ok)
663 memcpy(destination, alt_virt, length);
664}
665
666static int send_page_prepare(struct gpmi_nand_data *this,
667 const void *source, unsigned length,
668 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
669 const void **use_virt, dma_addr_t *use_phys)
670{
671 struct device *dev = this->dev;
672
673 if (virt_addr_valid(source)) {
674 dma_addr_t source_phys;
675
676 source_phys = dma_map_single(dev, (void *)source, length,
677 DMA_TO_DEVICE);
678 if (dma_mapping_error(dev, source_phys)) {
679 if (alt_size < length) {
680 pr_err("Alternate buffer is too small\n");
681 return -ENOMEM;
682 }
683 goto map_failed;
684 }
685 *use_virt = source;
686 *use_phys = source_phys;
687 return 0;
688 }
689map_failed:
690 /*
691 * Copy the content of the source buffer into the alternate
692 * buffer and set up the return values accordingly.
693 */
694 memcpy(alt_virt, source, length);
695
696 *use_virt = alt_virt;
697 *use_phys = alt_phys;
698 return 0;
699}
700
701static void send_page_end(struct gpmi_nand_data *this,
702 const void *source, unsigned length,
703 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
704 const void *used_virt, dma_addr_t used_phys)
705{
706 struct device *dev = this->dev;
707 if (used_virt == source)
708 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
709}
710
711static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
712{
713 struct device *dev = this->dev;
714
715 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
716 dma_free_coherent(dev, this->page_buffer_size,
717 this->page_buffer_virt,
718 this->page_buffer_phys);
719 kfree(this->cmd_buffer);
720 kfree(this->data_buffer_dma);
721
722 this->cmd_buffer = NULL;
723 this->data_buffer_dma = NULL;
724 this->page_buffer_virt = NULL;
725 this->page_buffer_size = 0;
726}
727
728/* Allocate the DMA buffers */
729static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
730{
731 struct bch_geometry *geo = &this->bch_geometry;
732 struct device *dev = this->dev;
733
734 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800735 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800736 if (this->cmd_buffer == NULL)
737 goto error_alloc;
738
739 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800740 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800741 if (this->data_buffer_dma == NULL)
742 goto error_alloc;
743
744 /*
745 * [3] Allocate the page buffer.
746 *
747 * Both the payload buffer and the auxiliary buffer must appear on
748 * 32-bit boundaries. We presume the size of the payload buffer is a
749 * power of two and is much larger than four, which guarantees the
750 * auxiliary buffer will appear on a 32-bit boundary.
751 */
752 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
753 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
754 &this->page_buffer_phys, GFP_DMA);
755 if (!this->page_buffer_virt)
756 goto error_alloc;
757
758
759 /* Slice up the page buffer. */
760 this->payload_virt = this->page_buffer_virt;
761 this->payload_phys = this->page_buffer_phys;
762 this->auxiliary_virt = this->payload_virt + geo->payload_size;
763 this->auxiliary_phys = this->payload_phys + geo->payload_size;
764 return 0;
765
766error_alloc:
767 gpmi_free_dma_buffer(this);
768 pr_err("allocate DMA buffer ret!!\n");
769 return -ENOMEM;
770}
771
772static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
773{
774 struct nand_chip *chip = mtd->priv;
775 struct gpmi_nand_data *this = chip->priv;
776 int ret;
777
778 /*
779 * Every operation begins with a command byte and a series of zero or
780 * more address bytes. These are distinguished by either the Address
781 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
782 * asserted. When MTD is ready to execute the command, it will deassert
783 * both latch enables.
784 *
785 * Rather than run a separate DMA operation for every single byte, we
786 * queue them up and run a single DMA operation for the entire series
787 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
788 */
789 if ((ctrl & (NAND_ALE | NAND_CLE))) {
790 if (data != NAND_CMD_NONE)
791 this->cmd_buffer[this->command_length++] = data;
792 return;
793 }
794
795 if (!this->command_length)
796 return;
797
798 ret = gpmi_send_command(this);
799 if (ret)
800 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
801
802 this->command_length = 0;
803}
804
805static int gpmi_dev_ready(struct mtd_info *mtd)
806{
807 struct nand_chip *chip = mtd->priv;
808 struct gpmi_nand_data *this = chip->priv;
809
810 return gpmi_is_ready(this, this->current_chip);
811}
812
813static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
814{
815 struct nand_chip *chip = mtd->priv;
816 struct gpmi_nand_data *this = chip->priv;
817
818 if ((this->current_chip < 0) && (chipnr >= 0))
819 gpmi_begin(this);
820 else if ((this->current_chip >= 0) && (chipnr < 0))
821 gpmi_end(this);
822
823 this->current_chip = chipnr;
824}
825
826static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
827{
828 struct nand_chip *chip = mtd->priv;
829 struct gpmi_nand_data *this = chip->priv;
830
831 pr_debug("len is %d\n", len);
832 this->upper_buf = buf;
833 this->upper_len = len;
834
835 gpmi_read_data(this);
836}
837
838static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
839{
840 struct nand_chip *chip = mtd->priv;
841 struct gpmi_nand_data *this = chip->priv;
842
843 pr_debug("len is %d\n", len);
844 this->upper_buf = (uint8_t *)buf;
845 this->upper_len = len;
846
847 gpmi_send_data(this);
848}
849
850static uint8_t gpmi_read_byte(struct mtd_info *mtd)
851{
852 struct nand_chip *chip = mtd->priv;
853 struct gpmi_nand_data *this = chip->priv;
854 uint8_t *buf = this->data_buffer_dma;
855
856 gpmi_read_buf(mtd, buf, 1);
857 return buf[0];
858}
859
860/*
861 * Handles block mark swapping.
862 * It can be called in swapping the block mark, or swapping it back,
863 * because the the operations are the same.
864 */
865static void block_mark_swapping(struct gpmi_nand_data *this,
866 void *payload, void *auxiliary)
867{
868 struct bch_geometry *nfc_geo = &this->bch_geometry;
869 unsigned char *p;
870 unsigned char *a;
871 unsigned int bit;
872 unsigned char mask;
873 unsigned char from_data;
874 unsigned char from_oob;
875
876 if (!this->swap_block_mark)
877 return;
878
879 /*
880 * If control arrives here, we're swapping. Make some convenience
881 * variables.
882 */
883 bit = nfc_geo->block_mark_bit_offset;
884 p = payload + nfc_geo->block_mark_byte_offset;
885 a = auxiliary;
886
887 /*
888 * Get the byte from the data area that overlays the block mark. Since
889 * the ECC engine applies its own view to the bits in the page, the
890 * physical block mark won't (in general) appear on a byte boundary in
891 * the data.
892 */
893 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
894
895 /* Get the byte from the OOB. */
896 from_oob = a[0];
897
898 /* Swap them. */
899 a[0] = from_data;
900
901 mask = (0x1 << bit) - 1;
902 p[0] = (p[0] & mask) | (from_oob << bit);
903
904 mask = ~0 << bit;
905 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
906}
907
908static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700909 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800910{
911 struct gpmi_nand_data *this = chip->priv;
912 struct bch_geometry *nfc_geo = &this->bch_geometry;
913 void *payload_virt;
914 dma_addr_t payload_phys;
915 void *auxiliary_virt;
916 dma_addr_t auxiliary_phys;
917 unsigned int i;
918 unsigned char *status;
919 unsigned int failed;
920 unsigned int corrected;
921 int ret;
922
923 pr_debug("page number is : %d\n", page);
924 ret = read_page_prepare(this, buf, mtd->writesize,
925 this->payload_virt, this->payload_phys,
926 nfc_geo->payload_size,
927 &payload_virt, &payload_phys);
928 if (ret) {
929 pr_err("Inadequate DMA buffer\n");
930 ret = -ENOMEM;
931 return ret;
932 }
933 auxiliary_virt = this->auxiliary_virt;
934 auxiliary_phys = this->auxiliary_phys;
935
936 /* go! */
937 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
938 read_page_end(this, buf, mtd->writesize,
939 this->payload_virt, this->payload_phys,
940 nfc_geo->payload_size,
941 payload_virt, payload_phys);
942 if (ret) {
943 pr_err("Error in ECC-based read: %d\n", ret);
944 goto exit_nfc;
945 }
946
947 /* handle the block mark swapping */
948 block_mark_swapping(this, payload_virt, auxiliary_virt);
949
950 /* Loop over status bytes, accumulating ECC status. */
951 failed = 0;
952 corrected = 0;
953 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
954
955 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
956 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
957 continue;
958
959 if (*status == STATUS_UNCORRECTABLE) {
960 failed++;
961 continue;
962 }
963 corrected += *status;
964 }
965
966 /*
967 * Propagate ECC status to the owning MTD only when failed or
968 * corrected times nearly reaches our ECC correction threshold.
969 */
970 if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
971 mtd->ecc_stats.failed += failed;
972 mtd->ecc_stats.corrected += corrected;
973 }
974
Brian Norris7725cc82012-05-02 10:15:02 -0700975 if (oob_required) {
976 /*
977 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
978 * for details about our policy for delivering the OOB.
979 *
980 * We fill the caller's buffer with set bits, and then copy the
981 * block mark to th caller's buffer. Note that, if block mark
982 * swapping was necessary, it has already been done, so we can
983 * rely on the first byte of the auxiliary buffer to contain
984 * the block mark.
985 */
986 memset(chip->oob_poi, ~0, mtd->oobsize);
987 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -0700988 }
Sascha Hauer60238132012-06-26 17:26:16 +0200989
990 read_page_swap_end(this, buf, mtd->writesize,
991 this->payload_virt, this->payload_phys,
992 nfc_geo->payload_size,
993 payload_virt, payload_phys);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800994exit_nfc:
995 return ret;
996}
997
Josh Wufdbad98d2012-06-25 18:07:45 +0800998static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700999 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001000{
1001 struct gpmi_nand_data *this = chip->priv;
1002 struct bch_geometry *nfc_geo = &this->bch_geometry;
1003 const void *payload_virt;
1004 dma_addr_t payload_phys;
1005 const void *auxiliary_virt;
1006 dma_addr_t auxiliary_phys;
1007 int ret;
1008
1009 pr_debug("ecc write page.\n");
1010 if (this->swap_block_mark) {
1011 /*
1012 * If control arrives here, we're doing block mark swapping.
1013 * Since we can't modify the caller's buffers, we must copy them
1014 * into our own.
1015 */
1016 memcpy(this->payload_virt, buf, mtd->writesize);
1017 payload_virt = this->payload_virt;
1018 payload_phys = this->payload_phys;
1019
1020 memcpy(this->auxiliary_virt, chip->oob_poi,
1021 nfc_geo->auxiliary_size);
1022 auxiliary_virt = this->auxiliary_virt;
1023 auxiliary_phys = this->auxiliary_phys;
1024
1025 /* Handle block mark swapping. */
1026 block_mark_swapping(this,
1027 (void *) payload_virt, (void *) auxiliary_virt);
1028 } else {
1029 /*
1030 * If control arrives here, we're not doing block mark swapping,
1031 * so we can to try and use the caller's buffers.
1032 */
1033 ret = send_page_prepare(this,
1034 buf, mtd->writesize,
1035 this->payload_virt, this->payload_phys,
1036 nfc_geo->payload_size,
1037 &payload_virt, &payload_phys);
1038 if (ret) {
1039 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001040 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001041 }
1042
1043 ret = send_page_prepare(this,
1044 chip->oob_poi, mtd->oobsize,
1045 this->auxiliary_virt, this->auxiliary_phys,
1046 nfc_geo->auxiliary_size,
1047 &auxiliary_virt, &auxiliary_phys);
1048 if (ret) {
1049 pr_err("Inadequate auxiliary DMA buffer\n");
1050 goto exit_auxiliary;
1051 }
1052 }
1053
1054 /* Ask the NFC. */
1055 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1056 if (ret)
1057 pr_err("Error in ECC-based write: %d\n", ret);
1058
1059 if (!this->swap_block_mark) {
1060 send_page_end(this, chip->oob_poi, mtd->oobsize,
1061 this->auxiliary_virt, this->auxiliary_phys,
1062 nfc_geo->auxiliary_size,
1063 auxiliary_virt, auxiliary_phys);
1064exit_auxiliary:
1065 send_page_end(this, buf, mtd->writesize,
1066 this->payload_virt, this->payload_phys,
1067 nfc_geo->payload_size,
1068 payload_virt, payload_phys);
1069 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001070
1071 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001072}
1073
1074/*
1075 * There are several places in this driver where we have to handle the OOB and
1076 * block marks. This is the function where things are the most complicated, so
1077 * this is where we try to explain it all. All the other places refer back to
1078 * here.
1079 *
1080 * These are the rules, in order of decreasing importance:
1081 *
1082 * 1) Nothing the caller does can be allowed to imperil the block mark.
1083 *
1084 * 2) In read operations, the first byte of the OOB we return must reflect the
1085 * true state of the block mark, no matter where that block mark appears in
1086 * the physical page.
1087 *
1088 * 3) ECC-based read operations return an OOB full of set bits (since we never
1089 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1090 * return).
1091 *
1092 * 4) "Raw" read operations return a direct view of the physical bytes in the
1093 * page, using the conventional definition of which bytes are data and which
1094 * are OOB. This gives the caller a way to see the actual, physical bytes
1095 * in the page, without the distortions applied by our ECC engine.
1096 *
1097 *
1098 * What we do for this specific read operation depends on two questions:
1099 *
1100 * 1) Are we doing a "raw" read, or an ECC-based read?
1101 *
1102 * 2) Are we using block mark swapping or transcription?
1103 *
1104 * There are four cases, illustrated by the following Karnaugh map:
1105 *
1106 * | Raw | ECC-based |
1107 * -------------+-------------------------+-------------------------+
1108 * | Read the conventional | |
1109 * | OOB at the end of the | |
1110 * Swapping | page and return it. It | |
1111 * | contains exactly what | |
1112 * | we want. | Read the block mark and |
1113 * -------------+-------------------------+ return it in a buffer |
1114 * | Read the conventional | full of set bits. |
1115 * | OOB at the end of the | |
1116 * | page and also the block | |
1117 * Transcribing | mark in the metadata. | |
1118 * | Copy the block mark | |
1119 * | into the first byte of | |
1120 * | the OOB. | |
1121 * -------------+-------------------------+-------------------------+
1122 *
1123 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1124 * giving an accurate view of the actual, physical bytes in the page (we're
1125 * overwriting the block mark). That's OK because it's more important to follow
1126 * rule #2.
1127 *
1128 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1129 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1130 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1131 * ECC-based or raw view of the page is implicit in which function it calls
1132 * (there is a similar pair of ECC-based/raw functions for writing).
1133 *
Brian Norris271b8742012-05-11 13:30:35 -07001134 * FIXME: The following paragraph is incorrect, now that there exist
1135 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1136 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001137 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1138 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1139 * caller wants an ECC-based or raw view of the page is not propagated down to
1140 * this driver.
1141 */
1142static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001143 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001144{
1145 struct gpmi_nand_data *this = chip->priv;
1146
1147 pr_debug("page number is %d\n", page);
1148 /* clear the OOB buffer */
1149 memset(chip->oob_poi, ~0, mtd->oobsize);
1150
1151 /* Read out the conventional OOB. */
1152 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1153 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1154
1155 /*
1156 * Now, we want to make sure the block mark is correct. In the
1157 * Swapping/Raw case, we already have it. Otherwise, we need to
1158 * explicitly read it.
1159 */
1160 if (!this->swap_block_mark) {
1161 /* Read the block mark into the first byte of the OOB buffer. */
1162 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1163 chip->oob_poi[0] = chip->read_byte(mtd);
1164 }
1165
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001166 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001167}
1168
1169static int
1170gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1171{
1172 /*
1173 * The BCH will use all the (page + oob).
1174 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1175 * But it can not stop some ioctls such MEMWRITEOOB which uses
Brian Norris0612b9d2011-08-30 18:45:40 -07001176 * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
Huang Shijie10a2bca2011-09-08 10:47:09 +08001177 * these ioctls too.
1178 */
1179 return -EPERM;
1180}
1181
1182static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1183{
1184 struct nand_chip *chip = mtd->priv;
1185 struct gpmi_nand_data *this = chip->priv;
1186 int block, ret = 0;
1187 uint8_t *block_mark;
1188 int column, page, status, chipnr;
1189
1190 /* Get block number */
1191 block = (int)(ofs >> chip->bbt_erase_shift);
1192 if (chip->bbt)
1193 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1194
1195 /* Do we have a flash based bad block table ? */
Wolfram Sang52899662012-01-31 13:10:43 +01001196 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001197 ret = nand_update_bbt(mtd, ofs);
1198 else {
1199 chipnr = (int)(ofs >> chip->chip_shift);
1200 chip->select_chip(mtd, chipnr);
1201
1202 column = this->swap_block_mark ? mtd->writesize : 0;
1203
1204 /* Write the block mark. */
1205 block_mark = this->data_buffer_dma;
1206 block_mark[0] = 0; /* bad block marker */
1207
1208 /* Shift to get page */
1209 page = (int)(ofs >> chip->page_shift);
1210
1211 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1212 chip->write_buf(mtd, block_mark, 1);
1213 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1214
1215 status = chip->waitfunc(mtd, chip);
1216 if (status & NAND_STATUS_FAIL)
1217 ret = -EIO;
1218
1219 chip->select_chip(mtd, -1);
1220 }
1221 if (!ret)
1222 mtd->ecc_stats.badblocks++;
1223
1224 return ret;
1225}
1226
Wolfram Sanga78da282012-03-21 19:29:17 +01001227static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001228{
1229 struct boot_rom_geometry *geometry = &this->rom_geometry;
1230
1231 /*
1232 * Set the boot block stride size.
1233 *
1234 * In principle, we should be reading this from the OTP bits, since
1235 * that's where the ROM is going to get it. In fact, we don't have any
1236 * way to read the OTP bits, so we go with the default and hope for the
1237 * best.
1238 */
1239 geometry->stride_size_in_pages = 64;
1240
1241 /*
1242 * Set the search area stride exponent.
1243 *
1244 * In principle, we should be reading this from the OTP bits, since
1245 * that's where the ROM is going to get it. In fact, we don't have any
1246 * way to read the OTP bits, so we go with the default and hope for the
1247 * best.
1248 */
1249 geometry->search_area_stride_exponent = 2;
1250 return 0;
1251}
1252
1253static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001254static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001255{
1256 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1257 struct device *dev = this->dev;
1258 struct mtd_info *mtd = &this->mtd;
1259 struct nand_chip *chip = &this->nand;
1260 unsigned int search_area_size_in_strides;
1261 unsigned int stride;
1262 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001263 uint8_t *buffer = chip->buffers->databuf;
1264 int saved_chip_number;
1265 int found_an_ncb_fingerprint = false;
1266
1267 /* Compute the number of strides in a search area. */
1268 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1269
1270 saved_chip_number = this->current_chip;
1271 chip->select_chip(mtd, 0);
1272
1273 /*
1274 * Loop through the first search area, looking for the NCB fingerprint.
1275 */
1276 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1277
1278 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001279 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001280 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001281
1282 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1283
1284 /*
1285 * Read the NCB fingerprint. The fingerprint is four bytes long
1286 * and starts in the 12th byte of the page.
1287 */
1288 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1289 chip->read_buf(mtd, buffer, strlen(fingerprint));
1290
1291 /* Look for the fingerprint. */
1292 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1293 found_an_ncb_fingerprint = true;
1294 break;
1295 }
1296
1297 }
1298
1299 chip->select_chip(mtd, saved_chip_number);
1300
1301 if (found_an_ncb_fingerprint)
1302 dev_dbg(dev, "\tFound a fingerprint\n");
1303 else
1304 dev_dbg(dev, "\tNo fingerprint found\n");
1305 return found_an_ncb_fingerprint;
1306}
1307
1308/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001309static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001310{
1311 struct device *dev = this->dev;
1312 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1313 struct mtd_info *mtd = &this->mtd;
1314 struct nand_chip *chip = &this->nand;
1315 unsigned int block_size_in_pages;
1316 unsigned int search_area_size_in_strides;
1317 unsigned int search_area_size_in_pages;
1318 unsigned int search_area_size_in_blocks;
1319 unsigned int block;
1320 unsigned int stride;
1321 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001322 uint8_t *buffer = chip->buffers->databuf;
1323 int saved_chip_number;
1324 int status;
1325
1326 /* Compute the search area geometry. */
1327 block_size_in_pages = mtd->erasesize / mtd->writesize;
1328 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1329 search_area_size_in_pages = search_area_size_in_strides *
1330 rom_geo->stride_size_in_pages;
1331 search_area_size_in_blocks =
1332 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1333 block_size_in_pages;
1334
1335 dev_dbg(dev, "Search Area Geometry :\n");
1336 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1337 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1338 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1339
1340 /* Select chip 0. */
1341 saved_chip_number = this->current_chip;
1342 chip->select_chip(mtd, 0);
1343
1344 /* Loop over blocks in the first search area, erasing them. */
1345 dev_dbg(dev, "Erasing the search area...\n");
1346
1347 for (block = 0; block < search_area_size_in_blocks; block++) {
1348 /* Compute the page address. */
1349 page = block * block_size_in_pages;
1350
1351 /* Erase this block. */
1352 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1353 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1354 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1355
1356 /* Wait for the erase to finish. */
1357 status = chip->waitfunc(mtd, chip);
1358 if (status & NAND_STATUS_FAIL)
1359 dev_err(dev, "[%s] Erase failed.\n", __func__);
1360 }
1361
1362 /* Write the NCB fingerprint into the page buffer. */
1363 memset(buffer, ~0, mtd->writesize);
1364 memset(chip->oob_poi, ~0, mtd->oobsize);
1365 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1366
1367 /* Loop through the first search area, writing NCB fingerprints. */
1368 dev_dbg(dev, "Writing NCB fingerprints...\n");
1369 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001370 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001371 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001372
1373 /* Write the first page of the current stride. */
1374 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1375 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001376 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001377 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1378
1379 /* Wait for the write to finish. */
1380 status = chip->waitfunc(mtd, chip);
1381 if (status & NAND_STATUS_FAIL)
1382 dev_err(dev, "[%s] Write failed.\n", __func__);
1383 }
1384
1385 /* Deselect chip 0. */
1386 chip->select_chip(mtd, saved_chip_number);
1387 return 0;
1388}
1389
Wolfram Sanga78da282012-03-21 19:29:17 +01001390static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001391{
1392 struct device *dev = this->dev;
1393 struct nand_chip *chip = &this->nand;
1394 struct mtd_info *mtd = &this->mtd;
1395 unsigned int block_count;
1396 unsigned int block;
1397 int chipnr;
1398 int page;
1399 loff_t byte;
1400 uint8_t block_mark;
1401 int ret = 0;
1402
1403 /*
1404 * If control arrives here, we can't use block mark swapping, which
1405 * means we're forced to use transcription. First, scan for the
1406 * transcription stamp. If we find it, then we don't have to do
1407 * anything -- the block marks are already transcribed.
1408 */
1409 if (mx23_check_transcription_stamp(this))
1410 return 0;
1411
1412 /*
1413 * If control arrives here, we couldn't find a transcription stamp, so
1414 * so we presume the block marks are in the conventional location.
1415 */
1416 dev_dbg(dev, "Transcribing bad block marks...\n");
1417
1418 /* Compute the number of blocks in the entire medium. */
1419 block_count = chip->chipsize >> chip->phys_erase_shift;
1420
1421 /*
1422 * Loop over all the blocks in the medium, transcribing block marks as
1423 * we go.
1424 */
1425 for (block = 0; block < block_count; block++) {
1426 /*
1427 * Compute the chip, page and byte addresses for this block's
1428 * conventional mark.
1429 */
1430 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1431 page = block << (chip->phys_erase_shift - chip->page_shift);
1432 byte = block << chip->phys_erase_shift;
1433
1434 /* Send the command to read the conventional block mark. */
1435 chip->select_chip(mtd, chipnr);
1436 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1437 block_mark = chip->read_byte(mtd);
1438 chip->select_chip(mtd, -1);
1439
1440 /*
1441 * Check if the block is marked bad. If so, we need to mark it
1442 * again, but this time the result will be a mark in the
1443 * location where we transcribe block marks.
1444 */
1445 if (block_mark != 0xff) {
1446 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1447 ret = chip->block_markbad(mtd, byte);
1448 if (ret)
1449 dev_err(dev, "Failed to mark block bad with "
1450 "ret %d\n", ret);
1451 }
1452 }
1453
1454 /* Write the stamp that indicates we've transcribed the block marks. */
1455 mx23_write_transcription_stamp(this);
1456 return 0;
1457}
1458
Wolfram Sanga78da282012-03-21 19:29:17 +01001459static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001460{
1461 nand_boot_set_geometry(this);
1462
1463 /* This is ROM arch-specific initilization before the BBT scanning. */
1464 if (GPMI_IS_MX23(this))
1465 return mx23_boot_init(this);
1466 return 0;
1467}
1468
Wolfram Sanga78da282012-03-21 19:29:17 +01001469static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001470{
1471 int ret;
1472
1473 /* Free the temporary DMA memory for reading ID. */
1474 gpmi_free_dma_buffer(this);
1475
1476 /* Set up the NFC geometry which is used by BCH. */
1477 ret = bch_set_geometry(this);
1478 if (ret) {
1479 pr_err("set geometry ret : %d\n", ret);
1480 return ret;
1481 }
1482
1483 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1484 return gpmi_alloc_dma_buffer(this);
1485}
1486
1487static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1488{
1489 int ret;
1490
1491 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1492 if (GPMI_IS_MX23(this))
1493 this->swap_block_mark = false;
1494 else
1495 this->swap_block_mark = true;
1496
1497 /* Set up the medium geometry */
1498 ret = gpmi_set_geometry(this);
1499 if (ret)
1500 return ret;
1501
Marek Vasut5636ce02012-05-21 22:59:27 +02001502 /* Adjust the ECC strength according to the chip. */
1503 this->nand.ecc.strength = this->bch_geometry.ecc_strength;
1504 this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
Huang Shijiee0dd89c2012-07-03 16:24:33 +08001505 this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
Marek Vasut5636ce02012-05-21 22:59:27 +02001506
Huang Shijie10a2bca2011-09-08 10:47:09 +08001507 /* NAND boot init, depends on the gpmi_set_geometry(). */
1508 return nand_boot_init(this);
1509}
1510
1511static int gpmi_scan_bbt(struct mtd_info *mtd)
1512{
1513 struct nand_chip *chip = mtd->priv;
1514 struct gpmi_nand_data *this = chip->priv;
1515 int ret;
1516
1517 /* Prepare for the BBT scan. */
1518 ret = gpmi_pre_bbt_scan(this);
1519 if (ret)
1520 return ret;
1521
1522 /* use the default BBT implementation */
1523 return nand_default_bbt(mtd);
1524}
1525
Huang Shijie513d57e2012-07-17 14:14:02 +08001526static void gpmi_nfc_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001527{
1528 nand_release(&this->mtd);
1529 gpmi_free_dma_buffer(this);
1530}
1531
1532static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
1533{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001534 struct mtd_info *mtd = &this->mtd;
1535 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001536 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001537 int ret;
1538
1539 /* init current chip */
1540 this->current_chip = -1;
1541
1542 /* init the MTD data structures */
1543 mtd->priv = chip;
1544 mtd->name = "gpmi-nand";
1545 mtd->owner = THIS_MODULE;
1546
1547 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1548 chip->priv = this;
1549 chip->select_chip = gpmi_select_chip;
1550 chip->cmd_ctrl = gpmi_cmd_ctrl;
1551 chip->dev_ready = gpmi_dev_ready;
1552 chip->read_byte = gpmi_read_byte;
1553 chip->read_buf = gpmi_read_buf;
1554 chip->write_buf = gpmi_write_buf;
1555 chip->ecc.read_page = gpmi_ecc_read_page;
1556 chip->ecc.write_page = gpmi_ecc_write_page;
1557 chip->ecc.read_oob = gpmi_ecc_read_oob;
1558 chip->ecc.write_oob = gpmi_ecc_write_oob;
1559 chip->scan_bbt = gpmi_scan_bbt;
1560 chip->badblock_pattern = &gpmi_bbt_descr;
1561 chip->block_markbad = gpmi_block_markbad;
1562 chip->options |= NAND_NO_SUBPAGE_WRITE;
1563 chip->ecc.mode = NAND_ECC_HW;
1564 chip->ecc.size = 1;
Marek Vasut5636ce02012-05-21 22:59:27 +02001565 chip->ecc.strength = 8;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001566 chip->ecc.layout = &gpmi_hw_ecclayout;
Huang Shijiec50c6942012-07-03 16:24:32 +08001567 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1568 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001569
1570 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1571 this->bch_geometry.payload_size = 1024;
1572 this->bch_geometry.auxiliary_size = 128;
1573 ret = gpmi_alloc_dma_buffer(this);
1574 if (ret)
1575 goto err_out;
1576
Huang Shijiee10db1f2012-05-04 21:42:05 -04001577 ret = nand_scan(mtd, 1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001578 if (ret) {
1579 pr_err("Chip scan failed\n");
1580 goto err_out;
1581 }
1582
Huang Shijiee10db1f2012-05-04 21:42:05 -04001583 ppdata.of_node = this->pdev->dev.of_node;
1584 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001585 if (ret)
1586 goto err_out;
1587 return 0;
1588
1589err_out:
1590 gpmi_nfc_exit(this);
1591 return ret;
1592}
1593
Huang Shijiee10db1f2012-05-04 21:42:05 -04001594static const struct platform_device_id gpmi_ids[] = {
1595 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1596 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001597 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Huang Shijiee10db1f2012-05-04 21:42:05 -04001598 {},
1599};
1600
1601static const struct of_device_id gpmi_nand_id_table[] = {
1602 {
1603 .compatible = "fsl,imx23-gpmi-nand",
1604 .data = (void *)&gpmi_ids[IS_MX23]
1605 }, {
1606 .compatible = "fsl,imx28-gpmi-nand",
1607 .data = (void *)&gpmi_ids[IS_MX28]
Huang Shijie9013bb42012-05-04 21:42:06 -04001608 }, {
1609 .compatible = "fsl,imx6q-gpmi-nand",
1610 .data = (void *)&gpmi_ids[IS_MX6Q]
Huang Shijiee10db1f2012-05-04 21:42:05 -04001611 }, {}
1612};
1613MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1614
Huang Shijie10a2bca2011-09-08 10:47:09 +08001615static int __devinit gpmi_nand_probe(struct platform_device *pdev)
1616{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001617 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001618 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001619 int ret;
1620
Huang Shijiee10db1f2012-05-04 21:42:05 -04001621 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1622 if (of_id) {
1623 pdev->id_entry = of_id->data;
1624 } else {
1625 pr_err("Failed to find the right device id.\n");
1626 return -ENOMEM;
1627 }
1628
Huang Shijie10a2bca2011-09-08 10:47:09 +08001629 this = kzalloc(sizeof(*this), GFP_KERNEL);
1630 if (!this) {
1631 pr_err("Failed to allocate per-device memory\n");
1632 return -ENOMEM;
1633 }
1634
1635 platform_set_drvdata(pdev, this);
1636 this->pdev = pdev;
1637 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001638
1639 ret = acquire_resources(this);
1640 if (ret)
1641 goto exit_acquire_resources;
1642
1643 ret = init_hardware(this);
1644 if (ret)
1645 goto exit_nfc_init;
1646
1647 ret = gpmi_nfc_init(this);
1648 if (ret)
1649 goto exit_nfc_init;
1650
1651 return 0;
1652
1653exit_nfc_init:
1654 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001655exit_acquire_resources:
1656 platform_set_drvdata(pdev, NULL);
1657 kfree(this);
1658 return ret;
1659}
1660
1661static int __exit gpmi_nand_remove(struct platform_device *pdev)
1662{
1663 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1664
1665 gpmi_nfc_exit(this);
1666 release_resources(this);
1667 platform_set_drvdata(pdev, NULL);
1668 kfree(this);
1669 return 0;
1670}
1671
Huang Shijie10a2bca2011-09-08 10:47:09 +08001672static struct platform_driver gpmi_nand_driver = {
1673 .driver = {
1674 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001675 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001676 },
1677 .probe = gpmi_nand_probe,
1678 .remove = __exit_p(gpmi_nand_remove),
1679 .id_table = gpmi_ids,
1680};
1681
1682static int __init gpmi_nand_init(void)
1683{
1684 int err;
1685
1686 err = platform_driver_register(&gpmi_nand_driver);
1687 if (err == 0)
1688 printk(KERN_INFO "GPMI NAND driver registered. (IMX)\n");
1689 else
1690 pr_err("i.MX GPMI NAND driver registration failed\n");
1691 return err;
1692}
1693
1694static void __exit gpmi_nand_exit(void)
1695{
1696 platform_driver_unregister(&gpmi_nand_driver);
1697}
1698
1699module_init(gpmi_nand_init);
1700module_exit(gpmi_nand_exit);
1701
1702MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1703MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1704MODULE_LICENSE("GPL");