blob: 38c8b8bfc42894dcc2bd566f33de3eb1934be5cb [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
115 /* The default for chunk size. There is no oobsize greater then 512. */
116 geo->ecc_chunk_size = 512;
117 while (geo->ecc_chunk_size < mtd->oobsize)
118 geo->ecc_chunk_size *= 2; /* keep C >= O */
119
120 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
121
122 /* We use the same ECC strength for all chunks. */
123 geo->ecc_strength = get_ecc_strength(this);
124 if (!geo->ecc_strength) {
Fabio Estevam3d100952012-09-05 10:27:33 -0300125 pr_err("wrong ECC strength.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800126 return -EINVAL;
127 }
128
129 geo->page_size = mtd->writesize + mtd->oobsize;
130 geo->payload_size = mtd->writesize;
131
132 /*
133 * The auxiliary buffer contains the metadata and the ECC status. The
134 * metadata is padded to the nearest 32-bit boundary. The ECC status
135 * contains one byte for every ECC chunk, and is also padded to the
136 * nearest 32-bit boundary.
137 */
138 metadata_size = ALIGN(geo->metadata_size, 4);
139 status_size = ALIGN(geo->ecc_chunk_count, 4);
140
141 geo->auxiliary_size = metadata_size + status_size;
142 geo->auxiliary_status_offset = metadata_size;
143
144 if (!this->swap_block_mark)
145 return 0;
146
147 /*
148 * We need to compute the byte and bit offsets of
149 * the physical block mark within the ECC-based view of the page.
150 *
151 * NAND chip with 2K page shows below:
152 * (Block Mark)
153 * | |
154 * | D |
155 * |<---->|
156 * V V
157 * +---+----------+-+----------+-+----------+-+----------+-+
158 * | M | data |E| data |E| data |E| data |E|
159 * +---+----------+-+----------+-+----------+-+----------+-+
160 *
161 * The position of block mark moves forward in the ECC-based view
162 * of page, and the delta is:
163 *
164 * E * G * (N - 1)
165 * D = (---------------- + M)
166 * 8
167 *
168 * With the formula to compute the ECC strength, and the condition
169 * : C >= O (C is the ecc chunk size)
170 *
171 * It's easy to deduce to the following result:
172 *
173 * E * G (O - M) C - M C - M
174 * ----------- <= ------- <= -------- < ---------
175 * 8 N N (N - 1)
176 *
177 * So, we get:
178 *
179 * E * G * (N - 1)
180 * D = (---------------- + M) < C
181 * 8
182 *
183 * The above inequality means the position of block mark
184 * within the ECC-based view of the page is still in the data chunk,
185 * and it's NOT in the ECC bits of the chunk.
186 *
187 * Use the following to compute the bit position of the
188 * physical block mark within the ECC-based view of the page:
189 * (page_size - D) * 8
190 *
191 * --Huang Shijie
192 */
193 block_mark_bit_offset = mtd->writesize * 8 -
194 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
195 + geo->metadata_size * 8);
196
197 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
198 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
199 return 0;
200}
201
202struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
203{
204 int chipnr = this->current_chip;
205
206 return this->dma_chans[chipnr];
207}
208
209/* Can we use the upper's buffer directly for DMA? */
210void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
211{
212 struct scatterlist *sgl = &this->data_sgl;
213 int ret;
214
215 this->direct_dma_map_ok = true;
216
217 /* first try to map the upper buffer directly */
218 sg_init_one(sgl, this->upper_buf, this->upper_len);
219 ret = dma_map_sg(this->dev, sgl, 1, dr);
220 if (ret == 0) {
221 /* We have to use our own DMA buffer. */
222 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
223
224 if (dr == DMA_TO_DEVICE)
225 memcpy(this->data_buffer_dma, this->upper_buf,
226 this->upper_len);
227
228 ret = dma_map_sg(this->dev, sgl, 1, dr);
229 if (ret == 0)
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530230 pr_err("DMA mapping failed.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800231
232 this->direct_dma_map_ok = false;
233 }
234}
235
236/* This will be called after the DMA operation is finished. */
237static void dma_irq_callback(void *param)
238{
239 struct gpmi_nand_data *this = param;
240 struct completion *dma_c = &this->dma_done;
241
242 complete(dma_c);
243
244 switch (this->dma_type) {
245 case DMA_FOR_COMMAND:
246 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
247 break;
248
249 case DMA_FOR_READ_DATA:
250 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
251 if (this->direct_dma_map_ok == false)
252 memcpy(this->upper_buf, this->data_buffer_dma,
253 this->upper_len);
254 break;
255
256 case DMA_FOR_WRITE_DATA:
257 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
258 break;
259
260 case DMA_FOR_READ_ECC_PAGE:
261 case DMA_FOR_WRITE_ECC_PAGE:
262 /* We have to wait the BCH interrupt to finish. */
263 break;
264
265 default:
266 pr_err("in wrong DMA operation.\n");
267 }
268}
269
270int start_dma_without_bch_irq(struct gpmi_nand_data *this,
271 struct dma_async_tx_descriptor *desc)
272{
273 struct completion *dma_c = &this->dma_done;
274 int err;
275
276 init_completion(dma_c);
277
278 desc->callback = dma_irq_callback;
279 desc->callback_param = this;
280 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800281 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800282
283 /* Wait for the interrupt from the DMA block. */
284 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
285 if (!err) {
286 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
287 gpmi_dump_info(this);
288 return -ETIMEDOUT;
289 }
290 return 0;
291}
292
293/*
294 * This function is used in BCH reading or BCH writing pages.
295 * It will wait for the BCH interrupt as long as ONE second.
296 * Actually, we must wait for two interrupts :
297 * [1] firstly the DMA interrupt and
298 * [2] secondly the BCH interrupt.
299 */
300int start_dma_with_bch_irq(struct gpmi_nand_data *this,
301 struct dma_async_tx_descriptor *desc)
302{
303 struct completion *bch_c = &this->bch_done;
304 int err;
305
306 /* Prepare to receive an interrupt from the BCH block. */
307 init_completion(bch_c);
308
309 /* start the DMA */
310 start_dma_without_bch_irq(this, desc);
311
312 /* Wait for the interrupt from the BCH block. */
313 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
314 if (!err) {
315 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
316 gpmi_dump_info(this);
317 return -ETIMEDOUT;
318 }
319 return 0;
320}
321
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800322static int acquire_register_block(struct gpmi_nand_data *this,
323 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800324{
325 struct platform_device *pdev = this->pdev;
326 struct resources *res = &this->resources;
327 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800328 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800329
330 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
331 if (!r) {
332 pr_err("Can't get resource for %s\n", res_name);
333 return -ENXIO;
334 }
335
336 p = ioremap(r->start, resource_size(r));
337 if (!p) {
338 pr_err("Can't remap %s\n", res_name);
339 return -ENOMEM;
340 }
341
342 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
343 res->gpmi_regs = p;
344 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
345 res->bch_regs = p;
346 else
347 pr_err("unknown resource name : %s\n", res_name);
348
349 return 0;
350}
351
352static void release_register_block(struct gpmi_nand_data *this)
353{
354 struct resources *res = &this->resources;
355 if (res->gpmi_regs)
356 iounmap(res->gpmi_regs);
357 if (res->bch_regs)
358 iounmap(res->bch_regs);
359 res->gpmi_regs = NULL;
360 res->bch_regs = NULL;
361}
362
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800363static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800364{
365 struct platform_device *pdev = this->pdev;
366 struct resources *res = &this->resources;
367 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
368 struct resource *r;
369 int err;
370
371 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
372 if (!r) {
373 pr_err("Can't get resource for %s\n", res_name);
374 return -ENXIO;
375 }
376
377 err = request_irq(r->start, irq_h, 0, res_name, this);
378 if (err) {
379 pr_err("Can't own %s\n", res_name);
380 return err;
381 }
382
383 res->bch_low_interrupt = r->start;
384 res->bch_high_interrupt = r->end;
385 return 0;
386}
387
388static void release_bch_irq(struct gpmi_nand_data *this)
389{
390 struct resources *res = &this->resources;
391 int i = res->bch_low_interrupt;
392
393 for (; i <= res->bch_high_interrupt; i++)
394 free_irq(i, this);
395}
396
397static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
398{
399 struct gpmi_nand_data *this = param;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400400 int dma_channel = (int)this->private;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800401
402 if (!mxs_dma_is_apbh(chan))
403 return false;
404 /*
405 * only catch the GPMI dma channels :
406 * for mx23 : MX23_DMA_GPMI0 ~ MX23_DMA_GPMI3
407 * (These four channels share the same IRQ!)
408 *
409 * for mx28 : MX28_DMA_GPMI0 ~ MX28_DMA_GPMI7
410 * (These eight channels share the same IRQ!)
411 */
Huang Shijiee10db1f2012-05-04 21:42:05 -0400412 if (dma_channel == chan->chan_id) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800413 chan->private = &this->dma_data;
414 return true;
415 }
416 return false;
417}
418
419static void release_dma_channels(struct gpmi_nand_data *this)
420{
421 unsigned int i;
422 for (i = 0; i < DMA_CHANS; i++)
423 if (this->dma_chans[i]) {
424 dma_release_channel(this->dma_chans[i]);
425 this->dma_chans[i] = NULL;
426 }
427}
428
Bill Pemberton06f25512012-11-19 13:23:07 -0500429static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800430{
431 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400432 struct resource *r_dma;
433 struct device_node *dn;
Huang Shijie513d57e2012-07-17 14:14:02 +0800434 u32 dma_channel;
435 int ret;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400436 struct dma_chan *dma_chan;
437 dma_cap_mask_t mask;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800438
Huang Shijiee10db1f2012-05-04 21:42:05 -0400439 /* dma channel, we only use the first one. */
440 dn = pdev->dev.of_node;
441 ret = of_property_read_u32(dn, "fsl,gpmi-dma-channel", &dma_channel);
442 if (ret) {
443 pr_err("unable to get DMA channel from dt.\n");
444 goto acquire_err;
445 }
446 this->private = (void *)dma_channel;
447
448 /* gpmi dma interrupt */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800449 r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
450 GPMI_NAND_DMA_INTERRUPT_RES_NAME);
Huang Shijiee10db1f2012-05-04 21:42:05 -0400451 if (!r_dma) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800452 pr_err("Can't get resource for DMA\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400453 goto acquire_err;
454 }
455 this->dma_data.chan_irq = r_dma->start;
456
457 /* request dma channel */
458 dma_cap_zero(mask);
459 dma_cap_set(DMA_SLAVE, mask);
460
461 dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
462 if (!dma_chan) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530463 pr_err("Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400464 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800465 }
466
Huang Shijiee10db1f2012-05-04 21:42:05 -0400467 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800468 return 0;
469
470acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800471 release_dma_channels(this);
472 return -EINVAL;
473}
474
Huang Shijieff506172012-07-02 21:39:32 -0400475static void gpmi_put_clks(struct gpmi_nand_data *this)
476{
477 struct resources *r = &this->resources;
478 struct clk *clk;
479 int i;
480
481 for (i = 0; i < GPMI_CLK_MAX; i++) {
482 clk = r->clock[i];
483 if (clk) {
484 clk_put(clk);
485 r->clock[i] = NULL;
486 }
487 }
488}
489
490static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
491 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
492};
493
Bill Pemberton06f25512012-11-19 13:23:07 -0500494static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400495{
496 struct resources *r = &this->resources;
497 char **extra_clks = NULL;
498 struct clk *clk;
499 int i;
500
501 /* The main clock is stored in the first. */
502 r->clock[0] = clk_get(this->dev, "gpmi_io");
503 if (IS_ERR(r->clock[0]))
504 goto err_clock;
505
506 /* Get extra clocks */
507 if (GPMI_IS_MX6Q(this))
508 extra_clks = extra_clks_for_mx6q;
509 if (!extra_clks)
510 return 0;
511
512 for (i = 1; i < GPMI_CLK_MAX; i++) {
513 if (extra_clks[i - 1] == NULL)
514 break;
515
516 clk = clk_get(this->dev, extra_clks[i - 1]);
517 if (IS_ERR(clk))
518 goto err_clock;
519
520 r->clock[i] = clk;
521 }
522
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800523 if (GPMI_IS_MX6Q(this))
Huang Shijieff506172012-07-02 21:39:32 -0400524 /*
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800525 * Set the default value for the gpmi clock in mx6q:
Huang Shijieff506172012-07-02 21:39:32 -0400526 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800527 * If you want to use the ONFI nand which is in the
528 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400529 */
530 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800531
Huang Shijieff506172012-07-02 21:39:32 -0400532 return 0;
533
534err_clock:
535 dev_dbg(this->dev, "failed in finding the clocks.\n");
536 gpmi_put_clks(this);
537 return -ENOMEM;
538}
539
Bill Pemberton06f25512012-11-19 13:23:07 -0500540static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800541{
Shawn Guo39febc02012-05-06 22:57:41 +0800542 struct pinctrl *pinctrl;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800543 int ret;
544
545 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
546 if (ret)
547 goto exit_regs;
548
549 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
550 if (ret)
551 goto exit_regs;
552
553 ret = acquire_bch_irq(this, bch_irq);
554 if (ret)
555 goto exit_regs;
556
557 ret = acquire_dma_channels(this);
558 if (ret)
559 goto exit_dma_channels;
560
Shawn Guo3e48b1b2012-05-19 21:06:13 +0800561 pinctrl = devm_pinctrl_get_select_default(&this->pdev->dev);
Shawn Guo39febc02012-05-06 22:57:41 +0800562 if (IS_ERR(pinctrl)) {
563 ret = PTR_ERR(pinctrl);
564 goto exit_pin;
565 }
566
Huang Shijieff506172012-07-02 21:39:32 -0400567 ret = gpmi_get_clks(this);
568 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800569 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800570 return 0;
571
572exit_clock:
Shawn Guo39febc02012-05-06 22:57:41 +0800573exit_pin:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800574 release_dma_channels(this);
575exit_dma_channels:
576 release_bch_irq(this);
577exit_regs:
578 release_register_block(this);
579 return ret;
580}
581
582static void release_resources(struct gpmi_nand_data *this)
583{
Huang Shijieff506172012-07-02 21:39:32 -0400584 gpmi_put_clks(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800585 release_register_block(this);
586 release_bch_irq(this);
587 release_dma_channels(this);
588}
589
Bill Pemberton06f25512012-11-19 13:23:07 -0500590static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800591{
592 int ret;
593
594 /*
595 * This structure contains the "safe" GPMI timing that should succeed
596 * with any NAND Flash device
597 * (although, with less-than-optimal performance).
598 */
599 struct nand_timing safe_timing = {
600 .data_setup_in_ns = 80,
601 .data_hold_in_ns = 60,
602 .address_setup_in_ns = 25,
603 .gpmi_sample_delay_in_ns = 6,
604 .tREA_in_ns = -1,
605 .tRLOH_in_ns = -1,
606 .tRHOH_in_ns = -1,
607 };
608
609 /* Initialize the hardwares. */
610 ret = gpmi_init(this);
611 if (ret)
612 return ret;
613
614 this->timing = safe_timing;
615 return 0;
616}
617
618static int read_page_prepare(struct gpmi_nand_data *this,
619 void *destination, unsigned length,
620 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
621 void **use_virt, dma_addr_t *use_phys)
622{
623 struct device *dev = this->dev;
624
625 if (virt_addr_valid(destination)) {
626 dma_addr_t dest_phys;
627
628 dest_phys = dma_map_single(dev, destination,
629 length, DMA_FROM_DEVICE);
630 if (dma_mapping_error(dev, dest_phys)) {
631 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530632 pr_err("%s, Alternate buffer is too small\n",
633 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800634 return -ENOMEM;
635 }
636 goto map_failed;
637 }
638 *use_virt = destination;
639 *use_phys = dest_phys;
640 this->direct_dma_map_ok = true;
641 return 0;
642 }
643
644map_failed:
645 *use_virt = alt_virt;
646 *use_phys = alt_phys;
647 this->direct_dma_map_ok = false;
648 return 0;
649}
650
651static inline void read_page_end(struct gpmi_nand_data *this,
652 void *destination, unsigned length,
653 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
654 void *used_virt, dma_addr_t used_phys)
655{
656 if (this->direct_dma_map_ok)
657 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
658}
659
660static inline void read_page_swap_end(struct gpmi_nand_data *this,
661 void *destination, unsigned length,
662 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
663 void *used_virt, dma_addr_t used_phys)
664{
665 if (!this->direct_dma_map_ok)
666 memcpy(destination, alt_virt, length);
667}
668
669static int send_page_prepare(struct gpmi_nand_data *this,
670 const void *source, unsigned length,
671 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
672 const void **use_virt, dma_addr_t *use_phys)
673{
674 struct device *dev = this->dev;
675
676 if (virt_addr_valid(source)) {
677 dma_addr_t source_phys;
678
679 source_phys = dma_map_single(dev, (void *)source, length,
680 DMA_TO_DEVICE);
681 if (dma_mapping_error(dev, source_phys)) {
682 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530683 pr_err("%s, Alternate buffer is too small\n",
684 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800685 return -ENOMEM;
686 }
687 goto map_failed;
688 }
689 *use_virt = source;
690 *use_phys = source_phys;
691 return 0;
692 }
693map_failed:
694 /*
695 * Copy the content of the source buffer into the alternate
696 * buffer and set up the return values accordingly.
697 */
698 memcpy(alt_virt, source, length);
699
700 *use_virt = alt_virt;
701 *use_phys = alt_phys;
702 return 0;
703}
704
705static void send_page_end(struct gpmi_nand_data *this,
706 const void *source, unsigned length,
707 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
708 const void *used_virt, dma_addr_t used_phys)
709{
710 struct device *dev = this->dev;
711 if (used_virt == source)
712 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
713}
714
715static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
716{
717 struct device *dev = this->dev;
718
719 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
720 dma_free_coherent(dev, this->page_buffer_size,
721 this->page_buffer_virt,
722 this->page_buffer_phys);
723 kfree(this->cmd_buffer);
724 kfree(this->data_buffer_dma);
725
726 this->cmd_buffer = NULL;
727 this->data_buffer_dma = NULL;
728 this->page_buffer_virt = NULL;
729 this->page_buffer_size = 0;
730}
731
732/* Allocate the DMA buffers */
733static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
734{
735 struct bch_geometry *geo = &this->bch_geometry;
736 struct device *dev = this->dev;
737
738 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800739 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800740 if (this->cmd_buffer == NULL)
741 goto error_alloc;
742
743 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800744 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800745 if (this->data_buffer_dma == NULL)
746 goto error_alloc;
747
748 /*
749 * [3] Allocate the page buffer.
750 *
751 * Both the payload buffer and the auxiliary buffer must appear on
752 * 32-bit boundaries. We presume the size of the payload buffer is a
753 * power of two and is much larger than four, which guarantees the
754 * auxiliary buffer will appear on a 32-bit boundary.
755 */
756 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
757 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
758 &this->page_buffer_phys, GFP_DMA);
759 if (!this->page_buffer_virt)
760 goto error_alloc;
761
762
763 /* Slice up the page buffer. */
764 this->payload_virt = this->page_buffer_virt;
765 this->payload_phys = this->page_buffer_phys;
766 this->auxiliary_virt = this->payload_virt + geo->payload_size;
767 this->auxiliary_phys = this->payload_phys + geo->payload_size;
768 return 0;
769
770error_alloc:
771 gpmi_free_dma_buffer(this);
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530772 pr_err("Error allocating DMA buffers!\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800773 return -ENOMEM;
774}
775
776static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
777{
778 struct nand_chip *chip = mtd->priv;
779 struct gpmi_nand_data *this = chip->priv;
780 int ret;
781
782 /*
783 * Every operation begins with a command byte and a series of zero or
784 * more address bytes. These are distinguished by either the Address
785 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
786 * asserted. When MTD is ready to execute the command, it will deassert
787 * both latch enables.
788 *
789 * Rather than run a separate DMA operation for every single byte, we
790 * queue them up and run a single DMA operation for the entire series
791 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
792 */
793 if ((ctrl & (NAND_ALE | NAND_CLE))) {
794 if (data != NAND_CMD_NONE)
795 this->cmd_buffer[this->command_length++] = data;
796 return;
797 }
798
799 if (!this->command_length)
800 return;
801
802 ret = gpmi_send_command(this);
803 if (ret)
804 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
805
806 this->command_length = 0;
807}
808
809static int gpmi_dev_ready(struct mtd_info *mtd)
810{
811 struct nand_chip *chip = mtd->priv;
812 struct gpmi_nand_data *this = chip->priv;
813
814 return gpmi_is_ready(this, this->current_chip);
815}
816
817static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
818{
819 struct nand_chip *chip = mtd->priv;
820 struct gpmi_nand_data *this = chip->priv;
821
822 if ((this->current_chip < 0) && (chipnr >= 0))
823 gpmi_begin(this);
824 else if ((this->current_chip >= 0) && (chipnr < 0))
825 gpmi_end(this);
826
827 this->current_chip = chipnr;
828}
829
830static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
831{
832 struct nand_chip *chip = mtd->priv;
833 struct gpmi_nand_data *this = chip->priv;
834
835 pr_debug("len is %d\n", len);
836 this->upper_buf = buf;
837 this->upper_len = len;
838
839 gpmi_read_data(this);
840}
841
842static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
843{
844 struct nand_chip *chip = mtd->priv;
845 struct gpmi_nand_data *this = chip->priv;
846
847 pr_debug("len is %d\n", len);
848 this->upper_buf = (uint8_t *)buf;
849 this->upper_len = len;
850
851 gpmi_send_data(this);
852}
853
854static uint8_t gpmi_read_byte(struct mtd_info *mtd)
855{
856 struct nand_chip *chip = mtd->priv;
857 struct gpmi_nand_data *this = chip->priv;
858 uint8_t *buf = this->data_buffer_dma;
859
860 gpmi_read_buf(mtd, buf, 1);
861 return buf[0];
862}
863
864/*
865 * Handles block mark swapping.
866 * It can be called in swapping the block mark, or swapping it back,
867 * because the the operations are the same.
868 */
869static void block_mark_swapping(struct gpmi_nand_data *this,
870 void *payload, void *auxiliary)
871{
872 struct bch_geometry *nfc_geo = &this->bch_geometry;
873 unsigned char *p;
874 unsigned char *a;
875 unsigned int bit;
876 unsigned char mask;
877 unsigned char from_data;
878 unsigned char from_oob;
879
880 if (!this->swap_block_mark)
881 return;
882
883 /*
884 * If control arrives here, we're swapping. Make some convenience
885 * variables.
886 */
887 bit = nfc_geo->block_mark_bit_offset;
888 p = payload + nfc_geo->block_mark_byte_offset;
889 a = auxiliary;
890
891 /*
892 * Get the byte from the data area that overlays the block mark. Since
893 * the ECC engine applies its own view to the bits in the page, the
894 * physical block mark won't (in general) appear on a byte boundary in
895 * the data.
896 */
897 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
898
899 /* Get the byte from the OOB. */
900 from_oob = a[0];
901
902 /* Swap them. */
903 a[0] = from_data;
904
905 mask = (0x1 << bit) - 1;
906 p[0] = (p[0] & mask) | (from_oob << bit);
907
908 mask = ~0 << bit;
909 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
910}
911
912static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700913 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800914{
915 struct gpmi_nand_data *this = chip->priv;
916 struct bch_geometry *nfc_geo = &this->bch_geometry;
917 void *payload_virt;
918 dma_addr_t payload_phys;
919 void *auxiliary_virt;
920 dma_addr_t auxiliary_phys;
921 unsigned int i;
922 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -0600923 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800924 int ret;
925
926 pr_debug("page number is : %d\n", page);
927 ret = read_page_prepare(this, buf, mtd->writesize,
928 this->payload_virt, this->payload_phys,
929 nfc_geo->payload_size,
930 &payload_virt, &payload_phys);
931 if (ret) {
932 pr_err("Inadequate DMA buffer\n");
933 ret = -ENOMEM;
934 return ret;
935 }
936 auxiliary_virt = this->auxiliary_virt;
937 auxiliary_phys = this->auxiliary_phys;
938
939 /* go! */
940 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
941 read_page_end(this, buf, mtd->writesize,
942 this->payload_virt, this->payload_phys,
943 nfc_geo->payload_size,
944 payload_virt, payload_phys);
945 if (ret) {
946 pr_err("Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -0600947 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800948 }
949
950 /* handle the block mark swapping */
951 block_mark_swapping(this, payload_virt, auxiliary_virt);
952
953 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -0600954 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800955
956 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
957 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
958 continue;
959
960 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -0600961 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800962 continue;
963 }
Zach Sadeckib23b7462012-12-13 20:36:29 -0600964 mtd->ecc_stats.corrected += *status;
965 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800966 }
967
Brian Norris7725cc82012-05-02 10:15:02 -0700968 if (oob_required) {
969 /*
970 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
971 * for details about our policy for delivering the OOB.
972 *
973 * We fill the caller's buffer with set bits, and then copy the
974 * block mark to th caller's buffer. Note that, if block mark
975 * swapping was necessary, it has already been done, so we can
976 * rely on the first byte of the auxiliary buffer to contain
977 * the block mark.
978 */
979 memset(chip->oob_poi, ~0, mtd->oobsize);
980 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -0700981 }
Sascha Hauer6023813a2012-06-26 17:26:16 +0200982
983 read_page_swap_end(this, buf, mtd->writesize,
984 this->payload_virt, this->payload_phys,
985 nfc_geo->payload_size,
986 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -0600987
988 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800989}
990
Josh Wufdbad98d2012-06-25 18:07:45 +0800991static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700992 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800993{
994 struct gpmi_nand_data *this = chip->priv;
995 struct bch_geometry *nfc_geo = &this->bch_geometry;
996 const void *payload_virt;
997 dma_addr_t payload_phys;
998 const void *auxiliary_virt;
999 dma_addr_t auxiliary_phys;
1000 int ret;
1001
1002 pr_debug("ecc write page.\n");
1003 if (this->swap_block_mark) {
1004 /*
1005 * If control arrives here, we're doing block mark swapping.
1006 * Since we can't modify the caller's buffers, we must copy them
1007 * into our own.
1008 */
1009 memcpy(this->payload_virt, buf, mtd->writesize);
1010 payload_virt = this->payload_virt;
1011 payload_phys = this->payload_phys;
1012
1013 memcpy(this->auxiliary_virt, chip->oob_poi,
1014 nfc_geo->auxiliary_size);
1015 auxiliary_virt = this->auxiliary_virt;
1016 auxiliary_phys = this->auxiliary_phys;
1017
1018 /* Handle block mark swapping. */
1019 block_mark_swapping(this,
1020 (void *) payload_virt, (void *) auxiliary_virt);
1021 } else {
1022 /*
1023 * If control arrives here, we're not doing block mark swapping,
1024 * so we can to try and use the caller's buffers.
1025 */
1026 ret = send_page_prepare(this,
1027 buf, mtd->writesize,
1028 this->payload_virt, this->payload_phys,
1029 nfc_geo->payload_size,
1030 &payload_virt, &payload_phys);
1031 if (ret) {
1032 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001033 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001034 }
1035
1036 ret = send_page_prepare(this,
1037 chip->oob_poi, mtd->oobsize,
1038 this->auxiliary_virt, this->auxiliary_phys,
1039 nfc_geo->auxiliary_size,
1040 &auxiliary_virt, &auxiliary_phys);
1041 if (ret) {
1042 pr_err("Inadequate auxiliary DMA buffer\n");
1043 goto exit_auxiliary;
1044 }
1045 }
1046
1047 /* Ask the NFC. */
1048 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1049 if (ret)
1050 pr_err("Error in ECC-based write: %d\n", ret);
1051
1052 if (!this->swap_block_mark) {
1053 send_page_end(this, chip->oob_poi, mtd->oobsize,
1054 this->auxiliary_virt, this->auxiliary_phys,
1055 nfc_geo->auxiliary_size,
1056 auxiliary_virt, auxiliary_phys);
1057exit_auxiliary:
1058 send_page_end(this, buf, mtd->writesize,
1059 this->payload_virt, this->payload_phys,
1060 nfc_geo->payload_size,
1061 payload_virt, payload_phys);
1062 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001063
1064 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001065}
1066
1067/*
1068 * There are several places in this driver where we have to handle the OOB and
1069 * block marks. This is the function where things are the most complicated, so
1070 * this is where we try to explain it all. All the other places refer back to
1071 * here.
1072 *
1073 * These are the rules, in order of decreasing importance:
1074 *
1075 * 1) Nothing the caller does can be allowed to imperil the block mark.
1076 *
1077 * 2) In read operations, the first byte of the OOB we return must reflect the
1078 * true state of the block mark, no matter where that block mark appears in
1079 * the physical page.
1080 *
1081 * 3) ECC-based read operations return an OOB full of set bits (since we never
1082 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1083 * return).
1084 *
1085 * 4) "Raw" read operations return a direct view of the physical bytes in the
1086 * page, using the conventional definition of which bytes are data and which
1087 * are OOB. This gives the caller a way to see the actual, physical bytes
1088 * in the page, without the distortions applied by our ECC engine.
1089 *
1090 *
1091 * What we do for this specific read operation depends on two questions:
1092 *
1093 * 1) Are we doing a "raw" read, or an ECC-based read?
1094 *
1095 * 2) Are we using block mark swapping or transcription?
1096 *
1097 * There are four cases, illustrated by the following Karnaugh map:
1098 *
1099 * | Raw | ECC-based |
1100 * -------------+-------------------------+-------------------------+
1101 * | Read the conventional | |
1102 * | OOB at the end of the | |
1103 * Swapping | page and return it. It | |
1104 * | contains exactly what | |
1105 * | we want. | Read the block mark and |
1106 * -------------+-------------------------+ return it in a buffer |
1107 * | Read the conventional | full of set bits. |
1108 * | OOB at the end of the | |
1109 * | page and also the block | |
1110 * Transcribing | mark in the metadata. | |
1111 * | Copy the block mark | |
1112 * | into the first byte of | |
1113 * | the OOB. | |
1114 * -------------+-------------------------+-------------------------+
1115 *
1116 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1117 * giving an accurate view of the actual, physical bytes in the page (we're
1118 * overwriting the block mark). That's OK because it's more important to follow
1119 * rule #2.
1120 *
1121 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1122 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1123 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1124 * ECC-based or raw view of the page is implicit in which function it calls
1125 * (there is a similar pair of ECC-based/raw functions for writing).
1126 *
Brian Norris271b874b2012-05-11 13:30:35 -07001127 * FIXME: The following paragraph is incorrect, now that there exist
1128 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1129 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001130 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1131 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1132 * caller wants an ECC-based or raw view of the page is not propagated down to
1133 * this driver.
1134 */
1135static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001136 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001137{
1138 struct gpmi_nand_data *this = chip->priv;
1139
1140 pr_debug("page number is %d\n", page);
1141 /* clear the OOB buffer */
1142 memset(chip->oob_poi, ~0, mtd->oobsize);
1143
1144 /* Read out the conventional OOB. */
1145 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1146 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1147
1148 /*
1149 * Now, we want to make sure the block mark is correct. In the
1150 * Swapping/Raw case, we already have it. Otherwise, we need to
1151 * explicitly read it.
1152 */
1153 if (!this->swap_block_mark) {
1154 /* Read the block mark into the first byte of the OOB buffer. */
1155 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1156 chip->oob_poi[0] = chip->read_byte(mtd);
1157 }
1158
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001159 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001160}
1161
1162static int
1163gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1164{
1165 /*
1166 * The BCH will use all the (page + oob).
1167 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1168 * But it can not stop some ioctls such MEMWRITEOOB which uses
Brian Norris0612b9d2011-08-30 18:45:40 -07001169 * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
Huang Shijie10a2bca2011-09-08 10:47:09 +08001170 * these ioctls too.
1171 */
1172 return -EPERM;
1173}
1174
1175static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1176{
1177 struct nand_chip *chip = mtd->priv;
1178 struct gpmi_nand_data *this = chip->priv;
1179 int block, ret = 0;
1180 uint8_t *block_mark;
1181 int column, page, status, chipnr;
1182
1183 /* Get block number */
1184 block = (int)(ofs >> chip->bbt_erase_shift);
1185 if (chip->bbt)
1186 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1187
1188 /* Do we have a flash based bad block table ? */
Wolfram Sang52899662012-01-31 13:10:43 +01001189 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001190 ret = nand_update_bbt(mtd, ofs);
1191 else {
1192 chipnr = (int)(ofs >> chip->chip_shift);
1193 chip->select_chip(mtd, chipnr);
1194
1195 column = this->swap_block_mark ? mtd->writesize : 0;
1196
1197 /* Write the block mark. */
1198 block_mark = this->data_buffer_dma;
1199 block_mark[0] = 0; /* bad block marker */
1200
1201 /* Shift to get page */
1202 page = (int)(ofs >> chip->page_shift);
1203
1204 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1205 chip->write_buf(mtd, block_mark, 1);
1206 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1207
1208 status = chip->waitfunc(mtd, chip);
1209 if (status & NAND_STATUS_FAIL)
1210 ret = -EIO;
1211
1212 chip->select_chip(mtd, -1);
1213 }
1214 if (!ret)
1215 mtd->ecc_stats.badblocks++;
1216
1217 return ret;
1218}
1219
Wolfram Sanga78da282012-03-21 19:29:17 +01001220static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001221{
1222 struct boot_rom_geometry *geometry = &this->rom_geometry;
1223
1224 /*
1225 * Set the boot block stride size.
1226 *
1227 * In principle, we should be reading this from the OTP bits, since
1228 * that's where the ROM is going to get it. In fact, we don't have any
1229 * way to read the OTP bits, so we go with the default and hope for the
1230 * best.
1231 */
1232 geometry->stride_size_in_pages = 64;
1233
1234 /*
1235 * Set the search area stride exponent.
1236 *
1237 * In principle, we should be reading this from the OTP bits, since
1238 * that's where the ROM is going to get it. In fact, we don't have any
1239 * way to read the OTP bits, so we go with the default and hope for the
1240 * best.
1241 */
1242 geometry->search_area_stride_exponent = 2;
1243 return 0;
1244}
1245
1246static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001247static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001248{
1249 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1250 struct device *dev = this->dev;
1251 struct mtd_info *mtd = &this->mtd;
1252 struct nand_chip *chip = &this->nand;
1253 unsigned int search_area_size_in_strides;
1254 unsigned int stride;
1255 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001256 uint8_t *buffer = chip->buffers->databuf;
1257 int saved_chip_number;
1258 int found_an_ncb_fingerprint = false;
1259
1260 /* Compute the number of strides in a search area. */
1261 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1262
1263 saved_chip_number = this->current_chip;
1264 chip->select_chip(mtd, 0);
1265
1266 /*
1267 * Loop through the first search area, looking for the NCB fingerprint.
1268 */
1269 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1270
1271 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001272 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001273 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001274
1275 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1276
1277 /*
1278 * Read the NCB fingerprint. The fingerprint is four bytes long
1279 * and starts in the 12th byte of the page.
1280 */
1281 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1282 chip->read_buf(mtd, buffer, strlen(fingerprint));
1283
1284 /* Look for the fingerprint. */
1285 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1286 found_an_ncb_fingerprint = true;
1287 break;
1288 }
1289
1290 }
1291
1292 chip->select_chip(mtd, saved_chip_number);
1293
1294 if (found_an_ncb_fingerprint)
1295 dev_dbg(dev, "\tFound a fingerprint\n");
1296 else
1297 dev_dbg(dev, "\tNo fingerprint found\n");
1298 return found_an_ncb_fingerprint;
1299}
1300
1301/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001302static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001303{
1304 struct device *dev = this->dev;
1305 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1306 struct mtd_info *mtd = &this->mtd;
1307 struct nand_chip *chip = &this->nand;
1308 unsigned int block_size_in_pages;
1309 unsigned int search_area_size_in_strides;
1310 unsigned int search_area_size_in_pages;
1311 unsigned int search_area_size_in_blocks;
1312 unsigned int block;
1313 unsigned int stride;
1314 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001315 uint8_t *buffer = chip->buffers->databuf;
1316 int saved_chip_number;
1317 int status;
1318
1319 /* Compute the search area geometry. */
1320 block_size_in_pages = mtd->erasesize / mtd->writesize;
1321 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1322 search_area_size_in_pages = search_area_size_in_strides *
1323 rom_geo->stride_size_in_pages;
1324 search_area_size_in_blocks =
1325 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1326 block_size_in_pages;
1327
1328 dev_dbg(dev, "Search Area Geometry :\n");
1329 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1330 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1331 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1332
1333 /* Select chip 0. */
1334 saved_chip_number = this->current_chip;
1335 chip->select_chip(mtd, 0);
1336
1337 /* Loop over blocks in the first search area, erasing them. */
1338 dev_dbg(dev, "Erasing the search area...\n");
1339
1340 for (block = 0; block < search_area_size_in_blocks; block++) {
1341 /* Compute the page address. */
1342 page = block * block_size_in_pages;
1343
1344 /* Erase this block. */
1345 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1346 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1347 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1348
1349 /* Wait for the erase to finish. */
1350 status = chip->waitfunc(mtd, chip);
1351 if (status & NAND_STATUS_FAIL)
1352 dev_err(dev, "[%s] Erase failed.\n", __func__);
1353 }
1354
1355 /* Write the NCB fingerprint into the page buffer. */
1356 memset(buffer, ~0, mtd->writesize);
1357 memset(chip->oob_poi, ~0, mtd->oobsize);
1358 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1359
1360 /* Loop through the first search area, writing NCB fingerprints. */
1361 dev_dbg(dev, "Writing NCB fingerprints...\n");
1362 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001363 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001364 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001365
1366 /* Write the first page of the current stride. */
1367 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1368 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001369 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001370 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1371
1372 /* Wait for the write to finish. */
1373 status = chip->waitfunc(mtd, chip);
1374 if (status & NAND_STATUS_FAIL)
1375 dev_err(dev, "[%s] Write failed.\n", __func__);
1376 }
1377
1378 /* Deselect chip 0. */
1379 chip->select_chip(mtd, saved_chip_number);
1380 return 0;
1381}
1382
Wolfram Sanga78da282012-03-21 19:29:17 +01001383static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001384{
1385 struct device *dev = this->dev;
1386 struct nand_chip *chip = &this->nand;
1387 struct mtd_info *mtd = &this->mtd;
1388 unsigned int block_count;
1389 unsigned int block;
1390 int chipnr;
1391 int page;
1392 loff_t byte;
1393 uint8_t block_mark;
1394 int ret = 0;
1395
1396 /*
1397 * If control arrives here, we can't use block mark swapping, which
1398 * means we're forced to use transcription. First, scan for the
1399 * transcription stamp. If we find it, then we don't have to do
1400 * anything -- the block marks are already transcribed.
1401 */
1402 if (mx23_check_transcription_stamp(this))
1403 return 0;
1404
1405 /*
1406 * If control arrives here, we couldn't find a transcription stamp, so
1407 * so we presume the block marks are in the conventional location.
1408 */
1409 dev_dbg(dev, "Transcribing bad block marks...\n");
1410
1411 /* Compute the number of blocks in the entire medium. */
1412 block_count = chip->chipsize >> chip->phys_erase_shift;
1413
1414 /*
1415 * Loop over all the blocks in the medium, transcribing block marks as
1416 * we go.
1417 */
1418 for (block = 0; block < block_count; block++) {
1419 /*
1420 * Compute the chip, page and byte addresses for this block's
1421 * conventional mark.
1422 */
1423 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1424 page = block << (chip->phys_erase_shift - chip->page_shift);
1425 byte = block << chip->phys_erase_shift;
1426
1427 /* Send the command to read the conventional block mark. */
1428 chip->select_chip(mtd, chipnr);
1429 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1430 block_mark = chip->read_byte(mtd);
1431 chip->select_chip(mtd, -1);
1432
1433 /*
1434 * Check if the block is marked bad. If so, we need to mark it
1435 * again, but this time the result will be a mark in the
1436 * location where we transcribe block marks.
1437 */
1438 if (block_mark != 0xff) {
1439 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1440 ret = chip->block_markbad(mtd, byte);
1441 if (ret)
1442 dev_err(dev, "Failed to mark block bad with "
1443 "ret %d\n", ret);
1444 }
1445 }
1446
1447 /* Write the stamp that indicates we've transcribed the block marks. */
1448 mx23_write_transcription_stamp(this);
1449 return 0;
1450}
1451
Wolfram Sanga78da282012-03-21 19:29:17 +01001452static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001453{
1454 nand_boot_set_geometry(this);
1455
1456 /* This is ROM arch-specific initilization before the BBT scanning. */
1457 if (GPMI_IS_MX23(this))
1458 return mx23_boot_init(this);
1459 return 0;
1460}
1461
Wolfram Sanga78da282012-03-21 19:29:17 +01001462static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001463{
1464 int ret;
1465
1466 /* Free the temporary DMA memory for reading ID. */
1467 gpmi_free_dma_buffer(this);
1468
1469 /* Set up the NFC geometry which is used by BCH. */
1470 ret = bch_set_geometry(this);
1471 if (ret) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +05301472 pr_err("Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001473 return ret;
1474 }
1475
1476 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1477 return gpmi_alloc_dma_buffer(this);
1478}
1479
1480static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1481{
1482 int ret;
1483
1484 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1485 if (GPMI_IS_MX23(this))
1486 this->swap_block_mark = false;
1487 else
1488 this->swap_block_mark = true;
1489
1490 /* Set up the medium geometry */
1491 ret = gpmi_set_geometry(this);
1492 if (ret)
1493 return ret;
1494
Marek Vasut5636ce02012-05-21 22:59:27 +02001495 /* Adjust the ECC strength according to the chip. */
1496 this->nand.ecc.strength = this->bch_geometry.ecc_strength;
1497 this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
Huang Shijiee0dd89c2012-07-03 16:24:33 +08001498 this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
Marek Vasut5636ce02012-05-21 22:59:27 +02001499
Huang Shijie10a2bca2011-09-08 10:47:09 +08001500 /* NAND boot init, depends on the gpmi_set_geometry(). */
1501 return nand_boot_init(this);
1502}
1503
1504static int gpmi_scan_bbt(struct mtd_info *mtd)
1505{
1506 struct nand_chip *chip = mtd->priv;
1507 struct gpmi_nand_data *this = chip->priv;
1508 int ret;
1509
1510 /* Prepare for the BBT scan. */
1511 ret = gpmi_pre_bbt_scan(this);
1512 if (ret)
1513 return ret;
1514
Huang Shijie995fbbf2012-09-13 14:57:59 +08001515 /*
1516 * Can we enable the extra features? such as EDO or Sync mode.
1517 *
1518 * We do not check the return value now. That's means if we fail in
1519 * enable the extra features, we still can run in the normal way.
1520 */
1521 gpmi_extra_init(this);
1522
Huang Shijie10a2bca2011-09-08 10:47:09 +08001523 /* use the default BBT implementation */
1524 return nand_default_bbt(mtd);
1525}
1526
Huang Shijie513d57e2012-07-17 14:14:02 +08001527static void gpmi_nfc_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001528{
1529 nand_release(&this->mtd);
1530 gpmi_free_dma_buffer(this);
1531}
1532
Bill Pemberton06f25512012-11-19 13:23:07 -05001533static int gpmi_nfc_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001534{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001535 struct mtd_info *mtd = &this->mtd;
1536 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001537 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001538 int ret;
1539
1540 /* init current chip */
1541 this->current_chip = -1;
1542
1543 /* init the MTD data structures */
1544 mtd->priv = chip;
1545 mtd->name = "gpmi-nand";
1546 mtd->owner = THIS_MODULE;
1547
1548 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1549 chip->priv = this;
1550 chip->select_chip = gpmi_select_chip;
1551 chip->cmd_ctrl = gpmi_cmd_ctrl;
1552 chip->dev_ready = gpmi_dev_ready;
1553 chip->read_byte = gpmi_read_byte;
1554 chip->read_buf = gpmi_read_buf;
1555 chip->write_buf = gpmi_write_buf;
1556 chip->ecc.read_page = gpmi_ecc_read_page;
1557 chip->ecc.write_page = gpmi_ecc_write_page;
1558 chip->ecc.read_oob = gpmi_ecc_read_oob;
1559 chip->ecc.write_oob = gpmi_ecc_write_oob;
1560 chip->scan_bbt = gpmi_scan_bbt;
1561 chip->badblock_pattern = &gpmi_bbt_descr;
1562 chip->block_markbad = gpmi_block_markbad;
1563 chip->options |= NAND_NO_SUBPAGE_WRITE;
1564 chip->ecc.mode = NAND_ECC_HW;
1565 chip->ecc.size = 1;
Marek Vasut5636ce02012-05-21 22:59:27 +02001566 chip->ecc.strength = 8;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001567 chip->ecc.layout = &gpmi_hw_ecclayout;
Huang Shijiec50c6942012-07-03 16:24:32 +08001568 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1569 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001570
1571 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1572 this->bch_geometry.payload_size = 1024;
1573 this->bch_geometry.auxiliary_size = 128;
1574 ret = gpmi_alloc_dma_buffer(this);
1575 if (ret)
1576 goto err_out;
1577
Huang Shijiee10db1f2012-05-04 21:42:05 -04001578 ret = nand_scan(mtd, 1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001579 if (ret) {
1580 pr_err("Chip scan failed\n");
1581 goto err_out;
1582 }
1583
Huang Shijiee10db1f2012-05-04 21:42:05 -04001584 ppdata.of_node = this->pdev->dev.of_node;
1585 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001586 if (ret)
1587 goto err_out;
1588 return 0;
1589
1590err_out:
1591 gpmi_nfc_exit(this);
1592 return ret;
1593}
1594
Huang Shijiee10db1f2012-05-04 21:42:05 -04001595static const struct platform_device_id gpmi_ids[] = {
1596 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1597 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001598 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Huang Shijiee10db1f2012-05-04 21:42:05 -04001599 {},
1600};
1601
1602static const struct of_device_id gpmi_nand_id_table[] = {
1603 {
1604 .compatible = "fsl,imx23-gpmi-nand",
1605 .data = (void *)&gpmi_ids[IS_MX23]
1606 }, {
1607 .compatible = "fsl,imx28-gpmi-nand",
1608 .data = (void *)&gpmi_ids[IS_MX28]
Huang Shijie9013bb42012-05-04 21:42:06 -04001609 }, {
1610 .compatible = "fsl,imx6q-gpmi-nand",
1611 .data = (void *)&gpmi_ids[IS_MX6Q]
Huang Shijiee10db1f2012-05-04 21:42:05 -04001612 }, {}
1613};
1614MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1615
Bill Pemberton06f25512012-11-19 13:23:07 -05001616static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001617{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001618 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001619 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001620 int ret;
1621
Huang Shijiee10db1f2012-05-04 21:42:05 -04001622 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1623 if (of_id) {
1624 pdev->id_entry = of_id->data;
1625 } else {
1626 pr_err("Failed to find the right device id.\n");
1627 return -ENOMEM;
1628 }
1629
Huang Shijie10a2bca2011-09-08 10:47:09 +08001630 this = kzalloc(sizeof(*this), GFP_KERNEL);
1631 if (!this) {
1632 pr_err("Failed to allocate per-device memory\n");
1633 return -ENOMEM;
1634 }
1635
1636 platform_set_drvdata(pdev, this);
1637 this->pdev = pdev;
1638 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001639
1640 ret = acquire_resources(this);
1641 if (ret)
1642 goto exit_acquire_resources;
1643
1644 ret = init_hardware(this);
1645 if (ret)
1646 goto exit_nfc_init;
1647
1648 ret = gpmi_nfc_init(this);
1649 if (ret)
1650 goto exit_nfc_init;
1651
Fabio Estevam490e2802012-09-05 11:35:24 -03001652 dev_info(this->dev, "driver registered.\n");
1653
Huang Shijie10a2bca2011-09-08 10:47:09 +08001654 return 0;
1655
1656exit_nfc_init:
1657 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001658exit_acquire_resources:
1659 platform_set_drvdata(pdev, NULL);
Fabio Estevam490e2802012-09-05 11:35:24 -03001660 dev_err(this->dev, "driver registration failed: %d\n", ret);
Huang Shijie26738dd2013-01-23 16:20:53 +08001661 kfree(this);
Fabio Estevam490e2802012-09-05 11:35:24 -03001662
Huang Shijie10a2bca2011-09-08 10:47:09 +08001663 return ret;
1664}
1665
Bill Pemberton810b7e02012-11-19 13:26:04 -05001666static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001667{
1668 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1669
1670 gpmi_nfc_exit(this);
1671 release_resources(this);
1672 platform_set_drvdata(pdev, NULL);
1673 kfree(this);
1674 return 0;
1675}
1676
Huang Shijie10a2bca2011-09-08 10:47:09 +08001677static struct platform_driver gpmi_nand_driver = {
1678 .driver = {
1679 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001680 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001681 },
1682 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001683 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001684 .id_table = gpmi_ids,
1685};
Fabio Estevam490e2802012-09-05 11:35:24 -03001686module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001687
1688MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1689MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1690MODULE_LICENSE("GPL");