eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/pxa3xx_nand.c |
| 3 | * |
| 4 | * Copyright © 2005 Intel Corporation |
| 5 | * Copyright © 2006 Marvell International Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Ezequiel Garcia | de484a3 | 2013-11-07 12:17:10 -0300 | [diff] [blame] | 10 | * |
| 11 | * See Documentation/mtd/nand/pxa3xx-nand.txt for more details. |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 12 | */ |
| 13 | |
Haojian Zhuang | a88bdbb | 2009-09-11 19:33:58 +0800 | [diff] [blame] | 14 | #include <linux/kernel.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/platform_device.h> |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 18 | #include <linux/dmaengine.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 20 | #include <linux/dma/pxa-dma.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/mtd/mtd.h> |
| 24 | #include <linux/mtd/nand.h> |
| 25 | #include <linux/mtd/partitions.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 27 | #include <linux/iopoll.h> |
David Woodhouse | a1c06ee | 2008-04-22 20:39:43 +0100 | [diff] [blame] | 28 | #include <linux/irq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_device.h> |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 32 | #include <linux/of_mtd.h> |
Arnd Bergmann | 293b2da | 2012-08-24 15:16:48 +0200 | [diff] [blame] | 33 | #include <linux/platform_data/mtd-nand-pxa3xx.h> |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 34 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 35 | #define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200) |
| 36 | #define NAND_STOP_DELAY msecs_to_jiffies(40) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 37 | #define PAGE_CHUNK_SIZE (2048) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 38 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 39 | /* |
| 40 | * Define a buffer size for the initial command that detects the flash device: |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 41 | * STATUS, READID and PARAM. |
| 42 | * ONFI param page is 256 bytes, and there are three redundant copies |
| 43 | * to be read. JEDEC param page is 512 bytes, and there are also three |
| 44 | * redundant copies to be read. |
| 45 | * Hence this buffer should be at least 512 x 3. Let's pick 2048. |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 46 | */ |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 47 | #define INIT_BUFFER_SIZE 2048 |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 48 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 49 | /* registers and bit definitions */ |
| 50 | #define NDCR (0x00) /* Control register */ |
| 51 | #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */ |
| 52 | #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */ |
| 53 | #define NDSR (0x14) /* Status Register */ |
| 54 | #define NDPCR (0x18) /* Page Count Register */ |
| 55 | #define NDBDR0 (0x1C) /* Bad Block Register 0 */ |
| 56 | #define NDBDR1 (0x20) /* Bad Block Register 1 */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 57 | #define NDECCCTRL (0x28) /* ECC control */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 58 | #define NDDB (0x40) /* Data Buffer */ |
| 59 | #define NDCB0 (0x48) /* Command Buffer0 */ |
| 60 | #define NDCB1 (0x4C) /* Command Buffer1 */ |
| 61 | #define NDCB2 (0x50) /* Command Buffer2 */ |
| 62 | |
| 63 | #define NDCR_SPARE_EN (0x1 << 31) |
| 64 | #define NDCR_ECC_EN (0x1 << 30) |
| 65 | #define NDCR_DMA_EN (0x1 << 29) |
| 66 | #define NDCR_ND_RUN (0x1 << 28) |
| 67 | #define NDCR_DWIDTH_C (0x1 << 27) |
| 68 | #define NDCR_DWIDTH_M (0x1 << 26) |
| 69 | #define NDCR_PAGE_SZ (0x1 << 24) |
| 70 | #define NDCR_NCSX (0x1 << 23) |
| 71 | #define NDCR_ND_MODE (0x3 << 21) |
| 72 | #define NDCR_NAND_MODE (0x0) |
| 73 | #define NDCR_CLR_PG_CNT (0x1 << 20) |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 74 | #define NFCV1_NDCR_ARB_CNTL (0x1 << 19) |
| 75 | #define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 76 | #define NDCR_RD_ID_CNT_MASK (0x7 << 16) |
| 77 | #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK) |
| 78 | |
| 79 | #define NDCR_RA_START (0x1 << 15) |
| 80 | #define NDCR_PG_PER_BLK (0x1 << 14) |
| 81 | #define NDCR_ND_ARB_EN (0x1 << 12) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 82 | #define NDCR_INT_MASK (0xFFF) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 83 | |
| 84 | #define NDSR_MASK (0xfff) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 85 | #define NDSR_ERR_CNT_OFF (16) |
| 86 | #define NDSR_ERR_CNT_MASK (0x1f) |
| 87 | #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 88 | #define NDSR_RDY (0x1 << 12) |
| 89 | #define NDSR_FLASH_RDY (0x1 << 11) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 90 | #define NDSR_CS0_PAGED (0x1 << 10) |
| 91 | #define NDSR_CS1_PAGED (0x1 << 9) |
| 92 | #define NDSR_CS0_CMDD (0x1 << 8) |
| 93 | #define NDSR_CS1_CMDD (0x1 << 7) |
| 94 | #define NDSR_CS0_BBD (0x1 << 6) |
| 95 | #define NDSR_CS1_BBD (0x1 << 5) |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 96 | #define NDSR_UNCORERR (0x1 << 4) |
| 97 | #define NDSR_CORERR (0x1 << 3) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 98 | #define NDSR_WRDREQ (0x1 << 2) |
| 99 | #define NDSR_RDDREQ (0x1 << 1) |
| 100 | #define NDSR_WRCMDREQ (0x1) |
| 101 | |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 102 | #define NDCB0_LEN_OVRD (0x1 << 28) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 103 | #define NDCB0_ST_ROW_EN (0x1 << 26) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 104 | #define NDCB0_AUTO_RS (0x1 << 25) |
| 105 | #define NDCB0_CSEL (0x1 << 24) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 106 | #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29) |
| 107 | #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 108 | #define NDCB0_CMD_TYPE_MASK (0x7 << 21) |
| 109 | #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK) |
| 110 | #define NDCB0_NC (0x1 << 20) |
| 111 | #define NDCB0_DBC (0x1 << 19) |
| 112 | #define NDCB0_ADDR_CYC_MASK (0x7 << 16) |
| 113 | #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK) |
| 114 | #define NDCB0_CMD2_MASK (0xff << 8) |
| 115 | #define NDCB0_CMD1_MASK (0xff) |
| 116 | #define NDCB0_ADDR_CYC_SHIFT (16) |
| 117 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 118 | #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */ |
| 119 | #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */ |
| 120 | #define EXT_CMD_TYPE_READ 4 /* Read */ |
| 121 | #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */ |
| 122 | #define EXT_CMD_TYPE_FINAL 3 /* Final command */ |
| 123 | #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */ |
| 124 | #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */ |
| 125 | |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 126 | /* |
| 127 | * This should be large enough to read 'ONFI' and 'JEDEC'. |
| 128 | * Let's use 7 bytes, which is the maximum ID count supported |
| 129 | * by the controller (see NDCR_RD_ID_CNT_MASK). |
| 130 | */ |
| 131 | #define READ_ID_BYTES 7 |
| 132 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 133 | /* macros for registers read/write */ |
| 134 | #define nand_writel(info, off, val) \ |
Thomas Petazzoni | b7e46062 | 2014-05-22 14:56:52 +0200 | [diff] [blame] | 135 | writel_relaxed((val), (info)->mmio_base + (off)) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 136 | |
| 137 | #define nand_readl(info, off) \ |
Thomas Petazzoni | b7e46062 | 2014-05-22 14:56:52 +0200 | [diff] [blame] | 138 | readl_relaxed((info)->mmio_base + (off)) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 139 | |
| 140 | /* error code and state */ |
| 141 | enum { |
| 142 | ERR_NONE = 0, |
| 143 | ERR_DMABUSERR = -1, |
| 144 | ERR_SENDCMD = -2, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 145 | ERR_UNCORERR = -3, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 146 | ERR_BBERR = -4, |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 147 | ERR_CORERR = -5, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | enum { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 151 | STATE_IDLE = 0, |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 152 | STATE_PREPARED, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 153 | STATE_CMD_HANDLE, |
| 154 | STATE_DMA_READING, |
| 155 | STATE_DMA_WRITING, |
| 156 | STATE_DMA_DONE, |
| 157 | STATE_PIO_READING, |
| 158 | STATE_PIO_WRITING, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 159 | STATE_CMD_DONE, |
| 160 | STATE_READY, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 163 | enum pxa3xx_nand_variant { |
| 164 | PXA3XX_NAND_VARIANT_PXA, |
| 165 | PXA3XX_NAND_VARIANT_ARMADA370, |
| 166 | }; |
| 167 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 168 | struct pxa3xx_nand_host { |
| 169 | struct nand_chip chip; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 170 | void *info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 171 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 172 | /* page size of attached chip */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 173 | int use_ecc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 174 | int cs; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 175 | |
| 176 | /* calculated from pxa3xx_nand_flash data */ |
| 177 | unsigned int col_addr_cycles; |
| 178 | unsigned int row_addr_cycles; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | struct pxa3xx_nand_info { |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 182 | struct nand_hw_control controller; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 183 | struct platform_device *pdev; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 184 | |
| 185 | struct clk *clk; |
| 186 | void __iomem *mmio_base; |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 187 | unsigned long mmio_phys; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 188 | struct completion cmd_complete, dev_ready; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 189 | |
| 190 | unsigned int buf_start; |
| 191 | unsigned int buf_count; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 192 | unsigned int buf_size; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 193 | unsigned int data_buff_pos; |
| 194 | unsigned int oob_buff_pos; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 195 | |
| 196 | /* DMA information */ |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 197 | struct scatterlist sg; |
| 198 | enum dma_data_direction dma_dir; |
| 199 | struct dma_chan *dma_chan; |
| 200 | dma_cookie_t dma_cookie; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 201 | int drcmr_dat; |
| 202 | int drcmr_cmd; |
| 203 | |
| 204 | unsigned char *data_buff; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 205 | unsigned char *oob_buff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 206 | dma_addr_t data_buff_phys; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 207 | int data_dma_ch; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 208 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 209 | struct pxa3xx_nand_host *host[NUM_CHIP_SELECT]; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 210 | unsigned int state; |
| 211 | |
Ezequiel Garcia | c0f3b86 | 2013-08-10 16:34:52 -0300 | [diff] [blame] | 212 | /* |
| 213 | * This driver supports NFCv1 (as found in PXA SoC) |
| 214 | * and NFCv2 (as found in Armada 370/XP SoC). |
| 215 | */ |
| 216 | enum pxa3xx_nand_variant variant; |
| 217 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 218 | int cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 219 | int use_ecc; /* use HW ECC ? */ |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 220 | int ecc_bch; /* using BCH ECC? */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 221 | int use_dma; /* use DMA ? */ |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 222 | int use_spare; /* use spare ? */ |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 223 | int need_wait; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 224 | |
Ezequiel Garcia | 2128b08 | 2013-11-07 12:17:16 -0300 | [diff] [blame] | 225 | unsigned int data_size; /* data to be read from FIFO */ |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 226 | unsigned int chunk_size; /* split commands chunk size */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 227 | unsigned int oob_size; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 228 | unsigned int spare_size; |
| 229 | unsigned int ecc_size; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 230 | unsigned int ecc_err_cnt; |
| 231 | unsigned int max_bitflips; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 232 | int retcode; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 233 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 234 | /* cached register value */ |
| 235 | uint32_t reg_ndcr; |
| 236 | uint32_t ndtr0cs0; |
| 237 | uint32_t ndtr1cs0; |
| 238 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 239 | /* generated NDCBx register values */ |
| 240 | uint32_t ndcb0; |
| 241 | uint32_t ndcb1; |
| 242 | uint32_t ndcb2; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 243 | uint32_t ndcb3; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 244 | }; |
| 245 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 246 | static bool use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 247 | module_param(use_dma, bool, 0444); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 248 | MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW"); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 249 | |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 250 | struct pxa3xx_nand_timing { |
| 251 | unsigned int tCH; /* Enable signal hold time */ |
| 252 | unsigned int tCS; /* Enable signal setup time */ |
| 253 | unsigned int tWH; /* ND_nWE high duration */ |
| 254 | unsigned int tWP; /* ND_nWE pulse time */ |
| 255 | unsigned int tRH; /* ND_nRE high duration */ |
| 256 | unsigned int tRP; /* ND_nRE pulse width */ |
| 257 | unsigned int tR; /* ND_nWE high to ND_nRE low for read */ |
| 258 | unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */ |
| 259 | unsigned int tAR; /* ND_ALE low to ND_nRE low delay */ |
| 260 | }; |
| 261 | |
| 262 | struct pxa3xx_nand_flash { |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 263 | uint32_t chip_id; |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 264 | unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */ |
| 265 | unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */ |
Ezequiel García | a9cadf7 | 2015-08-21 15:47:28 -0300 | [diff] [blame] | 266 | struct pxa3xx_nand_timing *timing; /* NAND Flash timing */ |
| 267 | }; |
| 268 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 269 | static struct pxa3xx_nand_timing timing[] = { |
Lei Wen | 227a886 | 2010-08-18 18:00:03 +0800 | [diff] [blame] | 270 | { 40, 80, 60, 100, 80, 100, 90000, 400, 40, }, |
| 271 | { 10, 0, 20, 40, 30, 40, 11123, 110, 10, }, |
| 272 | { 10, 25, 15, 25, 15, 30, 25000, 60, 10, }, |
| 273 | { 10, 35, 15, 25, 15, 25, 25000, 60, 10, }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 274 | }; |
| 275 | |
Lei Wen | c1f8247 | 2010-08-17 13:50:23 +0800 | [diff] [blame] | 276 | static struct pxa3xx_nand_flash builtin_flash_types[] = { |
Antoine Ténart | 89c1702 | 2015-10-21 10:29:04 +0200 | [diff] [blame] | 277 | { 0x46ec, 16, 16, &timing[1] }, |
| 278 | { 0xdaec, 8, 8, &timing[1] }, |
| 279 | { 0xd7ec, 8, 8, &timing[1] }, |
| 280 | { 0xa12c, 8, 8, &timing[2] }, |
| 281 | { 0xb12c, 16, 16, &timing[2] }, |
| 282 | { 0xdc2c, 8, 8, &timing[2] }, |
| 283 | { 0xcc2c, 16, 16, &timing[2] }, |
| 284 | { 0xba20, 16, 16, &timing[3] }, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 285 | }; |
| 286 | |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 287 | static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' }; |
| 288 | static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' }; |
| 289 | |
| 290 | static struct nand_bbt_descr bbt_main_descr = { |
| 291 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 292 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 293 | .offs = 8, |
| 294 | .len = 6, |
| 295 | .veroffs = 14, |
| 296 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 297 | .pattern = bbt_pattern |
| 298 | }; |
| 299 | |
| 300 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 301 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 302 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 303 | .offs = 8, |
| 304 | .len = 6, |
| 305 | .veroffs = 14, |
| 306 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 307 | .pattern = bbt_mirror_pattern |
| 308 | }; |
| 309 | |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 310 | static struct nand_ecclayout ecc_layout_2KB_bch4bit = { |
| 311 | .eccbytes = 32, |
| 312 | .eccpos = { |
| 313 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 314 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 315 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 316 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 317 | .oobfree = { {2, 30} } |
| 318 | }; |
| 319 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 320 | static struct nand_ecclayout ecc_layout_4KB_bch4bit = { |
| 321 | .eccbytes = 64, |
| 322 | .eccpos = { |
| 323 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 324 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 325 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 326 | 56, 57, 58, 59, 60, 61, 62, 63, |
| 327 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 328 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 329 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 330 | 120, 121, 122, 123, 124, 125, 126, 127}, |
| 331 | /* Bootrom looks in bytes 0 & 5 for bad blocks */ |
| 332 | .oobfree = { {6, 26}, { 64, 32} } |
| 333 | }; |
| 334 | |
| 335 | static struct nand_ecclayout ecc_layout_4KB_bch8bit = { |
| 336 | .eccbytes = 128, |
| 337 | .eccpos = { |
| 338 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 339 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 340 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 341 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 342 | .oobfree = { } |
| 343 | }; |
| 344 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 345 | #define NDTR0_tCH(c) (min((c), 7) << 19) |
| 346 | #define NDTR0_tCS(c) (min((c), 7) << 16) |
| 347 | #define NDTR0_tWH(c) (min((c), 7) << 11) |
| 348 | #define NDTR0_tWP(c) (min((c), 7) << 8) |
| 349 | #define NDTR0_tRH(c) (min((c), 7) << 3) |
| 350 | #define NDTR0_tRP(c) (min((c), 7) << 0) |
| 351 | |
| 352 | #define NDTR1_tR(c) (min((c), 65535) << 16) |
| 353 | #define NDTR1_tWHR(c) (min((c), 15) << 4) |
| 354 | #define NDTR1_tAR(c) (min((c), 15) << 0) |
| 355 | |
| 356 | /* convert nano-seconds to nand flash controller clock cycles */ |
Axel Lin | 93b352f | 2010-08-16 16:09:09 +0800 | [diff] [blame] | 357 | #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 358 | |
Jingoo Han | 17754ad | 2014-05-07 17:49:13 +0900 | [diff] [blame] | 359 | static const struct of_device_id pxa3xx_nand_dt_ids[] = { |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 360 | { |
| 361 | .compatible = "marvell,pxa3xx-nand", |
| 362 | .data = (void *)PXA3XX_NAND_VARIANT_PXA, |
| 363 | }, |
Ezequiel Garcia | 1963ff9 | 2013-12-24 12:40:07 -0300 | [diff] [blame] | 364 | { |
| 365 | .compatible = "marvell,armada370-nand", |
| 366 | .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370, |
| 367 | }, |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 368 | {} |
| 369 | }; |
| 370 | MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids); |
| 371 | |
| 372 | static enum pxa3xx_nand_variant |
| 373 | pxa3xx_nand_get_variant(struct platform_device *pdev) |
| 374 | { |
| 375 | const struct of_device_id *of_id = |
| 376 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 377 | if (!of_id) |
| 378 | return PXA3XX_NAND_VARIANT_PXA; |
| 379 | return (enum pxa3xx_nand_variant)of_id->data; |
| 380 | } |
| 381 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 382 | static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host, |
Enrico Scholz | 7dad482 | 2008-08-29 12:59:50 +0200 | [diff] [blame] | 383 | const struct pxa3xx_nand_timing *t) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 384 | { |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 385 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 386 | unsigned long nand_clk = clk_get_rate(info->clk); |
| 387 | uint32_t ndtr0, ndtr1; |
| 388 | |
| 389 | ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) | |
| 390 | NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) | |
| 391 | NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) | |
| 392 | NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) | |
| 393 | NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) | |
| 394 | NDTR0_tRP(ns2cycle(t->tRP, nand_clk)); |
| 395 | |
| 396 | ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) | |
| 397 | NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) | |
| 398 | NDTR1_tAR(ns2cycle(t->tAR, nand_clk)); |
| 399 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 400 | info->ndtr0cs0 = ndtr0; |
| 401 | info->ndtr1cs0 = ndtr1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 402 | nand_writel(info, NDTR0CS0, ndtr0); |
| 403 | nand_writel(info, NDTR1CS0, ndtr1); |
| 404 | } |
| 405 | |
Antoine Ténart | 3f225b7 | 2015-10-21 10:29:02 +0200 | [diff] [blame] | 406 | static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host, |
| 407 | const struct nand_sdr_timings *t) |
| 408 | { |
| 409 | struct pxa3xx_nand_info *info = host->info_data; |
| 410 | struct nand_chip *chip = &host->chip; |
| 411 | unsigned long nand_clk = clk_get_rate(info->clk); |
| 412 | uint32_t ndtr0, ndtr1; |
| 413 | |
| 414 | u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000); |
| 415 | u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000); |
| 416 | u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000); |
| 417 | u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000); |
| 418 | u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000); |
| 419 | u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000); |
| 420 | u32 tR = chip->chip_delay * 1000; |
| 421 | u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000); |
| 422 | u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000); |
| 423 | |
| 424 | /* fallback to a default value if tR = 0 */ |
| 425 | if (!tR) |
| 426 | tR = 20000; |
| 427 | |
| 428 | ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) | |
| 429 | NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) | |
| 430 | NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) | |
| 431 | NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) | |
| 432 | NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) | |
| 433 | NDTR0_tRP(ns2cycle(tRP_min, nand_clk)); |
| 434 | |
| 435 | ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) | |
| 436 | NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) | |
| 437 | NDTR1_tAR(ns2cycle(tAR_min, nand_clk)); |
| 438 | |
| 439 | info->ndtr0cs0 = ndtr0; |
| 440 | info->ndtr1cs0 = ndtr1; |
| 441 | nand_writel(info, NDTR0CS0, ndtr0); |
| 442 | nand_writel(info, NDTR1CS0, ndtr1); |
| 443 | } |
| 444 | |
| 445 | static int pxa3xx_nand_init_timings_compat(struct pxa3xx_nand_host *host, |
| 446 | unsigned int *flash_width, |
| 447 | unsigned int *dfc_width) |
| 448 | { |
| 449 | struct nand_chip *chip = &host->chip; |
| 450 | struct pxa3xx_nand_info *info = host->info_data; |
| 451 | const struct pxa3xx_nand_flash *f = NULL; |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 452 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
Antoine Ténart | 3f225b7 | 2015-10-21 10:29:02 +0200 | [diff] [blame] | 453 | int i, id, ntypes; |
| 454 | |
| 455 | ntypes = ARRAY_SIZE(builtin_flash_types); |
| 456 | |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 457 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Antoine Ténart | 3f225b7 | 2015-10-21 10:29:02 +0200 | [diff] [blame] | 458 | |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 459 | id = chip->read_byte(mtd); |
| 460 | id |= chip->read_byte(mtd) << 0x8; |
Antoine Ténart | 3f225b7 | 2015-10-21 10:29:02 +0200 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < ntypes; i++) { |
| 463 | f = &builtin_flash_types[i]; |
| 464 | |
| 465 | if (f->chip_id == id) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | if (i == ntypes) { |
| 470 | dev_err(&info->pdev->dev, "Error: timings not found\n"); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | pxa3xx_nand_set_timing(host, f->timing); |
| 475 | |
| 476 | *flash_width = f->flash_width; |
| 477 | *dfc_width = f->dfc_width; |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int pxa3xx_nand_init_timings_onfi(struct pxa3xx_nand_host *host, |
| 483 | int mode) |
| 484 | { |
| 485 | const struct nand_sdr_timings *timings; |
| 486 | |
| 487 | mode = fls(mode) - 1; |
| 488 | if (mode < 0) |
| 489 | mode = 0; |
| 490 | |
| 491 | timings = onfi_async_timing_mode_to_sdr_timings(mode); |
| 492 | if (IS_ERR(timings)) |
| 493 | return PTR_ERR(timings); |
| 494 | |
| 495 | pxa3xx_nand_set_sdr_timing(host, timings); |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static int pxa3xx_nand_init(struct pxa3xx_nand_host *host) |
| 501 | { |
| 502 | struct nand_chip *chip = &host->chip; |
| 503 | struct pxa3xx_nand_info *info = host->info_data; |
| 504 | unsigned int flash_width = 0, dfc_width = 0; |
| 505 | int mode, err; |
| 506 | |
| 507 | mode = onfi_get_async_timing_mode(chip); |
| 508 | if (mode == ONFI_TIMING_MODE_UNKNOWN) { |
| 509 | err = pxa3xx_nand_init_timings_compat(host, &flash_width, |
| 510 | &dfc_width); |
| 511 | if (err) |
| 512 | return err; |
| 513 | |
| 514 | if (flash_width == 16) { |
| 515 | info->reg_ndcr |= NDCR_DWIDTH_M; |
| 516 | chip->options |= NAND_BUSWIDTH_16; |
| 517 | } |
| 518 | |
| 519 | info->reg_ndcr |= (dfc_width == 16) ? NDCR_DWIDTH_C : 0; |
| 520 | } else { |
| 521 | err = pxa3xx_nand_init_timings_onfi(host, mode); |
| 522 | if (err) |
| 523 | return err; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Ezequiel Garcia | 6a3e486 | 2013-11-07 12:17:18 -0300 | [diff] [blame] | 529 | /* |
| 530 | * Set the data and OOB size, depending on the selected |
| 531 | * spare and ECC configuration. |
| 532 | * Only applicable to READ0, READOOB and PAGEPROG commands. |
| 533 | */ |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 534 | static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info, |
| 535 | struct mtd_info *mtd) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 536 | { |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 537 | int oob_enable = info->reg_ndcr & NDCR_SPARE_EN; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 538 | |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 539 | info->data_size = mtd->writesize; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 540 | if (!oob_enable) |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 541 | return; |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 542 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 543 | info->oob_size = info->spare_size; |
| 544 | if (!info->use_ecc) |
| 545 | info->oob_size += info->ecc_size; |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 546 | } |
| 547 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 548 | /** |
| 549 | * NOTE: it is a must to set ND_RUN firstly, then write |
| 550 | * command buffer, otherwise, it does not work. |
| 551 | * We enable all the interrupt at the same time, and |
| 552 | * let pxa3xx_nand_irq to handle all logic. |
| 553 | */ |
| 554 | static void pxa3xx_nand_start(struct pxa3xx_nand_info *info) |
| 555 | { |
| 556 | uint32_t ndcr; |
| 557 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 558 | ndcr = info->reg_ndcr; |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 559 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 560 | if (info->use_ecc) { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 561 | ndcr |= NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 562 | if (info->ecc_bch) |
| 563 | nand_writel(info, NDECCCTRL, 0x1); |
| 564 | } else { |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 565 | ndcr &= ~NDCR_ECC_EN; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 566 | if (info->ecc_bch) |
| 567 | nand_writel(info, NDECCCTRL, 0x0); |
| 568 | } |
Ezequiel Garcia | cd9d118 | 2013-08-12 14:14:48 -0300 | [diff] [blame] | 569 | |
| 570 | if (info->use_dma) |
| 571 | ndcr |= NDCR_DMA_EN; |
| 572 | else |
| 573 | ndcr &= ~NDCR_DMA_EN; |
| 574 | |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 575 | if (info->use_spare) |
| 576 | ndcr |= NDCR_SPARE_EN; |
| 577 | else |
| 578 | ndcr &= ~NDCR_SPARE_EN; |
| 579 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 580 | ndcr |= NDCR_ND_RUN; |
| 581 | |
| 582 | /* clear status bits and run */ |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 583 | nand_writel(info, NDSR, NDSR_MASK); |
Robert Jarzmik | 0b14392 | 2015-08-19 20:30:14 +0200 | [diff] [blame] | 584 | nand_writel(info, NDCR, 0); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 585 | nand_writel(info, NDCR, ndcr); |
| 586 | } |
| 587 | |
| 588 | static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info) |
| 589 | { |
| 590 | uint32_t ndcr; |
| 591 | int timeout = NAND_STOP_DELAY; |
| 592 | |
| 593 | /* wait RUN bit in NDCR become 0 */ |
| 594 | ndcr = nand_readl(info, NDCR); |
| 595 | while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) { |
| 596 | ndcr = nand_readl(info, NDCR); |
| 597 | udelay(1); |
| 598 | } |
| 599 | |
| 600 | if (timeout <= 0) { |
| 601 | ndcr &= ~NDCR_ND_RUN; |
| 602 | nand_writel(info, NDCR, ndcr); |
| 603 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 604 | if (info->dma_chan) |
| 605 | dmaengine_terminate_all(info->dma_chan); |
| 606 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 607 | /* clear status bits */ |
| 608 | nand_writel(info, NDSR, NDSR_MASK); |
| 609 | } |
| 610 | |
Ezequiel Garcia | 57ff88f | 2013-08-12 14:14:57 -0300 | [diff] [blame] | 611 | static void __maybe_unused |
| 612 | enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 613 | { |
| 614 | uint32_t ndcr; |
| 615 | |
| 616 | ndcr = nand_readl(info, NDCR); |
| 617 | nand_writel(info, NDCR, ndcr & ~int_mask); |
| 618 | } |
| 619 | |
| 620 | static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 621 | { |
| 622 | uint32_t ndcr; |
| 623 | |
| 624 | ndcr = nand_readl(info, NDCR); |
| 625 | nand_writel(info, NDCR, ndcr | int_mask); |
| 626 | } |
| 627 | |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 628 | static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len) |
| 629 | { |
| 630 | if (info->ecc_bch) { |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 631 | u32 val; |
| 632 | int ret; |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * According to the datasheet, when reading from NDDB |
| 636 | * with BCH enabled, after each 32 bytes reads, we |
| 637 | * have to make sure that the NDSR.RDDREQ bit is set. |
| 638 | * |
| 639 | * Drain the FIFO 8 32 bits reads at a time, and skip |
| 640 | * the polling on the last read. |
| 641 | */ |
| 642 | while (len > 8) { |
Antoine Ténart | ab53a57 | 2015-10-21 10:29:00 +0200 | [diff] [blame] | 643 | ioread32_rep(info->mmio_base + NDDB, data, 8); |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 644 | |
Maxime Ripard | afca11e | 2015-04-07 15:32:45 +0200 | [diff] [blame] | 645 | ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val, |
| 646 | val & NDSR_RDDREQ, 1000, 5000); |
| 647 | if (ret) { |
| 648 | dev_err(&info->pdev->dev, |
| 649 | "Timeout on RDDREQ while draining the FIFO\n"); |
| 650 | return; |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | data += 32; |
| 654 | len -= 8; |
| 655 | } |
| 656 | } |
| 657 | |
Antoine Ténart | ab53a57 | 2015-10-21 10:29:00 +0200 | [diff] [blame] | 658 | ioread32_rep(info->mmio_base + NDDB, data, len); |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 659 | } |
| 660 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 661 | static void handle_data_pio(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 662 | { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 663 | unsigned int do_bytes = min(info->data_size, info->chunk_size); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 664 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 665 | switch (info->state) { |
| 666 | case STATE_PIO_WRITING: |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 667 | writesl(info->mmio_base + NDDB, |
| 668 | info->data_buff + info->data_buff_pos, |
| 669 | DIV_ROUND_UP(do_bytes, 4)); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 670 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 671 | if (info->oob_size > 0) |
Rob Herring | ce914e6 | 2015-04-30 15:17:47 -0500 | [diff] [blame] | 672 | writesl(info->mmio_base + NDDB, |
| 673 | info->oob_buff + info->oob_buff_pos, |
| 674 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 675 | break; |
| 676 | case STATE_PIO_READING: |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 677 | drain_fifo(info, |
| 678 | info->data_buff + info->data_buff_pos, |
| 679 | DIV_ROUND_UP(do_bytes, 4)); |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 680 | |
Lei Wen | 9d8b104 | 2010-08-17 14:09:30 +0800 | [diff] [blame] | 681 | if (info->oob_size > 0) |
Maxime Ripard | 8dad038 | 2015-02-18 11:32:07 +0100 | [diff] [blame] | 682 | drain_fifo(info, |
| 683 | info->oob_buff + info->oob_buff_pos, |
| 684 | DIV_ROUND_UP(info->oob_size, 4)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 685 | break; |
| 686 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 687 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 688 | info->state); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 689 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 690 | } |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 691 | |
| 692 | /* Update buffer pointers for multi-page read/write */ |
| 693 | info->data_buff_pos += do_bytes; |
| 694 | info->oob_buff_pos += info->oob_size; |
| 695 | info->data_size -= do_bytes; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 696 | } |
| 697 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 698 | static void pxa3xx_nand_data_dma_irq(void *data) |
| 699 | { |
| 700 | struct pxa3xx_nand_info *info = data; |
| 701 | struct dma_tx_state state; |
| 702 | enum dma_status status; |
| 703 | |
| 704 | status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state); |
| 705 | if (likely(status == DMA_COMPLETE)) { |
| 706 | info->state = STATE_DMA_DONE; |
| 707 | } else { |
| 708 | dev_err(&info->pdev->dev, "DMA error on data channel\n"); |
| 709 | info->retcode = ERR_DMABUSERR; |
| 710 | } |
| 711 | dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir); |
| 712 | |
| 713 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
| 714 | enable_int(info, NDCR_INT_MASK); |
| 715 | } |
| 716 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 717 | static void start_data_dma(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 718 | { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 719 | enum dma_transfer_direction direction; |
| 720 | struct dma_async_tx_descriptor *tx; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 721 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 722 | switch (info->state) { |
| 723 | case STATE_DMA_WRITING: |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 724 | info->dma_dir = DMA_TO_DEVICE; |
| 725 | direction = DMA_MEM_TO_DEV; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 726 | break; |
| 727 | case STATE_DMA_READING: |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 728 | info->dma_dir = DMA_FROM_DEVICE; |
| 729 | direction = DMA_DEV_TO_MEM; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 730 | break; |
| 731 | default: |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 732 | dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__, |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 733 | info->state); |
| 734 | BUG(); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 735 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 736 | info->sg.length = info->data_size + |
| 737 | (info->oob_size ? info->spare_size + info->ecc_size : 0); |
| 738 | dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 739 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 740 | tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction, |
| 741 | DMA_PREP_INTERRUPT); |
| 742 | if (!tx) { |
| 743 | dev_err(&info->pdev->dev, "prep_slave_sg() failed\n"); |
| 744 | return; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 745 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 746 | tx->callback = pxa3xx_nand_data_dma_irq; |
| 747 | tx->callback_param = info; |
| 748 | info->dma_cookie = dmaengine_submit(tx); |
| 749 | dma_async_issue_pending(info->dma_chan); |
| 750 | dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n", |
| 751 | __func__, direction, info->dma_cookie, info->sg.length); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 752 | } |
| 753 | |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 754 | static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data) |
| 755 | { |
| 756 | struct pxa3xx_nand_info *info = data; |
| 757 | |
| 758 | handle_data_pio(info); |
| 759 | |
| 760 | info->state = STATE_CMD_DONE; |
| 761 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
| 762 | |
| 763 | return IRQ_HANDLED; |
| 764 | } |
| 765 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 766 | static irqreturn_t pxa3xx_nand_irq(int irq, void *devid) |
| 767 | { |
| 768 | struct pxa3xx_nand_info *info = devid; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 769 | unsigned int status, is_completed = 0, is_ready = 0; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 770 | unsigned int ready, cmd_done; |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 771 | irqreturn_t ret = IRQ_HANDLED; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 772 | |
| 773 | if (info->cs == 0) { |
| 774 | ready = NDSR_FLASH_RDY; |
| 775 | cmd_done = NDSR_CS0_CMDD; |
| 776 | } else { |
| 777 | ready = NDSR_RDY; |
| 778 | cmd_done = NDSR_CS1_CMDD; |
| 779 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 780 | |
| 781 | status = nand_readl(info, NDSR); |
| 782 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 783 | if (status & NDSR_UNCORERR) |
| 784 | info->retcode = ERR_UNCORERR; |
| 785 | if (status & NDSR_CORERR) { |
| 786 | info->retcode = ERR_CORERR; |
| 787 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 && |
| 788 | info->ecc_bch) |
| 789 | info->ecc_err_cnt = NDSR_ERR_CNT(status); |
| 790 | else |
| 791 | info->ecc_err_cnt = 1; |
| 792 | |
| 793 | /* |
| 794 | * Each chunk composing a page is corrected independently, |
| 795 | * and we need to store maximum number of corrected bitflips |
| 796 | * to return it to the MTD layer in ecc.read_page(). |
| 797 | */ |
| 798 | info->max_bitflips = max_t(unsigned int, |
| 799 | info->max_bitflips, |
| 800 | info->ecc_err_cnt); |
| 801 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 802 | if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) { |
| 803 | /* whether use dma to transfer data */ |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 804 | if (info->use_dma) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 805 | disable_int(info, NDCR_INT_MASK); |
| 806 | info->state = (status & NDSR_RDDREQ) ? |
| 807 | STATE_DMA_READING : STATE_DMA_WRITING; |
| 808 | start_data_dma(info); |
| 809 | goto NORMAL_IRQ_EXIT; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 810 | } else { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 811 | info->state = (status & NDSR_RDDREQ) ? |
| 812 | STATE_PIO_READING : STATE_PIO_WRITING; |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 813 | ret = IRQ_WAKE_THREAD; |
| 814 | goto NORMAL_IRQ_EXIT; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 815 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 816 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 817 | if (status & cmd_done) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 818 | info->state = STATE_CMD_DONE; |
| 819 | is_completed = 1; |
| 820 | } |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 821 | if (status & ready) { |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 822 | info->state = STATE_READY; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 823 | is_ready = 1; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 824 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 825 | |
Robert Jarzmik | 21fc0ef | 2015-08-19 20:30:15 +0200 | [diff] [blame] | 826 | /* |
| 827 | * Clear all status bit before issuing the next command, which |
| 828 | * can and will alter the status bits and will deserve a new |
| 829 | * interrupt on its own. This lets the controller exit the IRQ |
| 830 | */ |
| 831 | nand_writel(info, NDSR, status); |
| 832 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 833 | if (status & NDSR_WRCMDREQ) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 834 | status &= ~NDSR_WRCMDREQ; |
| 835 | info->state = STATE_CMD_HANDLE; |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 836 | |
| 837 | /* |
| 838 | * Command buffer registers NDCB{0-2} (and optionally NDCB3) |
| 839 | * must be loaded by writing directly either 12 or 16 |
| 840 | * bytes directly to NDCB0, four bytes at a time. |
| 841 | * |
| 842 | * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored |
| 843 | * but each NDCBx register can be read. |
| 844 | */ |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 845 | nand_writel(info, NDCB0, info->ndcb0); |
| 846 | nand_writel(info, NDCB0, info->ndcb1); |
| 847 | nand_writel(info, NDCB0, info->ndcb2); |
Ezequiel Garcia | 3a1a344 | 2013-08-12 14:14:50 -0300 | [diff] [blame] | 848 | |
| 849 | /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */ |
| 850 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 851 | nand_writel(info, NDCB0, info->ndcb3); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 852 | } |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 853 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 854 | if (is_completed) |
| 855 | complete(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 856 | if (is_ready) |
| 857 | complete(&info->dev_ready); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 858 | NORMAL_IRQ_EXIT: |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 859 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 860 | } |
| 861 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 862 | static inline int is_buf_blank(uint8_t *buf, size_t len) |
| 863 | { |
| 864 | for (; len > 0; len--) |
| 865 | if (*buf++ != 0xff) |
| 866 | return 0; |
| 867 | return 1; |
| 868 | } |
| 869 | |
Ezequiel Garcia | 86beeba | 2013-11-14 18:25:31 -0300 | [diff] [blame] | 870 | static void set_command_address(struct pxa3xx_nand_info *info, |
| 871 | unsigned int page_size, uint16_t column, int page_addr) |
| 872 | { |
| 873 | /* small page addr setting */ |
| 874 | if (page_size < PAGE_CHUNK_SIZE) { |
| 875 | info->ndcb1 = ((page_addr & 0xFFFFFF) << 8) |
| 876 | | (column & 0xFF); |
| 877 | |
| 878 | info->ndcb2 = 0; |
| 879 | } else { |
| 880 | info->ndcb1 = ((page_addr & 0xFFFF) << 16) |
| 881 | | (column & 0xFFFF); |
| 882 | |
| 883 | if (page_addr & 0xFF0000) |
| 884 | info->ndcb2 = (page_addr & 0xFF0000) >> 16; |
| 885 | else |
| 886 | info->ndcb2 = 0; |
| 887 | } |
| 888 | } |
| 889 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 890 | static void prepare_start_command(struct pxa3xx_nand_info *info, int command) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 891 | { |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 892 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 893 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 894 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 895 | /* reset data and oob column point to handle data */ |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 896 | info->buf_start = 0; |
| 897 | info->buf_count = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 898 | info->oob_size = 0; |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 899 | info->data_buff_pos = 0; |
| 900 | info->oob_buff_pos = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 901 | info->use_ecc = 0; |
Ezequiel Garcia | 5bb653e | 2013-08-12 14:14:49 -0300 | [diff] [blame] | 902 | info->use_spare = 1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 903 | info->retcode = ERR_NONE; |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 904 | info->ecc_err_cnt = 0; |
Ezequiel Garcia | f0e6a32e | 2013-11-14 18:25:30 -0300 | [diff] [blame] | 905 | info->ndcb3 = 0; |
Ezequiel Garcia | d20d0a6 | 2013-12-18 18:44:08 -0300 | [diff] [blame] | 906 | info->need_wait = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 907 | |
| 908 | switch (command) { |
| 909 | case NAND_CMD_READ0: |
| 910 | case NAND_CMD_PAGEPROG: |
| 911 | info->use_ecc = 1; |
| 912 | case NAND_CMD_READOOB: |
Ezequiel Garcia | fa543be | 2013-11-14 18:25:36 -0300 | [diff] [blame] | 913 | pxa3xx_set_datasize(info, mtd); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 914 | break; |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 915 | case NAND_CMD_PARAM: |
| 916 | info->use_spare = 0; |
| 917 | break; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 918 | default: |
| 919 | info->ndcb1 = 0; |
| 920 | info->ndcb2 = 0; |
| 921 | break; |
| 922 | } |
Ezequiel Garcia | 39f83d1 | 2013-11-14 18:25:34 -0300 | [diff] [blame] | 923 | |
| 924 | /* |
| 925 | * If we are about to issue a read command, or about to set |
| 926 | * the write address, then clean the data buffer. |
| 927 | */ |
| 928 | if (command == NAND_CMD_READ0 || |
| 929 | command == NAND_CMD_READOOB || |
| 930 | command == NAND_CMD_SEQIN) { |
| 931 | |
| 932 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 933 | memset(info->data_buff, 0xFF, info->buf_count); |
| 934 | } |
| 935 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int prepare_set_command(struct pxa3xx_nand_info *info, int command, |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 939 | int ext_cmd_type, uint16_t column, int page_addr) |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 940 | { |
| 941 | int addr_cycle, exec_cmd; |
| 942 | struct pxa3xx_nand_host *host; |
| 943 | struct mtd_info *mtd; |
| 944 | |
| 945 | host = info->host[info->cs]; |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 946 | mtd = nand_to_mtd(&host->chip); |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 947 | addr_cycle = 0; |
| 948 | exec_cmd = 1; |
| 949 | |
| 950 | if (info->cs != 0) |
| 951 | info->ndcb0 = NDCB0_CSEL; |
| 952 | else |
| 953 | info->ndcb0 = 0; |
| 954 | |
| 955 | if (command == NAND_CMD_SEQIN) |
| 956 | exec_cmd = 0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 957 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 958 | addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles |
| 959 | + host->col_addr_cycles); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 960 | |
| 961 | switch (command) { |
| 962 | case NAND_CMD_READOOB: |
| 963 | case NAND_CMD_READ0: |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 964 | info->buf_start = column; |
| 965 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 966 | | addr_cycle |
| 967 | | NAND_CMD_READ0; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 968 | |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 969 | if (command == NAND_CMD_READOOB) |
| 970 | info->buf_start += mtd->writesize; |
| 971 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 972 | /* |
| 973 | * Multiple page read needs an 'extended command type' field, |
| 974 | * which is either naked-read or last-read according to the |
| 975 | * state. |
| 976 | */ |
| 977 | if (mtd->writesize == PAGE_CHUNK_SIZE) { |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 978 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8); |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 979 | } else if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 980 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
| 981 | | NDCB0_LEN_OVRD |
| 982 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 983 | info->ndcb3 = info->chunk_size + |
| 984 | info->oob_size; |
| 985 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 986 | |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 987 | set_command_address(info, mtd->writesize, column, page_addr); |
Ezequiel Garcia | 01d9947 | 2013-11-14 18:25:32 -0300 | [diff] [blame] | 988 | break; |
| 989 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 990 | case NAND_CMD_SEQIN: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 991 | |
Ezequiel Garcia | e7f9a6a | 2013-11-14 18:25:35 -0300 | [diff] [blame] | 992 | info->buf_start = column; |
| 993 | set_command_address(info, mtd->writesize, 0, page_addr); |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Multiple page programming needs to execute the initial |
| 997 | * SEQIN command that sets the page address. |
| 998 | */ |
| 999 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 1000 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 1001 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 1002 | | addr_cycle |
| 1003 | | command; |
| 1004 | /* No data transfer in this case */ |
| 1005 | info->data_size = 0; |
| 1006 | exec_cmd = 1; |
| 1007 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1008 | break; |
| 1009 | |
| 1010 | case NAND_CMD_PAGEPROG: |
| 1011 | if (is_buf_blank(info->data_buff, |
| 1012 | (mtd->writesize + mtd->oobsize))) { |
| 1013 | exec_cmd = 0; |
| 1014 | break; |
| 1015 | } |
| 1016 | |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1017 | /* Second command setting for large pages */ |
| 1018 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 1019 | /* |
| 1020 | * Multiple page write uses the 'extended command' |
| 1021 | * field. This can be used to issue a command dispatch |
| 1022 | * or a naked-write depending on the current stage. |
| 1023 | */ |
| 1024 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 1025 | | NDCB0_LEN_OVRD |
| 1026 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 1027 | info->ndcb3 = info->chunk_size + |
| 1028 | info->oob_size; |
| 1029 | |
| 1030 | /* |
| 1031 | * This is the command dispatch that completes a chunked |
| 1032 | * page program operation. |
| 1033 | */ |
| 1034 | if (info->data_size == 0) { |
| 1035 | info->ndcb0 = NDCB0_CMD_TYPE(0x1) |
| 1036 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 1037 | | command; |
| 1038 | info->ndcb1 = 0; |
| 1039 | info->ndcb2 = 0; |
| 1040 | info->ndcb3 = 0; |
| 1041 | } |
| 1042 | } else { |
| 1043 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 1044 | | NDCB0_AUTO_RS |
| 1045 | | NDCB0_ST_ROW_EN |
| 1046 | | NDCB0_DBC |
| 1047 | | (NAND_CMD_PAGEPROG << 8) |
| 1048 | | NAND_CMD_SEQIN |
| 1049 | | addr_cycle; |
| 1050 | } |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1051 | break; |
| 1052 | |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 1053 | case NAND_CMD_PARAM: |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 1054 | info->buf_count = INIT_BUFFER_SIZE; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 1055 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 1056 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | 41a6343 | 2013-08-12 14:14:51 -0300 | [diff] [blame] | 1057 | | NDCB0_LEN_OVRD |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 1058 | | command; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 1059 | info->ndcb1 = (column & 0xFF); |
Ezequiel Garcia | c163409 | 2015-08-03 11:31:26 -0300 | [diff] [blame] | 1060 | info->ndcb3 = INIT_BUFFER_SIZE; |
| 1061 | info->data_size = INIT_BUFFER_SIZE; |
Ezequiel Garcia | ce0268f | 2013-05-14 08:15:25 -0300 | [diff] [blame] | 1062 | break; |
| 1063 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1064 | case NAND_CMD_READID: |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 1065 | info->buf_count = READ_ID_BYTES; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1066 | info->ndcb0 |= NDCB0_CMD_TYPE(3) |
| 1067 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 1068 | | command; |
Ezequiel Garcia | d14231f | 2013-05-14 08:15:24 -0300 | [diff] [blame] | 1069 | info->ndcb1 = (column & 0xFF); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1070 | |
| 1071 | info->data_size = 8; |
| 1072 | break; |
| 1073 | case NAND_CMD_STATUS: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1074 | info->buf_count = 1; |
| 1075 | info->ndcb0 |= NDCB0_CMD_TYPE(4) |
| 1076 | | NDCB0_ADDR_CYC(1) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 1077 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1078 | |
| 1079 | info->data_size = 8; |
| 1080 | break; |
| 1081 | |
| 1082 | case NAND_CMD_ERASE1: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1083 | info->ndcb0 |= NDCB0_CMD_TYPE(2) |
| 1084 | | NDCB0_AUTO_RS |
| 1085 | | NDCB0_ADDR_CYC(3) |
| 1086 | | NDCB0_DBC |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 1087 | | (NAND_CMD_ERASE2 << 8) |
| 1088 | | NAND_CMD_ERASE1; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1089 | info->ndcb1 = page_addr; |
| 1090 | info->ndcb2 = 0; |
| 1091 | |
| 1092 | break; |
| 1093 | case NAND_CMD_RESET: |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1094 | info->ndcb0 |= NDCB0_CMD_TYPE(5) |
Ezequiel Garcia | ec82135 | 2013-08-12 14:14:54 -0300 | [diff] [blame] | 1095 | | command; |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1096 | |
| 1097 | break; |
| 1098 | |
| 1099 | case NAND_CMD_ERASE2: |
| 1100 | exec_cmd = 0; |
| 1101 | break; |
| 1102 | |
| 1103 | default: |
| 1104 | exec_cmd = 0; |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1105 | dev_err(&info->pdev->dev, "non-supported command %x\n", |
| 1106 | command); |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | return exec_cmd; |
| 1111 | } |
| 1112 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1113 | static void nand_cmdfunc(struct mtd_info *mtd, unsigned command, |
| 1114 | int column, int page_addr) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1115 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1116 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1117 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1118 | struct pxa3xx_nand_info *info = host->info_data; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1119 | int exec_cmd; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1120 | |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1121 | /* |
| 1122 | * if this is a x16 device ,then convert the input |
| 1123 | * "byte" address into a "word" address appropriate |
| 1124 | * for indexing a word-oriented device |
| 1125 | */ |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1126 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | 4eb2da8 | 2011-02-28 10:32:13 +0800 | [diff] [blame] | 1127 | column /= 2; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1128 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1129 | /* |
| 1130 | * There may be different NAND chip hooked to |
| 1131 | * different chip select, so check whether |
| 1132 | * chip select has been changed, if yes, reset the timing |
| 1133 | */ |
| 1134 | if (info->cs != host->cs) { |
| 1135 | info->cs = host->cs; |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1136 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1137 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Ezequiel Garcia | c39ff03 | 2013-11-14 18:25:33 -0300 | [diff] [blame] | 1140 | prepare_start_command(info, command); |
| 1141 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1142 | info->state = STATE_PREPARED; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1143 | exec_cmd = prepare_set_command(info, command, 0, column, page_addr); |
| 1144 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1145 | if (exec_cmd) { |
| 1146 | init_completion(&info->cmd_complete); |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1147 | init_completion(&info->dev_ready); |
| 1148 | info->need_wait = 1; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1149 | pxa3xx_nand_start(info); |
| 1150 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1151 | if (!wait_for_completion_timeout(&info->cmd_complete, |
| 1152 | CHIP_DELAY_TIMEOUT)) { |
Lei Wen | da675b4 | 2011-07-14 20:44:31 -0700 | [diff] [blame] | 1153 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1154 | /* Stop State Machine for next command cycle */ |
| 1155 | pxa3xx_nand_stop(info); |
| 1156 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1157 | } |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1158 | info->state = STATE_IDLE; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1161 | static void nand_cmdfunc_extended(struct mtd_info *mtd, |
| 1162 | const unsigned command, |
| 1163 | int column, int page_addr) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1164 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1165 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1166 | struct pxa3xx_nand_host *host = chip->priv; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1167 | struct pxa3xx_nand_info *info = host->info_data; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1168 | int exec_cmd, ext_cmd_type; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | * if this is a x16 device then convert the input |
| 1172 | * "byte" address into a "word" address appropriate |
| 1173 | * for indexing a word-oriented device |
| 1174 | */ |
| 1175 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
| 1176 | column /= 2; |
| 1177 | |
| 1178 | /* |
| 1179 | * There may be different NAND chip hooked to |
| 1180 | * different chip select, so check whether |
| 1181 | * chip select has been changed, if yes, reset the timing |
| 1182 | */ |
| 1183 | if (info->cs != host->cs) { |
| 1184 | info->cs = host->cs; |
| 1185 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1186 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
| 1187 | } |
| 1188 | |
| 1189 | /* Select the extended command for the first command */ |
| 1190 | switch (command) { |
| 1191 | case NAND_CMD_READ0: |
| 1192 | case NAND_CMD_READOOB: |
| 1193 | ext_cmd_type = EXT_CMD_TYPE_MONO; |
| 1194 | break; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1195 | case NAND_CMD_SEQIN: |
| 1196 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
| 1197 | break; |
| 1198 | case NAND_CMD_PAGEPROG: |
| 1199 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
| 1200 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1201 | default: |
| 1202 | ext_cmd_type = 0; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1203 | break; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | prepare_start_command(info, command); |
| 1207 | |
| 1208 | /* |
| 1209 | * Prepare the "is ready" completion before starting a command |
| 1210 | * transaction sequence. If the command is not executed the |
| 1211 | * completion will be completed, see below. |
| 1212 | * |
| 1213 | * We can do that inside the loop because the command variable |
| 1214 | * is invariant and thus so is the exec_cmd. |
| 1215 | */ |
| 1216 | info->need_wait = 1; |
| 1217 | init_completion(&info->dev_ready); |
| 1218 | do { |
| 1219 | info->state = STATE_PREPARED; |
| 1220 | exec_cmd = prepare_set_command(info, command, ext_cmd_type, |
| 1221 | column, page_addr); |
| 1222 | if (!exec_cmd) { |
| 1223 | info->need_wait = 0; |
| 1224 | complete(&info->dev_ready); |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | init_completion(&info->cmd_complete); |
| 1229 | pxa3xx_nand_start(info); |
| 1230 | |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1231 | if (!wait_for_completion_timeout(&info->cmd_complete, |
| 1232 | CHIP_DELAY_TIMEOUT)) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1233 | dev_err(&info->pdev->dev, "Wait time out!!!\n"); |
| 1234 | /* Stop State Machine for next command cycle */ |
| 1235 | pxa3xx_nand_stop(info); |
| 1236 | break; |
| 1237 | } |
| 1238 | |
| 1239 | /* Check if the sequence is complete */ |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1240 | if (info->data_size == 0 && command != NAND_CMD_PAGEPROG) |
| 1241 | break; |
| 1242 | |
| 1243 | /* |
| 1244 | * After a splitted program command sequence has issued |
| 1245 | * the command dispatch, the command sequence is complete. |
| 1246 | */ |
| 1247 | if (info->data_size == 0 && |
| 1248 | command == NAND_CMD_PAGEPROG && |
| 1249 | ext_cmd_type == EXT_CMD_TYPE_DISPATCH) |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1250 | break; |
| 1251 | |
| 1252 | if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) { |
| 1253 | /* Last read: issue a 'last naked read' */ |
| 1254 | if (info->data_size == info->chunk_size) |
| 1255 | ext_cmd_type = EXT_CMD_TYPE_LAST_RW; |
| 1256 | else |
| 1257 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
Ezequiel Garcia | 535cb57 | 2013-11-14 18:25:38 -0300 | [diff] [blame] | 1258 | |
| 1259 | /* |
| 1260 | * If a splitted program command has no more data to transfer, |
| 1261 | * the command dispatch must be issued to complete. |
| 1262 | */ |
| 1263 | } else if (command == NAND_CMD_PAGEPROG && |
| 1264 | info->data_size == 0) { |
| 1265 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1266 | } |
| 1267 | } while (1); |
| 1268 | |
| 1269 | info->state = STATE_IDLE; |
| 1270 | } |
| 1271 | |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1272 | static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1273 | struct nand_chip *chip, const uint8_t *buf, int oob_required, |
| 1274 | int page) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1275 | { |
| 1276 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1277 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1278 | |
| 1279 | return 0; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1283 | struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 1284 | int page) |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1285 | { |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1286 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1287 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1288 | |
| 1289 | chip->read_buf(mtd, buf, mtd->writesize); |
| 1290 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1291 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1292 | if (info->retcode == ERR_CORERR && info->use_ecc) { |
| 1293 | mtd->ecc_stats.corrected += info->ecc_err_cnt; |
| 1294 | |
| 1295 | } else if (info->retcode == ERR_UNCORERR) { |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1296 | /* |
| 1297 | * for blank page (all 0xff), HW will calculate its ECC as |
| 1298 | * 0, which is different from the ECC information within |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1299 | * OOB, ignore such uncorrectable errors |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1300 | */ |
| 1301 | if (is_buf_blank(buf, mtd->writesize)) |
Daniel Mack | 543e32d | 2011-06-07 03:01:07 -0700 | [diff] [blame] | 1302 | info->retcode = ERR_NONE; |
| 1303 | else |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1304 | mtd->ecc_stats.failed++; |
| 1305 | } |
| 1306 | |
Ezequiel Garcia | 87f5336 | 2013-11-14 18:25:39 -0300 | [diff] [blame] | 1307 | return info->max_bitflips; |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1308 | } |
| 1309 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1310 | static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd) |
| 1311 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1312 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1313 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1314 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1315 | char retval = 0xFF; |
| 1316 | |
| 1317 | if (info->buf_start < info->buf_count) |
| 1318 | /* Has just send a new command? */ |
| 1319 | retval = info->data_buff[info->buf_start++]; |
| 1320 | |
| 1321 | return retval; |
| 1322 | } |
| 1323 | |
| 1324 | static u16 pxa3xx_nand_read_word(struct mtd_info *mtd) |
| 1325 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1326 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1327 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1328 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1329 | u16 retval = 0xFFFF; |
| 1330 | |
| 1331 | if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) { |
| 1332 | retval = *((u16 *)(info->data_buff+info->buf_start)); |
| 1333 | info->buf_start += 2; |
| 1334 | } |
| 1335 | return retval; |
| 1336 | } |
| 1337 | |
| 1338 | static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 1339 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1340 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1341 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1342 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1343 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1344 | |
| 1345 | memcpy(buf, info->data_buff + info->buf_start, real_len); |
| 1346 | info->buf_start += real_len; |
| 1347 | } |
| 1348 | |
| 1349 | static void pxa3xx_nand_write_buf(struct mtd_info *mtd, |
| 1350 | const uint8_t *buf, int len) |
| 1351 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1352 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1353 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1354 | struct pxa3xx_nand_info *info = host->info_data; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1355 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1356 | |
| 1357 | memcpy(info->data_buff + info->buf_start, buf, real_len); |
| 1358 | info->buf_start += real_len; |
| 1359 | } |
| 1360 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1361 | static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip) |
| 1362 | { |
| 1363 | return; |
| 1364 | } |
| 1365 | |
| 1366 | static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 1367 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1368 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1369 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1370 | struct pxa3xx_nand_info *info = host->info_data; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1371 | |
| 1372 | if (info->need_wait) { |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1373 | info->need_wait = 0; |
Nicholas Mc Guire | e5860c1 | 2015-02-01 11:55:37 -0500 | [diff] [blame] | 1374 | if (!wait_for_completion_timeout(&info->dev_ready, |
| 1375 | CHIP_DELAY_TIMEOUT)) { |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1376 | dev_err(&info->pdev->dev, "Ready time out!!!\n"); |
| 1377 | return NAND_STATUS_FAIL; |
| 1378 | } |
| 1379 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1380 | |
| 1381 | /* pxa3xx_nand_send_command has waited for command complete */ |
| 1382 | if (this->state == FL_WRITING || this->state == FL_ERASING) { |
| 1383 | if (info->retcode == ERR_NONE) |
| 1384 | return 0; |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1385 | else |
| 1386 | return NAND_STATUS_FAIL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Ezequiel Garcia | 55d9fd6 | 2013-11-14 18:25:26 -0300 | [diff] [blame] | 1389 | return NAND_STATUS_READY; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1392 | static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1393 | { |
Ezequiel García | b1e4857 | 2015-11-04 13:13:44 -0300 | [diff] [blame] | 1394 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1395 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1396 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Ezequiel García | b1e4857 | 2015-11-04 13:13:44 -0300 | [diff] [blame] | 1397 | const struct nand_sdr_timings *timings; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1398 | |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1399 | /* Configure default flash values */ |
| 1400 | info->chunk_size = PAGE_CHUNK_SIZE; |
Antoine Ténart | f19fe98 | 2015-10-21 10:29:03 +0200 | [diff] [blame] | 1401 | info->reg_ndcr = 0x0; /* enable all interrupts */ |
| 1402 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
| 1403 | info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES); |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1404 | info->reg_ndcr |= NDCR_SPARE_EN; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1405 | |
Ezequiel García | b1e4857 | 2015-11-04 13:13:44 -0300 | [diff] [blame] | 1406 | /* use the common timing to make a try */ |
| 1407 | timings = onfi_async_timing_mode_to_sdr_timings(0); |
| 1408 | if (IS_ERR(timings)) |
| 1409 | return PTR_ERR(timings); |
| 1410 | |
| 1411 | pxa3xx_nand_set_sdr_timing(host, timings); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1412 | return 0; |
| 1413 | } |
| 1414 | |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1415 | static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info) |
| 1416 | { |
| 1417 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 1418 | struct nand_chip *chip = &host->chip; |
| 1419 | struct mtd_info *mtd = nand_to_mtd(chip); |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1420 | |
| 1421 | info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0; |
| 1422 | info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0; |
| 1423 | info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0; |
| 1424 | } |
| 1425 | |
Ezequiel García | 154f50f | 2015-11-04 13:13:43 -0300 | [diff] [blame] | 1426 | static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info) |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1427 | { |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1428 | struct platform_device *pdev = info->pdev; |
| 1429 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1430 | uint32_t ndcr = nand_readl(info, NDCR); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1431 | |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1432 | /* Set an initial chunk size */ |
Ezequiel García | b226eca | 2015-08-19 19:40:09 -0300 | [diff] [blame] | 1433 | info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512; |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 1434 | info->reg_ndcr = ndcr & |
| 1435 | ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL); |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1436 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1437 | info->ndtr0cs0 = nand_readl(info, NDTR0CS0); |
| 1438 | info->ndtr1cs0 = nand_readl(info, NDTR1CS0); |
Mike Rapoport | f271049 | 2009-02-17 13:54:47 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1441 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 1442 | { |
| 1443 | struct platform_device *pdev = info->pdev; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1444 | struct dma_slave_config config; |
| 1445 | dma_cap_mask_t mask; |
| 1446 | struct pxad_param param; |
| 1447 | int ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1448 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1449 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1450 | if (info->data_buff == NULL) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1451 | return -ENOMEM; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1452 | if (use_dma == 0) |
| 1453 | return 0; |
| 1454 | |
| 1455 | ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
| 1456 | if (ret) |
| 1457 | return ret; |
| 1458 | |
| 1459 | sg_init_one(&info->sg, info->data_buff, info->buf_size); |
| 1460 | dma_cap_zero(mask); |
| 1461 | dma_cap_set(DMA_SLAVE, mask); |
| 1462 | param.prio = PXAD_PRIO_LOWEST; |
| 1463 | param.drcmr = info->drcmr_dat; |
| 1464 | info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn, |
| 1465 | ¶m, &pdev->dev, |
| 1466 | "data"); |
| 1467 | if (!info->dma_chan) { |
| 1468 | dev_err(&pdev->dev, "unable to request data dma channel\n"); |
| 1469 | return -ENODEV; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1472 | memset(&config, 0, sizeof(config)); |
| 1473 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1474 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1475 | config.src_addr = info->mmio_phys + NDDB; |
| 1476 | config.dst_addr = info->mmio_phys + NDDB; |
| 1477 | config.src_maxburst = 32; |
| 1478 | config.dst_maxburst = 32; |
| 1479 | ret = dmaengine_slave_config(info->dma_chan, &config); |
| 1480 | if (ret < 0) { |
| 1481 | dev_err(&info->pdev->dev, |
| 1482 | "dma channel configuration failed: %d\n", |
| 1483 | ret); |
| 1484 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1485 | } |
| 1486 | |
Ezequiel Garcia | 95b2656 | 2013-10-04 15:30:37 -0300 | [diff] [blame] | 1487 | /* |
| 1488 | * Now that DMA buffers are allocated we turn on |
| 1489 | * DMA proper for I/O operations. |
| 1490 | */ |
| 1491 | info->use_dma = 1; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1492 | return 0; |
| 1493 | } |
| 1494 | |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1495 | static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info) |
| 1496 | { |
Ezequiel Garcia | 15b540c | 2013-12-10 09:57:15 -0300 | [diff] [blame] | 1497 | if (info->use_dma) { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1498 | dmaengine_terminate_all(info->dma_chan); |
| 1499 | dma_release_channel(info->dma_chan); |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1500 | } |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1501 | kfree(info->data_buff); |
| 1502 | } |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1503 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1504 | static int pxa_ecc_init(struct pxa3xx_nand_info *info, |
| 1505 | struct nand_ecc_ctrl *ecc, |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1506 | int strength, int ecc_stepsize, int page_size) |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1507 | { |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1508 | if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1509 | info->chunk_size = 2048; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1510 | info->spare_size = 40; |
| 1511 | info->ecc_size = 24; |
| 1512 | ecc->mode = NAND_ECC_HW; |
| 1513 | ecc->size = 512; |
| 1514 | ecc->strength = 1; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1515 | |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1516 | } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1517 | info->chunk_size = 512; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1518 | info->spare_size = 8; |
| 1519 | info->ecc_size = 8; |
| 1520 | ecc->mode = NAND_ECC_HW; |
| 1521 | ecc->size = 512; |
| 1522 | ecc->strength = 1; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1523 | |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1524 | /* |
| 1525 | * Required ECC: 4-bit correction per 512 bytes |
| 1526 | * Select: 16-bit correction per 2048 bytes |
| 1527 | */ |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 1528 | } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) { |
| 1529 | info->ecc_bch = 1; |
| 1530 | info->chunk_size = 2048; |
| 1531 | info->spare_size = 32; |
| 1532 | info->ecc_size = 32; |
| 1533 | ecc->mode = NAND_ECC_HW; |
| 1534 | ecc->size = info->chunk_size; |
| 1535 | ecc->layout = &ecc_layout_2KB_bch4bit; |
| 1536 | ecc->strength = 16; |
Rodolfo Giometti | 3db227b | 2014-01-13 15:35:38 +0100 | [diff] [blame] | 1537 | |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1538 | } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1539 | info->ecc_bch = 1; |
| 1540 | info->chunk_size = 2048; |
| 1541 | info->spare_size = 32; |
| 1542 | info->ecc_size = 32; |
| 1543 | ecc->mode = NAND_ECC_HW; |
| 1544 | ecc->size = info->chunk_size; |
| 1545 | ecc->layout = &ecc_layout_4KB_bch4bit; |
| 1546 | ecc->strength = 16; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1547 | |
Brian Norris | 6033a94 | 2013-11-14 14:41:32 -0800 | [diff] [blame] | 1548 | /* |
| 1549 | * Required ECC: 8-bit correction per 512 bytes |
| 1550 | * Select: 16-bit correction per 1024 bytes |
| 1551 | */ |
| 1552 | } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) { |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1553 | info->ecc_bch = 1; |
| 1554 | info->chunk_size = 1024; |
| 1555 | info->spare_size = 0; |
| 1556 | info->ecc_size = 32; |
| 1557 | ecc->mode = NAND_ECC_HW; |
| 1558 | ecc->size = info->chunk_size; |
| 1559 | ecc->layout = &ecc_layout_4KB_bch8bit; |
| 1560 | ecc->strength = 16; |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1561 | } else { |
| 1562 | dev_err(&info->pdev->dev, |
| 1563 | "ECC strength %d at page size %d is not supported\n", |
| 1564 | strength, page_size); |
| 1565 | return -ENODEV; |
Ezequiel Garcia | 70ed852 | 2013-11-14 18:25:37 -0300 | [diff] [blame] | 1566 | } |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1567 | |
| 1568 | dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n", |
| 1569 | ecc->strength, ecc->size); |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1573 | static int pxa3xx_nand_scan(struct mtd_info *mtd) |
| 1574 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1575 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1576 | struct pxa3xx_nand_host *host = chip->priv; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1577 | struct pxa3xx_nand_info *info = host->info_data; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1578 | struct platform_device *pdev = info->pdev; |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1579 | struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Antoine Ténart | f19fe98 | 2015-10-21 10:29:03 +0200 | [diff] [blame] | 1580 | int ret; |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1581 | uint16_t ecc_strength, ecc_step; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1582 | |
Ezequiel García | 154f50f | 2015-11-04 13:13:43 -0300 | [diff] [blame] | 1583 | if (pdata->keep_config) { |
| 1584 | pxa3xx_nand_detect_config(info); |
| 1585 | } else { |
| 1586 | ret = pxa3xx_nand_config_ident(info); |
| 1587 | if (ret) |
| 1588 | return ret; |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1589 | } |
| 1590 | |
Ezequiel Garcia | 48cf7ef | 2013-08-12 14:14:55 -0300 | [diff] [blame] | 1591 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1592 | chip->options |= NAND_BUSWIDTH_16; |
| 1593 | |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1594 | /* Device detection must be done with ECC disabled */ |
| 1595 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) |
| 1596 | nand_writel(info, NDECCCTRL, 0x0); |
| 1597 | |
Antoine Ténart | f19fe98 | 2015-10-21 10:29:03 +0200 | [diff] [blame] | 1598 | if (nand_scan_ident(mtd, 1, NULL)) |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1599 | return -ENODEV; |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1600 | |
Antoine Ténart | f19fe98 | 2015-10-21 10:29:03 +0200 | [diff] [blame] | 1601 | if (!pdata->keep_config) { |
| 1602 | ret = pxa3xx_nand_init(host); |
| 1603 | if (ret) { |
| 1604 | dev_err(&info->pdev->dev, "Failed to init nand: %d\n", |
| 1605 | ret); |
| 1606 | return ret; |
| 1607 | } |
| 1608 | } |
| 1609 | |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1610 | if (pdata->flash_bbt) { |
| 1611 | /* |
| 1612 | * We'll use a bad block table stored in-flash and don't |
| 1613 | * allow writing the bad block marker to the flash. |
| 1614 | */ |
| 1615 | chip->bbt_options |= NAND_BBT_USE_FLASH | |
| 1616 | NAND_BBT_NO_OOB_BBM; |
| 1617 | chip->bbt_td = &bbt_main_descr; |
| 1618 | chip->bbt_md = &bbt_mirror_descr; |
| 1619 | } |
| 1620 | |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1621 | /* |
| 1622 | * If the page size is bigger than the FIFO size, let's check |
| 1623 | * we are given the right variant and then switch to the extended |
| 1624 | * (aka splitted) command handling, |
| 1625 | */ |
| 1626 | if (mtd->writesize > PAGE_CHUNK_SIZE) { |
| 1627 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) { |
| 1628 | chip->cmdfunc = nand_cmdfunc_extended; |
| 1629 | } else { |
| 1630 | dev_err(&info->pdev->dev, |
| 1631 | "unsupported page size on this variant\n"); |
| 1632 | return -ENODEV; |
| 1633 | } |
| 1634 | } |
| 1635 | |
Ezequiel Garcia | 5b3e507 | 2014-05-14 14:58:08 -0300 | [diff] [blame] | 1636 | if (pdata->ecc_strength && pdata->ecc_step_size) { |
| 1637 | ecc_strength = pdata->ecc_strength; |
| 1638 | ecc_step = pdata->ecc_step_size; |
| 1639 | } else { |
| 1640 | ecc_strength = chip->ecc_strength_ds; |
| 1641 | ecc_step = chip->ecc_step_ds; |
| 1642 | } |
Ezequiel Garcia | 30b2afc | 2013-12-18 18:44:10 -0300 | [diff] [blame] | 1643 | |
| 1644 | /* Set default ECC strength requirements on non-ONFI devices */ |
| 1645 | if (ecc_strength < 1 && ecc_step < 1) { |
| 1646 | ecc_strength = 1; |
| 1647 | ecc_step = 512; |
| 1648 | } |
| 1649 | |
| 1650 | ret = pxa_ecc_init(info, &chip->ecc, ecc_strength, |
| 1651 | ecc_step, mtd->writesize); |
Ezequiel Garcia | eee0166 | 2014-05-14 14:58:07 -0300 | [diff] [blame] | 1652 | if (ret) |
| 1653 | return ret; |
Ezequiel Garcia | 43bcfd2 | 2013-11-14 18:25:29 -0300 | [diff] [blame] | 1654 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1655 | /* calculate addressing information */ |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1656 | if (mtd->writesize >= 2048) |
| 1657 | host->col_addr_cycles = 2; |
| 1658 | else |
| 1659 | host->col_addr_cycles = 1; |
| 1660 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1661 | /* release the initial buffer */ |
| 1662 | kfree(info->data_buff); |
| 1663 | |
| 1664 | /* allocate the real data + oob buffer */ |
| 1665 | info->buf_size = mtd->writesize + mtd->oobsize; |
| 1666 | ret = pxa3xx_nand_init_buff(info); |
| 1667 | if (ret) |
| 1668 | return ret; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1669 | info->oob_buff = info->data_buff + mtd->writesize; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1670 | |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1671 | if ((mtd->size >> chip->page_shift) > 65536) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1672 | host->row_addr_cycles = 3; |
Lei Wen | 4332c11 | 2011-03-03 11:27:01 +0800 | [diff] [blame] | 1673 | else |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1674 | host->row_addr_cycles = 2; |
Ezequiel García | 66e8e47 | 2015-11-04 13:13:42 -0300 | [diff] [blame] | 1675 | |
| 1676 | if (!pdata->keep_config) |
| 1677 | pxa3xx_nand_config_tail(info); |
| 1678 | |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1679 | return nand_scan_tail(mtd); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1680 | } |
| 1681 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1682 | static int alloc_nand_resource(struct platform_device *pdev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1683 | { |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1684 | struct device_node *np = pdev->dev.of_node; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1685 | struct pxa3xx_nand_platform_data *pdata; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1686 | struct pxa3xx_nand_info *info; |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1687 | struct pxa3xx_nand_host *host; |
Haojian Zhuang | 6e308f8 | 2012-08-20 13:40:31 +0800 | [diff] [blame] | 1688 | struct nand_chip *chip = NULL; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1689 | struct mtd_info *mtd; |
| 1690 | struct resource *r; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1691 | int ret, irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1692 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1693 | pdata = dev_get_platdata(&pdev->dev); |
Robert Jarzmik | e423c90 | 2015-02-08 21:02:09 +0100 | [diff] [blame] | 1694 | if (pdata->num_cs <= 0) |
| 1695 | return -ENODEV; |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 1696 | info = devm_kzalloc(&pdev->dev, |
| 1697 | sizeof(*info) + sizeof(*host) * pdata->num_cs, |
| 1698 | GFP_KERNEL); |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1699 | if (!info) |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1700 | return -ENOMEM; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1701 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1702 | info->pdev = pdev; |
Ezequiel Garcia | c7e9c7e | 2013-11-07 12:17:14 -0300 | [diff] [blame] | 1703 | info->variant = pxa3xx_nand_get_variant(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1704 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 1705 | host = (void *)&info[1] + sizeof(*host) * cs; |
| 1706 | chip = &host->chip; |
| 1707 | chip->priv = host; |
| 1708 | mtd = nand_to_mtd(chip); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1709 | info->host[cs] = host; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1710 | host->cs = cs; |
| 1711 | host->info_data = info; |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1712 | mtd->priv = chip; |
Frans Klaver | 550dab5 | 2015-06-10 22:39:01 +0200 | [diff] [blame] | 1713 | mtd->dev.parent = &pdev->dev; |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1714 | /* FIXME: all chips use the same device tree partitions */ |
| 1715 | nand_set_flash_node(chip, np); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1716 | |
Boris BREZILLON | 1d8d8b5 | 2015-11-16 14:37:34 +0100 | [diff] [blame] | 1717 | chip->priv = host; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1718 | chip->ecc.read_page = pxa3xx_nand_read_page_hwecc; |
| 1719 | chip->ecc.write_page = pxa3xx_nand_write_page_hwecc; |
| 1720 | chip->controller = &info->controller; |
| 1721 | chip->waitfunc = pxa3xx_nand_waitfunc; |
| 1722 | chip->select_chip = pxa3xx_nand_select_chip; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1723 | chip->read_word = pxa3xx_nand_read_word; |
| 1724 | chip->read_byte = pxa3xx_nand_read_byte; |
| 1725 | chip->read_buf = pxa3xx_nand_read_buf; |
| 1726 | chip->write_buf = pxa3xx_nand_write_buf; |
Ezequiel Garcia | 664c7f5 | 2013-11-07 12:17:12 -0300 | [diff] [blame] | 1727 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Ezequiel Garcia | 5cbbdc6 | 2013-12-18 18:44:09 -0300 | [diff] [blame] | 1728 | chip->cmdfunc = nand_cmdfunc; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1729 | } |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1730 | |
| 1731 | spin_lock_init(&chip->controller->lock); |
| 1732 | init_waitqueue_head(&chip->controller->wq); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1733 | info->clk = devm_clk_get(&pdev->dev, NULL); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1734 | if (IS_ERR(info->clk)) { |
| 1735 | dev_err(&pdev->dev, "failed to get nand clock\n"); |
Ezequiel Garcia | 4c073cd | 2013-04-17 13:38:09 -0300 | [diff] [blame] | 1736 | return PTR_ERR(info->clk); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1737 | } |
Ezequiel Garcia | 1f8eaff | 2013-04-17 13:38:13 -0300 | [diff] [blame] | 1738 | ret = clk_prepare_enable(info->clk); |
| 1739 | if (ret < 0) |
| 1740 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1741 | |
Ezequiel Garcia | 6b45c1e | 2013-08-12 14:14:58 -0300 | [diff] [blame] | 1742 | if (use_dma) { |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1743 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1744 | if (r == NULL) { |
| 1745 | dev_err(&pdev->dev, |
| 1746 | "no resource defined for data DMA\n"); |
| 1747 | ret = -ENXIO; |
| 1748 | goto fail_disable_clk; |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1749 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1750 | info->drcmr_dat = r->start; |
| 1751 | |
| 1752 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1753 | if (r == NULL) { |
| 1754 | dev_err(&pdev->dev, |
| 1755 | "no resource defined for cmd DMA\n"); |
| 1756 | ret = -ENXIO; |
| 1757 | goto fail_disable_clk; |
| 1758 | } |
| 1759 | info->drcmr_cmd = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1760 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1761 | |
| 1762 | irq = platform_get_irq(pdev, 0); |
| 1763 | if (irq < 0) { |
| 1764 | dev_err(&pdev->dev, "no IRQ resource defined\n"); |
| 1765 | ret = -ENXIO; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1766 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ezequiel Garcia | 0ddd846 | 2013-04-17 13:38:10 -0300 | [diff] [blame] | 1770 | info->mmio_base = devm_ioremap_resource(&pdev->dev, r); |
| 1771 | if (IS_ERR(info->mmio_base)) { |
| 1772 | ret = PTR_ERR(info->mmio_base); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1773 | goto fail_disable_clk; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1774 | } |
Haojian Zhuang | 8638fac | 2009-09-10 14:11:44 +0800 | [diff] [blame] | 1775 | info->mmio_phys = r->start; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1776 | |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1777 | /* Allocate a buffer to allow flash detection */ |
| 1778 | info->buf_size = INIT_BUFFER_SIZE; |
| 1779 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1780 | if (info->data_buff == NULL) { |
| 1781 | ret = -ENOMEM; |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1782 | goto fail_disable_clk; |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1783 | } |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1784 | |
Haojian Zhuang | 346e125 | 2009-09-10 14:27:23 +0800 | [diff] [blame] | 1785 | /* initialize all interrupts to be disabled */ |
| 1786 | disable_int(info, NDSR_MASK); |
| 1787 | |
Robert Jarzmik | 2454225 | 2015-02-20 19:36:43 +0100 | [diff] [blame] | 1788 | ret = request_threaded_irq(irq, pxa3xx_nand_irq, |
| 1789 | pxa3xx_nand_irq_thread, IRQF_ONESHOT, |
| 1790 | pdev->name, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1791 | if (ret < 0) { |
| 1792 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 1793 | goto fail_free_buf; |
| 1794 | } |
| 1795 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1796 | platform_set_drvdata(pdev, info); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1797 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1798 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1799 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1800 | fail_free_buf: |
Lei Wen | 401e67e | 2011-02-28 10:32:14 +0800 | [diff] [blame] | 1801 | free_irq(irq, info); |
Ezequiel Garcia | 62e8b85 | 2013-10-04 15:30:38 -0300 | [diff] [blame] | 1802 | kfree(info->data_buff); |
Ezequiel Garcia | 9ca7944 | 2013-04-17 13:38:11 -0300 | [diff] [blame] | 1803 | fail_disable_clk: |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1804 | clk_disable_unprepare(info->clk); |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1805 | return ret; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | static int pxa3xx_nand_remove(struct platform_device *pdev) |
| 1809 | { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1810 | struct pxa3xx_nand_info *info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1811 | struct pxa3xx_nand_platform_data *pdata; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1812 | int irq, cs; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1813 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1814 | if (!info) |
| 1815 | return 0; |
| 1816 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1817 | pdata = dev_get_platdata(&pdev->dev); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1818 | |
Haojian Zhuang | dbf5986 | 2009-09-10 14:22:55 +0800 | [diff] [blame] | 1819 | irq = platform_get_irq(pdev, 0); |
| 1820 | if (irq >= 0) |
| 1821 | free_irq(irq, info); |
Ezequiel Garcia | 498b614 | 2013-04-17 13:38:14 -0300 | [diff] [blame] | 1822 | pxa3xx_nand_free_buff(info); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1823 | |
Robert Jarzmik | e971aff | 2015-09-28 22:56:51 +0200 | [diff] [blame] | 1824 | /* |
| 1825 | * In the pxa3xx case, the DFI bus is shared between the SMC and NFC. |
| 1826 | * In order to prevent a lockup of the system bus, the DFI bus |
| 1827 | * arbitration is granted to SMC upon driver removal. This is done by |
| 1828 | * setting the x_ARB_CNTL bit, which also prevents the NAND to have |
| 1829 | * access to the bus anymore. |
| 1830 | */ |
| 1831 | nand_writel(info, NDCR, |
| 1832 | (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) | |
| 1833 | NFCV1_NDCR_ARB_CNTL); |
Ezequiel Garcia | fb32061 | 2013-04-17 13:38:12 -0300 | [diff] [blame] | 1834 | clk_disable_unprepare(info->clk); |
Mike Rapoport | 82a72d1 | 2009-02-17 13:54:46 +0200 | [diff] [blame] | 1835 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1836 | for (cs = 0; cs < pdata->num_cs; cs++) |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 1837 | nand_release(nand_to_mtd(&info->host[cs]->chip)); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1841 | static int pxa3xx_nand_probe_dt(struct platform_device *pdev) |
| 1842 | { |
| 1843 | struct pxa3xx_nand_platform_data *pdata; |
| 1844 | struct device_node *np = pdev->dev.of_node; |
| 1845 | const struct of_device_id *of_id = |
| 1846 | of_match_device(pxa3xx_nand_dt_ids, &pdev->dev); |
| 1847 | |
| 1848 | if (!of_id) |
| 1849 | return 0; |
| 1850 | |
| 1851 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1852 | if (!pdata) |
| 1853 | return -ENOMEM; |
| 1854 | |
| 1855 | if (of_get_property(np, "marvell,nand-enable-arbiter", NULL)) |
| 1856 | pdata->enable_arbiter = 1; |
| 1857 | if (of_get_property(np, "marvell,nand-keep-config", NULL)) |
| 1858 | pdata->keep_config = 1; |
| 1859 | of_property_read_u32(np, "num-cs", &pdata->num_cs); |
Ezequiel Garcia | 776f265 | 2013-11-14 18:25:28 -0300 | [diff] [blame] | 1860 | pdata->flash_bbt = of_get_nand_on_flash_bbt(np); |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1861 | |
Ezequiel Garcia | 5b3e507 | 2014-05-14 14:58:08 -0300 | [diff] [blame] | 1862 | pdata->ecc_strength = of_get_nand_ecc_strength(np); |
| 1863 | if (pdata->ecc_strength < 0) |
| 1864 | pdata->ecc_strength = 0; |
| 1865 | |
| 1866 | pdata->ecc_step_size = of_get_nand_ecc_step_size(np); |
| 1867 | if (pdata->ecc_step_size < 0) |
| 1868 | pdata->ecc_step_size = 0; |
| 1869 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1870 | pdev->dev.platform_data = pdata; |
| 1871 | |
| 1872 | return 0; |
| 1873 | } |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1874 | |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1875 | static int pxa3xx_nand_probe(struct platform_device *pdev) |
| 1876 | { |
| 1877 | struct pxa3xx_nand_platform_data *pdata; |
| 1878 | struct pxa3xx_nand_info *info; |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1879 | int ret, cs, probe_success, dma_available; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1880 | |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1881 | dma_available = IS_ENABLED(CONFIG_ARM) && |
| 1882 | (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP)); |
| 1883 | if (use_dma && !dma_available) { |
Ezequiel Garcia | f4db2e3 | 2013-08-12 14:14:56 -0300 | [diff] [blame] | 1884 | use_dma = 0; |
| 1885 | dev_warn(&pdev->dev, |
| 1886 | "This platform can't do DMA on this device\n"); |
| 1887 | } |
Robert Jarzmik | 8f5ba31 | 2015-09-06 15:12:47 +0200 | [diff] [blame] | 1888 | |
Daniel Mack | 1e7ba63 | 2012-07-22 19:51:02 +0200 | [diff] [blame] | 1889 | ret = pxa3xx_nand_probe_dt(pdev); |
| 1890 | if (ret) |
| 1891 | return ret; |
| 1892 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 1893 | pdata = dev_get_platdata(&pdev->dev); |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1894 | if (!pdata) { |
| 1895 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 1896 | return -ENODEV; |
| 1897 | } |
| 1898 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1899 | ret = alloc_nand_resource(pdev); |
| 1900 | if (ret) { |
| 1901 | dev_err(&pdev->dev, "alloc nand resource failed\n"); |
| 1902 | return ret; |
| 1903 | } |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1904 | |
Lei Wen | d456882 | 2011-07-14 20:44:32 -0700 | [diff] [blame] | 1905 | info = platform_get_drvdata(pdev); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1906 | probe_success = 0; |
| 1907 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Boris BREZILLON | 063294a | 2015-12-10 09:00:20 +0100 | [diff] [blame^] | 1908 | struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip); |
Ezequiel Garcia | f455578 | 2013-08-12 14:14:53 -0300 | [diff] [blame] | 1909 | |
Ezequiel Garcia | 18a84e9 | 2013-10-19 18:19:25 -0300 | [diff] [blame] | 1910 | /* |
| 1911 | * The mtd name matches the one used in 'mtdparts' kernel |
| 1912 | * parameter. This name cannot be changed or otherwise |
| 1913 | * user's mtd partitions configuration would get broken. |
| 1914 | */ |
| 1915 | mtd->name = "pxa3xx_nand-0"; |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1916 | info->cs = cs; |
Ezequiel Garcia | b7655bc | 2013-08-12 14:14:52 -0300 | [diff] [blame] | 1917 | ret = pxa3xx_nand_scan(mtd); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1918 | if (ret) { |
| 1919 | dev_warn(&pdev->dev, "failed to scan nand at cs %d\n", |
| 1920 | cs); |
| 1921 | continue; |
| 1922 | } |
| 1923 | |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1924 | ret = mtd_device_register(mtd, pdata->parts[cs], |
| 1925 | pdata->nr_parts[cs]); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1926 | if (!ret) |
| 1927 | probe_success = 1; |
| 1928 | } |
| 1929 | |
| 1930 | if (!probe_success) { |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1931 | pxa3xx_nand_remove(pdev); |
| 1932 | return -ENODEV; |
| 1933 | } |
| 1934 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1935 | return 0; |
Lei Wen | e353a20 | 2011-03-03 11:08:30 +0800 | [diff] [blame] | 1936 | } |
| 1937 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1938 | #ifdef CONFIG_PM |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1939 | static int pxa3xx_nand_suspend(struct device *dev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1940 | { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1941 | struct pxa3xx_nand_info *info = dev_get_drvdata(dev); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1942 | |
Lei Wen | f8155a4 | 2011-02-28 10:32:11 +0800 | [diff] [blame] | 1943 | if (info->state) { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1944 | dev_err(dev, "driver busy, state = %d\n", info->state); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1945 | return -EAGAIN; |
| 1946 | } |
| 1947 | |
Ezequiel García | d55d31a | 2015-11-04 13:13:46 -0300 | [diff] [blame] | 1948 | clk_disable(info->clk); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
| 1951 | |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1952 | static int pxa3xx_nand_resume(struct device *dev) |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1953 | { |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1954 | struct pxa3xx_nand_info *info = dev_get_drvdata(dev); |
Ezequiel García | d55d31a | 2015-11-04 13:13:46 -0300 | [diff] [blame] | 1955 | int ret; |
| 1956 | |
| 1957 | ret = clk_enable(info->clk); |
| 1958 | if (ret < 0) |
| 1959 | return ret; |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1960 | |
| 1961 | /* We don't want to handle interrupt without calling mtd routine */ |
| 1962 | disable_int(info, NDCR_INT_MASK); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1963 | |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1964 | /* |
| 1965 | * Directly set the chip select to a invalid value, |
| 1966 | * then the driver would reset the timing according |
| 1967 | * to current chip select at the beginning of cmdfunc |
| 1968 | */ |
| 1969 | info->cs = 0xff; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1970 | |
Lei Wen | 051fc41 | 2011-07-14 20:44:30 -0700 | [diff] [blame] | 1971 | /* |
| 1972 | * As the spec says, the NDSR would be updated to 0x1800 when |
| 1973 | * doing the nand_clk disable/enable. |
| 1974 | * To prevent it damaging state machine of the driver, clear |
| 1975 | * all status before resume |
| 1976 | */ |
| 1977 | nand_writel(info, NDSR, NDSR_MASK); |
Lei Wen | f3c8cfc | 2011-07-14 20:44:33 -0700 | [diff] [blame] | 1978 | |
Lei Wen | 18c81b1 | 2010-08-17 17:25:57 +0800 | [diff] [blame] | 1979 | return 0; |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1980 | } |
| 1981 | #else |
| 1982 | #define pxa3xx_nand_suspend NULL |
| 1983 | #define pxa3xx_nand_resume NULL |
| 1984 | #endif |
| 1985 | |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1986 | static const struct dev_pm_ops pxa3xx_nand_pm_ops = { |
| 1987 | .suspend = pxa3xx_nand_suspend, |
| 1988 | .resume = pxa3xx_nand_resume, |
| 1989 | }; |
| 1990 | |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1991 | static struct platform_driver pxa3xx_nand_driver = { |
| 1992 | .driver = { |
| 1993 | .name = "pxa3xx-nand", |
Sachin Kamat | 5576bc7 | 2013-09-30 15:10:24 +0530 | [diff] [blame] | 1994 | .of_match_table = pxa3xx_nand_dt_ids, |
Brian Norris | d3e94f3 | 2015-10-12 14:07:41 -0700 | [diff] [blame] | 1995 | .pm = &pxa3xx_nand_pm_ops, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1996 | }, |
| 1997 | .probe = pxa3xx_nand_probe, |
| 1998 | .remove = pxa3xx_nand_remove, |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 1999 | }; |
| 2000 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 2001 | module_platform_driver(pxa3xx_nand_driver); |
eric miao | fe69af0 | 2008-02-14 15:48:23 +0800 | [diff] [blame] | 2002 | |
| 2003 | MODULE_LICENSE("GPL"); |
| 2004 | MODULE_DESCRIPTION("PXA3xx NAND controller driver"); |