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