Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 2 | /* |
Miquel Raynal | 1f1ae15 | 2018-08-16 17:30:07 +0200 | [diff] [blame] | 3 | * drivers/mtd/nand/raw/pxa3xx_nand.c |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright © 2005 Intel Corporation |
| 6 | * Copyright © 2006 Marvell International Ltd. |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 10 | #include <fdtdec.h> |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 11 | #include <nand.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 16 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 19 | #include <linux/errno.h> |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <asm/arch/cpu.h> |
| 22 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 23 | #include <linux/mtd/rawnand.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 24 | #include <linux/printk.h> |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 25 | #include <linux/types.h> |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 26 | #include <syscon.h> |
| 27 | #include <regmap.h> |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 28 | #include <dm/uclass.h> |
| 29 | #include <dm/read.h> |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 30 | |
| 31 | #include "pxa3xx_nand.h" |
| 32 | |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 35 | #define TIMEOUT_DRAIN_FIFO 5 /* in ms */ |
| 36 | #define CHIP_DELAY_TIMEOUT 200 |
| 37 | #define NAND_STOP_DELAY 40 |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Define a buffer size for the initial command that detects the flash device: |
Ofer Heifetz | fdf5b23 | 2018-08-29 11:56:00 +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. |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 46 | */ |
Ofer Heifetz | fdf5b23 | 2018-08-29 11:56:00 +0300 | [diff] [blame] | 47 | #define INIT_BUFFER_SIZE 2048 |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 48 | |
| 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 */ |
| 57 | #define NDECCCTRL (0x28) /* ECC control */ |
| 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) |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 74 | #define NFCV1_NDCR_ARB_CNTL (0x1 << 19) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 75 | #define NDCR_RD_ID_CNT_MASK (0x7 << 16) |
| 76 | #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK) |
| 77 | |
| 78 | #define NDCR_RA_START (0x1 << 15) |
| 79 | #define NDCR_PG_PER_BLK (0x1 << 14) |
| 80 | #define NDCR_ND_ARB_EN (0x1 << 12) |
| 81 | #define NDCR_INT_MASK (0xFFF) |
| 82 | |
| 83 | #define NDSR_MASK (0xfff) |
| 84 | #define NDSR_ERR_CNT_OFF (16) |
| 85 | #define NDSR_ERR_CNT_MASK (0x1f) |
| 86 | #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK) |
| 87 | #define NDSR_RDY (0x1 << 12) |
| 88 | #define NDSR_FLASH_RDY (0x1 << 11) |
| 89 | #define NDSR_CS0_PAGED (0x1 << 10) |
| 90 | #define NDSR_CS1_PAGED (0x1 << 9) |
| 91 | #define NDSR_CS0_CMDD (0x1 << 8) |
| 92 | #define NDSR_CS1_CMDD (0x1 << 7) |
| 93 | #define NDSR_CS0_BBD (0x1 << 6) |
| 94 | #define NDSR_CS1_BBD (0x1 << 5) |
| 95 | #define NDSR_UNCORERR (0x1 << 4) |
| 96 | #define NDSR_CORERR (0x1 << 3) |
| 97 | #define NDSR_WRDREQ (0x1 << 2) |
| 98 | #define NDSR_RDDREQ (0x1 << 1) |
| 99 | #define NDSR_WRCMDREQ (0x1) |
| 100 | |
| 101 | #define NDCB0_LEN_OVRD (0x1 << 28) |
| 102 | #define NDCB0_ST_ROW_EN (0x1 << 26) |
| 103 | #define NDCB0_AUTO_RS (0x1 << 25) |
| 104 | #define NDCB0_CSEL (0x1 << 24) |
| 105 | #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29) |
| 106 | #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK) |
| 107 | #define NDCB0_CMD_TYPE_MASK (0x7 << 21) |
| 108 | #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK) |
| 109 | #define NDCB0_NC (0x1 << 20) |
| 110 | #define NDCB0_DBC (0x1 << 19) |
| 111 | #define NDCB0_ADDR_CYC_MASK (0x7 << 16) |
| 112 | #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK) |
| 113 | #define NDCB0_CMD2_MASK (0xff << 8) |
| 114 | #define NDCB0_CMD1_MASK (0xff) |
| 115 | #define NDCB0_ADDR_CYC_SHIFT (16) |
| 116 | |
| 117 | #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */ |
| 118 | #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */ |
| 119 | #define EXT_CMD_TYPE_READ 4 /* Read */ |
| 120 | #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */ |
| 121 | #define EXT_CMD_TYPE_FINAL 3 /* Final command */ |
| 122 | #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */ |
| 123 | #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */ |
| 124 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 125 | /* System control register and bit to enable NAND on some SoCs */ |
| 126 | #define GENCONF_SOC_DEVICE_MUX 0x208 |
| 127 | #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0) |
Chris Packham | f2c1ea7 | 2023-07-10 10:47:35 +1200 | [diff] [blame] | 128 | #define GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN BIT(27) |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 129 | |
Ofer Heifetz | 4a574aa | 2018-08-29 11:56:05 +0300 | [diff] [blame] | 130 | /* |
| 131 | * This should be large enough to read 'ONFI' and 'JEDEC'. |
| 132 | * Let's use 7 bytes, which is the maximum ID count supported |
| 133 | * by the controller (see NDCR_RD_ID_CNT_MASK). |
| 134 | */ |
| 135 | #define READ_ID_BYTES 7 |
| 136 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 137 | /* macros for registers read/write */ |
| 138 | #define nand_writel(info, off, val) \ |
| 139 | writel((val), (info)->mmio_base + (off)) |
| 140 | |
| 141 | #define nand_readl(info, off) \ |
| 142 | readl((info)->mmio_base + (off)) |
| 143 | |
| 144 | /* error code and state */ |
| 145 | enum { |
| 146 | ERR_NONE = 0, |
| 147 | ERR_DMABUSERR = -1, |
| 148 | ERR_SENDCMD = -2, |
| 149 | ERR_UNCORERR = -3, |
| 150 | ERR_BBERR = -4, |
| 151 | ERR_CORERR = -5, |
| 152 | }; |
| 153 | |
| 154 | enum { |
| 155 | STATE_IDLE = 0, |
| 156 | STATE_PREPARED, |
| 157 | STATE_CMD_HANDLE, |
| 158 | STATE_DMA_READING, |
| 159 | STATE_DMA_WRITING, |
| 160 | STATE_DMA_DONE, |
| 161 | STATE_PIO_READING, |
| 162 | STATE_PIO_WRITING, |
| 163 | STATE_CMD_DONE, |
| 164 | STATE_READY, |
| 165 | }; |
| 166 | |
| 167 | enum pxa3xx_nand_variant { |
| 168 | PXA3XX_NAND_VARIANT_PXA, |
| 169 | PXA3XX_NAND_VARIANT_ARMADA370, |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 170 | PXA3XX_NAND_VARIANT_ARMADA_8K, |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 171 | PXA3XX_NAND_VARIANT_AC5, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | struct pxa3xx_nand_host { |
| 175 | struct nand_chip chip; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 176 | void *info_data; |
| 177 | |
| 178 | /* page size of attached chip */ |
| 179 | int use_ecc; |
| 180 | int cs; |
| 181 | |
| 182 | /* calculated from pxa3xx_nand_flash data */ |
| 183 | unsigned int col_addr_cycles; |
| 184 | unsigned int row_addr_cycles; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct pxa3xx_nand_info { |
| 188 | struct nand_hw_control controller; |
| 189 | struct pxa3xx_nand_platform_data *pdata; |
| 190 | |
| 191 | struct clk *clk; |
| 192 | void __iomem *mmio_base; |
| 193 | unsigned long mmio_phys; |
| 194 | int cmd_complete, dev_ready; |
| 195 | |
| 196 | unsigned int buf_start; |
| 197 | unsigned int buf_count; |
| 198 | unsigned int buf_size; |
| 199 | unsigned int data_buff_pos; |
| 200 | unsigned int oob_buff_pos; |
| 201 | |
| 202 | unsigned char *data_buff; |
| 203 | unsigned char *oob_buff; |
| 204 | |
| 205 | struct pxa3xx_nand_host *host[NUM_CHIP_SELECT]; |
| 206 | unsigned int state; |
| 207 | |
| 208 | /* |
| 209 | * This driver supports NFCv1 (as found in PXA SoC) |
| 210 | * and NFCv2 (as found in Armada 370/XP SoC). |
| 211 | */ |
| 212 | enum pxa3xx_nand_variant variant; |
| 213 | |
| 214 | int cs; |
| 215 | int use_ecc; /* use HW ECC ? */ |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 216 | int force_raw; /* prevent use_ecc to be set */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 217 | int ecc_bch; /* using BCH ECC? */ |
| 218 | int use_spare; /* use spare ? */ |
| 219 | int need_wait; |
| 220 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 221 | /* Amount of real data per full chunk */ |
| 222 | unsigned int chunk_size; |
| 223 | |
| 224 | /* Amount of spare data per full chunk */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 225 | unsigned int spare_size; |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 226 | |
| 227 | /* Number of full chunks (i.e chunk_size + spare_size) */ |
| 228 | unsigned int nfullchunks; |
| 229 | |
| 230 | /* |
| 231 | * Total number of chunks. If equal to nfullchunks, then there |
| 232 | * are only full chunks. Otherwise, there is one last chunk of |
| 233 | * size (last_chunk_size + last_spare_size) |
| 234 | */ |
| 235 | unsigned int ntotalchunks; |
| 236 | |
| 237 | /* Amount of real data in the last chunk */ |
| 238 | unsigned int last_chunk_size; |
| 239 | |
| 240 | /* Amount of spare data in the last chunk */ |
| 241 | unsigned int last_spare_size; |
| 242 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 243 | unsigned int ecc_size; |
| 244 | unsigned int ecc_err_cnt; |
| 245 | unsigned int max_bitflips; |
| 246 | int retcode; |
| 247 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 248 | /* |
| 249 | * Variables only valid during command |
| 250 | * execution. step_chunk_size and step_spare_size is the |
| 251 | * amount of real data and spare data in the current |
| 252 | * chunk. cur_chunk is the current chunk being |
| 253 | * read/programmed. |
| 254 | */ |
| 255 | unsigned int step_chunk_size; |
| 256 | unsigned int step_spare_size; |
| 257 | unsigned int cur_chunk; |
| 258 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 259 | /* cached register value */ |
| 260 | uint32_t reg_ndcr; |
| 261 | uint32_t ndtr0cs0; |
| 262 | uint32_t ndtr1cs0; |
| 263 | |
| 264 | /* generated NDCBx register values */ |
| 265 | uint32_t ndcb0; |
| 266 | uint32_t ndcb1; |
| 267 | uint32_t ndcb2; |
| 268 | uint32_t ndcb3; |
| 269 | }; |
| 270 | |
| 271 | static struct pxa3xx_nand_timing timing[] = { |
Konstantin Porotchkin | a692cde | 2018-08-29 11:56:16 +0300 | [diff] [blame] | 272 | /* |
| 273 | * tCH Enable signal hold time |
| 274 | * tCS Enable signal setup time |
| 275 | * tWH ND_nWE high duration |
| 276 | * tWP ND_nWE pulse time |
| 277 | * tRH ND_nRE high duration |
| 278 | * tRP ND_nRE pulse width |
| 279 | * tR ND_nWE high to ND_nRE low for read |
| 280 | * tWHR ND_nWE high to ND_nRE low for status read |
| 281 | * tAR ND_ALE low to ND_nRE low delay |
| 282 | */ |
Konstantin Porotchkin | 029be94 | 2018-08-29 11:56:14 +0300 | [diff] [blame] | 283 | /*ch cs wh wp rh rp r whr ar */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 284 | { 40, 80, 60, 100, 80, 100, 90000, 400, 40, }, |
| 285 | { 10, 0, 20, 40, 30, 40, 11123, 110, 10, }, |
| 286 | { 10, 25, 15, 25, 15, 30, 25000, 60, 10, }, |
| 287 | { 10, 35, 15, 25, 15, 25, 25000, 60, 10, }, |
Konstantin Porotchkin | 029be94 | 2018-08-29 11:56:14 +0300 | [diff] [blame] | 288 | { 5, 20, 10, 12, 10, 12, 25000, 60, 10, }, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | static struct pxa3xx_nand_flash builtin_flash_types[] = { |
Konstantin Porotchkin | a692cde | 2018-08-29 11:56:16 +0300 | [diff] [blame] | 292 | /* |
| 293 | * chip_id |
| 294 | * flash_width Width of Flash memory (DWIDTH_M) |
| 295 | * dfc_width Width of flash controller(DWIDTH_C) |
| 296 | * *timing |
| 297 | * http://www.linux-mtd.infradead.org/nand-data/nanddata.html |
| 298 | */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 299 | { 0x46ec, 16, 16, &timing[1] }, |
| 300 | { 0xdaec, 8, 8, &timing[1] }, |
| 301 | { 0xd7ec, 8, 8, &timing[1] }, |
| 302 | { 0xa12c, 8, 8, &timing[2] }, |
| 303 | { 0xb12c, 16, 16, &timing[2] }, |
| 304 | { 0xdc2c, 8, 8, &timing[2] }, |
| 305 | { 0xcc2c, 16, 16, &timing[2] }, |
| 306 | { 0xba20, 16, 16, &timing[3] }, |
Konstantin Porotchkin | 029be94 | 2018-08-29 11:56:14 +0300 | [diff] [blame] | 307 | { 0xda98, 8, 8, &timing[4] }, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 308 | }; |
| 309 | |
Sean Nyekjaer | a12a8e8 | 2017-11-22 13:39:08 +0100 | [diff] [blame] | 310 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 311 | static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' }; |
| 312 | static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' }; |
| 313 | |
| 314 | static struct nand_bbt_descr bbt_main_descr = { |
| 315 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 316 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 317 | .offs = 8, |
| 318 | .len = 6, |
| 319 | .veroffs = 14, |
| 320 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 321 | .pattern = bbt_pattern |
| 322 | }; |
| 323 | |
| 324 | static struct nand_bbt_descr bbt_mirror_descr = { |
| 325 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 326 | | NAND_BBT_2BIT | NAND_BBT_VERSION, |
| 327 | .offs = 8, |
| 328 | .len = 6, |
| 329 | .veroffs = 14, |
| 330 | .maxblocks = 8, /* Last 8 blocks in each chip */ |
| 331 | .pattern = bbt_mirror_pattern |
| 332 | }; |
Sean Nyekjaer | a12a8e8 | 2017-11-22 13:39:08 +0100 | [diff] [blame] | 333 | #endif |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 334 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 335 | struct marvell_hw_ecc_layout { |
| 336 | int page_size; |
| 337 | int strength; |
| 338 | unsigned int ecc_size; |
| 339 | unsigned int nfullchunks; |
| 340 | unsigned int chunk_size; |
| 341 | unsigned int spare_size; |
| 342 | unsigned int last_chunk_size; |
| 343 | unsigned int last_spare_size; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 344 | }; |
| 345 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 346 | static const struct marvell_hw_ecc_layout nfc_layouts[] = { |
| 347 | /* page_size strength ecc_size nfullchunks chunk_size spare_size last_chunk last_spare */ |
| 348 | { 512, 1, 8, 1, 512, 8, 0, 0 }, |
| 349 | { 2048, 1, 24, 1, 2048, 40, 0, 0 }, |
Konstantin Porotchkin | e0e232e | 2018-08-29 11:56:17 +0300 | [diff] [blame] | 350 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 351 | { 2048, 4, 32, 1, 2048, 32, 0, 0 }, |
| 352 | { 2048, 8, 32, 1, 1024, 0, 1024, 32 }, |
| 353 | { 2048, 12, 32, 2, 704, 0, 640, 0 }, |
| 354 | { 2048, 16, 32, 4, 512, 0, 0, 32 }, |
| 355 | { 4096, 4, 32, 2, 2048, 32, 0, 0 }, |
| 356 | { 4096, 8, 32, 4, 1024, 0, 0, 64 }, |
| 357 | { 4096, 12, 32, 5, 704, 0, 576, 32 }, |
| 358 | { 4096, 16, 32, 8, 512, 0, 0, 32 }, |
Konstantin Porotchkin | e0e232e | 2018-08-29 11:56:17 +0300 | [diff] [blame] | 359 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 360 | { 8192, 4, 32, 4, 2048, 32, 0, 0 }, |
| 361 | { 8192, 8, 32, 8, 1024, 0, 0, 160 }, |
| 362 | { 8192, 12, 32, 11, 704, 0, 448, 64 }, |
| 363 | { 8192, 16, 32, 16, 512, 0, 0, 32 }, |
| 364 | { }, |
Konstantin Porotchkin | e0e232e | 2018-08-29 11:56:17 +0300 | [diff] [blame] | 365 | }; |
| 366 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 367 | static struct nand_ecclayout ecc_layout_empty = { |
| 368 | .eccbytes = 0, |
| 369 | .eccpos = { }, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 370 | .oobfree = { } |
| 371 | }; |
| 372 | |
| 373 | #define NDTR0_tCH(c) (min((c), 7) << 19) |
| 374 | #define NDTR0_tCS(c) (min((c), 7) << 16) |
| 375 | #define NDTR0_tWH(c) (min((c), 7) << 11) |
| 376 | #define NDTR0_tWP(c) (min((c), 7) << 8) |
| 377 | #define NDTR0_tRH(c) (min((c), 7) << 3) |
| 378 | #define NDTR0_tRP(c) (min((c), 7) << 0) |
| 379 | |
| 380 | #define NDTR1_tR(c) (min((c), 65535) << 16) |
| 381 | #define NDTR1_tWHR(c) (min((c), 15) << 4) |
| 382 | #define NDTR1_tAR(c) (min((c), 15) << 0) |
| 383 | |
| 384 | /* convert nano-seconds to nand flash controller clock cycles */ |
| 385 | #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000) |
| 386 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 387 | static const struct udevice_id pxa3xx_nand_dt_ids[] = { |
| 388 | { |
Pali Rohár | c96bc1d | 2022-07-27 14:47:35 +0200 | [diff] [blame] | 389 | .compatible = "marvell,armada370-nand-controller", |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 390 | .data = PXA3XX_NAND_VARIANT_ARMADA370, |
| 391 | }, |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 392 | { |
| 393 | .compatible = "marvell,armada-8k-nand-controller", |
| 394 | .data = PXA3XX_NAND_VARIANT_ARMADA_8K, |
| 395 | }, |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 396 | { |
| 397 | .compatible = "marvell,mvebu-ac5-pxa3xx-nand", |
| 398 | .data = PXA3XX_NAND_VARIANT_AC5, |
| 399 | }, |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 400 | {} |
| 401 | }; |
| 402 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 403 | static enum pxa3xx_nand_variant pxa3xx_nand_get_variant(struct udevice *dev) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 404 | { |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 405 | return dev_get_driver_data(dev); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host, |
| 409 | const struct pxa3xx_nand_timing *t) |
| 410 | { |
| 411 | struct pxa3xx_nand_info *info = host->info_data; |
| 412 | unsigned long nand_clk = mvebu_get_nand_clock(); |
| 413 | uint32_t ndtr0, ndtr1; |
| 414 | |
| 415 | ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) | |
| 416 | NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) | |
| 417 | NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) | |
| 418 | NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) | |
| 419 | NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) | |
| 420 | NDTR0_tRP(ns2cycle(t->tRP, nand_clk)); |
| 421 | |
| 422 | ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) | |
| 423 | NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) | |
| 424 | NDTR1_tAR(ns2cycle(t->tAR, nand_clk)); |
| 425 | |
| 426 | info->ndtr0cs0 = ndtr0; |
| 427 | info->ndtr1cs0 = ndtr1; |
| 428 | nand_writel(info, NDTR0CS0, ndtr0); |
| 429 | nand_writel(info, NDTR1CS0, ndtr1); |
| 430 | } |
| 431 | |
| 432 | static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host, |
| 433 | const struct nand_sdr_timings *t) |
| 434 | { |
| 435 | struct pxa3xx_nand_info *info = host->info_data; |
| 436 | struct nand_chip *chip = &host->chip; |
| 437 | unsigned long nand_clk = mvebu_get_nand_clock(); |
| 438 | uint32_t ndtr0, ndtr1; |
| 439 | |
| 440 | u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000); |
| 441 | u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000); |
| 442 | u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000); |
Ofer Heifetz | 8f8d458 | 2018-08-29 11:56:02 +0300 | [diff] [blame] | 443 | u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 444 | u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000); |
Ofer Heifetz | 8f8d458 | 2018-08-29 11:56:02 +0300 | [diff] [blame] | 445 | u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 446 | u32 tR = chip->chip_delay * 1000; |
| 447 | u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000); |
| 448 | u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000); |
| 449 | |
| 450 | /* fallback to a default value if tR = 0 */ |
| 451 | if (!tR) |
| 452 | tR = 20000; |
| 453 | |
| 454 | ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) | |
| 455 | NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) | |
| 456 | NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) | |
| 457 | NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) | |
| 458 | NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) | |
| 459 | NDTR0_tRP(ns2cycle(tRP_min, nand_clk)); |
| 460 | |
| 461 | ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) | |
| 462 | NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) | |
| 463 | NDTR1_tAR(ns2cycle(tAR_min, nand_clk)); |
| 464 | |
| 465 | info->ndtr0cs0 = ndtr0; |
| 466 | info->ndtr1cs0 = ndtr1; |
| 467 | nand_writel(info, NDTR0CS0, ndtr0); |
| 468 | nand_writel(info, NDTR1CS0, ndtr1); |
| 469 | } |
| 470 | |
| 471 | static int pxa3xx_nand_init_timings(struct pxa3xx_nand_host *host) |
| 472 | { |
| 473 | const struct nand_sdr_timings *timings; |
| 474 | struct nand_chip *chip = &host->chip; |
| 475 | struct pxa3xx_nand_info *info = host->info_data; |
| 476 | const struct pxa3xx_nand_flash *f = NULL; |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 477 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 478 | int mode, id, ntypes, i; |
| 479 | |
| 480 | mode = onfi_get_async_timing_mode(chip); |
| 481 | if (mode == ONFI_TIMING_MODE_UNKNOWN) { |
| 482 | ntypes = ARRAY_SIZE(builtin_flash_types); |
| 483 | |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 484 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 485 | |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 486 | id = chip->read_byte(mtd); |
| 487 | id |= chip->read_byte(mtd) << 0x8; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 488 | |
| 489 | for (i = 0; i < ntypes; i++) { |
| 490 | f = &builtin_flash_types[i]; |
| 491 | |
| 492 | if (f->chip_id == id) |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | if (i == ntypes) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 497 | dev_err(mtd->dev, "Error: timings not found\n"); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | pxa3xx_nand_set_timing(host, f->timing); |
| 502 | |
| 503 | if (f->flash_width == 16) { |
| 504 | info->reg_ndcr |= NDCR_DWIDTH_M; |
| 505 | chip->options |= NAND_BUSWIDTH_16; |
| 506 | } |
| 507 | |
| 508 | info->reg_ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0; |
| 509 | } else { |
| 510 | mode = fls(mode) - 1; |
| 511 | if (mode < 0) |
| 512 | mode = 0; |
| 513 | |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 514 | if (info->variant == PXA3XX_NAND_VARIANT_AC5) |
| 515 | mode = min(mode, 3); |
| 516 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 517 | timings = onfi_async_timing_mode_to_sdr_timings(mode); |
| 518 | if (IS_ERR(timings)) |
| 519 | return PTR_ERR(timings); |
| 520 | |
| 521 | pxa3xx_nand_set_sdr_timing(host, timings); |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 527 | /** |
Vagrant Cascadian | beb288b | 2015-11-24 14:46:24 -0800 | [diff] [blame] | 528 | * NOTE: it is a must to set ND_RUN first, then write |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 529 | * command buffer, otherwise, it does not work. |
| 530 | * We enable all the interrupt at the same time, and |
| 531 | * let pxa3xx_nand_irq to handle all logic. |
| 532 | */ |
| 533 | static void pxa3xx_nand_start(struct pxa3xx_nand_info *info) |
| 534 | { |
| 535 | uint32_t ndcr; |
| 536 | |
| 537 | ndcr = info->reg_ndcr; |
| 538 | |
| 539 | if (info->use_ecc) { |
| 540 | ndcr |= NDCR_ECC_EN; |
| 541 | if (info->ecc_bch) |
| 542 | nand_writel(info, NDECCCTRL, 0x1); |
| 543 | } else { |
| 544 | ndcr &= ~NDCR_ECC_EN; |
| 545 | if (info->ecc_bch) |
| 546 | nand_writel(info, NDECCCTRL, 0x0); |
| 547 | } |
| 548 | |
| 549 | ndcr &= ~NDCR_DMA_EN; |
| 550 | |
| 551 | if (info->use_spare) |
| 552 | ndcr |= NDCR_SPARE_EN; |
| 553 | else |
| 554 | ndcr &= ~NDCR_SPARE_EN; |
| 555 | |
| 556 | ndcr |= NDCR_ND_RUN; |
| 557 | |
| 558 | /* clear status bits and run */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 559 | nand_writel(info, NDSR, NDSR_MASK); |
Ofer Heifetz | d92d899 | 2018-08-29 11:56:03 +0300 | [diff] [blame] | 560 | nand_writel(info, NDCR, 0); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 561 | nand_writel(info, NDCR, ndcr); |
| 562 | } |
| 563 | |
| 564 | static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask) |
| 565 | { |
| 566 | uint32_t ndcr; |
| 567 | |
| 568 | ndcr = nand_readl(info, NDCR); |
| 569 | nand_writel(info, NDCR, ndcr | int_mask); |
| 570 | } |
| 571 | |
| 572 | static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len) |
| 573 | { |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 574 | if (info->ecc_bch && !info->force_raw) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 575 | u32 ts; |
| 576 | |
| 577 | /* |
| 578 | * According to the datasheet, when reading from NDDB |
| 579 | * with BCH enabled, after each 32 bytes reads, we |
| 580 | * have to make sure that the NDSR.RDDREQ bit is set. |
| 581 | * |
| 582 | * Drain the FIFO 8 32 bits reads at a time, and skip |
| 583 | * the polling on the last read. |
| 584 | */ |
| 585 | while (len > 8) { |
| 586 | readsl(info->mmio_base + NDDB, data, 8); |
| 587 | |
| 588 | ts = get_timer(0); |
| 589 | while (!(nand_readl(info, NDSR) & NDSR_RDDREQ)) { |
| 590 | if (get_timer(ts) > TIMEOUT_DRAIN_FIFO) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 591 | dev_err(info->controller.active->mtd.dev, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 592 | "Timeout on RDDREQ while draining the FIFO\n"); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | data += 32; |
| 598 | len -= 8; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | readsl(info->mmio_base + NDDB, data, len); |
| 603 | } |
| 604 | |
| 605 | static void handle_data_pio(struct pxa3xx_nand_info *info) |
| 606 | { |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 607 | int data_len = info->step_chunk_size; |
| 608 | |
| 609 | /* |
| 610 | * In raw mode, include the spare area and the ECC bytes that are not |
| 611 | * consumed by the controller in the data section. Do not reorganize |
| 612 | * here, do it in the ->read_page_raw() handler instead. |
| 613 | */ |
| 614 | if (info->force_raw) |
| 615 | data_len += info->step_spare_size + info->ecc_size; |
| 616 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 617 | switch (info->state) { |
| 618 | case STATE_PIO_WRITING: |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 619 | if (info->step_chunk_size) |
| 620 | writesl(info->mmio_base + NDDB, |
| 621 | info->data_buff + info->data_buff_pos, |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 622 | DIV_ROUND_UP(data_len, 4)); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 623 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 624 | if (info->step_spare_size) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 625 | writesl(info->mmio_base + NDDB, |
| 626 | info->oob_buff + info->oob_buff_pos, |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 627 | DIV_ROUND_UP(info->step_spare_size, 4)); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 628 | break; |
| 629 | case STATE_PIO_READING: |
Baruch Siach | 9167e4d | 2020-04-05 19:19:31 +0300 | [diff] [blame] | 630 | if (data_len) |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 631 | drain_fifo(info, |
| 632 | info->data_buff + info->data_buff_pos, |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 633 | DIV_ROUND_UP(data_len, 4)); |
| 634 | |
| 635 | if (info->force_raw) |
| 636 | break; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 637 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 638 | if (info->step_spare_size) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 639 | drain_fifo(info, |
| 640 | info->oob_buff + info->oob_buff_pos, |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 641 | DIV_ROUND_UP(info->step_spare_size, 4)); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 642 | break; |
| 643 | default: |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 644 | dev_err(info->controller.active->mtd.dev, |
| 645 | "%s: invalid state %d\n", __func__, info->state); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 646 | BUG(); |
| 647 | } |
| 648 | |
| 649 | /* Update buffer pointers for multi-page read/write */ |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 650 | info->data_buff_pos += data_len; |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 651 | info->oob_buff_pos += info->step_spare_size; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static void pxa3xx_nand_irq_thread(struct pxa3xx_nand_info *info) |
| 655 | { |
| 656 | handle_data_pio(info); |
| 657 | |
| 658 | info->state = STATE_CMD_DONE; |
| 659 | nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ); |
| 660 | } |
| 661 | |
| 662 | static irqreturn_t pxa3xx_nand_irq(struct pxa3xx_nand_info *info) |
| 663 | { |
| 664 | unsigned int status, is_completed = 0, is_ready = 0; |
| 665 | unsigned int ready, cmd_done; |
| 666 | irqreturn_t ret = IRQ_HANDLED; |
| 667 | |
| 668 | if (info->cs == 0) { |
| 669 | ready = NDSR_FLASH_RDY; |
| 670 | cmd_done = NDSR_CS0_CMDD; |
| 671 | } else { |
| 672 | ready = NDSR_RDY; |
| 673 | cmd_done = NDSR_CS1_CMDD; |
| 674 | } |
| 675 | |
David Sniatkiwicz | 2087f7e | 2018-08-29 11:56:18 +0300 | [diff] [blame] | 676 | /* TODO - find out why we need the delay during write operation. */ |
| 677 | ndelay(1); |
| 678 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 679 | status = nand_readl(info, NDSR); |
| 680 | |
| 681 | if (status & NDSR_UNCORERR) |
| 682 | info->retcode = ERR_UNCORERR; |
| 683 | if (status & NDSR_CORERR) { |
| 684 | info->retcode = ERR_CORERR; |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 685 | if ((info->variant == PXA3XX_NAND_VARIANT_ARMADA370 || |
| 686 | info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K) && |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 687 | info->ecc_bch) |
| 688 | info->ecc_err_cnt = NDSR_ERR_CNT(status); |
| 689 | else |
| 690 | info->ecc_err_cnt = 1; |
| 691 | |
| 692 | /* |
| 693 | * Each chunk composing a page is corrected independently, |
| 694 | * and we need to store maximum number of corrected bitflips |
| 695 | * to return it to the MTD layer in ecc.read_page(). |
| 696 | */ |
| 697 | info->max_bitflips = max_t(unsigned int, |
| 698 | info->max_bitflips, |
| 699 | info->ecc_err_cnt); |
| 700 | } |
| 701 | if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) { |
| 702 | info->state = (status & NDSR_RDDREQ) ? |
| 703 | STATE_PIO_READING : STATE_PIO_WRITING; |
| 704 | /* Call the IRQ thread in U-Boot directly */ |
| 705 | pxa3xx_nand_irq_thread(info); |
| 706 | return 0; |
| 707 | } |
| 708 | if (status & cmd_done) { |
| 709 | info->state = STATE_CMD_DONE; |
| 710 | is_completed = 1; |
| 711 | } |
| 712 | if (status & ready) { |
| 713 | info->state = STATE_READY; |
| 714 | is_ready = 1; |
| 715 | } |
| 716 | |
Ofer Heifetz | de32316 | 2018-08-29 11:56:04 +0300 | [diff] [blame] | 717 | /* |
| 718 | * Clear all status bit before issuing the next command, which |
| 719 | * can and will alter the status bits and will deserve a new |
| 720 | * interrupt on its own. This lets the controller exit the IRQ |
| 721 | */ |
| 722 | nand_writel(info, NDSR, status); |
| 723 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 724 | if (status & NDSR_WRCMDREQ) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 725 | status &= ~NDSR_WRCMDREQ; |
| 726 | info->state = STATE_CMD_HANDLE; |
| 727 | |
| 728 | /* |
| 729 | * Command buffer registers NDCB{0-2} (and optionally NDCB3) |
| 730 | * must be loaded by writing directly either 12 or 16 |
| 731 | * bytes directly to NDCB0, four bytes at a time. |
| 732 | * |
| 733 | * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored |
| 734 | * but each NDCBx register can be read. |
| 735 | */ |
| 736 | nand_writel(info, NDCB0, info->ndcb0); |
| 737 | nand_writel(info, NDCB0, info->ndcb1); |
| 738 | nand_writel(info, NDCB0, info->ndcb2); |
| 739 | |
| 740 | /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */ |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 741 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 || |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 742 | info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K || |
| 743 | info->variant == PXA3XX_NAND_VARIANT_AC5) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 744 | nand_writel(info, NDCB0, info->ndcb3); |
| 745 | } |
| 746 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 747 | if (is_completed) |
| 748 | info->cmd_complete = 1; |
| 749 | if (is_ready) |
| 750 | info->dev_ready = 1; |
| 751 | |
| 752 | return ret; |
| 753 | } |
| 754 | |
| 755 | static inline int is_buf_blank(uint8_t *buf, size_t len) |
| 756 | { |
| 757 | for (; len > 0; len--) |
| 758 | if (*buf++ != 0xff) |
| 759 | return 0; |
| 760 | return 1; |
| 761 | } |
| 762 | |
| 763 | static void set_command_address(struct pxa3xx_nand_info *info, |
| 764 | unsigned int page_size, uint16_t column, int page_addr) |
| 765 | { |
| 766 | /* small page addr setting */ |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 767 | if (page_size < info->chunk_size) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 768 | info->ndcb1 = ((page_addr & 0xFFFFFF) << 8) |
| 769 | | (column & 0xFF); |
| 770 | |
| 771 | info->ndcb2 = 0; |
| 772 | } else { |
| 773 | info->ndcb1 = ((page_addr & 0xFFFF) << 16) |
| 774 | | (column & 0xFFFF); |
| 775 | |
| 776 | if (page_addr & 0xFF0000) |
| 777 | info->ndcb2 = (page_addr & 0xFF0000) >> 16; |
| 778 | else |
| 779 | info->ndcb2 = 0; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static void prepare_start_command(struct pxa3xx_nand_info *info, int command) |
| 784 | { |
| 785 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 786 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 787 | |
| 788 | /* reset data and oob column point to handle data */ |
| 789 | info->buf_start = 0; |
| 790 | info->buf_count = 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 791 | info->data_buff_pos = 0; |
| 792 | info->oob_buff_pos = 0; |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 793 | info->step_chunk_size = 0; |
| 794 | info->step_spare_size = 0; |
| 795 | info->cur_chunk = 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 796 | info->use_ecc = 0; |
| 797 | info->use_spare = 1; |
| 798 | info->retcode = ERR_NONE; |
| 799 | info->ecc_err_cnt = 0; |
| 800 | info->ndcb3 = 0; |
| 801 | info->need_wait = 0; |
Ravi Minnikanti | a68da5b | 2024-04-27 09:15:28 -0700 | [diff] [blame] | 802 | /* |
| 803 | * Reset max_bitflips to zero. Once command is complete, |
| 804 | * max_bitflips for this READ is returned in ecc.read_page() |
| 805 | */ |
| 806 | info->max_bitflips = 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 807 | |
| 808 | switch (command) { |
| 809 | case NAND_CMD_READ0: |
Boris Brezillon | a558a39 | 2018-08-29 11:56:12 +0300 | [diff] [blame] | 810 | case NAND_CMD_READOOB: |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 811 | case NAND_CMD_PAGEPROG: |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 812 | if (!info->force_raw) |
| 813 | info->use_ecc = 1; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 814 | break; |
| 815 | case NAND_CMD_PARAM: |
| 816 | info->use_spare = 0; |
| 817 | break; |
| 818 | default: |
| 819 | info->ndcb1 = 0; |
| 820 | info->ndcb2 = 0; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * If we are about to issue a read command, or about to set |
| 826 | * the write address, then clean the data buffer. |
| 827 | */ |
| 828 | if (command == NAND_CMD_READ0 || |
| 829 | command == NAND_CMD_READOOB || |
| 830 | command == NAND_CMD_SEQIN) { |
| 831 | info->buf_count = mtd->writesize + mtd->oobsize; |
| 832 | memset(info->data_buff, 0xFF, info->buf_count); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static int prepare_set_command(struct pxa3xx_nand_info *info, int command, |
| 837 | int ext_cmd_type, uint16_t column, int page_addr) |
| 838 | { |
| 839 | int addr_cycle, exec_cmd; |
| 840 | struct pxa3xx_nand_host *host; |
| 841 | struct mtd_info *mtd; |
| 842 | |
| 843 | host = info->host[info->cs]; |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 844 | mtd = nand_to_mtd(&host->chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 845 | addr_cycle = 0; |
| 846 | exec_cmd = 1; |
| 847 | |
| 848 | if (info->cs != 0) |
| 849 | info->ndcb0 = NDCB0_CSEL; |
| 850 | else |
| 851 | info->ndcb0 = 0; |
| 852 | |
| 853 | if (command == NAND_CMD_SEQIN) |
| 854 | exec_cmd = 0; |
| 855 | |
| 856 | addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles |
| 857 | + host->col_addr_cycles); |
| 858 | |
| 859 | switch (command) { |
| 860 | case NAND_CMD_READOOB: |
| 861 | case NAND_CMD_READ0: |
| 862 | info->buf_start = column; |
| 863 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 864 | | addr_cycle |
| 865 | | NAND_CMD_READ0; |
| 866 | |
| 867 | if (command == NAND_CMD_READOOB) |
| 868 | info->buf_start += mtd->writesize; |
| 869 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 870 | if (info->cur_chunk < info->nfullchunks) { |
| 871 | info->step_chunk_size = info->chunk_size; |
| 872 | info->step_spare_size = info->spare_size; |
| 873 | } else { |
| 874 | info->step_chunk_size = info->last_chunk_size; |
| 875 | info->step_spare_size = info->last_spare_size; |
| 876 | } |
| 877 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 878 | /* |
| 879 | * Multiple page read needs an 'extended command type' field, |
| 880 | * which is either naked-read or last-read according to the |
| 881 | * state. |
| 882 | */ |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 883 | if (info->force_raw) { |
| 884 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) | |
| 885 | NDCB0_LEN_OVRD | |
| 886 | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
| 887 | info->ndcb3 = info->step_chunk_size + |
| 888 | info->step_spare_size + info->ecc_size; |
| 889 | } else if (mtd->writesize == info->chunk_size) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 890 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8); |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 891 | } else if (mtd->writesize > info->chunk_size) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 892 | info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8) |
| 893 | | NDCB0_LEN_OVRD |
| 894 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 895 | info->ndcb3 = info->step_chunk_size + |
| 896 | info->step_spare_size; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | set_command_address(info, mtd->writesize, column, page_addr); |
| 900 | break; |
| 901 | |
| 902 | case NAND_CMD_SEQIN: |
| 903 | |
| 904 | info->buf_start = column; |
| 905 | set_command_address(info, mtd->writesize, 0, page_addr); |
| 906 | |
| 907 | /* |
| 908 | * Multiple page programming needs to execute the initial |
| 909 | * SEQIN command that sets the page address. |
| 910 | */ |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 911 | if (mtd->writesize > info->chunk_size) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 912 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 913 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 914 | | addr_cycle |
| 915 | | command; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 916 | exec_cmd = 1; |
| 917 | } |
| 918 | break; |
| 919 | |
| 920 | case NAND_CMD_PAGEPROG: |
| 921 | if (is_buf_blank(info->data_buff, |
| 922 | (mtd->writesize + mtd->oobsize))) { |
| 923 | exec_cmd = 0; |
| 924 | break; |
| 925 | } |
| 926 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 927 | if (info->cur_chunk < info->nfullchunks) { |
| 928 | info->step_chunk_size = info->chunk_size; |
| 929 | info->step_spare_size = info->spare_size; |
| 930 | } else { |
| 931 | info->step_chunk_size = info->last_chunk_size; |
| 932 | info->step_spare_size = info->last_spare_size; |
| 933 | } |
| 934 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 935 | /* Second command setting for large pages */ |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 936 | if (mtd->writesize > info->chunk_size) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 937 | /* |
| 938 | * Multiple page write uses the 'extended command' |
| 939 | * field. This can be used to issue a command dispatch |
| 940 | * or a naked-write depending on the current stage. |
| 941 | */ |
| 942 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 943 | | NDCB0_LEN_OVRD |
| 944 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type); |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 945 | info->ndcb3 = info->step_chunk_size + |
| 946 | info->step_spare_size; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 947 | |
| 948 | /* |
| 949 | * This is the command dispatch that completes a chunked |
| 950 | * page program operation. |
| 951 | */ |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 952 | if (info->cur_chunk == info->ntotalchunks) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 953 | info->ndcb0 = NDCB0_CMD_TYPE(0x1) |
| 954 | | NDCB0_EXT_CMD_TYPE(ext_cmd_type) |
| 955 | | command; |
| 956 | info->ndcb1 = 0; |
| 957 | info->ndcb2 = 0; |
| 958 | info->ndcb3 = 0; |
| 959 | } |
| 960 | } else { |
| 961 | info->ndcb0 |= NDCB0_CMD_TYPE(0x1) |
| 962 | | NDCB0_AUTO_RS |
| 963 | | NDCB0_ST_ROW_EN |
| 964 | | NDCB0_DBC |
| 965 | | (NAND_CMD_PAGEPROG << 8) |
| 966 | | NAND_CMD_SEQIN |
| 967 | | addr_cycle; |
| 968 | } |
| 969 | break; |
| 970 | |
| 971 | case NAND_CMD_PARAM: |
Ofer Heifetz | fdf5b23 | 2018-08-29 11:56:00 +0300 | [diff] [blame] | 972 | info->buf_count = INIT_BUFFER_SIZE; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 973 | info->ndcb0 |= NDCB0_CMD_TYPE(0) |
| 974 | | NDCB0_ADDR_CYC(1) |
| 975 | | NDCB0_LEN_OVRD |
| 976 | | command; |
| 977 | info->ndcb1 = (column & 0xFF); |
Ofer Heifetz | fdf5b23 | 2018-08-29 11:56:00 +0300 | [diff] [blame] | 978 | info->ndcb3 = INIT_BUFFER_SIZE; |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 979 | info->step_chunk_size = INIT_BUFFER_SIZE; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 980 | break; |
| 981 | |
| 982 | case NAND_CMD_READID: |
Ofer Heifetz | 4a574aa | 2018-08-29 11:56:05 +0300 | [diff] [blame] | 983 | info->buf_count = READ_ID_BYTES; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 984 | info->ndcb0 |= NDCB0_CMD_TYPE(3) |
| 985 | | NDCB0_ADDR_CYC(1) |
| 986 | | command; |
| 987 | info->ndcb1 = (column & 0xFF); |
| 988 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 989 | info->step_chunk_size = 8; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 990 | break; |
| 991 | case NAND_CMD_STATUS: |
| 992 | info->buf_count = 1; |
| 993 | info->ndcb0 |= NDCB0_CMD_TYPE(4) |
| 994 | | NDCB0_ADDR_CYC(1) |
| 995 | | command; |
| 996 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 997 | info->step_chunk_size = 8; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 998 | break; |
| 999 | |
| 1000 | case NAND_CMD_ERASE1: |
| 1001 | info->ndcb0 |= NDCB0_CMD_TYPE(2) |
| 1002 | | NDCB0_AUTO_RS |
| 1003 | | NDCB0_ADDR_CYC(3) |
| 1004 | | NDCB0_DBC |
| 1005 | | (NAND_CMD_ERASE2 << 8) |
| 1006 | | NAND_CMD_ERASE1; |
| 1007 | info->ndcb1 = page_addr; |
| 1008 | info->ndcb2 = 0; |
| 1009 | |
| 1010 | break; |
| 1011 | case NAND_CMD_RESET: |
| 1012 | info->ndcb0 |= NDCB0_CMD_TYPE(5) |
| 1013 | | command; |
| 1014 | |
| 1015 | break; |
| 1016 | |
| 1017 | case NAND_CMD_ERASE2: |
| 1018 | exec_cmd = 0; |
| 1019 | break; |
| 1020 | |
| 1021 | default: |
| 1022 | exec_cmd = 0; |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1023 | dev_err(mtd->dev, "non-supported command %x\n", |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1024 | command); |
| 1025 | break; |
| 1026 | } |
| 1027 | |
| 1028 | return exec_cmd; |
| 1029 | } |
| 1030 | |
| 1031 | static void nand_cmdfunc(struct mtd_info *mtd, unsigned command, |
| 1032 | int column, int page_addr) |
| 1033 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1034 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1035 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1036 | struct pxa3xx_nand_info *info = host->info_data; |
| 1037 | int exec_cmd; |
| 1038 | |
| 1039 | /* |
| 1040 | * if this is a x16 device ,then convert the input |
| 1041 | * "byte" address into a "word" address appropriate |
| 1042 | * for indexing a word-oriented device |
| 1043 | */ |
| 1044 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
| 1045 | column /= 2; |
| 1046 | |
| 1047 | /* |
| 1048 | * There may be different NAND chip hooked to |
| 1049 | * different chip select, so check whether |
| 1050 | * chip select has been changed, if yes, reset the timing |
| 1051 | */ |
| 1052 | if (info->cs != host->cs) { |
| 1053 | info->cs = host->cs; |
| 1054 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1055 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
| 1056 | } |
| 1057 | |
| 1058 | prepare_start_command(info, command); |
| 1059 | |
| 1060 | info->state = STATE_PREPARED; |
| 1061 | exec_cmd = prepare_set_command(info, command, 0, column, page_addr); |
| 1062 | |
| 1063 | if (exec_cmd) { |
| 1064 | u32 ts; |
| 1065 | |
| 1066 | info->cmd_complete = 0; |
| 1067 | info->dev_ready = 0; |
| 1068 | info->need_wait = 1; |
| 1069 | pxa3xx_nand_start(info); |
| 1070 | |
| 1071 | ts = get_timer(0); |
| 1072 | while (1) { |
| 1073 | u32 status; |
| 1074 | |
| 1075 | status = nand_readl(info, NDSR); |
| 1076 | if (status) |
| 1077 | pxa3xx_nand_irq(info); |
| 1078 | |
| 1079 | if (info->cmd_complete) |
| 1080 | break; |
| 1081 | |
| 1082 | if (get_timer(ts) > CHIP_DELAY_TIMEOUT) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1083 | dev_err(mtd->dev, "Wait timeout!!!\n"); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | info->state = STATE_IDLE; |
| 1089 | } |
| 1090 | |
| 1091 | static void nand_cmdfunc_extended(struct mtd_info *mtd, |
| 1092 | const unsigned command, |
| 1093 | int column, int page_addr) |
| 1094 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1095 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1096 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1097 | struct pxa3xx_nand_info *info = host->info_data; |
| 1098 | int exec_cmd, ext_cmd_type; |
| 1099 | |
| 1100 | /* |
| 1101 | * if this is a x16 device then convert the input |
| 1102 | * "byte" address into a "word" address appropriate |
| 1103 | * for indexing a word-oriented device |
| 1104 | */ |
| 1105 | if (info->reg_ndcr & NDCR_DWIDTH_M) |
| 1106 | column /= 2; |
| 1107 | |
| 1108 | /* |
| 1109 | * There may be different NAND chip hooked to |
| 1110 | * different chip select, so check whether |
| 1111 | * chip select has been changed, if yes, reset the timing |
| 1112 | */ |
| 1113 | if (info->cs != host->cs) { |
| 1114 | info->cs = host->cs; |
| 1115 | nand_writel(info, NDTR0CS0, info->ndtr0cs0); |
| 1116 | nand_writel(info, NDTR1CS0, info->ndtr1cs0); |
| 1117 | } |
| 1118 | |
| 1119 | /* Select the extended command for the first command */ |
| 1120 | switch (command) { |
| 1121 | case NAND_CMD_READ0: |
| 1122 | case NAND_CMD_READOOB: |
| 1123 | ext_cmd_type = EXT_CMD_TYPE_MONO; |
| 1124 | break; |
| 1125 | case NAND_CMD_SEQIN: |
| 1126 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
| 1127 | break; |
| 1128 | case NAND_CMD_PAGEPROG: |
| 1129 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
| 1130 | break; |
| 1131 | default: |
| 1132 | ext_cmd_type = 0; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | prepare_start_command(info, command); |
| 1137 | |
| 1138 | /* |
| 1139 | * Prepare the "is ready" completion before starting a command |
| 1140 | * transaction sequence. If the command is not executed the |
| 1141 | * completion will be completed, see below. |
| 1142 | * |
| 1143 | * We can do that inside the loop because the command variable |
| 1144 | * is invariant and thus so is the exec_cmd. |
| 1145 | */ |
| 1146 | info->need_wait = 1; |
| 1147 | info->dev_ready = 0; |
| 1148 | |
| 1149 | do { |
| 1150 | u32 ts; |
| 1151 | |
| 1152 | info->state = STATE_PREPARED; |
| 1153 | exec_cmd = prepare_set_command(info, command, ext_cmd_type, |
| 1154 | column, page_addr); |
| 1155 | if (!exec_cmd) { |
| 1156 | info->need_wait = 0; |
| 1157 | info->dev_ready = 1; |
| 1158 | break; |
| 1159 | } |
| 1160 | |
| 1161 | info->cmd_complete = 0; |
| 1162 | pxa3xx_nand_start(info); |
| 1163 | |
| 1164 | ts = get_timer(0); |
| 1165 | while (1) { |
| 1166 | u32 status; |
| 1167 | |
| 1168 | status = nand_readl(info, NDSR); |
| 1169 | if (status) |
| 1170 | pxa3xx_nand_irq(info); |
| 1171 | |
| 1172 | if (info->cmd_complete) |
| 1173 | break; |
| 1174 | |
| 1175 | if (get_timer(ts) > CHIP_DELAY_TIMEOUT) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1176 | dev_err(mtd->dev, "Wait timeout!!!\n"); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1177 | return; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 1181 | /* Only a few commands need several steps */ |
| 1182 | if (command != NAND_CMD_PAGEPROG && |
| 1183 | command != NAND_CMD_READ0 && |
| 1184 | command != NAND_CMD_READOOB) |
| 1185 | break; |
| 1186 | |
| 1187 | info->cur_chunk++; |
| 1188 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1189 | /* Check if the sequence is complete */ |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 1190 | if (info->cur_chunk == info->ntotalchunks && |
| 1191 | command != NAND_CMD_PAGEPROG) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1192 | break; |
| 1193 | |
| 1194 | /* |
| 1195 | * After a splitted program command sequence has issued |
| 1196 | * the command dispatch, the command sequence is complete. |
| 1197 | */ |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 1198 | if (info->cur_chunk == (info->ntotalchunks + 1) && |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1199 | command == NAND_CMD_PAGEPROG && |
| 1200 | ext_cmd_type == EXT_CMD_TYPE_DISPATCH) |
| 1201 | break; |
| 1202 | |
| 1203 | if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) { |
| 1204 | /* Last read: issue a 'last naked read' */ |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 1205 | if (info->cur_chunk == info->ntotalchunks - 1) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1206 | ext_cmd_type = EXT_CMD_TYPE_LAST_RW; |
| 1207 | else |
| 1208 | ext_cmd_type = EXT_CMD_TYPE_NAKED_RW; |
| 1209 | |
| 1210 | /* |
| 1211 | * If a splitted program command has no more data to transfer, |
| 1212 | * the command dispatch must be issued to complete. |
| 1213 | */ |
| 1214 | } else if (command == NAND_CMD_PAGEPROG && |
Ofer Heifetz | 191b5be | 2018-08-29 11:56:09 +0300 | [diff] [blame] | 1215 | info->cur_chunk == info->ntotalchunks) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1216 | ext_cmd_type = EXT_CMD_TYPE_DISPATCH; |
| 1217 | } |
| 1218 | } while (1); |
| 1219 | |
| 1220 | info->state = STATE_IDLE; |
| 1221 | } |
| 1222 | |
| 1223 | static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 1224 | struct nand_chip *chip, const uint8_t *buf, int oob_required, |
| 1225 | int page) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1226 | { |
| 1227 | chip->write_buf(mtd, buf, mtd->writesize); |
| 1228 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd, |
| 1234 | struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 1235 | int page) |
| 1236 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1237 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1238 | struct pxa3xx_nand_info *info = host->info_data; |
Miquel Raynal | 35f1ebd | 2018-10-11 17:45:43 +0200 | [diff] [blame] | 1239 | int bf; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1240 | |
| 1241 | chip->read_buf(mtd, buf, mtd->writesize); |
| 1242 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1243 | |
| 1244 | if (info->retcode == ERR_CORERR && info->use_ecc) { |
| 1245 | mtd->ecc_stats.corrected += info->ecc_err_cnt; |
| 1246 | |
Miquel Raynal | 35f1ebd | 2018-10-11 17:45:43 +0200 | [diff] [blame] | 1247 | } else if (info->retcode == ERR_UNCORERR && info->ecc_bch) { |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1248 | /* |
Miquel Raynal | 35f1ebd | 2018-10-11 17:45:43 +0200 | [diff] [blame] | 1249 | * Empty pages will trigger uncorrectable errors. Re-read the |
| 1250 | * entire page in raw mode and check for bits not being "1". |
| 1251 | * If there are more than the supported strength, then it means |
| 1252 | * this is an actual uncorrectable error. |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1253 | */ |
Miquel Raynal | 35f1ebd | 2018-10-11 17:45:43 +0200 | [diff] [blame] | 1254 | chip->ecc.read_page_raw(mtd, chip, buf, oob_required, page); |
| 1255 | bf = nand_check_erased_ecc_chunk(buf, mtd->writesize, |
| 1256 | chip->oob_poi, mtd->oobsize, |
| 1257 | NULL, 0, chip->ecc.strength); |
| 1258 | if (bf < 0) { |
| 1259 | mtd->ecc_stats.failed++; |
| 1260 | } else if (bf) { |
| 1261 | mtd->ecc_stats.corrected += bf; |
| 1262 | info->max_bitflips = max_t(unsigned int, |
| 1263 | info->max_bitflips, bf); |
| 1264 | info->retcode = ERR_CORERR; |
| 1265 | } else { |
| 1266 | info->retcode = ERR_NONE; |
| 1267 | } |
| 1268 | |
| 1269 | } else if (info->retcode == ERR_UNCORERR && !info->ecc_bch) { |
| 1270 | /* Raw read is not supported with Hamming ECC engine */ |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1271 | if (is_buf_blank(buf, mtd->writesize)) |
| 1272 | info->retcode = ERR_NONE; |
| 1273 | else |
| 1274 | mtd->ecc_stats.failed++; |
| 1275 | } |
| 1276 | |
| 1277 | return info->max_bitflips; |
| 1278 | } |
| 1279 | |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 1280 | static int pxa3xx_nand_read_page_raw(struct mtd_info *mtd, |
| 1281 | struct nand_chip *chip, uint8_t *buf, |
| 1282 | int oob_required, int page) |
| 1283 | { |
| 1284 | struct pxa3xx_nand_host *host = chip->priv; |
| 1285 | struct pxa3xx_nand_info *info = host->info_data; |
| 1286 | int chunk, ecc_off_buf; |
| 1287 | |
| 1288 | if (!info->ecc_bch) |
| 1289 | return -ENOTSUPP; |
| 1290 | |
| 1291 | /* |
| 1292 | * Set the force_raw boolean, then re-call ->cmdfunc() that will run |
| 1293 | * pxa3xx_nand_start(), which will actually disable the ECC engine. |
| 1294 | */ |
| 1295 | info->force_raw = true; |
| 1296 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
| 1297 | |
| 1298 | ecc_off_buf = (info->nfullchunks * info->spare_size) + |
| 1299 | info->last_spare_size; |
| 1300 | for (chunk = 0; chunk < info->nfullchunks; chunk++) { |
| 1301 | chip->read_buf(mtd, |
| 1302 | buf + (chunk * info->chunk_size), |
| 1303 | info->chunk_size); |
| 1304 | chip->read_buf(mtd, |
| 1305 | chip->oob_poi + |
| 1306 | (chunk * (info->spare_size)), |
| 1307 | info->spare_size); |
| 1308 | chip->read_buf(mtd, |
| 1309 | chip->oob_poi + ecc_off_buf + |
| 1310 | (chunk * (info->ecc_size)), |
| 1311 | info->ecc_size - 2); |
| 1312 | } |
| 1313 | |
| 1314 | if (info->ntotalchunks > info->nfullchunks) { |
| 1315 | chip->read_buf(mtd, |
| 1316 | buf + (info->nfullchunks * info->chunk_size), |
| 1317 | info->last_chunk_size); |
| 1318 | chip->read_buf(mtd, |
| 1319 | chip->oob_poi + |
| 1320 | (info->nfullchunks * (info->spare_size)), |
| 1321 | info->last_spare_size); |
| 1322 | chip->read_buf(mtd, |
| 1323 | chip->oob_poi + ecc_off_buf + |
| 1324 | (info->nfullchunks * (info->ecc_size)), |
| 1325 | info->ecc_size - 2); |
| 1326 | } |
| 1327 | |
| 1328 | info->force_raw = false; |
| 1329 | |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | static int pxa3xx_nand_read_oob_raw(struct mtd_info *mtd, |
| 1334 | struct nand_chip *chip, int page) |
| 1335 | { |
| 1336 | /* Invalidate page cache */ |
| 1337 | chip->pagebuf = -1; |
| 1338 | |
| 1339 | return chip->ecc.read_page_raw(mtd, chip, chip->buffers->databuf, true, |
| 1340 | page); |
| 1341 | } |
| 1342 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1343 | static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd) |
| 1344 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1345 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1346 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1347 | struct pxa3xx_nand_info *info = host->info_data; |
| 1348 | char retval = 0xFF; |
| 1349 | |
| 1350 | if (info->buf_start < info->buf_count) |
| 1351 | /* Has just send a new command? */ |
| 1352 | retval = info->data_buff[info->buf_start++]; |
| 1353 | |
| 1354 | return retval; |
| 1355 | } |
| 1356 | |
| 1357 | static u16 pxa3xx_nand_read_word(struct mtd_info *mtd) |
| 1358 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1359 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1360 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1361 | struct pxa3xx_nand_info *info = host->info_data; |
| 1362 | u16 retval = 0xFFFF; |
| 1363 | |
| 1364 | if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) { |
| 1365 | retval = *((u16 *)(info->data_buff+info->buf_start)); |
| 1366 | info->buf_start += 2; |
| 1367 | } |
| 1368 | return retval; |
| 1369 | } |
| 1370 | |
| 1371 | static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 1372 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1373 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1374 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1375 | struct pxa3xx_nand_info *info = host->info_data; |
| 1376 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1377 | |
| 1378 | memcpy(buf, info->data_buff + info->buf_start, real_len); |
| 1379 | info->buf_start += real_len; |
| 1380 | } |
| 1381 | |
| 1382 | static void pxa3xx_nand_write_buf(struct mtd_info *mtd, |
| 1383 | const uint8_t *buf, int len) |
| 1384 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1385 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1386 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1387 | struct pxa3xx_nand_info *info = host->info_data; |
| 1388 | int real_len = min_t(size_t, len, info->buf_count - info->buf_start); |
| 1389 | |
| 1390 | memcpy(info->data_buff + info->buf_start, buf, real_len); |
| 1391 | info->buf_start += real_len; |
| 1392 | } |
| 1393 | |
| 1394 | static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip) |
| 1395 | { |
| 1396 | return; |
| 1397 | } |
| 1398 | |
| 1399 | static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 1400 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1401 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1402 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1403 | struct pxa3xx_nand_info *info = host->info_data; |
| 1404 | |
| 1405 | if (info->need_wait) { |
| 1406 | u32 ts; |
| 1407 | |
| 1408 | info->need_wait = 0; |
| 1409 | |
| 1410 | ts = get_timer(0); |
| 1411 | while (1) { |
| 1412 | u32 status; |
| 1413 | |
| 1414 | status = nand_readl(info, NDSR); |
| 1415 | if (status) |
| 1416 | pxa3xx_nand_irq(info); |
| 1417 | |
| 1418 | if (info->dev_ready) |
| 1419 | break; |
| 1420 | |
| 1421 | if (get_timer(ts) > CHIP_DELAY_TIMEOUT) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1422 | dev_err(mtd->dev, "Ready timeout!!!\n"); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1423 | return NAND_STATUS_FAIL; |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | /* pxa3xx_nand_send_command has waited for command complete */ |
| 1429 | if (this->state == FL_WRITING || this->state == FL_ERASING) { |
| 1430 | if (info->retcode == ERR_NONE) |
| 1431 | return 0; |
| 1432 | else |
| 1433 | return NAND_STATUS_FAIL; |
| 1434 | } |
| 1435 | |
| 1436 | return NAND_STATUS_READY; |
| 1437 | } |
| 1438 | |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1439 | static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info) |
| 1440 | { |
| 1441 | struct pxa3xx_nand_platform_data *pdata = info->pdata; |
| 1442 | |
| 1443 | /* Configure default flash values */ |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1444 | info->reg_ndcr = 0x0; /* enable all interrupts */ |
| 1445 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
| 1446 | info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES); |
| 1447 | info->reg_ndcr |= NDCR_SPARE_EN; |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1453 | { |
| 1454 | struct pxa3xx_nand_host *host = info->host[info->cs]; |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1455 | struct mtd_info *mtd = nand_to_mtd(&info->host[info->cs]->chip); |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1456 | struct nand_chip *chip = mtd_to_nand(mtd); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1457 | |
| 1458 | info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0; |
| 1459 | info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0; |
| 1460 | info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Ofer Heifetz | 268979f | 2018-08-29 11:56:08 +0300 | [diff] [blame] | 1463 | static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1464 | { |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1465 | struct pxa3xx_nand_platform_data *pdata = info->pdata; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1466 | uint32_t ndcr = nand_readl(info, NDCR); |
| 1467 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1468 | /* Set an initial chunk size */ |
Ofer Heifetz | 4a574aa | 2018-08-29 11:56:05 +0300 | [diff] [blame] | 1469 | info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512; |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1470 | info->reg_ndcr = ndcr & |
| 1471 | ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL); |
| 1472 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1473 | info->ndtr0cs0 = nand_readl(info, NDTR0CS0); |
| 1474 | info->ndtr1cs0 = nand_readl(info, NDTR1CS0); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info) |
| 1478 | { |
| 1479 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
| 1480 | if (info->data_buff == NULL) |
| 1481 | return -ENOMEM; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host) |
| 1486 | { |
| 1487 | struct pxa3xx_nand_info *info = host->info_data; |
| 1488 | struct pxa3xx_nand_platform_data *pdata = info->pdata; |
| 1489 | struct mtd_info *mtd; |
| 1490 | struct nand_chip *chip; |
| 1491 | const struct nand_sdr_timings *timings; |
| 1492 | int ret; |
| 1493 | |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 1494 | mtd = nand_to_mtd(&info->host[info->cs]->chip); |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1495 | chip = mtd_to_nand(mtd); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1496 | |
| 1497 | /* configure default flash values */ |
| 1498 | info->reg_ndcr = 0x0; /* enable all interrupts */ |
| 1499 | info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0; |
Ofer Heifetz | 4a574aa | 2018-08-29 11:56:05 +0300 | [diff] [blame] | 1500 | info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1501 | info->reg_ndcr |= NDCR_SPARE_EN; /* enable spare by default */ |
| 1502 | |
| 1503 | /* use the common timing to make a try */ |
| 1504 | timings = onfi_async_timing_mode_to_sdr_timings(0); |
| 1505 | if (IS_ERR(timings)) |
| 1506 | return PTR_ERR(timings); |
| 1507 | |
| 1508 | pxa3xx_nand_set_sdr_timing(host, timings); |
| 1509 | |
| 1510 | chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0); |
| 1511 | ret = chip->waitfunc(mtd, chip); |
| 1512 | if (ret & NAND_STATUS_FAIL) |
| 1513 | return -ENODEV; |
| 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | static int pxa_ecc_init(struct pxa3xx_nand_info *info, |
| 1519 | struct nand_ecc_ctrl *ecc, |
| 1520 | int strength, int ecc_stepsize, int page_size) |
| 1521 | { |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1522 | int i = 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1523 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1524 | /* if ecc strength is 1 ecc algo is Hamming else bch */ |
| 1525 | info->ecc_bch = (strength == 1) ? 0 : 1; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1526 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1527 | ecc->mode = NAND_ECC_HW; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1528 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1529 | /* ecc->layout is not in use for pxa driver (but shouldn't be NULL)*/ |
| 1530 | if (info->ecc_bch == 1) |
| 1531 | ecc->layout = &ecc_layout_empty; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1532 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1533 | /* for bch actual ecc strength is 16 per chunk */ |
| 1534 | ecc->strength = (info->ecc_bch == 1) ? 16 : 1; |
Konstantin Porotchkin | e0e232e | 2018-08-29 11:56:17 +0300 | [diff] [blame] | 1535 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1536 | while (nfc_layouts[i].strength) { |
| 1537 | if (strength == nfc_layouts[i].strength && page_size == nfc_layouts[i].page_size) { |
| 1538 | info->nfullchunks = nfc_layouts[i].nfullchunks; |
| 1539 | info->chunk_size = nfc_layouts[i].chunk_size; |
| 1540 | info->spare_size = nfc_layouts[i].spare_size; |
| 1541 | info->last_chunk_size = nfc_layouts[i].last_chunk_size; |
| 1542 | info->last_spare_size = nfc_layouts[i].last_spare_size; |
| 1543 | info->ntotalchunks = (info->last_spare_size || info->last_chunk_size) ? |
| 1544 | info->nfullchunks + 1 : info->nfullchunks; |
| 1545 | info->ecc_size = nfc_layouts[i].ecc_size; |
| 1546 | break; |
| 1547 | } |
| 1548 | ++i; |
| 1549 | } |
Konstantin Porotchkin | e0e232e | 2018-08-29 11:56:17 +0300 | [diff] [blame] | 1550 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1551 | /* for bch the ecc is calculated per chunk size and for Hamming it is 512 */ |
| 1552 | ecc->size = (info->ecc_bch) ? info->chunk_size : 512; |
Konstantin Porotchkin | a692cde | 2018-08-29 11:56:16 +0300 | [diff] [blame] | 1553 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1554 | /* nand_scan_tail func perform validity tests for ECC strength, and it |
| 1555 | * assumes that all chunks are with same size. in our case when ecc is 12 |
| 1556 | * the chunk size is 704 but the last chunk is with different size so |
| 1557 | * we cheat it nand_scan_tail validity tests by set info->ecc_size value to 512 |
| 1558 | */ |
| 1559 | if (strength == 12) |
| 1560 | ecc->size = 512; |
Konstantin Porotchkin | a692cde | 2018-08-29 11:56:16 +0300 | [diff] [blame] | 1561 | |
Chris Packham | 03085ca | 2022-08-25 16:59:49 +1200 | [diff] [blame] | 1562 | if (ecc_stepsize != 512 || !(nfc_layouts[i].strength)) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1563 | dev_err(info->controller.active->mtd.dev, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1564 | "ECC strength %d at page size %d is not supported\n", |
| 1565 | strength, page_size); |
| 1566 | return -ENODEV; |
| 1567 | } |
| 1568 | |
| 1569 | return 0; |
| 1570 | } |
| 1571 | |
| 1572 | static int pxa3xx_nand_scan(struct mtd_info *mtd) |
| 1573 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1574 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1575 | struct pxa3xx_nand_host *host = nand_get_controller_data(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1576 | struct pxa3xx_nand_info *info = host->info_data; |
| 1577 | struct pxa3xx_nand_platform_data *pdata = info->pdata; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1578 | int ret; |
| 1579 | uint16_t ecc_strength, ecc_step; |
| 1580 | |
Ofer Heifetz | 268979f | 2018-08-29 11:56:08 +0300 | [diff] [blame] | 1581 | if (pdata->keep_config) { |
| 1582 | pxa3xx_nand_detect_config(info); |
| 1583 | } else { |
| 1584 | ret = pxa3xx_nand_config_ident(info); |
| 1585 | if (ret) |
| 1586 | return ret; |
| 1587 | ret = pxa3xx_nand_sensing(host); |
| 1588 | if (ret) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1589 | dev_info(mtd->dev, "There is no chip on cs %d!\n", |
Ofer Heifetz | 268979f | 2018-08-29 11:56:08 +0300 | [diff] [blame] | 1590 | info->cs); |
| 1591 | return ret; |
| 1592 | } |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1595 | /* Device detection must be done with ECC disabled */ |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1596 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 || |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 1597 | info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K || |
| 1598 | info->variant == PXA3XX_NAND_VARIANT_AC5) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1599 | nand_writel(info, NDECCCTRL, 0x0); |
| 1600 | |
| 1601 | if (nand_scan_ident(mtd, 1, NULL)) |
| 1602 | return -ENODEV; |
| 1603 | |
| 1604 | if (!pdata->keep_config) { |
| 1605 | ret = pxa3xx_nand_init_timings(host); |
| 1606 | if (ret) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1607 | dev_err(mtd->dev, |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1608 | "Failed to set timings: %d\n", ret); |
| 1609 | return ret; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1613 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
| 1614 | /* |
| 1615 | * We'll use a bad block table stored in-flash and don't |
| 1616 | * allow writing the bad block marker to the flash. |
| 1617 | */ |
| 1618 | chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB_BBM; |
| 1619 | chip->bbt_td = &bbt_main_descr; |
| 1620 | chip->bbt_md = &bbt_mirror_descr; |
| 1621 | #endif |
| 1622 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1623 | if (pdata->ecc_strength && pdata->ecc_step_size) { |
| 1624 | ecc_strength = pdata->ecc_strength; |
| 1625 | ecc_step = pdata->ecc_step_size; |
| 1626 | } else { |
| 1627 | ecc_strength = chip->ecc_strength_ds; |
| 1628 | ecc_step = chip->ecc_step_ds; |
| 1629 | } |
| 1630 | |
| 1631 | /* Set default ECC strength requirements on non-ONFI devices */ |
| 1632 | if (ecc_strength < 1 && ecc_step < 1) { |
| 1633 | ecc_strength = 1; |
| 1634 | ecc_step = 512; |
| 1635 | } |
| 1636 | |
| 1637 | ret = pxa_ecc_init(info, &chip->ecc, ecc_strength, |
| 1638 | ecc_step, mtd->writesize); |
| 1639 | if (ret) |
| 1640 | return ret; |
| 1641 | |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 1642 | /* |
| 1643 | * If the page size is bigger than the FIFO size, let's check |
| 1644 | * we are given the right variant and then switch to the extended |
| 1645 | * (aka split) command handling, |
| 1646 | */ |
| 1647 | if (mtd->writesize > info->chunk_size) { |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1648 | if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 || |
Chris Packham | bd3ce6f | 2023-07-10 10:47:34 +1200 | [diff] [blame] | 1649 | info->variant == PXA3XX_NAND_VARIANT_ARMADA_8K || |
| 1650 | info->variant == PXA3XX_NAND_VARIANT_AC5) { |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 1651 | chip->cmdfunc = nand_cmdfunc_extended; |
| 1652 | } else { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1653 | dev_err(mtd->dev, |
Konstantin Porotchkin | 06f9b6b | 2018-08-29 11:56:15 +0300 | [diff] [blame] | 1654 | "unsupported page size on this variant\n"); |
| 1655 | return -ENODEV; |
| 1656 | } |
| 1657 | } |
| 1658 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1659 | /* calculate addressing information */ |
| 1660 | if (mtd->writesize >= 2048) |
| 1661 | host->col_addr_cycles = 2; |
| 1662 | else |
| 1663 | host->col_addr_cycles = 1; |
| 1664 | |
| 1665 | /* release the initial buffer */ |
| 1666 | kfree(info->data_buff); |
| 1667 | |
| 1668 | /* allocate the real data + oob buffer */ |
| 1669 | info->buf_size = mtd->writesize + mtd->oobsize; |
| 1670 | ret = pxa3xx_nand_init_buff(info); |
| 1671 | if (ret) |
| 1672 | return ret; |
| 1673 | info->oob_buff = info->data_buff + mtd->writesize; |
| 1674 | |
| 1675 | if ((mtd->size >> chip->page_shift) > 65536) |
| 1676 | host->row_addr_cycles = 3; |
| 1677 | else |
| 1678 | host->row_addr_cycles = 2; |
Ofer Heifetz | 531816e | 2018-08-29 11:56:07 +0300 | [diff] [blame] | 1679 | |
| 1680 | if (!pdata->keep_config) |
| 1681 | pxa3xx_nand_config_tail(info); |
| 1682 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1683 | return nand_scan_tail(mtd); |
| 1684 | } |
| 1685 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1686 | static int alloc_nand_resource(struct udevice *dev, struct pxa3xx_nand_info *info) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1687 | { |
| 1688 | struct pxa3xx_nand_platform_data *pdata; |
| 1689 | struct pxa3xx_nand_host *host; |
| 1690 | struct nand_chip *chip = NULL; |
| 1691 | struct mtd_info *mtd; |
Baruch Siach | 807ae58 | 2020-10-29 08:52:19 +0200 | [diff] [blame] | 1692 | int cs; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1693 | |
| 1694 | pdata = info->pdata; |
| 1695 | if (pdata->num_cs <= 0) |
| 1696 | return -ENODEV; |
| 1697 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1698 | info->variant = pxa3xx_nand_get_variant(dev); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1699 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Kevin Smith | 4d21b59 | 2016-01-14 16:01:38 +0000 | [diff] [blame] | 1700 | chip = (struct nand_chip *) |
| 1701 | ((u8 *)&info[1] + sizeof(*host) * cs); |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1702 | mtd = nand_to_mtd(chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1703 | host = (struct pxa3xx_nand_host *)chip; |
| 1704 | info->host[cs] = host; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1705 | host->cs = cs; |
| 1706 | host->info_data = info; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1707 | mtd->owner = THIS_MODULE; |
| 1708 | |
Chris Packham | 3c2170a | 2016-08-29 15:20:52 +1200 | [diff] [blame] | 1709 | nand_set_controller_data(chip, host); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1710 | chip->ecc.read_page = pxa3xx_nand_read_page_hwecc; |
Miquel Raynal | 30a016a | 2018-10-11 17:45:42 +0200 | [diff] [blame] | 1711 | chip->ecc.read_page_raw = pxa3xx_nand_read_page_raw; |
| 1712 | chip->ecc.read_oob_raw = pxa3xx_nand_read_oob_raw; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1713 | chip->ecc.write_page = pxa3xx_nand_write_page_hwecc; |
| 1714 | chip->controller = &info->controller; |
| 1715 | chip->waitfunc = pxa3xx_nand_waitfunc; |
| 1716 | chip->select_chip = pxa3xx_nand_select_chip; |
| 1717 | chip->read_word = pxa3xx_nand_read_word; |
| 1718 | chip->read_byte = pxa3xx_nand_read_byte; |
| 1719 | chip->read_buf = pxa3xx_nand_read_buf; |
| 1720 | chip->write_buf = pxa3xx_nand_write_buf; |
| 1721 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
| 1722 | chip->cmdfunc = nand_cmdfunc; |
| 1723 | } |
| 1724 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1725 | /* Allocate a buffer to allow flash detection */ |
| 1726 | info->buf_size = INIT_BUFFER_SIZE; |
| 1727 | info->data_buff = kmalloc(info->buf_size, GFP_KERNEL); |
Baruch Siach | 807ae58 | 2020-10-29 08:52:19 +0200 | [diff] [blame] | 1728 | if (info->data_buff == NULL) |
| 1729 | return -ENOMEM; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1730 | |
| 1731 | /* initialize all interrupts to be disabled */ |
| 1732 | disable_int(info, NDSR_MASK); |
| 1733 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1734 | /* |
| 1735 | * Some SoCs like A7k/A8k need to enable manually the NAND |
| 1736 | * controller to avoid being bootloader dependent. This is done |
| 1737 | * through the use of a single bit in the System Functions registers. |
| 1738 | */ |
| 1739 | if (pxa3xx_nand_get_variant(dev) == PXA3XX_NAND_VARIANT_ARMADA_8K) { |
| 1740 | struct regmap *sysctrl_base = syscon_regmap_lookup_by_phandle( |
| 1741 | dev, "marvell,system-controller"); |
| 1742 | u32 reg; |
| 1743 | |
| 1744 | if (IS_ERR(sysctrl_base)) |
| 1745 | return PTR_ERR(sysctrl_base); |
| 1746 | |
| 1747 | regmap_read(sysctrl_base, GENCONF_SOC_DEVICE_MUX, ®); |
Chris Packham | f2c1ea7 | 2023-07-10 10:47:35 +1200 | [diff] [blame] | 1748 | reg |= GENCONF_SOC_DEVICE_MUX_NFC_EN | GENCONF_SOC_DEVICE_MUX_NFC_DEVBUS_ARB_EN; |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1749 | regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg); |
| 1750 | } |
| 1751 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1752 | return 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1753 | } |
| 1754 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1755 | static int pxa3xx_nand_probe_dt(struct udevice *dev, struct pxa3xx_nand_info *info) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1756 | { |
| 1757 | struct pxa3xx_nand_platform_data *pdata; |
| 1758 | |
| 1759 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 1760 | if (!pdata) |
| 1761 | return -ENOMEM; |
| 1762 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1763 | info->mmio_base = dev_read_addr_ptr(dev); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1764 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1765 | pdata->num_cs = dev_read_u32_default(dev, "num-cs", 1); |
| 1766 | if (pdata->num_cs != 1) { |
| 1767 | pr_err("pxa3xx driver supports single CS only\n"); |
| 1768 | return -EINVAL; |
| 1769 | } |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1770 | |
Pierre Bourdon | fa7890e | 2021-12-25 05:46:29 +0100 | [diff] [blame] | 1771 | if (dev_read_bool(dev, "marvell,nand-enable-arbiter")) |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1772 | pdata->enable_arbiter = 1; |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1773 | |
Pierre Bourdon | fa7890e | 2021-12-25 05:46:29 +0100 | [diff] [blame] | 1774 | if (dev_read_bool(dev, "marvell,nand-keep-config")) |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1775 | pdata->keep_config = 1; |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1776 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1777 | /* |
| 1778 | * ECC parameters. |
| 1779 | * If these are not set, they will be selected according |
| 1780 | * to the detected flash type. |
| 1781 | */ |
| 1782 | /* ECC strength */ |
| 1783 | pdata->ecc_strength = dev_read_u32_default(dev, "nand-ecc-strength", 0); |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1784 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1785 | /* ECC step size */ |
| 1786 | pdata->ecc_step_size = dev_read_u32_default(dev, "nand-ecc-step-size", |
| 1787 | 0); |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1788 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1789 | info->pdata = pdata; |
Konstantin Porotchkin | 93c9f39 | 2017-03-28 18:16:54 +0300 | [diff] [blame] | 1790 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1791 | return 0; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1792 | } |
| 1793 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1794 | static int pxa3xx_nand_probe(struct udevice *dev) |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1795 | { |
| 1796 | struct pxa3xx_nand_platform_data *pdata; |
| 1797 | int ret, cs, probe_success; |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1798 | struct pxa3xx_nand_info *info = dev_get_priv(dev); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1799 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1800 | ret = pxa3xx_nand_probe_dt(dev, info); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1801 | if (ret) |
| 1802 | return ret; |
| 1803 | |
| 1804 | pdata = info->pdata; |
| 1805 | |
Shmuel Hazan | 5898322 | 2020-10-29 08:52:20 +0200 | [diff] [blame] | 1806 | ret = alloc_nand_resource(dev, info); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1807 | if (ret) { |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1808 | dev_err(dev, "alloc nand resource failed\n"); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1809 | return ret; |
| 1810 | } |
| 1811 | |
| 1812 | probe_success = 0; |
| 1813 | for (cs = 0; cs < pdata->num_cs; cs++) { |
Ofer Heifetz | 0da35df | 2018-08-29 11:56:01 +0300 | [diff] [blame] | 1814 | struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip); |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1815 | |
| 1816 | /* |
| 1817 | * The mtd name matches the one used in 'mtdparts' kernel |
| 1818 | * parameter. This name cannot be changed or otherwise |
| 1819 | * user's mtd partitions configuration would get broken. |
| 1820 | */ |
| 1821 | mtd->name = "pxa3xx_nand-0"; |
Robert Marko | 142f41a | 2022-01-05 16:01:00 +0100 | [diff] [blame] | 1822 | mtd->dev = dev; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1823 | info->cs = cs; |
| 1824 | ret = pxa3xx_nand_scan(mtd); |
| 1825 | if (ret) { |
Sean Anderson | c6302f0 | 2020-09-15 10:44:40 -0400 | [diff] [blame] | 1826 | dev_info(mtd->dev, "failed to scan nand at cs %d\n", |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1827 | cs); |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 1831 | if (nand_register(cs, mtd)) |
| 1832 | continue; |
| 1833 | |
| 1834 | probe_success = 1; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | if (!probe_success) |
| 1838 | return -ENODEV; |
| 1839 | |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1843 | U_BOOT_DRIVER(pxa3xx_nand) = { |
| 1844 | .name = "pxa3xx-nand", |
| 1845 | .id = UCLASS_MTD, |
| 1846 | .of_match = pxa3xx_nand_dt_ids, |
| 1847 | .probe = pxa3xx_nand_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1848 | .priv_auto = sizeof(struct pxa3xx_nand_info) + |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1849 | sizeof(struct pxa3xx_nand_host) * CONFIG_SYS_MAX_NAND_DEVICE, |
| 1850 | }; |
| 1851 | |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1852 | void board_nand_init(void) |
| 1853 | { |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1854 | struct udevice *dev; |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1855 | int ret; |
| 1856 | |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1857 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1858 | DM_DRIVER_GET(pxa3xx_nand), &dev); |
Shmuel Hazan | 759349e | 2020-10-29 08:52:18 +0200 | [diff] [blame] | 1859 | if (ret && ret != -ENODEV) { |
| 1860 | pr_err("Failed to initialize %s. (error %d)\n", dev->name, |
| 1861 | ret); |
| 1862 | } |
Stefan Roese | 75659da | 2015-07-23 10:26:16 +0200 | [diff] [blame] | 1863 | } |