blob: 9da9ee88a824ce8a781efddc762256962d6c4e64 [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;
320 void *p;
321
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;
427 int dma_channel;
428 unsigned int ret;
429 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
468static int __devinit acquire_resources(struct gpmi_nand_data *this)
469{
470 struct resources *res = &this->resources;
Shawn Guo39febc02012-05-06 22:57:41 +0800471 struct pinctrl *pinctrl;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800472 int ret;
473
474 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
475 if (ret)
476 goto exit_regs;
477
478 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
479 if (ret)
480 goto exit_regs;
481
482 ret = acquire_bch_irq(this, bch_irq);
483 if (ret)
484 goto exit_regs;
485
486 ret = acquire_dma_channels(this);
487 if (ret)
488 goto exit_dma_channels;
489
Shawn Guo3e48b1b2012-05-19 21:06:13 +0800490 pinctrl = devm_pinctrl_get_select_default(&this->pdev->dev);
Shawn Guo39febc02012-05-06 22:57:41 +0800491 if (IS_ERR(pinctrl)) {
492 ret = PTR_ERR(pinctrl);
493 goto exit_pin;
494 }
495
Huang Shijie10a2bca2011-09-08 10:47:09 +0800496 res->clock = clk_get(&this->pdev->dev, NULL);
497 if (IS_ERR(res->clock)) {
498 pr_err("can not get the clock\n");
499 ret = -ENOENT;
500 goto exit_clock;
501 }
502 return 0;
503
504exit_clock:
Shawn Guo39febc02012-05-06 22:57:41 +0800505exit_pin:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800506 release_dma_channels(this);
507exit_dma_channels:
508 release_bch_irq(this);
509exit_regs:
510 release_register_block(this);
511 return ret;
512}
513
514static void release_resources(struct gpmi_nand_data *this)
515{
516 struct resources *r = &this->resources;
517
518 clk_put(r->clock);
519 release_register_block(this);
520 release_bch_irq(this);
521 release_dma_channels(this);
522}
523
524static int __devinit init_hardware(struct gpmi_nand_data *this)
525{
526 int ret;
527
528 /*
529 * This structure contains the "safe" GPMI timing that should succeed
530 * with any NAND Flash device
531 * (although, with less-than-optimal performance).
532 */
533 struct nand_timing safe_timing = {
534 .data_setup_in_ns = 80,
535 .data_hold_in_ns = 60,
536 .address_setup_in_ns = 25,
537 .gpmi_sample_delay_in_ns = 6,
538 .tREA_in_ns = -1,
539 .tRLOH_in_ns = -1,
540 .tRHOH_in_ns = -1,
541 };
542
543 /* Initialize the hardwares. */
544 ret = gpmi_init(this);
545 if (ret)
546 return ret;
547
548 this->timing = safe_timing;
549 return 0;
550}
551
552static int read_page_prepare(struct gpmi_nand_data *this,
553 void *destination, unsigned length,
554 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
555 void **use_virt, dma_addr_t *use_phys)
556{
557 struct device *dev = this->dev;
558
559 if (virt_addr_valid(destination)) {
560 dma_addr_t dest_phys;
561
562 dest_phys = dma_map_single(dev, destination,
563 length, DMA_FROM_DEVICE);
564 if (dma_mapping_error(dev, dest_phys)) {
565 if (alt_size < length) {
566 pr_err("Alternate buffer is too small\n");
567 return -ENOMEM;
568 }
569 goto map_failed;
570 }
571 *use_virt = destination;
572 *use_phys = dest_phys;
573 this->direct_dma_map_ok = true;
574 return 0;
575 }
576
577map_failed:
578 *use_virt = alt_virt;
579 *use_phys = alt_phys;
580 this->direct_dma_map_ok = false;
581 return 0;
582}
583
584static inline void read_page_end(struct gpmi_nand_data *this,
585 void *destination, unsigned length,
586 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
587 void *used_virt, dma_addr_t used_phys)
588{
589 if (this->direct_dma_map_ok)
590 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
591}
592
593static inline void read_page_swap_end(struct gpmi_nand_data *this,
594 void *destination, unsigned length,
595 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
596 void *used_virt, dma_addr_t used_phys)
597{
598 if (!this->direct_dma_map_ok)
599 memcpy(destination, alt_virt, length);
600}
601
602static int send_page_prepare(struct gpmi_nand_data *this,
603 const void *source, unsigned length,
604 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
605 const void **use_virt, dma_addr_t *use_phys)
606{
607 struct device *dev = this->dev;
608
609 if (virt_addr_valid(source)) {
610 dma_addr_t source_phys;
611
612 source_phys = dma_map_single(dev, (void *)source, length,
613 DMA_TO_DEVICE);
614 if (dma_mapping_error(dev, source_phys)) {
615 if (alt_size < length) {
616 pr_err("Alternate buffer is too small\n");
617 return -ENOMEM;
618 }
619 goto map_failed;
620 }
621 *use_virt = source;
622 *use_phys = source_phys;
623 return 0;
624 }
625map_failed:
626 /*
627 * Copy the content of the source buffer into the alternate
628 * buffer and set up the return values accordingly.
629 */
630 memcpy(alt_virt, source, length);
631
632 *use_virt = alt_virt;
633 *use_phys = alt_phys;
634 return 0;
635}
636
637static void send_page_end(struct gpmi_nand_data *this,
638 const void *source, unsigned length,
639 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
640 const void *used_virt, dma_addr_t used_phys)
641{
642 struct device *dev = this->dev;
643 if (used_virt == source)
644 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
645}
646
647static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
648{
649 struct device *dev = this->dev;
650
651 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
652 dma_free_coherent(dev, this->page_buffer_size,
653 this->page_buffer_virt,
654 this->page_buffer_phys);
655 kfree(this->cmd_buffer);
656 kfree(this->data_buffer_dma);
657
658 this->cmd_buffer = NULL;
659 this->data_buffer_dma = NULL;
660 this->page_buffer_virt = NULL;
661 this->page_buffer_size = 0;
662}
663
664/* Allocate the DMA buffers */
665static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
666{
667 struct bch_geometry *geo = &this->bch_geometry;
668 struct device *dev = this->dev;
669
670 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
671 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA);
672 if (this->cmd_buffer == NULL)
673 goto error_alloc;
674
675 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
676 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA);
677 if (this->data_buffer_dma == NULL)
678 goto error_alloc;
679
680 /*
681 * [3] Allocate the page buffer.
682 *
683 * Both the payload buffer and the auxiliary buffer must appear on
684 * 32-bit boundaries. We presume the size of the payload buffer is a
685 * power of two and is much larger than four, which guarantees the
686 * auxiliary buffer will appear on a 32-bit boundary.
687 */
688 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
689 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
690 &this->page_buffer_phys, GFP_DMA);
691 if (!this->page_buffer_virt)
692 goto error_alloc;
693
694
695 /* Slice up the page buffer. */
696 this->payload_virt = this->page_buffer_virt;
697 this->payload_phys = this->page_buffer_phys;
698 this->auxiliary_virt = this->payload_virt + geo->payload_size;
699 this->auxiliary_phys = this->payload_phys + geo->payload_size;
700 return 0;
701
702error_alloc:
703 gpmi_free_dma_buffer(this);
704 pr_err("allocate DMA buffer ret!!\n");
705 return -ENOMEM;
706}
707
708static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
709{
710 struct nand_chip *chip = mtd->priv;
711 struct gpmi_nand_data *this = chip->priv;
712 int ret;
713
714 /*
715 * Every operation begins with a command byte and a series of zero or
716 * more address bytes. These are distinguished by either the Address
717 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
718 * asserted. When MTD is ready to execute the command, it will deassert
719 * both latch enables.
720 *
721 * Rather than run a separate DMA operation for every single byte, we
722 * queue them up and run a single DMA operation for the entire series
723 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
724 */
725 if ((ctrl & (NAND_ALE | NAND_CLE))) {
726 if (data != NAND_CMD_NONE)
727 this->cmd_buffer[this->command_length++] = data;
728 return;
729 }
730
731 if (!this->command_length)
732 return;
733
734 ret = gpmi_send_command(this);
735 if (ret)
736 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
737
738 this->command_length = 0;
739}
740
741static int gpmi_dev_ready(struct mtd_info *mtd)
742{
743 struct nand_chip *chip = mtd->priv;
744 struct gpmi_nand_data *this = chip->priv;
745
746 return gpmi_is_ready(this, this->current_chip);
747}
748
749static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
750{
751 struct nand_chip *chip = mtd->priv;
752 struct gpmi_nand_data *this = chip->priv;
753
754 if ((this->current_chip < 0) && (chipnr >= 0))
755 gpmi_begin(this);
756 else if ((this->current_chip >= 0) && (chipnr < 0))
757 gpmi_end(this);
758
759 this->current_chip = chipnr;
760}
761
762static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
763{
764 struct nand_chip *chip = mtd->priv;
765 struct gpmi_nand_data *this = chip->priv;
766
767 pr_debug("len is %d\n", len);
768 this->upper_buf = buf;
769 this->upper_len = len;
770
771 gpmi_read_data(this);
772}
773
774static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
775{
776 struct nand_chip *chip = mtd->priv;
777 struct gpmi_nand_data *this = chip->priv;
778
779 pr_debug("len is %d\n", len);
780 this->upper_buf = (uint8_t *)buf;
781 this->upper_len = len;
782
783 gpmi_send_data(this);
784}
785
786static uint8_t gpmi_read_byte(struct mtd_info *mtd)
787{
788 struct nand_chip *chip = mtd->priv;
789 struct gpmi_nand_data *this = chip->priv;
790 uint8_t *buf = this->data_buffer_dma;
791
792 gpmi_read_buf(mtd, buf, 1);
793 return buf[0];
794}
795
796/*
797 * Handles block mark swapping.
798 * It can be called in swapping the block mark, or swapping it back,
799 * because the the operations are the same.
800 */
801static void block_mark_swapping(struct gpmi_nand_data *this,
802 void *payload, void *auxiliary)
803{
804 struct bch_geometry *nfc_geo = &this->bch_geometry;
805 unsigned char *p;
806 unsigned char *a;
807 unsigned int bit;
808 unsigned char mask;
809 unsigned char from_data;
810 unsigned char from_oob;
811
812 if (!this->swap_block_mark)
813 return;
814
815 /*
816 * If control arrives here, we're swapping. Make some convenience
817 * variables.
818 */
819 bit = nfc_geo->block_mark_bit_offset;
820 p = payload + nfc_geo->block_mark_byte_offset;
821 a = auxiliary;
822
823 /*
824 * Get the byte from the data area that overlays the block mark. Since
825 * the ECC engine applies its own view to the bits in the page, the
826 * physical block mark won't (in general) appear on a byte boundary in
827 * the data.
828 */
829 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
830
831 /* Get the byte from the OOB. */
832 from_oob = a[0];
833
834 /* Swap them. */
835 a[0] = from_data;
836
837 mask = (0x1 << bit) - 1;
838 p[0] = (p[0] & mask) | (from_oob << bit);
839
840 mask = ~0 << bit;
841 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
842}
843
844static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700845 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800846{
847 struct gpmi_nand_data *this = chip->priv;
848 struct bch_geometry *nfc_geo = &this->bch_geometry;
849 void *payload_virt;
850 dma_addr_t payload_phys;
851 void *auxiliary_virt;
852 dma_addr_t auxiliary_phys;
853 unsigned int i;
854 unsigned char *status;
855 unsigned int failed;
856 unsigned int corrected;
857 int ret;
858
859 pr_debug("page number is : %d\n", page);
860 ret = read_page_prepare(this, buf, mtd->writesize,
861 this->payload_virt, this->payload_phys,
862 nfc_geo->payload_size,
863 &payload_virt, &payload_phys);
864 if (ret) {
865 pr_err("Inadequate DMA buffer\n");
866 ret = -ENOMEM;
867 return ret;
868 }
869 auxiliary_virt = this->auxiliary_virt;
870 auxiliary_phys = this->auxiliary_phys;
871
872 /* go! */
873 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
874 read_page_end(this, buf, mtd->writesize,
875 this->payload_virt, this->payload_phys,
876 nfc_geo->payload_size,
877 payload_virt, payload_phys);
878 if (ret) {
879 pr_err("Error in ECC-based read: %d\n", ret);
880 goto exit_nfc;
881 }
882
883 /* handle the block mark swapping */
884 block_mark_swapping(this, payload_virt, auxiliary_virt);
885
886 /* Loop over status bytes, accumulating ECC status. */
887 failed = 0;
888 corrected = 0;
889 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
890
891 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
892 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
893 continue;
894
895 if (*status == STATUS_UNCORRECTABLE) {
896 failed++;
897 continue;
898 }
899 corrected += *status;
900 }
901
902 /*
903 * Propagate ECC status to the owning MTD only when failed or
904 * corrected times nearly reaches our ECC correction threshold.
905 */
906 if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
907 mtd->ecc_stats.failed += failed;
908 mtd->ecc_stats.corrected += corrected;
909 }
910
Brian Norris7725cc82012-05-02 10:15:02 -0700911 if (oob_required) {
912 /*
913 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
914 * for details about our policy for delivering the OOB.
915 *
916 * We fill the caller's buffer with set bits, and then copy the
917 * block mark to th caller's buffer. Note that, if block mark
918 * swapping was necessary, it has already been done, so we can
919 * rely on the first byte of the auxiliary buffer to contain
920 * the block mark.
921 */
922 memset(chip->oob_poi, ~0, mtd->oobsize);
923 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -0700924 }
Sascha Hauer6023813a2012-06-26 17:26:16 +0200925
926 read_page_swap_end(this, buf, mtd->writesize,
927 this->payload_virt, this->payload_phys,
928 nfc_geo->payload_size,
929 payload_virt, payload_phys);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800930exit_nfc:
931 return ret;
932}
933
Josh Wufdbad98d2012-06-25 18:07:45 +0800934static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700935 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800936{
937 struct gpmi_nand_data *this = chip->priv;
938 struct bch_geometry *nfc_geo = &this->bch_geometry;
939 const void *payload_virt;
940 dma_addr_t payload_phys;
941 const void *auxiliary_virt;
942 dma_addr_t auxiliary_phys;
943 int ret;
944
945 pr_debug("ecc write page.\n");
946 if (this->swap_block_mark) {
947 /*
948 * If control arrives here, we're doing block mark swapping.
949 * Since we can't modify the caller's buffers, we must copy them
950 * into our own.
951 */
952 memcpy(this->payload_virt, buf, mtd->writesize);
953 payload_virt = this->payload_virt;
954 payload_phys = this->payload_phys;
955
956 memcpy(this->auxiliary_virt, chip->oob_poi,
957 nfc_geo->auxiliary_size);
958 auxiliary_virt = this->auxiliary_virt;
959 auxiliary_phys = this->auxiliary_phys;
960
961 /* Handle block mark swapping. */
962 block_mark_swapping(this,
963 (void *) payload_virt, (void *) auxiliary_virt);
964 } else {
965 /*
966 * If control arrives here, we're not doing block mark swapping,
967 * so we can to try and use the caller's buffers.
968 */
969 ret = send_page_prepare(this,
970 buf, mtd->writesize,
971 this->payload_virt, this->payload_phys,
972 nfc_geo->payload_size,
973 &payload_virt, &payload_phys);
974 if (ret) {
975 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +0800976 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800977 }
978
979 ret = send_page_prepare(this,
980 chip->oob_poi, mtd->oobsize,
981 this->auxiliary_virt, this->auxiliary_phys,
982 nfc_geo->auxiliary_size,
983 &auxiliary_virt, &auxiliary_phys);
984 if (ret) {
985 pr_err("Inadequate auxiliary DMA buffer\n");
986 goto exit_auxiliary;
987 }
988 }
989
990 /* Ask the NFC. */
991 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
992 if (ret)
993 pr_err("Error in ECC-based write: %d\n", ret);
994
995 if (!this->swap_block_mark) {
996 send_page_end(this, chip->oob_poi, mtd->oobsize,
997 this->auxiliary_virt, this->auxiliary_phys,
998 nfc_geo->auxiliary_size,
999 auxiliary_virt, auxiliary_phys);
1000exit_auxiliary:
1001 send_page_end(this, buf, mtd->writesize,
1002 this->payload_virt, this->payload_phys,
1003 nfc_geo->payload_size,
1004 payload_virt, payload_phys);
1005 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001006
1007 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001008}
1009
1010/*
1011 * There are several places in this driver where we have to handle the OOB and
1012 * block marks. This is the function where things are the most complicated, so
1013 * this is where we try to explain it all. All the other places refer back to
1014 * here.
1015 *
1016 * These are the rules, in order of decreasing importance:
1017 *
1018 * 1) Nothing the caller does can be allowed to imperil the block mark.
1019 *
1020 * 2) In read operations, the first byte of the OOB we return must reflect the
1021 * true state of the block mark, no matter where that block mark appears in
1022 * the physical page.
1023 *
1024 * 3) ECC-based read operations return an OOB full of set bits (since we never
1025 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1026 * return).
1027 *
1028 * 4) "Raw" read operations return a direct view of the physical bytes in the
1029 * page, using the conventional definition of which bytes are data and which
1030 * are OOB. This gives the caller a way to see the actual, physical bytes
1031 * in the page, without the distortions applied by our ECC engine.
1032 *
1033 *
1034 * What we do for this specific read operation depends on two questions:
1035 *
1036 * 1) Are we doing a "raw" read, or an ECC-based read?
1037 *
1038 * 2) Are we using block mark swapping or transcription?
1039 *
1040 * There are four cases, illustrated by the following Karnaugh map:
1041 *
1042 * | Raw | ECC-based |
1043 * -------------+-------------------------+-------------------------+
1044 * | Read the conventional | |
1045 * | OOB at the end of the | |
1046 * Swapping | page and return it. It | |
1047 * | contains exactly what | |
1048 * | we want. | Read the block mark and |
1049 * -------------+-------------------------+ return it in a buffer |
1050 * | Read the conventional | full of set bits. |
1051 * | OOB at the end of the | |
1052 * | page and also the block | |
1053 * Transcribing | mark in the metadata. | |
1054 * | Copy the block mark | |
1055 * | into the first byte of | |
1056 * | the OOB. | |
1057 * -------------+-------------------------+-------------------------+
1058 *
1059 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1060 * giving an accurate view of the actual, physical bytes in the page (we're
1061 * overwriting the block mark). That's OK because it's more important to follow
1062 * rule #2.
1063 *
1064 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1065 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1066 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1067 * ECC-based or raw view of the page is implicit in which function it calls
1068 * (there is a similar pair of ECC-based/raw functions for writing).
1069 *
Brian Norris271b874b2012-05-11 13:30:35 -07001070 * FIXME: The following paragraph is incorrect, now that there exist
1071 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1072 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001073 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1074 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1075 * caller wants an ECC-based or raw view of the page is not propagated down to
1076 * this driver.
1077 */
1078static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001079 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001080{
1081 struct gpmi_nand_data *this = chip->priv;
1082
1083 pr_debug("page number is %d\n", page);
1084 /* clear the OOB buffer */
1085 memset(chip->oob_poi, ~0, mtd->oobsize);
1086
1087 /* Read out the conventional OOB. */
1088 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1089 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1090
1091 /*
1092 * Now, we want to make sure the block mark is correct. In the
1093 * Swapping/Raw case, we already have it. Otherwise, we need to
1094 * explicitly read it.
1095 */
1096 if (!this->swap_block_mark) {
1097 /* Read the block mark into the first byte of the OOB buffer. */
1098 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1099 chip->oob_poi[0] = chip->read_byte(mtd);
1100 }
1101
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001102 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001103}
1104
1105static int
1106gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1107{
1108 /*
1109 * The BCH will use all the (page + oob).
1110 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1111 * But it can not stop some ioctls such MEMWRITEOOB which uses
Brian Norris0612b9d2011-08-30 18:45:40 -07001112 * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
Huang Shijie10a2bca2011-09-08 10:47:09 +08001113 * these ioctls too.
1114 */
1115 return -EPERM;
1116}
1117
1118static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1119{
1120 struct nand_chip *chip = mtd->priv;
1121 struct gpmi_nand_data *this = chip->priv;
1122 int block, ret = 0;
1123 uint8_t *block_mark;
1124 int column, page, status, chipnr;
1125
1126 /* Get block number */
1127 block = (int)(ofs >> chip->bbt_erase_shift);
1128 if (chip->bbt)
1129 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1130
1131 /* Do we have a flash based bad block table ? */
Wolfram Sang52899662012-01-31 13:10:43 +01001132 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001133 ret = nand_update_bbt(mtd, ofs);
1134 else {
1135 chipnr = (int)(ofs >> chip->chip_shift);
1136 chip->select_chip(mtd, chipnr);
1137
1138 column = this->swap_block_mark ? mtd->writesize : 0;
1139
1140 /* Write the block mark. */
1141 block_mark = this->data_buffer_dma;
1142 block_mark[0] = 0; /* bad block marker */
1143
1144 /* Shift to get page */
1145 page = (int)(ofs >> chip->page_shift);
1146
1147 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1148 chip->write_buf(mtd, block_mark, 1);
1149 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1150
1151 status = chip->waitfunc(mtd, chip);
1152 if (status & NAND_STATUS_FAIL)
1153 ret = -EIO;
1154
1155 chip->select_chip(mtd, -1);
1156 }
1157 if (!ret)
1158 mtd->ecc_stats.badblocks++;
1159
1160 return ret;
1161}
1162
Wolfram Sanga78da282012-03-21 19:29:17 +01001163static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001164{
1165 struct boot_rom_geometry *geometry = &this->rom_geometry;
1166
1167 /*
1168 * Set the boot block stride size.
1169 *
1170 * In principle, we should be reading this from the OTP bits, since
1171 * that's where the ROM is going to get it. In fact, we don't have any
1172 * way to read the OTP bits, so we go with the default and hope for the
1173 * best.
1174 */
1175 geometry->stride_size_in_pages = 64;
1176
1177 /*
1178 * Set the search area stride exponent.
1179 *
1180 * In principle, we should be reading this from the OTP bits, since
1181 * that's where the ROM is going to get it. In fact, we don't have any
1182 * way to read the OTP bits, so we go with the default and hope for the
1183 * best.
1184 */
1185 geometry->search_area_stride_exponent = 2;
1186 return 0;
1187}
1188
1189static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001190static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001191{
1192 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1193 struct device *dev = this->dev;
1194 struct mtd_info *mtd = &this->mtd;
1195 struct nand_chip *chip = &this->nand;
1196 unsigned int search_area_size_in_strides;
1197 unsigned int stride;
1198 unsigned int page;
1199 loff_t byte;
1200 uint8_t *buffer = chip->buffers->databuf;
1201 int saved_chip_number;
1202 int found_an_ncb_fingerprint = false;
1203
1204 /* Compute the number of strides in a search area. */
1205 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1206
1207 saved_chip_number = this->current_chip;
1208 chip->select_chip(mtd, 0);
1209
1210 /*
1211 * Loop through the first search area, looking for the NCB fingerprint.
1212 */
1213 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1214
1215 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1216 /* Compute the page and byte addresses. */
1217 page = stride * rom_geo->stride_size_in_pages;
1218 byte = page * mtd->writesize;
1219
1220 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1221
1222 /*
1223 * Read the NCB fingerprint. The fingerprint is four bytes long
1224 * and starts in the 12th byte of the page.
1225 */
1226 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1227 chip->read_buf(mtd, buffer, strlen(fingerprint));
1228
1229 /* Look for the fingerprint. */
1230 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1231 found_an_ncb_fingerprint = true;
1232 break;
1233 }
1234
1235 }
1236
1237 chip->select_chip(mtd, saved_chip_number);
1238
1239 if (found_an_ncb_fingerprint)
1240 dev_dbg(dev, "\tFound a fingerprint\n");
1241 else
1242 dev_dbg(dev, "\tNo fingerprint found\n");
1243 return found_an_ncb_fingerprint;
1244}
1245
1246/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001247static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001248{
1249 struct device *dev = this->dev;
1250 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1251 struct mtd_info *mtd = &this->mtd;
1252 struct nand_chip *chip = &this->nand;
1253 unsigned int block_size_in_pages;
1254 unsigned int search_area_size_in_strides;
1255 unsigned int search_area_size_in_pages;
1256 unsigned int search_area_size_in_blocks;
1257 unsigned int block;
1258 unsigned int stride;
1259 unsigned int page;
1260 loff_t byte;
1261 uint8_t *buffer = chip->buffers->databuf;
1262 int saved_chip_number;
1263 int status;
1264
1265 /* Compute the search area geometry. */
1266 block_size_in_pages = mtd->erasesize / mtd->writesize;
1267 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1268 search_area_size_in_pages = search_area_size_in_strides *
1269 rom_geo->stride_size_in_pages;
1270 search_area_size_in_blocks =
1271 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1272 block_size_in_pages;
1273
1274 dev_dbg(dev, "Search Area Geometry :\n");
1275 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1276 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1277 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1278
1279 /* Select chip 0. */
1280 saved_chip_number = this->current_chip;
1281 chip->select_chip(mtd, 0);
1282
1283 /* Loop over blocks in the first search area, erasing them. */
1284 dev_dbg(dev, "Erasing the search area...\n");
1285
1286 for (block = 0; block < search_area_size_in_blocks; block++) {
1287 /* Compute the page address. */
1288 page = block * block_size_in_pages;
1289
1290 /* Erase this block. */
1291 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1292 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1293 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1294
1295 /* Wait for the erase to finish. */
1296 status = chip->waitfunc(mtd, chip);
1297 if (status & NAND_STATUS_FAIL)
1298 dev_err(dev, "[%s] Erase failed.\n", __func__);
1299 }
1300
1301 /* Write the NCB fingerprint into the page buffer. */
1302 memset(buffer, ~0, mtd->writesize);
1303 memset(chip->oob_poi, ~0, mtd->oobsize);
1304 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1305
1306 /* Loop through the first search area, writing NCB fingerprints. */
1307 dev_dbg(dev, "Writing NCB fingerprints...\n");
1308 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1309 /* Compute the page and byte addresses. */
1310 page = stride * rom_geo->stride_size_in_pages;
1311 byte = page * mtd->writesize;
1312
1313 /* Write the first page of the current stride. */
1314 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1315 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001316 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001317 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1318
1319 /* Wait for the write to finish. */
1320 status = chip->waitfunc(mtd, chip);
1321 if (status & NAND_STATUS_FAIL)
1322 dev_err(dev, "[%s] Write failed.\n", __func__);
1323 }
1324
1325 /* Deselect chip 0. */
1326 chip->select_chip(mtd, saved_chip_number);
1327 return 0;
1328}
1329
Wolfram Sanga78da282012-03-21 19:29:17 +01001330static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001331{
1332 struct device *dev = this->dev;
1333 struct nand_chip *chip = &this->nand;
1334 struct mtd_info *mtd = &this->mtd;
1335 unsigned int block_count;
1336 unsigned int block;
1337 int chipnr;
1338 int page;
1339 loff_t byte;
1340 uint8_t block_mark;
1341 int ret = 0;
1342
1343 /*
1344 * If control arrives here, we can't use block mark swapping, which
1345 * means we're forced to use transcription. First, scan for the
1346 * transcription stamp. If we find it, then we don't have to do
1347 * anything -- the block marks are already transcribed.
1348 */
1349 if (mx23_check_transcription_stamp(this))
1350 return 0;
1351
1352 /*
1353 * If control arrives here, we couldn't find a transcription stamp, so
1354 * so we presume the block marks are in the conventional location.
1355 */
1356 dev_dbg(dev, "Transcribing bad block marks...\n");
1357
1358 /* Compute the number of blocks in the entire medium. */
1359 block_count = chip->chipsize >> chip->phys_erase_shift;
1360
1361 /*
1362 * Loop over all the blocks in the medium, transcribing block marks as
1363 * we go.
1364 */
1365 for (block = 0; block < block_count; block++) {
1366 /*
1367 * Compute the chip, page and byte addresses for this block's
1368 * conventional mark.
1369 */
1370 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1371 page = block << (chip->phys_erase_shift - chip->page_shift);
1372 byte = block << chip->phys_erase_shift;
1373
1374 /* Send the command to read the conventional block mark. */
1375 chip->select_chip(mtd, chipnr);
1376 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1377 block_mark = chip->read_byte(mtd);
1378 chip->select_chip(mtd, -1);
1379
1380 /*
1381 * Check if the block is marked bad. If so, we need to mark it
1382 * again, but this time the result will be a mark in the
1383 * location where we transcribe block marks.
1384 */
1385 if (block_mark != 0xff) {
1386 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1387 ret = chip->block_markbad(mtd, byte);
1388 if (ret)
1389 dev_err(dev, "Failed to mark block bad with "
1390 "ret %d\n", ret);
1391 }
1392 }
1393
1394 /* Write the stamp that indicates we've transcribed the block marks. */
1395 mx23_write_transcription_stamp(this);
1396 return 0;
1397}
1398
Wolfram Sanga78da282012-03-21 19:29:17 +01001399static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001400{
1401 nand_boot_set_geometry(this);
1402
1403 /* This is ROM arch-specific initilization before the BBT scanning. */
1404 if (GPMI_IS_MX23(this))
1405 return mx23_boot_init(this);
1406 return 0;
1407}
1408
Wolfram Sanga78da282012-03-21 19:29:17 +01001409static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001410{
1411 int ret;
1412
1413 /* Free the temporary DMA memory for reading ID. */
1414 gpmi_free_dma_buffer(this);
1415
1416 /* Set up the NFC geometry which is used by BCH. */
1417 ret = bch_set_geometry(this);
1418 if (ret) {
1419 pr_err("set geometry ret : %d\n", ret);
1420 return ret;
1421 }
1422
1423 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1424 return gpmi_alloc_dma_buffer(this);
1425}
1426
1427static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1428{
1429 int ret;
1430
1431 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1432 if (GPMI_IS_MX23(this))
1433 this->swap_block_mark = false;
1434 else
1435 this->swap_block_mark = true;
1436
1437 /* Set up the medium geometry */
1438 ret = gpmi_set_geometry(this);
1439 if (ret)
1440 return ret;
1441
Marek Vasut5636ce02012-05-21 22:59:27 +02001442 /* Adjust the ECC strength according to the chip. */
1443 this->nand.ecc.strength = this->bch_geometry.ecc_strength;
1444 this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
Huang Shijiee0dd89c2012-07-03 16:24:33 +08001445 this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
Marek Vasut5636ce02012-05-21 22:59:27 +02001446
Huang Shijie10a2bca2011-09-08 10:47:09 +08001447 /* NAND boot init, depends on the gpmi_set_geometry(). */
1448 return nand_boot_init(this);
1449}
1450
1451static int gpmi_scan_bbt(struct mtd_info *mtd)
1452{
1453 struct nand_chip *chip = mtd->priv;
1454 struct gpmi_nand_data *this = chip->priv;
1455 int ret;
1456
1457 /* Prepare for the BBT scan. */
1458 ret = gpmi_pre_bbt_scan(this);
1459 if (ret)
1460 return ret;
1461
1462 /* use the default BBT implementation */
1463 return nand_default_bbt(mtd);
1464}
1465
1466void gpmi_nfc_exit(struct gpmi_nand_data *this)
1467{
1468 nand_release(&this->mtd);
1469 gpmi_free_dma_buffer(this);
1470}
1471
1472static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
1473{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001474 struct mtd_info *mtd = &this->mtd;
1475 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001476 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001477 int ret;
1478
1479 /* init current chip */
1480 this->current_chip = -1;
1481
1482 /* init the MTD data structures */
1483 mtd->priv = chip;
1484 mtd->name = "gpmi-nand";
1485 mtd->owner = THIS_MODULE;
1486
1487 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1488 chip->priv = this;
1489 chip->select_chip = gpmi_select_chip;
1490 chip->cmd_ctrl = gpmi_cmd_ctrl;
1491 chip->dev_ready = gpmi_dev_ready;
1492 chip->read_byte = gpmi_read_byte;
1493 chip->read_buf = gpmi_read_buf;
1494 chip->write_buf = gpmi_write_buf;
1495 chip->ecc.read_page = gpmi_ecc_read_page;
1496 chip->ecc.write_page = gpmi_ecc_write_page;
1497 chip->ecc.read_oob = gpmi_ecc_read_oob;
1498 chip->ecc.write_oob = gpmi_ecc_write_oob;
1499 chip->scan_bbt = gpmi_scan_bbt;
1500 chip->badblock_pattern = &gpmi_bbt_descr;
1501 chip->block_markbad = gpmi_block_markbad;
1502 chip->options |= NAND_NO_SUBPAGE_WRITE;
1503 chip->ecc.mode = NAND_ECC_HW;
1504 chip->ecc.size = 1;
Marek Vasut5636ce02012-05-21 22:59:27 +02001505 chip->ecc.strength = 8;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001506 chip->ecc.layout = &gpmi_hw_ecclayout;
Huang Shijiec50c6942012-07-03 16:24:32 +08001507 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1508 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001509
1510 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1511 this->bch_geometry.payload_size = 1024;
1512 this->bch_geometry.auxiliary_size = 128;
1513 ret = gpmi_alloc_dma_buffer(this);
1514 if (ret)
1515 goto err_out;
1516
Huang Shijiee10db1f2012-05-04 21:42:05 -04001517 ret = nand_scan(mtd, 1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001518 if (ret) {
1519 pr_err("Chip scan failed\n");
1520 goto err_out;
1521 }
1522
Huang Shijiee10db1f2012-05-04 21:42:05 -04001523 ppdata.of_node = this->pdev->dev.of_node;
1524 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001525 if (ret)
1526 goto err_out;
1527 return 0;
1528
1529err_out:
1530 gpmi_nfc_exit(this);
1531 return ret;
1532}
1533
Huang Shijiee10db1f2012-05-04 21:42:05 -04001534static const struct platform_device_id gpmi_ids[] = {
1535 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1536 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001537 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Huang Shijiee10db1f2012-05-04 21:42:05 -04001538 {},
1539};
1540
1541static const struct of_device_id gpmi_nand_id_table[] = {
1542 {
1543 .compatible = "fsl,imx23-gpmi-nand",
1544 .data = (void *)&gpmi_ids[IS_MX23]
1545 }, {
1546 .compatible = "fsl,imx28-gpmi-nand",
1547 .data = (void *)&gpmi_ids[IS_MX28]
Huang Shijie9013bb42012-05-04 21:42:06 -04001548 }, {
1549 .compatible = "fsl,imx6q-gpmi-nand",
1550 .data = (void *)&gpmi_ids[IS_MX6Q]
Huang Shijiee10db1f2012-05-04 21:42:05 -04001551 }, {}
1552};
1553MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1554
Huang Shijie10a2bca2011-09-08 10:47:09 +08001555static int __devinit gpmi_nand_probe(struct platform_device *pdev)
1556{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001557 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001558 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001559 int ret;
1560
Huang Shijiee10db1f2012-05-04 21:42:05 -04001561 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1562 if (of_id) {
1563 pdev->id_entry = of_id->data;
1564 } else {
1565 pr_err("Failed to find the right device id.\n");
1566 return -ENOMEM;
1567 }
1568
Huang Shijie10a2bca2011-09-08 10:47:09 +08001569 this = kzalloc(sizeof(*this), GFP_KERNEL);
1570 if (!this) {
1571 pr_err("Failed to allocate per-device memory\n");
1572 return -ENOMEM;
1573 }
1574
1575 platform_set_drvdata(pdev, this);
1576 this->pdev = pdev;
1577 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001578
1579 ret = acquire_resources(this);
1580 if (ret)
1581 goto exit_acquire_resources;
1582
1583 ret = init_hardware(this);
1584 if (ret)
1585 goto exit_nfc_init;
1586
1587 ret = gpmi_nfc_init(this);
1588 if (ret)
1589 goto exit_nfc_init;
1590
1591 return 0;
1592
1593exit_nfc_init:
1594 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001595exit_acquire_resources:
1596 platform_set_drvdata(pdev, NULL);
1597 kfree(this);
1598 return ret;
1599}
1600
1601static int __exit gpmi_nand_remove(struct platform_device *pdev)
1602{
1603 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1604
1605 gpmi_nfc_exit(this);
1606 release_resources(this);
1607 platform_set_drvdata(pdev, NULL);
1608 kfree(this);
1609 return 0;
1610}
1611
Huang Shijie10a2bca2011-09-08 10:47:09 +08001612static struct platform_driver gpmi_nand_driver = {
1613 .driver = {
1614 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001615 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001616 },
1617 .probe = gpmi_nand_probe,
1618 .remove = __exit_p(gpmi_nand_remove),
1619 .id_table = gpmi_ids,
1620};
1621
1622static int __init gpmi_nand_init(void)
1623{
1624 int err;
1625
1626 err = platform_driver_register(&gpmi_nand_driver);
1627 if (err == 0)
1628 printk(KERN_INFO "GPMI NAND driver registered. (IMX)\n");
1629 else
1630 pr_err("i.MX GPMI NAND driver registration failed\n");
1631 return err;
1632}
1633
1634static void __exit gpmi_nand_exit(void)
1635{
1636 platform_driver_unregister(&gpmi_nand_driver);
1637}
1638
1639module_init(gpmi_nand_init);
1640module_exit(gpmi_nand_exit);
1641
1642MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1643MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1644MODULE_LICENSE("GPL");