blob: 37508eb350d05864e5f25b0a693824912eef0b1e [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>
Huang Shijiee10db1f2012-05-04 21:42:05 -040029#include <linux/of.h>
30#include <linux/of_device.h>
Huang Shijiec50c6942012-07-03 16:24:32 +080031#include <linux/of_mtd.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080032#include "gpmi-nand.h"
33
Huang Shijie5de0b522012-10-13 13:03:29 -040034/* Resource names for the GPMI NAND driver. */
35#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
36#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
37#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
Huang Shijie5de0b522012-10-13 13:03:29 -040038
Huang Shijie10a2bca2011-09-08 10:47:09 +080039/* add our owner bbt descriptor */
40static uint8_t scan_ff_pattern[] = { 0xff };
41static struct nand_bbt_descr gpmi_bbt_descr = {
42 .options = 0,
43 .offs = 0,
44 .len = 1,
45 .pattern = scan_ff_pattern
46};
47
Huang Shijie7a2b89a2013-09-25 14:58:15 +080048/*
49 * We may change the layout if we can get the ECC info from the datasheet,
50 * else we will use all the (page + OOB).
51 */
Huang Shijie10a2bca2011-09-08 10:47:09 +080052static struct nand_ecclayout gpmi_hw_ecclayout = {
53 .eccbytes = 0,
54 .eccpos = { 0, },
55 .oobfree = { {.offset = 0, .length = 0} }
56};
57
58static irqreturn_t bch_irq(int irq, void *cookie)
59{
60 struct gpmi_nand_data *this = cookie;
61
62 gpmi_clear_bch(this);
63 complete(&this->bch_done);
64 return IRQ_HANDLED;
65}
66
67/*
68 * Calculate the ECC strength by hand:
69 * E : The ECC strength.
70 * G : the length of Galois Field.
71 * N : The chunk count of per page.
72 * O : the oobsize of the NAND chip.
73 * M : the metasize of per page.
74 *
75 * The formula is :
76 * E * G * N
77 * ------------ <= (O - M)
78 * 8
79 *
80 * So, we get E by:
81 * (O - M) * 8
82 * E <= -------------
83 * G * N
84 */
85static inline int get_ecc_strength(struct gpmi_nand_data *this)
86{
87 struct bch_geometry *geo = &this->bch_geometry;
88 struct mtd_info *mtd = &this->mtd;
89 int ecc_strength;
90
91 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
92 / (geo->gf_len * geo->ecc_chunk_count);
93
94 /* We need the minor even number. */
95 return round_down(ecc_strength, 2);
96}
97
Huang Shijie92d0e092013-01-29 09:23:38 +080098static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
99{
100 struct bch_geometry *geo = &this->bch_geometry;
101
102 /* Do the sanity check. */
103 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
104 /* The mx23/mx28 only support the GF13. */
105 if (geo->gf_len == 14)
106 return false;
107
108 if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
109 return false;
110 } else if (GPMI_IS_MX6Q(this)) {
111 if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
112 return false;
113 }
114 return true;
115}
116
Huang Shijie2febcdf2013-05-17 11:17:34 +0800117/*
118 * If we can get the ECC information from the nand chip, we do not
119 * need to calculate them ourselves.
120 *
121 * We may have available oob space in this case.
122 */
123static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
124{
125 struct bch_geometry *geo = &this->bch_geometry;
126 struct mtd_info *mtd = &this->mtd;
127 struct nand_chip *chip = mtd->priv;
128 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
129 unsigned int block_mark_bit_offset;
130
131 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
132 return false;
133
134 switch (chip->ecc_step_ds) {
135 case SZ_512:
136 geo->gf_len = 13;
137 break;
138 case SZ_1K:
139 geo->gf_len = 14;
140 break;
141 default:
142 dev_err(this->dev,
143 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
144 chip->ecc_strength_ds, chip->ecc_step_ds);
145 return false;
146 }
147 geo->ecc_chunk_size = chip->ecc_step_ds;
148 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
149 if (!gpmi_check_ecc(this))
150 return false;
151
152 /* Keep the C >= O */
153 if (geo->ecc_chunk_size < mtd->oobsize) {
154 dev_err(this->dev,
155 "unsupported nand chip. ecc size: %d, oob size : %d\n",
156 chip->ecc_step_ds, mtd->oobsize);
157 return false;
158 }
159
160 /* The default value, see comment in the legacy_set_geometry(). */
161 geo->metadata_size = 10;
162
163 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
164
165 /*
166 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
167 *
168 * | P |
169 * |<----------------------------------------------------->|
170 * | |
171 * | (Block Mark) |
172 * | P' | | | |
173 * |<-------------------------------------------->| D | | O' |
174 * | |<---->| |<--->|
175 * V V V V V
176 * +---+----------+-+----------+-+----------+-+----------+-+-----+
177 * | M | data |E| data |E| data |E| data |E| |
178 * +---+----------+-+----------+-+----------+-+----------+-+-----+
179 * ^ ^
180 * | O |
181 * |<------------>|
182 * | |
183 *
184 * P : the page size for BCH module.
185 * E : The ECC strength.
186 * G : the length of Galois Field.
187 * N : The chunk count of per page.
188 * M : the metasize of per page.
189 * C : the ecc chunk size, aka the "data" above.
190 * P': the nand chip's page size.
191 * O : the nand chip's oob size.
192 * O': the free oob.
193 *
194 * The formula for P is :
195 *
196 * E * G * N
197 * P = ------------ + P' + M
198 * 8
199 *
200 * The position of block mark moves forward in the ECC-based view
201 * of page, and the delta is:
202 *
203 * E * G * (N - 1)
204 * D = (---------------- + M)
205 * 8
206 *
207 * Please see the comment in legacy_set_geometry().
208 * With the condition C >= O , we still can get same result.
209 * So the bit position of the physical block mark within the ECC-based
210 * view of the page is :
211 * (P' - D) * 8
212 */
213 geo->page_size = mtd->writesize + geo->metadata_size +
214 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
215
216 /* The available oob size we have. */
217 if (geo->page_size < mtd->writesize + mtd->oobsize) {
218 of->offset = geo->page_size - mtd->writesize;
219 of->length = mtd->oobsize - of->offset;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800220 }
221
222 geo->payload_size = mtd->writesize;
223
224 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
225 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
226 + ALIGN(geo->ecc_chunk_count, 4);
227
228 if (!this->swap_block_mark)
229 return true;
230
231 /* For bit swap. */
232 block_mark_bit_offset = mtd->writesize * 8 -
233 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
234 + geo->metadata_size * 8);
235
236 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
237 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
238 return true;
239}
240
241static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800242{
243 struct bch_geometry *geo = &this->bch_geometry;
244 struct mtd_info *mtd = &this->mtd;
245 unsigned int metadata_size;
246 unsigned int status_size;
247 unsigned int block_mark_bit_offset;
248
249 /*
250 * The size of the metadata can be changed, though we set it to 10
251 * bytes now. But it can't be too large, because we have to save
252 * enough space for BCH.
253 */
254 geo->metadata_size = 10;
255
256 /* The default for the length of Galois Field. */
257 geo->gf_len = 13;
258
Huang Shijie9ff16f02013-01-25 14:04:07 +0800259 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800260 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800261 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800262 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800263 geo->gf_len = 14;
264 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800265
266 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
267
268 /* We use the same ECC strength for all chunks. */
269 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800270 if (!gpmi_check_ecc(this)) {
271 dev_err(this->dev,
272 "We can not support this nand chip."
273 " Its required ecc strength(%d) is beyond our"
274 " capability(%d).\n", geo->ecc_strength,
275 (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
276 : MXS_ECC_STRENGTH_MAX));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800277 return -EINVAL;
278 }
279
280 geo->page_size = mtd->writesize + mtd->oobsize;
281 geo->payload_size = mtd->writesize;
282
283 /*
284 * The auxiliary buffer contains the metadata and the ECC status. The
285 * metadata is padded to the nearest 32-bit boundary. The ECC status
286 * contains one byte for every ECC chunk, and is also padded to the
287 * nearest 32-bit boundary.
288 */
289 metadata_size = ALIGN(geo->metadata_size, 4);
290 status_size = ALIGN(geo->ecc_chunk_count, 4);
291
292 geo->auxiliary_size = metadata_size + status_size;
293 geo->auxiliary_status_offset = metadata_size;
294
295 if (!this->swap_block_mark)
296 return 0;
297
298 /*
299 * We need to compute the byte and bit offsets of
300 * the physical block mark within the ECC-based view of the page.
301 *
302 * NAND chip with 2K page shows below:
303 * (Block Mark)
304 * | |
305 * | D |
306 * |<---->|
307 * V V
308 * +---+----------+-+----------+-+----------+-+----------+-+
309 * | M | data |E| data |E| data |E| data |E|
310 * +---+----------+-+----------+-+----------+-+----------+-+
311 *
312 * The position of block mark moves forward in the ECC-based view
313 * of page, and the delta is:
314 *
315 * E * G * (N - 1)
316 * D = (---------------- + M)
317 * 8
318 *
319 * With the formula to compute the ECC strength, and the condition
320 * : C >= O (C is the ecc chunk size)
321 *
322 * It's easy to deduce to the following result:
323 *
324 * E * G (O - M) C - M C - M
325 * ----------- <= ------- <= -------- < ---------
326 * 8 N N (N - 1)
327 *
328 * So, we get:
329 *
330 * E * G * (N - 1)
331 * D = (---------------- + M) < C
332 * 8
333 *
334 * The above inequality means the position of block mark
335 * within the ECC-based view of the page is still in the data chunk,
336 * and it's NOT in the ECC bits of the chunk.
337 *
338 * Use the following to compute the bit position of the
339 * physical block mark within the ECC-based view of the page:
340 * (page_size - D) * 8
341 *
342 * --Huang Shijie
343 */
344 block_mark_bit_offset = mtd->writesize * 8 -
345 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
346 + geo->metadata_size * 8);
347
348 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
349 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
350 return 0;
351}
352
Huang Shijie2febcdf2013-05-17 11:17:34 +0800353int common_nfc_set_geometry(struct gpmi_nand_data *this)
354{
David Woodhouse031e2772013-10-25 15:03:59 +0100355 return legacy_set_geometry(this);
Huang Shijie2febcdf2013-05-17 11:17:34 +0800356}
357
Huang Shijie10a2bca2011-09-08 10:47:09 +0800358struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
359{
360 int chipnr = this->current_chip;
361
362 return this->dma_chans[chipnr];
363}
364
365/* Can we use the upper's buffer directly for DMA? */
366void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
367{
368 struct scatterlist *sgl = &this->data_sgl;
369 int ret;
370
371 this->direct_dma_map_ok = true;
372
373 /* first try to map the upper buffer directly */
374 sg_init_one(sgl, this->upper_buf, this->upper_len);
375 ret = dma_map_sg(this->dev, sgl, 1, dr);
376 if (ret == 0) {
377 /* We have to use our own DMA buffer. */
378 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
379
380 if (dr == DMA_TO_DEVICE)
381 memcpy(this->data_buffer_dma, this->upper_buf,
382 this->upper_len);
383
384 ret = dma_map_sg(this->dev, sgl, 1, dr);
385 if (ret == 0)
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530386 pr_err("DMA mapping failed.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800387
388 this->direct_dma_map_ok = false;
389 }
390}
391
392/* This will be called after the DMA operation is finished. */
393static void dma_irq_callback(void *param)
394{
395 struct gpmi_nand_data *this = param;
396 struct completion *dma_c = &this->dma_done;
397
398 complete(dma_c);
399
400 switch (this->dma_type) {
401 case DMA_FOR_COMMAND:
402 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
403 break;
404
405 case DMA_FOR_READ_DATA:
406 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
407 if (this->direct_dma_map_ok == false)
408 memcpy(this->upper_buf, this->data_buffer_dma,
409 this->upper_len);
410 break;
411
412 case DMA_FOR_WRITE_DATA:
413 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
414 break;
415
416 case DMA_FOR_READ_ECC_PAGE:
417 case DMA_FOR_WRITE_ECC_PAGE:
418 /* We have to wait the BCH interrupt to finish. */
419 break;
420
421 default:
422 pr_err("in wrong DMA operation.\n");
423 }
424}
425
426int start_dma_without_bch_irq(struct gpmi_nand_data *this,
427 struct dma_async_tx_descriptor *desc)
428{
429 struct completion *dma_c = &this->dma_done;
430 int err;
431
432 init_completion(dma_c);
433
434 desc->callback = dma_irq_callback;
435 desc->callback_param = this;
436 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800437 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800438
439 /* Wait for the interrupt from the DMA block. */
440 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
441 if (!err) {
442 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
443 gpmi_dump_info(this);
444 return -ETIMEDOUT;
445 }
446 return 0;
447}
448
449/*
450 * This function is used in BCH reading or BCH writing pages.
451 * It will wait for the BCH interrupt as long as ONE second.
452 * Actually, we must wait for two interrupts :
453 * [1] firstly the DMA interrupt and
454 * [2] secondly the BCH interrupt.
455 */
456int start_dma_with_bch_irq(struct gpmi_nand_data *this,
457 struct dma_async_tx_descriptor *desc)
458{
459 struct completion *bch_c = &this->bch_done;
460 int err;
461
462 /* Prepare to receive an interrupt from the BCH block. */
463 init_completion(bch_c);
464
465 /* start the DMA */
466 start_dma_without_bch_irq(this, desc);
467
468 /* Wait for the interrupt from the BCH block. */
469 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
470 if (!err) {
471 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
472 gpmi_dump_info(this);
473 return -ETIMEDOUT;
474 }
475 return 0;
476}
477
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800478static int acquire_register_block(struct gpmi_nand_data *this,
479 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800480{
481 struct platform_device *pdev = this->pdev;
482 struct resources *res = &this->resources;
483 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800484 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800485
486 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
487 if (!r) {
488 pr_err("Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200489 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800490 }
491
492 p = ioremap(r->start, resource_size(r));
493 if (!p) {
494 pr_err("Can't remap %s\n", res_name);
495 return -ENOMEM;
496 }
497
498 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
499 res->gpmi_regs = p;
500 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
501 res->bch_regs = p;
502 else
503 pr_err("unknown resource name : %s\n", res_name);
504
505 return 0;
506}
507
508static void release_register_block(struct gpmi_nand_data *this)
509{
510 struct resources *res = &this->resources;
511 if (res->gpmi_regs)
512 iounmap(res->gpmi_regs);
513 if (res->bch_regs)
514 iounmap(res->bch_regs);
515 res->gpmi_regs = NULL;
516 res->bch_regs = NULL;
517}
518
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800519static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800520{
521 struct platform_device *pdev = this->pdev;
522 struct resources *res = &this->resources;
523 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
524 struct resource *r;
525 int err;
526
527 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
528 if (!r) {
529 pr_err("Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200530 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800531 }
532
533 err = request_irq(r->start, irq_h, 0, res_name, this);
534 if (err) {
535 pr_err("Can't own %s\n", res_name);
536 return err;
537 }
538
539 res->bch_low_interrupt = r->start;
540 res->bch_high_interrupt = r->end;
541 return 0;
542}
543
544static void release_bch_irq(struct gpmi_nand_data *this)
545{
546 struct resources *res = &this->resources;
547 int i = res->bch_low_interrupt;
548
549 for (; i <= res->bch_high_interrupt; i++)
550 free_irq(i, this);
551}
552
Huang Shijie10a2bca2011-09-08 10:47:09 +0800553static void release_dma_channels(struct gpmi_nand_data *this)
554{
555 unsigned int i;
556 for (i = 0; i < DMA_CHANS; i++)
557 if (this->dma_chans[i]) {
558 dma_release_channel(this->dma_chans[i]);
559 this->dma_chans[i] = NULL;
560 }
561}
562
Bill Pemberton06f25512012-11-19 13:23:07 -0500563static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800564{
565 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400566 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400567
568 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800569 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400570 if (!dma_chan) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530571 pr_err("Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400572 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800573 }
574
Huang Shijiee10db1f2012-05-04 21:42:05 -0400575 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800576 return 0;
577
578acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800579 release_dma_channels(this);
580 return -EINVAL;
581}
582
Huang Shijieff506172012-07-02 21:39:32 -0400583static void gpmi_put_clks(struct gpmi_nand_data *this)
584{
585 struct resources *r = &this->resources;
586 struct clk *clk;
587 int i;
588
589 for (i = 0; i < GPMI_CLK_MAX; i++) {
590 clk = r->clock[i];
591 if (clk) {
592 clk_put(clk);
593 r->clock[i] = NULL;
594 }
595 }
596}
597
598static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
599 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
600};
601
Bill Pemberton06f25512012-11-19 13:23:07 -0500602static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400603{
604 struct resources *r = &this->resources;
605 char **extra_clks = NULL;
606 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200607 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400608
609 /* The main clock is stored in the first. */
610 r->clock[0] = clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200611 if (IS_ERR(r->clock[0])) {
612 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400613 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200614 }
Huang Shijieff506172012-07-02 21:39:32 -0400615
616 /* Get extra clocks */
617 if (GPMI_IS_MX6Q(this))
618 extra_clks = extra_clks_for_mx6q;
619 if (!extra_clks)
620 return 0;
621
622 for (i = 1; i < GPMI_CLK_MAX; i++) {
623 if (extra_clks[i - 1] == NULL)
624 break;
625
626 clk = clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200627 if (IS_ERR(clk)) {
628 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400629 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200630 }
Huang Shijieff506172012-07-02 21:39:32 -0400631
632 r->clock[i] = clk;
633 }
634
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800635 if (GPMI_IS_MX6Q(this))
Huang Shijieff506172012-07-02 21:39:32 -0400636 /*
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800637 * Set the default value for the gpmi clock in mx6q:
Huang Shijieff506172012-07-02 21:39:32 -0400638 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800639 * If you want to use the ONFI nand which is in the
640 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400641 */
642 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800643
Huang Shijieff506172012-07-02 21:39:32 -0400644 return 0;
645
646err_clock:
647 dev_dbg(this->dev, "failed in finding the clocks.\n");
648 gpmi_put_clks(this);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200649 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400650}
651
Bill Pemberton06f25512012-11-19 13:23:07 -0500652static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800653{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800654 int ret;
655
656 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
657 if (ret)
658 goto exit_regs;
659
660 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
661 if (ret)
662 goto exit_regs;
663
664 ret = acquire_bch_irq(this, bch_irq);
665 if (ret)
666 goto exit_regs;
667
668 ret = acquire_dma_channels(this);
669 if (ret)
670 goto exit_dma_channels;
671
Huang Shijieff506172012-07-02 21:39:32 -0400672 ret = gpmi_get_clks(this);
673 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800674 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800675 return 0;
676
677exit_clock:
678 release_dma_channels(this);
679exit_dma_channels:
680 release_bch_irq(this);
681exit_regs:
682 release_register_block(this);
683 return ret;
684}
685
686static void release_resources(struct gpmi_nand_data *this)
687{
Huang Shijieff506172012-07-02 21:39:32 -0400688 gpmi_put_clks(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800689 release_register_block(this);
690 release_bch_irq(this);
691 release_dma_channels(this);
692}
693
Bill Pemberton06f25512012-11-19 13:23:07 -0500694static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800695{
696 int ret;
697
698 /*
699 * This structure contains the "safe" GPMI timing that should succeed
700 * with any NAND Flash device
701 * (although, with less-than-optimal performance).
702 */
703 struct nand_timing safe_timing = {
704 .data_setup_in_ns = 80,
705 .data_hold_in_ns = 60,
706 .address_setup_in_ns = 25,
707 .gpmi_sample_delay_in_ns = 6,
708 .tREA_in_ns = -1,
709 .tRLOH_in_ns = -1,
710 .tRHOH_in_ns = -1,
711 };
712
713 /* Initialize the hardwares. */
714 ret = gpmi_init(this);
715 if (ret)
716 return ret;
717
718 this->timing = safe_timing;
719 return 0;
720}
721
722static int read_page_prepare(struct gpmi_nand_data *this,
723 void *destination, unsigned length,
724 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
725 void **use_virt, dma_addr_t *use_phys)
726{
727 struct device *dev = this->dev;
728
729 if (virt_addr_valid(destination)) {
730 dma_addr_t dest_phys;
731
732 dest_phys = dma_map_single(dev, destination,
733 length, DMA_FROM_DEVICE);
734 if (dma_mapping_error(dev, dest_phys)) {
735 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530736 pr_err("%s, Alternate buffer is too small\n",
737 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800738 return -ENOMEM;
739 }
740 goto map_failed;
741 }
742 *use_virt = destination;
743 *use_phys = dest_phys;
744 this->direct_dma_map_ok = true;
745 return 0;
746 }
747
748map_failed:
749 *use_virt = alt_virt;
750 *use_phys = alt_phys;
751 this->direct_dma_map_ok = false;
752 return 0;
753}
754
755static inline void read_page_end(struct gpmi_nand_data *this,
756 void *destination, unsigned length,
757 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
758 void *used_virt, dma_addr_t used_phys)
759{
760 if (this->direct_dma_map_ok)
761 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
762}
763
764static inline void read_page_swap_end(struct gpmi_nand_data *this,
765 void *destination, unsigned length,
766 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
767 void *used_virt, dma_addr_t used_phys)
768{
769 if (!this->direct_dma_map_ok)
770 memcpy(destination, alt_virt, length);
771}
772
773static int send_page_prepare(struct gpmi_nand_data *this,
774 const void *source, unsigned length,
775 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
776 const void **use_virt, dma_addr_t *use_phys)
777{
778 struct device *dev = this->dev;
779
780 if (virt_addr_valid(source)) {
781 dma_addr_t source_phys;
782
783 source_phys = dma_map_single(dev, (void *)source, length,
784 DMA_TO_DEVICE);
785 if (dma_mapping_error(dev, source_phys)) {
786 if (alt_size < length) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530787 pr_err("%s, Alternate buffer is too small\n",
788 __func__);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800789 return -ENOMEM;
790 }
791 goto map_failed;
792 }
793 *use_virt = source;
794 *use_phys = source_phys;
795 return 0;
796 }
797map_failed:
798 /*
799 * Copy the content of the source buffer into the alternate
800 * buffer and set up the return values accordingly.
801 */
802 memcpy(alt_virt, source, length);
803
804 *use_virt = alt_virt;
805 *use_phys = alt_phys;
806 return 0;
807}
808
809static void send_page_end(struct gpmi_nand_data *this,
810 const void *source, unsigned length,
811 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
812 const void *used_virt, dma_addr_t used_phys)
813{
814 struct device *dev = this->dev;
815 if (used_virt == source)
816 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
817}
818
819static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
820{
821 struct device *dev = this->dev;
822
823 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
824 dma_free_coherent(dev, this->page_buffer_size,
825 this->page_buffer_virt,
826 this->page_buffer_phys);
827 kfree(this->cmd_buffer);
828 kfree(this->data_buffer_dma);
829
830 this->cmd_buffer = NULL;
831 this->data_buffer_dma = NULL;
832 this->page_buffer_virt = NULL;
833 this->page_buffer_size = 0;
834}
835
836/* Allocate the DMA buffers */
837static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
838{
839 struct bch_geometry *geo = &this->bch_geometry;
840 struct device *dev = this->dev;
841
842 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800843 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800844 if (this->cmd_buffer == NULL)
845 goto error_alloc;
846
847 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800848 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800849 if (this->data_buffer_dma == NULL)
850 goto error_alloc;
851
852 /*
853 * [3] Allocate the page buffer.
854 *
855 * Both the payload buffer and the auxiliary buffer must appear on
856 * 32-bit boundaries. We presume the size of the payload buffer is a
857 * power of two and is much larger than four, which guarantees the
858 * auxiliary buffer will appear on a 32-bit boundary.
859 */
860 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
861 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
862 &this->page_buffer_phys, GFP_DMA);
863 if (!this->page_buffer_virt)
864 goto error_alloc;
865
866
867 /* Slice up the page buffer. */
868 this->payload_virt = this->page_buffer_virt;
869 this->payload_phys = this->page_buffer_phys;
870 this->auxiliary_virt = this->payload_virt + geo->payload_size;
871 this->auxiliary_phys = this->payload_phys + geo->payload_size;
872 return 0;
873
874error_alloc:
875 gpmi_free_dma_buffer(this);
Vikram Narayanan2d350e52012-09-23 15:18:32 +0530876 pr_err("Error allocating DMA buffers!\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800877 return -ENOMEM;
878}
879
880static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
881{
882 struct nand_chip *chip = mtd->priv;
883 struct gpmi_nand_data *this = chip->priv;
884 int ret;
885
886 /*
887 * Every operation begins with a command byte and a series of zero or
888 * more address bytes. These are distinguished by either the Address
889 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
890 * asserted. When MTD is ready to execute the command, it will deassert
891 * both latch enables.
892 *
893 * Rather than run a separate DMA operation for every single byte, we
894 * queue them up and run a single DMA operation for the entire series
895 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
896 */
897 if ((ctrl & (NAND_ALE | NAND_CLE))) {
898 if (data != NAND_CMD_NONE)
899 this->cmd_buffer[this->command_length++] = data;
900 return;
901 }
902
903 if (!this->command_length)
904 return;
905
906 ret = gpmi_send_command(this);
907 if (ret)
908 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
909
910 this->command_length = 0;
911}
912
913static int gpmi_dev_ready(struct mtd_info *mtd)
914{
915 struct nand_chip *chip = mtd->priv;
916 struct gpmi_nand_data *this = chip->priv;
917
918 return gpmi_is_ready(this, this->current_chip);
919}
920
921static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
922{
923 struct nand_chip *chip = mtd->priv;
924 struct gpmi_nand_data *this = chip->priv;
925
926 if ((this->current_chip < 0) && (chipnr >= 0))
927 gpmi_begin(this);
928 else if ((this->current_chip >= 0) && (chipnr < 0))
929 gpmi_end(this);
930
931 this->current_chip = chipnr;
932}
933
934static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
935{
936 struct nand_chip *chip = mtd->priv;
937 struct gpmi_nand_data *this = chip->priv;
938
939 pr_debug("len is %d\n", len);
940 this->upper_buf = buf;
941 this->upper_len = len;
942
943 gpmi_read_data(this);
944}
945
946static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
947{
948 struct nand_chip *chip = mtd->priv;
949 struct gpmi_nand_data *this = chip->priv;
950
951 pr_debug("len is %d\n", len);
952 this->upper_buf = (uint8_t *)buf;
953 this->upper_len = len;
954
955 gpmi_send_data(this);
956}
957
958static uint8_t gpmi_read_byte(struct mtd_info *mtd)
959{
960 struct nand_chip *chip = mtd->priv;
961 struct gpmi_nand_data *this = chip->priv;
962 uint8_t *buf = this->data_buffer_dma;
963
964 gpmi_read_buf(mtd, buf, 1);
965 return buf[0];
966}
967
968/*
969 * Handles block mark swapping.
970 * It can be called in swapping the block mark, or swapping it back,
971 * because the the operations are the same.
972 */
973static void block_mark_swapping(struct gpmi_nand_data *this,
974 void *payload, void *auxiliary)
975{
976 struct bch_geometry *nfc_geo = &this->bch_geometry;
977 unsigned char *p;
978 unsigned char *a;
979 unsigned int bit;
980 unsigned char mask;
981 unsigned char from_data;
982 unsigned char from_oob;
983
984 if (!this->swap_block_mark)
985 return;
986
987 /*
988 * If control arrives here, we're swapping. Make some convenience
989 * variables.
990 */
991 bit = nfc_geo->block_mark_bit_offset;
992 p = payload + nfc_geo->block_mark_byte_offset;
993 a = auxiliary;
994
995 /*
996 * Get the byte from the data area that overlays the block mark. Since
997 * the ECC engine applies its own view to the bits in the page, the
998 * physical block mark won't (in general) appear on a byte boundary in
999 * the data.
1000 */
1001 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1002
1003 /* Get the byte from the OOB. */
1004 from_oob = a[0];
1005
1006 /* Swap them. */
1007 a[0] = from_data;
1008
1009 mask = (0x1 << bit) - 1;
1010 p[0] = (p[0] & mask) | (from_oob << bit);
1011
1012 mask = ~0 << bit;
1013 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1014}
1015
1016static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001017 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001018{
1019 struct gpmi_nand_data *this = chip->priv;
1020 struct bch_geometry *nfc_geo = &this->bch_geometry;
1021 void *payload_virt;
1022 dma_addr_t payload_phys;
1023 void *auxiliary_virt;
1024 dma_addr_t auxiliary_phys;
1025 unsigned int i;
1026 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -06001027 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001028 int ret;
1029
1030 pr_debug("page number is : %d\n", page);
1031 ret = read_page_prepare(this, buf, mtd->writesize,
1032 this->payload_virt, this->payload_phys,
1033 nfc_geo->payload_size,
1034 &payload_virt, &payload_phys);
1035 if (ret) {
1036 pr_err("Inadequate DMA buffer\n");
1037 ret = -ENOMEM;
1038 return ret;
1039 }
1040 auxiliary_virt = this->auxiliary_virt;
1041 auxiliary_phys = this->auxiliary_phys;
1042
1043 /* go! */
1044 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
1045 read_page_end(this, buf, mtd->writesize,
1046 this->payload_virt, this->payload_phys,
1047 nfc_geo->payload_size,
1048 payload_virt, payload_phys);
1049 if (ret) {
1050 pr_err("Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001051 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001052 }
1053
1054 /* handle the block mark swapping */
1055 block_mark_swapping(this, payload_virt, auxiliary_virt);
1056
1057 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001058 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001059
1060 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1061 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1062 continue;
1063
1064 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001065 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001066 continue;
1067 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001068 mtd->ecc_stats.corrected += *status;
1069 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001070 }
1071
Brian Norris7725cc82012-05-02 10:15:02 -07001072 if (oob_required) {
1073 /*
1074 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1075 * for details about our policy for delivering the OOB.
1076 *
1077 * We fill the caller's buffer with set bits, and then copy the
1078 * block mark to th caller's buffer. Note that, if block mark
1079 * swapping was necessary, it has already been done, so we can
1080 * rely on the first byte of the auxiliary buffer to contain
1081 * the block mark.
1082 */
1083 memset(chip->oob_poi, ~0, mtd->oobsize);
1084 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001085 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001086
1087 read_page_swap_end(this, buf, mtd->writesize,
1088 this->payload_virt, this->payload_phys,
1089 nfc_geo->payload_size,
1090 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001091
1092 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001093}
1094
Josh Wufdbad98d2012-06-25 18:07:45 +08001095static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001096 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001097{
1098 struct gpmi_nand_data *this = chip->priv;
1099 struct bch_geometry *nfc_geo = &this->bch_geometry;
1100 const void *payload_virt;
1101 dma_addr_t payload_phys;
1102 const void *auxiliary_virt;
1103 dma_addr_t auxiliary_phys;
1104 int ret;
1105
1106 pr_debug("ecc write page.\n");
1107 if (this->swap_block_mark) {
1108 /*
1109 * If control arrives here, we're doing block mark swapping.
1110 * Since we can't modify the caller's buffers, we must copy them
1111 * into our own.
1112 */
1113 memcpy(this->payload_virt, buf, mtd->writesize);
1114 payload_virt = this->payload_virt;
1115 payload_phys = this->payload_phys;
1116
1117 memcpy(this->auxiliary_virt, chip->oob_poi,
1118 nfc_geo->auxiliary_size);
1119 auxiliary_virt = this->auxiliary_virt;
1120 auxiliary_phys = this->auxiliary_phys;
1121
1122 /* Handle block mark swapping. */
1123 block_mark_swapping(this,
1124 (void *) payload_virt, (void *) auxiliary_virt);
1125 } else {
1126 /*
1127 * If control arrives here, we're not doing block mark swapping,
1128 * so we can to try and use the caller's buffers.
1129 */
1130 ret = send_page_prepare(this,
1131 buf, mtd->writesize,
1132 this->payload_virt, this->payload_phys,
1133 nfc_geo->payload_size,
1134 &payload_virt, &payload_phys);
1135 if (ret) {
1136 pr_err("Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001137 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001138 }
1139
1140 ret = send_page_prepare(this,
1141 chip->oob_poi, mtd->oobsize,
1142 this->auxiliary_virt, this->auxiliary_phys,
1143 nfc_geo->auxiliary_size,
1144 &auxiliary_virt, &auxiliary_phys);
1145 if (ret) {
1146 pr_err("Inadequate auxiliary DMA buffer\n");
1147 goto exit_auxiliary;
1148 }
1149 }
1150
1151 /* Ask the NFC. */
1152 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1153 if (ret)
1154 pr_err("Error in ECC-based write: %d\n", ret);
1155
1156 if (!this->swap_block_mark) {
1157 send_page_end(this, chip->oob_poi, mtd->oobsize,
1158 this->auxiliary_virt, this->auxiliary_phys,
1159 nfc_geo->auxiliary_size,
1160 auxiliary_virt, auxiliary_phys);
1161exit_auxiliary:
1162 send_page_end(this, buf, mtd->writesize,
1163 this->payload_virt, this->payload_phys,
1164 nfc_geo->payload_size,
1165 payload_virt, payload_phys);
1166 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001167
1168 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001169}
1170
1171/*
1172 * There are several places in this driver where we have to handle the OOB and
1173 * block marks. This is the function where things are the most complicated, so
1174 * this is where we try to explain it all. All the other places refer back to
1175 * here.
1176 *
1177 * These are the rules, in order of decreasing importance:
1178 *
1179 * 1) Nothing the caller does can be allowed to imperil the block mark.
1180 *
1181 * 2) In read operations, the first byte of the OOB we return must reflect the
1182 * true state of the block mark, no matter where that block mark appears in
1183 * the physical page.
1184 *
1185 * 3) ECC-based read operations return an OOB full of set bits (since we never
1186 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1187 * return).
1188 *
1189 * 4) "Raw" read operations return a direct view of the physical bytes in the
1190 * page, using the conventional definition of which bytes are data and which
1191 * are OOB. This gives the caller a way to see the actual, physical bytes
1192 * in the page, without the distortions applied by our ECC engine.
1193 *
1194 *
1195 * What we do for this specific read operation depends on two questions:
1196 *
1197 * 1) Are we doing a "raw" read, or an ECC-based read?
1198 *
1199 * 2) Are we using block mark swapping or transcription?
1200 *
1201 * There are four cases, illustrated by the following Karnaugh map:
1202 *
1203 * | Raw | ECC-based |
1204 * -------------+-------------------------+-------------------------+
1205 * | Read the conventional | |
1206 * | OOB at the end of the | |
1207 * Swapping | page and return it. It | |
1208 * | contains exactly what | |
1209 * | we want. | Read the block mark and |
1210 * -------------+-------------------------+ return it in a buffer |
1211 * | Read the conventional | full of set bits. |
1212 * | OOB at the end of the | |
1213 * | page and also the block | |
1214 * Transcribing | mark in the metadata. | |
1215 * | Copy the block mark | |
1216 * | into the first byte of | |
1217 * | the OOB. | |
1218 * -------------+-------------------------+-------------------------+
1219 *
1220 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1221 * giving an accurate view of the actual, physical bytes in the page (we're
1222 * overwriting the block mark). That's OK because it's more important to follow
1223 * rule #2.
1224 *
1225 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1226 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1227 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1228 * ECC-based or raw view of the page is implicit in which function it calls
1229 * (there is a similar pair of ECC-based/raw functions for writing).
1230 *
Brian Norris271b874b2012-05-11 13:30:35 -07001231 * FIXME: The following paragraph is incorrect, now that there exist
1232 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1233 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001234 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1235 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1236 * caller wants an ECC-based or raw view of the page is not propagated down to
1237 * this driver.
1238 */
1239static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001240 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001241{
1242 struct gpmi_nand_data *this = chip->priv;
1243
1244 pr_debug("page number is %d\n", page);
1245 /* clear the OOB buffer */
1246 memset(chip->oob_poi, ~0, mtd->oobsize);
1247
1248 /* Read out the conventional OOB. */
1249 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1250 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1251
1252 /*
1253 * Now, we want to make sure the block mark is correct. In the
1254 * Swapping/Raw case, we already have it. Otherwise, we need to
1255 * explicitly read it.
1256 */
1257 if (!this->swap_block_mark) {
1258 /* Read the block mark into the first byte of the OOB buffer. */
1259 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1260 chip->oob_poi[0] = chip->read_byte(mtd);
1261 }
1262
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001263 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001264}
1265
1266static int
1267gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1268{
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001269 struct nand_oobfree *of = mtd->ecclayout->oobfree;
1270 int status = 0;
1271
1272 /* Do we have available oob area? */
1273 if (!of->length)
1274 return -EPERM;
1275
1276 if (!nand_is_slc(chip))
1277 return -EPERM;
1278
1279 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1280 chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1281 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1282
1283 status = chip->waitfunc(mtd, chip);
1284 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001285}
1286
1287static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1288{
1289 struct nand_chip *chip = mtd->priv;
1290 struct gpmi_nand_data *this = chip->priv;
Brian Norris5a0edb22013-07-30 17:52:58 -07001291 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001292 uint8_t *block_mark;
1293 int column, page, status, chipnr;
1294
Brian Norris5a0edb22013-07-30 17:52:58 -07001295 chipnr = (int)(ofs >> chip->chip_shift);
1296 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001297
Brian Norris5a0edb22013-07-30 17:52:58 -07001298 column = this->swap_block_mark ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001299
Brian Norris5a0edb22013-07-30 17:52:58 -07001300 /* Write the block mark. */
1301 block_mark = this->data_buffer_dma;
1302 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001303
Brian Norris5a0edb22013-07-30 17:52:58 -07001304 /* Shift to get page */
1305 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001306
Brian Norris5a0edb22013-07-30 17:52:58 -07001307 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1308 chip->write_buf(mtd, block_mark, 1);
1309 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001310
Brian Norris5a0edb22013-07-30 17:52:58 -07001311 status = chip->waitfunc(mtd, chip);
1312 if (status & NAND_STATUS_FAIL)
1313 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001314
Brian Norris5a0edb22013-07-30 17:52:58 -07001315 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001316
1317 return ret;
1318}
1319
Wolfram Sanga78da282012-03-21 19:29:17 +01001320static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001321{
1322 struct boot_rom_geometry *geometry = &this->rom_geometry;
1323
1324 /*
1325 * Set the boot block stride size.
1326 *
1327 * In principle, we should be reading this from the OTP bits, since
1328 * that's where the ROM is going to get it. In fact, we don't have any
1329 * way to read the OTP bits, so we go with the default and hope for the
1330 * best.
1331 */
1332 geometry->stride_size_in_pages = 64;
1333
1334 /*
1335 * Set the search area stride exponent.
1336 *
1337 * In principle, we should be reading this from the OTP bits, since
1338 * that's where the ROM is going to get it. In fact, we don't have any
1339 * way to read the OTP bits, so we go with the default and hope for the
1340 * best.
1341 */
1342 geometry->search_area_stride_exponent = 2;
1343 return 0;
1344}
1345
1346static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001347static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001348{
1349 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1350 struct device *dev = this->dev;
1351 struct mtd_info *mtd = &this->mtd;
1352 struct nand_chip *chip = &this->nand;
1353 unsigned int search_area_size_in_strides;
1354 unsigned int stride;
1355 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001356 uint8_t *buffer = chip->buffers->databuf;
1357 int saved_chip_number;
1358 int found_an_ncb_fingerprint = false;
1359
1360 /* Compute the number of strides in a search area. */
1361 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1362
1363 saved_chip_number = this->current_chip;
1364 chip->select_chip(mtd, 0);
1365
1366 /*
1367 * Loop through the first search area, looking for the NCB fingerprint.
1368 */
1369 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1370
1371 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001372 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001373 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001374
1375 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1376
1377 /*
1378 * Read the NCB fingerprint. The fingerprint is four bytes long
1379 * and starts in the 12th byte of the page.
1380 */
1381 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1382 chip->read_buf(mtd, buffer, strlen(fingerprint));
1383
1384 /* Look for the fingerprint. */
1385 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1386 found_an_ncb_fingerprint = true;
1387 break;
1388 }
1389
1390 }
1391
1392 chip->select_chip(mtd, saved_chip_number);
1393
1394 if (found_an_ncb_fingerprint)
1395 dev_dbg(dev, "\tFound a fingerprint\n");
1396 else
1397 dev_dbg(dev, "\tNo fingerprint found\n");
1398 return found_an_ncb_fingerprint;
1399}
1400
1401/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001402static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001403{
1404 struct device *dev = this->dev;
1405 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1406 struct mtd_info *mtd = &this->mtd;
1407 struct nand_chip *chip = &this->nand;
1408 unsigned int block_size_in_pages;
1409 unsigned int search_area_size_in_strides;
1410 unsigned int search_area_size_in_pages;
1411 unsigned int search_area_size_in_blocks;
1412 unsigned int block;
1413 unsigned int stride;
1414 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001415 uint8_t *buffer = chip->buffers->databuf;
1416 int saved_chip_number;
1417 int status;
1418
1419 /* Compute the search area geometry. */
1420 block_size_in_pages = mtd->erasesize / mtd->writesize;
1421 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1422 search_area_size_in_pages = search_area_size_in_strides *
1423 rom_geo->stride_size_in_pages;
1424 search_area_size_in_blocks =
1425 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1426 block_size_in_pages;
1427
1428 dev_dbg(dev, "Search Area Geometry :\n");
1429 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1430 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1431 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1432
1433 /* Select chip 0. */
1434 saved_chip_number = this->current_chip;
1435 chip->select_chip(mtd, 0);
1436
1437 /* Loop over blocks in the first search area, erasing them. */
1438 dev_dbg(dev, "Erasing the search area...\n");
1439
1440 for (block = 0; block < search_area_size_in_blocks; block++) {
1441 /* Compute the page address. */
1442 page = block * block_size_in_pages;
1443
1444 /* Erase this block. */
1445 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1446 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1447 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1448
1449 /* Wait for the erase to finish. */
1450 status = chip->waitfunc(mtd, chip);
1451 if (status & NAND_STATUS_FAIL)
1452 dev_err(dev, "[%s] Erase failed.\n", __func__);
1453 }
1454
1455 /* Write the NCB fingerprint into the page buffer. */
1456 memset(buffer, ~0, mtd->writesize);
1457 memset(chip->oob_poi, ~0, mtd->oobsize);
1458 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1459
1460 /* Loop through the first search area, writing NCB fingerprints. */
1461 dev_dbg(dev, "Writing NCB fingerprints...\n");
1462 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001463 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001464 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001465
1466 /* Write the first page of the current stride. */
1467 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1468 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001469 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001470 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1471
1472 /* Wait for the write to finish. */
1473 status = chip->waitfunc(mtd, chip);
1474 if (status & NAND_STATUS_FAIL)
1475 dev_err(dev, "[%s] Write failed.\n", __func__);
1476 }
1477
1478 /* Deselect chip 0. */
1479 chip->select_chip(mtd, saved_chip_number);
1480 return 0;
1481}
1482
Wolfram Sanga78da282012-03-21 19:29:17 +01001483static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001484{
1485 struct device *dev = this->dev;
1486 struct nand_chip *chip = &this->nand;
1487 struct mtd_info *mtd = &this->mtd;
1488 unsigned int block_count;
1489 unsigned int block;
1490 int chipnr;
1491 int page;
1492 loff_t byte;
1493 uint8_t block_mark;
1494 int ret = 0;
1495
1496 /*
1497 * If control arrives here, we can't use block mark swapping, which
1498 * means we're forced to use transcription. First, scan for the
1499 * transcription stamp. If we find it, then we don't have to do
1500 * anything -- the block marks are already transcribed.
1501 */
1502 if (mx23_check_transcription_stamp(this))
1503 return 0;
1504
1505 /*
1506 * If control arrives here, we couldn't find a transcription stamp, so
1507 * so we presume the block marks are in the conventional location.
1508 */
1509 dev_dbg(dev, "Transcribing bad block marks...\n");
1510
1511 /* Compute the number of blocks in the entire medium. */
1512 block_count = chip->chipsize >> chip->phys_erase_shift;
1513
1514 /*
1515 * Loop over all the blocks in the medium, transcribing block marks as
1516 * we go.
1517 */
1518 for (block = 0; block < block_count; block++) {
1519 /*
1520 * Compute the chip, page and byte addresses for this block's
1521 * conventional mark.
1522 */
1523 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1524 page = block << (chip->phys_erase_shift - chip->page_shift);
1525 byte = block << chip->phys_erase_shift;
1526
1527 /* Send the command to read the conventional block mark. */
1528 chip->select_chip(mtd, chipnr);
1529 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1530 block_mark = chip->read_byte(mtd);
1531 chip->select_chip(mtd, -1);
1532
1533 /*
1534 * Check if the block is marked bad. If so, we need to mark it
1535 * again, but this time the result will be a mark in the
1536 * location where we transcribe block marks.
1537 */
1538 if (block_mark != 0xff) {
1539 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1540 ret = chip->block_markbad(mtd, byte);
1541 if (ret)
1542 dev_err(dev, "Failed to mark block bad with "
1543 "ret %d\n", ret);
1544 }
1545 }
1546
1547 /* Write the stamp that indicates we've transcribed the block marks. */
1548 mx23_write_transcription_stamp(this);
1549 return 0;
1550}
1551
Wolfram Sanga78da282012-03-21 19:29:17 +01001552static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001553{
1554 nand_boot_set_geometry(this);
1555
1556 /* This is ROM arch-specific initilization before the BBT scanning. */
1557 if (GPMI_IS_MX23(this))
1558 return mx23_boot_init(this);
1559 return 0;
1560}
1561
Wolfram Sanga78da282012-03-21 19:29:17 +01001562static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001563{
1564 int ret;
1565
1566 /* Free the temporary DMA memory for reading ID. */
1567 gpmi_free_dma_buffer(this);
1568
1569 /* Set up the NFC geometry which is used by BCH. */
1570 ret = bch_set_geometry(this);
1571 if (ret) {
Vikram Narayanan2d350e52012-09-23 15:18:32 +05301572 pr_err("Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001573 return ret;
1574 }
1575
1576 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1577 return gpmi_alloc_dma_buffer(this);
1578}
1579
1580static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1581{
1582 int ret;
1583
1584 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1585 if (GPMI_IS_MX23(this))
1586 this->swap_block_mark = false;
1587 else
1588 this->swap_block_mark = true;
1589
1590 /* Set up the medium geometry */
1591 ret = gpmi_set_geometry(this);
1592 if (ret)
1593 return ret;
1594
1595 /* NAND boot init, depends on the gpmi_set_geometry(). */
1596 return nand_boot_init(this);
1597}
1598
Huang Shijief720e7c2013-08-16 10:10:08 +08001599static void gpmi_nfc_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001600{
Huang Shijief720e7c2013-08-16 10:10:08 +08001601 nand_release(&this->mtd);
1602 gpmi_free_dma_buffer(this);
1603}
1604
1605static int gpmi_init_last(struct gpmi_nand_data *this)
1606{
1607 struct mtd_info *mtd = &this->mtd;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001608 struct nand_chip *chip = mtd->priv;
Huang Shijief720e7c2013-08-16 10:10:08 +08001609 struct nand_ecc_ctrl *ecc = &chip->ecc;
1610 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001611 int ret;
1612
1613 /* Prepare for the BBT scan. */
1614 ret = gpmi_pre_bbt_scan(this);
1615 if (ret)
1616 return ret;
1617
Huang Shijief720e7c2013-08-16 10:10:08 +08001618 /* Init the nand_ecc_ctrl{} */
1619 ecc->read_page = gpmi_ecc_read_page;
1620 ecc->write_page = gpmi_ecc_write_page;
1621 ecc->read_oob = gpmi_ecc_read_oob;
1622 ecc->write_oob = gpmi_ecc_write_oob;
1623 ecc->mode = NAND_ECC_HW;
1624 ecc->size = bch_geo->ecc_chunk_size;
1625 ecc->strength = bch_geo->ecc_strength;
1626 ecc->layout = &gpmi_hw_ecclayout;
1627
Huang Shijie995fbbf2012-09-13 14:57:59 +08001628 /*
1629 * Can we enable the extra features? such as EDO or Sync mode.
1630 *
1631 * We do not check the return value now. That's means if we fail in
1632 * enable the extra features, we still can run in the normal way.
1633 */
1634 gpmi_extra_init(this);
1635
Huang Shijief720e7c2013-08-16 10:10:08 +08001636 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001637}
1638
Bill Pemberton06f25512012-11-19 13:23:07 -05001639static int gpmi_nfc_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001640{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001641 struct mtd_info *mtd = &this->mtd;
1642 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001643 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001644 int ret;
1645
1646 /* init current chip */
1647 this->current_chip = -1;
1648
1649 /* init the MTD data structures */
1650 mtd->priv = chip;
1651 mtd->name = "gpmi-nand";
1652 mtd->owner = THIS_MODULE;
1653
1654 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1655 chip->priv = this;
1656 chip->select_chip = gpmi_select_chip;
1657 chip->cmd_ctrl = gpmi_cmd_ctrl;
1658 chip->dev_ready = gpmi_dev_ready;
1659 chip->read_byte = gpmi_read_byte;
1660 chip->read_buf = gpmi_read_buf;
1661 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001662 chip->badblock_pattern = &gpmi_bbt_descr;
1663 chip->block_markbad = gpmi_block_markbad;
1664 chip->options |= NAND_NO_SUBPAGE_WRITE;
Huang Shijiec50c6942012-07-03 16:24:32 +08001665 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1666 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001667
Huang Shijief720e7c2013-08-16 10:10:08 +08001668 /*
1669 * Allocate a temporary DMA buffer for reading ID in the
1670 * nand_scan_ident().
1671 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001672 this->bch_geometry.payload_size = 1024;
1673 this->bch_geometry.auxiliary_size = 128;
1674 ret = gpmi_alloc_dma_buffer(this);
1675 if (ret)
1676 goto err_out;
1677
Huang Shijief720e7c2013-08-16 10:10:08 +08001678 ret = nand_scan_ident(mtd, 1, NULL);
1679 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001680 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001681
1682 ret = gpmi_init_last(this);
1683 if (ret)
1684 goto err_out;
1685
1686 ret = nand_scan_tail(mtd);
1687 if (ret)
1688 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001689
Huang Shijiee10db1f2012-05-04 21:42:05 -04001690 ppdata.of_node = this->pdev->dev.of_node;
1691 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001692 if (ret)
1693 goto err_out;
1694 return 0;
1695
1696err_out:
1697 gpmi_nfc_exit(this);
1698 return ret;
1699}
1700
Huang Shijiee10db1f2012-05-04 21:42:05 -04001701static const struct platform_device_id gpmi_ids[] = {
1702 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1703 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001704 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001705 {}
Huang Shijiee10db1f2012-05-04 21:42:05 -04001706};
1707
1708static const struct of_device_id gpmi_nand_id_table[] = {
1709 {
1710 .compatible = "fsl,imx23-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001711 .data = (void *)&gpmi_ids[IS_MX23],
Huang Shijiee10db1f2012-05-04 21:42:05 -04001712 }, {
1713 .compatible = "fsl,imx28-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001714 .data = (void *)&gpmi_ids[IS_MX28],
Huang Shijie9013bb42012-05-04 21:42:06 -04001715 }, {
1716 .compatible = "fsl,imx6q-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001717 .data = (void *)&gpmi_ids[IS_MX6Q],
Huang Shijiee10db1f2012-05-04 21:42:05 -04001718 }, {}
1719};
1720MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1721
Bill Pemberton06f25512012-11-19 13:23:07 -05001722static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001723{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001724 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001725 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001726 int ret;
1727
Huang Shijiee10db1f2012-05-04 21:42:05 -04001728 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1729 if (of_id) {
1730 pdev->id_entry = of_id->data;
1731 } else {
1732 pr_err("Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02001733 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001734 }
1735
Huang Shijie10a2bca2011-09-08 10:47:09 +08001736 this = kzalloc(sizeof(*this), GFP_KERNEL);
1737 if (!this) {
1738 pr_err("Failed to allocate per-device memory\n");
1739 return -ENOMEM;
1740 }
1741
1742 platform_set_drvdata(pdev, this);
1743 this->pdev = pdev;
1744 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001745
1746 ret = acquire_resources(this);
1747 if (ret)
1748 goto exit_acquire_resources;
1749
1750 ret = init_hardware(this);
1751 if (ret)
1752 goto exit_nfc_init;
1753
1754 ret = gpmi_nfc_init(this);
1755 if (ret)
1756 goto exit_nfc_init;
1757
Fabio Estevam490e2802012-09-05 11:35:24 -03001758 dev_info(this->dev, "driver registered.\n");
1759
Huang Shijie10a2bca2011-09-08 10:47:09 +08001760 return 0;
1761
1762exit_nfc_init:
1763 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001764exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03001765 dev_err(this->dev, "driver registration failed: %d\n", ret);
Huang Shijie26738dd2013-01-23 16:20:53 +08001766 kfree(this);
Fabio Estevam490e2802012-09-05 11:35:24 -03001767
Huang Shijie10a2bca2011-09-08 10:47:09 +08001768 return ret;
1769}
1770
Bill Pemberton810b7e02012-11-19 13:26:04 -05001771static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001772{
1773 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1774
1775 gpmi_nfc_exit(this);
1776 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001777 kfree(this);
1778 return 0;
1779}
1780
Huang Shijie10a2bca2011-09-08 10:47:09 +08001781static struct platform_driver gpmi_nand_driver = {
1782 .driver = {
1783 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001784 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001785 },
1786 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001787 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001788 .id_table = gpmi_ids,
1789};
Fabio Estevam490e2802012-09-05 11:35:24 -03001790module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001791
1792MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1793MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1794MODULE_LICENSE("GPL");