1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale GPMI NAND Flash Driver
5 * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
6 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
9 #include <linux/slab.h>
10 #include <linux/sched/task_stack.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mtd/partitions.h>
15 #include <linux/of_device.h>
16 #include "gpmi-nand.h"
19 /* Resource names for the GPMI NAND driver. */
20 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
21 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
22 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
24 /* add our owner bbt descriptor */
25 static uint8_t scan_ff_pattern[] = { 0xff };
26 static struct nand_bbt_descr gpmi_bbt_descr = {
30 .pattern = scan_ff_pattern
34 * We may change the layout if we can get the ECC info from the datasheet,
35 * else we will use all the (page + OOB).
37 static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
38 struct mtd_oob_region *oobregion)
40 struct nand_chip *chip = mtd_to_nand(mtd);
41 struct gpmi_nand_data *this = nand_get_controller_data(chip);
42 struct bch_geometry *geo = &this->bch_geometry;
47 oobregion->offset = 0;
48 oobregion->length = geo->page_size - mtd->writesize;
53 static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
54 struct mtd_oob_region *oobregion)
56 struct nand_chip *chip = mtd_to_nand(mtd);
57 struct gpmi_nand_data *this = nand_get_controller_data(chip);
58 struct bch_geometry *geo = &this->bch_geometry;
63 /* The available oob size we have. */
64 if (geo->page_size < mtd->writesize + mtd->oobsize) {
65 oobregion->offset = geo->page_size - mtd->writesize;
66 oobregion->length = mtd->oobsize - oobregion->offset;
72 static const char * const gpmi_clks_for_mx2x[] = {
76 static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
77 .ecc = gpmi_ooblayout_ecc,
78 .free = gpmi_ooblayout_free,
81 static const struct gpmi_devdata gpmi_devdata_imx23 = {
83 .bch_max_ecc_strength = 20,
84 .max_chain_delay = 16000,
85 .clks = gpmi_clks_for_mx2x,
86 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
89 static const struct gpmi_devdata gpmi_devdata_imx28 = {
91 .bch_max_ecc_strength = 20,
92 .max_chain_delay = 16000,
93 .clks = gpmi_clks_for_mx2x,
94 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
97 static const char * const gpmi_clks_for_mx6[] = {
98 "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
101 static const struct gpmi_devdata gpmi_devdata_imx6q = {
103 .bch_max_ecc_strength = 40,
104 .max_chain_delay = 12000,
105 .clks = gpmi_clks_for_mx6,
106 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
109 static const struct gpmi_devdata gpmi_devdata_imx6sx = {
111 .bch_max_ecc_strength = 62,
112 .max_chain_delay = 12000,
113 .clks = gpmi_clks_for_mx6,
114 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
117 static const char * const gpmi_clks_for_mx7d[] = {
118 "gpmi_io", "gpmi_bch_apb",
121 static const struct gpmi_devdata gpmi_devdata_imx7d = {
123 .bch_max_ecc_strength = 62,
124 .max_chain_delay = 12000,
125 .clks = gpmi_clks_for_mx7d,
126 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
129 static irqreturn_t bch_irq(int irq, void *cookie)
131 struct gpmi_nand_data *this = cookie;
133 gpmi_clear_bch(this);
134 complete(&this->bch_done);
139 * Calculate the ECC strength by hand:
140 * E : The ECC strength.
141 * G : the length of Galois Field.
142 * N : The chunk count of per page.
143 * O : the oobsize of the NAND chip.
144 * M : the metasize of per page.
148 * ------------ <= (O - M)
156 static inline int get_ecc_strength(struct gpmi_nand_data *this)
158 struct bch_geometry *geo = &this->bch_geometry;
159 struct mtd_info *mtd = nand_to_mtd(&this->nand);
162 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
163 / (geo->gf_len * geo->ecc_chunk_count);
165 /* We need the minor even number. */
166 return round_down(ecc_strength, 2);
169 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
171 struct bch_geometry *geo = &this->bch_geometry;
173 /* Do the sanity check. */
174 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
175 /* The mx23/mx28 only support the GF13. */
176 if (geo->gf_len == 14)
179 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
183 * If we can get the ECC information from the nand chip, we do not
184 * need to calculate them ourselves.
186 * We may have available oob space in this case.
188 static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
189 unsigned int ecc_strength,
190 unsigned int ecc_step)
192 struct bch_geometry *geo = &this->bch_geometry;
193 struct nand_chip *chip = &this->nand;
194 struct mtd_info *mtd = nand_to_mtd(chip);
195 unsigned int block_mark_bit_offset;
206 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
207 chip->ecc_strength_ds, chip->ecc_step_ds);
210 geo->ecc_chunk_size = ecc_step;
211 geo->ecc_strength = round_up(ecc_strength, 2);
212 if (!gpmi_check_ecc(this))
215 /* Keep the C >= O */
216 if (geo->ecc_chunk_size < mtd->oobsize) {
218 "unsupported nand chip. ecc size: %d, oob size : %d\n",
219 ecc_step, mtd->oobsize);
223 /* The default value, see comment in the legacy_set_geometry(). */
224 geo->metadata_size = 10;
226 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
229 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
232 * |<----------------------------------------------------->|
236 * |<-------------------------------------------->| D | | O' |
239 * +---+----------+-+----------+-+----------+-+----------+-+-----+
240 * | M | data |E| data |E| data |E| data |E| |
241 * +---+----------+-+----------+-+----------+-+----------+-+-----+
247 * P : the page size for BCH module.
248 * E : The ECC strength.
249 * G : the length of Galois Field.
250 * N : The chunk count of per page.
251 * M : the metasize of per page.
252 * C : the ecc chunk size, aka the "data" above.
253 * P': the nand chip's page size.
254 * O : the nand chip's oob size.
257 * The formula for P is :
260 * P = ------------ + P' + M
263 * The position of block mark moves forward in the ECC-based view
264 * of page, and the delta is:
267 * D = (---------------- + M)
270 * Please see the comment in legacy_set_geometry().
271 * With the condition C >= O , we still can get same result.
272 * So the bit position of the physical block mark within the ECC-based
273 * view of the page is :
276 geo->page_size = mtd->writesize + geo->metadata_size +
277 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
279 geo->payload_size = mtd->writesize;
281 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
282 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
283 + ALIGN(geo->ecc_chunk_count, 4);
285 if (!this->swap_block_mark)
289 block_mark_bit_offset = mtd->writesize * 8 -
290 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
291 + geo->metadata_size * 8);
293 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
294 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
298 static int legacy_set_geometry(struct gpmi_nand_data *this)
300 struct bch_geometry *geo = &this->bch_geometry;
301 struct mtd_info *mtd = nand_to_mtd(&this->nand);
302 unsigned int metadata_size;
303 unsigned int status_size;
304 unsigned int block_mark_bit_offset;
307 * The size of the metadata can be changed, though we set it to 10
308 * bytes now. But it can't be too large, because we have to save
309 * enough space for BCH.
311 geo->metadata_size = 10;
313 /* The default for the length of Galois Field. */
316 /* The default for chunk size. */
317 geo->ecc_chunk_size = 512;
318 while (geo->ecc_chunk_size < mtd->oobsize) {
319 geo->ecc_chunk_size *= 2; /* keep C >= O */
323 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
325 /* We use the same ECC strength for all chunks. */
326 geo->ecc_strength = get_ecc_strength(this);
327 if (!gpmi_check_ecc(this)) {
329 "ecc strength: %d cannot be supported by the controller (%d)\n"
330 "try to use minimum ecc strength that NAND chip required\n",
332 this->devdata->bch_max_ecc_strength);
336 geo->page_size = mtd->writesize + geo->metadata_size +
337 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
338 geo->payload_size = mtd->writesize;
341 * The auxiliary buffer contains the metadata and the ECC status. The
342 * metadata is padded to the nearest 32-bit boundary. The ECC status
343 * contains one byte for every ECC chunk, and is also padded to the
344 * nearest 32-bit boundary.
346 metadata_size = ALIGN(geo->metadata_size, 4);
347 status_size = ALIGN(geo->ecc_chunk_count, 4);
349 geo->auxiliary_size = metadata_size + status_size;
350 geo->auxiliary_status_offset = metadata_size;
352 if (!this->swap_block_mark)
356 * We need to compute the byte and bit offsets of
357 * the physical block mark within the ECC-based view of the page.
359 * NAND chip with 2K page shows below:
365 * +---+----------+-+----------+-+----------+-+----------+-+
366 * | M | data |E| data |E| data |E| data |E|
367 * +---+----------+-+----------+-+----------+-+----------+-+
369 * The position of block mark moves forward in the ECC-based view
370 * of page, and the delta is:
373 * D = (---------------- + M)
376 * With the formula to compute the ECC strength, and the condition
377 * : C >= O (C is the ecc chunk size)
379 * It's easy to deduce to the following result:
381 * E * G (O - M) C - M C - M
382 * ----------- <= ------- <= -------- < ---------
388 * D = (---------------- + M) < C
391 * The above inequality means the position of block mark
392 * within the ECC-based view of the page is still in the data chunk,
393 * and it's NOT in the ECC bits of the chunk.
395 * Use the following to compute the bit position of the
396 * physical block mark within the ECC-based view of the page:
397 * (page_size - D) * 8
401 block_mark_bit_offset = mtd->writesize * 8 -
402 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
403 + geo->metadata_size * 8);
405 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
406 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
410 int common_nfc_set_geometry(struct gpmi_nand_data *this)
412 struct nand_chip *chip = &this->nand;
414 if (chip->ecc.strength > 0 && chip->ecc.size > 0)
415 return set_geometry_by_ecc_info(this, chip->ecc.strength,
418 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
419 || legacy_set_geometry(this)) {
420 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
423 return set_geometry_by_ecc_info(this, chip->ecc_strength_ds,
430 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
432 /* We use the DMA channel 0 to access all the nand chips. */
433 return this->dma_chans[0];
436 /* Can we use the upper's buffer directly for DMA? */
437 bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, int len,
438 enum dma_data_direction dr)
440 struct scatterlist *sgl = &this->data_sgl;
443 /* first try to map the upper buffer directly */
444 if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
445 sg_init_one(sgl, buf, len);
446 ret = dma_map_sg(this->dev, sgl, 1, dr);
454 /* We have to use our own DMA buffer. */
455 sg_init_one(sgl, this->data_buffer_dma, len);
457 if (dr == DMA_TO_DEVICE)
458 memcpy(this->data_buffer_dma, buf, len);
460 dma_map_sg(this->dev, sgl, 1, dr);
465 /* This will be called after the DMA operation is finished. */
466 static void dma_irq_callback(void *param)
468 struct gpmi_nand_data *this = param;
469 struct completion *dma_c = &this->dma_done;
474 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
475 struct dma_async_tx_descriptor *desc)
477 struct completion *dma_c = &this->dma_done;
478 unsigned long timeout;
480 init_completion(dma_c);
482 desc->callback = dma_irq_callback;
483 desc->callback_param = this;
484 dmaengine_submit(desc);
485 dma_async_issue_pending(get_dma_chan(this));
487 /* Wait for the interrupt from the DMA block. */
488 timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
490 dev_err(this->dev, "DMA timeout, last DMA\n");
491 gpmi_dump_info(this);
498 * This function is used in BCH reading or BCH writing pages.
499 * It will wait for the BCH interrupt as long as ONE second.
500 * Actually, we must wait for two interrupts :
501 * [1] firstly the DMA interrupt and
502 * [2] secondly the BCH interrupt.
504 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
505 struct dma_async_tx_descriptor *desc)
507 struct completion *bch_c = &this->bch_done;
508 unsigned long timeout;
510 /* Prepare to receive an interrupt from the BCH block. */
511 init_completion(bch_c);
514 start_dma_without_bch_irq(this, desc);
516 /* Wait for the interrupt from the BCH block. */
517 timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
519 dev_err(this->dev, "BCH timeout\n");
520 gpmi_dump_info(this);
526 static int acquire_register_block(struct gpmi_nand_data *this,
527 const char *res_name)
529 struct platform_device *pdev = this->pdev;
530 struct resources *res = &this->resources;
534 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
535 p = devm_ioremap_resource(&pdev->dev, r);
539 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
541 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
544 dev_err(this->dev, "unknown resource name : %s\n", res_name);
549 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
551 struct platform_device *pdev = this->pdev;
552 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
556 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
558 dev_err(this->dev, "Can't get resource for %s\n", res_name);
562 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
564 dev_err(this->dev, "error requesting BCH IRQ\n");
569 static void release_dma_channels(struct gpmi_nand_data *this)
572 for (i = 0; i < DMA_CHANS; i++)
573 if (this->dma_chans[i]) {
574 dma_release_channel(this->dma_chans[i]);
575 this->dma_chans[i] = NULL;
579 static int acquire_dma_channels(struct gpmi_nand_data *this)
581 struct platform_device *pdev = this->pdev;
582 struct dma_chan *dma_chan;
584 /* request dma channel */
585 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
587 dev_err(this->dev, "Failed to request DMA channel.\n");
591 this->dma_chans[0] = dma_chan;
595 release_dma_channels(this);
599 static int gpmi_get_clks(struct gpmi_nand_data *this)
601 struct resources *r = &this->resources;
605 for (i = 0; i < this->devdata->clks_count; i++) {
606 clk = devm_clk_get(this->dev, this->devdata->clks[i]);
618 dev_dbg(this->dev, "failed in finding the clocks.\n");
622 static int acquire_resources(struct gpmi_nand_data *this)
626 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
630 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
634 ret = acquire_bch_irq(this, bch_irq);
638 ret = acquire_dma_channels(this);
642 ret = gpmi_get_clks(this);
648 release_dma_channels(this);
653 static void release_resources(struct gpmi_nand_data *this)
655 release_dma_channels(this);
658 static int send_page_prepare(struct gpmi_nand_data *this,
659 const void *source, unsigned length,
660 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
661 const void **use_virt, dma_addr_t *use_phys)
663 struct device *dev = this->dev;
665 if (virt_addr_valid(source)) {
666 dma_addr_t source_phys;
668 source_phys = dma_map_single(dev, (void *)source, length,
670 if (dma_mapping_error(dev, source_phys)) {
671 if (alt_size < length) {
672 dev_err(dev, "Alternate buffer is too small\n");
678 *use_phys = source_phys;
683 * Copy the content of the source buffer into the alternate
684 * buffer and set up the return values accordingly.
686 memcpy(alt_virt, source, length);
688 *use_virt = alt_virt;
689 *use_phys = alt_phys;
693 static void send_page_end(struct gpmi_nand_data *this,
694 const void *source, unsigned length,
695 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
696 const void *used_virt, dma_addr_t used_phys)
698 struct device *dev = this->dev;
699 if (used_virt == source)
700 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
703 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
705 struct device *dev = this->dev;
707 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
708 dma_free_coherent(dev, this->page_buffer_size,
709 this->page_buffer_virt,
710 this->page_buffer_phys);
711 kfree(this->cmd_buffer);
712 kfree(this->data_buffer_dma);
713 kfree(this->raw_buffer);
715 this->cmd_buffer = NULL;
716 this->data_buffer_dma = NULL;
717 this->raw_buffer = NULL;
718 this->page_buffer_virt = NULL;
719 this->page_buffer_size = 0;
722 /* Allocate the DMA buffers */
723 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
725 struct bch_geometry *geo = &this->bch_geometry;
726 struct device *dev = this->dev;
727 struct mtd_info *mtd = nand_to_mtd(&this->nand);
729 /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
730 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
731 if (this->cmd_buffer == NULL)
735 * [2] Allocate a read/write data buffer.
736 * The gpmi_alloc_dma_buffer can be called twice.
737 * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
738 * is called before the NAND identification; and we allocate a
739 * buffer of the real NAND page size when the gpmi_alloc_dma_buffer
742 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
743 GFP_DMA | GFP_KERNEL);
744 if (this->data_buffer_dma == NULL)
748 * [3] Allocate the page buffer.
750 * Both the payload buffer and the auxiliary buffer must appear on
751 * 32-bit boundaries. We presume the size of the payload buffer is a
752 * power of two and is much larger than four, which guarantees the
753 * auxiliary buffer will appear on a 32-bit boundary.
755 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
756 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
757 &this->page_buffer_phys, GFP_DMA);
758 if (!this->page_buffer_virt)
761 this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
762 if (!this->raw_buffer)
765 /* Slice up the page buffer. */
766 this->payload_virt = this->page_buffer_virt;
767 this->payload_phys = this->page_buffer_phys;
768 this->auxiliary_virt = this->payload_virt + geo->payload_size;
769 this->auxiliary_phys = this->payload_phys + geo->payload_size;
773 gpmi_free_dma_buffer(this);
777 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
779 struct nand_chip *chip = mtd_to_nand(mtd);
780 struct gpmi_nand_data *this = nand_get_controller_data(chip);
784 * Every operation begins with a command byte and a series of zero or
785 * more address bytes. These are distinguished by either the Address
786 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
787 * asserted. When MTD is ready to execute the command, it will deassert
788 * both latch enables.
790 * Rather than run a separate DMA operation for every single byte, we
791 * queue them up and run a single DMA operation for the entire series
792 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
794 if ((ctrl & (NAND_ALE | NAND_CLE))) {
795 if (data != NAND_CMD_NONE)
796 this->cmd_buffer[this->command_length++] = data;
800 if (!this->command_length)
803 ret = gpmi_send_command(this);
805 dev_err(this->dev, "Chip: %u, Error %d\n",
806 this->current_chip, ret);
808 this->command_length = 0;
811 static int gpmi_dev_ready(struct mtd_info *mtd)
813 struct nand_chip *chip = mtd_to_nand(mtd);
814 struct gpmi_nand_data *this = nand_get_controller_data(chip);
816 return gpmi_is_ready(this, this->current_chip);
819 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
821 struct nand_chip *chip = mtd_to_nand(mtd);
822 struct gpmi_nand_data *this = nand_get_controller_data(chip);
826 * For power consumption matters, disable/enable the clock each time a
827 * die is selected/unselected.
829 if (this->current_chip < 0 && chipnr >= 0) {
830 ret = gpmi_enable_clk(this);
832 dev_err(this->dev, "Failed to enable the clock\n");
833 } else if (this->current_chip >= 0 && chipnr < 0) {
834 ret = gpmi_disable_clk(this);
836 dev_err(this->dev, "Failed to disable the clock\n");
840 * This driver currently supports only one NAND chip. Plus, dies share
841 * the same configuration. So once timings have been applied on the
842 * controller side, they will not change anymore. When the time will
843 * come, the check on must_apply_timings will have to be dropped.
845 if (chipnr >= 0 && this->hw.must_apply_timings) {
846 this->hw.must_apply_timings = false;
847 gpmi_nfc_apply_timings(this);
850 this->current_chip = chipnr;
853 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
855 struct nand_chip *chip = mtd_to_nand(mtd);
856 struct gpmi_nand_data *this = nand_get_controller_data(chip);
858 dev_dbg(this->dev, "len is %d\n", len);
860 gpmi_read_data(this, buf, len);
863 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
865 struct nand_chip *chip = mtd_to_nand(mtd);
866 struct gpmi_nand_data *this = nand_get_controller_data(chip);
868 dev_dbg(this->dev, "len is %d\n", len);
870 gpmi_send_data(this, buf, len);
873 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
875 struct nand_chip *chip = mtd_to_nand(mtd);
876 struct gpmi_nand_data *this = nand_get_controller_data(chip);
877 uint8_t *buf = this->data_buffer_dma;
879 gpmi_read_buf(mtd, buf, 1);
884 * Handles block mark swapping.
885 * It can be called in swapping the block mark, or swapping it back,
886 * because the the operations are the same.
888 static void block_mark_swapping(struct gpmi_nand_data *this,
889 void *payload, void *auxiliary)
891 struct bch_geometry *nfc_geo = &this->bch_geometry;
896 unsigned char from_data;
897 unsigned char from_oob;
899 if (!this->swap_block_mark)
903 * If control arrives here, we're swapping. Make some convenience
906 bit = nfc_geo->block_mark_bit_offset;
907 p = payload + nfc_geo->block_mark_byte_offset;
911 * Get the byte from the data area that overlays the block mark. Since
912 * the ECC engine applies its own view to the bits in the page, the
913 * physical block mark won't (in general) appear on a byte boundary in
916 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
918 /* Get the byte from the OOB. */
924 mask = (0x1 << bit) - 1;
925 p[0] = (p[0] & mask) | (from_oob << bit);
928 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
931 static int gpmi_ecc_read_page_data(struct nand_chip *chip,
932 uint8_t *buf, int oob_required,
935 struct gpmi_nand_data *this = nand_get_controller_data(chip);
936 struct bch_geometry *nfc_geo = &this->bch_geometry;
937 struct mtd_info *mtd = nand_to_mtd(chip);
938 dma_addr_t payload_phys;
940 unsigned char *status;
941 unsigned int max_bitflips = 0;
945 dev_dbg(this->dev, "page number is : %d\n", page);
947 payload_phys = this->payload_phys;
949 if (virt_addr_valid(buf)) {
950 dma_addr_t dest_phys;
952 dest_phys = dma_map_single(this->dev, buf, nfc_geo->payload_size,
954 if (!dma_mapping_error(this->dev, dest_phys)) {
955 payload_phys = dest_phys;
961 ret = gpmi_read_page(this, payload_phys, this->auxiliary_phys);
964 dma_unmap_single(this->dev, payload_phys, nfc_geo->payload_size,
968 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
972 /* Loop over status bytes, accumulating ECC status. */
973 status = this->auxiliary_virt + nfc_geo->auxiliary_status_offset;
976 memcpy(buf, this->payload_virt, nfc_geo->payload_size);
978 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
979 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
982 if (*status == STATUS_UNCORRECTABLE) {
983 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
984 u8 *eccbuf = this->raw_buffer;
985 int offset, bitoffset;
989 /* Read ECC bytes into our internal raw_buffer */
990 offset = nfc_geo->metadata_size * 8;
991 offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
993 bitoffset = offset % 8;
994 eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
997 nand_change_read_column_op(chip, offset, eccbuf,
1001 * ECC data are not byte aligned and we may have
1002 * in-band data in the first and last byte of
1003 * eccbuf. Set non-eccbits to one so that
1004 * nand_check_erased_ecc_chunk() does not count them
1008 eccbuf[0] |= GENMASK(bitoffset - 1, 0);
1010 bitoffset = (bitoffset + eccbits) % 8;
1012 eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
1015 * The ECC hardware has an uncorrectable ECC status
1016 * code in case we have bitflips in an erased page. As
1017 * nothing was written into this subpage the ECC is
1018 * obviously wrong and we can not trust it. We assume
1019 * at this point that we are reading an erased page and
1020 * try to correct the bitflips in buffer up to
1021 * ecc_strength bitflips. If this is a page with random
1022 * data, we exceed this number of bitflips and have a
1023 * ECC failure. Otherwise we use the corrected buffer.
1026 /* The first block includes metadata */
1027 flips = nand_check_erased_ecc_chunk(
1028 buf + i * nfc_geo->ecc_chunk_size,
1029 nfc_geo->ecc_chunk_size,
1031 this->auxiliary_virt,
1032 nfc_geo->metadata_size,
1033 nfc_geo->ecc_strength);
1035 flips = nand_check_erased_ecc_chunk(
1036 buf + i * nfc_geo->ecc_chunk_size,
1037 nfc_geo->ecc_chunk_size,
1040 nfc_geo->ecc_strength);
1044 max_bitflips = max_t(unsigned int, max_bitflips,
1046 mtd->ecc_stats.corrected += flips;
1050 mtd->ecc_stats.failed++;
1054 mtd->ecc_stats.corrected += *status;
1055 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1058 /* handle the block mark swapping */
1059 block_mark_swapping(this, buf, this->auxiliary_virt);
1063 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1064 * for details about our policy for delivering the OOB.
1066 * We fill the caller's buffer with set bits, and then copy the
1067 * block mark to th caller's buffer. Note that, if block mark
1068 * swapping was necessary, it has already been done, so we can
1069 * rely on the first byte of the auxiliary buffer to contain
1072 memset(chip->oob_poi, ~0, mtd->oobsize);
1073 chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
1076 return max_bitflips;
1079 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1080 uint8_t *buf, int oob_required, int page)
1082 nand_read_page_op(chip, page, 0, NULL, 0);
1084 return gpmi_ecc_read_page_data(chip, buf, oob_required, page);
1087 /* Fake a virtual small page for the subpage read */
1088 static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1089 uint32_t offs, uint32_t len, uint8_t *buf, int page)
1091 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1092 void __iomem *bch_regs = this->resources.bch_regs;
1093 struct bch_geometry old_geo = this->bch_geometry;
1094 struct bch_geometry *geo = &this->bch_geometry;
1095 int size = chip->ecc.size; /* ECC chunk size */
1096 int meta, n, page_size;
1097 u32 r1_old, r2_old, r1_new, r2_new;
1098 unsigned int max_bitflips;
1099 int first, last, marker_pos;
1100 int ecc_parity_size;
1102 int old_swap_block_mark = this->swap_block_mark;
1104 /* The size of ECC parity */
1105 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1107 /* Align it with the chunk size */
1108 first = offs / size;
1109 last = (offs + len - 1) / size;
1111 if (this->swap_block_mark) {
1113 * Find the chunk which contains the Block Marker.
1114 * If this chunk is in the range of [first, last],
1115 * we have to read out the whole page.
1116 * Why? since we had swapped the data at the position of Block
1117 * Marker to the metadata which is bound with the chunk 0.
1119 marker_pos = geo->block_mark_byte_offset / size;
1120 if (last >= marker_pos && first <= marker_pos) {
1122 "page:%d, first:%d, last:%d, marker at:%d\n",
1123 page, first, last, marker_pos);
1124 return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1128 meta = geo->metadata_size;
1130 col = meta + (size + ecc_parity_size) * first;
1132 buf = buf + first * size;
1135 nand_read_page_op(chip, page, col, NULL, 0);
1137 /* Save the old environment */
1138 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1139 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1141 /* change the BCH registers and bch_geometry{} */
1142 n = last - first + 1;
1143 page_size = meta + (size + ecc_parity_size) * n;
1145 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1146 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1147 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1148 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1149 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1151 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1152 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1153 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1155 geo->ecc_chunk_count = n;
1156 geo->payload_size = n * size;
1157 geo->page_size = page_size;
1158 geo->auxiliary_status_offset = ALIGN(meta, 4);
1160 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1161 page, offs, len, col, first, n, page_size);
1163 /* Read the subpage now */
1164 this->swap_block_mark = false;
1165 max_bitflips = gpmi_ecc_read_page_data(chip, buf, 0, page);
1168 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1169 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1170 this->bch_geometry = old_geo;
1171 this->swap_block_mark = old_swap_block_mark;
1173 return max_bitflips;
1176 static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1177 const uint8_t *buf, int oob_required, int page)
1179 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1180 struct bch_geometry *nfc_geo = &this->bch_geometry;
1181 const void *payload_virt;
1182 dma_addr_t payload_phys;
1183 const void *auxiliary_virt;
1184 dma_addr_t auxiliary_phys;
1187 dev_dbg(this->dev, "ecc write page.\n");
1189 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1191 if (this->swap_block_mark) {
1193 * If control arrives here, we're doing block mark swapping.
1194 * Since we can't modify the caller's buffers, we must copy them
1197 memcpy(this->payload_virt, buf, mtd->writesize);
1198 payload_virt = this->payload_virt;
1199 payload_phys = this->payload_phys;
1201 memcpy(this->auxiliary_virt, chip->oob_poi,
1202 nfc_geo->auxiliary_size);
1203 auxiliary_virt = this->auxiliary_virt;
1204 auxiliary_phys = this->auxiliary_phys;
1206 /* Handle block mark swapping. */
1207 block_mark_swapping(this,
1208 (void *)payload_virt, (void *)auxiliary_virt);
1211 * If control arrives here, we're not doing block mark swapping,
1212 * so we can to try and use the caller's buffers.
1214 ret = send_page_prepare(this,
1215 buf, mtd->writesize,
1216 this->payload_virt, this->payload_phys,
1217 nfc_geo->payload_size,
1218 &payload_virt, &payload_phys);
1220 dev_err(this->dev, "Inadequate payload DMA buffer\n");
1224 ret = send_page_prepare(this,
1225 chip->oob_poi, mtd->oobsize,
1226 this->auxiliary_virt, this->auxiliary_phys,
1227 nfc_geo->auxiliary_size,
1228 &auxiliary_virt, &auxiliary_phys);
1230 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
1231 goto exit_auxiliary;
1236 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1238 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
1240 if (!this->swap_block_mark) {
1241 send_page_end(this, chip->oob_poi, mtd->oobsize,
1242 this->auxiliary_virt, this->auxiliary_phys,
1243 nfc_geo->auxiliary_size,
1244 auxiliary_virt, auxiliary_phys);
1246 send_page_end(this, buf, mtd->writesize,
1247 this->payload_virt, this->payload_phys,
1248 nfc_geo->payload_size,
1249 payload_virt, payload_phys);
1255 return nand_prog_page_end_op(chip);
1259 * There are several places in this driver where we have to handle the OOB and
1260 * block marks. This is the function where things are the most complicated, so
1261 * this is where we try to explain it all. All the other places refer back to
1264 * These are the rules, in order of decreasing importance:
1266 * 1) Nothing the caller does can be allowed to imperil the block mark.
1268 * 2) In read operations, the first byte of the OOB we return must reflect the
1269 * true state of the block mark, no matter where that block mark appears in
1270 * the physical page.
1272 * 3) ECC-based read operations return an OOB full of set bits (since we never
1273 * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1276 * 4) "Raw" read operations return a direct view of the physical bytes in the
1277 * page, using the conventional definition of which bytes are data and which
1278 * are OOB. This gives the caller a way to see the actual, physical bytes
1279 * in the page, without the distortions applied by our ECC engine.
1282 * What we do for this specific read operation depends on two questions:
1284 * 1) Are we doing a "raw" read, or an ECC-based read?
1286 * 2) Are we using block mark swapping or transcription?
1288 * There are four cases, illustrated by the following Karnaugh map:
1290 * | Raw | ECC-based |
1291 * -------------+-------------------------+-------------------------+
1292 * | Read the conventional | |
1293 * | OOB at the end of the | |
1294 * Swapping | page and return it. It | |
1295 * | contains exactly what | |
1296 * | we want. | Read the block mark and |
1297 * -------------+-------------------------+ return it in a buffer |
1298 * | Read the conventional | full of set bits. |
1299 * | OOB at the end of the | |
1300 * | page and also the block | |
1301 * Transcribing | mark in the metadata. | |
1302 * | Copy the block mark | |
1303 * | into the first byte of | |
1305 * -------------+-------------------------+-------------------------+
1307 * Note that we break rule #4 in the Transcribing/Raw case because we're not
1308 * giving an accurate view of the actual, physical bytes in the page (we're
1309 * overwriting the block mark). That's OK because it's more important to follow
1312 * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1313 * easy. When reading a page, for example, the NAND Flash MTD code calls our
1314 * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1315 * ECC-based or raw view of the page is implicit in which function it calls
1316 * (there is a similar pair of ECC-based/raw functions for writing).
1318 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1321 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1323 dev_dbg(this->dev, "page number is %d\n", page);
1324 /* clear the OOB buffer */
1325 memset(chip->oob_poi, ~0, mtd->oobsize);
1327 /* Read out the conventional OOB. */
1328 nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1329 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1332 * Now, we want to make sure the block mark is correct. In the
1333 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1334 * Otherwise, we need to explicitly read it.
1336 if (GPMI_IS_MX23(this)) {
1337 /* Read the block mark into the first byte of the OOB buffer. */
1338 nand_read_page_op(chip, page, 0, NULL, 0);
1339 chip->oob_poi[0] = chip->read_byte(mtd);
1346 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1348 struct mtd_oob_region of = { };
1350 /* Do we have available oob area? */
1351 mtd_ooblayout_free(mtd, 0, &of);
1355 if (!nand_is_slc(chip))
1358 return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
1359 chip->oob_poi + of.offset, of.length);
1363 * This function reads a NAND page without involving the ECC engine (no HW
1365 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1366 * inline (interleaved with payload DATA), and do not align data chunk on
1368 * We thus need to take care moving the payload data and ECC bits stored in the
1369 * page into the provided buffers, which is why we're using gpmi_copy_bits.
1371 * See set_geometry_by_ecc_info inline comments to have a full description
1372 * of the layout used by the GPMI controller.
1374 static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
1375 struct nand_chip *chip, uint8_t *buf,
1376 int oob_required, int page)
1378 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1379 struct bch_geometry *nfc_geo = &this->bch_geometry;
1380 int eccsize = nfc_geo->ecc_chunk_size;
1381 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1382 u8 *tmp_buf = this->raw_buffer;
1385 size_t oob_byte_off;
1386 uint8_t *oob = chip->oob_poi;
1389 nand_read_page_op(chip, page, 0, tmp_buf,
1390 mtd->writesize + mtd->oobsize);
1393 * If required, swap the bad block marker and the data stored in the
1394 * metadata section, so that we don't wrongly consider a block as bad.
1396 * See the layout description for a detailed explanation on why this
1399 if (this->swap_block_mark)
1400 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1403 * Copy the metadata section into the oob buffer (this section is
1404 * guaranteed to be aligned on a byte boundary).
1407 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1409 oob_bit_off = nfc_geo->metadata_size * 8;
1410 src_bit_off = oob_bit_off;
1412 /* Extract interleaved payload data and ECC bits */
1413 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1415 gpmi_copy_bits(buf, step * eccsize * 8,
1416 tmp_buf, src_bit_off,
1418 src_bit_off += eccsize * 8;
1420 /* Align last ECC block to align a byte boundary */
1421 if (step == nfc_geo->ecc_chunk_count - 1 &&
1422 (oob_bit_off + eccbits) % 8)
1423 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1426 gpmi_copy_bits(oob, oob_bit_off,
1427 tmp_buf, src_bit_off,
1430 src_bit_off += eccbits;
1431 oob_bit_off += eccbits;
1435 oob_byte_off = oob_bit_off / 8;
1437 if (oob_byte_off < mtd->oobsize)
1438 memcpy(oob + oob_byte_off,
1439 tmp_buf + mtd->writesize + oob_byte_off,
1440 mtd->oobsize - oob_byte_off);
1447 * This function writes a NAND page without involving the ECC engine (no HW
1449 * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1450 * inline (interleaved with payload DATA), and do not align data chunk on
1452 * We thus need to take care moving the OOB area at the right place in the
1453 * final page, which is why we're using gpmi_copy_bits.
1455 * See set_geometry_by_ecc_info inline comments to have a full description
1456 * of the layout used by the GPMI controller.
1458 static int gpmi_ecc_write_page_raw(struct mtd_info *mtd,
1459 struct nand_chip *chip,
1461 int oob_required, int page)
1463 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1464 struct bch_geometry *nfc_geo = &this->bch_geometry;
1465 int eccsize = nfc_geo->ecc_chunk_size;
1466 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1467 u8 *tmp_buf = this->raw_buffer;
1468 uint8_t *oob = chip->oob_poi;
1471 size_t oob_byte_off;
1475 * Initialize all bits to 1 in case we don't have a buffer for the
1476 * payload or oob data in order to leave unspecified bits of data
1477 * to their initial state.
1479 if (!buf || !oob_required)
1480 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1483 * First copy the metadata section (stored in oob buffer) at the
1484 * beginning of the page, as imposed by the GPMI layout.
1486 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1487 oob_bit_off = nfc_geo->metadata_size * 8;
1488 dst_bit_off = oob_bit_off;
1490 /* Interleave payload data and ECC bits */
1491 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1493 gpmi_copy_bits(tmp_buf, dst_bit_off,
1494 buf, step * eccsize * 8, eccsize * 8);
1495 dst_bit_off += eccsize * 8;
1497 /* Align last ECC block to align a byte boundary */
1498 if (step == nfc_geo->ecc_chunk_count - 1 &&
1499 (oob_bit_off + eccbits) % 8)
1500 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1503 gpmi_copy_bits(tmp_buf, dst_bit_off,
1504 oob, oob_bit_off, eccbits);
1506 dst_bit_off += eccbits;
1507 oob_bit_off += eccbits;
1510 oob_byte_off = oob_bit_off / 8;
1512 if (oob_required && oob_byte_off < mtd->oobsize)
1513 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1514 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1517 * If required, swap the bad block marker and the first byte of the
1518 * metadata section, so that we don't modify the bad block marker.
1520 * See the layout description for a detailed explanation on why this
1523 if (this->swap_block_mark)
1524 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1526 return nand_prog_page_op(chip, page, 0, tmp_buf,
1527 mtd->writesize + mtd->oobsize);
1530 static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1533 return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
1536 static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1539 return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
1542 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1544 struct nand_chip *chip = mtd_to_nand(mtd);
1545 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1547 uint8_t *block_mark;
1548 int column, page, chipnr;
1550 chipnr = (int)(ofs >> chip->chip_shift);
1551 chip->select_chip(mtd, chipnr);
1553 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
1555 /* Write the block mark. */
1556 block_mark = this->data_buffer_dma;
1557 block_mark[0] = 0; /* bad block marker */
1559 /* Shift to get page */
1560 page = (int)(ofs >> chip->page_shift);
1562 ret = nand_prog_page_op(chip, page, column, block_mark, 1);
1564 chip->select_chip(mtd, -1);
1569 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1571 struct boot_rom_geometry *geometry = &this->rom_geometry;
1574 * Set the boot block stride size.
1576 * In principle, we should be reading this from the OTP bits, since
1577 * that's where the ROM is going to get it. In fact, we don't have any
1578 * way to read the OTP bits, so we go with the default and hope for the
1581 geometry->stride_size_in_pages = 64;
1584 * Set the search area stride exponent.
1586 * In principle, we should be reading this from the OTP bits, since
1587 * that's where the ROM is going to get it. In fact, we don't have any
1588 * way to read the OTP bits, so we go with the default and hope for the
1591 geometry->search_area_stride_exponent = 2;
1595 static const char *fingerprint = "STMP";
1596 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1598 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1599 struct device *dev = this->dev;
1600 struct nand_chip *chip = &this->nand;
1601 struct mtd_info *mtd = nand_to_mtd(chip);
1602 unsigned int search_area_size_in_strides;
1603 unsigned int stride;
1605 uint8_t *buffer = chip->data_buf;
1606 int saved_chip_number;
1607 int found_an_ncb_fingerprint = false;
1609 /* Compute the number of strides in a search area. */
1610 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1612 saved_chip_number = this->current_chip;
1613 chip->select_chip(mtd, 0);
1616 * Loop through the first search area, looking for the NCB fingerprint.
1618 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1620 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1621 /* Compute the page addresses. */
1622 page = stride * rom_geo->stride_size_in_pages;
1624 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1627 * Read the NCB fingerprint. The fingerprint is four bytes long
1628 * and starts in the 12th byte of the page.
1630 nand_read_page_op(chip, page, 12, NULL, 0);
1631 chip->read_buf(mtd, buffer, strlen(fingerprint));
1633 /* Look for the fingerprint. */
1634 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1635 found_an_ncb_fingerprint = true;
1641 chip->select_chip(mtd, saved_chip_number);
1643 if (found_an_ncb_fingerprint)
1644 dev_dbg(dev, "\tFound a fingerprint\n");
1646 dev_dbg(dev, "\tNo fingerprint found\n");
1647 return found_an_ncb_fingerprint;
1650 /* Writes a transcription stamp. */
1651 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1653 struct device *dev = this->dev;
1654 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1655 struct nand_chip *chip = &this->nand;
1656 struct mtd_info *mtd = nand_to_mtd(chip);
1657 unsigned int block_size_in_pages;
1658 unsigned int search_area_size_in_strides;
1659 unsigned int search_area_size_in_pages;
1660 unsigned int search_area_size_in_blocks;
1662 unsigned int stride;
1664 uint8_t *buffer = chip->data_buf;
1665 int saved_chip_number;
1668 /* Compute the search area geometry. */
1669 block_size_in_pages = mtd->erasesize / mtd->writesize;
1670 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1671 search_area_size_in_pages = search_area_size_in_strides *
1672 rom_geo->stride_size_in_pages;
1673 search_area_size_in_blocks =
1674 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1675 block_size_in_pages;
1677 dev_dbg(dev, "Search Area Geometry :\n");
1678 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1679 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1680 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1682 /* Select chip 0. */
1683 saved_chip_number = this->current_chip;
1684 chip->select_chip(mtd, 0);
1686 /* Loop over blocks in the first search area, erasing them. */
1687 dev_dbg(dev, "Erasing the search area...\n");
1689 for (block = 0; block < search_area_size_in_blocks; block++) {
1690 /* Erase this block. */
1691 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1692 status = nand_erase_op(chip, block);
1694 dev_err(dev, "[%s] Erase failed.\n", __func__);
1697 /* Write the NCB fingerprint into the page buffer. */
1698 memset(buffer, ~0, mtd->writesize);
1699 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1701 /* Loop through the first search area, writing NCB fingerprints. */
1702 dev_dbg(dev, "Writing NCB fingerprints...\n");
1703 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1704 /* Compute the page addresses. */
1705 page = stride * rom_geo->stride_size_in_pages;
1707 /* Write the first page of the current stride. */
1708 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1710 status = chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
1712 dev_err(dev, "[%s] Write failed.\n", __func__);
1715 /* Deselect chip 0. */
1716 chip->select_chip(mtd, saved_chip_number);
1720 static int mx23_boot_init(struct gpmi_nand_data *this)
1722 struct device *dev = this->dev;
1723 struct nand_chip *chip = &this->nand;
1724 struct mtd_info *mtd = nand_to_mtd(chip);
1725 unsigned int block_count;
1734 * If control arrives here, we can't use block mark swapping, which
1735 * means we're forced to use transcription. First, scan for the
1736 * transcription stamp. If we find it, then we don't have to do
1737 * anything -- the block marks are already transcribed.
1739 if (mx23_check_transcription_stamp(this))
1743 * If control arrives here, we couldn't find a transcription stamp, so
1744 * so we presume the block marks are in the conventional location.
1746 dev_dbg(dev, "Transcribing bad block marks...\n");
1748 /* Compute the number of blocks in the entire medium. */
1749 block_count = chip->chipsize >> chip->phys_erase_shift;
1752 * Loop over all the blocks in the medium, transcribing block marks as
1755 for (block = 0; block < block_count; block++) {
1757 * Compute the chip, page and byte addresses for this block's
1758 * conventional mark.
1760 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1761 page = block << (chip->phys_erase_shift - chip->page_shift);
1762 byte = block << chip->phys_erase_shift;
1764 /* Send the command to read the conventional block mark. */
1765 chip->select_chip(mtd, chipnr);
1766 nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1767 block_mark = chip->read_byte(mtd);
1768 chip->select_chip(mtd, -1);
1771 * Check if the block is marked bad. If so, we need to mark it
1772 * again, but this time the result will be a mark in the
1773 * location where we transcribe block marks.
1775 if (block_mark != 0xff) {
1776 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1777 ret = chip->block_markbad(mtd, byte);
1780 "Failed to mark block bad with ret %d\n",
1785 /* Write the stamp that indicates we've transcribed the block marks. */
1786 mx23_write_transcription_stamp(this);
1790 static int nand_boot_init(struct gpmi_nand_data *this)
1792 nand_boot_set_geometry(this);
1794 /* This is ROM arch-specific initilization before the BBT scanning. */
1795 if (GPMI_IS_MX23(this))
1796 return mx23_boot_init(this);
1800 static int gpmi_set_geometry(struct gpmi_nand_data *this)
1804 /* Free the temporary DMA memory for reading ID. */
1805 gpmi_free_dma_buffer(this);
1807 /* Set up the NFC geometry which is used by BCH. */
1808 ret = bch_set_geometry(this);
1810 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
1814 /* Alloc the new DMA buffers according to the pagesize and oobsize */
1815 return gpmi_alloc_dma_buffer(this);
1818 static int gpmi_init_last(struct gpmi_nand_data *this)
1820 struct nand_chip *chip = &this->nand;
1821 struct mtd_info *mtd = nand_to_mtd(chip);
1822 struct nand_ecc_ctrl *ecc = &chip->ecc;
1823 struct bch_geometry *bch_geo = &this->bch_geometry;
1826 /* Set up the medium geometry */
1827 ret = gpmi_set_geometry(this);
1831 /* Init the nand_ecc_ctrl{} */
1832 ecc->read_page = gpmi_ecc_read_page;
1833 ecc->write_page = gpmi_ecc_write_page;
1834 ecc->read_oob = gpmi_ecc_read_oob;
1835 ecc->write_oob = gpmi_ecc_write_oob;
1836 ecc->read_page_raw = gpmi_ecc_read_page_raw;
1837 ecc->write_page_raw = gpmi_ecc_write_page_raw;
1838 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1839 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
1840 ecc->mode = NAND_ECC_HW;
1841 ecc->size = bch_geo->ecc_chunk_size;
1842 ecc->strength = bch_geo->ecc_strength;
1843 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
1846 * We only enable the subpage read when:
1847 * (1) the chip is imx6, and
1848 * (2) the size of the ECC parity is byte aligned.
1850 if (GPMI_IS_MX6(this) &&
1851 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1852 ecc->read_subpage = gpmi_ecc_read_subpage;
1853 chip->options |= NAND_SUBPAGE_READ;
1859 static int gpmi_nand_attach_chip(struct nand_chip *chip)
1861 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1864 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1865 chip->bbt_options |= NAND_BBT_NO_OOB;
1867 if (of_property_read_bool(this->dev->of_node,
1868 "fsl,no-blockmark-swap"))
1869 this->swap_block_mark = false;
1871 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
1872 this->swap_block_mark ? "en" : "dis");
1874 ret = gpmi_init_last(this);
1878 chip->options |= NAND_SKIP_BBTSCAN;
1883 static const struct nand_controller_ops gpmi_nand_controller_ops = {
1884 .attach_chip = gpmi_nand_attach_chip,
1887 static int gpmi_nand_init(struct gpmi_nand_data *this)
1889 struct nand_chip *chip = &this->nand;
1890 struct mtd_info *mtd = nand_to_mtd(chip);
1893 /* init current chip */
1894 this->current_chip = -1;
1896 /* init the MTD data structures */
1897 mtd->name = "gpmi-nand";
1898 mtd->dev.parent = this->dev;
1900 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1901 nand_set_controller_data(chip, this);
1902 nand_set_flash_node(chip, this->pdev->dev.of_node);
1903 chip->select_chip = gpmi_select_chip;
1904 chip->setup_data_interface = gpmi_setup_data_interface;
1905 chip->cmd_ctrl = gpmi_cmd_ctrl;
1906 chip->dev_ready = gpmi_dev_ready;
1907 chip->read_byte = gpmi_read_byte;
1908 chip->read_buf = gpmi_read_buf;
1909 chip->write_buf = gpmi_write_buf;
1910 chip->badblock_pattern = &gpmi_bbt_descr;
1911 chip->block_markbad = gpmi_block_markbad;
1912 chip->options |= NAND_NO_SUBPAGE_WRITE;
1914 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1915 this->swap_block_mark = !GPMI_IS_MX23(this);
1918 * Allocate a temporary DMA buffer for reading ID in the
1919 * nand_scan_ident().
1921 this->bch_geometry.payload_size = 1024;
1922 this->bch_geometry.auxiliary_size = 128;
1923 ret = gpmi_alloc_dma_buffer(this);
1927 chip->dummy_controller.ops = &gpmi_nand_controller_ops;
1928 ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
1932 ret = nand_boot_init(this);
1934 goto err_nand_cleanup;
1935 ret = nand_create_bbt(chip);
1937 goto err_nand_cleanup;
1939 ret = mtd_device_register(mtd, NULL, 0);
1941 goto err_nand_cleanup;
1947 gpmi_free_dma_buffer(this);
1951 static const struct of_device_id gpmi_nand_id_table[] = {
1953 .compatible = "fsl,imx23-gpmi-nand",
1954 .data = &gpmi_devdata_imx23,
1956 .compatible = "fsl,imx28-gpmi-nand",
1957 .data = &gpmi_devdata_imx28,
1959 .compatible = "fsl,imx6q-gpmi-nand",
1960 .data = &gpmi_devdata_imx6q,
1962 .compatible = "fsl,imx6sx-gpmi-nand",
1963 .data = &gpmi_devdata_imx6sx,
1965 .compatible = "fsl,imx7d-gpmi-nand",
1966 .data = &gpmi_devdata_imx7d,
1969 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1971 static int gpmi_nand_probe(struct platform_device *pdev)
1973 struct gpmi_nand_data *this;
1974 const struct of_device_id *of_id;
1977 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1981 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1983 this->devdata = of_id->data;
1985 dev_err(&pdev->dev, "Failed to find the right device id.\n");
1989 platform_set_drvdata(pdev, this);
1991 this->dev = &pdev->dev;
1993 ret = acquire_resources(this);
1995 goto exit_acquire_resources;
1997 ret = gpmi_init(this);
2001 ret = gpmi_nand_init(this);
2005 dev_info(this->dev, "driver registered.\n");
2010 release_resources(this);
2011 exit_acquire_resources:
2016 static int gpmi_nand_remove(struct platform_device *pdev)
2018 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2020 nand_release(&this->nand);
2021 gpmi_free_dma_buffer(this);
2022 release_resources(this);
2026 #ifdef CONFIG_PM_SLEEP
2027 static int gpmi_pm_suspend(struct device *dev)
2029 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2031 release_dma_channels(this);
2035 static int gpmi_pm_resume(struct device *dev)
2037 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2040 ret = acquire_dma_channels(this);
2044 /* re-init the GPMI registers */
2045 ret = gpmi_init(this);
2047 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2051 /* re-init the BCH registers */
2052 ret = bch_set_geometry(this);
2054 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2060 #endif /* CONFIG_PM_SLEEP */
2062 static const struct dev_pm_ops gpmi_pm_ops = {
2063 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2066 static struct platform_driver gpmi_nand_driver = {
2068 .name = "gpmi-nand",
2070 .of_match_table = gpmi_nand_id_table,
2072 .probe = gpmi_nand_probe,
2073 .remove = gpmi_nand_remove,
2075 module_platform_driver(gpmi_nand_driver);
2077 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2078 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2079 MODULE_LICENSE("GPL");