blob: e2c56fc4574be517b87e437a635da83cc91e5122 [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)
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530225 pr_err("DMA mapping failed.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800226
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) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530459 pr_err("Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400460 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) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530628 pr_err("%s, Alternate buffer is too small\n",
629 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800630 return -ENOMEM;
631 }
632 goto map_failed;
633 }
634 *use_virt = destination;
635 *use_phys = dest_phys;
636 this->direct_dma_map_ok = true;
637 return 0;
638 }
639
640map_failed:
641 *use_virt = alt_virt;
642 *use_phys = alt_phys;
643 this->direct_dma_map_ok = false;
644 return 0;
645}
646
647static inline void read_page_end(struct gpmi_nand_data *this,
648 void *destination, unsigned length,
649 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
650 void *used_virt, dma_addr_t used_phys)
651{
652 if (this->direct_dma_map_ok)
653 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
654}
655
656static inline void read_page_swap_end(struct gpmi_nand_data *this,
657 void *destination, unsigned length,
658 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
659 void *used_virt, dma_addr_t used_phys)
660{
661 if (!this->direct_dma_map_ok)
662 memcpy(destination, alt_virt, length);
663}
664
665static int send_page_prepare(struct gpmi_nand_data *this,
666 const void *source, unsigned length,
667 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
668 const void **use_virt, dma_addr_t *use_phys)
669{
670 struct device *dev = this->dev;
671
672 if (virt_addr_valid(source)) {
673 dma_addr_t source_phys;
674
675 source_phys = dma_map_single(dev, (void *)source, length,
676 DMA_TO_DEVICE);
677 if (dma_mapping_error(dev, source_phys)) {
678 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530679 pr_err("%s, Alternate buffer is too small\n",
680 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800681 return -ENOMEM;
682 }
683 goto map_failed;
684 }
685 *use_virt = source;
686 *use_phys = source_phys;
687 return 0;
688 }
689map_failed:
690 /*
691 * Copy the content of the source buffer into the alternate
692 * buffer and set up the return values accordingly.
693 */
694 memcpy(alt_virt, source, length);
695
696 *use_virt = alt_virt;
697 *use_phys = alt_phys;
698 return 0;
699}
700
701static void send_page_end(struct gpmi_nand_data *this,
702 const void *source, unsigned length,
703 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
704 const void *used_virt, dma_addr_t used_phys)
705{
706 struct device *dev = this->dev;
707 if (used_virt == source)
708 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
709}
710
711static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
712{
713 struct device *dev = this->dev;
714
715 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
716 dma_free_coherent(dev, this->page_buffer_size,
717 this->page_buffer_virt,
718 this->page_buffer_phys);
719 kfree(this->cmd_buffer);
720 kfree(this->data_buffer_dma);
721
722 this->cmd_buffer = NULL;
723 this->data_buffer_dma = NULL;
724 this->page_buffer_virt = NULL;
725 this->page_buffer_size = 0;
726}
727
728/* Allocate the DMA buffers */
729static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
730{
731 struct bch_geometry *geo = &this->bch_geometry;
732 struct device *dev = this->dev;
733
734 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800735 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800736 if (this->cmd_buffer == NULL)
737 goto error_alloc;
738
739 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800740 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800741 if (this->data_buffer_dma == NULL)
742 goto error_alloc;
743
744 /*
745 * [3] Allocate the page buffer.
746 *
747 * Both the payload buffer and the auxiliary buffer must appear on
748 * 32-bit boundaries. We presume the size of the payload buffer is a
749 * power of two and is much larger than four, which guarantees the
750 * auxiliary buffer will appear on a 32-bit boundary.
751 */
752 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
753 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
754 &this->page_buffer_phys, GFP_DMA);
755 if (!this->page_buffer_virt)
756 goto error_alloc;
757
758
759 /* Slice up the page buffer. */
760 this->payload_virt = this->page_buffer_virt;
761 this->payload_phys = this->page_buffer_phys;
762 this->auxiliary_virt = this->payload_virt + geo->payload_size;
763 this->auxiliary_phys = this->payload_phys + geo->payload_size;
764 return 0;
765
766error_alloc:
767 gpmi_free_dma_buffer(this);
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530768 pr_err("Error allocating DMA buffers!\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800769 return -ENOMEM;
770}
771
772static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
773{
774 struct nand_chip *chip = mtd->priv;
775 struct gpmi_nand_data *this = chip->priv;
776 int ret;
777
778 /*
779 * Every operation begins with a command byte and a series of zero or
780 * more address bytes. These are distinguished by either the Address
781 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
782 * asserted. When MTD is ready to execute the command, it will deassert
783 * both latch enables.
784 *
785 * Rather than run a separate DMA operation for every single byte, we
786 * queue them up and run a single DMA operation for the entire series
787 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
788 */
789 if ((ctrl & (NAND_ALE | NAND_CLE))) {
790 if (data != NAND_CMD_NONE)
791 this->cmd_buffer[this->command_length++] = data;
792 return;
793 }
794
795 if (!this->command_length)
796 return;
797
798 ret = gpmi_send_command(this);
799 if (ret)
800 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
801
802 this->command_length = 0;
803}
804
805static int gpmi_dev_ready(struct mtd_info *mtd)
806{
807 struct nand_chip *chip = mtd->priv;
808 struct gpmi_nand_data *this = chip->priv;
809
810 return gpmi_is_ready(this, this->current_chip);
811}
812
813static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
814{
815 struct nand_chip *chip = mtd->priv;
816 struct gpmi_nand_data *this = chip->priv;
817
818 if ((this->current_chip < 0) && (chipnr >= 0))
819 gpmi_begin(this);
820 else if ((this->current_chip >= 0) && (chipnr < 0))
821 gpmi_end(this);
822
823 this->current_chip = chipnr;
824}
825
826static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
827{
828 struct nand_chip *chip = mtd->priv;
829 struct gpmi_nand_data *this = chip->priv;
830
831 pr_debug("len is %d\n", len);
832 this->upper_buf = buf;
833 this->upper_len = len;
834
835 gpmi_read_data(this);
836}
837
838static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
839{
840 struct nand_chip *chip = mtd->priv;
841 struct gpmi_nand_data *this = chip->priv;
842
843 pr_debug("len is %d\n", len);
844 this->upper_buf = (uint8_t *)buf;
845 this->upper_len = len;
846
847 gpmi_send_data(this);
848}
849
850static uint8_t gpmi_read_byte(struct mtd_info *mtd)
851{
852 struct nand_chip *chip = mtd->priv;
853 struct gpmi_nand_data *this = chip->priv;
854 uint8_t *buf = this->data_buffer_dma;
855
856 gpmi_read_buf(mtd, buf, 1);
857 return buf[0];
858}
859
860/*
861 * Handles block mark swapping.
862 * It can be called in swapping the block mark, or swapping it back,
863 * because the the operations are the same.
864 */
865static void block_mark_swapping(struct gpmi_nand_data *this,
866 void *payload, void *auxiliary)
867{
868 struct bch_geometry *nfc_geo = &this->bch_geometry;
869 unsigned char *p;
870 unsigned char *a;
871 unsigned int bit;
872 unsigned char mask;
873 unsigned char from_data;
874 unsigned char from_oob;
875
876 if (!this->swap_block_mark)
877 return;
878
879 /*
880 * If control arrives here, we're swapping. Make some convenience
881 * variables.
882 */
883 bit = nfc_geo->block_mark_bit_offset;
884 p = payload + nfc_geo->block_mark_byte_offset;
885 a = auxiliary;
886
887 /*
888 * Get the byte from the data area that overlays the block mark. Since
889 * the ECC engine applies its own view to the bits in the page, the
890 * physical block mark won't (in general) appear on a byte boundary in
891 * the data.
892 */
893 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
894
895 /* Get the byte from the OOB. */
896 from_oob = a[0];
897
898 /* Swap them. */
899 a[0] = from_data;
900
901 mask = (0x1 << bit) - 1;
902 p[0] = (p[0] & mask) | (from_oob << bit);
903
904 mask = ~0 << bit;
905 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
906}
907
908static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700909 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800910{
911 struct gpmi_nand_data *this = chip->priv;
912 struct bch_geometry *nfc_geo = &this->bch_geometry;
913 void *payload_virt;
914 dma_addr_t payload_phys;
915 void *auxiliary_virt;
916 dma_addr_t auxiliary_phys;
917 unsigned int i;
918 unsigned char *status;
919 unsigned int failed;
920 unsigned int corrected;
921 int ret;
922
923 pr_debug("page number is : %d\n", page);
924 ret = read_page_prepare(this, buf, mtd->writesize,
925 this->payload_virt, this->payload_phys,
926 nfc_geo->payload_size,
927 &payload_virt, &payload_phys);
928 if (ret) {
929 pr_err("Inadequate DMA buffer\n");
930 ret = -ENOMEM;
931 return ret;
932 }
933 auxiliary_virt = this->auxiliary_virt;
934 auxiliary_phys = this->auxiliary_phys;
935
936 /* go! */
937 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
938 read_page_end(this, buf, mtd->writesize,
939 this->payload_virt, this->payload_phys,
940 nfc_geo->payload_size,
941 payload_virt, payload_phys);
942 if (ret) {
943 pr_err("Error in ECC-based read: %d\n", ret);
944 goto exit_nfc;
945 }
946
947 /* handle the block mark swapping */
948 block_mark_swapping(this, payload_virt, auxiliary_virt);
949
950 /* Loop over status bytes, accumulating ECC status. */
951 failed = 0;
952 corrected = 0;
953 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
954
955 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
956 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
957 continue;
958
959 if (*status == STATUS_UNCORRECTABLE) {
960 failed++;
961 continue;
962 }
963 corrected += *status;
964 }
965
966 /*
967 * Propagate ECC status to the owning MTD only when failed or
968 * corrected times nearly reaches our ECC correction threshold.
969 */
970 if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
971 mtd->ecc_stats.failed += failed;
972 mtd->ecc_stats.corrected += corrected;
973 }
974
Brian Norris7725cc82012-05-02 10:15:02 -0700975 if (oob_required) {
976 /*
977 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
978 * for details about our policy for delivering the OOB.
979 *
980 * We fill the caller's buffer with set bits, and then copy the
981 * block mark to th caller's buffer. Note that, if block mark
982 * swapping was necessary, it has already been done, so we can
983 * rely on the first byte of the auxiliary buffer to contain
984 * the block mark.
985 */
986 memset(chip->oob_poi, ~0, mtd->oobsize);
987 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -0700988 }
Sascha Hauer6023813a2012-06-26 17:26:16 +0200989
990 read_page_swap_end(this, buf, mtd->writesize,
991 this->payload_virt, this->payload_phys,
992 nfc_geo->payload_size,
993 payload_virt, payload_phys);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800994exit_nfc:
995 return ret;
996}
997
Josh Wufdbad98d2012-06-25 18:07:45 +0800998static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700999 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001000{
1001 struct gpmi_nand_data *this = chip->priv;
1002 struct bch_geometry *nfc_geo = &this->bch_geometry;
1003 const void *payload_virt;
1004 dma_addr_t payload_phys;
1005 const void *auxiliary_virt;
1006 dma_addr_t auxiliary_phys;
1007 int ret;
1008
1009 pr_debug("ecc write page.\n");
1010 if (this->swap_block_mark) {
1011 /*
1012 * If control arrives here, we're doing block mark swapping.
1013 * Since we can't modify the caller's buffers, we must copy them
1014 * into our own.
1015 */
1016 memcpy(this->payload_virt, buf, mtd->writesize);
1017 payload_virt = this->payload_virt;
1018 payload_phys = this->payload_phys;
1019
1020 memcpy(this->auxiliary_virt, chip->oob_poi,
1021 nfc_geo->auxiliary_size);
1022 auxiliary_virt = this->auxiliary_virt;
1023 auxiliary_phys = this->auxiliary_phys;
1024
1025 /* Handle block mark swapping. */
1026 block_mark_swapping(this,
1027 (void *) payload_virt, (void *) auxiliary_virt);
1028 } else {
1029 /*
1030 * If control arrives here, we're not doing block mark swapping,
1031 * so we can to try and use the caller's buffers.
1032 */
1033 ret = send_page_prepare(this,
1034 buf, mtd->writesize,
1035 this->payload_virt, this->payload_phys,
1036 nfc_geo->payload_size,
1037 &payload_virt, &payload_phys);
1038 if (ret) {
1039 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001040 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001041 }
1042
1043 ret = send_page_prepare(this,
1044 chip->oob_poi, mtd->oobsize,
1045 this->auxiliary_virt, this->auxiliary_phys,
1046 nfc_geo->auxiliary_size,
1047 &auxiliary_virt, &auxiliary_phys);
1048 if (ret) {
1049 pr_err("Inadequate auxiliary DMA buffer\n");
1050 goto exit_auxiliary;
1051 }
1052 }
1053
1054 /* Ask the NFC. */
1055 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1056 if (ret)
1057 pr_err("Error in ECC-based write: %d\n", ret);
1058
1059 if (!this->swap_block_mark) {
1060 send_page_end(this, chip->oob_poi, mtd->oobsize,
1061 this->auxiliary_virt, this->auxiliary_phys,
1062 nfc_geo->auxiliary_size,
1063 auxiliary_virt, auxiliary_phys);
1064exit_auxiliary:
1065 send_page_end(this, buf, mtd->writesize,
1066 this->payload_virt, this->payload_phys,
1067 nfc_geo->payload_size,
1068 payload_virt, payload_phys);
1069 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001070
1071 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001072}
1073
1074/*
1075 * There are several places in this driver where we have to handle the OOB and
1076 * block marks. This is the function where things are the most complicated, so
1077 * this is where we try to explain it all. All the other places refer back to
1078 * here.
1079 *
1080 * These are the rules, in order of decreasing importance:
1081 *
1082 * 1) Nothing the caller does can be allowed to imperil the block mark.
1083 *
1084 * 2) In read operations, the first byte of the OOB we return must reflect the
1085 * true state of the block mark, no matter where that block mark appears in
1086 * the physical page.
1087 *
1088 * 3) ECC-based read operations return an OOB full of set bits (since we never
1089 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1090 * return).
1091 *
1092 * 4) "Raw" read operations return a direct view of the physical bytes in the
1093 * page, using the conventional definition of which bytes are data and which
1094 * are OOB. This gives the caller a way to see the actual, physical bytes
1095 * in the page, without the distortions applied by our ECC engine.
1096 *
1097 *
1098 * What we do for this specific read operation depends on two questions:
1099 *
1100 * 1) Are we doing a "raw" read, or an ECC-based read?
1101 *
1102 * 2) Are we using block mark swapping or transcription?
1103 *
1104 * There are four cases, illustrated by the following Karnaugh map:
1105 *
1106 * | Raw | ECC-based |
1107 * -------------+-------------------------+-------------------------+
1108 * | Read the conventional | |
1109 * | OOB at the end of the | |
1110 * Swapping | page and return it. It | |
1111 * | contains exactly what | |
1112 * | we want. | Read the block mark and |
1113 * -------------+-------------------------+ return it in a buffer |
1114 * | Read the conventional | full of set bits. |
1115 * | OOB at the end of the | |
1116 * | page and also the block | |
1117 * Transcribing | mark in the metadata. | |
1118 * | Copy the block mark | |
1119 * | into the first byte of | |
1120 * | the OOB. | |
1121 * -------------+-------------------------+-------------------------+
1122 *
1123 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1124 * giving an accurate view of the actual, physical bytes in the page (we're
1125 * overwriting the block mark). That's OK because it's more important to follow
1126 * rule #2.
1127 *
1128 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1129 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1130 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1131 * ECC-based or raw view of the page is implicit in which function it calls
1132 * (there is a similar pair of ECC-based/raw functions for writing).
1133 *
Brian Norris271b874b2012-05-11 13:30:35 -07001134 * FIXME: The following paragraph is incorrect, now that there exist
1135 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1136 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001137 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1138 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1139 * caller wants an ECC-based or raw view of the page is not propagated down to
1140 * this driver.
1141 */
1142static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001143 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001144{
1145 struct gpmi_nand_data *this = chip->priv;
1146
1147 pr_debug("page number is %d\n", page);
1148 /* clear the OOB buffer */
1149 memset(chip->oob_poi, ~0, mtd->oobsize);
1150
1151 /* Read out the conventional OOB. */
1152 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1153 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1154
1155 /*
1156 * Now, we want to make sure the block mark is correct. In the
1157 * Swapping/Raw case, we already have it. Otherwise, we need to
1158 * explicitly read it.
1159 */
1160 if (!this->swap_block_mark) {
1161 /* Read the block mark into the first byte of the OOB buffer. */
1162 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1163 chip->oob_poi[0] = chip->read_byte(mtd);
1164 }
1165
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001166 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001167}
1168
1169static int
1170gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1171{
1172 /*
1173 * The BCH will use all the (page + oob).
1174 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1175 * But it can not stop some ioctls such MEMWRITEOOB which uses
Brian Norris0612b9d2011-08-30 18:45:40 -07001176 * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
Huang Shijie10a2bca2011-09-08 10:47:09 +08001177 * these ioctls too.
1178 */
1179 return -EPERM;
1180}
1181
1182static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1183{
1184 struct nand_chip *chip = mtd->priv;
1185 struct gpmi_nand_data *this = chip->priv;
1186 int block, ret = 0;
1187 uint8_t *block_mark;
1188 int column, page, status, chipnr;
1189
1190 /* Get block number */
1191 block = (int)(ofs >> chip->bbt_erase_shift);
1192 if (chip->bbt)
1193 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1194
1195 /* Do we have a flash based bad block table ? */
Wolfram Sang52899662012-01-31 13:10:43 +01001196 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001197 ret = nand_update_bbt(mtd, ofs);
1198 else {
1199 chipnr = (int)(ofs >> chip->chip_shift);
1200 chip->select_chip(mtd, chipnr);
1201
1202 column = this->swap_block_mark ? mtd->writesize : 0;
1203
1204 /* Write the block mark. */
1205 block_mark = this->data_buffer_dma;
1206 block_mark[0] = 0; /* bad block marker */
1207
1208 /* Shift to get page */
1209 page = (int)(ofs >> chip->page_shift);
1210
1211 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1212 chip->write_buf(mtd, block_mark, 1);
1213 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1214
1215 status = chip->waitfunc(mtd, chip);
1216 if (status & NAND_STATUS_FAIL)
1217 ret = -EIO;
1218
1219 chip->select_chip(mtd, -1);
1220 }
1221 if (!ret)
1222 mtd->ecc_stats.badblocks++;
1223
1224 return ret;
1225}
1226
Wolfram Sanga78da282012-03-21 19:29:17 +01001227static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001228{
1229 struct boot_rom_geometry *geometry = &this->rom_geometry;
1230
1231 /*
1232 * Set the boot block stride size.
1233 *
1234 * In principle, we should be reading this from the OTP bits, since
1235 * that's where the ROM is going to get it. In fact, we don't have any
1236 * way to read the OTP bits, so we go with the default and hope for the
1237 * best.
1238 */
1239 geometry->stride_size_in_pages = 64;
1240
1241 /*
1242 * Set the search area stride exponent.
1243 *
1244 * In principle, we should be reading this from the OTP bits, since
1245 * that's where the ROM is going to get it. In fact, we don't have any
1246 * way to read the OTP bits, so we go with the default and hope for the
1247 * best.
1248 */
1249 geometry->search_area_stride_exponent = 2;
1250 return 0;
1251}
1252
1253static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001254static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001255{
1256 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1257 struct device *dev = this->dev;
1258 struct mtd_info *mtd = &this->mtd;
1259 struct nand_chip *chip = &this->nand;
1260 unsigned int search_area_size_in_strides;
1261 unsigned int stride;
1262 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001263 uint8_t *buffer = chip->buffers->databuf;
1264 int saved_chip_number;
1265 int found_an_ncb_fingerprint = false;
1266
1267 /* Compute the number of strides in a search area. */
1268 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1269
1270 saved_chip_number = this->current_chip;
1271 chip->select_chip(mtd, 0);
1272
1273 /*
1274 * Loop through the first search area, looking for the NCB fingerprint.
1275 */
1276 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1277
1278 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001279 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001280 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001281
1282 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1283
1284 /*
1285 * Read the NCB fingerprint. The fingerprint is four bytes long
1286 * and starts in the 12th byte of the page.
1287 */
1288 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1289 chip->read_buf(mtd, buffer, strlen(fingerprint));
1290
1291 /* Look for the fingerprint. */
1292 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1293 found_an_ncb_fingerprint = true;
1294 break;
1295 }
1296
1297 }
1298
1299 chip->select_chip(mtd, saved_chip_number);
1300
1301 if (found_an_ncb_fingerprint)
1302 dev_dbg(dev, "\tFound a fingerprint\n");
1303 else
1304 dev_dbg(dev, "\tNo fingerprint found\n");
1305 return found_an_ncb_fingerprint;
1306}
1307
1308/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001309static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001310{
1311 struct device *dev = this->dev;
1312 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1313 struct mtd_info *mtd = &this->mtd;
1314 struct nand_chip *chip = &this->nand;
1315 unsigned int block_size_in_pages;
1316 unsigned int search_area_size_in_strides;
1317 unsigned int search_area_size_in_pages;
1318 unsigned int search_area_size_in_blocks;
1319 unsigned int block;
1320 unsigned int stride;
1321 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001322 uint8_t *buffer = chip->buffers->databuf;
1323 int saved_chip_number;
1324 int status;
1325
1326 /* Compute the search area geometry. */
1327 block_size_in_pages = mtd->erasesize / mtd->writesize;
1328 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1329 search_area_size_in_pages = search_area_size_in_strides *
1330 rom_geo->stride_size_in_pages;
1331 search_area_size_in_blocks =
1332 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1333 block_size_in_pages;
1334
1335 dev_dbg(dev, "Search Area Geometry :\n");
1336 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1337 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1338 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1339
1340 /* Select chip 0. */
1341 saved_chip_number = this->current_chip;
1342 chip->select_chip(mtd, 0);
1343
1344 /* Loop over blocks in the first search area, erasing them. */
1345 dev_dbg(dev, "Erasing the search area...\n");
1346
1347 for (block = 0; block < search_area_size_in_blocks; block++) {
1348 /* Compute the page address. */
1349 page = block * block_size_in_pages;
1350
1351 /* Erase this block. */
1352 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1353 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1354 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1355
1356 /* Wait for the erase to finish. */
1357 status = chip->waitfunc(mtd, chip);
1358 if (status & NAND_STATUS_FAIL)
1359 dev_err(dev, "[%s] Erase failed.\n", __func__);
1360 }
1361
1362 /* Write the NCB fingerprint into the page buffer. */
1363 memset(buffer, ~0, mtd->writesize);
1364 memset(chip->oob_poi, ~0, mtd->oobsize);
1365 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1366
1367 /* Loop through the first search area, writing NCB fingerprints. */
1368 dev_dbg(dev, "Writing NCB fingerprints...\n");
1369 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001370 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001371 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001372
1373 /* Write the first page of the current stride. */
1374 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1375 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001376 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001377 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1378
1379 /* Wait for the write to finish. */
1380 status = chip->waitfunc(mtd, chip);
1381 if (status & NAND_STATUS_FAIL)
1382 dev_err(dev, "[%s] Write failed.\n", __func__);
1383 }
1384
1385 /* Deselect chip 0. */
1386 chip->select_chip(mtd, saved_chip_number);
1387 return 0;
1388}
1389
Wolfram Sanga78da282012-03-21 19:29:17 +01001390static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001391{
1392 struct device *dev = this->dev;
1393 struct nand_chip *chip = &this->nand;
1394 struct mtd_info *mtd = &this->mtd;
1395 unsigned int block_count;
1396 unsigned int block;
1397 int chipnr;
1398 int page;
1399 loff_t byte;
1400 uint8_t block_mark;
1401 int ret = 0;
1402
1403 /*
1404 * If control arrives here, we can't use block mark swapping, which
1405 * means we're forced to use transcription. First, scan for the
1406 * transcription stamp. If we find it, then we don't have to do
1407 * anything -- the block marks are already transcribed.
1408 */
1409 if (mx23_check_transcription_stamp(this))
1410 return 0;
1411
1412 /*
1413 * If control arrives here, we couldn't find a transcription stamp, so
1414 * so we presume the block marks are in the conventional location.
1415 */
1416 dev_dbg(dev, "Transcribing bad block marks...\n");
1417
1418 /* Compute the number of blocks in the entire medium. */
1419 block_count = chip->chipsize >> chip->phys_erase_shift;
1420
1421 /*
1422 * Loop over all the blocks in the medium, transcribing block marks as
1423 * we go.
1424 */
1425 for (block = 0; block < block_count; block++) {
1426 /*
1427 * Compute the chip, page and byte addresses for this block's
1428 * conventional mark.
1429 */
1430 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1431 page = block << (chip->phys_erase_shift - chip->page_shift);
1432 byte = block << chip->phys_erase_shift;
1433
1434 /* Send the command to read the conventional block mark. */
1435 chip->select_chip(mtd, chipnr);
1436 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1437 block_mark = chip->read_byte(mtd);
1438 chip->select_chip(mtd, -1);
1439
1440 /*
1441 * Check if the block is marked bad. If so, we need to mark it
1442 * again, but this time the result will be a mark in the
1443 * location where we transcribe block marks.
1444 */
1445 if (block_mark != 0xff) {
1446 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1447 ret = chip->block_markbad(mtd, byte);
1448 if (ret)
1449 dev_err(dev, "Failed to mark block bad with "
1450 "ret %d\n", ret);
1451 }
1452 }
1453
1454 /* Write the stamp that indicates we've transcribed the block marks. */
1455 mx23_write_transcription_stamp(this);
1456 return 0;
1457}
1458
Wolfram Sanga78da282012-03-21 19:29:17 +01001459static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001460{
1461 nand_boot_set_geometry(this);
1462
1463 /* This is ROM arch-specific initilization before the BBT scanning. */
1464 if (GPMI_IS_MX23(this))
1465 return mx23_boot_init(this);
1466 return 0;
1467}
1468
Wolfram Sanga78da282012-03-21 19:29:17 +01001469static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001470{
1471 int ret;
1472
1473 /* Free the temporary DMA memory for reading ID. */
1474 gpmi_free_dma_buffer(this);
1475
1476 /* Set up the NFC geometry which is used by BCH. */
1477 ret = bch_set_geometry(this);
1478 if (ret) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +05301479 pr_err("Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001480 return ret;
1481 }
1482
1483 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1484 return gpmi_alloc_dma_buffer(this);
1485}
1486
1487static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1488{
1489 int ret;
1490
1491 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1492 if (GPMI_IS_MX23(this))
1493 this->swap_block_mark = false;
1494 else
1495 this->swap_block_mark = true;
1496
1497 /* Set up the medium geometry */
1498 ret = gpmi_set_geometry(this);
1499 if (ret)
1500 return ret;
1501
Marek Vasut5636ce02012-05-21 22:59:27 +02001502 /* Adjust the ECC strength according to the chip. */
1503 this->nand.ecc.strength = this->bch_geometry.ecc_strength;
1504 this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
Huang Shijiee0dd89c2012-07-03 16:24:33 +08001505 this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
Marek Vasut5636ce02012-05-21 22:59:27 +02001506
Huang Shijie10a2bca2011-09-08 10:47:09 +08001507 /* NAND boot init, depends on the gpmi_set_geometry(). */
1508 return nand_boot_init(this);
1509}
1510
1511static int gpmi_scan_bbt(struct mtd_info *mtd)
1512{
1513 struct nand_chip *chip = mtd->priv;
1514 struct gpmi_nand_data *this = chip->priv;
1515 int ret;
1516
1517 /* Prepare for the BBT scan. */
1518 ret = gpmi_pre_bbt_scan(this);
1519 if (ret)
1520 return ret;
1521
Huang Shijie995fbbf2012-09-13 14:57:59 +08001522 /*
1523 * Can we enable the extra features? such as EDO or Sync mode.
1524 *
1525 * We do not check the return value now. That's means if we fail in
1526 * enable the extra features, we still can run in the normal way.
1527 */
1528 gpmi_extra_init(this);
1529
Huang Shijie10a2bca2011-09-08 10:47:09 +08001530 /* use the default BBT implementation */
1531 return nand_default_bbt(mtd);
1532}
1533
Huang Shijie513d57e2012-07-17 14:14:02 +08001534static void gpmi_nfc_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001535{
1536 nand_release(&this->mtd);
1537 gpmi_free_dma_buffer(this);
1538}
1539
1540static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
1541{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001542 struct mtd_info *mtd = &this->mtd;
1543 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001544 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001545 int ret;
1546
1547 /* init current chip */
1548 this->current_chip = -1;
1549
1550 /* init the MTD data structures */
1551 mtd->priv = chip;
1552 mtd->name = "gpmi-nand";
1553 mtd->owner = THIS_MODULE;
1554
1555 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1556 chip->priv = this;
1557 chip->select_chip = gpmi_select_chip;
1558 chip->cmd_ctrl = gpmi_cmd_ctrl;
1559 chip->dev_ready = gpmi_dev_ready;
1560 chip->read_byte = gpmi_read_byte;
1561 chip->read_buf = gpmi_read_buf;
1562 chip->write_buf = gpmi_write_buf;
1563 chip->ecc.read_page = gpmi_ecc_read_page;
1564 chip->ecc.write_page = gpmi_ecc_write_page;
1565 chip->ecc.read_oob = gpmi_ecc_read_oob;
1566 chip->ecc.write_oob = gpmi_ecc_write_oob;
1567 chip->scan_bbt = gpmi_scan_bbt;
1568 chip->badblock_pattern = &gpmi_bbt_descr;
1569 chip->block_markbad = gpmi_block_markbad;
1570 chip->options |= NAND_NO_SUBPAGE_WRITE;
1571 chip->ecc.mode = NAND_ECC_HW;
1572 chip->ecc.size = 1;
Marek Vasut5636ce02012-05-21 22:59:27 +02001573 chip->ecc.strength = 8;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001574 chip->ecc.layout = &gpmi_hw_ecclayout;
Huang Shijiec50c6942012-07-03 16:24:32 +08001575 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1576 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001577
1578 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1579 this->bch_geometry.payload_size = 1024;
1580 this->bch_geometry.auxiliary_size = 128;
1581 ret = gpmi_alloc_dma_buffer(this);
1582 if (ret)
1583 goto err_out;
1584
Huang Shijiee10db1f2012-05-04 21:42:05 -04001585 ret = nand_scan(mtd, 1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001586 if (ret) {
1587 pr_err("Chip scan failed\n");
1588 goto err_out;
1589 }
1590
Huang Shijiee10db1f2012-05-04 21:42:05 -04001591 ppdata.of_node = this->pdev->dev.of_node;
1592 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001593 if (ret)
1594 goto err_out;
1595 return 0;
1596
1597err_out:
1598 gpmi_nfc_exit(this);
1599 return ret;
1600}
1601
Huang Shijiee10db1f2012-05-04 21:42:05 -04001602static const struct platform_device_id gpmi_ids[] = {
1603 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1604 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001605 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Huang Shijiee10db1f2012-05-04 21:42:05 -04001606 {},
1607};
1608
1609static const struct of_device_id gpmi_nand_id_table[] = {
1610 {
1611 .compatible = "fsl,imx23-gpmi-nand",
1612 .data = (void *)&gpmi_ids[IS_MX23]
1613 }, {
1614 .compatible = "fsl,imx28-gpmi-nand",
1615 .data = (void *)&gpmi_ids[IS_MX28]
Huang Shijie9013bb42012-05-04 21:42:06 -04001616 }, {
1617 .compatible = "fsl,imx6q-gpmi-nand",
1618 .data = (void *)&gpmi_ids[IS_MX6Q]
Huang Shijiee10db1f2012-05-04 21:42:05 -04001619 }, {}
1620};
1621MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1622
Huang Shijie10a2bca2011-09-08 10:47:09 +08001623static int __devinit gpmi_nand_probe(struct platform_device *pdev)
1624{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001625 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001626 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001627 int ret;
1628
Huang Shijiee10db1f2012-05-04 21:42:05 -04001629 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1630 if (of_id) {
1631 pdev->id_entry = of_id->data;
1632 } else {
1633 pr_err("Failed to find the right device id.\n");
1634 return -ENOMEM;
1635 }
1636
Huang Shijie10a2bca2011-09-08 10:47:09 +08001637 this = kzalloc(sizeof(*this), GFP_KERNEL);
1638 if (!this) {
1639 pr_err("Failed to allocate per-device memory\n");
1640 return -ENOMEM;
1641 }
1642
1643 platform_set_drvdata(pdev, this);
1644 this->pdev = pdev;
1645 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001646
1647 ret = acquire_resources(this);
1648 if (ret)
1649 goto exit_acquire_resources;
1650
1651 ret = init_hardware(this);
1652 if (ret)
1653 goto exit_nfc_init;
1654
1655 ret = gpmi_nfc_init(this);
1656 if (ret)
1657 goto exit_nfc_init;
1658
Fabio Estevam490e2802012-09-05 11:35:24 -03001659 dev_info(this->dev, "driver registered.\n");
1660
Huang Shijie10a2bca2011-09-08 10:47:09 +08001661 return 0;
1662
1663exit_nfc_init:
1664 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001665exit_acquire_resources:
1666 platform_set_drvdata(pdev, NULL);
1667 kfree(this);
Fabio Estevam490e2802012-09-05 11:35:24 -03001668 dev_err(this->dev, "driver registration failed: %d\n", ret);
1669
Huang Shijie10a2bca2011-09-08 10:47:09 +08001670 return ret;
1671}
1672
Fabio Estevam490e2802012-09-05 11:35:24 -03001673static int __devexit gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001674{
1675 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1676
1677 gpmi_nfc_exit(this);
1678 release_resources(this);
1679 platform_set_drvdata(pdev, NULL);
1680 kfree(this);
1681 return 0;
1682}
1683
Huang Shijie10a2bca2011-09-08 10:47:09 +08001684static struct platform_driver gpmi_nand_driver = {
1685 .driver = {
1686 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001687 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001688 },
1689 .probe = gpmi_nand_probe,
Fabio Estevam490e2802012-09-05 11:35:24 -03001690 .remove = __devexit_p(gpmi_nand_remove),
Huang Shijie10a2bca2011-09-08 10:47:09 +08001691 .id_table = gpmi_ids,
1692};
Fabio Estevam490e2802012-09-05 11:35:24 -03001693module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001694
1695MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1696MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1697MODULE_LICENSE("GPL");