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/module.h>
25 #include <linux/mtd/partitions.h>
27 #include <linux/of_device.h>
28 #include <linux/of_mtd.h>
29 #include "gpmi-nand.h"
32 /* Resource names for the GPMI NAND driver. */
33 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
34 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
35 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
37 /* add our owner bbt descriptor */
38 static uint8_t scan_ff_pattern[] = { 0xff };
39 static struct nand_bbt_descr gpmi_bbt_descr = {
43 .pattern = scan_ff_pattern
47 * We may change the layout if we can get the ECC info from the datasheet,
48 * else we will use all the (page + OOB).
50 static struct nand_ecclayout gpmi_hw_ecclayout = {
53 .oobfree = { {.offset = 0, .length = 0} }
56 static irqreturn_t bch_irq(int irq, void *cookie)
58 struct gpmi_nand_data *this = cookie;
61 complete(&this->bch_done);
66 * Calculate the ECC strength by hand:
67 * E : The ECC strength.
68 * G : the length of Galois Field.
69 * N : The chunk count of per page.
70 * O : the oobsize of the NAND chip.
71 * M : the metasize of per page.
75 * ------------ <= (O - M)
83 static inline int get_ecc_strength(struct gpmi_nand_data *this)
85 struct bch_geometry *geo = &this->bch_geometry;
86 struct mtd_info *mtd = &this->mtd;
89 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
90 / (geo->gf_len * geo->ecc_chunk_count);
92 /* We need the minor even number. */
93 return round_down(ecc_strength, 2);
96 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
98 struct bch_geometry *geo = &this->bch_geometry;
100 /* Do the sanity check. */
101 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
102 /* The mx23/mx28 only support the GF13. */
103 if (geo->gf_len == 14)
106 if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
108 } else if (GPMI_IS_MX6Q(this)) {
109 if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
116 * If we can get the ECC information from the nand chip, we do not
117 * need to calculate them ourselves.
119 * We may have available oob space in this case.
121 static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
123 struct bch_geometry *geo = &this->bch_geometry;
124 struct mtd_info *mtd = &this->mtd;
125 struct nand_chip *chip = mtd->priv;
126 struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
127 unsigned int block_mark_bit_offset;
129 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
132 switch (chip->ecc_step_ds) {
141 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
142 chip->ecc_strength_ds, chip->ecc_step_ds);
145 geo->ecc_chunk_size = chip->ecc_step_ds;
146 geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
147 if (!gpmi_check_ecc(this))
150 /* Keep the C >= O */
151 if (geo->ecc_chunk_size < mtd->oobsize) {
153 "unsupported nand chip. ecc size: %d, oob size : %d\n",
154 chip->ecc_step_ds, mtd->oobsize);
158 /* The default value, see comment in the legacy_set_geometry(). */
159 geo->metadata_size = 10;
161 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
164 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
167 * |<----------------------------------------------------->|
171 * |<-------------------------------------------->| D | | O' |
174 * +---+----------+-+----------+-+----------+-+----------+-+-----+
175 * | M | data |E| data |E| data |E| data |E| |
176 * +---+----------+-+----------+-+----------+-+----------+-+-----+
182 * P : the page size for BCH module.
183 * E : The ECC strength.
184 * G : the length of Galois Field.
185 * N : The chunk count of per page.
186 * M : the metasize of per page.
187 * C : the ecc chunk size, aka the "data" above.
188 * P': the nand chip's page size.
189 * O : the nand chip's oob size.
192 * The formula for P is :
195 * P = ------------ + P' + M
198 * The position of block mark moves forward in the ECC-based view
199 * of page, and the delta is:
202 * D = (---------------- + M)
205 * Please see the comment in legacy_set_geometry().
206 * With the condition C >= O , we still can get same result.
207 * So the bit position of the physical block mark within the ECC-based
208 * view of the page is :
211 geo->page_size = mtd->writesize + geo->metadata_size +
212 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
214 /* The available oob size we have. */
215 if (geo->page_size < mtd->writesize + mtd->oobsize) {
216 of->offset = geo->page_size - mtd->writesize;
217 of->length = mtd->oobsize - of->offset;
220 geo->payload_size = mtd->writesize;
222 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
223 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
224 + ALIGN(geo->ecc_chunk_count, 4);
226 if (!this->swap_block_mark)
230 block_mark_bit_offset = mtd->writesize * 8 -
231 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
232 + geo->metadata_size * 8);
234 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
235 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
239 static int legacy_set_geometry(struct gpmi_nand_data *this)
241 struct bch_geometry *geo = &this->bch_geometry;
242 struct mtd_info *mtd = &this->mtd;
243 unsigned int metadata_size;
244 unsigned int status_size;
245 unsigned int block_mark_bit_offset;
248 * The size of the metadata can be changed, though we set it to 10
249 * bytes now. But it can't be too large, because we have to save
250 * enough space for BCH.
252 geo->metadata_size = 10;
254 /* The default for the length of Galois Field. */
257 /* The default for chunk size. */
258 geo->ecc_chunk_size = 512;
259 while (geo->ecc_chunk_size < mtd->oobsize) {
260 geo->ecc_chunk_size *= 2; /* keep C >= O */
264 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
266 /* We use the same ECC strength for all chunks. */
267 geo->ecc_strength = get_ecc_strength(this);
268 if (!gpmi_check_ecc(this)) {
270 "We can not support this nand chip."
271 " Its required ecc strength(%d) is beyond our"
272 " capability(%d).\n", geo->ecc_strength,
273 (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
274 : MXS_ECC_STRENGTH_MAX));
278 geo->page_size = mtd->writesize + mtd->oobsize;
279 geo->payload_size = mtd->writesize;
282 * The auxiliary buffer contains the metadata and the ECC status. The
283 * metadata is padded to the nearest 32-bit boundary. The ECC status
284 * contains one byte for every ECC chunk, and is also padded to the
285 * nearest 32-bit boundary.
287 metadata_size = ALIGN(geo->metadata_size, 4);
288 status_size = ALIGN(geo->ecc_chunk_count, 4);
290 geo->auxiliary_size = metadata_size + status_size;
291 geo->auxiliary_status_offset = metadata_size;
293 if (!this->swap_block_mark)
297 * We need to compute the byte and bit offsets of
298 * the physical block mark within the ECC-based view of the page.
300 * NAND chip with 2K page shows below:
306 * +---+----------+-+----------+-+----------+-+----------+-+
307 * | M | data |E| data |E| data |E| data |E|
308 * +---+----------+-+----------+-+----------+-+----------+-+
310 * The position of block mark moves forward in the ECC-based view
311 * of page, and the delta is:
314 * D = (---------------- + M)
317 * With the formula to compute the ECC strength, and the condition
318 * : C >= O (C is the ecc chunk size)
320 * It's easy to deduce to the following result:
322 * E * G (O - M) C - M C - M
323 * ----------- <= ------- <= -------- < ---------
329 * D = (---------------- + M) < C
332 * The above inequality means the position of block mark
333 * within the ECC-based view of the page is still in the data chunk,
334 * and it's NOT in the ECC bits of the chunk.
336 * Use the following to compute the bit position of the
337 * physical block mark within the ECC-based view of the page:
338 * (page_size - D) * 8
342 block_mark_bit_offset = mtd->writesize * 8 -
343 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
344 + geo->metadata_size * 8);
346 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
347 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
351 int common_nfc_set_geometry(struct gpmi_nand_data *this)
353 if (of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")
354 && set_geometry_by_ecc_info(this))
356 return legacy_set_geometry(this);
359 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
361 /* We use the DMA channel 0 to access all the nand chips. */
362 return this->dma_chans[0];
365 /* Can we use the upper's buffer directly for DMA? */
366 void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
368 struct scatterlist *sgl = &this->data_sgl;
371 /* first try to map the upper buffer directly */
372 if (virt_addr_valid(this->upper_buf) &&
373 !object_is_on_stack(this->upper_buf)) {
374 sg_init_one(sgl, this->upper_buf, this->upper_len);
375 ret = dma_map_sg(this->dev, sgl, 1, dr);
379 this->direct_dma_map_ok = true;
384 /* We have to use our own DMA buffer. */
385 sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
387 if (dr == DMA_TO_DEVICE)
388 memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
390 dma_map_sg(this->dev, sgl, 1, dr);
392 this->direct_dma_map_ok = false;
395 /* This will be called after the DMA operation is finished. */
396 static void dma_irq_callback(void *param)
398 struct gpmi_nand_data *this = param;
399 struct completion *dma_c = &this->dma_done;
401 switch (this->dma_type) {
402 case DMA_FOR_COMMAND:
403 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
406 case DMA_FOR_READ_DATA:
407 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
408 if (this->direct_dma_map_ok == false)
409 memcpy(this->upper_buf, this->data_buffer_dma,
413 case DMA_FOR_WRITE_DATA:
414 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
417 case DMA_FOR_READ_ECC_PAGE:
418 case DMA_FOR_WRITE_ECC_PAGE:
419 /* We have to wait the BCH interrupt to finish. */
423 dev_err(this->dev, "in wrong DMA operation.\n");
429 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
430 struct dma_async_tx_descriptor *desc)
432 struct completion *dma_c = &this->dma_done;
435 init_completion(dma_c);
437 desc->callback = dma_irq_callback;
438 desc->callback_param = this;
439 dmaengine_submit(desc);
440 dma_async_issue_pending(get_dma_chan(this));
442 /* Wait for the interrupt from the DMA block. */
443 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
445 dev_err(this->dev, "DMA timeout, last DMA :%d\n",
446 this->last_dma_type);
447 gpmi_dump_info(this);
454 * This function is used in BCH reading or BCH writing pages.
455 * It will wait for the BCH interrupt as long as ONE second.
456 * Actually, we must wait for two interrupts :
457 * [1] firstly the DMA interrupt and
458 * [2] secondly the BCH interrupt.
460 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
461 struct dma_async_tx_descriptor *desc)
463 struct completion *bch_c = &this->bch_done;
466 /* Prepare to receive an interrupt from the BCH block. */
467 init_completion(bch_c);
470 start_dma_without_bch_irq(this, desc);
472 /* Wait for the interrupt from the BCH block. */
473 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
475 dev_err(this->dev, "BCH timeout, last DMA :%d\n",
476 this->last_dma_type);
477 gpmi_dump_info(this);
483 static int acquire_register_block(struct gpmi_nand_data *this,
484 const char *res_name)
486 struct platform_device *pdev = this->pdev;
487 struct resources *res = &this->resources;
491 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
492 p = devm_ioremap_resource(&pdev->dev, r);
496 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
498 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
501 dev_err(this->dev, "unknown resource name : %s\n", res_name);
506 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
508 struct platform_device *pdev = this->pdev;
509 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
513 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
515 dev_err(this->dev, "Can't get resource for %s\n", res_name);
519 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
521 dev_err(this->dev, "error requesting BCH IRQ\n");
526 static void release_dma_channels(struct gpmi_nand_data *this)
529 for (i = 0; i < DMA_CHANS; i++)
530 if (this->dma_chans[i]) {
531 dma_release_channel(this->dma_chans[i]);
532 this->dma_chans[i] = NULL;
536 static int acquire_dma_channels(struct gpmi_nand_data *this)
538 struct platform_device *pdev = this->pdev;
539 struct dma_chan *dma_chan;
541 /* request dma channel */
542 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
544 dev_err(this->dev, "Failed to request DMA channel.\n");
548 this->dma_chans[0] = dma_chan;
552 release_dma_channels(this);
556 static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
557 "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
560 static int gpmi_get_clks(struct gpmi_nand_data *this)
562 struct resources *r = &this->resources;
563 char **extra_clks = NULL;
567 /* The main clock is stored in the first. */
568 r->clock[0] = devm_clk_get(this->dev, "gpmi_io");
569 if (IS_ERR(r->clock[0])) {
570 err = PTR_ERR(r->clock[0]);
574 /* Get extra clocks */
575 if (GPMI_IS_MX6Q(this))
576 extra_clks = extra_clks_for_mx6q;
580 for (i = 1; i < GPMI_CLK_MAX; i++) {
581 if (extra_clks[i - 1] == NULL)
584 clk = devm_clk_get(this->dev, extra_clks[i - 1]);
593 if (GPMI_IS_MX6Q(this))
595 * Set the default value for the gpmi clock in mx6q:
597 * If you want to use the ONFI nand which is in the
598 * Synchronous Mode, you should change the clock as you need.
600 clk_set_rate(r->clock[0], 22000000);
605 dev_dbg(this->dev, "failed in finding the clocks.\n");
609 static int acquire_resources(struct gpmi_nand_data *this)
613 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
617 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
621 ret = acquire_bch_irq(this, bch_irq);
625 ret = acquire_dma_channels(this);
629 ret = gpmi_get_clks(this);
635 release_dma_channels(this);
640 static void release_resources(struct gpmi_nand_data *this)
642 release_dma_channels(this);
645 static int init_hardware(struct gpmi_nand_data *this)
650 * This structure contains the "safe" GPMI timing that should succeed
651 * with any NAND Flash device
652 * (although, with less-than-optimal performance).
654 struct nand_timing safe_timing = {
655 .data_setup_in_ns = 80,
656 .data_hold_in_ns = 60,
657 .address_setup_in_ns = 25,
658 .gpmi_sample_delay_in_ns = 6,
664 /* Initialize the hardwares. */
665 ret = gpmi_init(this);
669 this->timing = safe_timing;
673 static int read_page_prepare(struct gpmi_nand_data *this,
674 void *destination, unsigned length,
675 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
676 void **use_virt, dma_addr_t *use_phys)
678 struct device *dev = this->dev;
680 if (virt_addr_valid(destination)) {
681 dma_addr_t dest_phys;
683 dest_phys = dma_map_single(dev, destination,
684 length, DMA_FROM_DEVICE);
685 if (dma_mapping_error(dev, dest_phys)) {
686 if (alt_size < length) {
687 dev_err(dev, "Alternate buffer is too small\n");
692 *use_virt = destination;
693 *use_phys = dest_phys;
694 this->direct_dma_map_ok = true;
699 *use_virt = alt_virt;
700 *use_phys = alt_phys;
701 this->direct_dma_map_ok = false;
705 static inline void read_page_end(struct gpmi_nand_data *this,
706 void *destination, unsigned length,
707 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
708 void *used_virt, dma_addr_t used_phys)
710 if (this->direct_dma_map_ok)
711 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
714 static inline void read_page_swap_end(struct gpmi_nand_data *this,
715 void *destination, unsigned length,
716 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
717 void *used_virt, dma_addr_t used_phys)
719 if (!this->direct_dma_map_ok)
720 memcpy(destination, alt_virt, length);
723 static int send_page_prepare(struct gpmi_nand_data *this,
724 const void *source, unsigned length,
725 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
726 const void **use_virt, dma_addr_t *use_phys)
728 struct device *dev = this->dev;
730 if (virt_addr_valid(source)) {
731 dma_addr_t source_phys;
733 source_phys = dma_map_single(dev, (void *)source, length,
735 if (dma_mapping_error(dev, source_phys)) {
736 if (alt_size < length) {
737 dev_err(dev, "Alternate buffer is too small\n");
743 *use_phys = source_phys;
748 * Copy the content of the source buffer into the alternate
749 * buffer and set up the return values accordingly.
751 memcpy(alt_virt, source, length);
753 *use_virt = alt_virt;
754 *use_phys = alt_phys;
758 static void send_page_end(struct gpmi_nand_data *this,
759 const void *source, unsigned length,
760 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
761 const void *used_virt, dma_addr_t used_phys)
763 struct device *dev = this->dev;
764 if (used_virt == source)
765 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
768 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
770 struct device *dev = this->dev;
772 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
773 dma_free_coherent(dev, this->page_buffer_size,
774 this->page_buffer_virt,
775 this->page_buffer_phys);
776 kfree(this->cmd_buffer);
777 kfree(this->data_buffer_dma);
779 this->cmd_buffer = NULL;
780 this->data_buffer_dma = NULL;
781 this->page_buffer_virt = NULL;
782 this->page_buffer_size = 0;
785 /* Allocate the DMA buffers */
786 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
788 struct bch_geometry *geo = &this->bch_geometry;
789 struct device *dev = this->dev;
790 struct mtd_info *mtd = &this->mtd;
792 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
793 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
794 if (this->cmd_buffer == NULL)
798 * [2] Allocate a read/write data buffer.
799 * The gpmi_alloc_dma_buffer can be called twice.
800 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
801 * is called before the nand_scan_ident; and we allocate a buffer
802 * of the real NAND page size when the gpmi_alloc_dma_buffer is
803 * called after the nand_scan_ident.
805 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
806 GFP_DMA | GFP_KERNEL);
807 if (this->data_buffer_dma == NULL)
811 * [3] Allocate the page buffer.
813 * Both the payload buffer and the auxiliary buffer must appear on
814 * 32-bit boundaries. We presume the size of the payload buffer is a
815 * power of two and is much larger than four, which guarantees the
816 * auxiliary buffer will appear on a 32-bit boundary.
818 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
819 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
820 &this->page_buffer_phys, GFP_DMA);
821 if (!this->page_buffer_virt)
825 /* Slice up the page buffer. */
826 this->payload_virt = this->page_buffer_virt;
827 this->payload_phys = this->page_buffer_phys;
828 this->auxiliary_virt = this->payload_virt + geo->payload_size;
829 this->auxiliary_phys = this->payload_phys + geo->payload_size;
833 gpmi_free_dma_buffer(this);
837 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
839 struct nand_chip *chip = mtd->priv;
840 struct gpmi_nand_data *this = chip->priv;
844 * Every operation begins with a command byte and a series of zero or
845 * more address bytes. These are distinguished by either the Address
846 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
847 * asserted. When MTD is ready to execute the command, it will deassert
848 * both latch enables.
850 * Rather than run a separate DMA operation for every single byte, we
851 * queue them up and run a single DMA operation for the entire series
852 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
854 if ((ctrl & (NAND_ALE | NAND_CLE))) {
855 if (data != NAND_CMD_NONE)
856 this->cmd_buffer[this->command_length++] = data;
860 if (!this->command_length)
863 ret = gpmi_send_command(this);
865 dev_err(this->dev, "Chip: %u, Error %d\n",
866 this->current_chip, ret);
868 this->command_length = 0;
871 static int gpmi_dev_ready(struct mtd_info *mtd)
873 struct nand_chip *chip = mtd->priv;
874 struct gpmi_nand_data *this = chip->priv;
876 return gpmi_is_ready(this, this->current_chip);
879 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
881 struct nand_chip *chip = mtd->priv;
882 struct gpmi_nand_data *this = chip->priv;
884 if ((this->current_chip < 0) && (chipnr >= 0))
886 else if ((this->current_chip >= 0) && (chipnr < 0))
889 this->current_chip = chipnr;
892 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
894 struct nand_chip *chip = mtd->priv;
895 struct gpmi_nand_data *this = chip->priv;
897 dev_dbg(this->dev, "len is %d\n", len);
898 this->upper_buf = buf;
899 this->upper_len = len;
901 gpmi_read_data(this);
904 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
906 struct nand_chip *chip = mtd->priv;
907 struct gpmi_nand_data *this = chip->priv;
909 dev_dbg(this->dev, "len is %d\n", len);
910 this->upper_buf = (uint8_t *)buf;
911 this->upper_len = len;
913 gpmi_send_data(this);
916 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
918 struct nand_chip *chip = mtd->priv;
919 struct gpmi_nand_data *this = chip->priv;
920 uint8_t *buf = this->data_buffer_dma;
922 gpmi_read_buf(mtd, buf, 1);
927 * Handles block mark swapping.
928 * It can be called in swapping the block mark, or swapping it back,
929 * because the the operations are the same.
931 static void block_mark_swapping(struct gpmi_nand_data *this,
932 void *payload, void *auxiliary)
934 struct bch_geometry *nfc_geo = &this->bch_geometry;
939 unsigned char from_data;
940 unsigned char from_oob;
942 if (!this->swap_block_mark)
946 * If control arrives here, we're swapping. Make some convenience
949 bit = nfc_geo->block_mark_bit_offset;
950 p = payload + nfc_geo->block_mark_byte_offset;
954 * Get the byte from the data area that overlays the block mark. Since
955 * the ECC engine applies its own view to the bits in the page, the
956 * physical block mark won't (in general) appear on a byte boundary in
959 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
961 /* Get the byte from the OOB. */
967 mask = (0x1 << bit) - 1;
968 p[0] = (p[0] & mask) | (from_oob << bit);
971 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
974 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
975 uint8_t *buf, int oob_required, int page)
977 struct gpmi_nand_data *this = chip->priv;
978 struct bch_geometry *nfc_geo = &this->bch_geometry;
980 dma_addr_t payload_phys;
981 void *auxiliary_virt;
982 dma_addr_t auxiliary_phys;
984 unsigned char *status;
985 unsigned int max_bitflips = 0;
988 dev_dbg(this->dev, "page number is : %d\n", page);
989 ret = read_page_prepare(this, buf, nfc_geo->payload_size,
990 this->payload_virt, this->payload_phys,
991 nfc_geo->payload_size,
992 &payload_virt, &payload_phys);
994 dev_err(this->dev, "Inadequate DMA buffer\n");
998 auxiliary_virt = this->auxiliary_virt;
999 auxiliary_phys = this->auxiliary_phys;
1002 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
1003 read_page_end(this, buf, nfc_geo->payload_size,
1004 this->payload_virt, this->payload_phys,
1005 nfc_geo->payload_size,
1006 payload_virt, payload_phys);
1008 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
1012 /* handle the block mark swapping */
1013 block_mark_swapping(this, payload_virt, auxiliary_virt);
1015 /* Loop over status bytes, accumulating ECC status. */
1016 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
1018 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1019 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1022 if (*status == STATUS_UNCORRECTABLE) {
1023 mtd->ecc_stats.failed++;
1026 mtd->ecc_stats.corrected += *status;
1027 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1032 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1033 * for details about our policy for delivering the OOB.
1035 * We fill the caller's buffer with set bits, and then copy the
1036 * block mark to th caller's buffer. Note that, if block mark
1037 * swapping was necessary, it has already been done, so we can
1038 * rely on the first byte of the auxiliary buffer to contain
1041 memset(chip->oob_poi, ~0, mtd->oobsize);
1042 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
1045 read_page_swap_end(this, buf, nfc_geo->payload_size,
1046 this->payload_virt, this->payload_phys,
1047 nfc_geo->payload_size,
1048 payload_virt, payload_phys);
1050 return max_bitflips;
1053 /* Fake a virtual small page for the subpage read */
1054 static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1055 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1057 struct gpmi_nand_data *this = chip->priv;
1058 void __iomem *bch_regs = this->resources.bch_regs;
1059 struct bch_geometry old_geo = this->bch_geometry;
1060 struct bch_geometry *geo = &this->bch_geometry;
1061 int size = chip->ecc.size; /* ECC chunk size */
1062 int meta, n, page_size;
1063 u32 r1_old, r2_old, r1_new, r2_new;
1064 unsigned int max_bitflips;
1065 int first, last, marker_pos;
1066 int ecc_parity_size;
1069 /* The size of ECC parity */
1070 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1072 /* Align it with the chunk size */
1073 first = offs / size;
1074 last = (offs + len - 1) / size;
1077 * Find the chunk which contains the Block Marker. If this chunk is
1078 * in the range of [first, last], we have to read out the whole page.
1079 * Why? since we had swapped the data at the position of Block Marker
1080 * to the metadata which is bound with the chunk 0.
1082 marker_pos = geo->block_mark_byte_offset / size;
1083 if (last >= marker_pos && first <= marker_pos) {
1084 dev_dbg(this->dev, "page:%d, first:%d, last:%d, marker at:%d\n",
1085 page, first, last, marker_pos);
1086 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1089 meta = geo->metadata_size;
1091 col = meta + (size + ecc_parity_size) * first;
1092 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, col, -1);
1095 buf = buf + first * size;
1098 /* Save the old environment */
1099 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1100 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1102 /* change the BCH registers and bch_geometry{} */
1103 n = last - first + 1;
1104 page_size = meta + (size + ecc_parity_size) * n;
1106 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1107 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1108 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1109 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1110 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1112 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1113 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1114 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1116 geo->ecc_chunk_count = n;
1117 geo->payload_size = n * size;
1118 geo->page_size = page_size;
1119 geo->auxiliary_status_offset = ALIGN(meta, 4);
1121 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1122 page, offs, len, col, first, n, page_size);
1124 /* Read the subpage now */
1125 this->swap_block_mark = false;
1126 max_bitflips = gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1129 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1130 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1131 this->bch_geometry = old_geo;
1132 this->swap_block_mark = true;
1134 return max_bitflips;
1137 static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1138 const uint8_t *buf, int oob_required)
1140 struct gpmi_nand_data *this = chip->priv;
1141 struct bch_geometry *nfc_geo = &this->bch_geometry;
1142 const void *payload_virt;
1143 dma_addr_t payload_phys;
1144 const void *auxiliary_virt;
1145 dma_addr_t auxiliary_phys;
1148 dev_dbg(this->dev, "ecc write page.\n");
1149 if (this->swap_block_mark) {
1151 * If control arrives here, we're doing block mark swapping.
1152 * Since we can't modify the caller's buffers, we must copy them
1155 memcpy(this->payload_virt, buf, mtd->writesize);
1156 payload_virt = this->payload_virt;
1157 payload_phys = this->payload_phys;
1159 memcpy(this->auxiliary_virt, chip->oob_poi,
1160 nfc_geo->auxiliary_size);
1161 auxiliary_virt = this->auxiliary_virt;
1162 auxiliary_phys = this->auxiliary_phys;
1164 /* Handle block mark swapping. */
1165 block_mark_swapping(this,
1166 (void *) payload_virt, (void *) auxiliary_virt);
1169 * If control arrives here, we're not doing block mark swapping,
1170 * so we can to try and use the caller's buffers.
1172 ret = send_page_prepare(this,
1173 buf, mtd->writesize,
1174 this->payload_virt, this->payload_phys,
1175 nfc_geo->payload_size,
1176 &payload_virt, &payload_phys);
1178 dev_err(this->dev, "Inadequate payload DMA buffer\n");
1182 ret = send_page_prepare(this,
1183 chip->oob_poi, mtd->oobsize,
1184 this->auxiliary_virt, this->auxiliary_phys,
1185 nfc_geo->auxiliary_size,
1186 &auxiliary_virt, &auxiliary_phys);
1188 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
1189 goto exit_auxiliary;
1194 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1196 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
1198 if (!this->swap_block_mark) {
1199 send_page_end(this, chip->oob_poi, mtd->oobsize,
1200 this->auxiliary_virt, this->auxiliary_phys,
1201 nfc_geo->auxiliary_size,
1202 auxiliary_virt, auxiliary_phys);
1204 send_page_end(this, buf, mtd->writesize,
1205 this->payload_virt, this->payload_phys,
1206 nfc_geo->payload_size,
1207 payload_virt, payload_phys);
1214 * There are several places in this driver where we have to handle the OOB and
1215 * block marks. This is the function where things are the most complicated, so
1216 * this is where we try to explain it all. All the other places refer back to
1219 * These are the rules, in order of decreasing importance:
1221 * 1) Nothing the caller does can be allowed to imperil the block mark.
1223 * 2) In read operations, the first byte of the OOB we return must reflect the
1224 * true state of the block mark, no matter where that block mark appears in
1225 * the physical page.
1227 * 3) ECC-based read operations return an OOB full of set bits (since we never
1228 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1231 * 4) "Raw" read operations return a direct view of the physical bytes in the
1232 * page, using the conventional definition of which bytes are data and which
1233 * are OOB. This gives the caller a way to see the actual, physical bytes
1234 * in the page, without the distortions applied by our ECC engine.
1237 * What we do for this specific read operation depends on two questions:
1239 * 1) Are we doing a "raw" read, or an ECC-based read?
1241 * 2) Are we using block mark swapping or transcription?
1243 * There are four cases, illustrated by the following Karnaugh map:
1245 * | Raw | ECC-based |
1246 * -------------+-------------------------+-------------------------+
1247 * | Read the conventional | |
1248 * | OOB at the end of the | |
1249 * Swapping | page and return it. It | |
1250 * | contains exactly what | |
1251 * | we want. | Read the block mark and |
1252 * -------------+-------------------------+ return it in a buffer |
1253 * | Read the conventional | full of set bits. |
1254 * | OOB at the end of the | |
1255 * | page and also the block | |
1256 * Transcribing | mark in the metadata. | |
1257 * | Copy the block mark | |
1258 * | into the first byte of | |
1260 * -------------+-------------------------+-------------------------+
1262 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1263 * giving an accurate view of the actual, physical bytes in the page (we're
1264 * overwriting the block mark). That's OK because it's more important to follow
1267 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1268 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1269 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1270 * ECC-based or raw view of the page is implicit in which function it calls
1271 * (there is a similar pair of ECC-based/raw functions for writing).
1273 * FIXME: The following paragraph is incorrect, now that there exist
1274 * ecc.read_oob_raw and ecc.write_oob_raw functions.
1276 * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1277 * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1278 * caller wants an ECC-based or raw view of the page is not propagated down to
1281 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1284 struct gpmi_nand_data *this = chip->priv;
1286 dev_dbg(this->dev, "page number is %d\n", page);
1287 /* clear the OOB buffer */
1288 memset(chip->oob_poi, ~0, mtd->oobsize);
1290 /* Read out the conventional OOB. */
1291 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1292 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1295 * Now, we want to make sure the block mark is correct. In the
1296 * Swapping/Raw case, we already have it. Otherwise, we need to
1297 * explicitly read it.
1299 if (!this->swap_block_mark) {
1300 /* Read the block mark into the first byte of the OOB buffer. */
1301 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1302 chip->oob_poi[0] = chip->read_byte(mtd);
1309 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1311 struct nand_oobfree *of = mtd->ecclayout->oobfree;
1314 /* Do we have available oob area? */
1318 if (!nand_is_slc(chip))
1321 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
1322 chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
1323 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1325 status = chip->waitfunc(mtd, chip);
1326 return status & NAND_STATUS_FAIL ? -EIO : 0;
1329 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1331 struct nand_chip *chip = mtd->priv;
1332 struct gpmi_nand_data *this = chip->priv;
1334 uint8_t *block_mark;
1335 int column, page, status, chipnr;
1337 chipnr = (int)(ofs >> chip->chip_shift);
1338 chip->select_chip(mtd, chipnr);
1340 column = this->swap_block_mark ? mtd->writesize : 0;
1342 /* Write the block mark. */
1343 block_mark = this->data_buffer_dma;
1344 block_mark[0] = 0; /* bad block marker */
1346 /* Shift to get page */
1347 page = (int)(ofs >> chip->page_shift);
1349 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1350 chip->write_buf(mtd, block_mark, 1);
1351 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1353 status = chip->waitfunc(mtd, chip);
1354 if (status & NAND_STATUS_FAIL)
1357 chip->select_chip(mtd, -1);
1362 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1364 struct boot_rom_geometry *geometry = &this->rom_geometry;
1367 * Set the boot block stride size.
1369 * In principle, we should be reading this from the OTP bits, since
1370 * that's where the ROM is going to get it. In fact, we don't have any
1371 * way to read the OTP bits, so we go with the default and hope for the
1374 geometry->stride_size_in_pages = 64;
1377 * Set the search area stride exponent.
1379 * In principle, we should be reading this from the OTP bits, since
1380 * that's where the ROM is going to get it. In fact, we don't have any
1381 * way to read the OTP bits, so we go with the default and hope for the
1384 geometry->search_area_stride_exponent = 2;
1388 static const char *fingerprint = "STMP";
1389 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1391 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1392 struct device *dev = this->dev;
1393 struct mtd_info *mtd = &this->mtd;
1394 struct nand_chip *chip = &this->nand;
1395 unsigned int search_area_size_in_strides;
1396 unsigned int stride;
1398 uint8_t *buffer = chip->buffers->databuf;
1399 int saved_chip_number;
1400 int found_an_ncb_fingerprint = false;
1402 /* Compute the number of strides in a search area. */
1403 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1405 saved_chip_number = this->current_chip;
1406 chip->select_chip(mtd, 0);
1409 * Loop through the first search area, looking for the NCB fingerprint.
1411 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1413 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1414 /* Compute the page addresses. */
1415 page = stride * rom_geo->stride_size_in_pages;
1417 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1420 * Read the NCB fingerprint. The fingerprint is four bytes long
1421 * and starts in the 12th byte of the page.
1423 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1424 chip->read_buf(mtd, buffer, strlen(fingerprint));
1426 /* Look for the fingerprint. */
1427 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1428 found_an_ncb_fingerprint = true;
1434 chip->select_chip(mtd, saved_chip_number);
1436 if (found_an_ncb_fingerprint)
1437 dev_dbg(dev, "\tFound a fingerprint\n");
1439 dev_dbg(dev, "\tNo fingerprint found\n");
1440 return found_an_ncb_fingerprint;
1443 /* Writes a transcription stamp. */
1444 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1446 struct device *dev = this->dev;
1447 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1448 struct mtd_info *mtd = &this->mtd;
1449 struct nand_chip *chip = &this->nand;
1450 unsigned int block_size_in_pages;
1451 unsigned int search_area_size_in_strides;
1452 unsigned int search_area_size_in_pages;
1453 unsigned int search_area_size_in_blocks;
1455 unsigned int stride;
1457 uint8_t *buffer = chip->buffers->databuf;
1458 int saved_chip_number;
1461 /* Compute the search area geometry. */
1462 block_size_in_pages = mtd->erasesize / mtd->writesize;
1463 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1464 search_area_size_in_pages = search_area_size_in_strides *
1465 rom_geo->stride_size_in_pages;
1466 search_area_size_in_blocks =
1467 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1468 block_size_in_pages;
1470 dev_dbg(dev, "Search Area Geometry :\n");
1471 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1472 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1473 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1475 /* Select chip 0. */
1476 saved_chip_number = this->current_chip;
1477 chip->select_chip(mtd, 0);
1479 /* Loop over blocks in the first search area, erasing them. */
1480 dev_dbg(dev, "Erasing the search area...\n");
1482 for (block = 0; block < search_area_size_in_blocks; block++) {
1483 /* Compute the page address. */
1484 page = block * block_size_in_pages;
1486 /* Erase this block. */
1487 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1488 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1489 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1491 /* Wait for the erase to finish. */
1492 status = chip->waitfunc(mtd, chip);
1493 if (status & NAND_STATUS_FAIL)
1494 dev_err(dev, "[%s] Erase failed.\n", __func__);
1497 /* Write the NCB fingerprint into the page buffer. */
1498 memset(buffer, ~0, mtd->writesize);
1499 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1501 /* Loop through the first search area, writing NCB fingerprints. */
1502 dev_dbg(dev, "Writing NCB fingerprints...\n");
1503 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1504 /* Compute the page addresses. */
1505 page = stride * rom_geo->stride_size_in_pages;
1507 /* Write the first page of the current stride. */
1508 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1509 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1510 chip->ecc.write_page_raw(mtd, chip, buffer, 0);
1511 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1513 /* Wait for the write to finish. */
1514 status = chip->waitfunc(mtd, chip);
1515 if (status & NAND_STATUS_FAIL)
1516 dev_err(dev, "[%s] Write failed.\n", __func__);
1519 /* Deselect chip 0. */
1520 chip->select_chip(mtd, saved_chip_number);
1524 static int mx23_boot_init(struct gpmi_nand_data *this)
1526 struct device *dev = this->dev;
1527 struct nand_chip *chip = &this->nand;
1528 struct mtd_info *mtd = &this->mtd;
1529 unsigned int block_count;
1538 * If control arrives here, we can't use block mark swapping, which
1539 * means we're forced to use transcription. First, scan for the
1540 * transcription stamp. If we find it, then we don't have to do
1541 * anything -- the block marks are already transcribed.
1543 if (mx23_check_transcription_stamp(this))
1547 * If control arrives here, we couldn't find a transcription stamp, so
1548 * so we presume the block marks are in the conventional location.
1550 dev_dbg(dev, "Transcribing bad block marks...\n");
1552 /* Compute the number of blocks in the entire medium. */
1553 block_count = chip->chipsize >> chip->phys_erase_shift;
1556 * Loop over all the blocks in the medium, transcribing block marks as
1559 for (block = 0; block < block_count; block++) {
1561 * Compute the chip, page and byte addresses for this block's
1562 * conventional mark.
1564 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1565 page = block << (chip->phys_erase_shift - chip->page_shift);
1566 byte = block << chip->phys_erase_shift;
1568 /* Send the command to read the conventional block mark. */
1569 chip->select_chip(mtd, chipnr);
1570 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1571 block_mark = chip->read_byte(mtd);
1572 chip->select_chip(mtd, -1);
1575 * Check if the block is marked bad. If so, we need to mark it
1576 * again, but this time the result will be a mark in the
1577 * location where we transcribe block marks.
1579 if (block_mark != 0xff) {
1580 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1581 ret = chip->block_markbad(mtd, byte);
1583 dev_err(dev, "Failed to mark block bad with "
1588 /* Write the stamp that indicates we've transcribed the block marks. */
1589 mx23_write_transcription_stamp(this);
1593 static int nand_boot_init(struct gpmi_nand_data *this)
1595 nand_boot_set_geometry(this);
1597 /* This is ROM arch-specific initilization before the BBT scanning. */
1598 if (GPMI_IS_MX23(this))
1599 return mx23_boot_init(this);
1603 static int gpmi_set_geometry(struct gpmi_nand_data *this)
1607 /* Free the temporary DMA memory for reading ID. */
1608 gpmi_free_dma_buffer(this);
1610 /* Set up the NFC geometry which is used by BCH. */
1611 ret = bch_set_geometry(this);
1613 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
1617 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1618 return gpmi_alloc_dma_buffer(this);
1621 static void gpmi_nand_exit(struct gpmi_nand_data *this)
1623 nand_release(&this->mtd);
1624 gpmi_free_dma_buffer(this);
1627 static int gpmi_init_last(struct gpmi_nand_data *this)
1629 struct mtd_info *mtd = &this->mtd;
1630 struct nand_chip *chip = mtd->priv;
1631 struct nand_ecc_ctrl *ecc = &chip->ecc;
1632 struct bch_geometry *bch_geo = &this->bch_geometry;
1635 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1636 this->swap_block_mark = !GPMI_IS_MX23(this);
1638 /* Set up the medium geometry */
1639 ret = gpmi_set_geometry(this);
1643 /* Init the nand_ecc_ctrl{} */
1644 ecc->read_page = gpmi_ecc_read_page;
1645 ecc->write_page = gpmi_ecc_write_page;
1646 ecc->read_oob = gpmi_ecc_read_oob;
1647 ecc->write_oob = gpmi_ecc_write_oob;
1648 ecc->mode = NAND_ECC_HW;
1649 ecc->size = bch_geo->ecc_chunk_size;
1650 ecc->strength = bch_geo->ecc_strength;
1651 ecc->layout = &gpmi_hw_ecclayout;
1654 * We only enable the subpage read when:
1655 * (1) the chip is imx6, and
1656 * (2) the size of the ECC parity is byte aligned.
1658 if (GPMI_IS_MX6Q(this) &&
1659 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1660 ecc->read_subpage = gpmi_ecc_read_subpage;
1661 chip->options |= NAND_SUBPAGE_READ;
1665 * Can we enable the extra features? such as EDO or Sync mode.
1667 * We do not check the return value now. That's means if we fail in
1668 * enable the extra features, we still can run in the normal way.
1670 gpmi_extra_init(this);
1675 static int gpmi_nand_init(struct gpmi_nand_data *this)
1677 struct mtd_info *mtd = &this->mtd;
1678 struct nand_chip *chip = &this->nand;
1679 struct mtd_part_parser_data ppdata = {};
1682 /* init current chip */
1683 this->current_chip = -1;
1685 /* init the MTD data structures */
1687 mtd->name = "gpmi-nand";
1688 mtd->owner = THIS_MODULE;
1690 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1692 chip->select_chip = gpmi_select_chip;
1693 chip->cmd_ctrl = gpmi_cmd_ctrl;
1694 chip->dev_ready = gpmi_dev_ready;
1695 chip->read_byte = gpmi_read_byte;
1696 chip->read_buf = gpmi_read_buf;
1697 chip->write_buf = gpmi_write_buf;
1698 chip->badblock_pattern = &gpmi_bbt_descr;
1699 chip->block_markbad = gpmi_block_markbad;
1700 chip->options |= NAND_NO_SUBPAGE_WRITE;
1701 if (of_get_nand_on_flash_bbt(this->dev->of_node))
1702 chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1705 * Allocate a temporary DMA buffer for reading ID in the
1706 * nand_scan_ident().
1708 this->bch_geometry.payload_size = 1024;
1709 this->bch_geometry.auxiliary_size = 128;
1710 ret = gpmi_alloc_dma_buffer(this);
1714 ret = nand_scan_ident(mtd, GPMI_IS_MX6Q(this) ? 2 : 1, NULL);
1718 ret = gpmi_init_last(this);
1722 chip->options |= NAND_SKIP_BBTSCAN;
1723 ret = nand_scan_tail(mtd);
1727 ret = nand_boot_init(this);
1730 chip->scan_bbt(mtd);
1732 ppdata.of_node = this->pdev->dev.of_node;
1733 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
1739 gpmi_nand_exit(this);
1743 static const struct platform_device_id gpmi_ids[] = {
1744 { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
1745 { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
1746 { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
1750 static const struct of_device_id gpmi_nand_id_table[] = {
1752 .compatible = "fsl,imx23-gpmi-nand",
1753 .data = (void *)&gpmi_ids[IS_MX23],
1755 .compatible = "fsl,imx28-gpmi-nand",
1756 .data = (void *)&gpmi_ids[IS_MX28],
1758 .compatible = "fsl,imx6q-gpmi-nand",
1759 .data = (void *)&gpmi_ids[IS_MX6Q],
1762 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1764 static int gpmi_nand_probe(struct platform_device *pdev)
1766 struct gpmi_nand_data *this;
1767 const struct of_device_id *of_id;
1770 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1772 pdev->id_entry = of_id->data;
1774 dev_err(&pdev->dev, "Failed to find the right device id.\n");
1778 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1782 platform_set_drvdata(pdev, this);
1784 this->dev = &pdev->dev;
1786 ret = acquire_resources(this);
1788 goto exit_acquire_resources;
1790 ret = init_hardware(this);
1794 ret = gpmi_nand_init(this);
1798 dev_info(this->dev, "driver registered.\n");
1803 release_resources(this);
1804 exit_acquire_resources:
1805 dev_err(this->dev, "driver registration failed: %d\n", ret);
1810 static int gpmi_nand_remove(struct platform_device *pdev)
1812 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1814 gpmi_nand_exit(this);
1815 release_resources(this);
1819 static struct platform_driver gpmi_nand_driver = {
1821 .name = "gpmi-nand",
1822 .of_match_table = gpmi_nand_id_table,
1824 .probe = gpmi_nand_probe,
1825 .remove = gpmi_nand_remove,
1826 .id_table = gpmi_ids,
1828 module_platform_driver(gpmi_nand_driver);
1830 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1831 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1832 MODULE_LICENSE("GPL");