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.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/clk.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/mtd/partitions.h>
30 #include <linux/of_device.h>
31 #include <linux/of_mtd.h>
32 #include "gpmi-nand.h"
34 /* Resource names for the GPMI NAND driver. */
35 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
36 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
37 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
39 /* add our owner bbt descriptor */
40 static uint8_t scan_ff_pattern[] = { 0xff };
41 static struct nand_bbt_descr gpmi_bbt_descr = {
45 .pattern = scan_ff_pattern
49 * We may change the layout if we can get the ECC info from the datasheet,
50 * else we will use all the (page + OOB).
52 static struct nand_ecclayout gpmi_hw_ecclayout = {
55 .oobfree = { {.offset = 0, .length = 0} }
58 static irqreturn_t bch_irq(int irq, void *cookie)
60 struct gpmi_nand_data *this = cookie;
63 complete(&this->bch_done);
68 * Calculate the ECC strength by hand:
69 * E : The ECC strength.
70 * G : the length of Galois Field.
71 * N : The chunk count of per page.
72 * O : the oobsize of the NAND chip.
73 * M : the metasize of per page.
77 * ------------ <= (O - M)
85 static inline int get_ecc_strength(struct gpmi_nand_data *this)
87 struct bch_geometry *geo = &this->bch_geometry;
88 struct mtd_info *mtd = &this->mtd;
91 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
92 / (geo->gf_len * geo->ecc_chunk_count);
94 /* We need the minor even number. */
95 return round_down(ecc_strength, 2);
98 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
100 struct bch_geometry *geo = &this->bch_geometry;
102 /* Do the sanity check. */
103 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
104 /* The mx23/mx28 only support the GF13. */
105 if (geo->gf_len == 14)
108 if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
110 } else if (GPMI_IS_MX6Q(this)) {
111 if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
118 * If we can get the ECC information from the nand chip, we do not
119 * need to calculate them ourselves.
121 * We may have available oob space in this case.
123 static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
125 struct bch_geometry *geo = &this->bch_geometry;
126 struct mtd_info *mtd = &this->mtd;
127 struct nand_chip *chip = mtd->priv;
128 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
129 unsigned int block_mark_bit_offset;
131 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
134 switch (chip->ecc_step_ds) {
143 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
144 chip->ecc_strength_ds, chip->ecc_step_ds);
147 geo->ecc_chunk_size = chip->ecc_step_ds;
148 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
149 if (!gpmi_check_ecc(this))
152 /* Keep the C >= O */
153 if (geo->ecc_chunk_size < mtd->oobsize) {
155 "unsupported nand chip. ecc size: %d, oob size : %d\n",
156 chip->ecc_step_ds, mtd->oobsize);
160 /* The default value, see comment in the legacy_set_geometry(). */
161 geo->metadata_size = 10;
163 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
166 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
169 * |<----------------------------------------------------->|
173 * |<-------------------------------------------->| D | | O' |
176 * +---+----------+-+----------+-+----------+-+----------+-+-----+
177 * | M | data |E| data |E| data |E| data |E| |
178 * +---+----------+-+----------+-+----------+-+----------+-+-----+
184 * P : the page size for BCH module.
185 * E : The ECC strength.
186 * G : the length of Galois Field.
187 * N : The chunk count of per page.
188 * M : the metasize of per page.
189 * C : the ecc chunk size, aka the "data" above.
190 * P': the nand chip's page size.
191 * O : the nand chip's oob size.
194 * The formula for P is :
197 * P = ------------ + P' + M
200 * The position of block mark moves forward in the ECC-based view
201 * of page, and the delta is:
204 * D = (---------------- + M)
207 * Please see the comment in legacy_set_geometry().
208 * With the condition C >= O , we still can get same result.
209 * So the bit position of the physical block mark within the ECC-based
210 * view of the page is :
213 geo->page_size = mtd->writesize + geo->metadata_size +
214 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
216 /* The available oob size we have. */
217 if (geo->page_size < mtd->writesize + mtd->oobsize) {
218 of->offset = geo->page_size - mtd->writesize;
219 of->length = mtd->oobsize - of->offset;
222 geo->payload_size = mtd->writesize;
224 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
225 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
226 + ALIGN(geo->ecc_chunk_count, 4);
228 if (!this->swap_block_mark)
232 block_mark_bit_offset = mtd->writesize * 8 -
233 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
234 + geo->metadata_size * 8);
236 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
237 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
241 static int legacy_set_geometry(struct gpmi_nand_data *this)
243 struct bch_geometry *geo = &this->bch_geometry;
244 struct mtd_info *mtd = &this->mtd;
245 unsigned int metadata_size;
246 unsigned int status_size;
247 unsigned int block_mark_bit_offset;
250 * The size of the metadata can be changed, though we set it to 10
251 * bytes now. But it can't be too large, because we have to save
252 * enough space for BCH.
254 geo->metadata_size = 10;
256 /* The default for the length of Galois Field. */
259 /* The default for chunk size. */
260 geo->ecc_chunk_size = 512;
261 while (geo->ecc_chunk_size < mtd->oobsize) {
262 geo->ecc_chunk_size *= 2; /* keep C >= O */
266 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
268 /* We use the same ECC strength for all chunks. */
269 geo->ecc_strength = get_ecc_strength(this);
270 if (!gpmi_check_ecc(this)) {
272 "We can not support this nand chip."
273 " Its required ecc strength(%d) is beyond our"
274 " capability(%d).\n", geo->ecc_strength,
275 (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
276 : MXS_ECC_STRENGTH_MAX));
280 geo->page_size = mtd->writesize + mtd->oobsize;
281 geo->payload_size = mtd->writesize;
284 * The auxiliary buffer contains the metadata and the ECC status. The
285 * metadata is padded to the nearest 32-bit boundary. The ECC status
286 * contains one byte for every ECC chunk, and is also padded to the
287 * nearest 32-bit boundary.
289 metadata_size = ALIGN(geo->metadata_size, 4);
290 status_size = ALIGN(geo->ecc_chunk_count, 4);
292 geo->auxiliary_size = metadata_size + status_size;
293 geo->auxiliary_status_offset = metadata_size;
295 if (!this->swap_block_mark)
299 * We need to compute the byte and bit offsets of
300 * the physical block mark within the ECC-based view of the page.
302 * NAND chip with 2K page shows below:
308 * +---+----------+-+----------+-+----------+-+----------+-+
309 * | M | data |E| data |E| data |E| data |E|
310 * +---+----------+-+----------+-+----------+-+----------+-+
312 * The position of block mark moves forward in the ECC-based view
313 * of page, and the delta is:
316 * D = (---------------- + M)
319 * With the formula to compute the ECC strength, and the condition
320 * : C >= O (C is the ecc chunk size)
322 * It's easy to deduce to the following result:
324 * E * G (O - M) C - M C - M
325 * ----------- <= ------- <= -------- < ---------
331 * D = (---------------- + M) < C
334 * The above inequality means the position of block mark
335 * within the ECC-based view of the page is still in the data chunk,
336 * and it's NOT in the ECC bits of the chunk.
338 * Use the following to compute the bit position of the
339 * physical block mark within the ECC-based view of the page:
340 * (page_size - D) * 8
344 block_mark_bit_offset = mtd->writesize * 8 -
345 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
346 + geo->metadata_size * 8);
348 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
349 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
353 int common_nfc_set_geometry(struct gpmi_nand_data *this)
355 return legacy_set_geometry(this);
358 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
360 /* We use the DMA channel 0 to access all the nand chips. */
361 return this->dma_chans[0];
364 /* Can we use the upper's buffer directly for DMA? */
365 void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
367 struct scatterlist *sgl = &this->data_sgl;
370 this->direct_dma_map_ok = true;
372 /* first try to map the upper buffer directly */
373 sg_init_one(sgl, this->upper_buf, this->upper_len);
374 ret = dma_map_sg(this->dev, sgl, 1, dr);
376 /* We have to use our own DMA buffer. */
377 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
379 if (dr == DMA_TO_DEVICE)
380 memcpy(this->data_buffer_dma, this->upper_buf,
383 ret = dma_map_sg(this->dev, sgl, 1, dr);
385 pr_err("DMA mapping failed.\n");
387 this->direct_dma_map_ok = false;
391 /* This will be called after the DMA operation is finished. */
392 static void dma_irq_callback(void *param)
394 struct gpmi_nand_data *this = param;
395 struct completion *dma_c = &this->dma_done;
399 switch (this->dma_type) {
400 case DMA_FOR_COMMAND:
401 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
404 case DMA_FOR_READ_DATA:
405 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
406 if (this->direct_dma_map_ok == false)
407 memcpy(this->upper_buf, this->data_buffer_dma,
411 case DMA_FOR_WRITE_DATA:
412 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
415 case DMA_FOR_READ_ECC_PAGE:
416 case DMA_FOR_WRITE_ECC_PAGE:
417 /* We have to wait the BCH interrupt to finish. */
421 pr_err("in wrong DMA operation.\n");
425 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
426 struct dma_async_tx_descriptor *desc)
428 struct completion *dma_c = &this->dma_done;
431 init_completion(dma_c);
433 desc->callback = dma_irq_callback;
434 desc->callback_param = this;
435 dmaengine_submit(desc);
436 dma_async_issue_pending(get_dma_chan(this));
438 /* Wait for the interrupt from the DMA block. */
439 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
441 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
442 gpmi_dump_info(this);
449 * This function is used in BCH reading or BCH writing pages.
450 * It will wait for the BCH interrupt as long as ONE second.
451 * Actually, we must wait for two interrupts :
452 * [1] firstly the DMA interrupt and
453 * [2] secondly the BCH interrupt.
455 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
456 struct dma_async_tx_descriptor *desc)
458 struct completion *bch_c = &this->bch_done;
461 /* Prepare to receive an interrupt from the BCH block. */
462 init_completion(bch_c);
465 start_dma_without_bch_irq(this, desc);
467 /* Wait for the interrupt from the BCH block. */
468 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
470 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
471 gpmi_dump_info(this);
477 static int acquire_register_block(struct gpmi_nand_data *this,
478 const char *res_name)
480 struct platform_device *pdev = this->pdev;
481 struct resources *res = &this->resources;
485 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
487 pr_err("Can't get resource for %s\n", res_name);
491 p = ioremap(r->start, resource_size(r));
493 pr_err("Can't remap %s\n", res_name);
497 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
499 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
502 pr_err("unknown resource name : %s\n", res_name);
507 static void release_register_block(struct gpmi_nand_data *this)
509 struct resources *res = &this->resources;
511 iounmap(res->gpmi_regs);
513 iounmap(res->bch_regs);
514 res->gpmi_regs = NULL;
515 res->bch_regs = NULL;
518 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
520 struct platform_device *pdev = this->pdev;
521 struct resources *res = &this->resources;
522 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
526 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
528 pr_err("Can't get resource for %s\n", res_name);
532 err = request_irq(r->start, irq_h, 0, res_name, this);
534 pr_err("Can't own %s\n", res_name);
538 res->bch_low_interrupt = r->start;
539 res->bch_high_interrupt = r->end;
543 static void release_bch_irq(struct gpmi_nand_data *this)
545 struct resources *res = &this->resources;
546 int i = res->bch_low_interrupt;
548 for (; i <= res->bch_high_interrupt; i++)
552 static void release_dma_channels(struct gpmi_nand_data *this)
555 for (i = 0; i < DMA_CHANS; i++)
556 if (this->dma_chans[i]) {
557 dma_release_channel(this->dma_chans[i]);
558 this->dma_chans[i] = NULL;
562 static int acquire_dma_channels(struct gpmi_nand_data *this)
564 struct platform_device *pdev = this->pdev;
565 struct dma_chan *dma_chan;
567 /* request dma channel */
568 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
570 pr_err("Failed to request DMA channel.\n");
574 this->dma_chans[0] = dma_chan;
578 release_dma_channels(this);
582 static void gpmi_put_clks(struct gpmi_nand_data *this)
584 struct resources *r = &this->resources;
588 for (i = 0; i < GPMI_CLK_MAX; i++) {
597 static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
598 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
601 static int gpmi_get_clks(struct gpmi_nand_data *this)
603 struct resources *r = &this->resources;
604 char **extra_clks = NULL;
608 /* The main clock is stored in the first. */
609 r->clock[0] = clk_get(this->dev, "gpmi_io");
610 if (IS_ERR(r->clock[0])) {
611 err = PTR_ERR(r->clock[0]);
615 /* Get extra clocks */
616 if (GPMI_IS_MX6Q(this))
617 extra_clks = extra_clks_for_mx6q;
621 for (i = 1; i < GPMI_CLK_MAX; i++) {
622 if (extra_clks[i - 1] == NULL)
625 clk = clk_get(this->dev, extra_clks[i - 1]);
634 if (GPMI_IS_MX6Q(this))
636 * Set the default value for the gpmi clock in mx6q:
638 * If you want to use the ONFI nand which is in the
639 * Synchronous Mode, you should change the clock as you need.
641 clk_set_rate(r->clock[0], 22000000);
646 dev_dbg(this->dev, "failed in finding the clocks.\n");
651 static int acquire_resources(struct gpmi_nand_data *this)
655 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
659 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
663 ret = acquire_bch_irq(this, bch_irq);
667 ret = acquire_dma_channels(this);
669 goto exit_dma_channels;
671 ret = gpmi_get_clks(this);
677 release_dma_channels(this);
679 release_bch_irq(this);
681 release_register_block(this);
685 static void release_resources(struct gpmi_nand_data *this)
688 release_register_block(this);
689 release_bch_irq(this);
690 release_dma_channels(this);
693 static int init_hardware(struct gpmi_nand_data *this)
698 * This structure contains the "safe" GPMI timing that should succeed
699 * with any NAND Flash device
700 * (although, with less-than-optimal performance).
702 struct nand_timing safe_timing = {
703 .data_setup_in_ns = 80,
704 .data_hold_in_ns = 60,
705 .address_setup_in_ns = 25,
706 .gpmi_sample_delay_in_ns = 6,
712 /* Initialize the hardwares. */
713 ret = gpmi_init(this);
717 this->timing = safe_timing;
721 static int read_page_prepare(struct gpmi_nand_data *this,
722 void *destination, unsigned length,
723 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
724 void **use_virt, dma_addr_t *use_phys)
726 struct device *dev = this->dev;
728 if (virt_addr_valid(destination)) {
729 dma_addr_t dest_phys;
731 dest_phys = dma_map_single(dev, destination,
732 length, DMA_FROM_DEVICE);
733 if (dma_mapping_error(dev, dest_phys)) {
734 if (alt_size < length) {
735 pr_err("%s, Alternate buffer is too small\n",
741 *use_virt = destination;
742 *use_phys = dest_phys;
743 this->direct_dma_map_ok = true;
748 *use_virt = alt_virt;
749 *use_phys = alt_phys;
750 this->direct_dma_map_ok = false;
754 static inline void read_page_end(struct gpmi_nand_data *this,
755 void *destination, unsigned length,
756 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
757 void *used_virt, dma_addr_t used_phys)
759 if (this->direct_dma_map_ok)
760 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
763 static inline void read_page_swap_end(struct gpmi_nand_data *this,
764 void *destination, unsigned length,
765 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
766 void *used_virt, dma_addr_t used_phys)
768 if (!this->direct_dma_map_ok)
769 memcpy(destination, alt_virt, length);
772 static int send_page_prepare(struct gpmi_nand_data *this,
773 const void *source, unsigned length,
774 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
775 const void **use_virt, dma_addr_t *use_phys)
777 struct device *dev = this->dev;
779 if (virt_addr_valid(source)) {
780 dma_addr_t source_phys;
782 source_phys = dma_map_single(dev, (void *)source, length,
784 if (dma_mapping_error(dev, source_phys)) {
785 if (alt_size < length) {
786 pr_err("%s, Alternate buffer is too small\n",
793 *use_phys = source_phys;
798 * Copy the content of the source buffer into the alternate
799 * buffer and set up the return values accordingly.
801 memcpy(alt_virt, source, length);
803 *use_virt = alt_virt;
804 *use_phys = alt_phys;
808 static void send_page_end(struct gpmi_nand_data *this,
809 const void *source, unsigned length,
810 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
811 const void *used_virt, dma_addr_t used_phys)
813 struct device *dev = this->dev;
814 if (used_virt == source)
815 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
818 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
820 struct device *dev = this->dev;
822 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
823 dma_free_coherent(dev, this->page_buffer_size,
824 this->page_buffer_virt,
825 this->page_buffer_phys);
826 kfree(this->cmd_buffer);
827 kfree(this->data_buffer_dma);
829 this->cmd_buffer = NULL;
830 this->data_buffer_dma = NULL;
831 this->page_buffer_virt = NULL;
832 this->page_buffer_size = 0;
835 /* Allocate the DMA buffers */
836 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
838 struct bch_geometry *geo = &this->bch_geometry;
839 struct device *dev = this->dev;
841 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
842 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
843 if (this->cmd_buffer == NULL)
846 /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
847 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
848 if (this->data_buffer_dma == NULL)
852 * [3] Allocate the page buffer.
854 * Both the payload buffer and the auxiliary buffer must appear on
855 * 32-bit boundaries. We presume the size of the payload buffer is a
856 * power of two and is much larger than four, which guarantees the
857 * auxiliary buffer will appear on a 32-bit boundary.
859 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
860 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
861 &this->page_buffer_phys, GFP_DMA);
862 if (!this->page_buffer_virt)
866 /* Slice up the page buffer. */
867 this->payload_virt = this->page_buffer_virt;
868 this->payload_phys = this->page_buffer_phys;
869 this->auxiliary_virt = this->payload_virt + geo->payload_size;
870 this->auxiliary_phys = this->payload_phys + geo->payload_size;
874 gpmi_free_dma_buffer(this);
875 pr_err("Error allocating DMA buffers!\n");
879 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
881 struct nand_chip *chip = mtd->priv;
882 struct gpmi_nand_data *this = chip->priv;
886 * Every operation begins with a command byte and a series of zero or
887 * more address bytes. These are distinguished by either the Address
888 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
889 * asserted. When MTD is ready to execute the command, it will deassert
890 * both latch enables.
892 * Rather than run a separate DMA operation for every single byte, we
893 * queue them up and run a single DMA operation for the entire series
894 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
896 if ((ctrl & (NAND_ALE | NAND_CLE))) {
897 if (data != NAND_CMD_NONE)
898 this->cmd_buffer[this->command_length++] = data;
902 if (!this->command_length)
905 ret = gpmi_send_command(this);
907 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
909 this->command_length = 0;
912 static int gpmi_dev_ready(struct mtd_info *mtd)
914 struct nand_chip *chip = mtd->priv;
915 struct gpmi_nand_data *this = chip->priv;
917 return gpmi_is_ready(this, this->current_chip);
920 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
922 struct nand_chip *chip = mtd->priv;
923 struct gpmi_nand_data *this = chip->priv;
925 if ((this->current_chip < 0) && (chipnr >= 0))
927 else if ((this->current_chip >= 0) && (chipnr < 0))
930 this->current_chip = chipnr;
933 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
935 struct nand_chip *chip = mtd->priv;
936 struct gpmi_nand_data *this = chip->priv;
938 pr_debug("len is %d\n", len);
939 this->upper_buf = buf;
940 this->upper_len = len;
942 gpmi_read_data(this);
945 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
947 struct nand_chip *chip = mtd->priv;
948 struct gpmi_nand_data *this = chip->priv;
950 pr_debug("len is %d\n", len);
951 this->upper_buf = (uint8_t *)buf;
952 this->upper_len = len;
954 gpmi_send_data(this);
957 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
959 struct nand_chip *chip = mtd->priv;
960 struct gpmi_nand_data *this = chip->priv;
961 uint8_t *buf = this->data_buffer_dma;
963 gpmi_read_buf(mtd, buf, 1);
968 * Handles block mark swapping.
969 * It can be called in swapping the block mark, or swapping it back,
970 * because the the operations are the same.
972 static void block_mark_swapping(struct gpmi_nand_data *this,
973 void *payload, void *auxiliary)
975 struct bch_geometry *nfc_geo = &this->bch_geometry;
980 unsigned char from_data;
981 unsigned char from_oob;
983 if (!this->swap_block_mark)
987 * If control arrives here, we're swapping. Make some convenience
990 bit = nfc_geo->block_mark_bit_offset;
991 p = payload + nfc_geo->block_mark_byte_offset;
995 * Get the byte from the data area that overlays the block mark. Since
996 * the ECC engine applies its own view to the bits in the page, the
997 * physical block mark won't (in general) appear on a byte boundary in
1000 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1002 /* Get the byte from the OOB. */
1008 mask = (0x1 << bit) - 1;
1009 p[0] = (p[0] & mask) | (from_oob << bit);
1012 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1015 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1016 uint8_t *buf, int oob_required, int page)
1018 struct gpmi_nand_data *this = chip->priv;
1019 struct bch_geometry *nfc_geo = &this->bch_geometry;
1021 dma_addr_t payload_phys;
1022 void *auxiliary_virt;
1023 dma_addr_t auxiliary_phys;
1025 unsigned char *status;
1026 unsigned int max_bitflips = 0;
1029 pr_debug("page number is : %d\n", page);
1030 ret = read_page_prepare(this, buf, mtd->writesize,
1031 this->payload_virt, this->payload_phys,
1032 nfc_geo->payload_size,
1033 &payload_virt, &payload_phys);
1035 pr_err("Inadequate DMA buffer\n");
1039 auxiliary_virt = this->auxiliary_virt;
1040 auxiliary_phys = this->auxiliary_phys;
1043 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
1044 read_page_end(this, buf, mtd->writesize,
1045 this->payload_virt, this->payload_phys,
1046 nfc_geo->payload_size,
1047 payload_virt, payload_phys);
1049 pr_err("Error in ECC-based read: %d\n", ret);
1053 /* handle the block mark swapping */
1054 block_mark_swapping(this, payload_virt, auxiliary_virt);
1056 /* Loop over status bytes, accumulating ECC status. */
1057 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
1059 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1060 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1063 if (*status == STATUS_UNCORRECTABLE) {
1064 mtd->ecc_stats.failed++;
1067 mtd->ecc_stats.corrected += *status;
1068 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1073 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1074 * for details about our policy for delivering the OOB.
1076 * We fill the caller's buffer with set bits, and then copy the
1077 * block mark to th caller's buffer. Note that, if block mark
1078 * swapping was necessary, it has already been done, so we can
1079 * rely on the first byte of the auxiliary buffer to contain
1082 memset(chip->oob_poi, ~0, mtd->oobsize);
1083 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
1086 read_page_swap_end(this, buf, mtd->writesize,
1087 this->payload_virt, this->payload_phys,
1088 nfc_geo->payload_size,
1089 payload_virt, payload_phys);
1091 return max_bitflips;
1094 static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1095 const uint8_t *buf, int oob_required)
1097 struct gpmi_nand_data *this = chip->priv;
1098 struct bch_geometry *nfc_geo = &this->bch_geometry;
1099 const void *payload_virt;
1100 dma_addr_t payload_phys;
1101 const void *auxiliary_virt;
1102 dma_addr_t auxiliary_phys;
1105 pr_debug("ecc write page.\n");
1106 if (this->swap_block_mark) {
1108 * If control arrives here, we're doing block mark swapping.
1109 * Since we can't modify the caller's buffers, we must copy them
1112 memcpy(this->payload_virt, buf, mtd->writesize);
1113 payload_virt = this->payload_virt;
1114 payload_phys = this->payload_phys;
1116 memcpy(this->auxiliary_virt, chip->oob_poi,
1117 nfc_geo->auxiliary_size);
1118 auxiliary_virt = this->auxiliary_virt;
1119 auxiliary_phys = this->auxiliary_phys;
1121 /* Handle block mark swapping. */
1122 block_mark_swapping(this,
1123 (void *) payload_virt, (void *) auxiliary_virt);
1126 * If control arrives here, we're not doing block mark swapping,
1127 * so we can to try and use the caller's buffers.
1129 ret = send_page_prepare(this,
1130 buf, mtd->writesize,
1131 this->payload_virt, this->payload_phys,
1132 nfc_geo->payload_size,
1133 &payload_virt, &payload_phys);
1135 pr_err("Inadequate payload DMA buffer\n");
1139 ret = send_page_prepare(this,
1140 chip->oob_poi, mtd->oobsize,
1141 this->auxiliary_virt, this->auxiliary_phys,
1142 nfc_geo->auxiliary_size,
1143 &auxiliary_virt, &auxiliary_phys);
1145 pr_err("Inadequate auxiliary DMA buffer\n");
1146 goto exit_auxiliary;
1151 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1153 pr_err("Error in ECC-based write: %d\n", ret);
1155 if (!this->swap_block_mark) {
1156 send_page_end(this, chip->oob_poi, mtd->oobsize,
1157 this->auxiliary_virt, this->auxiliary_phys,
1158 nfc_geo->auxiliary_size,
1159 auxiliary_virt, auxiliary_phys);
1161 send_page_end(this, buf, mtd->writesize,
1162 this->payload_virt, this->payload_phys,
1163 nfc_geo->payload_size,
1164 payload_virt, payload_phys);
1171 * There are several places in this driver where we have to handle the OOB and
1172 * block marks. This is the function where things are the most complicated, so
1173 * this is where we try to explain it all. All the other places refer back to
1176 * These are the rules, in order of decreasing importance:
1178 * 1) Nothing the caller does can be allowed to imperil the block mark.
1180 * 2) In read operations, the first byte of the OOB we return must reflect the
1181 * true state of the block mark, no matter where that block mark appears in
1182 * the physical page.
1184 * 3) ECC-based read operations return an OOB full of set bits (since we never
1185 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1188 * 4) "Raw" read operations return a direct view of the physical bytes in the
1189 * page, using the conventional definition of which bytes are data and which
1190 * are OOB. This gives the caller a way to see the actual, physical bytes
1191 * in the page, without the distortions applied by our ECC engine.
1194 * What we do for this specific read operation depends on two questions:
1196 * 1) Are we doing a "raw" read, or an ECC-based read?
1198 * 2) Are we using block mark swapping or transcription?
1200 * There are four cases, illustrated by the following Karnaugh map:
1202 * | Raw | ECC-based |
1203 * -------------+-------------------------+-------------------------+
1204 * | Read the conventional | |
1205 * | OOB at the end of the | |
1206 * Swapping | page and return it. It | |
1207 * | contains exactly what | |
1208 * | we want. | Read the block mark and |
1209 * -------------+-------------------------+ return it in a buffer |
1210 * | Read the conventional | full of set bits. |
1211 * | OOB at the end of the | |
1212 * | page and also the block | |
1213 * Transcribing | mark in the metadata. | |
1214 * | Copy the block mark | |
1215 * | into the first byte of | |
1217 * -------------+-------------------------+-------------------------+
1219 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1220 * giving an accurate view of the actual, physical bytes in the page (we're
1221 * overwriting the block mark). That's OK because it's more important to follow
1224 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1225 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1226 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1227 * ECC-based or raw view of the page is implicit in which function it calls
1228 * (there is a similar pair of ECC-based/raw functions for writing).
1230 * FIXME: The following paragraph is incorrect, now that there exist
1231 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1233 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1234 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1235 * caller wants an ECC-based or raw view of the page is not propagated down to
1238 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1241 struct gpmi_nand_data *this = chip->priv;
1243 pr_debug("page number is %d\n", page);
1244 /* clear the OOB buffer */
1245 memset(chip->oob_poi, ~0, mtd->oobsize);
1247 /* Read out the conventional OOB. */
1248 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1249 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1252 * Now, we want to make sure the block mark is correct. In the
1253 * Swapping/Raw case, we already have it. Otherwise, we need to
1254 * explicitly read it.
1256 if (!this->swap_block_mark) {
1257 /* Read the block mark into the first byte of the OOB buffer. */
1258 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1259 chip->oob_poi[0] = chip->read_byte(mtd);
1266 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1268 struct nand_oobfree *of = mtd->ecclayout->oobfree;
1271 /* Do we have available oob area? */
1275 if (!nand_is_slc(chip))
1278 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1279 chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1280 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1282 status = chip->waitfunc(mtd, chip);
1283 return status & NAND_STATUS_FAIL ? -EIO : 0;
1286 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1288 struct nand_chip *chip = mtd->priv;
1289 struct gpmi_nand_data *this = chip->priv;
1291 uint8_t *block_mark;
1292 int column, page, status, chipnr;
1294 chipnr = (int)(ofs >> chip->chip_shift);
1295 chip->select_chip(mtd, chipnr);
1297 column = this->swap_block_mark ? mtd->writesize : 0;
1299 /* Write the block mark. */
1300 block_mark = this->data_buffer_dma;
1301 block_mark[0] = 0; /* bad block marker */
1303 /* Shift to get page */
1304 page = (int)(ofs >> chip->page_shift);
1306 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1307 chip->write_buf(mtd, block_mark, 1);
1308 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1310 status = chip->waitfunc(mtd, chip);
1311 if (status & NAND_STATUS_FAIL)
1314 chip->select_chip(mtd, -1);
1319 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1321 struct boot_rom_geometry *geometry = &this->rom_geometry;
1324 * Set the boot block stride size.
1326 * In principle, we should be reading this from the OTP bits, since
1327 * that's where the ROM is going to get it. In fact, we don't have any
1328 * way to read the OTP bits, so we go with the default and hope for the
1331 geometry->stride_size_in_pages = 64;
1334 * Set the search area stride exponent.
1336 * In principle, we should be reading this from the OTP bits, since
1337 * that's where the ROM is going to get it. In fact, we don't have any
1338 * way to read the OTP bits, so we go with the default and hope for the
1341 geometry->search_area_stride_exponent = 2;
1345 static const char *fingerprint = "STMP";
1346 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1348 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1349 struct device *dev = this->dev;
1350 struct mtd_info *mtd = &this->mtd;
1351 struct nand_chip *chip = &this->nand;
1352 unsigned int search_area_size_in_strides;
1353 unsigned int stride;
1355 uint8_t *buffer = chip->buffers->databuf;
1356 int saved_chip_number;
1357 int found_an_ncb_fingerprint = false;
1359 /* Compute the number of strides in a search area. */
1360 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1362 saved_chip_number = this->current_chip;
1363 chip->select_chip(mtd, 0);
1366 * Loop through the first search area, looking for the NCB fingerprint.
1368 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1370 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1371 /* Compute the page addresses. */
1372 page = stride * rom_geo->stride_size_in_pages;
1374 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1377 * Read the NCB fingerprint. The fingerprint is four bytes long
1378 * and starts in the 12th byte of the page.
1380 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1381 chip->read_buf(mtd, buffer, strlen(fingerprint));
1383 /* Look for the fingerprint. */
1384 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1385 found_an_ncb_fingerprint = true;
1391 chip->select_chip(mtd, saved_chip_number);
1393 if (found_an_ncb_fingerprint)
1394 dev_dbg(dev, "\tFound a fingerprint\n");
1396 dev_dbg(dev, "\tNo fingerprint found\n");
1397 return found_an_ncb_fingerprint;
1400 /* Writes a transcription stamp. */
1401 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1403 struct device *dev = this->dev;
1404 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1405 struct mtd_info *mtd = &this->mtd;
1406 struct nand_chip *chip = &this->nand;
1407 unsigned int block_size_in_pages;
1408 unsigned int search_area_size_in_strides;
1409 unsigned int search_area_size_in_pages;
1410 unsigned int search_area_size_in_blocks;
1412 unsigned int stride;
1414 uint8_t *buffer = chip->buffers->databuf;
1415 int saved_chip_number;
1418 /* Compute the search area geometry. */
1419 block_size_in_pages = mtd->erasesize / mtd->writesize;
1420 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1421 search_area_size_in_pages = search_area_size_in_strides *
1422 rom_geo->stride_size_in_pages;
1423 search_area_size_in_blocks =
1424 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1425 block_size_in_pages;
1427 dev_dbg(dev, "Search Area Geometry :\n");
1428 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1429 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1430 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1432 /* Select chip 0. */
1433 saved_chip_number = this->current_chip;
1434 chip->select_chip(mtd, 0);
1436 /* Loop over blocks in the first search area, erasing them. */
1437 dev_dbg(dev, "Erasing the search area...\n");
1439 for (block = 0; block < search_area_size_in_blocks; block++) {
1440 /* Compute the page address. */
1441 page = block * block_size_in_pages;
1443 /* Erase this block. */
1444 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1445 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1446 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1448 /* Wait for the erase to finish. */
1449 status = chip->waitfunc(mtd, chip);
1450 if (status & NAND_STATUS_FAIL)
1451 dev_err(dev, "[%s] Erase failed.\n", __func__);
1454 /* Write the NCB fingerprint into the page buffer. */
1455 memset(buffer, ~0, mtd->writesize);
1456 memset(chip->oob_poi, ~0, mtd->oobsize);
1457 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1459 /* Loop through the first search area, writing NCB fingerprints. */
1460 dev_dbg(dev, "Writing NCB fingerprints...\n");
1461 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1462 /* Compute the page addresses. */
1463 page = stride * rom_geo->stride_size_in_pages;
1465 /* Write the first page of the current stride. */
1466 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1467 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1468 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
1469 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1471 /* Wait for the write to finish. */
1472 status = chip->waitfunc(mtd, chip);
1473 if (status & NAND_STATUS_FAIL)
1474 dev_err(dev, "[%s] Write failed.\n", __func__);
1477 /* Deselect chip 0. */
1478 chip->select_chip(mtd, saved_chip_number);
1482 static int mx23_boot_init(struct gpmi_nand_data *this)
1484 struct device *dev = this->dev;
1485 struct nand_chip *chip = &this->nand;
1486 struct mtd_info *mtd = &this->mtd;
1487 unsigned int block_count;
1496 * If control arrives here, we can't use block mark swapping, which
1497 * means we're forced to use transcription. First, scan for the
1498 * transcription stamp. If we find it, then we don't have to do
1499 * anything -- the block marks are already transcribed.
1501 if (mx23_check_transcription_stamp(this))
1505 * If control arrives here, we couldn't find a transcription stamp, so
1506 * so we presume the block marks are in the conventional location.
1508 dev_dbg(dev, "Transcribing bad block marks...\n");
1510 /* Compute the number of blocks in the entire medium. */
1511 block_count = chip->chipsize >> chip->phys_erase_shift;
1514 * Loop over all the blocks in the medium, transcribing block marks as
1517 for (block = 0; block < block_count; block++) {
1519 * Compute the chip, page and byte addresses for this block's
1520 * conventional mark.
1522 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1523 page = block << (chip->phys_erase_shift - chip->page_shift);
1524 byte = block << chip->phys_erase_shift;
1526 /* Send the command to read the conventional block mark. */
1527 chip->select_chip(mtd, chipnr);
1528 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1529 block_mark = chip->read_byte(mtd);
1530 chip->select_chip(mtd, -1);
1533 * Check if the block is marked bad. If so, we need to mark it
1534 * again, but this time the result will be a mark in the
1535 * location where we transcribe block marks.
1537 if (block_mark != 0xff) {
1538 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1539 ret = chip->block_markbad(mtd, byte);
1541 dev_err(dev, "Failed to mark block bad with "
1546 /* Write the stamp that indicates we've transcribed the block marks. */
1547 mx23_write_transcription_stamp(this);
1551 static int nand_boot_init(struct gpmi_nand_data *this)
1553 nand_boot_set_geometry(this);
1555 /* This is ROM arch-specific initilization before the BBT scanning. */
1556 if (GPMI_IS_MX23(this))
1557 return mx23_boot_init(this);
1561 static int gpmi_set_geometry(struct gpmi_nand_data *this)
1565 /* Free the temporary DMA memory for reading ID. */
1566 gpmi_free_dma_buffer(this);
1568 /* Set up the NFC geometry which is used by BCH. */
1569 ret = bch_set_geometry(this);
1571 pr_err("Error setting BCH geometry : %d\n", ret);
1575 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1576 return gpmi_alloc_dma_buffer(this);
1579 static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1583 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1584 if (GPMI_IS_MX23(this))
1585 this->swap_block_mark = false;
1587 this->swap_block_mark = true;
1589 /* Set up the medium geometry */
1590 ret = gpmi_set_geometry(this);
1594 /* NAND boot init, depends on the gpmi_set_geometry(). */
1595 return nand_boot_init(this);
1598 static void gpmi_nfc_exit(struct gpmi_nand_data *this)
1600 nand_release(&this->mtd);
1601 gpmi_free_dma_buffer(this);
1604 static int gpmi_init_last(struct gpmi_nand_data *this)
1606 struct mtd_info *mtd = &this->mtd;
1607 struct nand_chip *chip = mtd->priv;
1608 struct nand_ecc_ctrl *ecc = &chip->ecc;
1609 struct bch_geometry *bch_geo = &this->bch_geometry;
1612 /* Prepare for the BBT scan. */
1613 ret = gpmi_pre_bbt_scan(this);
1617 /* Init the nand_ecc_ctrl{} */
1618 ecc->read_page = gpmi_ecc_read_page;
1619 ecc->write_page = gpmi_ecc_write_page;
1620 ecc->read_oob = gpmi_ecc_read_oob;
1621 ecc->write_oob = gpmi_ecc_write_oob;
1622 ecc->mode = NAND_ECC_HW;
1623 ecc->size = bch_geo->ecc_chunk_size;
1624 ecc->strength = bch_geo->ecc_strength;
1625 ecc->layout = &gpmi_hw_ecclayout;
1628 * Can we enable the extra features? such as EDO or Sync mode.
1630 * We do not check the return value now. That's means if we fail in
1631 * enable the extra features, we still can run in the normal way.
1633 gpmi_extra_init(this);
1638 static int gpmi_nfc_init(struct gpmi_nand_data *this)
1640 struct mtd_info *mtd = &this->mtd;
1641 struct nand_chip *chip = &this->nand;
1642 struct mtd_part_parser_data ppdata = {};
1645 /* init current chip */
1646 this->current_chip = -1;
1648 /* init the MTD data structures */
1650 mtd->name = "gpmi-nand";
1651 mtd->owner = THIS_MODULE;
1653 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1655 chip->select_chip = gpmi_select_chip;
1656 chip->cmd_ctrl = gpmi_cmd_ctrl;
1657 chip->dev_ready = gpmi_dev_ready;
1658 chip->read_byte = gpmi_read_byte;
1659 chip->read_buf = gpmi_read_buf;
1660 chip->write_buf = gpmi_write_buf;
1661 chip->badblock_pattern = &gpmi_bbt_descr;
1662 chip->block_markbad = gpmi_block_markbad;
1663 chip->options |= NAND_NO_SUBPAGE_WRITE;
1664 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1665 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1668 * Allocate a temporary DMA buffer for reading ID in the
1669 * nand_scan_ident().
1671 this->bch_geometry.payload_size = 1024;
1672 this->bch_geometry.auxiliary_size = 128;
1673 ret = gpmi_alloc_dma_buffer(this);
1677 ret = nand_scan_ident(mtd, GPMI_IS_MX6Q(this) ? 2 : 1, NULL);
1681 ret = gpmi_init_last(this);
1685 ret = nand_scan_tail(mtd);
1689 ppdata.of_node = this->pdev->dev.of_node;
1690 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
1696 gpmi_nfc_exit(this);
1700 static const struct platform_device_id gpmi_ids[] = {
1701 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1702 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
1703 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
1707 static const struct of_device_id gpmi_nand_id_table[] = {
1709 .compatible = "fsl,imx23-gpmi-nand",
1710 .data = (void *)&gpmi_ids[IS_MX23],
1712 .compatible = "fsl,imx28-gpmi-nand",
1713 .data = (void *)&gpmi_ids[IS_MX28],
1715 .compatible = "fsl,imx6q-gpmi-nand",
1716 .data = (void *)&gpmi_ids[IS_MX6Q],
1719 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1721 static int gpmi_nand_probe(struct platform_device *pdev)
1723 struct gpmi_nand_data *this;
1724 const struct of_device_id *of_id;
1727 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1729 pdev->id_entry = of_id->data;
1731 pr_err("Failed to find the right device id.\n");
1735 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1737 pr_err("Failed to allocate per-device memory\n");
1741 platform_set_drvdata(pdev, this);
1743 this->dev = &pdev->dev;
1745 ret = acquire_resources(this);
1747 goto exit_acquire_resources;
1749 ret = init_hardware(this);
1753 ret = gpmi_nfc_init(this);
1757 dev_info(this->dev, "driver registered.\n");
1762 release_resources(this);
1763 exit_acquire_resources:
1764 dev_err(this->dev, "driver registration failed: %d\n", ret);
1769 static int gpmi_nand_remove(struct platform_device *pdev)
1771 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1773 gpmi_nfc_exit(this);
1774 release_resources(this);
1778 static struct platform_driver gpmi_nand_driver = {
1780 .name = "gpmi-nand",
1781 .of_match_table = gpmi_nand_id_table,
1783 .probe = gpmi_nand_probe,
1784 .remove = gpmi_nand_remove,
1785 .id_table = gpmi_ids,
1787 module_platform_driver(gpmi_nand_driver);
1789 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1790 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1791 MODULE_LICENSE("GPL");