Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / mtd / nand / gpmi-nand / gpmi-nand.c
blob99940bd056f320d96a054bc5223a04e47a5d0a89
1 /*
2 * Freescale GPMI NAND Flash Driver
4 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
5 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
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.
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.
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.
21 #include <linux/clk.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/mtd/gpmi-nand.h>
25 #include <linux/mtd/partitions.h>
27 #include "gpmi-nand.h"
29 /* add our owner bbt descriptor */
30 static uint8_t scan_ff_pattern[] = { 0xff };
31 static struct nand_bbt_descr gpmi_bbt_descr = {
32 .options = 0,
33 .offs = 0,
34 .len = 1,
35 .pattern = scan_ff_pattern
38 /* We will use all the (page + OOB). */
39 static struct nand_ecclayout gpmi_hw_ecclayout = {
40 .eccbytes = 0,
41 .eccpos = { 0, },
42 .oobfree = { {.offset = 0, .length = 0} }
45 /* debug control */
46 #define GPMI_DEBUG_READ_OOB 0x0001
47 #define GPMI_DEBUG_READ 0x0002
48 #define GPMI_DEBUG_WRITE 0x0004
49 #define GPMI_DEBUG_ECC_READ 0x0008
50 #define GPMI_DEBUG_ECC_WRITE 0x0010
51 #define GPMI_DEBUG_VERBOSE 0x0020
53 #define logio(level) \
54 do { \
55 if (gpmi_debug & level) \
56 pr_info("%s : %d\n", __func__, __LINE__); \
57 } while (0)
59 int gpmi_debug;
60 module_param(gpmi_debug, int, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(gpmi_debug, "print out the debug info.");
63 static irqreturn_t bch_irq(int irq, void *cookie)
65 struct gpmi_nand_data *this = cookie;
67 gpmi_clear_bch(this);
68 complete(&this->bch_done);
69 return IRQ_HANDLED;
72 /* calculate the ECC strength by hand */
73 static inline int get_ecc_strength(struct gpmi_nand_data *this)
75 struct mtd_info *mtd = &this->mtd;
76 int ecc_strength = 0;
78 switch (mtd->writesize) {
79 case 2048:
80 ecc_strength = 8;
81 break;
82 case 4096:
83 switch (mtd->oobsize) {
84 case 128:
85 ecc_strength = 8;
86 break;
87 case 224:
88 case 218:
89 ecc_strength = 16;
90 break;
92 break;
93 case 8192:
94 ecc_strength = 24;
95 break;
98 return ecc_strength;
101 int common_nfc_set_geometry(struct gpmi_nand_data *this)
103 struct bch_geometry *geo = &this->bch_geometry;
104 struct mtd_info *mtd = &this->mtd;
105 unsigned int metadata_size;
106 unsigned int status_size;
107 unsigned int chunk_data_size_in_bits;
108 unsigned int chunk_ecc_size_in_bits;
109 unsigned int chunk_total_size_in_bits;
110 unsigned int block_mark_chunk_number;
111 unsigned int block_mark_chunk_bit_offset;
112 unsigned int block_mark_bit_offset;
113 int gf_len = 13;/* use GP13 by default */
115 /* We only support BCH now. */
116 geo->ecc_algorithm = "BCH";
119 * The size of the metadata can be changed, though we set it to 10
120 * bytes now. But it can't be too large, because we have to save
121 * enough space for BCH.
123 geo->metadata_size_in_bytes = 10;
125 /* ECC chunks, used by default. */
126 geo->ecc_chunk_size_in_bytes = 512;
129 * Compute the total number of ECC chunks in a page. This includes the
130 * slightly larger chunk at the beginning of the page, which contains
131 * both data and metadata.
133 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size_in_bytes;
136 * We use the same ECC strength for all chunks, including the first one.
138 geo->ecc_strength = get_ecc_strength(this);
139 if (!geo->ecc_strength) {
140 pr_err("Page size:%d, OOB:%d\n", mtd->writesize, mtd->oobsize);
141 return -EINVAL;
144 /* Compute the page size, include page and oob. */
145 geo->page_size_in_bytes = mtd->writesize + mtd->oobsize;
146 geo->payload_size_in_bytes = mtd->writesize;
149 * In principle, computing the auxiliary buffer geometry is NFC
150 * version-specific. However, at this writing, all versions share the
151 * same model, so this code can also be shared.
153 * The auxiliary buffer contains the metadata and the ECC status. The
154 * metadata is padded to the nearest 32-bit boundary. The ECC status
155 * contains one byte for every ECC chunk, and is also padded to the
156 * nearest 32-bit boundary.
158 metadata_size = ALIGN(geo->metadata_size_in_bytes, 4);
159 status_size = ALIGN(geo->ecc_chunk_count, 4);
161 geo->auxiliary_size_in_bytes = metadata_size + status_size;
162 geo->auxiliary_status_offset = metadata_size;
164 /* Check if we're going to do block mark swapping. */
165 if (!this->swap_block_mark)
166 return 0;
169 * If control arrives here, we're doing block mark swapping, so we need
170 * to compute the byte and bit offsets of the physical block mark within
171 * the ECC-based view of the page data. In principle, this isn't a
172 * difficult computation -- but it's very important and it's easy to get
173 * it wrong, so we do it carefully.
175 * Note that this calculation is simpler because we use the same ECC
176 * strength for all chunks, including the zero'th one, which contains
177 * the metadata. The calculation would be slightly more complicated
178 * otherwise.
180 * We start by computing the physical bit offset of the block mark. We
181 * then subtract the number of metadata and ECC bits appearing before
182 * the mark to arrive at its bit offset within the data alone.
185 /* Compute some important facts about chunk geometry. */
186 chunk_data_size_in_bits = geo->ecc_chunk_size_in_bytes * 8;
187 chunk_ecc_size_in_bits = geo->ecc_strength * gf_len;
188 chunk_total_size_in_bits = chunk_data_size_in_bits
189 + chunk_ecc_size_in_bits;
191 /* Compute the bit offset of the block mark within the physical page. */
192 block_mark_bit_offset = mtd->writesize * 8;
194 /* Subtract the metadata bits. */
195 block_mark_bit_offset -= geo->metadata_size_in_bytes * 8;
198 * Compute the chunk number (starting at zero) in which the block mark
199 * appears.
201 block_mark_chunk_number =
202 block_mark_bit_offset / chunk_total_size_in_bits;
205 * Compute the bit offset of the block mark within its chunk, and
206 * validate it.
208 block_mark_chunk_bit_offset =
209 block_mark_bit_offset -
210 (block_mark_chunk_number * chunk_total_size_in_bits);
212 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits) {
214 * If control arrives here, the block mark actually appears in
215 * the ECC bits of this chunk. This wont' work.
217 pr_err("Unsupported page geometry : %u:%u\n",
218 mtd->writesize, mtd->oobsize);
219 return -EINVAL;
223 * Now that we know the chunk number in which the block mark appears,
224 * we can subtract all the ECC bits that appear before it.
226 block_mark_bit_offset -=
227 block_mark_chunk_number * chunk_ecc_size_in_bits;
230 * We now know the absolute bit offset of the block mark within the
231 * ECC-based data. We can now compute the byte offset and the bit
232 * offset within the byte.
234 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
235 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
237 return 0;
240 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
242 int chipnr = this->current_chip;
244 BUG_ON(chipnr < 0);
245 return this->dma_chans[chipnr];
248 /* Can we use the upper's buffer directly for DMA? */
249 void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
251 struct scatterlist *sgl = &this->data_sgl;
252 int ret;
254 this->direct_dma_map_ok = true;
256 /* first try to map the upper buffer directly */
257 sg_init_one(sgl, this->upper_buf, this->upper_len);
258 ret = dma_map_sg(this->dev, sgl, 1, dr);
259 if (ret == 0) {
260 /* We have to use our own DMA buffer. */
261 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
263 if (dr == DMA_TO_DEVICE)
264 memcpy(this->data_buffer_dma, this->upper_buf,
265 this->upper_len);
267 ret = dma_map_sg(this->dev, sgl, 1, dr);
268 BUG_ON(ret == 0);
270 this->direct_dma_map_ok = false;
274 /* This will be called after the DMA operation is finished. */
275 static void dma_irq_callback(void *param)
277 struct gpmi_nand_data *this = param;
278 struct completion *dma_c = &this->dma_done;
280 complete(dma_c);
282 switch (this->dma_type) {
283 case DMA_FOR_COMMAND:
284 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
285 break;
287 case DMA_FOR_READ_DATA:
288 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
289 if (this->direct_dma_map_ok == false)
290 memcpy(this->upper_buf, this->data_buffer_dma,
291 this->upper_len);
292 break;
294 case DMA_FOR_WRITE_DATA:
295 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
296 break;
298 case DMA_FOR_READ_ECC_PAGE:
299 case DMA_FOR_WRITE_ECC_PAGE:
300 /* We have to wait the BCH interrupt to finish. */
301 break;
303 default:
304 BUG();
308 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
309 struct dma_async_tx_descriptor *desc)
311 struct completion *dma_c = &this->dma_done;
312 int err;
314 init_completion(dma_c);
316 desc->callback = dma_irq_callback;
317 desc->callback_param = this;
318 dmaengine_submit(desc);
320 /* Wait for the interrupt from the DMA block. */
321 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
322 err = (!err) ? -ETIMEDOUT : 0;
323 if (err) {
324 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
325 if (gpmi_debug & GPMI_DEBUG_VERBOSE)
326 gpmi_dump_info(this);
328 return err;
332 * This function is used in BCH reading or BCH writing pages.
333 * It will wait for the BCH interrupt as long as ONE second.
334 * Actually, we must wait for two interrupts :
335 * [1] firstly the DMA interrupt and
336 * [2] secondly the BCH interrupt.
338 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
339 struct dma_async_tx_descriptor *desc)
341 int err;
343 /* Prepare to receive an interrupt from the BCH block. */
344 init_completion(&this->bch_done);
346 /* start the DMA */
347 start_dma_without_bch_irq(this, desc);
349 /* Wait for the interrupt from the BCH block. */
350 err = wait_for_completion_timeout(&this->bch_done,
351 msecs_to_jiffies(1000));
352 err = (!err) ? -ETIMEDOUT : 0;
353 if (err) {
354 pr_err("BCH timeout!\n");
355 if (gpmi_debug & GPMI_DEBUG_VERBOSE)
356 gpmi_dump_info(this);
358 return err;
361 static int __devinit
362 acquire_register_block(struct gpmi_nand_data *this, const char *res_name)
364 struct platform_device *pdev = this->pdev;
365 struct resources *res = &this->resources;
366 struct resource *r;
367 void *p;
369 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
370 if (!r) {
371 pr_err("Can't get resource for %s\n", res_name);
372 return -ENXIO;
375 p = ioremap(r->start, resource_size(r));
376 if (!p) {
377 pr_err("Can't remap %s\n", res_name);
378 return -ENOMEM;
381 if (res_name == GPMI_NAND_GPMI_REGS_ADDR_RES_NAME)
382 res->gpmi_regs = p;
383 else if (res_name == GPMI_NAND_BCH_REGS_ADDR_RES_NAME)
384 res->bch_regs = p;
385 else
386 BUG();
388 return 0;
391 static void release_register_block(struct gpmi_nand_data *this)
393 struct resources *res = &this->resources;
394 if (res->gpmi_regs)
395 iounmap(res->gpmi_regs);
396 if (res->bch_regs)
397 iounmap(res->bch_regs);
398 res->gpmi_regs = NULL;
399 res->bch_regs = NULL;
402 static int __devinit
403 acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
405 struct platform_device *pdev = this->pdev;
406 struct resources *res = &this->resources;
407 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
408 struct resource *r;
409 int err;
411 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
412 if (!r) {
413 pr_err("Can't get resource for %s\n", res_name);
414 return -ENXIO;
417 BUG_ON(r->start != r->end);
418 err = request_irq(r->start, irq_h, 0, res_name, this);
419 if (err) {
420 pr_err("Can't own %s\n", res_name);
421 return err;
424 res->bch_low_interrupt = r->start;
425 res->bch_high_interrupt = r->end;
426 return 0;
429 static void release_bch_irq(struct gpmi_nand_data *this)
431 struct resources *res = &this->resources;
432 int i = res->bch_low_interrupt;
434 for (; i <= res->bch_high_interrupt; i++)
435 free_irq(i, this);
438 static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
440 struct gpmi_nand_data *this = param;
441 struct resource *r = this->private;
443 if (!mxs_dma_is_apbh(chan))
444 return false;
446 * only catch the GPMI dma channels :
447 * for mx23 : MX23_DMA_GPMI0 ~ MX23_DMA_GPMI3
448 * (These four channels share the same IRQ!)
450 * for mx28 : MX28_DMA_GPMI0 ~ MX28_DMA_GPMI7
451 * (These eight channels share the same IRQ!)
453 if (r->start <= chan->chan_id && chan->chan_id <= r->end) {
454 chan->private = &this->dma_data;
455 return true;
457 return false;
460 static void release_dma_channels(struct gpmi_nand_data *this)
462 unsigned int i;
463 for (i = 0; i < DMA_CHANS; i++)
464 if (this->dma_chans[i]) {
465 dma_release_channel(this->dma_chans[i]);
466 this->dma_chans[i] = NULL;
470 static int __devinit acquire_dma_channels(struct gpmi_nand_data *this)
472 struct platform_device *pdev = this->pdev;
473 struct gpmi_nand_platform_data *pdata = this->pdata;
474 struct resources *res = &this->resources;
475 struct resource *r, *r_dma;
476 unsigned int i;
478 r = platform_get_resource_byname(pdev, IORESOURCE_DMA,
479 GPMI_NAND_DMA_CHANNELS_RES_NAME);
480 r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
481 GPMI_NAND_DMA_INTERRUPT_RES_NAME);
482 if (!r || !r_dma) {
483 pr_err("Can't get resource for DMA\n");
484 return -ENXIO;
487 /* used in gpmi_dma_filter() */
488 this->private = r;
490 for (i = r->start; i <= r->end; i++) {
491 struct dma_chan *dma_chan;
492 dma_cap_mask_t mask;
494 if (i - r->start >= pdata->max_chip_count)
495 break;
497 dma_cap_zero(mask);
498 dma_cap_set(DMA_SLAVE, mask);
500 /* get the DMA interrupt */
501 if (r_dma->start == r_dma->end) {
502 /* only register the first. */
503 if (i == r->start)
504 this->dma_data.chan_irq = r_dma->start;
505 else
506 this->dma_data.chan_irq = NO_IRQ;
507 } else
508 this->dma_data.chan_irq = r_dma->start + (i - r->start);
510 dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
511 if (!dma_chan)
512 goto acquire_err;
514 /* fill the first empty item */
515 this->dma_chans[i - r->start] = dma_chan;
518 res->dma_low_channel = r->start;
519 res->dma_high_channel = i;
520 return 0;
522 acquire_err:
523 pr_err("Can't acquire DMA channel %u\n", i);
524 release_dma_channels(this);
525 return -EINVAL;
528 static int __devinit acquire_resources(struct gpmi_nand_data *this)
530 struct resources *res = &this->resources;
531 int ret;
533 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
534 if (ret)
535 goto exit_regs;
537 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
538 if (ret)
539 goto exit_regs;
541 ret = acquire_bch_irq(this, bch_irq);
542 if (ret)
543 goto exit_regs;
545 ret = acquire_dma_channels(this);
546 if (ret)
547 goto exit_dma_channels;
549 res->clock = clk_get(&this->pdev->dev, NULL);
550 if (IS_ERR(res->clock)) {
551 pr_err("can not get the clock\n");
552 ret = -ENOENT;
553 goto exit_clock;
555 return 0;
557 exit_clock:
558 release_dma_channels(this);
559 exit_dma_channels:
560 release_bch_irq(this);
561 exit_regs:
562 release_register_block(this);
563 return ret;
566 static void release_resources(struct gpmi_nand_data *this)
568 struct resources *r = &this->resources;
570 clk_put(r->clock);
571 release_register_block(this);
572 release_bch_irq(this);
573 release_dma_channels(this);
576 static int __devinit init_hardware(struct gpmi_nand_data *this)
578 int ret;
581 * This structure contains the "safe" GPMI timing that should succeed
582 * with any NAND Flash device
583 * (although, with less-than-optimal performance).
585 struct nand_timing safe_timing = {
586 .data_setup_in_ns = 80,
587 .data_hold_in_ns = 60,
588 .address_setup_in_ns = 25,
589 .gpmi_sample_delay_in_ns = 6,
590 .tREA_in_ns = -1,
591 .tRLOH_in_ns = -1,
592 .tRHOH_in_ns = -1,
595 /* Initialize the hardwares. */
596 ret = gpmi_init(this);
597 if (ret)
598 return ret;
600 this->timing = safe_timing;
601 return 0;
604 static int read_page_prepare(struct gpmi_nand_data *this,
605 void *destination, unsigned length,
606 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
607 void **use_virt, dma_addr_t *use_phys)
609 struct device *dev = this->dev;
611 if (virt_addr_valid(destination)) {
612 dma_addr_t dest_phys;
614 dest_phys = dma_map_single(dev, destination,
615 length, DMA_FROM_DEVICE);
616 if (dma_mapping_error(dev, dest_phys)) {
617 if (alt_size < length) {
618 pr_err("Alternate buffer is too small\n");
619 return -ENOMEM;
621 goto map_failed;
623 *use_virt = destination;
624 *use_phys = dest_phys;
625 this->direct_dma_map_ok = true;
626 return 0;
629 map_failed:
630 *use_virt = alt_virt;
631 *use_phys = alt_phys;
632 this->direct_dma_map_ok = false;
633 return 0;
636 static inline void read_page_end(struct gpmi_nand_data *this,
637 void *destination, unsigned length,
638 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
639 void *used_virt, dma_addr_t used_phys)
641 if (this->direct_dma_map_ok)
642 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
645 static inline void read_page_swap_end(struct gpmi_nand_data *this,
646 void *destination, unsigned length,
647 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
648 void *used_virt, dma_addr_t used_phys)
650 if (!this->direct_dma_map_ok)
651 memcpy(destination, alt_virt, length);
654 static int send_page_prepare(struct gpmi_nand_data *this,
655 const void *source, unsigned length,
656 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
657 const void **use_virt, dma_addr_t *use_phys)
659 struct device *dev = this->dev;
661 if (virt_addr_valid(source)) {
662 dma_addr_t source_phys;
664 source_phys = dma_map_single(dev, (void *)source, length,
665 DMA_TO_DEVICE);
666 if (dma_mapping_error(dev, source_phys)) {
667 if (alt_size < length) {
668 pr_err("Alternate buffer is too small\n");
669 return -ENOMEM;
671 goto map_failed;
673 *use_virt = source;
674 *use_phys = source_phys;
675 return 0;
677 map_failed:
679 * Copy the content of the source buffer into the alternate
680 * buffer and set up the return values accordingly.
682 memcpy(alt_virt, source, length);
684 *use_virt = alt_virt;
685 *use_phys = alt_phys;
686 return 0;
689 static void send_page_end(struct gpmi_nand_data *this,
690 const void *source, unsigned length,
691 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
692 const void *used_virt, dma_addr_t used_phys)
694 struct device *dev = this->dev;
695 if (used_virt == source)
696 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
699 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
701 struct device *dev = this->dev;
703 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
704 dma_free_coherent(dev, this->page_buffer_size,
705 this->page_buffer_virt,
706 this->page_buffer_phys);
707 kfree(this->cmd_buffer);
708 kfree(this->data_buffer_dma);
710 this->cmd_buffer = NULL;
711 this->data_buffer_dma = NULL;
712 this->page_buffer_virt = NULL;
713 this->page_buffer_size = 0;
716 /* Allocate the DMA buffers */
717 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
719 struct bch_geometry *geo = &this->bch_geometry;
720 struct device *dev = this->dev;
722 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
723 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA);
724 if (this->cmd_buffer == NULL)
725 goto error_alloc;
727 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
728 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA);
729 if (this->data_buffer_dma == NULL)
730 goto error_alloc;
733 * [3] Allocate the page buffer.
735 * Both the payload buffer and the auxiliary buffer must appear on
736 * 32-bit boundaries. We presume the size of the payload buffer is a
737 * power of two and is much larger than four, which guarantees the
738 * auxiliary buffer will appear on a 32-bit boundary.
740 this->page_buffer_size = geo->payload_size_in_bytes +
741 geo->auxiliary_size_in_bytes;
743 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
744 &this->page_buffer_phys, GFP_DMA);
745 if (!this->page_buffer_virt)
746 goto error_alloc;
749 /* Slice up the page buffer. */
750 this->payload_virt = this->page_buffer_virt;
751 this->payload_phys = this->page_buffer_phys;
752 this->auxiliary_virt = this->payload_virt + geo->payload_size_in_bytes;
753 this->auxiliary_phys = this->payload_phys + geo->payload_size_in_bytes;
754 return 0;
756 error_alloc:
757 gpmi_free_dma_buffer(this);
758 pr_err("allocate DMA buffer ret!!\n");
759 return -ENOMEM;
762 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
764 struct nand_chip *chip = mtd->priv;
765 struct gpmi_nand_data *this = chip->priv;
766 int ret;
769 * Every operation begins with a command byte and a series of zero or
770 * more address bytes. These are distinguished by either the Address
771 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
772 * asserted. When MTD is ready to execute the command, it will deassert
773 * both latch enables.
775 * Rather than run a separate DMA operation for every single byte, we
776 * queue them up and run a single DMA operation for the entire series
777 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
779 if ((ctrl & (NAND_ALE | NAND_CLE))) {
780 if (data != NAND_CMD_NONE)
781 this->cmd_buffer[this->command_length++] = data;
782 return;
785 if (!this->command_length)
786 return;
788 ret = gpmi_send_command(this);
789 if (ret)
790 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
792 this->command_length = 0;
795 static int gpmi_dev_ready(struct mtd_info *mtd)
797 struct nand_chip *chip = mtd->priv;
798 struct gpmi_nand_data *this = chip->priv;
800 return gpmi_is_ready(this, this->current_chip);
803 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
805 struct nand_chip *chip = mtd->priv;
806 struct gpmi_nand_data *this = chip->priv;
808 if ((this->current_chip < 0) && (chipnr >= 0))
809 gpmi_begin(this);
810 else if ((this->current_chip >= 0) && (chipnr < 0))
811 gpmi_end(this);
813 this->current_chip = chipnr;
816 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
818 struct nand_chip *chip = mtd->priv;
819 struct gpmi_nand_data *this = chip->priv;
821 logio(GPMI_DEBUG_READ);
822 this->upper_buf = buf;
823 this->upper_len = len;
825 gpmi_read_data(this);
828 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
830 struct nand_chip *chip = mtd->priv;
831 struct gpmi_nand_data *this = chip->priv;
833 logio(GPMI_DEBUG_WRITE);
834 this->upper_buf = (uint8_t *)buf;
835 this->upper_len = len;
837 gpmi_send_data(this);
840 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
842 struct nand_chip *chip = mtd->priv;
843 struct gpmi_nand_data *this = chip->priv;
844 uint8_t *buf = this->data_buffer_dma;
846 gpmi_read_buf(mtd, buf, 1);
847 return buf[0];
851 * Handles block mark swapping.
852 * It can be called in swapping the block mark, or swapping it back,
853 * because the the operations are the same.
855 static void block_mark_swapping(struct gpmi_nand_data *this,
856 void *payload, void *auxiliary)
858 struct bch_geometry *nfc_geo = &this->bch_geometry;
859 unsigned char *p;
860 unsigned char *a;
861 unsigned int bit;
862 unsigned char mask;
863 unsigned char from_data;
864 unsigned char from_oob;
866 if (!this->swap_block_mark)
867 return;
870 * If control arrives here, we're swapping. Make some convenience
871 * variables.
873 bit = nfc_geo->block_mark_bit_offset;
874 p = payload + nfc_geo->block_mark_byte_offset;
875 a = auxiliary;
878 * Get the byte from the data area that overlays the block mark. Since
879 * the ECC engine applies its own view to the bits in the page, the
880 * physical block mark won't (in general) appear on a byte boundary in
881 * the data.
883 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
885 /* Get the byte from the OOB. */
886 from_oob = a[0];
888 /* Swap them. */
889 a[0] = from_data;
891 mask = (0x1 << bit) - 1;
892 p[0] = (p[0] & mask) | (from_oob << bit);
894 mask = ~0 << bit;
895 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
898 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
899 uint8_t *buf, int page)
901 struct gpmi_nand_data *this = chip->priv;
902 struct bch_geometry *nfc_geo = &this->bch_geometry;
903 void *payload_virt;
904 dma_addr_t payload_phys;
905 void *auxiliary_virt;
906 dma_addr_t auxiliary_phys;
907 unsigned int i;
908 unsigned char *status;
909 unsigned int failed;
910 unsigned int corrected;
911 int ret;
913 logio(GPMI_DEBUG_ECC_READ);
914 ret = read_page_prepare(this, buf, mtd->writesize,
915 this->payload_virt, this->payload_phys,
916 nfc_geo->payload_size_in_bytes,
917 &payload_virt, &payload_phys);
918 if (ret) {
919 pr_err("Inadequate DMA buffer\n");
920 ret = -ENOMEM;
921 return ret;
923 auxiliary_virt = this->auxiliary_virt;
924 auxiliary_phys = this->auxiliary_phys;
926 /* go! */
927 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
928 read_page_end(this, buf, mtd->writesize,
929 this->payload_virt, this->payload_phys,
930 nfc_geo->payload_size_in_bytes,
931 payload_virt, payload_phys);
932 if (ret) {
933 pr_err("Error in ECC-based read: %d\n", ret);
934 goto exit_nfc;
937 /* handle the block mark swapping */
938 block_mark_swapping(this, payload_virt, auxiliary_virt);
940 /* Loop over status bytes, accumulating ECC status. */
941 failed = 0;
942 corrected = 0;
943 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
945 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
946 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
947 continue;
949 if (*status == STATUS_UNCORRECTABLE) {
950 failed++;
951 continue;
953 corrected += *status;
957 * Propagate ECC status to the owning MTD only when failed or
958 * corrected times nearly reaches our ECC correction threshold.
960 if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
961 mtd->ecc_stats.failed += failed;
962 mtd->ecc_stats.corrected += corrected;
966 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob() for
967 * details about our policy for delivering the OOB.
969 * We fill the caller's buffer with set bits, and then copy the block
970 * mark to th caller's buffer. Note that, if block mark swapping was
971 * necessary, it has already been done, so we can rely on the first
972 * byte of the auxiliary buffer to contain the block mark.
974 memset(chip->oob_poi, ~0, mtd->oobsize);
975 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
977 read_page_swap_end(this, buf, mtd->writesize,
978 this->payload_virt, this->payload_phys,
979 nfc_geo->payload_size_in_bytes,
980 payload_virt, payload_phys);
981 exit_nfc:
982 return ret;
985 static void gpmi_ecc_write_page(struct mtd_info *mtd,
986 struct nand_chip *chip, const uint8_t *buf)
988 struct gpmi_nand_data *this = chip->priv;
989 struct bch_geometry *nfc_geo = &this->bch_geometry;
990 const void *payload_virt;
991 dma_addr_t payload_phys;
992 const void *auxiliary_virt;
993 dma_addr_t auxiliary_phys;
994 int ret;
996 logio(GPMI_DEBUG_ECC_WRITE);
997 if (this->swap_block_mark) {
999 * If control arrives here, we're doing block mark swapping.
1000 * Since we can't modify the caller's buffers, we must copy them
1001 * into our own.
1003 memcpy(this->payload_virt, buf, mtd->writesize);
1004 payload_virt = this->payload_virt;
1005 payload_phys = this->payload_phys;
1007 memcpy(this->auxiliary_virt, chip->oob_poi,
1008 nfc_geo->auxiliary_size_in_bytes);
1009 auxiliary_virt = this->auxiliary_virt;
1010 auxiliary_phys = this->auxiliary_phys;
1012 /* Handle block mark swapping. */
1013 block_mark_swapping(this,
1014 (void *) payload_virt, (void *) auxiliary_virt);
1015 } else {
1017 * If control arrives here, we're not doing block mark swapping,
1018 * so we can to try and use the caller's buffers.
1020 ret = send_page_prepare(this,
1021 buf, mtd->writesize,
1022 this->payload_virt, this->payload_phys,
1023 nfc_geo->payload_size_in_bytes,
1024 &payload_virt, &payload_phys);
1025 if (ret) {
1026 pr_err("Inadequate payload DMA buffer\n");
1027 return;
1030 ret = send_page_prepare(this,
1031 chip->oob_poi, mtd->oobsize,
1032 this->auxiliary_virt, this->auxiliary_phys,
1033 nfc_geo->auxiliary_size_in_bytes,
1034 &auxiliary_virt, &auxiliary_phys);
1035 if (ret) {
1036 pr_err("Inadequate auxiliary DMA buffer\n");
1037 goto exit_auxiliary;
1041 /* Ask the NFC. */
1042 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1043 if (ret)
1044 pr_err("Error in ECC-based write: %d\n", ret);
1046 if (!this->swap_block_mark) {
1047 send_page_end(this, chip->oob_poi, mtd->oobsize,
1048 this->auxiliary_virt, this->auxiliary_phys,
1049 nfc_geo->auxiliary_size_in_bytes,
1050 auxiliary_virt, auxiliary_phys);
1051 exit_auxiliary:
1052 send_page_end(this, buf, mtd->writesize,
1053 this->payload_virt, this->payload_phys,
1054 nfc_geo->payload_size_in_bytes,
1055 payload_virt, payload_phys);
1060 * There are several places in this driver where we have to handle the OOB and
1061 * block marks. This is the function where things are the most complicated, so
1062 * this is where we try to explain it all. All the other places refer back to
1063 * here.
1065 * These are the rules, in order of decreasing importance:
1067 * 1) Nothing the caller does can be allowed to imperil the block mark.
1069 * 2) In read operations, the first byte of the OOB we return must reflect the
1070 * true state of the block mark, no matter where that block mark appears in
1071 * the physical page.
1073 * 3) ECC-based read operations return an OOB full of set bits (since we never
1074 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1075 * return).
1077 * 4) "Raw" read operations return a direct view of the physical bytes in the
1078 * page, using the conventional definition of which bytes are data and which
1079 * are OOB. This gives the caller a way to see the actual, physical bytes
1080 * in the page, without the distortions applied by our ECC engine.
1083 * What we do for this specific read operation depends on two questions:
1085 * 1) Are we doing a "raw" read, or an ECC-based read?
1087 * 2) Are we using block mark swapping or transcription?
1089 * There are four cases, illustrated by the following Karnaugh map:
1091 * | Raw | ECC-based |
1092 * -------------+-------------------------+-------------------------+
1093 * | Read the conventional | |
1094 * | OOB at the end of the | |
1095 * Swapping | page and return it. It | |
1096 * | contains exactly what | |
1097 * | we want. | Read the block mark and |
1098 * -------------+-------------------------+ return it in a buffer |
1099 * | Read the conventional | full of set bits. |
1100 * | OOB at the end of the | |
1101 * | page and also the block | |
1102 * Transcribing | mark in the metadata. | |
1103 * | Copy the block mark | |
1104 * | into the first byte of | |
1105 * | the OOB. | |
1106 * -------------+-------------------------+-------------------------+
1108 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1109 * giving an accurate view of the actual, physical bytes in the page (we're
1110 * overwriting the block mark). That's OK because it's more important to follow
1111 * rule #2.
1113 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1114 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1115 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1116 * ECC-based or raw view of the page is implicit in which function it calls
1117 * (there is a similar pair of ECC-based/raw functions for writing).
1119 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1120 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1121 * caller wants an ECC-based or raw view of the page is not propagated down to
1122 * this driver.
1124 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1125 int page, int sndcmd)
1127 struct gpmi_nand_data *this = chip->priv;
1129 logio(GPMI_DEBUG_READ_OOB);
1130 /* clear the OOB buffer */
1131 memset(chip->oob_poi, ~0, mtd->oobsize);
1133 /* Read out the conventional OOB. */
1134 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1135 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1138 * Now, we want to make sure the block mark is correct. In the
1139 * Swapping/Raw case, we already have it. Otherwise, we need to
1140 * explicitly read it.
1142 if (!this->swap_block_mark) {
1143 /* Read the block mark into the first byte of the OOB buffer. */
1144 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1145 chip->oob_poi[0] = chip->read_byte(mtd);
1149 * Return true, indicating that the next call to this function must send
1150 * a command.
1152 return true;
1155 static int
1156 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1159 * The BCH will use all the (page + oob).
1160 * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
1161 * But it can not stop some ioctls such MEMWRITEOOB which uses
1162 * MTD_OOB_PLACE. So We have to implement this function to prohibit
1163 * these ioctls too.
1165 return -EPERM;
1168 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1170 struct nand_chip *chip = mtd->priv;
1171 struct gpmi_nand_data *this = chip->priv;
1172 int block, ret = 0;
1173 uint8_t *block_mark;
1174 int column, page, status, chipnr;
1176 /* Get block number */
1177 block = (int)(ofs >> chip->bbt_erase_shift);
1178 if (chip->bbt)
1179 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1181 /* Do we have a flash based bad block table ? */
1182 if (chip->options & NAND_BBT_USE_FLASH)
1183 ret = nand_update_bbt(mtd, ofs);
1184 else {
1185 chipnr = (int)(ofs >> chip->chip_shift);
1186 chip->select_chip(mtd, chipnr);
1188 column = this->swap_block_mark ? mtd->writesize : 0;
1190 /* Write the block mark. */
1191 block_mark = this->data_buffer_dma;
1192 block_mark[0] = 0; /* bad block marker */
1194 /* Shift to get page */
1195 page = (int)(ofs >> chip->page_shift);
1197 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1198 chip->write_buf(mtd, block_mark, 1);
1199 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1201 status = chip->waitfunc(mtd, chip);
1202 if (status & NAND_STATUS_FAIL)
1203 ret = -EIO;
1205 chip->select_chip(mtd, -1);
1207 if (!ret)
1208 mtd->ecc_stats.badblocks++;
1210 return ret;
1213 static int __devinit nand_boot_set_geometry(struct gpmi_nand_data *this)
1215 struct boot_rom_geometry *geometry = &this->rom_geometry;
1218 * Set the boot block stride size.
1220 * In principle, we should be reading this from the OTP bits, since
1221 * that's where the ROM is going to get it. In fact, we don't have any
1222 * way to read the OTP bits, so we go with the default and hope for the
1223 * best.
1225 geometry->stride_size_in_pages = 64;
1228 * Set the search area stride exponent.
1230 * In principle, we should be reading this from the OTP bits, since
1231 * that's where the ROM is going to get it. In fact, we don't have any
1232 * way to read the OTP bits, so we go with the default and hope for the
1233 * best.
1235 geometry->search_area_stride_exponent = 2;
1236 return 0;
1239 static const char *fingerprint = "STMP";
1240 static int __devinit mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1242 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1243 struct device *dev = this->dev;
1244 struct mtd_info *mtd = &this->mtd;
1245 struct nand_chip *chip = &this->nand;
1246 unsigned int search_area_size_in_strides;
1247 unsigned int stride;
1248 unsigned int page;
1249 loff_t byte;
1250 uint8_t *buffer = chip->buffers->databuf;
1251 int saved_chip_number;
1252 int found_an_ncb_fingerprint = false;
1254 /* Compute the number of strides in a search area. */
1255 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1257 saved_chip_number = this->current_chip;
1258 chip->select_chip(mtd, 0);
1261 * Loop through the first search area, looking for the NCB fingerprint.
1263 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1265 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1266 /* Compute the page and byte addresses. */
1267 page = stride * rom_geo->stride_size_in_pages;
1268 byte = page * mtd->writesize;
1270 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1273 * Read the NCB fingerprint. The fingerprint is four bytes long
1274 * and starts in the 12th byte of the page.
1276 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1277 chip->read_buf(mtd, buffer, strlen(fingerprint));
1279 /* Look for the fingerprint. */
1280 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1281 found_an_ncb_fingerprint = true;
1282 break;
1287 chip->select_chip(mtd, saved_chip_number);
1289 if (found_an_ncb_fingerprint)
1290 dev_dbg(dev, "\tFound a fingerprint\n");
1291 else
1292 dev_dbg(dev, "\tNo fingerprint found\n");
1293 return found_an_ncb_fingerprint;
1296 /* Writes a transcription stamp. */
1297 static int __devinit mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1299 struct device *dev = this->dev;
1300 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1301 struct mtd_info *mtd = &this->mtd;
1302 struct nand_chip *chip = &this->nand;
1303 unsigned int block_size_in_pages;
1304 unsigned int search_area_size_in_strides;
1305 unsigned int search_area_size_in_pages;
1306 unsigned int search_area_size_in_blocks;
1307 unsigned int block;
1308 unsigned int stride;
1309 unsigned int page;
1310 loff_t byte;
1311 uint8_t *buffer = chip->buffers->databuf;
1312 int saved_chip_number;
1313 int status;
1315 /* Compute the search area geometry. */
1316 block_size_in_pages = mtd->erasesize / mtd->writesize;
1317 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1318 search_area_size_in_pages = search_area_size_in_strides *
1319 rom_geo->stride_size_in_pages;
1320 search_area_size_in_blocks =
1321 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1322 block_size_in_pages;
1324 dev_dbg(dev, "Search Area Geometry :\n");
1325 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1326 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1327 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1329 /* Select chip 0. */
1330 saved_chip_number = this->current_chip;
1331 chip->select_chip(mtd, 0);
1333 /* Loop over blocks in the first search area, erasing them. */
1334 dev_dbg(dev, "Erasing the search area...\n");
1336 for (block = 0; block < search_area_size_in_blocks; block++) {
1337 /* Compute the page address. */
1338 page = block * block_size_in_pages;
1340 /* Erase this block. */
1341 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1342 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1343 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1345 /* Wait for the erase to finish. */
1346 status = chip->waitfunc(mtd, chip);
1347 if (status & NAND_STATUS_FAIL)
1348 dev_err(dev, "[%s] Erase failed.\n", __func__);
1351 /* Write the NCB fingerprint into the page buffer. */
1352 memset(buffer, ~0, mtd->writesize);
1353 memset(chip->oob_poi, ~0, mtd->oobsize);
1354 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1356 /* Loop through the first search area, writing NCB fingerprints. */
1357 dev_dbg(dev, "Writing NCB fingerprints...\n");
1358 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1359 /* Compute the page and byte addresses. */
1360 page = stride * rom_geo->stride_size_in_pages;
1361 byte = page * mtd->writesize;
1363 /* Write the first page of the current stride. */
1364 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1365 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1366 chip->ecc.write_page_raw(mtd, chip, buffer);
1367 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1369 /* Wait for the write to finish. */
1370 status = chip->waitfunc(mtd, chip);
1371 if (status & NAND_STATUS_FAIL)
1372 dev_err(dev, "[%s] Write failed.\n", __func__);
1375 /* Deselect chip 0. */
1376 chip->select_chip(mtd, saved_chip_number);
1377 return 0;
1380 static int __devinit mx23_boot_init(struct gpmi_nand_data *this)
1382 struct device *dev = this->dev;
1383 struct nand_chip *chip = &this->nand;
1384 struct mtd_info *mtd = &this->mtd;
1385 unsigned int block_count;
1386 unsigned int block;
1387 int chipnr;
1388 int page;
1389 loff_t byte;
1390 uint8_t block_mark;
1391 int ret = 0;
1394 * If control arrives here, we can't use block mark swapping, which
1395 * means we're forced to use transcription. First, scan for the
1396 * transcription stamp. If we find it, then we don't have to do
1397 * anything -- the block marks are already transcribed.
1399 if (mx23_check_transcription_stamp(this))
1400 return 0;
1403 * If control arrives here, we couldn't find a transcription stamp, so
1404 * so we presume the block marks are in the conventional location.
1406 dev_dbg(dev, "Transcribing bad block marks...\n");
1408 /* Compute the number of blocks in the entire medium. */
1409 block_count = chip->chipsize >> chip->phys_erase_shift;
1412 * Loop over all the blocks in the medium, transcribing block marks as
1413 * we go.
1415 for (block = 0; block < block_count; block++) {
1417 * Compute the chip, page and byte addresses for this block's
1418 * conventional mark.
1420 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1421 page = block << (chip->phys_erase_shift - chip->page_shift);
1422 byte = block << chip->phys_erase_shift;
1424 /* Send the command to read the conventional block mark. */
1425 chip->select_chip(mtd, chipnr);
1426 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1427 block_mark = chip->read_byte(mtd);
1428 chip->select_chip(mtd, -1);
1431 * Check if the block is marked bad. If so, we need to mark it
1432 * again, but this time the result will be a mark in the
1433 * location where we transcribe block marks.
1435 if (block_mark != 0xff) {
1436 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1437 ret = chip->block_markbad(mtd, byte);
1438 if (ret)
1439 dev_err(dev, "Failed to mark block bad with "
1440 "ret %d\n", ret);
1444 /* Write the stamp that indicates we've transcribed the block marks. */
1445 mx23_write_transcription_stamp(this);
1446 return 0;
1449 static int __devinit nand_boot_init(struct gpmi_nand_data *this)
1451 nand_boot_set_geometry(this);
1453 /* This is ROM arch-specific initilization before the BBT scanning. */
1454 if (GPMI_IS_MX23(this))
1455 return mx23_boot_init(this);
1456 return 0;
1459 static int __devinit gpmi_set_geometry(struct gpmi_nand_data *this)
1461 int ret;
1463 /* Free the temporary DMA memory for reading ID. */
1464 gpmi_free_dma_buffer(this);
1466 /* Set up the NFC geometry which is used by BCH. */
1467 ret = bch_set_geometry(this);
1468 if (ret) {
1469 pr_err("set geometry ret : %d\n", ret);
1470 return ret;
1473 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1474 return gpmi_alloc_dma_buffer(this);
1477 static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1479 int ret;
1481 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1482 if (GPMI_IS_MX23(this))
1483 this->swap_block_mark = false;
1484 else
1485 this->swap_block_mark = true;
1487 /* Set up the medium geometry */
1488 ret = gpmi_set_geometry(this);
1489 if (ret)
1490 return ret;
1492 /* NAND boot init, depends on the gpmi_set_geometry(). */
1493 return nand_boot_init(this);
1496 static int gpmi_scan_bbt(struct mtd_info *mtd)
1498 struct nand_chip *chip = mtd->priv;
1499 struct gpmi_nand_data *this = chip->priv;
1500 int ret;
1502 /* Prepare for the BBT scan. */
1503 ret = gpmi_pre_bbt_scan(this);
1504 if (ret)
1505 return ret;
1507 /* use the default BBT implementation */
1508 return nand_default_bbt(mtd);
1511 void gpmi_nfc_exit(struct gpmi_nand_data *this)
1513 nand_release(&this->mtd);
1514 gpmi_free_dma_buffer(this);
1517 static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
1519 struct gpmi_nand_platform_data *pdata = this->pdata;
1520 struct mtd_info *mtd = &this->mtd;
1521 struct nand_chip *chip = &this->nand;
1522 int ret;
1524 /* init current chip */
1525 this->current_chip = -1;
1527 /* init the MTD data structures */
1528 mtd->priv = chip;
1529 mtd->name = "gpmi-nand";
1530 mtd->owner = THIS_MODULE;
1532 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1533 chip->priv = this;
1534 chip->select_chip = gpmi_select_chip;
1535 chip->cmd_ctrl = gpmi_cmd_ctrl;
1536 chip->dev_ready = gpmi_dev_ready;
1537 chip->read_byte = gpmi_read_byte;
1538 chip->read_buf = gpmi_read_buf;
1539 chip->write_buf = gpmi_write_buf;
1540 chip->ecc.read_page = gpmi_ecc_read_page;
1541 chip->ecc.write_page = gpmi_ecc_write_page;
1542 chip->ecc.read_oob = gpmi_ecc_read_oob;
1543 chip->ecc.write_oob = gpmi_ecc_write_oob;
1544 chip->scan_bbt = gpmi_scan_bbt;
1545 chip->badblock_pattern = &gpmi_bbt_descr;
1546 chip->block_markbad = gpmi_block_markbad;
1547 chip->options |= NAND_NO_SUBPAGE_WRITE;
1548 chip->ecc.mode = NAND_ECC_HW;
1549 chip->ecc.size = 1;
1550 chip->ecc.layout = &gpmi_hw_ecclayout;
1552 /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
1553 this->bch_geometry.payload_size_in_bytes = 1024;
1554 this->bch_geometry.auxiliary_size_in_bytes = 128;
1555 ret = gpmi_alloc_dma_buffer(this);
1556 if (ret)
1557 goto err_out;
1559 ret = nand_scan(mtd, pdata->max_chip_count);
1560 if (ret) {
1561 pr_err("Chip scan failed\n");
1562 goto err_out;
1565 ret = mtd_device_parse_register(mtd, NULL, NULL,
1566 pdata->partitions, pdata->partition_count);
1567 if (ret)
1568 goto err_out;
1569 return 0;
1571 err_out:
1572 gpmi_nfc_exit(this);
1573 return ret;
1576 static int __devinit gpmi_nand_probe(struct platform_device *pdev)
1578 struct gpmi_nand_platform_data *pdata = pdev->dev.platform_data;
1579 struct gpmi_nand_data *this;
1580 int ret;
1582 this = kzalloc(sizeof(*this), GFP_KERNEL);
1583 if (!this) {
1584 pr_err("Failed to allocate per-device memory\n");
1585 return -ENOMEM;
1588 platform_set_drvdata(pdev, this);
1589 this->pdev = pdev;
1590 this->dev = &pdev->dev;
1591 this->pdata = pdata;
1593 if (pdata->platform_init) {
1594 ret = pdata->platform_init();
1595 if (ret)
1596 goto platform_init_error;
1599 ret = acquire_resources(this);
1600 if (ret)
1601 goto exit_acquire_resources;
1603 ret = init_hardware(this);
1604 if (ret)
1605 goto exit_nfc_init;
1607 ret = gpmi_nfc_init(this);
1608 if (ret)
1609 goto exit_nfc_init;
1611 return 0;
1613 exit_nfc_init:
1614 release_resources(this);
1615 platform_init_error:
1616 exit_acquire_resources:
1617 platform_set_drvdata(pdev, NULL);
1618 kfree(this);
1619 return ret;
1622 static int __exit gpmi_nand_remove(struct platform_device *pdev)
1624 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1626 gpmi_nfc_exit(this);
1627 release_resources(this);
1628 platform_set_drvdata(pdev, NULL);
1629 kfree(this);
1630 return 0;
1633 static const struct platform_device_id gpmi_ids[] = {
1635 .name = "imx23-gpmi-nand",
1636 .driver_data = IS_MX23,
1637 }, {
1638 .name = "imx28-gpmi-nand",
1639 .driver_data = IS_MX28,
1640 }, {},
1643 static struct platform_driver gpmi_nand_driver = {
1644 .driver = {
1645 .name = "gpmi-nand",
1647 .probe = gpmi_nand_probe,
1648 .remove = __exit_p(gpmi_nand_remove),
1649 .id_table = gpmi_ids,
1652 static int __init gpmi_nand_init(void)
1654 int err;
1656 err = platform_driver_register(&gpmi_nand_driver);
1657 if (err == 0)
1658 printk(KERN_INFO "GPMI NAND driver registered. (IMX)\n");
1659 else
1660 pr_err("i.MX GPMI NAND driver registration failed\n");
1661 return err;
1664 static void __exit gpmi_nand_exit(void)
1666 platform_driver_unregister(&gpmi_nand_driver);
1669 module_init(gpmi_nand_init);
1670 module_exit(gpmi_nand_exit);
1672 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1673 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1674 MODULE_LICENSE("GPL");