blob: e2f58207c779313c1c254d60b2c515b8963421c2 [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 */
21#include <linux/clk.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
Wolfram Sangdf16c862011-11-23 15:57:06 +010024#include <linux/module.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080025#include <linux/mtd/partitions.h>
Huang Shijiee10db1f2012-05-04 21:42:05 -040026#include <linux/of.h>
27#include <linux/of_device.h>
Huang Shijiec50c6942012-07-03 16:24:32 +080028#include <linux/of_mtd.h>
Huang Shijie10a2bca2011-09-08 10:47:09 +080029#include "gpmi-nand.h"
30
Huang Shijie5de0b522012-10-13 13:03:29 -040031/* Resource names for the GPMI NAND driver. */
32#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
33#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
34#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
Huang Shijie5de0b522012-10-13 13:03:29 -040035
Huang Shijie10a2bca2011-09-08 10:47:09 +080036/* add our owner bbt descriptor */
37static uint8_t scan_ff_pattern[] = { 0xff };
38static struct nand_bbt_descr gpmi_bbt_descr = {
39 .options = 0,
40 .offs = 0,
41 .len = 1,
42 .pattern = scan_ff_pattern
43};
44
Huang Shijie7a2b89a2013-09-25 14:58:15 +080045/*
46 * We may change the layout if we can get the ECC info from the datasheet,
47 * else we will use all the (page + OOB).
48 */
Huang Shijie10a2bca2011-09-08 10:47:09 +080049static struct nand_ecclayout gpmi_hw_ecclayout = {
50 .eccbytes = 0,
51 .eccpos = { 0, },
52 .oobfree = { {.offset = 0, .length = 0} }
53};
54
55static irqreturn_t bch_irq(int irq, void *cookie)
56{
57 struct gpmi_nand_data *this = cookie;
58
59 gpmi_clear_bch(this);
60 complete(&this->bch_done);
61 return IRQ_HANDLED;
62}
63
64/*
65 * Calculate the ECC strength by hand:
66 * E : The ECC strength.
67 * G : the length of Galois Field.
68 * N : The chunk count of per page.
69 * O : the oobsize of the NAND chip.
70 * M : the metasize of per page.
71 *
72 * The formula is :
73 * E * G * N
74 * ------------ <= (O - M)
75 * 8
76 *
77 * So, we get E by:
78 * (O - M) * 8
79 * E <= -------------
80 * G * N
81 */
82static inline int get_ecc_strength(struct gpmi_nand_data *this)
83{
84 struct bch_geometry *geo = &this->bch_geometry;
85 struct mtd_info *mtd = &this->mtd;
86 int ecc_strength;
87
88 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
89 / (geo->gf_len * geo->ecc_chunk_count);
90
91 /* We need the minor even number. */
92 return round_down(ecc_strength, 2);
93}
94
Huang Shijie92d0e092013-01-29 09:23:38 +080095static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
96{
97 struct bch_geometry *geo = &this->bch_geometry;
98
99 /* Do the sanity check. */
100 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
101 /* The mx23/mx28 only support the GF13. */
102 if (geo->gf_len == 14)
103 return false;
104
105 if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
106 return false;
107 } else if (GPMI_IS_MX6Q(this)) {
108 if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
109 return false;
110 }
111 return true;
112}
113
Huang Shijie2febcdf2013-05-17 11:17:34 +0800114/*
115 * If we can get the ECC information from the nand chip, we do not
116 * need to calculate them ourselves.
117 *
118 * We may have available oob space in this case.
119 */
120static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
121{
122 struct bch_geometry *geo = &this->bch_geometry;
123 struct mtd_info *mtd = &this->mtd;
124 struct nand_chip *chip = mtd->priv;
125 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
126 unsigned int block_mark_bit_offset;
127
128 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
129 return false;
130
131 switch (chip->ecc_step_ds) {
132 case SZ_512:
133 geo->gf_len = 13;
134 break;
135 case SZ_1K:
136 geo->gf_len = 14;
137 break;
138 default:
139 dev_err(this->dev,
140 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
141 chip->ecc_strength_ds, chip->ecc_step_ds);
142 return false;
143 }
144 geo->ecc_chunk_size = chip->ecc_step_ds;
145 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
146 if (!gpmi_check_ecc(this))
147 return false;
148
149 /* Keep the C >= O */
150 if (geo->ecc_chunk_size < mtd->oobsize) {
151 dev_err(this->dev,
152 "unsupported nand chip. ecc size: %d, oob size : %d\n",
153 chip->ecc_step_ds, mtd->oobsize);
154 return false;
155 }
156
157 /* The default value, see comment in the legacy_set_geometry(). */
158 geo->metadata_size = 10;
159
160 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
161
162 /*
163 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
164 *
165 * | P |
166 * |<----------------------------------------------------->|
167 * | |
168 * | (Block Mark) |
169 * | P' | | | |
170 * |<-------------------------------------------->| D | | O' |
171 * | |<---->| |<--->|
172 * V V V V V
173 * +---+----------+-+----------+-+----------+-+----------+-+-----+
174 * | M | data |E| data |E| data |E| data |E| |
175 * +---+----------+-+----------+-+----------+-+----------+-+-----+
176 * ^ ^
177 * | O |
178 * |<------------>|
179 * | |
180 *
181 * P : the page size for BCH module.
182 * E : The ECC strength.
183 * G : the length of Galois Field.
184 * N : The chunk count of per page.
185 * M : the metasize of per page.
186 * C : the ecc chunk size, aka the "data" above.
187 * P': the nand chip's page size.
188 * O : the nand chip's oob size.
189 * O': the free oob.
190 *
191 * The formula for P is :
192 *
193 * E * G * N
194 * P = ------------ + P' + M
195 * 8
196 *
197 * The position of block mark moves forward in the ECC-based view
198 * of page, and the delta is:
199 *
200 * E * G * (N - 1)
201 * D = (---------------- + M)
202 * 8
203 *
204 * Please see the comment in legacy_set_geometry().
205 * With the condition C >= O , we still can get same result.
206 * So the bit position of the physical block mark within the ECC-based
207 * view of the page is :
208 * (P' - D) * 8
209 */
210 geo->page_size = mtd->writesize + geo->metadata_size +
211 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
212
213 /* The available oob size we have. */
214 if (geo->page_size < mtd->writesize + mtd->oobsize) {
215 of->offset = geo->page_size - mtd->writesize;
216 of->length = mtd->oobsize - of->offset;
Huang Shijie2febcdf2013-05-17 11:17:34 +0800217 }
218
219 geo->payload_size = mtd->writesize;
220
221 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
222 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
223 + ALIGN(geo->ecc_chunk_count, 4);
224
225 if (!this->swap_block_mark)
226 return true;
227
228 /* For bit swap. */
229 block_mark_bit_offset = mtd->writesize * 8 -
230 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
231 + geo->metadata_size * 8);
232
233 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
234 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
235 return true;
236}
237
238static int legacy_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800239{
240 struct bch_geometry *geo = &this->bch_geometry;
241 struct mtd_info *mtd = &this->mtd;
242 unsigned int metadata_size;
243 unsigned int status_size;
244 unsigned int block_mark_bit_offset;
245
246 /*
247 * The size of the metadata can be changed, though we set it to 10
248 * bytes now. But it can't be too large, because we have to save
249 * enough space for BCH.
250 */
251 geo->metadata_size = 10;
252
253 /* The default for the length of Galois Field. */
254 geo->gf_len = 13;
255
Huang Shijie9ff16f02013-01-25 14:04:07 +0800256 /* The default for chunk size. */
Huang Shijie10a2bca2011-09-08 10:47:09 +0800257 geo->ecc_chunk_size = 512;
Huang Shijie9ff16f02013-01-25 14:04:07 +0800258 while (geo->ecc_chunk_size < mtd->oobsize) {
Huang Shijie10a2bca2011-09-08 10:47:09 +0800259 geo->ecc_chunk_size *= 2; /* keep C >= O */
Huang Shijie9ff16f02013-01-25 14:04:07 +0800260 geo->gf_len = 14;
261 }
Huang Shijie10a2bca2011-09-08 10:47:09 +0800262
263 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
264
265 /* We use the same ECC strength for all chunks. */
266 geo->ecc_strength = get_ecc_strength(this);
Huang Shijie92d0e092013-01-29 09:23:38 +0800267 if (!gpmi_check_ecc(this)) {
268 dev_err(this->dev,
269 "We can not support this nand chip."
270 " Its required ecc strength(%d) is beyond our"
271 " capability(%d).\n", geo->ecc_strength,
272 (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
273 : MXS_ECC_STRENGTH_MAX));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800274 return -EINVAL;
275 }
276
277 geo->page_size = mtd->writesize + mtd->oobsize;
278 geo->payload_size = mtd->writesize;
279
280 /*
281 * The auxiliary buffer contains the metadata and the ECC status. The
282 * metadata is padded to the nearest 32-bit boundary. The ECC status
283 * contains one byte for every ECC chunk, and is also padded to the
284 * nearest 32-bit boundary.
285 */
286 metadata_size = ALIGN(geo->metadata_size, 4);
287 status_size = ALIGN(geo->ecc_chunk_count, 4);
288
289 geo->auxiliary_size = metadata_size + status_size;
290 geo->auxiliary_status_offset = metadata_size;
291
292 if (!this->swap_block_mark)
293 return 0;
294
295 /*
296 * We need to compute the byte and bit offsets of
297 * the physical block mark within the ECC-based view of the page.
298 *
299 * NAND chip with 2K page shows below:
300 * (Block Mark)
301 * | |
302 * | D |
303 * |<---->|
304 * V V
305 * +---+----------+-+----------+-+----------+-+----------+-+
306 * | M | data |E| data |E| data |E| data |E|
307 * +---+----------+-+----------+-+----------+-+----------+-+
308 *
309 * The position of block mark moves forward in the ECC-based view
310 * of page, and the delta is:
311 *
312 * E * G * (N - 1)
313 * D = (---------------- + M)
314 * 8
315 *
316 * With the formula to compute the ECC strength, and the condition
317 * : C >= O (C is the ecc chunk size)
318 *
319 * It's easy to deduce to the following result:
320 *
321 * E * G (O - M) C - M C - M
322 * ----------- <= ------- <= -------- < ---------
323 * 8 N N (N - 1)
324 *
325 * So, we get:
326 *
327 * E * G * (N - 1)
328 * D = (---------------- + M) < C
329 * 8
330 *
331 * The above inequality means the position of block mark
332 * within the ECC-based view of the page is still in the data chunk,
333 * and it's NOT in the ECC bits of the chunk.
334 *
335 * Use the following to compute the bit position of the
336 * physical block mark within the ECC-based view of the page:
337 * (page_size - D) * 8
338 *
339 * --Huang Shijie
340 */
341 block_mark_bit_offset = mtd->writesize * 8 -
342 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
343 + geo->metadata_size * 8);
344
345 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
346 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
347 return 0;
348}
349
Huang Shijie2febcdf2013-05-17 11:17:34 +0800350int common_nfc_set_geometry(struct gpmi_nand_data *this)
351{
Huang Shijie89b59e62013-11-07 18:07:38 +0800352 if (of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")
353 && set_geometry_by_ecc_info(this))
354 return 0;
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{
Huang Shijiea7c12d02013-08-27 17:29:05 +0800360 /* We use the DMA channel 0 to access all the nand chips. */
361 return this->dma_chans[0];
Huang Shijie10a2bca2011-09-08 10:47:09 +0800362}
363
364/* Can we use the upper's buffer directly for DMA? */
365void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
366{
367 struct scatterlist *sgl = &this->data_sgl;
368 int ret;
369
370 this->direct_dma_map_ok = true;
371
372 /* first try to map the upper buffer directly */
373 sg_init_one(sgl, this->upper_buf, this->upper_len);
374 ret = dma_map_sg(this->dev, sgl, 1, dr);
375 if (ret == 0) {
376 /* We have to use our own DMA buffer. */
377 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
378
379 if (dr == DMA_TO_DEVICE)
380 memcpy(this->data_buffer_dma, this->upper_buf,
381 this->upper_len);
382
383 ret = dma_map_sg(this->dev, sgl, 1, dr);
384 if (ret == 0)
Huang Shijieda40c162013-11-20 10:09:43 +0800385 dev_err(this->dev, "DMA mapping failed.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800386
387 this->direct_dma_map_ok = false;
388 }
389}
390
391/* This will be called after the DMA operation is finished. */
392static void dma_irq_callback(void *param)
393{
394 struct gpmi_nand_data *this = param;
395 struct completion *dma_c = &this->dma_done;
396
Huang Shijie10a2bca2011-09-08 10:47:09 +0800397 switch (this->dma_type) {
398 case DMA_FOR_COMMAND:
399 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
400 break;
401
402 case DMA_FOR_READ_DATA:
403 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
404 if (this->direct_dma_map_ok == false)
405 memcpy(this->upper_buf, this->data_buffer_dma,
406 this->upper_len);
407 break;
408
409 case DMA_FOR_WRITE_DATA:
410 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
411 break;
412
413 case DMA_FOR_READ_ECC_PAGE:
414 case DMA_FOR_WRITE_ECC_PAGE:
415 /* We have to wait the BCH interrupt to finish. */
416 break;
417
418 default:
Huang Shijieda40c162013-11-20 10:09:43 +0800419 dev_err(this->dev, "in wrong DMA operation.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800420 }
Huang Shijie7b3d2fb2013-11-11 12:13:45 +0800421
422 complete(dma_c);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800423}
424
425int start_dma_without_bch_irq(struct gpmi_nand_data *this,
426 struct dma_async_tx_descriptor *desc)
427{
428 struct completion *dma_c = &this->dma_done;
429 int err;
430
431 init_completion(dma_c);
432
433 desc->callback = dma_irq_callback;
434 desc->callback_param = this;
435 dmaengine_submit(desc);
Shawn Guod04525e2012-04-11 13:29:31 +0800436 dma_async_issue_pending(get_dma_chan(this));
Huang Shijie10a2bca2011-09-08 10:47:09 +0800437
438 /* Wait for the interrupt from the DMA block. */
439 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
440 if (!err) {
Huang Shijieda40c162013-11-20 10:09:43 +0800441 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
442 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800443 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) {
Huang Shijieda40c162013-11-20 10:09:43 +0800471 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
472 this->last_dma_type);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800473 gpmi_dump_info(this);
474 return -ETIMEDOUT;
475 }
476 return 0;
477}
478
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800479static int acquire_register_block(struct gpmi_nand_data *this,
480 const char *res_name)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800481{
482 struct platform_device *pdev = this->pdev;
483 struct resources *res = &this->resources;
484 struct resource *r;
Huang Shijie513d57e2012-07-17 14:14:02 +0800485 void __iomem *p;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800486
487 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
Huang Shijie87a9d692013-11-14 14:25:48 +0800488 p = devm_ioremap_resource(&pdev->dev, r);
489 if (IS_ERR(p))
490 return PTR_ERR(p);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800491
492 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
493 res->gpmi_regs = p;
494 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
495 res->bch_regs = p;
496 else
Huang Shijieda40c162013-11-20 10:09:43 +0800497 dev_err(this->dev, "unknown resource name : %s\n", res_name);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800498
499 return 0;
500}
501
Greg Kroah-Hartmand8929942012-12-21 13:19:05 -0800502static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800503{
504 struct platform_device *pdev = this->pdev;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800505 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
506 struct resource *r;
507 int err;
508
509 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
510 if (!r) {
Huang Shijieda40c162013-11-20 10:09:43 +0800511 dev_err(this->dev, "Can't get resource for %s\n", res_name);
Lothar Waßmann52a073b2013-08-07 08:15:38 +0200512 return -ENODEV;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800513 }
514
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800515 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
516 if (err)
517 dev_err(this->dev, "error requesting BCH IRQ\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800518
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800519 return err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800520}
521
Huang Shijie10a2bca2011-09-08 10:47:09 +0800522static void release_dma_channels(struct gpmi_nand_data *this)
523{
524 unsigned int i;
525 for (i = 0; i < DMA_CHANS; i++)
526 if (this->dma_chans[i]) {
527 dma_release_channel(this->dma_chans[i]);
528 this->dma_chans[i] = NULL;
529 }
530}
531
Bill Pemberton06f25512012-11-19 13:23:07 -0500532static int acquire_dma_channels(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800533{
534 struct platform_device *pdev = this->pdev;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400535 struct dma_chan *dma_chan;
Huang Shijiee10db1f2012-05-04 21:42:05 -0400536
537 /* request dma channel */
Shawn Guo5fac0e12013-02-26 11:44:28 +0800538 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400539 if (!dma_chan) {
Huang Shijieda40c162013-11-20 10:09:43 +0800540 dev_err(this->dev, "Failed to request DMA channel.\n");
Huang Shijiee10db1f2012-05-04 21:42:05 -0400541 goto acquire_err;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800542 }
543
Huang Shijiee10db1f2012-05-04 21:42:05 -0400544 this->dma_chans[0] = dma_chan;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800545 return 0;
546
547acquire_err:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800548 release_dma_channels(this);
549 return -EINVAL;
550}
551
Huang Shijieff506172012-07-02 21:39:32 -0400552static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
553 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
554};
555
Bill Pemberton06f25512012-11-19 13:23:07 -0500556static int gpmi_get_clks(struct gpmi_nand_data *this)
Huang Shijieff506172012-07-02 21:39:32 -0400557{
558 struct resources *r = &this->resources;
559 char **extra_clks = NULL;
560 struct clk *clk;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200561 int err, i;
Huang Shijieff506172012-07-02 21:39:32 -0400562
563 /* The main clock is stored in the first. */
Fabio Estevam554cbc52013-11-07 22:32:38 -0200564 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200565 if (IS_ERR(r->clock[0])) {
566 err = PTR_ERR(r->clock[0]);
Huang Shijieff506172012-07-02 21:39:32 -0400567 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200568 }
Huang Shijieff506172012-07-02 21:39:32 -0400569
570 /* Get extra clocks */
571 if (GPMI_IS_MX6Q(this))
572 extra_clks = extra_clks_for_mx6q;
573 if (!extra_clks)
574 return 0;
575
576 for (i = 1; i < GPMI_CLK_MAX; i++) {
577 if (extra_clks[i - 1] == NULL)
578 break;
579
Fabio Estevam554cbc52013-11-07 22:32:38 -0200580 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200581 if (IS_ERR(clk)) {
582 err = PTR_ERR(clk);
Huang Shijieff506172012-07-02 21:39:32 -0400583 goto err_clock;
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200584 }
Huang Shijieff506172012-07-02 21:39:32 -0400585
586 r->clock[i] = clk;
587 }
588
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800589 if (GPMI_IS_MX6Q(this))
Huang Shijieff506172012-07-02 21:39:32 -0400590 /*
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800591 * Set the default value for the gpmi clock in mx6q:
Huang Shijieff506172012-07-02 21:39:32 -0400592 *
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800593 * If you want to use the ONFI nand which is in the
594 * Synchronous Mode, you should change the clock as you need.
Huang Shijieff506172012-07-02 21:39:32 -0400595 */
596 clk_set_rate(r->clock[0], 22000000);
Huang Shijiee1ca95e2012-09-13 14:57:58 +0800597
Huang Shijieff506172012-07-02 21:39:32 -0400598 return 0;
599
600err_clock:
601 dev_dbg(this->dev, "failed in finding the clocks.\n");
Michał Mirosławd1cb5562013-05-04 15:19:35 +0200602 return err;
Huang Shijieff506172012-07-02 21:39:32 -0400603}
604
Bill Pemberton06f25512012-11-19 13:23:07 -0500605static int acquire_resources(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800606{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800607 int ret;
608
609 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
610 if (ret)
611 goto exit_regs;
612
613 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
614 if (ret)
615 goto exit_regs;
616
617 ret = acquire_bch_irq(this, bch_irq);
618 if (ret)
619 goto exit_regs;
620
621 ret = acquire_dma_channels(this);
622 if (ret)
Huang Shijie3cb2c1e2013-11-14 14:25:49 +0800623 goto exit_regs;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800624
Huang Shijieff506172012-07-02 21:39:32 -0400625 ret = gpmi_get_clks(this);
626 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800627 goto exit_clock;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800628 return 0;
629
630exit_clock:
631 release_dma_channels(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800632exit_regs:
Huang Shijie10a2bca2011-09-08 10:47:09 +0800633 return ret;
634}
635
636static void release_resources(struct gpmi_nand_data *this)
637{
Huang Shijie10a2bca2011-09-08 10:47:09 +0800638 release_dma_channels(this);
639}
640
Bill Pemberton06f25512012-11-19 13:23:07 -0500641static int init_hardware(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800642{
643 int ret;
644
645 /*
646 * This structure contains the "safe" GPMI timing that should succeed
647 * with any NAND Flash device
648 * (although, with less-than-optimal performance).
649 */
650 struct nand_timing safe_timing = {
651 .data_setup_in_ns = 80,
652 .data_hold_in_ns = 60,
653 .address_setup_in_ns = 25,
654 .gpmi_sample_delay_in_ns = 6,
655 .tREA_in_ns = -1,
656 .tRLOH_in_ns = -1,
657 .tRHOH_in_ns = -1,
658 };
659
660 /* Initialize the hardwares. */
661 ret = gpmi_init(this);
662 if (ret)
663 return ret;
664
665 this->timing = safe_timing;
666 return 0;
667}
668
669static int read_page_prepare(struct gpmi_nand_data *this,
670 void *destination, unsigned length,
671 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
672 void **use_virt, dma_addr_t *use_phys)
673{
674 struct device *dev = this->dev;
675
676 if (virt_addr_valid(destination)) {
677 dma_addr_t dest_phys;
678
679 dest_phys = dma_map_single(dev, destination,
680 length, DMA_FROM_DEVICE);
681 if (dma_mapping_error(dev, dest_phys)) {
682 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800683 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800684 return -ENOMEM;
685 }
686 goto map_failed;
687 }
688 *use_virt = destination;
689 *use_phys = dest_phys;
690 this->direct_dma_map_ok = true;
691 return 0;
692 }
693
694map_failed:
695 *use_virt = alt_virt;
696 *use_phys = alt_phys;
697 this->direct_dma_map_ok = false;
698 return 0;
699}
700
701static inline void read_page_end(struct gpmi_nand_data *this,
702 void *destination, unsigned length,
703 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
704 void *used_virt, dma_addr_t used_phys)
705{
706 if (this->direct_dma_map_ok)
707 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
708}
709
710static inline void read_page_swap_end(struct gpmi_nand_data *this,
711 void *destination, unsigned length,
712 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
713 void *used_virt, dma_addr_t used_phys)
714{
715 if (!this->direct_dma_map_ok)
716 memcpy(destination, alt_virt, length);
717}
718
719static int send_page_prepare(struct gpmi_nand_data *this,
720 const void *source, unsigned length,
721 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
722 const void **use_virt, dma_addr_t *use_phys)
723{
724 struct device *dev = this->dev;
725
726 if (virt_addr_valid(source)) {
727 dma_addr_t source_phys;
728
729 source_phys = dma_map_single(dev, (void *)source, length,
730 DMA_TO_DEVICE);
731 if (dma_mapping_error(dev, source_phys)) {
732 if (alt_size < length) {
Huang Shijieda40c162013-11-20 10:09:43 +0800733 dev_err(dev, "Alternate buffer is too small\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800734 return -ENOMEM;
735 }
736 goto map_failed;
737 }
738 *use_virt = source;
739 *use_phys = source_phys;
740 return 0;
741 }
742map_failed:
743 /*
744 * Copy the content of the source buffer into the alternate
745 * buffer and set up the return values accordingly.
746 */
747 memcpy(alt_virt, source, length);
748
749 *use_virt = alt_virt;
750 *use_phys = alt_phys;
751 return 0;
752}
753
754static void send_page_end(struct gpmi_nand_data *this,
755 const void *source, unsigned length,
756 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
757 const void *used_virt, dma_addr_t used_phys)
758{
759 struct device *dev = this->dev;
760 if (used_virt == source)
761 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
762}
763
764static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
765{
766 struct device *dev = this->dev;
767
768 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
769 dma_free_coherent(dev, this->page_buffer_size,
770 this->page_buffer_virt,
771 this->page_buffer_phys);
772 kfree(this->cmd_buffer);
773 kfree(this->data_buffer_dma);
774
775 this->cmd_buffer = NULL;
776 this->data_buffer_dma = NULL;
777 this->page_buffer_virt = NULL;
778 this->page_buffer_size = 0;
779}
780
781/* Allocate the DMA buffers */
782static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
783{
784 struct bch_geometry *geo = &this->bch_geometry;
785 struct device *dev = this->dev;
786
787 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800788 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800789 if (this->cmd_buffer == NULL)
790 goto error_alloc;
791
792 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
Huang Shijie513d57e2012-07-17 14:14:02 +0800793 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800794 if (this->data_buffer_dma == NULL)
795 goto error_alloc;
796
797 /*
798 * [3] Allocate the page buffer.
799 *
800 * Both the payload buffer and the auxiliary buffer must appear on
801 * 32-bit boundaries. We presume the size of the payload buffer is a
802 * power of two and is much larger than four, which guarantees the
803 * auxiliary buffer will appear on a 32-bit boundary.
804 */
805 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
806 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
807 &this->page_buffer_phys, GFP_DMA);
808 if (!this->page_buffer_virt)
809 goto error_alloc;
810
811
812 /* Slice up the page buffer. */
813 this->payload_virt = this->page_buffer_virt;
814 this->payload_phys = this->page_buffer_phys;
815 this->auxiliary_virt = this->payload_virt + geo->payload_size;
816 this->auxiliary_phys = this->payload_phys + geo->payload_size;
817 return 0;
818
819error_alloc:
820 gpmi_free_dma_buffer(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800821 return -ENOMEM;
822}
823
824static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
825{
826 struct nand_chip *chip = mtd->priv;
827 struct gpmi_nand_data *this = chip->priv;
828 int ret;
829
830 /*
831 * Every operation begins with a command byte and a series of zero or
832 * more address bytes. These are distinguished by either the Address
833 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
834 * asserted. When MTD is ready to execute the command, it will deassert
835 * both latch enables.
836 *
837 * Rather than run a separate DMA operation for every single byte, we
838 * queue them up and run a single DMA operation for the entire series
839 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
840 */
841 if ((ctrl & (NAND_ALE | NAND_CLE))) {
842 if (data != NAND_CMD_NONE)
843 this->cmd_buffer[this->command_length++] = data;
844 return;
845 }
846
847 if (!this->command_length)
848 return;
849
850 ret = gpmi_send_command(this);
851 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +0800852 dev_err(this->dev, "Chip: %u, Error %d\n",
853 this->current_chip, ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800854
855 this->command_length = 0;
856}
857
858static int gpmi_dev_ready(struct mtd_info *mtd)
859{
860 struct nand_chip *chip = mtd->priv;
861 struct gpmi_nand_data *this = chip->priv;
862
863 return gpmi_is_ready(this, this->current_chip);
864}
865
866static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
867{
868 struct nand_chip *chip = mtd->priv;
869 struct gpmi_nand_data *this = chip->priv;
870
871 if ((this->current_chip < 0) && (chipnr >= 0))
872 gpmi_begin(this);
873 else if ((this->current_chip >= 0) && (chipnr < 0))
874 gpmi_end(this);
875
876 this->current_chip = chipnr;
877}
878
879static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
880{
881 struct nand_chip *chip = mtd->priv;
882 struct gpmi_nand_data *this = chip->priv;
883
Huang Shijiec2325962013-11-20 10:09:44 +0800884 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800885 this->upper_buf = buf;
886 this->upper_len = len;
887
888 gpmi_read_data(this);
889}
890
891static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
892{
893 struct nand_chip *chip = mtd->priv;
894 struct gpmi_nand_data *this = chip->priv;
895
Huang Shijiec2325962013-11-20 10:09:44 +0800896 dev_dbg(this->dev, "len is %d\n", len);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800897 this->upper_buf = (uint8_t *)buf;
898 this->upper_len = len;
899
900 gpmi_send_data(this);
901}
902
903static uint8_t gpmi_read_byte(struct mtd_info *mtd)
904{
905 struct nand_chip *chip = mtd->priv;
906 struct gpmi_nand_data *this = chip->priv;
907 uint8_t *buf = this->data_buffer_dma;
908
909 gpmi_read_buf(mtd, buf, 1);
910 return buf[0];
911}
912
913/*
914 * Handles block mark swapping.
915 * It can be called in swapping the block mark, or swapping it back,
916 * because the the operations are the same.
917 */
918static void block_mark_swapping(struct gpmi_nand_data *this,
919 void *payload, void *auxiliary)
920{
921 struct bch_geometry *nfc_geo = &this->bch_geometry;
922 unsigned char *p;
923 unsigned char *a;
924 unsigned int bit;
925 unsigned char mask;
926 unsigned char from_data;
927 unsigned char from_oob;
928
929 if (!this->swap_block_mark)
930 return;
931
932 /*
933 * If control arrives here, we're swapping. Make some convenience
934 * variables.
935 */
936 bit = nfc_geo->block_mark_bit_offset;
937 p = payload + nfc_geo->block_mark_byte_offset;
938 a = auxiliary;
939
940 /*
941 * Get the byte from the data area that overlays the block mark. Since
942 * the ECC engine applies its own view to the bits in the page, the
943 * physical block mark won't (in general) appear on a byte boundary in
944 * the data.
945 */
946 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
947
948 /* Get the byte from the OOB. */
949 from_oob = a[0];
950
951 /* Swap them. */
952 a[0] = from_data;
953
954 mask = (0x1 << bit) - 1;
955 p[0] = (p[0] & mask) | (from_oob << bit);
956
957 mask = ~0 << bit;
958 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
959}
960
961static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700962 uint8_t *buf, int oob_required, int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +0800963{
964 struct gpmi_nand_data *this = chip->priv;
965 struct bch_geometry *nfc_geo = &this->bch_geometry;
966 void *payload_virt;
967 dma_addr_t payload_phys;
968 void *auxiliary_virt;
969 dma_addr_t auxiliary_phys;
970 unsigned int i;
971 unsigned char *status;
Zach Sadeckib23b7462012-12-13 20:36:29 -0600972 unsigned int max_bitflips = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800973 int ret;
974
Huang Shijiec2325962013-11-20 10:09:44 +0800975 dev_dbg(this->dev, "page number is : %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +0800976 ret = read_page_prepare(this, buf, mtd->writesize,
977 this->payload_virt, this->payload_phys,
978 nfc_geo->payload_size,
979 &payload_virt, &payload_phys);
980 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +0800981 dev_err(this->dev, "Inadequate DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +0800982 ret = -ENOMEM;
983 return ret;
984 }
985 auxiliary_virt = this->auxiliary_virt;
986 auxiliary_phys = this->auxiliary_phys;
987
988 /* go! */
989 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
990 read_page_end(this, buf, mtd->writesize,
991 this->payload_virt, this->payload_phys,
992 nfc_geo->payload_size,
993 payload_virt, payload_phys);
994 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +0800995 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
Zach Sadeckib23b7462012-12-13 20:36:29 -0600996 return ret;
Huang Shijie10a2bca2011-09-08 10:47:09 +0800997 }
998
999 /* handle the block mark swapping */
1000 block_mark_swapping(this, payload_virt, auxiliary_virt);
1001
1002 /* Loop over status bytes, accumulating ECC status. */
Zach Sadeckib23b7462012-12-13 20:36:29 -06001003 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001004
1005 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1006 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1007 continue;
1008
1009 if (*status == STATUS_UNCORRECTABLE) {
Zach Sadeckib23b7462012-12-13 20:36:29 -06001010 mtd->ecc_stats.failed++;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001011 continue;
1012 }
Zach Sadeckib23b7462012-12-13 20:36:29 -06001013 mtd->ecc_stats.corrected += *status;
1014 max_bitflips = max_t(unsigned int, max_bitflips, *status);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001015 }
1016
Brian Norris7725cc82012-05-02 10:15:02 -07001017 if (oob_required) {
1018 /*
1019 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1020 * for details about our policy for delivering the OOB.
1021 *
1022 * We fill the caller's buffer with set bits, and then copy the
1023 * block mark to th caller's buffer. Note that, if block mark
1024 * swapping was necessary, it has already been done, so we can
1025 * rely on the first byte of the auxiliary buffer to contain
1026 * the block mark.
1027 */
1028 memset(chip->oob_poi, ~0, mtd->oobsize);
1029 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
Brian Norris7725cc82012-05-02 10:15:02 -07001030 }
Sascha Hauer6023813a2012-06-26 17:26:16 +02001031
1032 read_page_swap_end(this, buf, mtd->writesize,
1033 this->payload_virt, this->payload_phys,
1034 nfc_geo->payload_size,
1035 payload_virt, payload_phys);
Zach Sadeckib23b7462012-12-13 20:36:29 -06001036
1037 return max_bitflips;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001038}
1039
Josh Wufdbad98d2012-06-25 18:07:45 +08001040static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001041 const uint8_t *buf, int oob_required)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001042{
1043 struct gpmi_nand_data *this = chip->priv;
1044 struct bch_geometry *nfc_geo = &this->bch_geometry;
1045 const void *payload_virt;
1046 dma_addr_t payload_phys;
1047 const void *auxiliary_virt;
1048 dma_addr_t auxiliary_phys;
1049 int ret;
1050
Huang Shijiec2325962013-11-20 10:09:44 +08001051 dev_dbg(this->dev, "ecc write page.\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001052 if (this->swap_block_mark) {
1053 /*
1054 * If control arrives here, we're doing block mark swapping.
1055 * Since we can't modify the caller's buffers, we must copy them
1056 * into our own.
1057 */
1058 memcpy(this->payload_virt, buf, mtd->writesize);
1059 payload_virt = this->payload_virt;
1060 payload_phys = this->payload_phys;
1061
1062 memcpy(this->auxiliary_virt, chip->oob_poi,
1063 nfc_geo->auxiliary_size);
1064 auxiliary_virt = this->auxiliary_virt;
1065 auxiliary_phys = this->auxiliary_phys;
1066
1067 /* Handle block mark swapping. */
1068 block_mark_swapping(this,
1069 (void *) payload_virt, (void *) auxiliary_virt);
1070 } else {
1071 /*
1072 * If control arrives here, we're not doing block mark swapping,
1073 * so we can to try and use the caller's buffers.
1074 */
1075 ret = send_page_prepare(this,
1076 buf, mtd->writesize,
1077 this->payload_virt, this->payload_phys,
1078 nfc_geo->payload_size,
1079 &payload_virt, &payload_phys);
1080 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001081 dev_err(this->dev, "Inadequate payload DMA buffer\n");
Josh Wufdbad98d2012-06-25 18:07:45 +08001082 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001083 }
1084
1085 ret = send_page_prepare(this,
1086 chip->oob_poi, mtd->oobsize,
1087 this->auxiliary_virt, this->auxiliary_phys,
1088 nfc_geo->auxiliary_size,
1089 &auxiliary_virt, &auxiliary_phys);
1090 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001091 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
Huang Shijie10a2bca2011-09-08 10:47:09 +08001092 goto exit_auxiliary;
1093 }
1094 }
1095
1096 /* Ask the NFC. */
1097 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1098 if (ret)
Huang Shijieda40c162013-11-20 10:09:43 +08001099 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001100
1101 if (!this->swap_block_mark) {
1102 send_page_end(this, chip->oob_poi, mtd->oobsize,
1103 this->auxiliary_virt, this->auxiliary_phys,
1104 nfc_geo->auxiliary_size,
1105 auxiliary_virt, auxiliary_phys);
1106exit_auxiliary:
1107 send_page_end(this, buf, mtd->writesize,
1108 this->payload_virt, this->payload_phys,
1109 nfc_geo->payload_size,
1110 payload_virt, payload_phys);
1111 }
Josh Wufdbad98d2012-06-25 18:07:45 +08001112
1113 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001114}
1115
1116/*
1117 * There are several places in this driver where we have to handle the OOB and
1118 * block marks. This is the function where things are the most complicated, so
1119 * this is where we try to explain it all. All the other places refer back to
1120 * here.
1121 *
1122 * These are the rules, in order of decreasing importance:
1123 *
1124 * 1) Nothing the caller does can be allowed to imperil the block mark.
1125 *
1126 * 2) In read operations, the first byte of the OOB we return must reflect the
1127 * true state of the block mark, no matter where that block mark appears in
1128 * the physical page.
1129 *
1130 * 3) ECC-based read operations return an OOB full of set bits (since we never
1131 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1132 * return).
1133 *
1134 * 4) "Raw" read operations return a direct view of the physical bytes in the
1135 * page, using the conventional definition of which bytes are data and which
1136 * are OOB. This gives the caller a way to see the actual, physical bytes
1137 * in the page, without the distortions applied by our ECC engine.
1138 *
1139 *
1140 * What we do for this specific read operation depends on two questions:
1141 *
1142 * 1) Are we doing a "raw" read, or an ECC-based read?
1143 *
1144 * 2) Are we using block mark swapping or transcription?
1145 *
1146 * There are four cases, illustrated by the following Karnaugh map:
1147 *
1148 * | Raw | ECC-based |
1149 * -------------+-------------------------+-------------------------+
1150 * | Read the conventional | |
1151 * | OOB at the end of the | |
1152 * Swapping | page and return it. It | |
1153 * | contains exactly what | |
1154 * | we want. | Read the block mark and |
1155 * -------------+-------------------------+ return it in a buffer |
1156 * | Read the conventional | full of set bits. |
1157 * | OOB at the end of the | |
1158 * | page and also the block | |
1159 * Transcribing | mark in the metadata. | |
1160 * | Copy the block mark | |
1161 * | into the first byte of | |
1162 * | the OOB. | |
1163 * -------------+-------------------------+-------------------------+
1164 *
1165 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1166 * giving an accurate view of the actual, physical bytes in the page (we're
1167 * overwriting the block mark). That's OK because it's more important to follow
1168 * rule #2.
1169 *
1170 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1171 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1172 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1173 * ECC-based or raw view of the page is implicit in which function it calls
1174 * (there is a similar pair of ECC-based/raw functions for writing).
1175 *
Brian Norris271b874b2012-05-11 13:30:35 -07001176 * FIXME: The following paragraph is incorrect, now that there exist
1177 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1178 *
Huang Shijie10a2bca2011-09-08 10:47:09 +08001179 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1180 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1181 * caller wants an ECC-based or raw view of the page is not propagated down to
1182 * this driver.
1183 */
1184static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001185 int page)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001186{
1187 struct gpmi_nand_data *this = chip->priv;
1188
Huang Shijiec2325962013-11-20 10:09:44 +08001189 dev_dbg(this->dev, "page number is %d\n", page);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001190 /* clear the OOB buffer */
1191 memset(chip->oob_poi, ~0, mtd->oobsize);
1192
1193 /* Read out the conventional OOB. */
1194 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1195 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1196
1197 /*
1198 * Now, we want to make sure the block mark is correct. In the
1199 * Swapping/Raw case, we already have it. Otherwise, we need to
1200 * explicitly read it.
1201 */
1202 if (!this->swap_block_mark) {
1203 /* Read the block mark into the first byte of the OOB buffer. */
1204 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1205 chip->oob_poi[0] = chip->read_byte(mtd);
1206 }
1207
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001208 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001209}
1210
1211static int
1212gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1213{
Huang Shijie7a2b89a2013-09-25 14:58:15 +08001214 struct nand_oobfree *of = mtd->ecclayout->oobfree;
1215 int status = 0;
1216
1217 /* Do we have available oob area? */
1218 if (!of->length)
1219 return -EPERM;
1220
1221 if (!nand_is_slc(chip))
1222 return -EPERM;
1223
1224 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1225 chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1226 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1227
1228 status = chip->waitfunc(mtd, chip);
1229 return status & NAND_STATUS_FAIL ? -EIO : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001230}
1231
1232static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1233{
1234 struct nand_chip *chip = mtd->priv;
1235 struct gpmi_nand_data *this = chip->priv;
Brian Norris5a0edb22013-07-30 17:52:58 -07001236 int ret = 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001237 uint8_t *block_mark;
1238 int column, page, status, chipnr;
1239
Brian Norris5a0edb22013-07-30 17:52:58 -07001240 chipnr = (int)(ofs >> chip->chip_shift);
1241 chip->select_chip(mtd, chipnr);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001242
Brian Norris5a0edb22013-07-30 17:52:58 -07001243 column = this->swap_block_mark ? mtd->writesize : 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001244
Brian Norris5a0edb22013-07-30 17:52:58 -07001245 /* Write the block mark. */
1246 block_mark = this->data_buffer_dma;
1247 block_mark[0] = 0; /* bad block marker */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001248
Brian Norris5a0edb22013-07-30 17:52:58 -07001249 /* Shift to get page */
1250 page = (int)(ofs >> chip->page_shift);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001251
Brian Norris5a0edb22013-07-30 17:52:58 -07001252 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1253 chip->write_buf(mtd, block_mark, 1);
1254 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001255
Brian Norris5a0edb22013-07-30 17:52:58 -07001256 status = chip->waitfunc(mtd, chip);
1257 if (status & NAND_STATUS_FAIL)
1258 ret = -EIO;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001259
Brian Norris5a0edb22013-07-30 17:52:58 -07001260 chip->select_chip(mtd, -1);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001261
1262 return ret;
1263}
1264
Wolfram Sanga78da282012-03-21 19:29:17 +01001265static int nand_boot_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001266{
1267 struct boot_rom_geometry *geometry = &this->rom_geometry;
1268
1269 /*
1270 * Set the boot block stride size.
1271 *
1272 * In principle, we should be reading this from the OTP bits, since
1273 * that's where the ROM is going to get it. In fact, we don't have any
1274 * way to read the OTP bits, so we go with the default and hope for the
1275 * best.
1276 */
1277 geometry->stride_size_in_pages = 64;
1278
1279 /*
1280 * Set the search area stride exponent.
1281 *
1282 * In principle, we should be reading this from the OTP bits, since
1283 * that's where the ROM is going to get it. In fact, we don't have any
1284 * way to read the OTP bits, so we go with the default and hope for the
1285 * best.
1286 */
1287 geometry->search_area_stride_exponent = 2;
1288 return 0;
1289}
1290
1291static const char *fingerprint = "STMP";
Wolfram Sanga78da282012-03-21 19:29:17 +01001292static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001293{
1294 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1295 struct device *dev = this->dev;
1296 struct mtd_info *mtd = &this->mtd;
1297 struct nand_chip *chip = &this->nand;
1298 unsigned int search_area_size_in_strides;
1299 unsigned int stride;
1300 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001301 uint8_t *buffer = chip->buffers->databuf;
1302 int saved_chip_number;
1303 int found_an_ncb_fingerprint = false;
1304
1305 /* Compute the number of strides in a search area. */
1306 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1307
1308 saved_chip_number = this->current_chip;
1309 chip->select_chip(mtd, 0);
1310
1311 /*
1312 * Loop through the first search area, looking for the NCB fingerprint.
1313 */
1314 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1315
1316 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001317 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001318 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001319
1320 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1321
1322 /*
1323 * Read the NCB fingerprint. The fingerprint is four bytes long
1324 * and starts in the 12th byte of the page.
1325 */
1326 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1327 chip->read_buf(mtd, buffer, strlen(fingerprint));
1328
1329 /* Look for the fingerprint. */
1330 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1331 found_an_ncb_fingerprint = true;
1332 break;
1333 }
1334
1335 }
1336
1337 chip->select_chip(mtd, saved_chip_number);
1338
1339 if (found_an_ncb_fingerprint)
1340 dev_dbg(dev, "\tFound a fingerprint\n");
1341 else
1342 dev_dbg(dev, "\tNo fingerprint found\n");
1343 return found_an_ncb_fingerprint;
1344}
1345
1346/* Writes a transcription stamp. */
Wolfram Sanga78da282012-03-21 19:29:17 +01001347static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001348{
1349 struct device *dev = this->dev;
1350 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1351 struct mtd_info *mtd = &this->mtd;
1352 struct nand_chip *chip = &this->nand;
1353 unsigned int block_size_in_pages;
1354 unsigned int search_area_size_in_strides;
1355 unsigned int search_area_size_in_pages;
1356 unsigned int search_area_size_in_blocks;
1357 unsigned int block;
1358 unsigned int stride;
1359 unsigned int page;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001360 uint8_t *buffer = chip->buffers->databuf;
1361 int saved_chip_number;
1362 int status;
1363
1364 /* Compute the search area geometry. */
1365 block_size_in_pages = mtd->erasesize / mtd->writesize;
1366 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1367 search_area_size_in_pages = search_area_size_in_strides *
1368 rom_geo->stride_size_in_pages;
1369 search_area_size_in_blocks =
1370 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1371 block_size_in_pages;
1372
1373 dev_dbg(dev, "Search Area Geometry :\n");
1374 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1375 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1376 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1377
1378 /* Select chip 0. */
1379 saved_chip_number = this->current_chip;
1380 chip->select_chip(mtd, 0);
1381
1382 /* Loop over blocks in the first search area, erasing them. */
1383 dev_dbg(dev, "Erasing the search area...\n");
1384
1385 for (block = 0; block < search_area_size_in_blocks; block++) {
1386 /* Compute the page address. */
1387 page = block * block_size_in_pages;
1388
1389 /* Erase this block. */
1390 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1391 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1392 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1393
1394 /* Wait for the erase to finish. */
1395 status = chip->waitfunc(mtd, chip);
1396 if (status & NAND_STATUS_FAIL)
1397 dev_err(dev, "[%s] Erase failed.\n", __func__);
1398 }
1399
1400 /* Write the NCB fingerprint into the page buffer. */
1401 memset(buffer, ~0, mtd->writesize);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001402 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1403
1404 /* Loop through the first search area, writing NCB fingerprints. */
1405 dev_dbg(dev, "Writing NCB fingerprints...\n");
1406 for (stride = 0; stride < search_area_size_in_strides; stride++) {
Huang Shijie513d57e2012-07-17 14:14:02 +08001407 /* Compute the page addresses. */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001408 page = stride * rom_geo->stride_size_in_pages;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001409
1410 /* Write the first page of the current stride. */
1411 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1412 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Brian Norris1fbb9382012-05-02 10:14:55 -07001413 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001414 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1415
1416 /* Wait for the write to finish. */
1417 status = chip->waitfunc(mtd, chip);
1418 if (status & NAND_STATUS_FAIL)
1419 dev_err(dev, "[%s] Write failed.\n", __func__);
1420 }
1421
1422 /* Deselect chip 0. */
1423 chip->select_chip(mtd, saved_chip_number);
1424 return 0;
1425}
1426
Wolfram Sanga78da282012-03-21 19:29:17 +01001427static int mx23_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001428{
1429 struct device *dev = this->dev;
1430 struct nand_chip *chip = &this->nand;
1431 struct mtd_info *mtd = &this->mtd;
1432 unsigned int block_count;
1433 unsigned int block;
1434 int chipnr;
1435 int page;
1436 loff_t byte;
1437 uint8_t block_mark;
1438 int ret = 0;
1439
1440 /*
1441 * If control arrives here, we can't use block mark swapping, which
1442 * means we're forced to use transcription. First, scan for the
1443 * transcription stamp. If we find it, then we don't have to do
1444 * anything -- the block marks are already transcribed.
1445 */
1446 if (mx23_check_transcription_stamp(this))
1447 return 0;
1448
1449 /*
1450 * If control arrives here, we couldn't find a transcription stamp, so
1451 * so we presume the block marks are in the conventional location.
1452 */
1453 dev_dbg(dev, "Transcribing bad block marks...\n");
1454
1455 /* Compute the number of blocks in the entire medium. */
1456 block_count = chip->chipsize >> chip->phys_erase_shift;
1457
1458 /*
1459 * Loop over all the blocks in the medium, transcribing block marks as
1460 * we go.
1461 */
1462 for (block = 0; block < block_count; block++) {
1463 /*
1464 * Compute the chip, page and byte addresses for this block's
1465 * conventional mark.
1466 */
1467 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1468 page = block << (chip->phys_erase_shift - chip->page_shift);
1469 byte = block << chip->phys_erase_shift;
1470
1471 /* Send the command to read the conventional block mark. */
1472 chip->select_chip(mtd, chipnr);
1473 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1474 block_mark = chip->read_byte(mtd);
1475 chip->select_chip(mtd, -1);
1476
1477 /*
1478 * Check if the block is marked bad. If so, we need to mark it
1479 * again, but this time the result will be a mark in the
1480 * location where we transcribe block marks.
1481 */
1482 if (block_mark != 0xff) {
1483 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1484 ret = chip->block_markbad(mtd, byte);
1485 if (ret)
1486 dev_err(dev, "Failed to mark block bad with "
1487 "ret %d\n", ret);
1488 }
1489 }
1490
1491 /* Write the stamp that indicates we've transcribed the block marks. */
1492 mx23_write_transcription_stamp(this);
1493 return 0;
1494}
1495
Wolfram Sanga78da282012-03-21 19:29:17 +01001496static int nand_boot_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001497{
1498 nand_boot_set_geometry(this);
1499
1500 /* This is ROM arch-specific initilization before the BBT scanning. */
1501 if (GPMI_IS_MX23(this))
1502 return mx23_boot_init(this);
1503 return 0;
1504}
1505
Wolfram Sanga78da282012-03-21 19:29:17 +01001506static int gpmi_set_geometry(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001507{
1508 int ret;
1509
1510 /* Free the temporary DMA memory for reading ID. */
1511 gpmi_free_dma_buffer(this);
1512
1513 /* Set up the NFC geometry which is used by BCH. */
1514 ret = bch_set_geometry(this);
1515 if (ret) {
Huang Shijieda40c162013-11-20 10:09:43 +08001516 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001517 return ret;
1518 }
1519
1520 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1521 return gpmi_alloc_dma_buffer(this);
1522}
1523
Huang Shijieccce4172013-11-14 14:25:47 +08001524static void gpmi_nand_exit(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001525{
Huang Shijief720e7c2013-08-16 10:10:08 +08001526 nand_release(&this->mtd);
1527 gpmi_free_dma_buffer(this);
1528}
1529
1530static int gpmi_init_last(struct gpmi_nand_data *this)
1531{
1532 struct mtd_info *mtd = &this->mtd;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001533 struct nand_chip *chip = mtd->priv;
Huang Shijief720e7c2013-08-16 10:10:08 +08001534 struct nand_ecc_ctrl *ecc = &chip->ecc;
1535 struct bch_geometry *bch_geo = &this->bch_geometry;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001536 int ret;
1537
Huang Shijied7364a272013-11-14 14:25:45 +08001538 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1539 this->swap_block_mark = !GPMI_IS_MX23(this);
1540
1541 /* Set up the medium geometry */
1542 ret = gpmi_set_geometry(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001543 if (ret)
1544 return ret;
1545
Huang Shijief720e7c2013-08-16 10:10:08 +08001546 /* Init the nand_ecc_ctrl{} */
1547 ecc->read_page = gpmi_ecc_read_page;
1548 ecc->write_page = gpmi_ecc_write_page;
1549 ecc->read_oob = gpmi_ecc_read_oob;
1550 ecc->write_oob = gpmi_ecc_write_oob;
1551 ecc->mode = NAND_ECC_HW;
1552 ecc->size = bch_geo->ecc_chunk_size;
1553 ecc->strength = bch_geo->ecc_strength;
1554 ecc->layout = &gpmi_hw_ecclayout;
1555
Huang Shijie995fbbf2012-09-13 14:57:59 +08001556 /*
1557 * Can we enable the extra features? such as EDO or Sync mode.
1558 *
1559 * We do not check the return value now. That's means if we fail in
1560 * enable the extra features, we still can run in the normal way.
1561 */
1562 gpmi_extra_init(this);
1563
Huang Shijief720e7c2013-08-16 10:10:08 +08001564 return 0;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001565}
1566
Huang Shijieccce4172013-11-14 14:25:47 +08001567static int gpmi_nand_init(struct gpmi_nand_data *this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001568{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001569 struct mtd_info *mtd = &this->mtd;
1570 struct nand_chip *chip = &this->nand;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001571 struct mtd_part_parser_data ppdata = {};
Huang Shijie10a2bca2011-09-08 10:47:09 +08001572 int ret;
1573
1574 /* init current chip */
1575 this->current_chip = -1;
1576
1577 /* init the MTD data structures */
1578 mtd->priv = chip;
1579 mtd->name = "gpmi-nand";
1580 mtd->owner = THIS_MODULE;
1581
1582 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1583 chip->priv = this;
1584 chip->select_chip = gpmi_select_chip;
1585 chip->cmd_ctrl = gpmi_cmd_ctrl;
1586 chip->dev_ready = gpmi_dev_ready;
1587 chip->read_byte = gpmi_read_byte;
1588 chip->read_buf = gpmi_read_buf;
1589 chip->write_buf = gpmi_write_buf;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001590 chip->badblock_pattern = &gpmi_bbt_descr;
1591 chip->block_markbad = gpmi_block_markbad;
1592 chip->options |= NAND_NO_SUBPAGE_WRITE;
Huang Shijiec50c6942012-07-03 16:24:32 +08001593 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1594 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001595
Huang Shijief720e7c2013-08-16 10:10:08 +08001596 /*
1597 * Allocate a temporary DMA buffer for reading ID in the
1598 * nand_scan_ident().
1599 */
Huang Shijie10a2bca2011-09-08 10:47:09 +08001600 this->bch_geometry.payload_size = 1024;
1601 this->bch_geometry.auxiliary_size = 128;
1602 ret = gpmi_alloc_dma_buffer(this);
1603 if (ret)
1604 goto err_out;
1605
Huang Shijie80bd33a2013-11-07 17:46:37 +08001606 ret = nand_scan_ident(mtd, GPMI_IS_MX6Q(this) ? 2 : 1, NULL);
Huang Shijief720e7c2013-08-16 10:10:08 +08001607 if (ret)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001608 goto err_out;
Huang Shijief720e7c2013-08-16 10:10:08 +08001609
1610 ret = gpmi_init_last(this);
1611 if (ret)
1612 goto err_out;
1613
Huang Shijie885d71e2013-11-12 12:23:08 +08001614 chip->options |= NAND_SKIP_BBTSCAN;
Huang Shijief720e7c2013-08-16 10:10:08 +08001615 ret = nand_scan_tail(mtd);
1616 if (ret)
1617 goto err_out;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001618
Huang Shijie885d71e2013-11-12 12:23:08 +08001619 ret = nand_boot_init(this);
1620 if (ret)
1621 goto err_out;
1622 chip->scan_bbt(mtd);
1623
Huang Shijiee10db1f2012-05-04 21:42:05 -04001624 ppdata.of_node = this->pdev->dev.of_node;
1625 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001626 if (ret)
1627 goto err_out;
1628 return 0;
1629
1630err_out:
Huang Shijieccce4172013-11-14 14:25:47 +08001631 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001632 return ret;
1633}
1634
Huang Shijiee10db1f2012-05-04 21:42:05 -04001635static const struct platform_device_id gpmi_ids[] = {
1636 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1637 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
Huang Shijie9013bb42012-05-04 21:42:06 -04001638 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001639 {}
Huang Shijiee10db1f2012-05-04 21:42:05 -04001640};
1641
1642static const struct of_device_id gpmi_nand_id_table[] = {
1643 {
1644 .compatible = "fsl,imx23-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001645 .data = (void *)&gpmi_ids[IS_MX23],
Huang Shijiee10db1f2012-05-04 21:42:05 -04001646 }, {
1647 .compatible = "fsl,imx28-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001648 .data = (void *)&gpmi_ids[IS_MX28],
Huang Shijie9013bb42012-05-04 21:42:06 -04001649 }, {
1650 .compatible = "fsl,imx6q-gpmi-nand",
Lothar Waßmannd41f9502013-08-07 08:15:37 +02001651 .data = (void *)&gpmi_ids[IS_MX6Q],
Huang Shijiee10db1f2012-05-04 21:42:05 -04001652 }, {}
1653};
1654MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1655
Bill Pemberton06f25512012-11-19 13:23:07 -05001656static int gpmi_nand_probe(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001657{
Huang Shijie10a2bca2011-09-08 10:47:09 +08001658 struct gpmi_nand_data *this;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001659 const struct of_device_id *of_id;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001660 int ret;
1661
Huang Shijiee10db1f2012-05-04 21:42:05 -04001662 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1663 if (of_id) {
1664 pdev->id_entry = of_id->data;
1665 } else {
Huang Shijieda40c162013-11-20 10:09:43 +08001666 dev_err(&pdev->dev, "Failed to find the right device id.\n");
Lothar Waßmann52a073b2013-08-07 08:15:38 +02001667 return -ENODEV;
Huang Shijiee10db1f2012-05-04 21:42:05 -04001668 }
1669
Fabio Estevamedaf4d42013-11-05 00:07:05 -02001670 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
Huang Shijieda40c162013-11-20 10:09:43 +08001671 if (!this)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001672 return -ENOMEM;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001673
1674 platform_set_drvdata(pdev, this);
1675 this->pdev = pdev;
1676 this->dev = &pdev->dev;
Huang Shijie10a2bca2011-09-08 10:47:09 +08001677
1678 ret = acquire_resources(this);
1679 if (ret)
1680 goto exit_acquire_resources;
1681
1682 ret = init_hardware(this);
1683 if (ret)
1684 goto exit_nfc_init;
1685
Huang Shijieccce4172013-11-14 14:25:47 +08001686 ret = gpmi_nand_init(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001687 if (ret)
1688 goto exit_nfc_init;
1689
Fabio Estevam490e2802012-09-05 11:35:24 -03001690 dev_info(this->dev, "driver registered.\n");
1691
Huang Shijie10a2bca2011-09-08 10:47:09 +08001692 return 0;
1693
1694exit_nfc_init:
1695 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001696exit_acquire_resources:
Fabio Estevam490e2802012-09-05 11:35:24 -03001697 dev_err(this->dev, "driver registration failed: %d\n", ret);
1698
Huang Shijie10a2bca2011-09-08 10:47:09 +08001699 return ret;
1700}
1701
Bill Pemberton810b7e02012-11-19 13:26:04 -05001702static int gpmi_nand_remove(struct platform_device *pdev)
Huang Shijie10a2bca2011-09-08 10:47:09 +08001703{
1704 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1705
Huang Shijieccce4172013-11-14 14:25:47 +08001706 gpmi_nand_exit(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001707 release_resources(this);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001708 return 0;
1709}
1710
Huang Shijie10a2bca2011-09-08 10:47:09 +08001711static struct platform_driver gpmi_nand_driver = {
1712 .driver = {
1713 .name = "gpmi-nand",
Huang Shijiee10db1f2012-05-04 21:42:05 -04001714 .of_match_table = gpmi_nand_id_table,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001715 },
1716 .probe = gpmi_nand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -05001717 .remove = gpmi_nand_remove,
Huang Shijie10a2bca2011-09-08 10:47:09 +08001718 .id_table = gpmi_ids,
1719};
Fabio Estevam490e2802012-09-05 11:35:24 -03001720module_platform_driver(gpmi_nand_driver);
Huang Shijie10a2bca2011-09-08 10:47:09 +08001721
1722MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1723MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1724MODULE_LICENSE("GPL");