blob: 25216785f180786168053c17a9a71e4dc9d9b571 [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/partitions.h>
Shawn Guo39febc02012-05-06 22:57:41 +080029#include <linux/pinctrl/consumer.h>
Huang Shijiee10db1f2012-05-04 21:42:05 -040030#include <linux/of.h>
31#include <linux/of_device.h>
Huang Shijiec50c6942012-07-03 16:24:32 +080032#include <linux/of_mtd.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080033#include "gpmi-nand.h"
34
Huang Shijie5de0b522012-10-13 13:03:29 -040035/* Resource names for the GPMI NAND driver. */
36#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
37#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
38#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
39#define GPMI_NAND_DMA_INTERRUPT_RES_NAME "gpmi-dma"
40
Huang Shijie10a2bca2011-09-08 10:47:09 +080041/* add our owner bbt descriptor */
42static uint8_t scan_ff_pattern[] = { 0xff };
43static struct nand_bbt_descr gpmi_bbt_descr = {
44 .options = 0,
45 .offs = 0,
46 .len = 1,
47 .pattern = scan_ff_pattern
48};
49
50/* We will use all the (page + OOB). */
51static struct nand_ecclayout gpmi_hw_ecclayout = {
52 .eccbytes = 0,
53 .eccpos = { 0, },
54 .oobfree = { {.offset = 0, .length = 0} }
55};
56
57static irqreturn_t bch_irq(int irq, void *cookie)
58{
59 struct gpmi_nand_data *this = cookie;
60
61 gpmi_clear_bch(this);
62 complete(&this->bch_done);
63 return IRQ_HANDLED;
64}
65
66/*
67 * Calculate the ECC strength by hand:
68 * E : The ECC strength.
69 * G : the length of Galois Field.
70 * N : The chunk count of per page.
71 * O : the oobsize of the NAND chip.
72 * M : the metasize of per page.
73 *
74 * The formula is :
75 * E * G * N
76 * ------------ <= (O - M)
77 * 8
78 *
79 * So, we get E by:
80 * (O - M) * 8
81 * E <= -------------
82 * G * N
83 */
84static inline int get_ecc_strength(struct gpmi_nand_data *this)
85{
86 struct bch_geometry *geo = &this->bch_geometry;
87 struct mtd_info *mtd = &this->mtd;
88 int ecc_strength;
89
90 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
91 / (geo->gf_len * geo->ecc_chunk_count);
92
93 /* We need the minor even number. */
94 return round_down(ecc_strength, 2);
95}
96
97int common_nfc_set_geometry(struct gpmi_nand_data *this)
98{
99 struct bch_geometry *geo = &this->bch_geometry;
100 struct mtd_info *mtd = &this->mtd;
101 unsigned int metadata_size;
102 unsigned int status_size;
103 unsigned int block_mark_bit_offset;
104
105 /*
106 * The size of the metadata can be changed, though we set it to 10
107 * bytes now. But it can't be too large, because we have to save
108 * enough space for BCH.
109 */
110 geo->metadata_size = 10;
111
112 /* The default for the length of Galois Field. */
113 geo->gf_len = 13;
114
Huang Shijie9ff16f02013-01-25 14:04:07 +0800115 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800116 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800117 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800118 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800119 geo->gf_len = 14;
120 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800121
122 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
123
124 /* We use the same ECC strength for all chunks. */
125 geo->ecc_strength = get_ecc_strength(this);
126 if (!geo->ecc_strength) {
Fabio Estevam3d100952012-09-05 10:27:33 -0300127 pr_err("wrong ECC strength.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800128 return -EINVAL;
129 }
130
131 geo->page_size = mtd->writesize + mtd->oobsize;
132 geo->payload_size = mtd->writesize;
133
134 /*
135 * The auxiliary buffer contains the metadata and the ECC status. The
136 * metadata is padded to the nearest 32-bit boundary. The ECC status
137 * contains one byte for every ECC chunk, and is also padded to the
138 * nearest 32-bit boundary.
139 */
140 metadata_size = ALIGN(geo->metadata_size, 4);
141 status_size = ALIGN(geo->ecc_chunk_count, 4);
142
143 geo->auxiliary_size = metadata_size + status_size;
144 geo->auxiliary_status_offset = metadata_size;
145
146 if (!this->swap_block_mark)
147 return 0;
148
149 /*
150 * We need to compute the byte and bit offsets of
151 * the physical block mark within the ECC-based view of the page.
152 *
153 * NAND chip with 2K page shows below:
154 * (Block Mark)
155 * | |
156 * | D |
157 * |<---->|
158 * V V
159 * +---+----------+-+----------+-+----------+-+----------+-+
160 * | M | data |E| data |E| data |E| data |E|
161 * +---+----------+-+----------+-+----------+-+----------+-+
162 *
163 * The position of block mark moves forward in the ECC-based view
164 * of page, and the delta is:
165 *
166 * E * G * (N - 1)
167 * D = (---------------- + M)
168 * 8
169 *
170 * With the formula to compute the ECC strength, and the condition
171 * : C >= O (C is the ecc chunk size)
172 *
173 * It's easy to deduce to the following result:
174 *
175 * E * G (O - M) C - M C - M
176 * ----------- <= ------- <= -------- < ---------
177 * 8 N N (N - 1)
178 *
179 * So, we get:
180 *
181 * E * G * (N - 1)
182 * D = (---------------- + M) < C
183 * 8
184 *
185 * The above inequality means the position of block mark
186 * within the ECC-based view of the page is still in the data chunk,
187 * and it's NOT in the ECC bits of the chunk.
188 *
189 * Use the following to compute the bit position of the
190 * physical block mark within the ECC-based view of the page:
191 * (page_size - D) * 8
192 *
193 * --Huang Shijie
194 */
195 block_mark_bit_offset = mtd->writesize * 8 -
196 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
197 + geo->metadata_size * 8);
198
199 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
200 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
201 return 0;
202}
203
204struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
205{
206 int chipnr = this->current_chip;
207
208 return this->dma_chans[chipnr];
209}
210
211/* Can we use the upper's buffer directly for DMA? */
212void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
213{
214 struct scatterlist *sgl = &this->data_sgl;
215 int ret;
216
217 this->direct_dma_map_ok = true;
218
219 /* first try to map the upper buffer directly */
220 sg_init_one(sgl, this->upper_buf, this->upper_len);
221 ret = dma_map_sg(this->dev, sgl, 1, dr);
222 if (ret == 0) {
223 /* We have to use our own DMA buffer. */
224 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
225
226 if (dr == DMA_TO_DEVICE)
227 memcpy(this->data_buffer_dma, this->upper_buf,
228 this->upper_len);
229
230 ret = dma_map_sg(this->dev, sgl, 1, dr);
231 if (ret == 0)
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530232 pr_err("DMA mapping failed.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800233
234 this->direct_dma_map_ok = false;
235 }
236}
237
238/* This will be called after the DMA operation is finished. */
239static void dma_irq_callback(void *param)
240{
241 struct gpmi_nand_data *this = param;
242 struct completion *dma_c = &this->dma_done;
243
244 complete(dma_c);
245
246 switch (this->dma_type) {
247 case DMA_FOR_COMMAND:
248 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
249 break;
250
251 case DMA_FOR_READ_DATA:
252 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
253 if (this->direct_dma_map_ok == false)
254 memcpy(this->upper_buf, this->data_buffer_dma,
255 this->upper_len);
256 break;
257
258 case DMA_FOR_WRITE_DATA:
259 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
260 break;
261
262 case DMA_FOR_READ_ECC_PAGE:
263 case DMA_FOR_WRITE_ECC_PAGE:
264 /* We have to wait the BCH interrupt to finish. */
265 break;
266
267 default:
268 pr_err("in wrong DMA operation.\n");
269 }
270}
271
272int start_dma_without_bch_irq(struct gpmi_nand_data *this,
273 struct dma_async_tx_descriptor *desc)
274{
275 struct completion *dma_c = &this->dma_done;
276 int err;
277
278 init_completion(dma_c);
279
280 desc->callback = dma_irq_callback;
281 desc->callback_param = this;
282 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800283 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800284
285 /* Wait for the interrupt from the DMA block. */
286 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
287 if (!err) {
288 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
289 gpmi_dump_info(this);
290 return -ETIMEDOUT;
291 }
292 return 0;
293}
294
295/*
296 * This function is used in BCH reading or BCH writing pages.
297 * It will wait for the BCH interrupt as long as ONE second.
298 * Actually, we must wait for two interrupts :
299 * [1] firstly the DMA interrupt and
300 * [2] secondly the BCH interrupt.
301 */
302int start_dma_with_bch_irq(struct gpmi_nand_data *this,
303 struct dma_async_tx_descriptor *desc)
304{
305 struct completion *bch_c = &this->bch_done;
306 int err;
307
308 /* Prepare to receive an interrupt from the BCH block. */
309 init_completion(bch_c);
310
311 /* start the DMA */
312 start_dma_without_bch_irq(this, desc);
313
314 /* Wait for the interrupt from the BCH block. */
315 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
316 if (!err) {
317 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
318 gpmi_dump_info(this);
319 return -ETIMEDOUT;
320 }
321 return 0;
322}
323
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800324static int acquire_register_block(struct gpmi_nand_data *this,
325 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800326{
327 struct platform_device *pdev = this->pdev;
328 struct resources *res = &this->resources;
329 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800330 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800331
332 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
333 if (!r) {
334 pr_err("Can't get resource for %s\n", res_name);
335 return -ENXIO;
336 }
337
338 p = ioremap(r->start, resource_size(r));
339 if (!p) {
340 pr_err("Can't remap %s\n", res_name);
341 return -ENOMEM;
342 }
343
344 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
345 res->gpmi_regs = p;
346 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
347 res->bch_regs = p;
348 else
349 pr_err("unknown resource name : %s\n", res_name);
350
351 return 0;
352}
353
354static void release_register_block(struct gpmi_nand_data *this)
355{
356 struct resources *res = &this->resources;
357 if (res->gpmi_regs)
358 iounmap(res->gpmi_regs);
359 if (res->bch_regs)
360 iounmap(res->bch_regs);
361 res->gpmi_regs = NULL;
362 res->bch_regs = NULL;
363}
364
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800365static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800366{
367 struct platform_device *pdev = this->pdev;
368 struct resources *res = &this->resources;
369 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
370 struct resource *r;
371 int err;
372
373 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
374 if (!r) {
375 pr_err("Can't get resource for %s\n", res_name);
376 return -ENXIO;
377 }
378
379 err = request_irq(r->start, irq_h, 0, res_name, this);
380 if (err) {
381 pr_err("Can't own %s\n", res_name);
382 return err;
383 }
384
385 res->bch_low_interrupt = r->start;
386 res->bch_high_interrupt = r->end;
387 return 0;
388}
389
390static void release_bch_irq(struct gpmi_nand_data *this)
391{
392 struct resources *res = &this->resources;
393 int i = res->bch_low_interrupt;
394
395 for (; i <= res->bch_high_interrupt; i++)
396 free_irq(i, this);
397}
398
399static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
400{
401 struct gpmi_nand_data *this = param;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400402 int dma_channel = (int)this->private;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800403
404 if (!mxs_dma_is_apbh(chan))
405 return false;
406 /*
407 * only catch the GPMI dma channels :
408 * for mx23 : MX23_DMA_GPMI0 ~ MX23_DMA_GPMI3
409 * (These four channels share the same IRQ!)
410 *
411 * for mx28 : MX28_DMA_GPMI0 ~ MX28_DMA_GPMI7
412 * (These eight channels share the same IRQ!)
413 */
Huang Shijiee10db1f2012-05-04 21:42:05 -0400414 if (dma_channel == chan->chan_id) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800415 chan->private = &this->dma_data;
416 return true;
417 }
418 return false;
419}
420
421static void release_dma_channels(struct gpmi_nand_data *this)
422{
423 unsigned int i;
424 for (i = 0; i < DMA_CHANS; i++)
425 if (this->dma_chans[i]) {
426 dma_release_channel(this->dma_chans[i]);
427 this->dma_chans[i] = NULL;
428 }
429}
430
Bill Pemberton06f25512012-11-19 13:23:07 -0500431static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800432{
433 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400434 struct resource *r_dma;
435 struct device_node *dn;
Huang Shijie513d57e2012-07-17 14:14:02 +0800436 u32 dma_channel;
437 int ret;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400438 struct dma_chan *dma_chan;
439 dma_cap_mask_t mask;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800440
Huang Shijiee10db1f2012-05-04 21:42:05 -0400441 /* dma channel, we only use the first one. */
442 dn = pdev->dev.of_node;
443 ret = of_property_read_u32(dn, "fsl,gpmi-dma-channel", &dma_channel);
444 if (ret) {
445 pr_err("unable to get DMA channel from dt.\n");
446 goto acquire_err;
447 }
448 this->private = (void *)dma_channel;
449
450 /* gpmi dma interrupt */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800451 r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
452 GPMI_NAND_DMA_INTERRUPT_RES_NAME);
Huang Shijiee10db1f2012-05-04 21:42:05 -0400453 if (!r_dma) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800454 pr_err("Can't get resource for DMA\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400455 goto acquire_err;
456 }
457 this->dma_data.chan_irq = r_dma->start;
458
459 /* request dma channel */
460 dma_cap_zero(mask);
461 dma_cap_set(DMA_SLAVE, mask);
462
463 dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
464 if (!dma_chan) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530465 pr_err("Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400466 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800467 }
468
Huang Shijiee10db1f2012-05-04 21:42:05 -0400469 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800470 return 0;
471
472acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800473 release_dma_channels(this);
474 return -EINVAL;
475}
476
Huang Shijieff506172012-07-02 21:39:32 -0400477static void gpmi_put_clks(struct gpmi_nand_data *this)
478{
479 struct resources *r = &this->resources;
480 struct clk *clk;
481 int i;
482
483 for (i = 0; i < GPMI_CLK_MAX; i++) {
484 clk = r->clock[i];
485 if (clk) {
486 clk_put(clk);
487 r->clock[i] = NULL;
488 }
489 }
490}
491
492static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
493 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
494};
495
Bill Pemberton06f25512012-11-19 13:23:07 -0500496static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400497{
498 struct resources *r = &this->resources;
499 char **extra_clks = NULL;
500 struct clk *clk;
501 int i;
502
503 /* The main clock is stored in the first. */
504 r->clock[0] = clk_get(this->dev, "gpmi_io");
505 if (IS_ERR(r->clock[0]))
506 goto err_clock;
507
508 /* Get extra clocks */
509 if (GPMI_IS_MX6Q(this))
510 extra_clks = extra_clks_for_mx6q;
511 if (!extra_clks)
512 return 0;
513
514 for (i = 1; i < GPMI_CLK_MAX; i++) {
515 if (extra_clks[i - 1] == NULL)
516 break;
517
518 clk = clk_get(this->dev, extra_clks[i - 1]);
519 if (IS_ERR(clk))
520 goto err_clock;
521
522 r->clock[i] = clk;
523 }
524
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800525 if (GPMI_IS_MX6Q(this))
Huang Shijieff506172012-07-02 21:39:32 -0400526 /*
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800527 * Set the default value for the gpmi clock in mx6q:
Huang Shijieff506172012-07-02 21:39:32 -0400528 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800529 * If you want to use the ONFI nand which is in the
530 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400531 */
532 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800533
Huang Shijieff506172012-07-02 21:39:32 -0400534 return 0;
535
536err_clock:
537 dev_dbg(this->dev, "failed in finding the clocks.\n");
538 gpmi_put_clks(this);
539 return -ENOMEM;
540}
541
Bill Pemberton06f25512012-11-19 13:23:07 -0500542static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800543{
Shawn Guo39febc02012-05-06 22:57:41 +0800544 struct pinctrl *pinctrl;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800545 int ret;
546
547 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
548 if (ret)
549 goto exit_regs;
550
551 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
552 if (ret)
553 goto exit_regs;
554
555 ret = acquire_bch_irq(this, bch_irq);
556 if (ret)
557 goto exit_regs;
558
559 ret = acquire_dma_channels(this);
560 if (ret)
561 goto exit_dma_channels;
562
Shawn Guo3e48b1b2012-05-19 21:06:13 +0800563 pinctrl = devm_pinctrl_get_select_default(&this->pdev->dev);
Shawn Guo39febc02012-05-06 22:57:41 +0800564 if (IS_ERR(pinctrl)) {
565 ret = PTR_ERR(pinctrl);
566 goto exit_pin;
567 }
568
Huang Shijieff506172012-07-02 21:39:32 -0400569 ret = gpmi_get_clks(this);
570 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800571 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800572 return 0;
573
574exit_clock:
Shawn Guo39febc02012-05-06 22:57:41 +0800575exit_pin:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800576 release_dma_channels(this);
577exit_dma_channels:
578 release_bch_irq(this);
579exit_regs:
580 release_register_block(this);
581 return ret;
582}
583
584static void release_resources(struct gpmi_nand_data *this)
585{
Huang Shijieff506172012-07-02 21:39:32 -0400586 gpmi_put_clks(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800587 release_register_block(this);
588 release_bch_irq(this);
589 release_dma_channels(this);
590}
591
Bill Pemberton06f25512012-11-19 13:23:07 -0500592static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800593{
594 int ret;
595
596 /*
597 * This structure contains the "safe" GPMI timing that should succeed
598 * with any NAND Flash device
599 * (although, with less-than-optimal performance).
600 */
601 struct nand_timing safe_timing = {
602 .data_setup_in_ns = 80,
603 .data_hold_in_ns = 60,
604 .address_setup_in_ns = 25,
605 .gpmi_sample_delay_in_ns = 6,
606 .tREA_in_ns = -1,
607 .tRLOH_in_ns = -1,
608 .tRHOH_in_ns = -1,
609 };
610
611 /* Initialize the hardwares. */
612 ret = gpmi_init(this);
613 if (ret)
614 return ret;
615
616 this->timing = safe_timing;
617 return 0;
618}
619
620static int read_page_prepare(struct gpmi_nand_data *this,
621 void *destination, unsigned length,
622 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
623 void **use_virt, dma_addr_t *use_phys)
624{
625 struct device *dev = this->dev;
626
627 if (virt_addr_valid(destination)) {
628 dma_addr_t dest_phys;
629
630 dest_phys = dma_map_single(dev, destination,
631 length, DMA_FROM_DEVICE);
632 if (dma_mapping_error(dev, dest_phys)) {
633 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530634 pr_err("%s, Alternate buffer is too small\n",
635 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800636 return -ENOMEM;
637 }
638 goto map_failed;
639 }
640 *use_virt = destination;
641 *use_phys = dest_phys;
642 this->direct_dma_map_ok = true;
643 return 0;
644 }
645
646map_failed:
647 *use_virt = alt_virt;
648 *use_phys = alt_phys;
649 this->direct_dma_map_ok = false;
650 return 0;
651}
652
653static inline void read_page_end(struct gpmi_nand_data *this,
654 void *destination, unsigned length,
655 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
656 void *used_virt, dma_addr_t used_phys)
657{
658 if (this->direct_dma_map_ok)
659 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
660}
661
662static inline void read_page_swap_end(struct gpmi_nand_data *this,
663 void *destination, unsigned length,
664 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
665 void *used_virt, dma_addr_t used_phys)
666{
667 if (!this->direct_dma_map_ok)
668 memcpy(destination, alt_virt, length);
669}
670
671static int send_page_prepare(struct gpmi_nand_data *this,
672 const void *source, unsigned length,
673 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
674 const void **use_virt, dma_addr_t *use_phys)
675{
676 struct device *dev = this->dev;
677
678 if (virt_addr_valid(source)) {
679 dma_addr_t source_phys;
680
681 source_phys = dma_map_single(dev, (void *)source, length,
682 DMA_TO_DEVICE);
683 if (dma_mapping_error(dev, source_phys)) {
684 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530685 pr_err("%s, Alternate buffer is too small\n",
686 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800687 return -ENOMEM;
688 }
689 goto map_failed;
690 }
691 *use_virt = source;
692 *use_phys = source_phys;
693 return 0;
694 }
695map_failed:
696 /*
697 * Copy the content of the source buffer into the alternate
698 * buffer and set up the return values accordingly.
699 */
700 memcpy(alt_virt, source, length);
701
702 *use_virt = alt_virt;
703 *use_phys = alt_phys;
704 return 0;
705}
706
707static void send_page_end(struct gpmi_nand_data *this,
708 const void *source, unsigned length,
709 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
710 const void *used_virt, dma_addr_t used_phys)
711{
712 struct device *dev = this->dev;
713 if (used_virt == source)
714 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
715}
716
717static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
718{
719 struct device *dev = this->dev;
720
721 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
722 dma_free_coherent(dev, this->page_buffer_size,
723 this->page_buffer_virt,
724 this->page_buffer_phys);
725 kfree(this->cmd_buffer);
726 kfree(this->data_buffer_dma);
727
728 this->cmd_buffer = NULL;
729 this->data_buffer_dma = NULL;
730 this->page_buffer_virt = NULL;
731 this->page_buffer_size = 0;
732}
733
734/* Allocate the DMA buffers */
735static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
736{
737 struct bch_geometry *geo = &this->bch_geometry;
738 struct device *dev = this->dev;
739
740 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800741 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800742 if (this->cmd_buffer == NULL)
743 goto error_alloc;
744
745 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800746 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800747 if (this->data_buffer_dma == NULL)
748 goto error_alloc;
749
750 /*
751 * [3] Allocate the page buffer.
752 *
753 * Both the payload buffer and the auxiliary buffer must appear on
754 * 32-bit boundaries. We presume the size of the payload buffer is a
755 * power of two and is much larger than four, which guarantees the
756 * auxiliary buffer will appear on a 32-bit boundary.
757 */
758 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
759 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
760 &this->page_buffer_phys, GFP_DMA);
761 if (!this->page_buffer_virt)
762 goto error_alloc;
763
764
765 /* Slice up the page buffer. */
766 this->payload_virt = this->page_buffer_virt;
767 this->payload_phys = this->page_buffer_phys;
768 this->auxiliary_virt = this->payload_virt + geo->payload_size;
769 this->auxiliary_phys = this->payload_phys + geo->payload_size;
770 return 0;
771
772error_alloc:
773 gpmi_free_dma_buffer(this);
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530774 pr_err("Error allocating DMA buffers!\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800775 return -ENOMEM;
776}
777
778static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
779{
780 struct nand_chip *chip = mtd->priv;
781 struct gpmi_nand_data *this = chip->priv;
782 int ret;
783
784 /*
785 * Every operation begins with a command byte and a series of zero or
786 * more address bytes. These are distinguished by either the Address
787 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
788 * asserted. When MTD is ready to execute the command, it will deassert
789 * both latch enables.
790 *
791 * Rather than run a separate DMA operation for every single byte, we
792 * queue them up and run a single DMA operation for the entire series
793 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
794 */
795 if ((ctrl & (NAND_ALE | NAND_CLE))) {
796 if (data != NAND_CMD_NONE)
797 this->cmd_buffer[this->command_length++] = data;
798 return;
799 }
800
801 if (!this->command_length)
802 return;
803
804 ret = gpmi_send_command(this);
805 if (ret)
806 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
807
808 this->command_length = 0;
809}
810
811static int gpmi_dev_ready(struct mtd_info *mtd)
812{
813 struct nand_chip *chip = mtd->priv;
814 struct gpmi_nand_data *this = chip->priv;
815
816 return gpmi_is_ready(this, this->current_chip);
817}
818
819static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
820{
821 struct nand_chip *chip = mtd->priv;
822 struct gpmi_nand_data *this = chip->priv;
823
824 if ((this->current_chip < 0) && (chipnr >= 0))
825 gpmi_begin(this);
826 else if ((this->current_chip >= 0) && (chipnr < 0))
827 gpmi_end(this);
828
829 this->current_chip = chipnr;
830}
831
832static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
833{
834 struct nand_chip *chip = mtd->priv;
835 struct gpmi_nand_data *this = chip->priv;
836
837 pr_debug("len is %d\n", len);
838 this->upper_buf = buf;
839 this->upper_len = len;
840
841 gpmi_read_data(this);
842}
843
844static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
845{
846 struct nand_chip *chip = mtd->priv;
847 struct gpmi_nand_data *this = chip->priv;
848
849 pr_debug("len is %d\n", len);
850 this->upper_buf = (uint8_t *)buf;
851 this->upper_len = len;
852
853 gpmi_send_data(this);
854}
855
856static uint8_t gpmi_read_byte(struct mtd_info *mtd)
857{
858 struct nand_chip *chip = mtd->priv;
859 struct gpmi_nand_data *this = chip->priv;
860 uint8_t *buf = this->data_buffer_dma;
861
862 gpmi_read_buf(mtd, buf, 1);
863 return buf[0];
864}
865
866/*
867 * Handles block mark swapping.
868 * It can be called in swapping the block mark, or swapping it back,
869 * because the the operations are the same.
870 */
871static void block_mark_swapping(struct gpmi_nand_data *this,
872 void *payload, void *auxiliary)
873{
874 struct bch_geometry *nfc_geo = &this->bch_geometry;
875 unsigned char *p;
876 unsigned char *a;
877 unsigned int bit;
878 unsigned char mask;
879 unsigned char from_data;
880 unsigned char from_oob;
881
882 if (!this->swap_block_mark)
883 return;
884
885 /*
886 * If control arrives here, we're swapping. Make some convenience
887 * variables.
888 */
889 bit = nfc_geo->block_mark_bit_offset;
890 p = payload + nfc_geo->block_mark_byte_offset;
891 a = auxiliary;
892
893 /*
894 * Get the byte from the data area that overlays the block mark. Since
895 * the ECC engine applies its own view to the bits in the page, the
896 * physical block mark won't (in general) appear on a byte boundary in
897 * the data.
898 */
899 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
900
901 /* Get the byte from the OOB. */
902 from_oob = a[0];
903
904 /* Swap them. */
905 a[0] = from_data;
906
907 mask = (0x1 << bit) - 1;
908 p[0] = (p[0] & mask) | (from_oob << bit);
909
910 mask = ~0 << bit;
911 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
912}
913
914static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700915 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800916{
917 struct gpmi_nand_data *this = chip->priv;
918 struct bch_geometry *nfc_geo = &this->bch_geometry;
919 void *payload_virt;
920 dma_addr_t payload_phys;
921 void *auxiliary_virt;
922 dma_addr_t auxiliary_phys;
923 unsigned int i;
924 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -0600925 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800926 int ret;
927
928 pr_debug("page number is : %d\n", page);
929 ret = read_page_prepare(this, buf, mtd->writesize,
930 this->payload_virt, this->payload_phys,
931 nfc_geo->payload_size,
932 &payload_virt, &payload_phys);
933 if (ret) {
934 pr_err("Inadequate DMA buffer\n");
935 ret = -ENOMEM;
936 return ret;
937 }
938 auxiliary_virt = this->auxiliary_virt;
939 auxiliary_phys = this->auxiliary_phys;
940
941 /* go! */
942 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
943 read_page_end(this, buf, mtd->writesize,
944 this->payload_virt, this->payload_phys,
945 nfc_geo->payload_size,
946 payload_virt, payload_phys);
947 if (ret) {
948 pr_err("Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -0600949 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800950 }
951
952 /* handle the block mark swapping */
953 block_mark_swapping(this, payload_virt, auxiliary_virt);
954
955 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -0600956 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800957
958 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
959 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
960 continue;
961
962 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -0600963 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800964 continue;
965 }
Zach Sadeckib23b7462012-12-13 20:36:29 -0600966 mtd->ecc_stats.corrected += *status;
967 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800968 }
969
Brian Norris7725cc82012-05-02 10:15:02 -0700970 if (oob_required) {
971 /*
972 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
973 * for details about our policy for delivering the OOB.
974 *
975 * We fill the caller's buffer with set bits, and then copy the
976 * block mark to th caller's buffer. Note that, if block mark
977 * swapping was necessary, it has already been done, so we can
978 * rely on the first byte of the auxiliary buffer to contain
979 * the block mark.
980 */
981 memset(chip->oob_poi, ~0, mtd->oobsize);
982 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -0700983 }
Sascha Hauer60238132012-06-26 17:26:16 +0200984
985 read_page_swap_end(this, buf, mtd->writesize,
986 this->payload_virt, this->payload_phys,
987 nfc_geo->payload_size,
988 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -0600989
990 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800991}
992
Josh Wufdbad98d2012-06-25 18:07:45 +0800993static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700994 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800995{
996 struct gpmi_nand_data *this = chip->priv;
997 struct bch_geometry *nfc_geo = &this->bch_geometry;
998 const void *payload_virt;
999 dma_addr_t payload_phys;
1000 const void *auxiliary_virt;
1001 dma_addr_t auxiliary_phys;
1002 int ret;
1003
1004 pr_debug("ecc write page.\n");
1005 if (this->swap_block_mark) {
1006 /*
1007 * If control arrives here, we're doing block mark swapping.
1008 * Since we can't modify the caller's buffers, we must copy them
1009 * into our own.
1010 */
1011 memcpy(this->payload_virt, buf, mtd->writesize);
1012 payload_virt = this->payload_virt;
1013 payload_phys = this->payload_phys;
1014
1015 memcpy(this->auxiliary_virt, chip->oob_poi,
1016 nfc_geo->auxiliary_size);
1017 auxiliary_virt = this->auxiliary_virt;
1018 auxiliary_phys = this->auxiliary_phys;
1019
1020 /* Handle block mark swapping. */
1021 block_mark_swapping(this,
1022 (void *) payload_virt, (void *) auxiliary_virt);
1023 } else {
1024 /*
1025 * If control arrives here, we're not doing block mark swapping,
1026 * so we can to try and use the caller's buffers.
1027 */
1028 ret = send_page_prepare(this,
1029 buf, mtd->writesize,
1030 this->payload_virt, this->payload_phys,
1031 nfc_geo->payload_size,
1032 &payload_virt, &payload_phys);
1033 if (ret) {
1034 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001035 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001036 }
1037
1038 ret = send_page_prepare(this,
1039 chip->oob_poi, mtd->oobsize,
1040 this->auxiliary_virt, this->auxiliary_phys,
1041 nfc_geo->auxiliary_size,
1042 &auxiliary_virt, &auxiliary_phys);
1043 if (ret) {
1044 pr_err("Inadequate auxiliary DMA buffer\n");
1045 goto exit_auxiliary;
1046 }
1047 }
1048
1049 /* Ask the NFC. */
1050 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1051 if (ret)
1052 pr_err("Error in ECC-based write: %d\n", ret);
1053
1054 if (!this->swap_block_mark) {
1055 send_page_end(this, chip->oob_poi, mtd->oobsize,
1056 this->auxiliary_virt, this->auxiliary_phys,
1057 nfc_geo->auxiliary_size,
1058 auxiliary_virt, auxiliary_phys);
1059exit_auxiliary:
1060 send_page_end(this, buf, mtd->writesize,
1061 this->payload_virt, this->payload_phys,
1062 nfc_geo->payload_size,
1063 payload_virt, payload_phys);
1064 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001065
1066 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001067}
1068
1069/*
1070 * There are several places in this driver where we have to handle the OOB and
1071 * block marks. This is the function where things are the most complicated, so
1072 * this is where we try to explain it all. All the other places refer back to
1073 * here.
1074 *
1075 * These are the rules, in order of decreasing importance:
1076 *
1077 * 1) Nothing the caller does can be allowed to imperil the block mark.
1078 *
1079 * 2) In read operations, the first byte of the OOB we return must reflect the
1080 * true state of the block mark, no matter where that block mark appears in
1081 * the physical page.
1082 *
1083 * 3) ECC-based read operations return an OOB full of set bits (since we never
1084 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1085 * return).
1086 *
1087 * 4) "Raw" read operations return a direct view of the physical bytes in the
1088 * page, using the conventional definition of which bytes are data and which
1089 * are OOB. This gives the caller a way to see the actual, physical bytes
1090 * in the page, without the distortions applied by our ECC engine.
1091 *
1092 *
1093 * What we do for this specific read operation depends on two questions:
1094 *
1095 * 1) Are we doing a "raw" read, or an ECC-based read?
1096 *
1097 * 2) Are we using block mark swapping or transcription?
1098 *
1099 * There are four cases, illustrated by the following Karnaugh map:
1100 *
1101 * | Raw | ECC-based |
1102 * -------------+-------------------------+-------------------------+
1103 * | Read the conventional | |
1104 * | OOB at the end of the | |
1105 * Swapping | page and return it. It | |
1106 * | contains exactly what | |
1107 * | we want. | Read the block mark and |
1108 * -------------+-------------------------+ return it in a buffer |
1109 * | Read the conventional | full of set bits. |
1110 * | OOB at the end of the | |
1111 * | page and also the block | |
1112 * Transcribing | mark in the metadata. | |
1113 * | Copy the block mark | |
1114 * | into the first byte of | |
1115 * | the OOB. | |
1116 * -------------+-------------------------+-------------------------+
1117 *
1118 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1119 * giving an accurate view of the actual, physical bytes in the page (we're
1120 * overwriting the block mark). That's OK because it's more important to follow
1121 * rule #2.
1122 *
1123 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1124 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1125 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1126 * ECC-based or raw view of the page is implicit in which function it calls
1127 * (there is a similar pair of ECC-based/raw functions for writing).
1128 *
Brian Norris271b8742012-05-11 13:30:35 -07001129 * FIXME: The following paragraph is incorrect, now that there exist
1130 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1131 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001132 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1133 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1134 * caller wants an ECC-based or raw view of the page is not propagated down to
1135 * this driver.
1136 */
1137static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001138 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001139{
1140 struct gpmi_nand_data *this = chip->priv;
1141
1142 pr_debug("page number is %d\n", page);
1143 /* clear the OOB buffer */
1144 memset(chip->oob_poi, ~0, mtd->oobsize);
1145
1146 /* Read out the conventional OOB. */
1147 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1148 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1149
1150 /*
1151 * Now, we want to make sure the block mark is correct. In the
1152 * Swapping/Raw case, we already have it. Otherwise, we need to
1153 * explicitly read it.
1154 */
1155 if (!this->swap_block_mark) {
1156 /* Read the block mark into the first byte of the OOB buffer. */
1157 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1158 chip->oob_poi[0] = chip->read_byte(mtd);
1159 }
1160
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001161 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001162}
1163
1164static int
1165gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1166{
1167 /*
1168 * The BCH will use all the (page + oob).
1169 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1170 * But it can not stop some ioctls such MEMWRITEOOB which uses
Brian Norris0612b9d2011-08-30 18:45:40 -07001171 * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
Huang Shijie10a2bca2011-09-08 10:47:09 +08001172 * these ioctls too.
1173 */
1174 return -EPERM;
1175}
1176
1177static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1178{
1179 struct nand_chip *chip = mtd->priv;
1180 struct gpmi_nand_data *this = chip->priv;
1181 int block, ret = 0;
1182 uint8_t *block_mark;
1183 int column, page, status, chipnr;
1184
1185 /* Get block number */
1186 block = (int)(ofs >> chip->bbt_erase_shift);
1187 if (chip->bbt)
1188 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1189
1190 /* Do we have a flash based bad block table ? */
Wolfram Sang52899662012-01-31 13:10:43 +01001191 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001192 ret = nand_update_bbt(mtd, ofs);
1193 else {
1194 chipnr = (int)(ofs >> chip->chip_shift);
1195 chip->select_chip(mtd, chipnr);
1196
1197 column = this->swap_block_mark ? mtd->writesize : 0;
1198
1199 /* Write the block mark. */
1200 block_mark = this->data_buffer_dma;
1201 block_mark[0] = 0; /* bad block marker */
1202
1203 /* Shift to get page */
1204 page = (int)(ofs >> chip->page_shift);
1205
1206 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1207 chip->write_buf(mtd, block_mark, 1);
1208 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1209
1210 status = chip->waitfunc(mtd, chip);
1211 if (status & NAND_STATUS_FAIL)
1212 ret = -EIO;
1213
1214 chip->select_chip(mtd, -1);
1215 }
1216 if (!ret)
1217 mtd->ecc_stats.badblocks++;
1218
1219 return ret;
1220}
1221
Wolfram Sanga78da282012-03-21 19:29:17 +01001222static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001223{
1224 struct boot_rom_geometry *geometry = &this->rom_geometry;
1225
1226 /*
1227 * Set the boot block stride size.
1228 *
1229 * In principle, we should be reading this from the OTP bits, since
1230 * that's where the ROM is going to get it. In fact, we don't have any
1231 * way to read the OTP bits, so we go with the default and hope for the
1232 * best.
1233 */
1234 geometry->stride_size_in_pages = 64;
1235
1236 /*
1237 * Set the search area stride exponent.
1238 *
1239 * In principle, we should be reading this from the OTP bits, since
1240 * that's where the ROM is going to get it. In fact, we don't have any
1241 * way to read the OTP bits, so we go with the default and hope for the
1242 * best.
1243 */
1244 geometry->search_area_stride_exponent = 2;
1245 return 0;
1246}
1247
1248static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001249static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001250{
1251 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1252 struct device *dev = this->dev;
1253 struct mtd_info *mtd = &this->mtd;
1254 struct nand_chip *chip = &this->nand;
1255 unsigned int search_area_size_in_strides;
1256 unsigned int stride;
1257 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001258 uint8_t *buffer = chip->buffers->databuf;
1259 int saved_chip_number;
1260 int found_an_ncb_fingerprint = false;
1261
1262 /* Compute the number of strides in a search area. */
1263 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1264
1265 saved_chip_number = this->current_chip;
1266 chip->select_chip(mtd, 0);
1267
1268 /*
1269 * Loop through the first search area, looking for the NCB fingerprint.
1270 */
1271 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1272
1273 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001274 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001275 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001276
1277 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1278
1279 /*
1280 * Read the NCB fingerprint. The fingerprint is four bytes long
1281 * and starts in the 12th byte of the page.
1282 */
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1284 chip->read_buf(mtd, buffer, strlen(fingerprint));
1285
1286 /* Look for the fingerprint. */
1287 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1288 found_an_ncb_fingerprint = true;
1289 break;
1290 }
1291
1292 }
1293
1294 chip->select_chip(mtd, saved_chip_number);
1295
1296 if (found_an_ncb_fingerprint)
1297 dev_dbg(dev, "\tFound a fingerprint\n");
1298 else
1299 dev_dbg(dev, "\tNo fingerprint found\n");
1300 return found_an_ncb_fingerprint;
1301}
1302
1303/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001304static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001305{
1306 struct device *dev = this->dev;
1307 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1308 struct mtd_info *mtd = &this->mtd;
1309 struct nand_chip *chip = &this->nand;
1310 unsigned int block_size_in_pages;
1311 unsigned int search_area_size_in_strides;
1312 unsigned int search_area_size_in_pages;
1313 unsigned int search_area_size_in_blocks;
1314 unsigned int block;
1315 unsigned int stride;
1316 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001317 uint8_t *buffer = chip->buffers->databuf;
1318 int saved_chip_number;
1319 int status;
1320
1321 /* Compute the search area geometry. */
1322 block_size_in_pages = mtd->erasesize / mtd->writesize;
1323 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1324 search_area_size_in_pages = search_area_size_in_strides *
1325 rom_geo->stride_size_in_pages;
1326 search_area_size_in_blocks =
1327 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1328 block_size_in_pages;
1329
1330 dev_dbg(dev, "Search Area Geometry :\n");
1331 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1332 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1333 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1334
1335 /* Select chip 0. */
1336 saved_chip_number = this->current_chip;
1337 chip->select_chip(mtd, 0);
1338
1339 /* Loop over blocks in the first search area, erasing them. */
1340 dev_dbg(dev, "Erasing the search area...\n");
1341
1342 for (block = 0; block < search_area_size_in_blocks; block++) {
1343 /* Compute the page address. */
1344 page = block * block_size_in_pages;
1345
1346 /* Erase this block. */
1347 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1348 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1349 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1350
1351 /* Wait for the erase to finish. */
1352 status = chip->waitfunc(mtd, chip);
1353 if (status & NAND_STATUS_FAIL)
1354 dev_err(dev, "[%s] Erase failed.\n", __func__);
1355 }
1356
1357 /* Write the NCB fingerprint into the page buffer. */
1358 memset(buffer, ~0, mtd->writesize);
1359 memset(chip->oob_poi, ~0, mtd->oobsize);
1360 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1361
1362 /* Loop through the first search area, writing NCB fingerprints. */
1363 dev_dbg(dev, "Writing NCB fingerprints...\n");
1364 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001365 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001366 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001367
1368 /* Write the first page of the current stride. */
1369 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1370 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001371 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001372 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1373
1374 /* Wait for the write to finish. */
1375 status = chip->waitfunc(mtd, chip);
1376 if (status & NAND_STATUS_FAIL)
1377 dev_err(dev, "[%s] Write failed.\n", __func__);
1378 }
1379
1380 /* Deselect chip 0. */
1381 chip->select_chip(mtd, saved_chip_number);
1382 return 0;
1383}
1384
Wolfram Sanga78da282012-03-21 19:29:17 +01001385static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001386{
1387 struct device *dev = this->dev;
1388 struct nand_chip *chip = &this->nand;
1389 struct mtd_info *mtd = &this->mtd;
1390 unsigned int block_count;
1391 unsigned int block;
1392 int chipnr;
1393 int page;
1394 loff_t byte;
1395 uint8_t block_mark;
1396 int ret = 0;
1397
1398 /*
1399 * If control arrives here, we can't use block mark swapping, which
1400 * means we're forced to use transcription. First, scan for the
1401 * transcription stamp. If we find it, then we don't have to do
1402 * anything -- the block marks are already transcribed.
1403 */
1404 if (mx23_check_transcription_stamp(this))
1405 return 0;
1406
1407 /*
1408 * If control arrives here, we couldn't find a transcription stamp, so
1409 * so we presume the block marks are in the conventional location.
1410 */
1411 dev_dbg(dev, "Transcribing bad block marks...\n");
1412
1413 /* Compute the number of blocks in the entire medium. */
1414 block_count = chip->chipsize >> chip->phys_erase_shift;
1415
1416 /*
1417 * Loop over all the blocks in the medium, transcribing block marks as
1418 * we go.
1419 */
1420 for (block = 0; block < block_count; block++) {
1421 /*
1422 * Compute the chip, page and byte addresses for this block's
1423 * conventional mark.
1424 */
1425 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1426 page = block << (chip->phys_erase_shift - chip->page_shift);
1427 byte = block << chip->phys_erase_shift;
1428
1429 /* Send the command to read the conventional block mark. */
1430 chip->select_chip(mtd, chipnr);
1431 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1432 block_mark = chip->read_byte(mtd);
1433 chip->select_chip(mtd, -1);
1434
1435 /*
1436 * Check if the block is marked bad. If so, we need to mark it
1437 * again, but this time the result will be a mark in the
1438 * location where we transcribe block marks.
1439 */
1440 if (block_mark != 0xff) {
1441 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1442 ret = chip->block_markbad(mtd, byte);
1443 if (ret)
1444 dev_err(dev, "Failed to mark block bad with "
1445 "ret %d\n", ret);
1446 }
1447 }
1448
1449 /* Write the stamp that indicates we've transcribed the block marks. */
1450 mx23_write_transcription_stamp(this);
1451 return 0;
1452}
1453
Wolfram Sanga78da282012-03-21 19:29:17 +01001454static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001455{
1456 nand_boot_set_geometry(this);
1457
1458 /* This is ROM arch-specific initilization before the BBT scanning. */
1459 if (GPMI_IS_MX23(this))
1460 return mx23_boot_init(this);
1461 return 0;
1462}
1463
Wolfram Sanga78da282012-03-21 19:29:17 +01001464static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001465{
1466 int ret;
1467
1468 /* Free the temporary DMA memory for reading ID. */
1469 gpmi_free_dma_buffer(this);
1470
1471 /* Set up the NFC geometry which is used by BCH. */
1472 ret = bch_set_geometry(this);
1473 if (ret) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +05301474 pr_err("Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001475 return ret;
1476 }
1477
1478 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1479 return gpmi_alloc_dma_buffer(this);
1480}
1481
1482static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1483{
1484 int ret;
1485
1486 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1487 if (GPMI_IS_MX23(this))
1488 this->swap_block_mark = false;
1489 else
1490 this->swap_block_mark = true;
1491
1492 /* Set up the medium geometry */
1493 ret = gpmi_set_geometry(this);
1494 if (ret)
1495 return ret;
1496
Marek Vasut5636ce02012-05-21 22:59:27 +02001497 /* Adjust the ECC strength according to the chip. */
1498 this->nand.ecc.strength = this->bch_geometry.ecc_strength;
1499 this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
Huang Shijiee0dd89c2012-07-03 16:24:33 +08001500 this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
Marek Vasut5636ce02012-05-21 22:59:27 +02001501
Huang Shijie10a2bca2011-09-08 10:47:09 +08001502 /* NAND boot init, depends on the gpmi_set_geometry(). */
1503 return nand_boot_init(this);
1504}
1505
1506static int gpmi_scan_bbt(struct mtd_info *mtd)
1507{
1508 struct nand_chip *chip = mtd->priv;
1509 struct gpmi_nand_data *this = chip->priv;
1510 int ret;
1511
1512 /* Prepare for the BBT scan. */
1513 ret = gpmi_pre_bbt_scan(this);
1514 if (ret)
1515 return ret;
1516
Huang Shijie995fbbf2012-09-13 14:57:59 +08001517 /*
1518 * Can we enable the extra features? such as EDO or Sync mode.
1519 *
1520 * We do not check the return value now. That's means if we fail in
1521 * enable the extra features, we still can run in the normal way.
1522 */
1523 gpmi_extra_init(this);
1524
Huang Shijie10a2bca2011-09-08 10:47:09 +08001525 /* use the default BBT implementation */
1526 return nand_default_bbt(mtd);
1527}
1528
Huang Shijie513d57e2012-07-17 14:14:02 +08001529static void gpmi_nfc_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001530{
1531 nand_release(&this->mtd);
1532 gpmi_free_dma_buffer(this);
1533}
1534
Bill Pemberton06f25512012-11-19 13:23:07 -05001535static int gpmi_nfc_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001536{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001537 struct mtd_info *mtd = &this->mtd;
1538 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001539 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001540 int ret;
1541
1542 /* init current chip */
1543 this->current_chip = -1;
1544
1545 /* init the MTD data structures */
1546 mtd->priv = chip;
1547 mtd->name = "gpmi-nand";
1548 mtd->owner = THIS_MODULE;
1549
1550 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1551 chip->priv = this;
1552 chip->select_chip = gpmi_select_chip;
1553 chip->cmd_ctrl = gpmi_cmd_ctrl;
1554 chip->dev_ready = gpmi_dev_ready;
1555 chip->read_byte = gpmi_read_byte;
1556 chip->read_buf = gpmi_read_buf;
1557 chip->write_buf = gpmi_write_buf;
1558 chip->ecc.read_page = gpmi_ecc_read_page;
1559 chip->ecc.write_page = gpmi_ecc_write_page;
1560 chip->ecc.read_oob = gpmi_ecc_read_oob;
1561 chip->ecc.write_oob = gpmi_ecc_write_oob;
1562 chip->scan_bbt = gpmi_scan_bbt;
1563 chip->badblock_pattern = &gpmi_bbt_descr;
1564 chip->block_markbad = gpmi_block_markbad;
1565 chip->options |= NAND_NO_SUBPAGE_WRITE;
1566 chip->ecc.mode = NAND_ECC_HW;
1567 chip->ecc.size = 1;
Marek Vasut5636ce02012-05-21 22:59:27 +02001568 chip->ecc.strength = 8;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001569 chip->ecc.layout = &gpmi_hw_ecclayout;
Huang Shijiec50c6942012-07-03 16:24:32 +08001570 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1571 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001572
1573 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1574 this->bch_geometry.payload_size = 1024;
1575 this->bch_geometry.auxiliary_size = 128;
1576 ret = gpmi_alloc_dma_buffer(this);
1577 if (ret)
1578 goto err_out;
1579
Huang Shijiee10db1f2012-05-04 21:42:05 -04001580 ret = nand_scan(mtd, 1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001581 if (ret) {
1582 pr_err("Chip scan failed\n");
1583 goto err_out;
1584 }
1585
Huang Shijiee10db1f2012-05-04 21:42:05 -04001586 ppdata.of_node = this->pdev->dev.of_node;
1587 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001588 if (ret)
1589 goto err_out;
1590 return 0;
1591
1592err_out:
1593 gpmi_nfc_exit(this);
1594 return ret;
1595}
1596
Huang Shijiee10db1f2012-05-04 21:42:05 -04001597static const struct platform_device_id gpmi_ids[] = {
1598 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1599 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001600 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Huang Shijiee10db1f2012-05-04 21:42:05 -04001601 {},
1602};
1603
1604static const struct of_device_id gpmi_nand_id_table[] = {
1605 {
1606 .compatible = "fsl,imx23-gpmi-nand",
1607 .data = (void *)&gpmi_ids[IS_MX23]
1608 }, {
1609 .compatible = "fsl,imx28-gpmi-nand",
1610 .data = (void *)&gpmi_ids[IS_MX28]
Huang Shijie9013bb42012-05-04 21:42:06 -04001611 }, {
1612 .compatible = "fsl,imx6q-gpmi-nand",
1613 .data = (void *)&gpmi_ids[IS_MX6Q]
Huang Shijiee10db1f2012-05-04 21:42:05 -04001614 }, {}
1615};
1616MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1617
Bill Pemberton06f25512012-11-19 13:23:07 -05001618static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001619{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001620 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001621 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001622 int ret;
1623
Huang Shijiee10db1f2012-05-04 21:42:05 -04001624 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1625 if (of_id) {
1626 pdev->id_entry = of_id->data;
1627 } else {
1628 pr_err("Failed to find the right device id.\n");
1629 return -ENOMEM;
1630 }
1631
Huang Shijie10a2bca2011-09-08 10:47:09 +08001632 this = kzalloc(sizeof(*this), GFP_KERNEL);
1633 if (!this) {
1634 pr_err("Failed to allocate per-device memory\n");
1635 return -ENOMEM;
1636 }
1637
1638 platform_set_drvdata(pdev, this);
1639 this->pdev = pdev;
1640 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001641
1642 ret = acquire_resources(this);
1643 if (ret)
1644 goto exit_acquire_resources;
1645
1646 ret = init_hardware(this);
1647 if (ret)
1648 goto exit_nfc_init;
1649
1650 ret = gpmi_nfc_init(this);
1651 if (ret)
1652 goto exit_nfc_init;
1653
Fabio Estevam490e2802012-09-05 11:35:24 -03001654 dev_info(this->dev, "driver registered.\n");
1655
Huang Shijie10a2bca2011-09-08 10:47:09 +08001656 return 0;
1657
1658exit_nfc_init:
1659 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001660exit_acquire_resources:
1661 platform_set_drvdata(pdev, NULL);
Fabio Estevam490e2802012-09-05 11:35:24 -03001662 dev_err(this->dev, "driver registration failed: %d\n", ret);
Huang Shijie26738dd2013-01-23 16:20:53 +08001663 kfree(this);
Fabio Estevam490e2802012-09-05 11:35:24 -03001664
Huang Shijie10a2bca2011-09-08 10:47:09 +08001665 return ret;
1666}
1667
Bill Pemberton810b7e02012-11-19 13:26:04 -05001668static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001669{
1670 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1671
1672 gpmi_nfc_exit(this);
1673 release_resources(this);
1674 platform_set_drvdata(pdev, NULL);
1675 kfree(this);
1676 return 0;
1677}
1678
Huang Shijie10a2bca2011-09-08 10:47:09 +08001679static struct platform_driver gpmi_nand_driver = {
1680 .driver = {
1681 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001682 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001683 },
1684 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001685 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001686 .id_table = gpmi_ids,
1687};
Fabio Estevam490e2802012-09-05 11:35:24 -03001688module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001689
1690MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1691MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1692MODULE_LICENSE("GPL");