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
= {
35 .pattern
= scan_ff_pattern
38 /* We will use all the (page + OOB). */
39 static struct nand_ecclayout gpmi_hw_ecclayout
= {
42 .oobfree
= { {.offset
= 0, .length
= 0} }
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) \
55 if (gpmi_debug & level) \
56 pr_info("%s : %d\n", __func__, __LINE__); \
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
;
68 complete(&this->bch_done
);
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
;
78 switch (mtd
->writesize
) {
83 switch (mtd
->oobsize
) {
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
);
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
)
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
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
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
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
);
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;
240 struct dma_chan
*get_dma_chan(struct gpmi_nand_data
*this)
242 int chipnr
= this->current_chip
;
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
;
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
);
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
,
267 ret
= dma_map_sg(this->dev
, sgl
, 1, dr
);
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
;
282 switch (this->dma_type
) {
283 case DMA_FOR_COMMAND
:
284 dma_unmap_sg(this->dev
, &this->cmd_sgl
, 1, DMA_TO_DEVICE
);
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
,
294 case DMA_FOR_WRITE_DATA
:
295 dma_unmap_sg(this->dev
, &this->data_sgl
, 1, DMA_TO_DEVICE
);
298 case DMA_FOR_READ_ECC_PAGE
:
299 case DMA_FOR_WRITE_ECC_PAGE
:
300 /* We have to wait the BCH interrupt to finish. */
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
;
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;
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);
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
)
343 /* Prepare to receive an interrupt from the BCH block. */
344 init_completion(&this->bch_done
);
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;
354 pr_err("BCH timeout!\n");
355 if (gpmi_debug
& GPMI_DEBUG_VERBOSE
)
356 gpmi_dump_info(this);
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
;
369 r
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, res_name
);
371 pr_err("Can't get resource for %s\n", res_name
);
375 p
= ioremap(r
->start
, resource_size(r
));
377 pr_err("Can't remap %s\n", res_name
);
381 if (res_name
== GPMI_NAND_GPMI_REGS_ADDR_RES_NAME
)
383 else if (res_name
== GPMI_NAND_BCH_REGS_ADDR_RES_NAME
)
391 static void release_register_block(struct gpmi_nand_data
*this)
393 struct resources
*res
= &this->resources
;
395 iounmap(res
->gpmi_regs
);
397 iounmap(res
->bch_regs
);
398 res
->gpmi_regs
= NULL
;
399 res
->bch_regs
= NULL
;
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
;
411 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, res_name
);
413 pr_err("Can't get resource for %s\n", res_name
);
417 BUG_ON(r
->start
!= r
->end
);
418 err
= request_irq(r
->start
, irq_h
, 0, res_name
, this);
420 pr_err("Can't own %s\n", res_name
);
424 res
->bch_low_interrupt
= r
->start
;
425 res
->bch_high_interrupt
= r
->end
;
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
++)
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
))
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
;
460 static void release_dma_channels(struct gpmi_nand_data
*this)
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
;
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
);
483 pr_err("Can't get resource for DMA\n");
487 /* used in gpmi_dma_filter() */
490 for (i
= r
->start
; i
<= r
->end
; i
++) {
491 struct dma_chan
*dma_chan
;
494 if (i
- r
->start
>= pdata
->max_chip_count
)
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. */
504 this->dma_data
.chan_irq
= r_dma
->start
;
506 this->dma_data
.chan_irq
= NO_IRQ
;
508 this->dma_data
.chan_irq
= r_dma
->start
+ (i
- r
->start
);
510 dma_chan
= dma_request_channel(mask
, gpmi_dma_filter
, this);
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
;
523 pr_err("Can't acquire DMA channel %u\n", i
);
524 release_dma_channels(this);
528 static int __devinit
acquire_resources(struct gpmi_nand_data
*this)
530 struct resources
*res
= &this->resources
;
533 ret
= acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME
);
537 ret
= acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME
);
541 ret
= acquire_bch_irq(this, bch_irq
);
545 ret
= acquire_dma_channels(this);
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");
558 release_dma_channels(this);
560 release_bch_irq(this);
562 release_register_block(this);
566 static void release_resources(struct gpmi_nand_data
*this)
568 struct resources
*r
= &this->resources
;
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)
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,
595 /* Initialize the hardwares. */
596 ret
= gpmi_init(this);
600 this->timing
= safe_timing
;
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");
623 *use_virt
= destination
;
624 *use_phys
= dest_phys
;
625 this->direct_dma_map_ok
= true;
630 *use_virt
= alt_virt
;
631 *use_phys
= alt_phys
;
632 this->direct_dma_map_ok
= false;
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
,
666 if (dma_mapping_error(dev
, source_phys
)) {
667 if (alt_size
< length
) {
668 pr_err("Alternate buffer is too small\n");
674 *use_phys
= source_phys
;
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
;
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
)
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
)
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
)
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
;
757 gpmi_free_dma_buffer(this);
758 pr_err("allocate DMA buffer ret!!\n");
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
;
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
;
785 if (!this->command_length
)
788 ret
= gpmi_send_command(this);
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))
810 else if ((this->current_chip
>= 0) && (chipnr
< 0))
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);
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
;
863 unsigned char from_data
;
864 unsigned char from_oob
;
866 if (!this->swap_block_mark
)
870 * If control arrives here, we're swapping. Make some convenience
873 bit
= nfc_geo
->block_mark_bit_offset
;
874 p
= payload
+ nfc_geo
->block_mark_byte_offset
;
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
883 from_data
= (p
[0] >> bit
) | (p
[1] << (8 - bit
));
885 /* Get the byte from the OOB. */
891 mask
= (0x1 << bit
) - 1;
892 p
[0] = (p
[0] & mask
) | (from_oob
<< 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
;
904 dma_addr_t payload_phys
;
905 void *auxiliary_virt
;
906 dma_addr_t auxiliary_phys
;
908 unsigned char *status
;
910 unsigned int corrected
;
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
);
919 pr_err("Inadequate DMA buffer\n");
923 auxiliary_virt
= this->auxiliary_virt
;
924 auxiliary_phys
= this->auxiliary_phys
;
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
);
933 pr_err("Error in ECC-based read: %d\n", ret
);
937 /* handle the block mark swapping */
938 block_mark_swapping(this, payload_virt
, auxiliary_virt
);
940 /* Loop over status bytes, accumulating ECC status. */
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
))
949 if (*status
== STATUS_UNCORRECTABLE
) {
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
);
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
;
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
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
);
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
);
1026 pr_err("Inadequate payload DMA buffer\n");
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
);
1036 pr_err("Inadequate auxiliary DMA buffer\n");
1037 goto exit_auxiliary
;
1042 ret
= gpmi_send_page(this, payload_phys
, auxiliary_phys
);
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
);
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
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
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 | |
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
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
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
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
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
;
1173 uint8_t *block_mark
;
1174 int column
, page
, status
, chipnr
;
1176 /* Get block number */
1177 block
= (int)(ofs
>> chip
->bbt_erase_shift
);
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
);
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
)
1205 chip
->select_chip(mtd
, -1);
1208 mtd
->ecc_stats
.badblocks
++;
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
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
1235 geometry
->search_area_stride_exponent
= 2;
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
;
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;
1287 chip
->select_chip(mtd
, saved_chip_number
);
1289 if (found_an_ncb_fingerprint
)
1290 dev_dbg(dev
, "\tFound a fingerprint\n");
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
;
1308 unsigned int stride
;
1311 uint8_t *buffer
= chip
->buffers
->databuf
;
1312 int saved_chip_number
;
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
);
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
;
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))
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
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
);
1439 dev_err(dev
, "Failed to mark block bad with "
1444 /* Write the stamp that indicates we've transcribed the block marks. */
1445 mx23_write_transcription_stamp(this);
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);
1459 static int __devinit
gpmi_set_geometry(struct gpmi_nand_data
*this)
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);
1469 pr_err("set geometry ret : %d\n", 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)
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;
1485 this->swap_block_mark
= true;
1487 /* Set up the medium geometry */
1488 ret
= gpmi_set_geometry(this);
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
;
1502 /* Prepare for the BBT scan. */
1503 ret
= gpmi_pre_bbt_scan(this);
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
;
1524 /* init current chip */
1525 this->current_chip
= -1;
1527 /* init the MTD data structures */
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. */
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
;
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);
1559 ret
= nand_scan(mtd
, pdata
->max_chip_count
);
1561 pr_err("Chip scan failed\n");
1565 ret
= mtd_device_parse_register(mtd
, NULL
, NULL
,
1566 pdata
->partitions
, pdata
->partition_count
);
1572 gpmi_nfc_exit(this);
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;
1582 this = kzalloc(sizeof(*this), GFP_KERNEL
);
1584 pr_err("Failed to allocate per-device memory\n");
1588 platform_set_drvdata(pdev
, this);
1590 this->dev
= &pdev
->dev
;
1591 this->pdata
= pdata
;
1593 if (pdata
->platform_init
) {
1594 ret
= pdata
->platform_init();
1596 goto platform_init_error
;
1599 ret
= acquire_resources(this);
1601 goto exit_acquire_resources
;
1603 ret
= init_hardware(this);
1607 ret
= gpmi_nfc_init(this);
1614 release_resources(this);
1615 platform_init_error
:
1616 exit_acquire_resources
:
1617 platform_set_drvdata(pdev
, NULL
);
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
);
1633 static const struct platform_device_id gpmi_ids
[] = {
1635 .name
= "imx23-gpmi-nand",
1636 .driver_data
= IS_MX23
,
1638 .name
= "imx28-gpmi-nand",
1639 .driver_data
= IS_MX28
,
1643 static struct platform_driver gpmi_nand_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)
1656 err
= platform_driver_register(&gpmi_nand_driver
);
1658 printk(KERN_INFO
"GPMI NAND driver registered. (IMX)\n");
1660 pr_err("i.MX GPMI NAND driver registration failed\n");
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");