Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright © 2010-2015 Broadcom Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | #include <memalign.h> |
| 17 | #include <nand.h> |
| 18 | #include <clk.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 22 | #include <linux/bug.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 23 | #include <linux/err.h> |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 24 | #include <linux/ioport.h> |
| 25 | #include <linux/completion.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/log2.h> |
William Zhang | 3d8ade4 | 2024-09-16 11:58:46 +0200 | [diff] [blame] | 28 | #include <linux/mtd/nand.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 29 | #include <linux/mtd/rawnand.h> |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 30 | #include <asm/processor.h> |
| 31 | #include <dm.h> |
| 32 | |
| 33 | #include "brcmnand.h" |
| 34 | #include "brcmnand_compat.h" |
| 35 | |
| 36 | /* |
| 37 | * This flag controls if WP stays on between erase/write commands to mitigate |
| 38 | * flash corruption due to power glitches. Values: |
| 39 | * 0: NAND_WP is not used or not available |
| 40 | * 1: NAND_WP is set by default, cleared for erase/write operations |
| 41 | * 2: NAND_WP is always cleared |
| 42 | */ |
| 43 | static int wp_on = 1; |
| 44 | module_param(wp_on, int, 0444); |
| 45 | |
| 46 | /*********************************************************************** |
| 47 | * Definitions |
| 48 | ***********************************************************************/ |
| 49 | |
| 50 | #define DRV_NAME "brcmnand" |
| 51 | |
| 52 | #define CMD_NULL 0x00 |
| 53 | #define CMD_PAGE_READ 0x01 |
| 54 | #define CMD_SPARE_AREA_READ 0x02 |
| 55 | #define CMD_STATUS_READ 0x03 |
| 56 | #define CMD_PROGRAM_PAGE 0x04 |
| 57 | #define CMD_PROGRAM_SPARE_AREA 0x05 |
| 58 | #define CMD_COPY_BACK 0x06 |
| 59 | #define CMD_DEVICE_ID_READ 0x07 |
| 60 | #define CMD_BLOCK_ERASE 0x08 |
| 61 | #define CMD_FLASH_RESET 0x09 |
| 62 | #define CMD_BLOCKS_LOCK 0x0a |
| 63 | #define CMD_BLOCKS_LOCK_DOWN 0x0b |
| 64 | #define CMD_BLOCKS_UNLOCK 0x0c |
| 65 | #define CMD_READ_BLOCKS_LOCK_STATUS 0x0d |
| 66 | #define CMD_PARAMETER_READ 0x0e |
| 67 | #define CMD_PARAMETER_CHANGE_COL 0x0f |
| 68 | #define CMD_LOW_LEVEL_OP 0x10 |
| 69 | |
| 70 | struct brcm_nand_dma_desc { |
| 71 | u32 next_desc; |
| 72 | u32 next_desc_ext; |
| 73 | u32 cmd_irq; |
| 74 | u32 dram_addr; |
| 75 | u32 dram_addr_ext; |
| 76 | u32 tfr_len; |
| 77 | u32 total_len; |
| 78 | u32 flash_addr; |
| 79 | u32 flash_addr_ext; |
| 80 | u32 cs; |
| 81 | u32 pad2[5]; |
| 82 | u32 status_valid; |
| 83 | } __packed; |
| 84 | |
| 85 | /* Bitfields for brcm_nand_dma_desc::status_valid */ |
| 86 | #define FLASH_DMA_ECC_ERROR (1 << 8) |
| 87 | #define FLASH_DMA_CORR_ERROR (1 << 9) |
| 88 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 89 | /* Bitfields for DMA_MODE */ |
| 90 | #define FLASH_DMA_MODE_STOP_ON_ERROR BIT(1) /* stop in Uncorr ECC error */ |
| 91 | #define FLASH_DMA_MODE_MODE BIT(0) /* link list */ |
| 92 | #define FLASH_DMA_MODE_MASK (FLASH_DMA_MODE_STOP_ON_ERROR | \ |
| 93 | FLASH_DMA_MODE_MODE) |
| 94 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 95 | /* 512B flash cache in the NAND controller HW */ |
| 96 | #define FC_SHIFT 9U |
| 97 | #define FC_BYTES 512U |
| 98 | #define FC_WORDS (FC_BYTES >> 2) |
| 99 | |
| 100 | #define BRCMNAND_MIN_PAGESIZE 512 |
| 101 | #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024) |
| 102 | #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024) |
| 103 | |
| 104 | #define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY) |
| 105 | #define NAND_POLL_STATUS_TIMEOUT_MS 100 |
| 106 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 107 | /* flash_dma registers */ |
| 108 | enum flash_dma_reg { |
| 109 | FLASH_DMA_REVISION = 0, |
| 110 | FLASH_DMA_FIRST_DESC, |
| 111 | FLASH_DMA_FIRST_DESC_EXT, |
| 112 | FLASH_DMA_CTRL, |
| 113 | FLASH_DMA_MODE, |
| 114 | FLASH_DMA_STATUS, |
| 115 | FLASH_DMA_INTERRUPT_DESC, |
| 116 | FLASH_DMA_INTERRUPT_DESC_EXT, |
| 117 | FLASH_DMA_ERROR_STATUS, |
| 118 | FLASH_DMA_CURRENT_DESC, |
| 119 | FLASH_DMA_CURRENT_DESC_EXT, |
| 120 | }; |
| 121 | |
| 122 | #ifndef __UBOOT__ |
Kamal Dasu | b6233f7 | 2023-02-11 16:29:03 +0100 | [diff] [blame] | 123 | /* flash_dma registers v0*/ |
| 124 | static const u16 flash_dma_regs_v0[] = { |
| 125 | [FLASH_DMA_REVISION] = 0x00, |
| 126 | [FLASH_DMA_FIRST_DESC] = 0x04, |
| 127 | [FLASH_DMA_CTRL] = 0x08, |
| 128 | [FLASH_DMA_MODE] = 0x0c, |
| 129 | [FLASH_DMA_STATUS] = 0x10, |
| 130 | [FLASH_DMA_INTERRUPT_DESC] = 0x14, |
| 131 | [FLASH_DMA_ERROR_STATUS] = 0x18, |
| 132 | [FLASH_DMA_CURRENT_DESC] = 0x1c, |
| 133 | }; |
| 134 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 135 | /* flash_dma registers v1*/ |
| 136 | static const u16 flash_dma_regs_v1[] = { |
| 137 | [FLASH_DMA_REVISION] = 0x00, |
| 138 | [FLASH_DMA_FIRST_DESC] = 0x04, |
| 139 | [FLASH_DMA_FIRST_DESC_EXT] = 0x08, |
| 140 | [FLASH_DMA_CTRL] = 0x0c, |
| 141 | [FLASH_DMA_MODE] = 0x10, |
| 142 | [FLASH_DMA_STATUS] = 0x14, |
| 143 | [FLASH_DMA_INTERRUPT_DESC] = 0x18, |
| 144 | [FLASH_DMA_INTERRUPT_DESC_EXT] = 0x1c, |
| 145 | [FLASH_DMA_ERROR_STATUS] = 0x20, |
| 146 | [FLASH_DMA_CURRENT_DESC] = 0x24, |
| 147 | [FLASH_DMA_CURRENT_DESC_EXT] = 0x28, |
| 148 | }; |
| 149 | |
| 150 | /* flash_dma registers v4 */ |
| 151 | static const u16 flash_dma_regs_v4[] = { |
| 152 | [FLASH_DMA_REVISION] = 0x00, |
| 153 | [FLASH_DMA_FIRST_DESC] = 0x08, |
| 154 | [FLASH_DMA_FIRST_DESC_EXT] = 0x0c, |
| 155 | [FLASH_DMA_CTRL] = 0x10, |
| 156 | [FLASH_DMA_MODE] = 0x14, |
| 157 | [FLASH_DMA_STATUS] = 0x18, |
| 158 | [FLASH_DMA_INTERRUPT_DESC] = 0x20, |
| 159 | [FLASH_DMA_INTERRUPT_DESC_EXT] = 0x24, |
| 160 | [FLASH_DMA_ERROR_STATUS] = 0x28, |
| 161 | [FLASH_DMA_CURRENT_DESC] = 0x30, |
| 162 | [FLASH_DMA_CURRENT_DESC_EXT] = 0x34, |
| 163 | }; |
| 164 | #endif /* __UBOOT__ */ |
| 165 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 166 | /* Controller feature flags */ |
| 167 | enum { |
| 168 | BRCMNAND_HAS_1K_SECTORS = BIT(0), |
| 169 | BRCMNAND_HAS_PREFETCH = BIT(1), |
| 170 | BRCMNAND_HAS_CACHE_MODE = BIT(2), |
| 171 | BRCMNAND_HAS_WP = BIT(3), |
| 172 | }; |
| 173 | |
| 174 | struct brcmnand_controller { |
| 175 | #ifndef __UBOOT__ |
| 176 | struct device *dev; |
| 177 | #else |
| 178 | struct udevice *dev; |
| 179 | #endif /* __UBOOT__ */ |
| 180 | struct nand_hw_control controller; |
| 181 | void __iomem *nand_base; |
| 182 | void __iomem *nand_fc; /* flash cache */ |
| 183 | void __iomem *flash_dma_base; |
| 184 | unsigned int irq; |
| 185 | unsigned int dma_irq; |
| 186 | int nand_version; |
Philippe Reynes | 7f28cf6 | 2019-03-15 15:14:37 +0100 | [diff] [blame] | 187 | int parameter_page_big_endian; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 188 | |
| 189 | /* Some SoCs provide custom interrupt status register(s) */ |
| 190 | struct brcmnand_soc *soc; |
| 191 | |
| 192 | /* Some SoCs have a gateable clock for the controller */ |
| 193 | struct clk *clk; |
| 194 | |
| 195 | int cmd_pending; |
| 196 | bool dma_pending; |
| 197 | struct completion done; |
| 198 | struct completion dma_done; |
| 199 | |
| 200 | /* List of NAND hosts (one for each chip-select) */ |
| 201 | struct list_head host_list; |
| 202 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 203 | /* flash_dma reg */ |
| 204 | const u16 *flash_dma_offsets; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 205 | struct brcm_nand_dma_desc *dma_desc; |
| 206 | dma_addr_t dma_pa; |
| 207 | |
| 208 | /* in-memory cache of the FLASH_CACHE, used only for some commands */ |
| 209 | u8 flash_cache[FC_BYTES]; |
| 210 | |
| 211 | /* Controller revision details */ |
| 212 | const u16 *reg_offsets; |
| 213 | unsigned int reg_spacing; /* between CS1, CS2, ... regs */ |
| 214 | const u8 *cs_offsets; /* within each chip-select */ |
| 215 | const u8 *cs0_offsets; /* within CS0, if different */ |
| 216 | unsigned int max_block_size; |
| 217 | const unsigned int *block_sizes; |
| 218 | unsigned int max_page_size; |
| 219 | const unsigned int *page_sizes; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 220 | unsigned int page_size_shift; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 221 | unsigned int max_oob; |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 222 | u32 ecc_level_shift; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 223 | u32 features; |
| 224 | |
| 225 | /* for low-power standby/resume only */ |
| 226 | u32 nand_cs_nand_select; |
| 227 | u32 nand_cs_nand_xor; |
| 228 | u32 corr_stat_threshold; |
| 229 | u32 flash_dma_mode; |
| 230 | }; |
| 231 | |
| 232 | struct brcmnand_cfg { |
| 233 | u64 device_size; |
| 234 | unsigned int block_size; |
| 235 | unsigned int page_size; |
| 236 | unsigned int spare_area_size; |
| 237 | unsigned int device_width; |
| 238 | unsigned int col_adr_bytes; |
| 239 | unsigned int blk_adr_bytes; |
| 240 | unsigned int ful_adr_bytes; |
| 241 | unsigned int sector_size_1k; |
| 242 | unsigned int ecc_level; |
| 243 | /* use for low-power standby/resume only */ |
| 244 | u32 acc_control; |
| 245 | u32 config; |
| 246 | u32 config_ext; |
| 247 | u32 timing_1; |
| 248 | u32 timing_2; |
| 249 | }; |
| 250 | |
| 251 | struct brcmnand_host { |
| 252 | struct list_head node; |
| 253 | |
| 254 | struct nand_chip chip; |
| 255 | #ifndef __UBOOT__ |
| 256 | struct platform_device *pdev; |
| 257 | #else |
| 258 | struct udevice *pdev; |
| 259 | #endif /* __UBOOT__ */ |
| 260 | int cs; |
| 261 | |
| 262 | unsigned int last_cmd; |
| 263 | unsigned int last_byte; |
| 264 | u64 last_addr; |
| 265 | struct brcmnand_cfg hwcfg; |
| 266 | struct brcmnand_controller *ctrl; |
| 267 | }; |
| 268 | |
| 269 | enum brcmnand_reg { |
| 270 | BRCMNAND_CMD_START = 0, |
| 271 | BRCMNAND_CMD_EXT_ADDRESS, |
| 272 | BRCMNAND_CMD_ADDRESS, |
| 273 | BRCMNAND_INTFC_STATUS, |
| 274 | BRCMNAND_CS_SELECT, |
| 275 | BRCMNAND_CS_XOR, |
| 276 | BRCMNAND_LL_OP, |
| 277 | BRCMNAND_CS0_BASE, |
| 278 | BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */ |
| 279 | BRCMNAND_CORR_THRESHOLD, |
| 280 | BRCMNAND_CORR_THRESHOLD_EXT, |
| 281 | BRCMNAND_UNCORR_COUNT, |
| 282 | BRCMNAND_CORR_COUNT, |
| 283 | BRCMNAND_CORR_EXT_ADDR, |
| 284 | BRCMNAND_CORR_ADDR, |
| 285 | BRCMNAND_UNCORR_EXT_ADDR, |
| 286 | BRCMNAND_UNCORR_ADDR, |
| 287 | BRCMNAND_SEMAPHORE, |
| 288 | BRCMNAND_ID, |
| 289 | BRCMNAND_ID_EXT, |
| 290 | BRCMNAND_LL_RDATA, |
| 291 | BRCMNAND_OOB_READ_BASE, |
| 292 | BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */ |
| 293 | BRCMNAND_OOB_WRITE_BASE, |
| 294 | BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */ |
| 295 | BRCMNAND_FC_BASE, |
| 296 | }; |
| 297 | |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 298 | /* BRCMNAND v2.1-v2.2 */ |
| 299 | static const u16 brcmnand_regs_v21[] = { |
| 300 | [BRCMNAND_CMD_START] = 0x04, |
| 301 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 302 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 303 | [BRCMNAND_INTFC_STATUS] = 0x5c, |
| 304 | [BRCMNAND_CS_SELECT] = 0x14, |
| 305 | [BRCMNAND_CS_XOR] = 0x18, |
| 306 | [BRCMNAND_LL_OP] = 0, |
| 307 | [BRCMNAND_CS0_BASE] = 0x40, |
| 308 | [BRCMNAND_CS1_BASE] = 0, |
| 309 | [BRCMNAND_CORR_THRESHOLD] = 0, |
| 310 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0, |
| 311 | [BRCMNAND_UNCORR_COUNT] = 0, |
| 312 | [BRCMNAND_CORR_COUNT] = 0, |
| 313 | [BRCMNAND_CORR_EXT_ADDR] = 0x60, |
| 314 | [BRCMNAND_CORR_ADDR] = 0x64, |
| 315 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x68, |
| 316 | [BRCMNAND_UNCORR_ADDR] = 0x6c, |
| 317 | [BRCMNAND_SEMAPHORE] = 0x50, |
| 318 | [BRCMNAND_ID] = 0x54, |
| 319 | [BRCMNAND_ID_EXT] = 0, |
| 320 | [BRCMNAND_LL_RDATA] = 0, |
| 321 | [BRCMNAND_OOB_READ_BASE] = 0x20, |
| 322 | [BRCMNAND_OOB_READ_10_BASE] = 0, |
| 323 | [BRCMNAND_OOB_WRITE_BASE] = 0x30, |
| 324 | [BRCMNAND_OOB_WRITE_10_BASE] = 0, |
| 325 | [BRCMNAND_FC_BASE] = 0x200, |
| 326 | }; |
| 327 | |
Álvaro Fernández Rojas | 7a64a75 | 2023-02-11 16:29:05 +0100 | [diff] [blame] | 328 | /* BRCMNAND v3.3-v4.0 */ |
| 329 | static const u16 brcmnand_regs_v33[] = { |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 330 | [BRCMNAND_CMD_START] = 0x04, |
| 331 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 332 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 333 | [BRCMNAND_INTFC_STATUS] = 0x6c, |
| 334 | [BRCMNAND_CS_SELECT] = 0x14, |
| 335 | [BRCMNAND_CS_XOR] = 0x18, |
| 336 | [BRCMNAND_LL_OP] = 0x178, |
| 337 | [BRCMNAND_CS0_BASE] = 0x40, |
| 338 | [BRCMNAND_CS1_BASE] = 0xd0, |
| 339 | [BRCMNAND_CORR_THRESHOLD] = 0x84, |
| 340 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0, |
| 341 | [BRCMNAND_UNCORR_COUNT] = 0, |
| 342 | [BRCMNAND_CORR_COUNT] = 0, |
| 343 | [BRCMNAND_CORR_EXT_ADDR] = 0x70, |
| 344 | [BRCMNAND_CORR_ADDR] = 0x74, |
| 345 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x78, |
| 346 | [BRCMNAND_UNCORR_ADDR] = 0x7c, |
| 347 | [BRCMNAND_SEMAPHORE] = 0x58, |
| 348 | [BRCMNAND_ID] = 0x60, |
| 349 | [BRCMNAND_ID_EXT] = 0x64, |
| 350 | [BRCMNAND_LL_RDATA] = 0x17c, |
| 351 | [BRCMNAND_OOB_READ_BASE] = 0x20, |
| 352 | [BRCMNAND_OOB_READ_10_BASE] = 0x130, |
| 353 | [BRCMNAND_OOB_WRITE_BASE] = 0x30, |
| 354 | [BRCMNAND_OOB_WRITE_10_BASE] = 0, |
| 355 | [BRCMNAND_FC_BASE] = 0x200, |
| 356 | }; |
| 357 | |
| 358 | /* BRCMNAND v5.0 */ |
| 359 | static const u16 brcmnand_regs_v50[] = { |
| 360 | [BRCMNAND_CMD_START] = 0x04, |
| 361 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 362 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 363 | [BRCMNAND_INTFC_STATUS] = 0x6c, |
| 364 | [BRCMNAND_CS_SELECT] = 0x14, |
| 365 | [BRCMNAND_CS_XOR] = 0x18, |
| 366 | [BRCMNAND_LL_OP] = 0x178, |
| 367 | [BRCMNAND_CS0_BASE] = 0x40, |
| 368 | [BRCMNAND_CS1_BASE] = 0xd0, |
| 369 | [BRCMNAND_CORR_THRESHOLD] = 0x84, |
| 370 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0, |
| 371 | [BRCMNAND_UNCORR_COUNT] = 0, |
| 372 | [BRCMNAND_CORR_COUNT] = 0, |
| 373 | [BRCMNAND_CORR_EXT_ADDR] = 0x70, |
| 374 | [BRCMNAND_CORR_ADDR] = 0x74, |
| 375 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x78, |
| 376 | [BRCMNAND_UNCORR_ADDR] = 0x7c, |
| 377 | [BRCMNAND_SEMAPHORE] = 0x58, |
| 378 | [BRCMNAND_ID] = 0x60, |
| 379 | [BRCMNAND_ID_EXT] = 0x64, |
| 380 | [BRCMNAND_LL_RDATA] = 0x17c, |
| 381 | [BRCMNAND_OOB_READ_BASE] = 0x20, |
| 382 | [BRCMNAND_OOB_READ_10_BASE] = 0x130, |
| 383 | [BRCMNAND_OOB_WRITE_BASE] = 0x30, |
| 384 | [BRCMNAND_OOB_WRITE_10_BASE] = 0x140, |
| 385 | [BRCMNAND_FC_BASE] = 0x200, |
| 386 | }; |
| 387 | |
| 388 | /* BRCMNAND v6.0 - v7.1 */ |
| 389 | static const u16 brcmnand_regs_v60[] = { |
| 390 | [BRCMNAND_CMD_START] = 0x04, |
| 391 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 392 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 393 | [BRCMNAND_INTFC_STATUS] = 0x14, |
| 394 | [BRCMNAND_CS_SELECT] = 0x18, |
| 395 | [BRCMNAND_CS_XOR] = 0x1c, |
| 396 | [BRCMNAND_LL_OP] = 0x20, |
| 397 | [BRCMNAND_CS0_BASE] = 0x50, |
| 398 | [BRCMNAND_CS1_BASE] = 0, |
| 399 | [BRCMNAND_CORR_THRESHOLD] = 0xc0, |
| 400 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4, |
| 401 | [BRCMNAND_UNCORR_COUNT] = 0xfc, |
| 402 | [BRCMNAND_CORR_COUNT] = 0x100, |
| 403 | [BRCMNAND_CORR_EXT_ADDR] = 0x10c, |
| 404 | [BRCMNAND_CORR_ADDR] = 0x110, |
| 405 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x114, |
| 406 | [BRCMNAND_UNCORR_ADDR] = 0x118, |
| 407 | [BRCMNAND_SEMAPHORE] = 0x150, |
| 408 | [BRCMNAND_ID] = 0x194, |
| 409 | [BRCMNAND_ID_EXT] = 0x198, |
| 410 | [BRCMNAND_LL_RDATA] = 0x19c, |
| 411 | [BRCMNAND_OOB_READ_BASE] = 0x200, |
| 412 | [BRCMNAND_OOB_READ_10_BASE] = 0, |
| 413 | [BRCMNAND_OOB_WRITE_BASE] = 0x280, |
| 414 | [BRCMNAND_OOB_WRITE_10_BASE] = 0, |
| 415 | [BRCMNAND_FC_BASE] = 0x400, |
| 416 | }; |
| 417 | |
| 418 | /* BRCMNAND v7.1 */ |
| 419 | static const u16 brcmnand_regs_v71[] = { |
| 420 | [BRCMNAND_CMD_START] = 0x04, |
| 421 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 422 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 423 | [BRCMNAND_INTFC_STATUS] = 0x14, |
| 424 | [BRCMNAND_CS_SELECT] = 0x18, |
| 425 | [BRCMNAND_CS_XOR] = 0x1c, |
| 426 | [BRCMNAND_LL_OP] = 0x20, |
| 427 | [BRCMNAND_CS0_BASE] = 0x50, |
| 428 | [BRCMNAND_CS1_BASE] = 0, |
| 429 | [BRCMNAND_CORR_THRESHOLD] = 0xdc, |
| 430 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0, |
| 431 | [BRCMNAND_UNCORR_COUNT] = 0xfc, |
| 432 | [BRCMNAND_CORR_COUNT] = 0x100, |
| 433 | [BRCMNAND_CORR_EXT_ADDR] = 0x10c, |
| 434 | [BRCMNAND_CORR_ADDR] = 0x110, |
| 435 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x114, |
| 436 | [BRCMNAND_UNCORR_ADDR] = 0x118, |
| 437 | [BRCMNAND_SEMAPHORE] = 0x150, |
| 438 | [BRCMNAND_ID] = 0x194, |
| 439 | [BRCMNAND_ID_EXT] = 0x198, |
| 440 | [BRCMNAND_LL_RDATA] = 0x19c, |
| 441 | [BRCMNAND_OOB_READ_BASE] = 0x200, |
| 442 | [BRCMNAND_OOB_READ_10_BASE] = 0, |
| 443 | [BRCMNAND_OOB_WRITE_BASE] = 0x280, |
| 444 | [BRCMNAND_OOB_WRITE_10_BASE] = 0, |
| 445 | [BRCMNAND_FC_BASE] = 0x400, |
| 446 | }; |
| 447 | |
| 448 | /* BRCMNAND v7.2 */ |
| 449 | static const u16 brcmnand_regs_v72[] = { |
| 450 | [BRCMNAND_CMD_START] = 0x04, |
| 451 | [BRCMNAND_CMD_EXT_ADDRESS] = 0x08, |
| 452 | [BRCMNAND_CMD_ADDRESS] = 0x0c, |
| 453 | [BRCMNAND_INTFC_STATUS] = 0x14, |
| 454 | [BRCMNAND_CS_SELECT] = 0x18, |
| 455 | [BRCMNAND_CS_XOR] = 0x1c, |
| 456 | [BRCMNAND_LL_OP] = 0x20, |
| 457 | [BRCMNAND_CS0_BASE] = 0x50, |
| 458 | [BRCMNAND_CS1_BASE] = 0, |
| 459 | [BRCMNAND_CORR_THRESHOLD] = 0xdc, |
| 460 | [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0, |
| 461 | [BRCMNAND_UNCORR_COUNT] = 0xfc, |
| 462 | [BRCMNAND_CORR_COUNT] = 0x100, |
| 463 | [BRCMNAND_CORR_EXT_ADDR] = 0x10c, |
| 464 | [BRCMNAND_CORR_ADDR] = 0x110, |
| 465 | [BRCMNAND_UNCORR_EXT_ADDR] = 0x114, |
| 466 | [BRCMNAND_UNCORR_ADDR] = 0x118, |
| 467 | [BRCMNAND_SEMAPHORE] = 0x150, |
| 468 | [BRCMNAND_ID] = 0x194, |
| 469 | [BRCMNAND_ID_EXT] = 0x198, |
| 470 | [BRCMNAND_LL_RDATA] = 0x19c, |
| 471 | [BRCMNAND_OOB_READ_BASE] = 0x200, |
| 472 | [BRCMNAND_OOB_READ_10_BASE] = 0, |
| 473 | [BRCMNAND_OOB_WRITE_BASE] = 0x400, |
| 474 | [BRCMNAND_OOB_WRITE_10_BASE] = 0, |
| 475 | [BRCMNAND_FC_BASE] = 0x600, |
| 476 | }; |
| 477 | |
| 478 | enum brcmnand_cs_reg { |
| 479 | BRCMNAND_CS_CFG_EXT = 0, |
| 480 | BRCMNAND_CS_CFG, |
| 481 | BRCMNAND_CS_ACC_CONTROL, |
| 482 | BRCMNAND_CS_TIMING1, |
| 483 | BRCMNAND_CS_TIMING2, |
| 484 | }; |
| 485 | |
| 486 | /* Per chip-select offsets for v7.1 */ |
| 487 | static const u8 brcmnand_cs_offsets_v71[] = { |
| 488 | [BRCMNAND_CS_ACC_CONTROL] = 0x00, |
| 489 | [BRCMNAND_CS_CFG_EXT] = 0x04, |
| 490 | [BRCMNAND_CS_CFG] = 0x08, |
| 491 | [BRCMNAND_CS_TIMING1] = 0x0c, |
| 492 | [BRCMNAND_CS_TIMING2] = 0x10, |
| 493 | }; |
| 494 | |
| 495 | /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */ |
| 496 | static const u8 brcmnand_cs_offsets[] = { |
| 497 | [BRCMNAND_CS_ACC_CONTROL] = 0x00, |
| 498 | [BRCMNAND_CS_CFG_EXT] = 0x04, |
| 499 | [BRCMNAND_CS_CFG] = 0x04, |
| 500 | [BRCMNAND_CS_TIMING1] = 0x08, |
| 501 | [BRCMNAND_CS_TIMING2] = 0x0c, |
| 502 | }; |
| 503 | |
| 504 | /* Per chip-select offset for <= v5.0 on CS0 only */ |
| 505 | static const u8 brcmnand_cs_offsets_cs0[] = { |
| 506 | [BRCMNAND_CS_ACC_CONTROL] = 0x00, |
| 507 | [BRCMNAND_CS_CFG_EXT] = 0x08, |
| 508 | [BRCMNAND_CS_CFG] = 0x08, |
| 509 | [BRCMNAND_CS_TIMING1] = 0x10, |
| 510 | [BRCMNAND_CS_TIMING2] = 0x14, |
| 511 | }; |
| 512 | |
| 513 | /* |
| 514 | * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had |
| 515 | * one config register, but once the bitfields overflowed, newer controllers |
| 516 | * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around. |
| 517 | */ |
| 518 | enum { |
| 519 | CFG_BLK_ADR_BYTES_SHIFT = 8, |
| 520 | CFG_COL_ADR_BYTES_SHIFT = 12, |
| 521 | CFG_FUL_ADR_BYTES_SHIFT = 16, |
| 522 | CFG_BUS_WIDTH_SHIFT = 23, |
| 523 | CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT), |
| 524 | CFG_DEVICE_SIZE_SHIFT = 24, |
| 525 | |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 526 | /* Only for v2.1 */ |
| 527 | CFG_PAGE_SIZE_SHIFT_v2_1 = 30, |
| 528 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 529 | /* Only for pre-v7.1 (with no CFG_EXT register) */ |
| 530 | CFG_PAGE_SIZE_SHIFT = 20, |
| 531 | CFG_BLK_SIZE_SHIFT = 28, |
| 532 | |
| 533 | /* Only for v7.1+ (with CFG_EXT register) */ |
| 534 | CFG_EXT_PAGE_SIZE_SHIFT = 0, |
| 535 | CFG_EXT_BLK_SIZE_SHIFT = 4, |
| 536 | }; |
| 537 | |
| 538 | /* BRCMNAND_INTFC_STATUS */ |
| 539 | enum { |
| 540 | INTFC_FLASH_STATUS = GENMASK(7, 0), |
| 541 | |
| 542 | INTFC_ERASED = BIT(27), |
| 543 | INTFC_OOB_VALID = BIT(28), |
| 544 | INTFC_CACHE_VALID = BIT(29), |
| 545 | INTFC_FLASH_READY = BIT(30), |
| 546 | INTFC_CTLR_READY = BIT(31), |
| 547 | }; |
| 548 | |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 549 | /*********************************************************************** |
| 550 | * NAND ACC CONTROL bitfield |
| 551 | * |
| 552 | * Some bits have remained constant throughout hardware revision, while |
| 553 | * others have shifted around. |
| 554 | ***********************************************************************/ |
| 555 | |
| 556 | /* Constant for all versions (where supported) */ |
| 557 | enum { |
| 558 | /* See BRCMNAND_HAS_CACHE_MODE */ |
| 559 | ACC_CONTROL_CACHE_MODE = BIT(22), |
| 560 | |
| 561 | /* See BRCMNAND_HAS_PREFETCH */ |
| 562 | ACC_CONTROL_PREFETCH = BIT(23), |
| 563 | |
| 564 | ACC_CONTROL_PAGE_HIT = BIT(24), |
| 565 | ACC_CONTROL_WR_PREEMPT = BIT(25), |
| 566 | ACC_CONTROL_PARTIAL_PAGE = BIT(26), |
| 567 | ACC_CONTROL_RD_ERASED = BIT(27), |
| 568 | ACC_CONTROL_FAST_PGM_RDIN = BIT(28), |
| 569 | ACC_CONTROL_WR_ECC = BIT(30), |
| 570 | ACC_CONTROL_RD_ECC = BIT(31), |
| 571 | }; |
| 572 | |
| 573 | #define ACC_CONTROL_ECC_SHIFT 16 |
| 574 | /* Only for v7.2 */ |
| 575 | #define ACC_CONTROL_ECC_EXT_SHIFT 13 |
| 576 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 577 | static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs) |
| 578 | { |
| 579 | return brcmnand_readl(ctrl->nand_base + offs); |
| 580 | } |
| 581 | |
| 582 | static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs, |
| 583 | u32 val) |
| 584 | { |
| 585 | brcmnand_writel(val, ctrl->nand_base + offs); |
| 586 | } |
| 587 | |
| 588 | static int brcmnand_revision_init(struct brcmnand_controller *ctrl) |
| 589 | { |
| 590 | static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 }; |
| 591 | static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 }; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 592 | static const unsigned int block_sizes_v2_2[] = { 16, 128, 8, 512, 256, 0 }; |
| 593 | static const unsigned int block_sizes_v2_1[] = { 16, 128, 8, 512, 0 }; |
Álvaro Fernández Rojas | b5e800b | 2023-02-11 16:29:07 +0100 | [diff] [blame] | 594 | static const unsigned int page_sizes_v3_4[] = { 512, 2048, 4096, 8192, 0 }; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 595 | static const unsigned int page_sizes_v2_2[] = { 512, 2048, 4096, 0 }; |
| 596 | static const unsigned int page_sizes_v2_1[] = { 512, 2048, 0 }; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 597 | |
| 598 | ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff; |
| 599 | |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 600 | /* Only support v2.1+ */ |
| 601 | if (ctrl->nand_version < 0x0201) { |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 602 | dev_err(ctrl->dev, "version %#x not supported\n", |
| 603 | ctrl->nand_version); |
| 604 | return -ENODEV; |
| 605 | } |
| 606 | |
| 607 | /* Register offsets */ |
| 608 | if (ctrl->nand_version >= 0x0702) |
| 609 | ctrl->reg_offsets = brcmnand_regs_v72; |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 610 | else if (ctrl->nand_version == 0x0701) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 611 | ctrl->reg_offsets = brcmnand_regs_v71; |
| 612 | else if (ctrl->nand_version >= 0x0600) |
| 613 | ctrl->reg_offsets = brcmnand_regs_v60; |
| 614 | else if (ctrl->nand_version >= 0x0500) |
| 615 | ctrl->reg_offsets = brcmnand_regs_v50; |
Álvaro Fernández Rojas | 7a64a75 | 2023-02-11 16:29:05 +0100 | [diff] [blame] | 616 | else if (ctrl->nand_version >= 0x0303) |
| 617 | ctrl->reg_offsets = brcmnand_regs_v33; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 618 | else if (ctrl->nand_version >= 0x0201) |
| 619 | ctrl->reg_offsets = brcmnand_regs_v21; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 620 | |
| 621 | /* Chip-select stride */ |
| 622 | if (ctrl->nand_version >= 0x0701) |
| 623 | ctrl->reg_spacing = 0x14; |
| 624 | else |
| 625 | ctrl->reg_spacing = 0x10; |
| 626 | |
| 627 | /* Per chip-select registers */ |
| 628 | if (ctrl->nand_version >= 0x0701) { |
| 629 | ctrl->cs_offsets = brcmnand_cs_offsets_v71; |
| 630 | } else { |
| 631 | ctrl->cs_offsets = brcmnand_cs_offsets; |
| 632 | |
Álvaro Fernández Rojas | 2246119 | 2023-02-11 16:29:06 +0100 | [diff] [blame] | 633 | /* v3.3-5.0 have a different CS0 offset layout */ |
| 634 | if (ctrl->nand_version >= 0x0303 && |
| 635 | ctrl->nand_version <= 0x0500) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 636 | ctrl->cs0_offsets = brcmnand_cs_offsets_cs0; |
| 637 | } |
| 638 | |
| 639 | /* Page / block sizes */ |
| 640 | if (ctrl->nand_version >= 0x0701) { |
| 641 | /* >= v7.1 use nice power-of-2 values! */ |
| 642 | ctrl->max_page_size = 16 * 1024; |
| 643 | ctrl->max_block_size = 2 * 1024 * 1024; |
| 644 | } else { |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 645 | if (ctrl->nand_version >= 0x0304) |
| 646 | ctrl->page_sizes = page_sizes_v3_4; |
| 647 | else if (ctrl->nand_version >= 0x0202) |
| 648 | ctrl->page_sizes = page_sizes_v2_2; |
| 649 | else |
| 650 | ctrl->page_sizes = page_sizes_v2_1; |
| 651 | |
| 652 | if (ctrl->nand_version >= 0x0202) |
| 653 | ctrl->page_size_shift = CFG_PAGE_SIZE_SHIFT; |
| 654 | else |
| 655 | ctrl->page_size_shift = CFG_PAGE_SIZE_SHIFT_v2_1; |
| 656 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 657 | if (ctrl->nand_version >= 0x0600) |
| 658 | ctrl->block_sizes = block_sizes_v6; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 659 | else if (ctrl->nand_version >= 0x0400) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 660 | ctrl->block_sizes = block_sizes_v4; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 661 | else if (ctrl->nand_version >= 0x0202) |
| 662 | ctrl->block_sizes = block_sizes_v2_2; |
| 663 | else |
| 664 | ctrl->block_sizes = block_sizes_v2_1; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 665 | |
| 666 | if (ctrl->nand_version < 0x0400) { |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 667 | if (ctrl->nand_version < 0x0202) |
| 668 | ctrl->max_page_size = 2048; |
| 669 | else |
| 670 | ctrl->max_page_size = 4096; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 671 | ctrl->max_block_size = 512 * 1024; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /* Maximum spare area sector size (per 512B) */ |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 676 | if (ctrl->nand_version == 0x0702) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 677 | ctrl->max_oob = 128; |
| 678 | else if (ctrl->nand_version >= 0x0600) |
| 679 | ctrl->max_oob = 64; |
| 680 | else if (ctrl->nand_version >= 0x0500) |
| 681 | ctrl->max_oob = 32; |
| 682 | else |
| 683 | ctrl->max_oob = 16; |
| 684 | |
| 685 | /* v6.0 and newer (except v6.1) have prefetch support */ |
| 686 | if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601) |
| 687 | ctrl->features |= BRCMNAND_HAS_PREFETCH; |
| 688 | |
| 689 | /* |
| 690 | * v6.x has cache mode, but it's implemented differently. Ignore it for |
| 691 | * now. |
| 692 | */ |
| 693 | if (ctrl->nand_version >= 0x0700) |
| 694 | ctrl->features |= BRCMNAND_HAS_CACHE_MODE; |
| 695 | |
| 696 | if (ctrl->nand_version >= 0x0500) |
| 697 | ctrl->features |= BRCMNAND_HAS_1K_SECTORS; |
| 698 | |
| 699 | if (ctrl->nand_version >= 0x0700) |
| 700 | ctrl->features |= BRCMNAND_HAS_WP; |
| 701 | #ifndef __UBOOT__ |
| 702 | else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp")) |
| 703 | #else |
| 704 | else if (dev_read_bool(ctrl->dev, "brcm,nand-has-wp")) |
| 705 | #endif /* __UBOOT__ */ |
| 706 | ctrl->features |= BRCMNAND_HAS_WP; |
| 707 | |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 708 | /* v7.2 has different ecc level shift in the acc register */ |
| 709 | if (ctrl->nand_version == 0x0702) |
| 710 | ctrl->ecc_level_shift = ACC_CONTROL_ECC_EXT_SHIFT; |
| 711 | else |
| 712 | ctrl->ecc_level_shift = ACC_CONTROL_ECC_SHIFT; |
| 713 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 717 | #ifndef __UBOOT__ |
| 718 | static void brcmnand_flash_dma_revision_init(struct brcmnand_controller *ctrl) |
| 719 | { |
| 720 | /* flash_dma register offsets */ |
| 721 | if (ctrl->nand_version >= 0x0703) |
| 722 | ctrl->flash_dma_offsets = flash_dma_regs_v4; |
Kamal Dasu | b6233f7 | 2023-02-11 16:29:03 +0100 | [diff] [blame] | 723 | else if (ctrl->nand_version == 0x0602) |
| 724 | ctrl->flash_dma_offsets = flash_dma_regs_v0; |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 725 | else |
| 726 | ctrl->flash_dma_offsets = flash_dma_regs_v1; |
| 727 | } |
| 728 | #endif /* __UBOOT__ */ |
| 729 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 730 | static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl, |
| 731 | enum brcmnand_reg reg) |
| 732 | { |
| 733 | u16 offs = ctrl->reg_offsets[reg]; |
| 734 | |
| 735 | if (offs) |
| 736 | return nand_readreg(ctrl, offs); |
| 737 | else |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl, |
| 742 | enum brcmnand_reg reg, u32 val) |
| 743 | { |
| 744 | u16 offs = ctrl->reg_offsets[reg]; |
| 745 | |
| 746 | if (offs) |
| 747 | nand_writereg(ctrl, offs, val); |
| 748 | } |
| 749 | |
| 750 | static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl, |
| 751 | enum brcmnand_reg reg, u32 mask, unsigned |
| 752 | int shift, u32 val) |
| 753 | { |
| 754 | u32 tmp = brcmnand_read_reg(ctrl, reg); |
| 755 | |
| 756 | tmp &= ~mask; |
| 757 | tmp |= val << shift; |
| 758 | brcmnand_write_reg(ctrl, reg, tmp); |
| 759 | } |
| 760 | |
| 761 | static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word) |
| 762 | { |
| 763 | return __raw_readl(ctrl->nand_fc + word * 4); |
| 764 | } |
| 765 | |
| 766 | static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl, |
| 767 | int word, u32 val) |
| 768 | { |
| 769 | __raw_writel(val, ctrl->nand_fc + word * 4); |
| 770 | } |
| 771 | |
Linus Walleij | d2ad157 | 2024-09-16 11:58:47 +0200 | [diff] [blame] | 772 | static inline void brcmnand_read_data_bus(struct brcmnand_controller *ctrl, |
| 773 | void __iomem *flash_cache, u32 *buffer, int fc_words) |
| 774 | { |
| 775 | struct brcmnand_soc *soc = ctrl->soc; |
| 776 | int i; |
| 777 | |
| 778 | if (soc && soc->read_data_bus) { |
| 779 | soc->read_data_bus(soc, flash_cache, buffer, fc_words); |
| 780 | } else { |
| 781 | for (i = 0; i < fc_words; i++) |
| 782 | buffer[i] = brcmnand_read_fc(ctrl, i); |
| 783 | } |
| 784 | } |
| 785 | |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 786 | static void brcmnand_clear_ecc_addr(struct brcmnand_controller *ctrl) |
| 787 | { |
| 788 | |
| 789 | /* Clear error addresses */ |
| 790 | brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0); |
| 791 | brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0); |
| 792 | brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0); |
| 793 | brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0); |
| 794 | } |
| 795 | |
| 796 | static u64 brcmnand_get_uncorrecc_addr(struct brcmnand_controller *ctrl) |
| 797 | { |
| 798 | u64 err_addr; |
| 799 | |
| 800 | err_addr = brcmnand_read_reg(ctrl, BRCMNAND_UNCORR_ADDR); |
| 801 | err_addr |= ((u64)(brcmnand_read_reg(ctrl, |
| 802 | BRCMNAND_UNCORR_EXT_ADDR) |
| 803 | & 0xffff) << 32); |
| 804 | |
| 805 | return err_addr; |
| 806 | } |
| 807 | |
| 808 | static u64 brcmnand_get_correcc_addr(struct brcmnand_controller *ctrl) |
| 809 | { |
| 810 | u64 err_addr; |
| 811 | |
| 812 | err_addr = brcmnand_read_reg(ctrl, BRCMNAND_CORR_ADDR); |
| 813 | err_addr |= ((u64)(brcmnand_read_reg(ctrl, |
| 814 | BRCMNAND_CORR_EXT_ADDR) |
| 815 | & 0xffff) << 32); |
| 816 | |
| 817 | return err_addr; |
| 818 | } |
| 819 | |
| 820 | static void brcmnand_set_cmd_addr(struct mtd_info *mtd, u64 addr) |
| 821 | { |
| 822 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 823 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 824 | struct brcmnand_controller *ctrl = host->ctrl; |
| 825 | |
| 826 | brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS, |
| 827 | (host->cs << 16) | ((addr >> 32) & 0xffff)); |
| 828 | (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS); |
| 829 | brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, |
| 830 | lower_32_bits(addr)); |
| 831 | (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS); |
| 832 | } |
| 833 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 834 | static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs, |
| 835 | enum brcmnand_cs_reg reg) |
| 836 | { |
| 837 | u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE]; |
| 838 | u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE]; |
| 839 | u8 cs_offs; |
| 840 | |
| 841 | if (cs == 0 && ctrl->cs0_offsets) |
| 842 | cs_offs = ctrl->cs0_offsets[reg]; |
| 843 | else |
| 844 | cs_offs = ctrl->cs_offsets[reg]; |
| 845 | |
| 846 | if (cs && offs_cs1) |
| 847 | return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs; |
| 848 | |
| 849 | return offs_cs0 + cs * ctrl->reg_spacing + cs_offs; |
| 850 | } |
| 851 | |
| 852 | static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl) |
| 853 | { |
| 854 | if (ctrl->nand_version < 0x0600) |
| 855 | return 1; |
| 856 | return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT); |
| 857 | } |
| 858 | |
| 859 | static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val) |
| 860 | { |
| 861 | struct brcmnand_controller *ctrl = host->ctrl; |
| 862 | unsigned int shift = 0, bits; |
| 863 | enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD; |
| 864 | int cs = host->cs; |
| 865 | |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 866 | if (!ctrl->reg_offsets[reg]) |
| 867 | return; |
| 868 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 869 | if (ctrl->nand_version == 0x0702) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 870 | bits = 7; |
| 871 | else if (ctrl->nand_version >= 0x0600) |
| 872 | bits = 6; |
| 873 | else if (ctrl->nand_version >= 0x0500) |
| 874 | bits = 5; |
| 875 | else |
| 876 | bits = 4; |
| 877 | |
| 878 | if (ctrl->nand_version >= 0x0702) { |
| 879 | if (cs >= 4) |
| 880 | reg = BRCMNAND_CORR_THRESHOLD_EXT; |
| 881 | shift = (cs % 4) * bits; |
| 882 | } else if (ctrl->nand_version >= 0x0600) { |
| 883 | if (cs >= 5) |
| 884 | reg = BRCMNAND_CORR_THRESHOLD_EXT; |
| 885 | shift = (cs % 5) * bits; |
| 886 | } |
| 887 | brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val); |
| 888 | } |
| 889 | |
| 890 | static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl) |
| 891 | { |
| 892 | if (ctrl->nand_version < 0x0602) |
| 893 | return 24; |
| 894 | return 0; |
| 895 | } |
| 896 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 897 | static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl) |
| 898 | { |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 899 | if (ctrl->nand_version == 0x0702) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 900 | return GENMASK(7, 0); |
| 901 | else if (ctrl->nand_version >= 0x0600) |
| 902 | return GENMASK(6, 0); |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 903 | else if (ctrl->nand_version >= 0x0303) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 904 | return GENMASK(5, 0); |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 905 | else |
| 906 | return GENMASK(4, 0); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 909 | static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl) |
| 910 | { |
| 911 | u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f; |
| 912 | |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 913 | mask <<= ACC_CONTROL_ECC_SHIFT; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 914 | |
| 915 | /* v7.2 includes additional ECC levels */ |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 916 | if (ctrl->nand_version == 0x0702) |
| 917 | mask |= 0x7 << ACC_CONTROL_ECC_EXT_SHIFT; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 918 | |
| 919 | return mask; |
| 920 | } |
| 921 | |
| 922 | static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en) |
| 923 | { |
| 924 | struct brcmnand_controller *ctrl = host->ctrl; |
| 925 | u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL); |
| 926 | u32 acc_control = nand_readreg(ctrl, offs); |
| 927 | u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC; |
| 928 | |
| 929 | if (en) { |
| 930 | acc_control |= ecc_flags; /* enable RD/WR ECC */ |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 931 | acc_control &= ~brcmnand_ecc_level_mask(ctrl); |
| 932 | acc_control |= host->hwcfg.ecc_level << ctrl->ecc_level_shift; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 933 | } else { |
| 934 | acc_control &= ~ecc_flags; /* disable RD/WR ECC */ |
| 935 | acc_control &= ~brcmnand_ecc_level_mask(ctrl); |
| 936 | } |
| 937 | |
| 938 | nand_writereg(ctrl, offs, acc_control); |
| 939 | } |
| 940 | |
| 941 | static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl) |
| 942 | { |
| 943 | if (ctrl->nand_version >= 0x0702) |
| 944 | return 9; |
| 945 | else if (ctrl->nand_version >= 0x0600) |
| 946 | return 7; |
| 947 | else if (ctrl->nand_version >= 0x0500) |
| 948 | return 6; |
| 949 | else |
| 950 | return -1; |
| 951 | } |
| 952 | |
| 953 | static int brcmnand_get_sector_size_1k(struct brcmnand_host *host) |
| 954 | { |
| 955 | struct brcmnand_controller *ctrl = host->ctrl; |
| 956 | int shift = brcmnand_sector_1k_shift(ctrl); |
| 957 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 958 | BRCMNAND_CS_ACC_CONTROL); |
| 959 | |
| 960 | if (shift < 0) |
| 961 | return 0; |
| 962 | |
| 963 | return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1; |
| 964 | } |
| 965 | |
| 966 | static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val) |
| 967 | { |
| 968 | struct brcmnand_controller *ctrl = host->ctrl; |
| 969 | int shift = brcmnand_sector_1k_shift(ctrl); |
| 970 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 971 | BRCMNAND_CS_ACC_CONTROL); |
| 972 | u32 tmp; |
| 973 | |
| 974 | if (shift < 0) |
| 975 | return; |
| 976 | |
| 977 | tmp = nand_readreg(ctrl, acc_control_offs); |
| 978 | tmp &= ~(1 << shift); |
| 979 | tmp |= (!!val) << shift; |
| 980 | nand_writereg(ctrl, acc_control_offs, tmp); |
| 981 | } |
| 982 | |
William Zhang | a5837bf | 2024-09-16 11:58:49 +0200 | [diff] [blame] | 983 | static int brcmnand_get_spare_size(struct brcmnand_host *host) |
| 984 | { |
| 985 | struct brcmnand_controller *ctrl = host->ctrl; |
| 986 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 987 | BRCMNAND_CS_ACC_CONTROL); |
| 988 | u32 acc = nand_readreg(ctrl, acc_control_offs); |
| 989 | |
| 990 | return (acc & brcmnand_spare_area_mask(ctrl)); |
| 991 | } |
| 992 | |
| 993 | static void brcmnand_get_ecc_settings(struct brcmnand_host *host, struct nand_chip *chip) |
| 994 | { |
| 995 | struct brcmnand_controller *ctrl = host->ctrl; |
| 996 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 997 | BRCMNAND_CS_ACC_CONTROL); |
| 998 | bool sector_size_1k = brcmnand_get_sector_size_1k(host); |
| 999 | int spare_area_size, ecc_level; |
| 1000 | u32 acc; |
| 1001 | |
| 1002 | spare_area_size = brcmnand_get_spare_size(host); |
| 1003 | acc = nand_readreg(ctrl, acc_control_offs); |
| 1004 | ecc_level = (acc & brcmnand_ecc_level_mask(ctrl)) >> ctrl->ecc_level_shift; |
| 1005 | if (sector_size_1k) |
| 1006 | chip->ecc.strength = ecc_level * 2; |
| 1007 | else if (spare_area_size == 16 && ecc_level == 15) |
| 1008 | chip->ecc.strength = 1; /* hamming */ |
| 1009 | else |
| 1010 | chip->ecc.strength = ecc_level; |
| 1011 | |
| 1012 | if (chip->ecc.size == 0) { |
| 1013 | if (sector_size_1k) |
| 1014 | chip->ecc.size = 1024; |
| 1015 | else |
| 1016 | chip->ecc.size = 512; |
| 1017 | } |
| 1018 | } |
| 1019 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1020 | /*********************************************************************** |
| 1021 | * CS_NAND_SELECT |
| 1022 | ***********************************************************************/ |
| 1023 | |
| 1024 | enum { |
| 1025 | CS_SELECT_NAND_WP = BIT(29), |
| 1026 | CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30), |
| 1027 | }; |
| 1028 | |
| 1029 | static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl, |
| 1030 | u32 mask, u32 expected_val, |
| 1031 | unsigned long timeout_ms) |
| 1032 | { |
| 1033 | #ifndef __UBOOT__ |
| 1034 | unsigned long limit; |
| 1035 | u32 val; |
| 1036 | |
| 1037 | if (!timeout_ms) |
| 1038 | timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS; |
| 1039 | |
| 1040 | limit = jiffies + msecs_to_jiffies(timeout_ms); |
| 1041 | do { |
| 1042 | val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS); |
| 1043 | if ((val & mask) == expected_val) |
| 1044 | return 0; |
| 1045 | |
| 1046 | cpu_relax(); |
| 1047 | } while (time_after(limit, jiffies)); |
| 1048 | #else |
| 1049 | unsigned long base, limit; |
| 1050 | u32 val; |
| 1051 | |
| 1052 | if (!timeout_ms) |
| 1053 | timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS; |
| 1054 | |
| 1055 | base = get_timer(0); |
| 1056 | limit = CONFIG_SYS_HZ * timeout_ms / 1000; |
| 1057 | do { |
| 1058 | val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS); |
| 1059 | if ((val & mask) == expected_val) |
| 1060 | return 0; |
| 1061 | |
| 1062 | cpu_relax(); |
| 1063 | } while (get_timer(base) < limit); |
| 1064 | #endif /* __UBOOT__ */ |
| 1065 | |
William Zhang | 080ac0f | 2024-09-16 11:58:44 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * do a final check after time out in case the CPU was busy and the driver |
| 1068 | * did not get enough time to perform the polling to avoid false alarms |
| 1069 | */ |
| 1070 | val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS); |
| 1071 | if ((val & mask) == expected_val) |
| 1072 | return 0; |
| 1073 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1074 | dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n", |
| 1075 | expected_val, val & mask); |
| 1076 | |
| 1077 | return -ETIMEDOUT; |
| 1078 | } |
| 1079 | |
| 1080 | static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en) |
| 1081 | { |
| 1082 | u32 val = en ? CS_SELECT_NAND_WP : 0; |
| 1083 | |
| 1084 | brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val); |
| 1085 | } |
| 1086 | |
| 1087 | /*********************************************************************** |
| 1088 | * Flash DMA |
| 1089 | ***********************************************************************/ |
| 1090 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1091 | static inline bool has_flash_dma(struct brcmnand_controller *ctrl) |
| 1092 | { |
| 1093 | return ctrl->flash_dma_base; |
| 1094 | } |
| 1095 | |
| 1096 | static inline bool flash_dma_buf_ok(const void *buf) |
| 1097 | { |
| 1098 | #ifndef __UBOOT__ |
| 1099 | return buf && !is_vmalloc_addr(buf) && |
| 1100 | likely(IS_ALIGNED((uintptr_t)buf, 4)); |
| 1101 | #else |
| 1102 | return buf && likely(IS_ALIGNED((uintptr_t)buf, 4)); |
| 1103 | #endif /* __UBOOT__ */ |
| 1104 | } |
| 1105 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 1106 | static inline void flash_dma_writel(struct brcmnand_controller *ctrl, |
| 1107 | enum flash_dma_reg dma_reg, u32 val) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1108 | { |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 1109 | u16 offs = ctrl->flash_dma_offsets[dma_reg]; |
| 1110 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1111 | brcmnand_writel(val, ctrl->flash_dma_base + offs); |
| 1112 | } |
| 1113 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 1114 | static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, |
| 1115 | enum flash_dma_reg dma_reg) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1116 | { |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 1117 | u16 offs = ctrl->flash_dma_offsets[dma_reg]; |
| 1118 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1119 | return brcmnand_readl(ctrl->flash_dma_base + offs); |
| 1120 | } |
| 1121 | |
| 1122 | /* Low-level operation types: command, address, write, or read */ |
| 1123 | enum brcmnand_llop_type { |
| 1124 | LL_OP_CMD, |
| 1125 | LL_OP_ADDR, |
| 1126 | LL_OP_WR, |
| 1127 | LL_OP_RD, |
| 1128 | }; |
| 1129 | |
| 1130 | /*********************************************************************** |
| 1131 | * Internal support functions |
| 1132 | ***********************************************************************/ |
| 1133 | |
| 1134 | static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl, |
| 1135 | struct brcmnand_cfg *cfg) |
| 1136 | { |
| 1137 | if (ctrl->nand_version <= 0x0701) |
| 1138 | return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 && |
| 1139 | cfg->ecc_level == 15; |
| 1140 | else |
| 1141 | return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 && |
| 1142 | cfg->ecc_level == 15) || |
| 1143 | (cfg->spare_area_size == 28 && cfg->ecc_level == 16)); |
| 1144 | } |
| 1145 | |
| 1146 | /* |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1147 | * Returns a nand_ecclayout strucutre for the given layout/configuration. |
| 1148 | * Returns NULL on failure. |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1149 | */ |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1150 | static struct nand_ecclayout *brcmnand_create_layout(int ecc_level, |
| 1151 | struct brcmnand_host *host) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1152 | { |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1153 | struct brcmnand_cfg *cfg = &host->hwcfg; |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1154 | int i, j; |
| 1155 | struct nand_ecclayout *layout; |
| 1156 | int req; |
| 1157 | int sectors; |
| 1158 | int sas; |
| 1159 | int idx1, idx2; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1160 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1161 | #ifndef __UBOOT__ |
| 1162 | layout = devm_kzalloc(&host->pdev->dev, sizeof(*layout), GFP_KERNEL); |
| 1163 | #else |
| 1164 | layout = devm_kzalloc(host->pdev, sizeof(*layout), GFP_KERNEL); |
| 1165 | #endif |
| 1166 | if (!layout) |
| 1167 | return NULL; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1168 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1169 | sectors = cfg->page_size / (512 << cfg->sector_size_1k); |
| 1170 | sas = cfg->spare_area_size << cfg->sector_size_1k; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1171 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1172 | /* Hamming */ |
| 1173 | if (is_hamming_ecc(host->ctrl, cfg)) { |
| 1174 | for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) { |
| 1175 | /* First sector of each page may have BBI */ |
| 1176 | if (i == 0) { |
| 1177 | layout->oobfree[idx2].offset = i * sas + 1; |
| 1178 | /* Small-page NAND use byte 6 for BBI */ |
| 1179 | if (cfg->page_size == 512) |
| 1180 | layout->oobfree[idx2].offset--; |
| 1181 | layout->oobfree[idx2].length = 5; |
| 1182 | } else { |
| 1183 | layout->oobfree[idx2].offset = i * sas; |
| 1184 | layout->oobfree[idx2].length = 6; |
| 1185 | } |
| 1186 | idx2++; |
| 1187 | layout->eccpos[idx1++] = i * sas + 6; |
| 1188 | layout->eccpos[idx1++] = i * sas + 7; |
| 1189 | layout->eccpos[idx1++] = i * sas + 8; |
| 1190 | layout->oobfree[idx2].offset = i * sas + 9; |
| 1191 | layout->oobfree[idx2].length = 7; |
| 1192 | idx2++; |
| 1193 | /* Leave zero-terminated entry for OOBFREE */ |
| 1194 | if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE || |
| 1195 | idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1) |
| 1196 | break; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1197 | } |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1198 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1199 | return layout; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1200 | } |
| 1201 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1202 | /* |
| 1203 | * CONTROLLER_VERSION: |
| 1204 | * < v5.0: ECC_REQ = ceil(BCH_T * 13/8) |
| 1205 | * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8) |
| 1206 | * But we will just be conservative. |
| 1207 | */ |
| 1208 | req = DIV_ROUND_UP(ecc_level * 14, 8); |
| 1209 | if (req >= sas) { |
Sean Anderson | 4b5aa00 | 2020-09-15 10:44:50 -0400 | [diff] [blame] | 1210 | dev_err(host->pdev, |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1211 | "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n", |
| 1212 | req, sas); |
| 1213 | return NULL; |
| 1214 | } |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1215 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1216 | layout->eccbytes = req * sectors; |
| 1217 | for (i = 0, idx1 = 0, idx2 = 0; i < sectors; i++) { |
| 1218 | for (j = sas - req; j < sas && idx1 < |
| 1219 | MTD_MAX_ECCPOS_ENTRIES_LARGE; j++, idx1++) |
| 1220 | layout->eccpos[idx1] = i * sas + j; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1221 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1222 | /* First sector of each page may have BBI */ |
| 1223 | if (i == 0) { |
| 1224 | if (cfg->page_size == 512 && (sas - req >= 6)) { |
| 1225 | /* Small-page NAND use byte 6 for BBI */ |
| 1226 | layout->oobfree[idx2].offset = 0; |
| 1227 | layout->oobfree[idx2].length = 5; |
| 1228 | idx2++; |
| 1229 | if (sas - req > 6) { |
| 1230 | layout->oobfree[idx2].offset = 6; |
| 1231 | layout->oobfree[idx2].length = |
| 1232 | sas - req - 6; |
| 1233 | idx2++; |
| 1234 | } |
| 1235 | } else if (sas > req + 1) { |
| 1236 | layout->oobfree[idx2].offset = i * sas + 1; |
| 1237 | layout->oobfree[idx2].length = sas - req - 1; |
| 1238 | idx2++; |
| 1239 | } |
| 1240 | } else if (sas > req) { |
| 1241 | layout->oobfree[idx2].offset = i * sas; |
| 1242 | layout->oobfree[idx2].length = sas - req; |
| 1243 | idx2++; |
| 1244 | } |
| 1245 | /* Leave zero-terminated entry for OOBFREE */ |
| 1246 | if (idx1 >= MTD_MAX_ECCPOS_ENTRIES_LARGE || |
| 1247 | idx2 >= MTD_MAX_OOBFREE_ENTRIES_LARGE - 1) |
| 1248 | break; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1251 | return layout; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1252 | } |
| 1253 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1254 | static struct nand_ecclayout *brcmstb_choose_ecc_layout( |
| 1255 | struct brcmnand_host *host) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1256 | { |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1257 | struct nand_ecclayout *layout; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1258 | struct brcmnand_cfg *p = &host->hwcfg; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1259 | unsigned int ecc_level = p->ecc_level; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (p->sector_size_1k) |
| 1262 | ecc_level <<= 1; |
| 1263 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1264 | layout = brcmnand_create_layout(ecc_level, host); |
| 1265 | if (!layout) { |
Sean Anderson | 4b5aa00 | 2020-09-15 10:44:50 -0400 | [diff] [blame] | 1266 | dev_err(host->pdev, |
| 1267 | "no proper ecc_layout for this NAND cfg\n"); |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1268 | return NULL; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 1271 | return layout; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | static void brcmnand_wp(struct mtd_info *mtd, int wp) |
| 1275 | { |
| 1276 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1277 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1278 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1279 | |
| 1280 | if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) { |
| 1281 | static int old_wp = -1; |
| 1282 | int ret; |
| 1283 | |
| 1284 | if (old_wp != wp) { |
| 1285 | dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off"); |
| 1286 | old_wp = wp; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * make sure ctrl/flash ready before and after |
| 1291 | * changing state of #WP pin |
| 1292 | */ |
| 1293 | ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY | |
| 1294 | NAND_STATUS_READY, |
| 1295 | NAND_CTRL_RDY | |
| 1296 | NAND_STATUS_READY, 0); |
| 1297 | if (ret) |
| 1298 | return; |
| 1299 | |
| 1300 | brcmnand_set_wp(ctrl, wp); |
| 1301 | nand_status_op(chip, NULL); |
| 1302 | /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */ |
| 1303 | ret = bcmnand_ctrl_poll_status(ctrl, |
| 1304 | NAND_CTRL_RDY | |
| 1305 | NAND_STATUS_READY | |
| 1306 | NAND_STATUS_WP, |
| 1307 | NAND_CTRL_RDY | |
| 1308 | NAND_STATUS_READY | |
| 1309 | (wp ? 0 : NAND_STATUS_WP), 0); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1310 | if (ret) |
Sean Anderson | 4b5aa00 | 2020-09-15 10:44:50 -0400 | [diff] [blame] | 1311 | dev_err(host->pdev, "nand #WP expected %s\n", |
| 1312 | wp ? "on" : "off"); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* Helper functions for reading and writing OOB registers */ |
| 1317 | static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs) |
| 1318 | { |
| 1319 | u16 offset0, offset10, reg_offs; |
| 1320 | |
| 1321 | offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE]; |
| 1322 | offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE]; |
| 1323 | |
| 1324 | if (offs >= ctrl->max_oob) |
| 1325 | return 0x77; |
| 1326 | |
| 1327 | if (offs >= 16 && offset10) |
| 1328 | reg_offs = offset10 + ((offs - 0x10) & ~0x03); |
| 1329 | else |
| 1330 | reg_offs = offset0 + (offs & ~0x03); |
| 1331 | |
| 1332 | return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3)); |
| 1333 | } |
| 1334 | |
| 1335 | static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs, |
| 1336 | u32 data) |
| 1337 | { |
| 1338 | u16 offset0, offset10, reg_offs; |
| 1339 | |
| 1340 | offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE]; |
| 1341 | offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE]; |
| 1342 | |
| 1343 | if (offs >= ctrl->max_oob) |
| 1344 | return; |
| 1345 | |
| 1346 | if (offs >= 16 && offset10) |
| 1347 | reg_offs = offset10 + ((offs - 0x10) & ~0x03); |
| 1348 | else |
| 1349 | reg_offs = offset0 + (offs & ~0x03); |
| 1350 | |
| 1351 | nand_writereg(ctrl, reg_offs, data); |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * read_oob_from_regs - read data from OOB registers |
| 1356 | * @ctrl: NAND controller |
| 1357 | * @i: sub-page sector index |
| 1358 | * @oob: buffer to read to |
| 1359 | * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE) |
| 1360 | * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal |
| 1361 | */ |
| 1362 | static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob, |
| 1363 | int sas, int sector_1k) |
| 1364 | { |
| 1365 | int tbytes = sas << sector_1k; |
| 1366 | int j; |
| 1367 | |
| 1368 | /* Adjust OOB values for 1K sector size */ |
| 1369 | if (sector_1k && (i & 0x01)) |
| 1370 | tbytes = max(0, tbytes - (int)ctrl->max_oob); |
| 1371 | tbytes = min_t(int, tbytes, ctrl->max_oob); |
| 1372 | |
| 1373 | for (j = 0; j < tbytes; j++) |
| 1374 | oob[j] = oob_reg_read(ctrl, j); |
| 1375 | return tbytes; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * write_oob_to_regs - write data to OOB registers |
| 1380 | * @i: sub-page sector index |
| 1381 | * @oob: buffer to write from |
| 1382 | * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE) |
| 1383 | * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal |
| 1384 | */ |
| 1385 | static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i, |
| 1386 | const u8 *oob, int sas, int sector_1k) |
| 1387 | { |
| 1388 | int tbytes = sas << sector_1k; |
William Zhang | a645493 | 2024-09-16 11:58:45 +0200 | [diff] [blame] | 1389 | int j, k = 0; |
| 1390 | u32 last = 0xffffffff; |
| 1391 | u8 *plast = (u8 *)&last; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1392 | |
| 1393 | /* Adjust OOB values for 1K sector size */ |
| 1394 | if (sector_1k && (i & 0x01)) |
| 1395 | tbytes = max(0, tbytes - (int)ctrl->max_oob); |
| 1396 | tbytes = min_t(int, tbytes, ctrl->max_oob); |
| 1397 | |
William Zhang | a645493 | 2024-09-16 11:58:45 +0200 | [diff] [blame] | 1398 | /* |
| 1399 | * tbytes may not be multiple of words. Make sure we don't read out of |
| 1400 | * the boundary and stop at last word. |
| 1401 | */ |
| 1402 | for (j = 0; (j + 3) < tbytes; j += 4) |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1403 | oob_reg_write(ctrl, j, |
| 1404 | (oob[j + 0] << 24) | |
| 1405 | (oob[j + 1] << 16) | |
| 1406 | (oob[j + 2] << 8) | |
| 1407 | (oob[j + 3] << 0)); |
William Zhang | a645493 | 2024-09-16 11:58:45 +0200 | [diff] [blame] | 1408 | |
| 1409 | /* handle the remaing bytes */ |
| 1410 | while (j < tbytes) |
| 1411 | plast[k++] = oob[j++]; |
| 1412 | |
| 1413 | if (tbytes & 0x3) |
| 1414 | oob_reg_write(ctrl, (tbytes & ~0x3), (__force u32)cpu_to_be32(last)); |
| 1415 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1416 | return tbytes; |
| 1417 | } |
| 1418 | |
| 1419 | #ifndef __UBOOT__ |
| 1420 | static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data) |
| 1421 | { |
| 1422 | struct brcmnand_controller *ctrl = data; |
| 1423 | |
| 1424 | /* Discard all NAND_CTLRDY interrupts during DMA */ |
| 1425 | if (ctrl->dma_pending) |
| 1426 | return IRQ_HANDLED; |
| 1427 | |
| 1428 | complete(&ctrl->done); |
| 1429 | return IRQ_HANDLED; |
| 1430 | } |
| 1431 | |
| 1432 | /* Handle SoC-specific interrupt hardware */ |
| 1433 | static irqreturn_t brcmnand_irq(int irq, void *data) |
| 1434 | { |
| 1435 | struct brcmnand_controller *ctrl = data; |
| 1436 | |
| 1437 | if (ctrl->soc->ctlrdy_ack(ctrl->soc)) |
| 1438 | return brcmnand_ctlrdy_irq(irq, data); |
| 1439 | |
| 1440 | return IRQ_NONE; |
| 1441 | } |
| 1442 | |
| 1443 | static irqreturn_t brcmnand_dma_irq(int irq, void *data) |
| 1444 | { |
| 1445 | struct brcmnand_controller *ctrl = data; |
| 1446 | |
| 1447 | complete(&ctrl->dma_done); |
| 1448 | |
| 1449 | return IRQ_HANDLED; |
| 1450 | } |
| 1451 | #endif /* __UBOOT__ */ |
| 1452 | |
| 1453 | static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd) |
| 1454 | { |
| 1455 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1456 | int ret; |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1457 | u64 cmd_addr; |
| 1458 | |
| 1459 | cmd_addr = brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS); |
| 1460 | |
| 1461 | dev_dbg(ctrl->dev, "send native cmd %d addr 0x%llx\n", cmd, cmd_addr); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1462 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1463 | BUG_ON(ctrl->cmd_pending != 0); |
| 1464 | ctrl->cmd_pending = cmd; |
| 1465 | |
| 1466 | ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0); |
| 1467 | WARN_ON(ret); |
| 1468 | |
| 1469 | mb(); /* flush previous writes */ |
| 1470 | brcmnand_write_reg(ctrl, BRCMNAND_CMD_START, |
| 1471 | cmd << brcmnand_cmd_shift(ctrl)); |
| 1472 | } |
| 1473 | |
| 1474 | /*********************************************************************** |
| 1475 | * NAND MTD API: read/program/erase |
| 1476 | ***********************************************************************/ |
| 1477 | |
| 1478 | static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat, |
| 1479 | unsigned int ctrl) |
| 1480 | { |
| 1481 | /* intentionally left blank */ |
| 1482 | } |
| 1483 | |
| 1484 | static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this) |
| 1485 | { |
| 1486 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1487 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1488 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1489 | |
| 1490 | #ifndef __UBOOT__ |
| 1491 | unsigned long timeo = msecs_to_jiffies(100); |
| 1492 | |
| 1493 | dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending); |
| 1494 | if (ctrl->cmd_pending && |
| 1495 | wait_for_completion_timeout(&ctrl->done, timeo) <= 0) { |
| 1496 | u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START) |
| 1497 | >> brcmnand_cmd_shift(ctrl); |
| 1498 | |
| 1499 | dev_err_ratelimited(ctrl->dev, |
| 1500 | "timeout waiting for command %#02x\n", cmd); |
| 1501 | dev_err_ratelimited(ctrl->dev, "intfc status %08x\n", |
| 1502 | brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS)); |
| 1503 | } |
| 1504 | #else |
| 1505 | unsigned long timeo = 100; /* 100 msec */ |
| 1506 | int ret; |
| 1507 | |
| 1508 | dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending); |
| 1509 | |
| 1510 | ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, timeo); |
| 1511 | WARN_ON(ret); |
| 1512 | #endif /* __UBOOT__ */ |
| 1513 | |
| 1514 | ctrl->cmd_pending = 0; |
| 1515 | return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) & |
| 1516 | INTFC_FLASH_STATUS; |
| 1517 | } |
| 1518 | |
| 1519 | enum { |
| 1520 | LLOP_RE = BIT(16), |
| 1521 | LLOP_WE = BIT(17), |
| 1522 | LLOP_ALE = BIT(18), |
| 1523 | LLOP_CLE = BIT(19), |
| 1524 | LLOP_RETURN_IDLE = BIT(31), |
| 1525 | |
| 1526 | LLOP_DATA_MASK = GENMASK(15, 0), |
| 1527 | }; |
| 1528 | |
| 1529 | static int brcmnand_low_level_op(struct brcmnand_host *host, |
| 1530 | enum brcmnand_llop_type type, u32 data, |
| 1531 | bool last_op) |
| 1532 | { |
| 1533 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
| 1534 | struct nand_chip *chip = &host->chip; |
| 1535 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1536 | u32 tmp; |
| 1537 | |
| 1538 | tmp = data & LLOP_DATA_MASK; |
| 1539 | switch (type) { |
| 1540 | case LL_OP_CMD: |
| 1541 | tmp |= LLOP_WE | LLOP_CLE; |
| 1542 | break; |
| 1543 | case LL_OP_ADDR: |
| 1544 | /* WE | ALE */ |
| 1545 | tmp |= LLOP_WE | LLOP_ALE; |
| 1546 | break; |
| 1547 | case LL_OP_WR: |
| 1548 | /* WE */ |
| 1549 | tmp |= LLOP_WE; |
| 1550 | break; |
| 1551 | case LL_OP_RD: |
| 1552 | /* RE */ |
| 1553 | tmp |= LLOP_RE; |
| 1554 | break; |
| 1555 | } |
| 1556 | if (last_op) |
| 1557 | /* RETURN_IDLE */ |
| 1558 | tmp |= LLOP_RETURN_IDLE; |
| 1559 | |
| 1560 | dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp); |
| 1561 | |
| 1562 | brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp); |
| 1563 | (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP); |
| 1564 | |
| 1565 | brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP); |
| 1566 | return brcmnand_waitfunc(mtd, chip); |
| 1567 | } |
| 1568 | |
| 1569 | static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command, |
| 1570 | int column, int page_addr) |
| 1571 | { |
| 1572 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1573 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1574 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1575 | u64 addr = (u64)page_addr << chip->page_shift; |
| 1576 | int native_cmd = 0; |
| 1577 | |
| 1578 | if (command == NAND_CMD_READID || command == NAND_CMD_PARAM || |
| 1579 | command == NAND_CMD_RNDOUT) |
| 1580 | addr = (u64)column; |
| 1581 | /* Avoid propagating a negative, don't-care address */ |
| 1582 | else if (page_addr < 0) |
| 1583 | addr = 0; |
| 1584 | |
| 1585 | dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command, |
| 1586 | (unsigned long long)addr); |
| 1587 | |
| 1588 | host->last_cmd = command; |
| 1589 | host->last_byte = 0; |
| 1590 | host->last_addr = addr; |
| 1591 | |
| 1592 | switch (command) { |
| 1593 | case NAND_CMD_RESET: |
| 1594 | native_cmd = CMD_FLASH_RESET; |
| 1595 | break; |
| 1596 | case NAND_CMD_STATUS: |
| 1597 | native_cmd = CMD_STATUS_READ; |
| 1598 | break; |
| 1599 | case NAND_CMD_READID: |
| 1600 | native_cmd = CMD_DEVICE_ID_READ; |
| 1601 | break; |
| 1602 | case NAND_CMD_READOOB: |
| 1603 | native_cmd = CMD_SPARE_AREA_READ; |
| 1604 | break; |
| 1605 | case NAND_CMD_ERASE1: |
| 1606 | native_cmd = CMD_BLOCK_ERASE; |
| 1607 | brcmnand_wp(mtd, 0); |
| 1608 | break; |
| 1609 | case NAND_CMD_PARAM: |
| 1610 | native_cmd = CMD_PARAMETER_READ; |
| 1611 | break; |
| 1612 | case NAND_CMD_SET_FEATURES: |
| 1613 | case NAND_CMD_GET_FEATURES: |
| 1614 | brcmnand_low_level_op(host, LL_OP_CMD, command, false); |
| 1615 | brcmnand_low_level_op(host, LL_OP_ADDR, column, false); |
| 1616 | break; |
| 1617 | case NAND_CMD_RNDOUT: |
| 1618 | native_cmd = CMD_PARAMETER_CHANGE_COL; |
| 1619 | addr &= ~((u64)(FC_BYTES - 1)); |
| 1620 | /* |
| 1621 | * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0 |
| 1622 | * NB: hwcfg.sector_size_1k may not be initialized yet |
| 1623 | */ |
| 1624 | if (brcmnand_get_sector_size_1k(host)) { |
| 1625 | host->hwcfg.sector_size_1k = |
| 1626 | brcmnand_get_sector_size_1k(host); |
| 1627 | brcmnand_set_sector_size_1k(host, 0); |
| 1628 | } |
| 1629 | break; |
| 1630 | } |
| 1631 | |
| 1632 | if (!native_cmd) |
| 1633 | return; |
| 1634 | |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1635 | brcmnand_set_cmd_addr(mtd, addr); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1636 | brcmnand_send_cmd(host, native_cmd); |
| 1637 | brcmnand_waitfunc(mtd, chip); |
| 1638 | |
| 1639 | if (native_cmd == CMD_PARAMETER_READ || |
| 1640 | native_cmd == CMD_PARAMETER_CHANGE_COL) { |
| 1641 | /* Copy flash cache word-wise */ |
| 1642 | u32 *flash_cache = (u32 *)ctrl->flash_cache; |
| 1643 | int i; |
| 1644 | |
| 1645 | brcmnand_soc_data_bus_prepare(ctrl->soc, true); |
| 1646 | |
| 1647 | /* |
| 1648 | * Must cache the FLASH_CACHE now, since changes in |
| 1649 | * SECTOR_SIZE_1K may invalidate it |
| 1650 | */ |
Philippe Reynes | 7f28cf6 | 2019-03-15 15:14:37 +0100 | [diff] [blame] | 1651 | for (i = 0; i < FC_WORDS; i++) { |
| 1652 | u32 fc; |
| 1653 | |
| 1654 | fc = brcmnand_read_fc(ctrl, i); |
| 1655 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1656 | /* |
| 1657 | * Flash cache is big endian for parameter pages, at |
| 1658 | * least on STB SoCs |
| 1659 | */ |
Philippe Reynes | 7f28cf6 | 2019-03-15 15:14:37 +0100 | [diff] [blame] | 1660 | if (ctrl->parameter_page_big_endian) |
| 1661 | flash_cache[i] = be32_to_cpu(fc); |
| 1662 | else |
| 1663 | flash_cache[i] = le32_to_cpu(fc); |
| 1664 | } |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1665 | |
| 1666 | brcmnand_soc_data_bus_unprepare(ctrl->soc, true); |
| 1667 | |
| 1668 | /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */ |
| 1669 | if (host->hwcfg.sector_size_1k) |
| 1670 | brcmnand_set_sector_size_1k(host, |
| 1671 | host->hwcfg.sector_size_1k); |
| 1672 | } |
| 1673 | |
| 1674 | /* Re-enable protection is necessary only after erase */ |
| 1675 | if (command == NAND_CMD_ERASE1) |
| 1676 | brcmnand_wp(mtd, 1); |
| 1677 | } |
| 1678 | |
| 1679 | static uint8_t brcmnand_read_byte(struct mtd_info *mtd) |
| 1680 | { |
| 1681 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1682 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1683 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1684 | uint8_t ret = 0; |
| 1685 | int addr, offs; |
| 1686 | |
| 1687 | switch (host->last_cmd) { |
| 1688 | case NAND_CMD_READID: |
| 1689 | if (host->last_byte < 4) |
| 1690 | ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >> |
| 1691 | (24 - (host->last_byte << 3)); |
| 1692 | else if (host->last_byte < 8) |
| 1693 | ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >> |
| 1694 | (56 - (host->last_byte << 3)); |
| 1695 | break; |
| 1696 | |
| 1697 | case NAND_CMD_READOOB: |
| 1698 | ret = oob_reg_read(ctrl, host->last_byte); |
| 1699 | break; |
| 1700 | |
| 1701 | case NAND_CMD_STATUS: |
| 1702 | ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) & |
| 1703 | INTFC_FLASH_STATUS; |
| 1704 | if (wp_on) /* hide WP status */ |
| 1705 | ret |= NAND_STATUS_WP; |
| 1706 | break; |
| 1707 | |
| 1708 | case NAND_CMD_PARAM: |
| 1709 | case NAND_CMD_RNDOUT: |
| 1710 | addr = host->last_addr + host->last_byte; |
| 1711 | offs = addr & (FC_BYTES - 1); |
| 1712 | |
| 1713 | /* At FC_BYTES boundary, switch to next column */ |
| 1714 | if (host->last_byte > 0 && offs == 0) |
| 1715 | nand_change_read_column_op(chip, addr, NULL, 0, false); |
| 1716 | |
| 1717 | ret = ctrl->flash_cache[offs]; |
| 1718 | break; |
| 1719 | case NAND_CMD_GET_FEATURES: |
| 1720 | if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) { |
| 1721 | ret = 0; |
| 1722 | } else { |
| 1723 | bool last = host->last_byte == |
| 1724 | ONFI_SUBFEATURE_PARAM_LEN - 1; |
| 1725 | brcmnand_low_level_op(host, LL_OP_RD, 0, last); |
| 1726 | ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret); |
| 1731 | host->last_byte++; |
| 1732 | |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
| 1736 | static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 1737 | { |
| 1738 | int i; |
| 1739 | |
| 1740 | for (i = 0; i < len; i++, buf++) |
| 1741 | *buf = brcmnand_read_byte(mtd); |
| 1742 | } |
| 1743 | |
| 1744 | static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 1745 | int len) |
| 1746 | { |
| 1747 | int i; |
| 1748 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1749 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1750 | |
| 1751 | switch (host->last_cmd) { |
| 1752 | case NAND_CMD_SET_FEATURES: |
| 1753 | for (i = 0; i < len; i++) |
| 1754 | brcmnand_low_level_op(host, LL_OP_WR, buf[i], |
| 1755 | (i + 1) == len); |
| 1756 | break; |
| 1757 | default: |
| 1758 | BUG(); |
| 1759 | break; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * Construct a FLASH_DMA descriptor as part of a linked list. You must know the |
| 1765 | * following ahead of time: |
| 1766 | * - Is this descriptor the beginning or end of a linked list? |
| 1767 | * - What is the (DMA) address of the next descriptor in the linked list? |
| 1768 | */ |
| 1769 | #ifndef __UBOOT__ |
| 1770 | static int brcmnand_fill_dma_desc(struct brcmnand_host *host, |
| 1771 | struct brcm_nand_dma_desc *desc, u64 addr, |
| 1772 | dma_addr_t buf, u32 len, u8 dma_cmd, |
| 1773 | bool begin, bool end, |
| 1774 | dma_addr_t next_desc) |
| 1775 | { |
| 1776 | memset(desc, 0, sizeof(*desc)); |
| 1777 | /* Descriptors are written in native byte order (wordwise) */ |
| 1778 | desc->next_desc = lower_32_bits(next_desc); |
| 1779 | desc->next_desc_ext = upper_32_bits(next_desc); |
| 1780 | desc->cmd_irq = (dma_cmd << 24) | |
| 1781 | (end ? (0x03 << 8) : 0) | /* IRQ | STOP */ |
| 1782 | (!!begin) | ((!!end) << 1); /* head, tail */ |
Jiaxun Yang | 0803a27 | 2024-07-17 16:07:03 +0800 | [diff] [blame] | 1783 | #ifdef CONFIG_SYS_BIG_ENDIAN |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1784 | desc->cmd_irq |= 0x01 << 12; |
| 1785 | #endif |
| 1786 | desc->dram_addr = lower_32_bits(buf); |
| 1787 | desc->dram_addr_ext = upper_32_bits(buf); |
| 1788 | desc->tfr_len = len; |
| 1789 | desc->total_len = len; |
| 1790 | desc->flash_addr = lower_32_bits(addr); |
| 1791 | desc->flash_addr_ext = upper_32_bits(addr); |
| 1792 | desc->cs = host->cs; |
| 1793 | desc->status_valid = 0x01; |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * Kick the FLASH_DMA engine, with a given DMA descriptor |
| 1799 | */ |
| 1800 | static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc) |
| 1801 | { |
| 1802 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1803 | unsigned long timeo = msecs_to_jiffies(100); |
| 1804 | |
| 1805 | flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc)); |
| 1806 | (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC); |
Kamal Dasu | b6233f7 | 2023-02-11 16:29:03 +0100 | [diff] [blame] | 1807 | if (ctrl->nand_version > 0x0602) { |
| 1808 | flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, |
| 1809 | upper_32_bits(desc)); |
| 1810 | (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT); |
| 1811 | } |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* Start FLASH_DMA engine */ |
| 1814 | ctrl->dma_pending = true; |
| 1815 | mb(); /* flush previous writes */ |
| 1816 | flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */ |
| 1817 | |
| 1818 | if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) { |
| 1819 | dev_err(ctrl->dev, |
| 1820 | "timeout waiting for DMA; status %#x, error status %#x\n", |
| 1821 | flash_dma_readl(ctrl, FLASH_DMA_STATUS), |
| 1822 | flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS)); |
| 1823 | } |
| 1824 | ctrl->dma_pending = false; |
| 1825 | flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */ |
| 1826 | } |
| 1827 | |
| 1828 | static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf, |
| 1829 | u32 len, u8 dma_cmd) |
| 1830 | { |
| 1831 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1832 | dma_addr_t buf_pa; |
| 1833 | int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1834 | |
| 1835 | buf_pa = dma_map_single(ctrl->dev, buf, len, dir); |
| 1836 | if (dma_mapping_error(ctrl->dev, buf_pa)) { |
| 1837 | dev_err(ctrl->dev, "unable to map buffer for DMA\n"); |
| 1838 | return -ENOMEM; |
| 1839 | } |
| 1840 | |
| 1841 | brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len, |
| 1842 | dma_cmd, true, true, 0); |
| 1843 | |
| 1844 | brcmnand_dma_run(host, ctrl->dma_pa); |
| 1845 | |
| 1846 | dma_unmap_single(ctrl->dev, buf_pa, len, dir); |
| 1847 | |
| 1848 | if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR) |
| 1849 | return -EBADMSG; |
| 1850 | else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR) |
| 1851 | return -EUCLEAN; |
| 1852 | |
| 1853 | return 0; |
| 1854 | } |
| 1855 | #endif /* __UBOOT__ */ |
| 1856 | |
| 1857 | /* |
| 1858 | * Assumes proper CS is already set |
| 1859 | */ |
| 1860 | static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip, |
| 1861 | u64 addr, unsigned int trans, u32 *buf, |
| 1862 | u8 *oob, u64 *err_addr) |
| 1863 | { |
| 1864 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1865 | struct brcmnand_controller *ctrl = host->ctrl; |
Linus Walleij | d2ad157 | 2024-09-16 11:58:47 +0200 | [diff] [blame] | 1866 | int i, ret = 0; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1867 | |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1868 | brcmnand_clear_ecc_addr(ctrl); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1869 | |
| 1870 | for (i = 0; i < trans; i++, addr += FC_BYTES) { |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1871 | brcmnand_set_cmd_addr(mtd, addr); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1872 | /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */ |
| 1873 | brcmnand_send_cmd(host, CMD_PAGE_READ); |
| 1874 | brcmnand_waitfunc(mtd, chip); |
| 1875 | |
| 1876 | if (likely(buf)) { |
| 1877 | brcmnand_soc_data_bus_prepare(ctrl->soc, false); |
| 1878 | |
Linus Walleij | d2ad157 | 2024-09-16 11:58:47 +0200 | [diff] [blame] | 1879 | brcmnand_read_data_bus(ctrl, ctrl->nand_fc, buf, FC_WORDS); |
| 1880 | buf += FC_WORDS; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1881 | |
| 1882 | brcmnand_soc_data_bus_unprepare(ctrl->soc, false); |
| 1883 | } |
| 1884 | |
| 1885 | if (oob) |
| 1886 | oob += read_oob_from_regs(ctrl, i, oob, |
| 1887 | mtd->oobsize / trans, |
| 1888 | host->hwcfg.sector_size_1k); |
| 1889 | |
Joel Peshkin | 8384fd8 | 2021-12-20 20:15:47 -0800 | [diff] [blame] | 1890 | if (ret != -EBADMSG) { |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1891 | *err_addr = brcmnand_get_uncorrecc_addr(ctrl); |
| 1892 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1893 | if (*err_addr) |
| 1894 | ret = -EBADMSG; |
| 1895 | } |
| 1896 | |
| 1897 | if (!ret) { |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1898 | *err_addr = brcmnand_get_correcc_addr(ctrl); |
| 1899 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1900 | if (*err_addr) |
| 1901 | ret = -EUCLEAN; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | return ret; |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC |
| 1910 | * error |
| 1911 | * |
| 1912 | * Because the HW ECC signals an ECC error if an erase paged has even a single |
| 1913 | * bitflip, we must check each ECC error to see if it is actually an erased |
| 1914 | * page with bitflips, not a truly corrupted page. |
| 1915 | * |
| 1916 | * On a real error, return a negative error code (-EBADMSG for ECC error), and |
| 1917 | * buf will contain raw data. |
| 1918 | * Otherwise, buf gets filled with 0xffs and return the maximum number of |
| 1919 | * bitflips-per-ECC-sector to the caller. |
| 1920 | * |
| 1921 | */ |
| 1922 | static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd, |
| 1923 | struct nand_chip *chip, void *buf, u64 addr) |
| 1924 | { |
Álvaro Fernández Rojas | 210751f | 2023-02-11 16:29:04 +0100 | [diff] [blame] | 1925 | struct mtd_oob_region ecc; |
| 1926 | int i; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1927 | int bitflips = 0; |
| 1928 | int page = addr >> chip->page_shift; |
| 1929 | int ret; |
Álvaro Fernández Rojas | 210751f | 2023-02-11 16:29:04 +0100 | [diff] [blame] | 1930 | void *ecc_bytes; |
Claire Lin | 2bac579 | 2023-02-11 16:29:02 +0100 | [diff] [blame] | 1931 | void *ecc_chunk; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1932 | |
| 1933 | if (!buf) { |
| 1934 | #ifndef __UBOOT__ |
| 1935 | buf = chip->data_buf; |
| 1936 | #else |
| 1937 | buf = chip->buffers->databuf; |
| 1938 | #endif |
| 1939 | /* Invalidate page cache */ |
| 1940 | chip->pagebuf = -1; |
| 1941 | } |
| 1942 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1943 | /* read without ecc for verification */ |
| 1944 | ret = chip->ecc.read_page_raw(mtd, chip, buf, true, page); |
| 1945 | if (ret) |
| 1946 | return ret; |
| 1947 | |
Álvaro Fernández Rojas | 210751f | 2023-02-11 16:29:04 +0100 | [diff] [blame] | 1948 | for (i = 0; i < chip->ecc.steps; i++) { |
Claire Lin | 2bac579 | 2023-02-11 16:29:02 +0100 | [diff] [blame] | 1949 | ecc_chunk = buf + chip->ecc.size * i; |
Álvaro Fernández Rojas | 210751f | 2023-02-11 16:29:04 +0100 | [diff] [blame] | 1950 | |
| 1951 | mtd_ooblayout_ecc(mtd, i, &ecc); |
| 1952 | ecc_bytes = chip->oob_poi + ecc.offset; |
| 1953 | |
| 1954 | ret = nand_check_erased_ecc_chunk(ecc_chunk, chip->ecc.size, |
| 1955 | ecc_bytes, ecc.length, |
| 1956 | NULL, 0, |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1957 | chip->ecc.strength); |
| 1958 | if (ret < 0) |
| 1959 | return ret; |
| 1960 | |
| 1961 | bitflips = max(bitflips, ret); |
| 1962 | } |
| 1963 | |
| 1964 | return bitflips; |
| 1965 | } |
| 1966 | |
| 1967 | static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip, |
| 1968 | u64 addr, unsigned int trans, u32 *buf, u8 *oob) |
| 1969 | { |
| 1970 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 1971 | struct brcmnand_controller *ctrl = host->ctrl; |
| 1972 | u64 err_addr = 0; |
| 1973 | int err; |
| 1974 | bool retry = true; |
| 1975 | |
| 1976 | dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf); |
| 1977 | |
| 1978 | try_dmaread: |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 1979 | brcmnand_clear_ecc_addr(ctrl); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 1980 | |
| 1981 | #ifndef __UBOOT__ |
| 1982 | if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) { |
| 1983 | err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES, |
| 1984 | CMD_PAGE_READ); |
| 1985 | if (err) { |
| 1986 | if (mtd_is_bitflip_or_eccerr(err)) |
| 1987 | err_addr = addr; |
| 1988 | else |
| 1989 | return -EIO; |
| 1990 | } |
| 1991 | } else { |
| 1992 | if (oob) |
| 1993 | memset(oob, 0x99, mtd->oobsize); |
| 1994 | |
| 1995 | err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf, |
| 1996 | oob, &err_addr); |
| 1997 | } |
| 1998 | #else |
| 1999 | if (oob) |
| 2000 | memset(oob, 0x99, mtd->oobsize); |
| 2001 | |
| 2002 | err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf, |
| 2003 | oob, &err_addr); |
| 2004 | #endif /* __UBOOT__ */ |
| 2005 | |
| 2006 | if (mtd_is_eccerr(err)) { |
| 2007 | /* |
| 2008 | * On controller version and 7.0, 7.1 , DMA read after a |
| 2009 | * prior PIO read that reported uncorrectable error, |
| 2010 | * the DMA engine captures this error following DMA read |
| 2011 | * cleared only on subsequent DMA read, so just retry once |
| 2012 | * to clear a possible false error reported for current DMA |
| 2013 | * read |
| 2014 | */ |
| 2015 | if ((ctrl->nand_version == 0x0700) || |
| 2016 | (ctrl->nand_version == 0x0701)) { |
| 2017 | if (retry) { |
| 2018 | retry = false; |
| 2019 | goto try_dmaread; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | /* |
| 2024 | * Controller version 7.2 has hw encoder to detect erased page |
| 2025 | * bitflips, apply sw verification for older controllers only |
| 2026 | */ |
| 2027 | if (ctrl->nand_version < 0x0702) { |
| 2028 | err = brcmstb_nand_verify_erased_page(mtd, chip, buf, |
| 2029 | addr); |
| 2030 | /* erased page bitflips corrected */ |
| 2031 | if (err >= 0) |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
| 2035 | dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n", |
| 2036 | (unsigned long long)err_addr); |
| 2037 | mtd->ecc_stats.failed++; |
| 2038 | /* NAND layer expects zero on ECC errors */ |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | if (mtd_is_bitflip(err)) { |
| 2043 | unsigned int corrected = brcmnand_count_corrected(ctrl); |
| 2044 | |
| 2045 | dev_dbg(ctrl->dev, "corrected error at 0x%llx\n", |
| 2046 | (unsigned long long)err_addr); |
| 2047 | mtd->ecc_stats.corrected += corrected; |
| 2048 | /* Always exceed the software-imposed threshold */ |
| 2049 | return max(mtd->bitflip_threshold, corrected); |
| 2050 | } |
| 2051 | |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 2056 | uint8_t *buf, int oob_required, int page) |
| 2057 | { |
| 2058 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2059 | u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL; |
| 2060 | |
| 2061 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 2062 | |
| 2063 | return brcmnand_read(mtd, chip, host->last_addr, |
| 2064 | mtd->writesize >> FC_SHIFT, (u32 *)buf, oob); |
| 2065 | } |
| 2066 | |
| 2067 | static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2068 | uint8_t *buf, int oob_required, int page) |
| 2069 | { |
| 2070 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2071 | u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL; |
| 2072 | int ret; |
| 2073 | |
| 2074 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 2075 | |
| 2076 | brcmnand_set_ecc_enabled(host, 0); |
| 2077 | ret = brcmnand_read(mtd, chip, host->last_addr, |
| 2078 | mtd->writesize >> FC_SHIFT, (u32 *)buf, oob); |
| 2079 | brcmnand_set_ecc_enabled(host, 1); |
| 2080 | return ret; |
| 2081 | } |
| 2082 | |
| 2083 | static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip, |
| 2084 | int page) |
| 2085 | { |
| 2086 | return brcmnand_read(mtd, chip, (u64)page << chip->page_shift, |
| 2087 | mtd->writesize >> FC_SHIFT, |
| 2088 | NULL, (u8 *)chip->oob_poi); |
| 2089 | } |
| 2090 | |
| 2091 | static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2092 | int page) |
| 2093 | { |
| 2094 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2095 | |
| 2096 | brcmnand_set_ecc_enabled(host, 0); |
| 2097 | brcmnand_read(mtd, chip, (u64)page << chip->page_shift, |
| 2098 | mtd->writesize >> FC_SHIFT, |
| 2099 | NULL, (u8 *)chip->oob_poi); |
| 2100 | brcmnand_set_ecc_enabled(host, 1); |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip, |
| 2105 | u64 addr, const u32 *buf, u8 *oob) |
| 2106 | { |
| 2107 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2108 | struct brcmnand_controller *ctrl = host->ctrl; |
| 2109 | unsigned int i, j, trans = mtd->writesize >> FC_SHIFT; |
| 2110 | int status, ret = 0; |
| 2111 | |
| 2112 | dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf); |
| 2113 | |
| 2114 | if (unlikely((unsigned long)buf & 0x03)) { |
| 2115 | dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf); |
| 2116 | buf = (u32 *)((unsigned long)buf & ~0x03); |
| 2117 | } |
| 2118 | |
| 2119 | brcmnand_wp(mtd, 0); |
| 2120 | |
| 2121 | for (i = 0; i < ctrl->max_oob; i += 4) |
| 2122 | oob_reg_write(ctrl, i, 0xffffffff); |
| 2123 | |
| 2124 | #ifndef __UBOOT__ |
| 2125 | if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) { |
| 2126 | if (brcmnand_dma_trans(host, addr, (u32 *)buf, |
| 2127 | mtd->writesize, CMD_PROGRAM_PAGE)) |
| 2128 | ret = -EIO; |
| 2129 | goto out; |
| 2130 | } |
| 2131 | #endif /* __UBOOT__ */ |
| 2132 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2133 | for (i = 0; i < trans; i++, addr += FC_BYTES) { |
| 2134 | /* full address MUST be set before populating FC */ |
Kamal Dasu | 299c683 | 2023-02-11 16:29:00 +0100 | [diff] [blame] | 2135 | brcmnand_set_cmd_addr(mtd, addr); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2136 | |
| 2137 | if (buf) { |
| 2138 | brcmnand_soc_data_bus_prepare(ctrl->soc, false); |
| 2139 | |
| 2140 | for (j = 0; j < FC_WORDS; j++, buf++) |
| 2141 | brcmnand_write_fc(ctrl, j, *buf); |
| 2142 | |
| 2143 | brcmnand_soc_data_bus_unprepare(ctrl->soc, false); |
| 2144 | } else if (oob) { |
| 2145 | for (j = 0; j < FC_WORDS; j++) |
| 2146 | brcmnand_write_fc(ctrl, j, 0xffffffff); |
| 2147 | } |
| 2148 | |
| 2149 | if (oob) { |
| 2150 | oob += write_oob_to_regs(ctrl, i, oob, |
| 2151 | mtd->oobsize / trans, |
| 2152 | host->hwcfg.sector_size_1k); |
| 2153 | } |
| 2154 | |
| 2155 | /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */ |
| 2156 | brcmnand_send_cmd(host, CMD_PROGRAM_PAGE); |
| 2157 | status = brcmnand_waitfunc(mtd, chip); |
| 2158 | |
| 2159 | if (status & NAND_STATUS_FAIL) { |
| 2160 | dev_info(ctrl->dev, "program failed at %llx\n", |
| 2161 | (unsigned long long)addr); |
| 2162 | ret = -EIO; |
| 2163 | goto out; |
| 2164 | } |
| 2165 | } |
| 2166 | out: |
| 2167 | brcmnand_wp(mtd, 1); |
| 2168 | return ret; |
| 2169 | } |
| 2170 | |
| 2171 | static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 2172 | const uint8_t *buf, int oob_required, int page) |
| 2173 | { |
| 2174 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2175 | void *oob = oob_required ? chip->oob_poi : NULL; |
| 2176 | |
| 2177 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 2178 | brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob); |
| 2179 | |
| 2180 | return nand_prog_page_end_op(chip); |
| 2181 | } |
| 2182 | |
| 2183 | static int brcmnand_write_page_raw(struct mtd_info *mtd, |
| 2184 | struct nand_chip *chip, const uint8_t *buf, |
| 2185 | int oob_required, int page) |
| 2186 | { |
| 2187 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2188 | void *oob = oob_required ? chip->oob_poi : NULL; |
| 2189 | |
| 2190 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 2191 | brcmnand_set_ecc_enabled(host, 0); |
| 2192 | brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob); |
| 2193 | brcmnand_set_ecc_enabled(host, 1); |
| 2194 | |
| 2195 | return nand_prog_page_end_op(chip); |
| 2196 | } |
| 2197 | |
| 2198 | static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, |
| 2199 | int page) |
| 2200 | { |
| 2201 | return brcmnand_write(mtd, chip, (u64)page << chip->page_shift, |
| 2202 | NULL, chip->oob_poi); |
| 2203 | } |
| 2204 | |
| 2205 | static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2206 | int page) |
| 2207 | { |
| 2208 | struct brcmnand_host *host = nand_get_controller_data(chip); |
| 2209 | int ret; |
| 2210 | |
| 2211 | brcmnand_set_ecc_enabled(host, 0); |
| 2212 | ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL, |
| 2213 | (u8 *)chip->oob_poi); |
| 2214 | brcmnand_set_ecc_enabled(host, 1); |
| 2215 | |
| 2216 | return ret; |
| 2217 | } |
| 2218 | |
| 2219 | /*********************************************************************** |
| 2220 | * Per-CS setup (1 NAND device) |
| 2221 | ***********************************************************************/ |
| 2222 | |
| 2223 | static int brcmnand_set_cfg(struct brcmnand_host *host, |
| 2224 | struct brcmnand_cfg *cfg) |
| 2225 | { |
| 2226 | struct brcmnand_controller *ctrl = host->ctrl; |
| 2227 | struct nand_chip *chip = &host->chip; |
| 2228 | u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG); |
| 2229 | u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 2230 | BRCMNAND_CS_CFG_EXT); |
| 2231 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 2232 | BRCMNAND_CS_ACC_CONTROL); |
| 2233 | u8 block_size = 0, page_size = 0, device_size = 0; |
| 2234 | u32 tmp; |
| 2235 | |
| 2236 | if (ctrl->block_sizes) { |
| 2237 | int i, found; |
| 2238 | |
| 2239 | for (i = 0, found = 0; ctrl->block_sizes[i]; i++) |
| 2240 | if (ctrl->block_sizes[i] * 1024 == cfg->block_size) { |
| 2241 | block_size = i; |
| 2242 | found = 1; |
| 2243 | } |
| 2244 | if (!found) { |
| 2245 | dev_warn(ctrl->dev, "invalid block size %u\n", |
| 2246 | cfg->block_size); |
| 2247 | return -EINVAL; |
| 2248 | } |
| 2249 | } else { |
| 2250 | block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE); |
| 2251 | } |
| 2252 | |
| 2253 | if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size && |
| 2254 | cfg->block_size > ctrl->max_block_size)) { |
| 2255 | dev_warn(ctrl->dev, "invalid block size %u\n", |
| 2256 | cfg->block_size); |
| 2257 | block_size = 0; |
| 2258 | } |
| 2259 | |
| 2260 | if (ctrl->page_sizes) { |
| 2261 | int i, found; |
| 2262 | |
| 2263 | for (i = 0, found = 0; ctrl->page_sizes[i]; i++) |
| 2264 | if (ctrl->page_sizes[i] == cfg->page_size) { |
| 2265 | page_size = i; |
| 2266 | found = 1; |
| 2267 | } |
| 2268 | if (!found) { |
| 2269 | dev_warn(ctrl->dev, "invalid page size %u\n", |
| 2270 | cfg->page_size); |
| 2271 | return -EINVAL; |
| 2272 | } |
| 2273 | } else { |
| 2274 | page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE); |
| 2275 | } |
| 2276 | |
| 2277 | if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size && |
| 2278 | cfg->page_size > ctrl->max_page_size)) { |
| 2279 | dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size); |
| 2280 | return -EINVAL; |
| 2281 | } |
| 2282 | |
| 2283 | if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) { |
| 2284 | dev_warn(ctrl->dev, "invalid device size 0x%llx\n", |
| 2285 | (unsigned long long)cfg->device_size); |
| 2286 | return -EINVAL; |
| 2287 | } |
| 2288 | device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE); |
| 2289 | |
| 2290 | tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) | |
| 2291 | (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) | |
| 2292 | (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) | |
| 2293 | (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) | |
| 2294 | (device_size << CFG_DEVICE_SIZE_SHIFT); |
| 2295 | if (cfg_offs == cfg_ext_offs) { |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 2296 | tmp |= (page_size << ctrl->page_size_shift) | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2297 | (block_size << CFG_BLK_SIZE_SHIFT); |
| 2298 | nand_writereg(ctrl, cfg_offs, tmp); |
| 2299 | } else { |
| 2300 | nand_writereg(ctrl, cfg_offs, tmp); |
| 2301 | tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) | |
| 2302 | (block_size << CFG_EXT_BLK_SIZE_SHIFT); |
| 2303 | nand_writereg(ctrl, cfg_ext_offs, tmp); |
| 2304 | } |
| 2305 | |
| 2306 | tmp = nand_readreg(ctrl, acc_control_offs); |
| 2307 | tmp &= ~brcmnand_ecc_level_mask(ctrl); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2308 | tmp &= ~brcmnand_spare_area_mask(ctrl); |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 2309 | if (ctrl->nand_version >= 0x0302) { |
William Zhang | 26f66e6 | 2024-09-16 11:58:43 +0200 | [diff] [blame] | 2310 | tmp |= cfg->ecc_level << ctrl->ecc_level_shift; |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 2311 | tmp |= cfg->spare_area_size; |
| 2312 | } |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2313 | nand_writereg(ctrl, acc_control_offs, tmp); |
| 2314 | |
| 2315 | brcmnand_set_sector_size_1k(host, cfg->sector_size_1k); |
| 2316 | |
| 2317 | /* threshold = ceil(BCH-level * 0.75) */ |
| 2318 | brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4)); |
| 2319 | |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | static void brcmnand_print_cfg(struct brcmnand_host *host, |
| 2324 | char *buf, struct brcmnand_cfg *cfg) |
| 2325 | { |
| 2326 | buf += sprintf(buf, |
| 2327 | "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit", |
| 2328 | (unsigned long long)cfg->device_size >> 20, |
| 2329 | cfg->block_size >> 10, |
| 2330 | cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size, |
| 2331 | cfg->page_size >= 1024 ? "KiB" : "B", |
| 2332 | cfg->spare_area_size, cfg->device_width); |
| 2333 | |
| 2334 | /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */ |
| 2335 | if (is_hamming_ecc(host->ctrl, cfg)) |
| 2336 | sprintf(buf, ", Hamming ECC"); |
| 2337 | else if (cfg->sector_size_1k) |
| 2338 | sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1); |
| 2339 | else |
| 2340 | sprintf(buf, ", BCH-%u", cfg->ecc_level); |
| 2341 | } |
| 2342 | |
| 2343 | /* |
| 2344 | * Minimum number of bytes to address a page. Calculated as: |
| 2345 | * roundup(log2(size / page-size) / 8) |
| 2346 | * |
| 2347 | * NB: the following does not "round up" for non-power-of-2 'size'; but this is |
| 2348 | * OK because many other things will break if 'size' is irregular... |
| 2349 | */ |
| 2350 | static inline int get_blk_adr_bytes(u64 size, u32 writesize) |
| 2351 | { |
| 2352 | return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3; |
| 2353 | } |
| 2354 | |
| 2355 | static int brcmnand_setup_dev(struct brcmnand_host *host) |
| 2356 | { |
| 2357 | struct mtd_info *mtd = nand_to_mtd(&host->chip); |
| 2358 | struct nand_chip *chip = &host->chip; |
William Zhang | 3d8ade4 | 2024-09-16 11:58:46 +0200 | [diff] [blame] | 2359 | struct nand_device *nanddev = mtd_to_nanddev(mtd); |
| 2360 | struct nand_memory_organization *memorg = nanddev_get_memorg(nanddev); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2361 | struct brcmnand_controller *ctrl = host->ctrl; |
| 2362 | struct brcmnand_cfg *cfg = &host->hwcfg; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2363 | u32 offs, tmp, oob_sector; |
William Zhang | a5837bf | 2024-09-16 11:58:49 +0200 | [diff] [blame] | 2364 | bool use_strap = false; |
| 2365 | char msg[128]; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2366 | int ret; |
| 2367 | |
| 2368 | memset(cfg, 0, sizeof(*cfg)); |
| 2369 | |
| 2370 | #ifndef __UBOOT__ |
William Zhang | a5837bf | 2024-09-16 11:58:49 +0200 | [diff] [blame] | 2371 | use_strap = of_property_read_bool(nand_get_flash_node(chip), |
| 2372 | "brcm,nand-ecc-use-strap"): |
| 2373 | #else |
| 2374 | use_strap = ofnode_read_bool(nand_get_flash_node(chip), |
| 2375 | "brcm,nand-ecc-use-strap"); |
| 2376 | #endif /* __UBOOT__ */ |
| 2377 | /* |
| 2378 | * Either nand-ecc-xxx or brcm,nand-ecc-use-strap can be set. Error out |
| 2379 | * if both exist. |
| 2380 | */ |
| 2381 | if (chip->ecc.strength && use_strap) { |
| 2382 | dev_err(ctrl->dev, |
| 2383 | "ECC strap and DT ECC configuration properties are mutually exclusive\n"); |
| 2384 | return -EINVAL; |
| 2385 | } |
| 2386 | |
| 2387 | if (use_strap) |
| 2388 | brcmnand_get_ecc_settings(host, chip); |
| 2389 | |
| 2390 | #ifndef __UBOOT__ |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2391 | ret = of_property_read_u32(nand_get_flash_node(chip), |
| 2392 | "brcm,nand-oob-sector-size", |
| 2393 | &oob_sector); |
| 2394 | #else |
| 2395 | ret = ofnode_read_u32(nand_get_flash_node(chip), |
| 2396 | "brcm,nand-oob-sector-size", |
| 2397 | &oob_sector); |
| 2398 | #endif /* __UBOOT__ */ |
William Zhang | a5837bf | 2024-09-16 11:58:49 +0200 | [diff] [blame] | 2399 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2400 | if (ret) { |
William Zhang | a5837bf | 2024-09-16 11:58:49 +0200 | [diff] [blame] | 2401 | if (use_strap) |
| 2402 | cfg->spare_area_size = brcmnand_get_spare_size(host); |
| 2403 | else |
| 2404 | /* Use detected size */ |
| 2405 | cfg->spare_area_size = mtd->oobsize / |
| 2406 | (mtd->writesize >> FC_SHIFT); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2407 | } else { |
| 2408 | cfg->spare_area_size = oob_sector; |
| 2409 | } |
| 2410 | if (cfg->spare_area_size > ctrl->max_oob) |
| 2411 | cfg->spare_area_size = ctrl->max_oob; |
| 2412 | /* |
William Zhang | 3d8ade4 | 2024-09-16 11:58:46 +0200 | [diff] [blame] | 2413 | * Set mtd and memorg oobsize to be consistent with controller's |
| 2414 | * spare_area_size, as the rest is inaccessible. |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2415 | */ |
| 2416 | mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT); |
William Zhang | 3d8ade4 | 2024-09-16 11:58:46 +0200 | [diff] [blame] | 2417 | memorg->oobsize = mtd->oobsize; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2418 | |
| 2419 | cfg->device_size = mtd->size; |
| 2420 | cfg->block_size = mtd->erasesize; |
| 2421 | cfg->page_size = mtd->writesize; |
| 2422 | cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8; |
| 2423 | cfg->col_adr_bytes = 2; |
| 2424 | cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize); |
| 2425 | |
| 2426 | if (chip->ecc.mode != NAND_ECC_HW) { |
| 2427 | dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n", |
| 2428 | chip->ecc.mode); |
| 2429 | return -EINVAL; |
| 2430 | } |
| 2431 | |
| 2432 | if (chip->ecc.algo == NAND_ECC_UNKNOWN) { |
| 2433 | if (chip->ecc.strength == 1 && chip->ecc.size == 512) |
| 2434 | /* Default to Hamming for 1-bit ECC, if unspecified */ |
| 2435 | chip->ecc.algo = NAND_ECC_HAMMING; |
| 2436 | else |
| 2437 | /* Otherwise, BCH */ |
| 2438 | chip->ecc.algo = NAND_ECC_BCH; |
| 2439 | } |
| 2440 | |
| 2441 | if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 || |
| 2442 | chip->ecc.size != 512)) { |
| 2443 | dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n", |
| 2444 | chip->ecc.strength, chip->ecc.size); |
| 2445 | return -EINVAL; |
| 2446 | } |
| 2447 | |
| 2448 | switch (chip->ecc.size) { |
| 2449 | case 512: |
| 2450 | if (chip->ecc.algo == NAND_ECC_HAMMING) |
| 2451 | cfg->ecc_level = 15; |
| 2452 | else |
| 2453 | cfg->ecc_level = chip->ecc.strength; |
| 2454 | cfg->sector_size_1k = 0; |
| 2455 | break; |
| 2456 | case 1024: |
| 2457 | if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) { |
| 2458 | dev_err(ctrl->dev, "1KB sectors not supported\n"); |
| 2459 | return -EINVAL; |
| 2460 | } |
| 2461 | if (chip->ecc.strength & 0x1) { |
| 2462 | dev_err(ctrl->dev, |
| 2463 | "odd ECC not supported with 1KB sectors\n"); |
| 2464 | return -EINVAL; |
| 2465 | } |
| 2466 | |
| 2467 | cfg->ecc_level = chip->ecc.strength >> 1; |
| 2468 | cfg->sector_size_1k = 1; |
| 2469 | break; |
| 2470 | default: |
| 2471 | dev_err(ctrl->dev, "unsupported ECC size: %d\n", |
| 2472 | chip->ecc.size); |
| 2473 | return -EINVAL; |
| 2474 | } |
| 2475 | |
| 2476 | cfg->ful_adr_bytes = cfg->blk_adr_bytes; |
| 2477 | if (mtd->writesize > 512) |
| 2478 | cfg->ful_adr_bytes += cfg->col_adr_bytes; |
| 2479 | else |
| 2480 | cfg->ful_adr_bytes += 1; |
| 2481 | |
| 2482 | ret = brcmnand_set_cfg(host, cfg); |
| 2483 | if (ret) |
| 2484 | return ret; |
| 2485 | |
| 2486 | brcmnand_set_ecc_enabled(host, 1); |
| 2487 | |
| 2488 | brcmnand_print_cfg(host, msg, cfg); |
| 2489 | dev_info(ctrl->dev, "detected %s\n", msg); |
| 2490 | |
| 2491 | /* Configure ACC_CONTROL */ |
| 2492 | offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL); |
| 2493 | tmp = nand_readreg(ctrl, offs); |
| 2494 | tmp &= ~ACC_CONTROL_PARTIAL_PAGE; |
| 2495 | tmp &= ~ACC_CONTROL_RD_ERASED; |
| 2496 | |
| 2497 | /* We need to turn on Read from erased paged protected by ECC */ |
| 2498 | if (ctrl->nand_version >= 0x0702) |
| 2499 | tmp |= ACC_CONTROL_RD_ERASED; |
| 2500 | tmp &= ~ACC_CONTROL_FAST_PGM_RDIN; |
| 2501 | if (ctrl->features & BRCMNAND_HAS_PREFETCH) |
| 2502 | tmp &= ~ACC_CONTROL_PREFETCH; |
| 2503 | |
| 2504 | nand_writereg(ctrl, offs, tmp); |
| 2505 | |
| 2506 | return 0; |
| 2507 | } |
| 2508 | |
| 2509 | #ifndef __UBOOT__ |
| 2510 | static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn) |
| 2511 | #else |
| 2512 | static int brcmnand_init_cs(struct brcmnand_host *host, ofnode dn) |
| 2513 | #endif |
| 2514 | { |
| 2515 | struct brcmnand_controller *ctrl = host->ctrl; |
| 2516 | #ifndef __UBOOT__ |
| 2517 | struct platform_device *pdev = host->pdev; |
| 2518 | #else |
| 2519 | struct udevice *pdev = host->pdev; |
| 2520 | #endif /* __UBOOT__ */ |
| 2521 | struct mtd_info *mtd; |
| 2522 | struct nand_chip *chip; |
| 2523 | int ret; |
| 2524 | u16 cfg_offs; |
| 2525 | |
| 2526 | #ifndef __UBOOT__ |
| 2527 | ret = of_property_read_u32(dn, "reg", &host->cs); |
| 2528 | #else |
| 2529 | ret = ofnode_read_s32(dn, "reg", &host->cs); |
| 2530 | #endif |
| 2531 | if (ret) { |
Sean Anderson | 4b5aa00 | 2020-09-15 10:44:50 -0400 | [diff] [blame] | 2532 | dev_err(pdev, "can't get chip-select\n"); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2533 | return -ENXIO; |
| 2534 | } |
| 2535 | |
| 2536 | mtd = nand_to_mtd(&host->chip); |
| 2537 | chip = &host->chip; |
| 2538 | |
| 2539 | nand_set_flash_node(chip, dn); |
| 2540 | nand_set_controller_data(chip, host); |
| 2541 | #ifndef __UBOOT__ |
| 2542 | mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d", |
| 2543 | host->cs); |
| 2544 | #else |
| 2545 | mtd->name = devm_kasprintf(pdev, GFP_KERNEL, "brcmnand.%d", |
| 2546 | host->cs); |
| 2547 | #endif /* __UBOOT__ */ |
| 2548 | if (!mtd->name) |
| 2549 | return -ENOMEM; |
| 2550 | |
| 2551 | mtd->owner = THIS_MODULE; |
| 2552 | #ifndef __UBOOT__ |
| 2553 | mtd->dev.parent = &pdev->dev; |
| 2554 | #else |
| 2555 | mtd->dev->parent = pdev; |
| 2556 | #endif /* __UBOOT__ */ |
| 2557 | |
| 2558 | chip->IO_ADDR_R = (void __iomem *)0xdeadbeef; |
| 2559 | chip->IO_ADDR_W = (void __iomem *)0xdeadbeef; |
| 2560 | |
| 2561 | chip->cmd_ctrl = brcmnand_cmd_ctrl; |
| 2562 | chip->cmdfunc = brcmnand_cmdfunc; |
| 2563 | chip->waitfunc = brcmnand_waitfunc; |
| 2564 | chip->read_byte = brcmnand_read_byte; |
| 2565 | chip->read_buf = brcmnand_read_buf; |
| 2566 | chip->write_buf = brcmnand_write_buf; |
| 2567 | |
| 2568 | chip->ecc.mode = NAND_ECC_HW; |
| 2569 | chip->ecc.read_page = brcmnand_read_page; |
| 2570 | chip->ecc.write_page = brcmnand_write_page; |
| 2571 | chip->ecc.read_page_raw = brcmnand_read_page_raw; |
| 2572 | chip->ecc.write_page_raw = brcmnand_write_page_raw; |
| 2573 | chip->ecc.write_oob_raw = brcmnand_write_oob_raw; |
| 2574 | chip->ecc.read_oob_raw = brcmnand_read_oob_raw; |
| 2575 | chip->ecc.read_oob = brcmnand_read_oob; |
| 2576 | chip->ecc.write_oob = brcmnand_write_oob; |
| 2577 | |
| 2578 | chip->controller = &ctrl->controller; |
| 2579 | |
| 2580 | /* |
| 2581 | * The bootloader might have configured 16bit mode but |
| 2582 | * NAND READID command only works in 8bit mode. We force |
| 2583 | * 8bit mode here to ensure that NAND READID commands works. |
| 2584 | */ |
| 2585 | cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG); |
| 2586 | nand_writereg(ctrl, cfg_offs, |
| 2587 | nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH); |
| 2588 | |
| 2589 | ret = nand_scan_ident(mtd, 1, NULL); |
| 2590 | if (ret) |
| 2591 | return ret; |
| 2592 | |
| 2593 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
| 2594 | /* |
| 2595 | * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA |
| 2596 | * to/from, and have nand_base pass us a bounce buffer instead, as |
| 2597 | * needed. |
| 2598 | */ |
| 2599 | chip->options |= NAND_USE_BOUNCE_BUFFER; |
| 2600 | |
| 2601 | if (chip->bbt_options & NAND_BBT_USE_FLASH) |
| 2602 | chip->bbt_options |= NAND_BBT_NO_OOB; |
| 2603 | |
| 2604 | if (brcmnand_setup_dev(host)) |
| 2605 | return -ENXIO; |
| 2606 | |
| 2607 | chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512; |
| 2608 | /* only use our internal HW threshold */ |
| 2609 | mtd->bitflip_threshold = 1; |
| 2610 | |
William Zhang | 1100e49 | 2019-09-04 10:51:13 -0700 | [diff] [blame] | 2611 | chip->ecc.layout = brcmstb_choose_ecc_layout(host); |
| 2612 | if (!chip->ecc.layout) |
| 2613 | return -ENXIO; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2614 | |
| 2615 | ret = nand_scan_tail(mtd); |
| 2616 | if (ret) |
| 2617 | return ret; |
| 2618 | |
| 2619 | #ifndef __UBOOT__ |
| 2620 | ret = mtd_device_register(mtd, NULL, 0); |
| 2621 | if (ret) |
| 2622 | nand_cleanup(chip); |
| 2623 | #else |
| 2624 | ret = nand_register(0, mtd); |
| 2625 | #endif /* __UBOOT__ */ |
| 2626 | |
Álvaro Fernández Rojas | b3d66d9 | 2023-02-11 16:29:09 +0100 | [diff] [blame] | 2627 | /* If OOB is written with ECC enabled it will cause ECC errors */ |
| 2628 | if (is_hamming_ecc(host->ctrl, &host->hwcfg)) { |
| 2629 | chip->ecc.write_oob = brcmnand_write_oob_raw; |
| 2630 | chip->ecc.read_oob = brcmnand_read_oob_raw; |
| 2631 | } |
| 2632 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2633 | return ret; |
| 2634 | } |
| 2635 | |
| 2636 | #ifndef __UBOOT__ |
| 2637 | static void brcmnand_save_restore_cs_config(struct brcmnand_host *host, |
| 2638 | int restore) |
| 2639 | { |
| 2640 | struct brcmnand_controller *ctrl = host->ctrl; |
| 2641 | u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG); |
| 2642 | u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 2643 | BRCMNAND_CS_CFG_EXT); |
| 2644 | u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs, |
| 2645 | BRCMNAND_CS_ACC_CONTROL); |
| 2646 | u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1); |
| 2647 | u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2); |
| 2648 | |
| 2649 | if (restore) { |
| 2650 | nand_writereg(ctrl, cfg_offs, host->hwcfg.config); |
| 2651 | if (cfg_offs != cfg_ext_offs) |
| 2652 | nand_writereg(ctrl, cfg_ext_offs, |
| 2653 | host->hwcfg.config_ext); |
| 2654 | nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control); |
| 2655 | nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1); |
| 2656 | nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2); |
| 2657 | } else { |
| 2658 | host->hwcfg.config = nand_readreg(ctrl, cfg_offs); |
| 2659 | if (cfg_offs != cfg_ext_offs) |
| 2660 | host->hwcfg.config_ext = |
| 2661 | nand_readreg(ctrl, cfg_ext_offs); |
| 2662 | host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs); |
| 2663 | host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs); |
| 2664 | host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs); |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | static int brcmnand_suspend(struct device *dev) |
| 2669 | { |
| 2670 | struct brcmnand_controller *ctrl = dev_get_drvdata(dev); |
| 2671 | struct brcmnand_host *host; |
| 2672 | |
| 2673 | list_for_each_entry(host, &ctrl->host_list, node) |
| 2674 | brcmnand_save_restore_cs_config(host, 0); |
| 2675 | |
| 2676 | ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT); |
| 2677 | ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR); |
| 2678 | ctrl->corr_stat_threshold = |
| 2679 | brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD); |
| 2680 | |
| 2681 | if (has_flash_dma(ctrl)) |
| 2682 | ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE); |
| 2683 | |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
| 2687 | static int brcmnand_resume(struct device *dev) |
| 2688 | { |
| 2689 | struct brcmnand_controller *ctrl = dev_get_drvdata(dev); |
| 2690 | struct brcmnand_host *host; |
| 2691 | |
| 2692 | if (has_flash_dma(ctrl)) { |
| 2693 | flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode); |
| 2694 | flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0); |
| 2695 | } |
| 2696 | |
| 2697 | brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select); |
| 2698 | brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor); |
| 2699 | brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD, |
| 2700 | ctrl->corr_stat_threshold); |
| 2701 | if (ctrl->soc) { |
| 2702 | /* Clear/re-enable interrupt */ |
| 2703 | ctrl->soc->ctlrdy_ack(ctrl->soc); |
| 2704 | ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true); |
| 2705 | } |
| 2706 | |
| 2707 | list_for_each_entry(host, &ctrl->host_list, node) { |
| 2708 | struct nand_chip *chip = &host->chip; |
| 2709 | |
| 2710 | brcmnand_save_restore_cs_config(host, 1); |
| 2711 | |
| 2712 | /* Reset the chip, required by some chips after power-up */ |
| 2713 | nand_reset_op(chip); |
| 2714 | } |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
| 2719 | const struct dev_pm_ops brcmnand_pm_ops = { |
| 2720 | .suspend = brcmnand_suspend, |
| 2721 | .resume = brcmnand_resume, |
| 2722 | }; |
| 2723 | EXPORT_SYMBOL_GPL(brcmnand_pm_ops); |
| 2724 | |
| 2725 | static const struct of_device_id brcmnand_of_match[] = { |
Álvaro Fernández Rojas | 738d736 | 2023-02-11 16:29:08 +0100 | [diff] [blame] | 2726 | { .compatible = "brcm,brcmnand-v2.1" }, |
| 2727 | { .compatible = "brcm,brcmnand-v2.2" }, |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2728 | { .compatible = "brcm,brcmnand-v4.0" }, |
| 2729 | { .compatible = "brcm,brcmnand-v5.0" }, |
| 2730 | { .compatible = "brcm,brcmnand-v6.0" }, |
| 2731 | { .compatible = "brcm,brcmnand-v6.1" }, |
| 2732 | { .compatible = "brcm,brcmnand-v6.2" }, |
| 2733 | { .compatible = "brcm,brcmnand-v7.0" }, |
| 2734 | { .compatible = "brcm,brcmnand-v7.1" }, |
| 2735 | { .compatible = "brcm,brcmnand-v7.2" }, |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 2736 | { .compatible = "brcm,brcmnand-v7.3" }, |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2737 | {}, |
| 2738 | }; |
| 2739 | MODULE_DEVICE_TABLE(of, brcmnand_of_match); |
| 2740 | #endif /* __UBOOT__ */ |
| 2741 | |
| 2742 | /*********************************************************************** |
| 2743 | * Platform driver setup (per controller) |
| 2744 | ***********************************************************************/ |
| 2745 | |
| 2746 | #ifndef __UBOOT__ |
| 2747 | int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc) |
| 2748 | #else |
| 2749 | int brcmnand_probe(struct udevice *dev, struct brcmnand_soc *soc) |
| 2750 | #endif /* __UBOOT__ */ |
| 2751 | { |
| 2752 | #ifndef __UBOOT__ |
| 2753 | struct device *dev = &pdev->dev; |
| 2754 | struct device_node *dn = dev->of_node, *child; |
| 2755 | #else |
| 2756 | ofnode child; |
| 2757 | struct udevice *pdev = dev; |
| 2758 | #endif /* __UBOOT__ */ |
| 2759 | struct brcmnand_controller *ctrl; |
| 2760 | #ifndef __UBOOT__ |
| 2761 | struct resource *res; |
| 2762 | #else |
| 2763 | struct resource res; |
| 2764 | #endif /* __UBOOT__ */ |
| 2765 | int ret; |
| 2766 | |
| 2767 | #ifndef __UBOOT__ |
| 2768 | /* We only support device-tree instantiation */ |
| 2769 | if (!dn) |
| 2770 | return -ENODEV; |
| 2771 | |
| 2772 | if (!of_match_node(brcmnand_of_match, dn)) |
| 2773 | return -ENODEV; |
| 2774 | #endif /* __UBOOT__ */ |
| 2775 | |
| 2776 | ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL); |
| 2777 | if (!ctrl) |
| 2778 | return -ENOMEM; |
| 2779 | |
| 2780 | #ifndef __UBOOT__ |
| 2781 | dev_set_drvdata(dev, ctrl); |
| 2782 | #else |
| 2783 | /* |
| 2784 | * in u-boot, the data for the driver is allocated before probing |
| 2785 | * so to keep the reference to ctrl, we store it in the variable soc |
| 2786 | */ |
| 2787 | soc->ctrl = ctrl; |
| 2788 | #endif /* __UBOOT__ */ |
| 2789 | ctrl->dev = dev; |
| 2790 | |
| 2791 | init_completion(&ctrl->done); |
| 2792 | init_completion(&ctrl->dma_done); |
| 2793 | nand_hw_control_init(&ctrl->controller); |
| 2794 | INIT_LIST_HEAD(&ctrl->host_list); |
| 2795 | |
Philippe Reynes | 7f28cf6 | 2019-03-15 15:14:37 +0100 | [diff] [blame] | 2796 | /* Is parameter page in big endian ? */ |
| 2797 | ctrl->parameter_page_big_endian = |
| 2798 | dev_read_u32_default(dev, "parameter-page-big-endian", 1); |
| 2799 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2800 | /* NAND register range */ |
| 2801 | #ifndef __UBOOT__ |
| 2802 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2803 | ctrl->nand_base = devm_ioremap_resource(dev, res); |
| 2804 | #else |
| 2805 | dev_read_resource(pdev, 0, &res); |
| 2806 | ctrl->nand_base = devm_ioremap(pdev, res.start, resource_size(&res)); |
| 2807 | #endif |
| 2808 | if (IS_ERR(ctrl->nand_base)) |
| 2809 | return PTR_ERR(ctrl->nand_base); |
| 2810 | |
| 2811 | /* Enable clock before using NAND registers */ |
| 2812 | ctrl->clk = devm_clk_get(dev, "nand"); |
| 2813 | if (!IS_ERR(ctrl->clk)) { |
| 2814 | ret = clk_prepare_enable(ctrl->clk); |
| 2815 | if (ret) |
| 2816 | return ret; |
| 2817 | } else { |
Simon Glass | 7ca47bc | 2021-01-24 14:32:41 -0700 | [diff] [blame] | 2818 | /* Ignore PTR_ERR(ctrl->clk) */ |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2819 | ctrl->clk = NULL; |
| 2820 | } |
| 2821 | |
| 2822 | /* Initialize NAND revision */ |
| 2823 | ret = brcmnand_revision_init(ctrl); |
| 2824 | if (ret) |
| 2825 | goto err; |
| 2826 | |
| 2827 | /* |
| 2828 | * Most chips have this cache at a fixed offset within 'nand' block. |
| 2829 | * Some must specify this region separately. |
| 2830 | */ |
| 2831 | #ifndef __UBOOT__ |
| 2832 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache"); |
| 2833 | if (res) { |
| 2834 | ctrl->nand_fc = devm_ioremap_resource(dev, res); |
| 2835 | if (IS_ERR(ctrl->nand_fc)) { |
| 2836 | ret = PTR_ERR(ctrl->nand_fc); |
| 2837 | goto err; |
| 2838 | } |
| 2839 | } else { |
| 2840 | ctrl->nand_fc = ctrl->nand_base + |
| 2841 | ctrl->reg_offsets[BRCMNAND_FC_BASE]; |
| 2842 | } |
| 2843 | #else |
| 2844 | if (!dev_read_resource_byname(pdev, "nand-cache", &res)) { |
| 2845 | ctrl->nand_fc = devm_ioremap(dev, res.start, |
| 2846 | resource_size(&res)); |
| 2847 | if (IS_ERR(ctrl->nand_fc)) { |
| 2848 | ret = PTR_ERR(ctrl->nand_fc); |
| 2849 | goto err; |
| 2850 | } |
| 2851 | } else { |
| 2852 | ctrl->nand_fc = ctrl->nand_base + |
| 2853 | ctrl->reg_offsets[BRCMNAND_FC_BASE]; |
| 2854 | } |
| 2855 | #endif |
| 2856 | |
| 2857 | #ifndef __UBOOT__ |
| 2858 | /* FLASH_DMA */ |
| 2859 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma"); |
| 2860 | if (res) { |
| 2861 | ctrl->flash_dma_base = devm_ioremap_resource(dev, res); |
| 2862 | if (IS_ERR(ctrl->flash_dma_base)) { |
| 2863 | ret = PTR_ERR(ctrl->flash_dma_base); |
| 2864 | goto err; |
| 2865 | } |
| 2866 | |
Kamal Dasu | f47b36b | 2023-02-11 16:29:01 +0100 | [diff] [blame] | 2867 | /* initialize the dma version */ |
| 2868 | brcmnand_flash_dma_revision_init(ctrl); |
| 2869 | |
| 2870 | /* linked-list and stop on error */ |
| 2871 | flash_dma_writel(ctrl, FLASH_DMA_MODE, FLASH_DMA_MODE_MASK); |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2872 | flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0); |
| 2873 | |
| 2874 | /* Allocate descriptor(s) */ |
| 2875 | ctrl->dma_desc = dmam_alloc_coherent(dev, |
| 2876 | sizeof(*ctrl->dma_desc), |
| 2877 | &ctrl->dma_pa, GFP_KERNEL); |
| 2878 | if (!ctrl->dma_desc) { |
| 2879 | ret = -ENOMEM; |
| 2880 | goto err; |
| 2881 | } |
| 2882 | |
| 2883 | ctrl->dma_irq = platform_get_irq(pdev, 1); |
| 2884 | if ((int)ctrl->dma_irq < 0) { |
| 2885 | dev_err(dev, "missing FLASH_DMA IRQ\n"); |
| 2886 | ret = -ENODEV; |
| 2887 | goto err; |
| 2888 | } |
| 2889 | |
| 2890 | ret = devm_request_irq(dev, ctrl->dma_irq, |
| 2891 | brcmnand_dma_irq, 0, DRV_NAME, |
| 2892 | ctrl); |
| 2893 | if (ret < 0) { |
| 2894 | dev_err(dev, "can't allocate IRQ %d: error %d\n", |
| 2895 | ctrl->dma_irq, ret); |
| 2896 | goto err; |
| 2897 | } |
| 2898 | |
| 2899 | dev_info(dev, "enabling FLASH_DMA\n"); |
| 2900 | } |
| 2901 | #endif /* __UBOOT__ */ |
| 2902 | |
| 2903 | /* Disable automatic device ID config, direct addressing */ |
| 2904 | brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, |
| 2905 | CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0); |
| 2906 | /* Disable XOR addressing */ |
| 2907 | brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0); |
| 2908 | |
William Zhang | 27cfe7d | 2024-09-16 11:58:48 +0200 | [diff] [blame] | 2909 | /* Check if the board connects the WP pin */ |
| 2910 | #ifndef __UBOOT__ |
| 2911 | if (of_property_read_bool(dn, "brcm,wp-not-connected")) |
| 2912 | #else |
| 2913 | if (dev_read_bool(ctrl->dev, "brcm,wp-not-connected")) |
| 2914 | #endif /* __UBOOT__ */ |
| 2915 | wp_on = 0; |
| 2916 | |
Philippe Reynes | 77669af | 2019-03-15 15:14:38 +0100 | [diff] [blame] | 2917 | /* Read the write-protect configuration in the device tree */ |
William Zhang | 27cfe7d | 2024-09-16 11:58:48 +0200 | [diff] [blame] | 2918 | if (dev_read_bool(ctrl->dev, "write-protect")) |
| 2919 | wp_on = dev_read_u32_default(dev, "write-protect", wp_on); |
Philippe Reynes | 77669af | 2019-03-15 15:14:38 +0100 | [diff] [blame] | 2920 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 2921 | if (ctrl->features & BRCMNAND_HAS_WP) { |
| 2922 | /* Permanently disable write protection */ |
| 2923 | if (wp_on == 2) |
| 2924 | brcmnand_set_wp(ctrl, false); |
| 2925 | } else { |
| 2926 | wp_on = 0; |
| 2927 | } |
| 2928 | |
| 2929 | #ifndef __UBOOT__ |
| 2930 | /* IRQ */ |
| 2931 | ctrl->irq = platform_get_irq(pdev, 0); |
| 2932 | if ((int)ctrl->irq < 0) { |
| 2933 | dev_err(dev, "no IRQ defined\n"); |
| 2934 | ret = -ENODEV; |
| 2935 | goto err; |
| 2936 | } |
| 2937 | |
| 2938 | /* |
| 2939 | * Some SoCs integrate this controller (e.g., its interrupt bits) in |
| 2940 | * interesting ways |
| 2941 | */ |
| 2942 | if (soc) { |
| 2943 | ctrl->soc = soc; |
| 2944 | |
| 2945 | ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0, |
| 2946 | DRV_NAME, ctrl); |
| 2947 | |
| 2948 | /* Enable interrupt */ |
| 2949 | ctrl->soc->ctlrdy_ack(ctrl->soc); |
| 2950 | ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true); |
| 2951 | } else { |
| 2952 | /* Use standard interrupt infrastructure */ |
| 2953 | ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0, |
| 2954 | DRV_NAME, ctrl); |
| 2955 | } |
| 2956 | if (ret < 0) { |
| 2957 | dev_err(dev, "can't allocate IRQ %d: error %d\n", |
| 2958 | ctrl->irq, ret); |
| 2959 | goto err; |
| 2960 | } |
| 2961 | #endif /* __UBOOT__ */ |
| 2962 | |
| 2963 | #ifndef __UBOOT__ |
| 2964 | for_each_available_child_of_node(dn, child) { |
| 2965 | if (of_device_is_compatible(child, "brcm,nandcs")) { |
| 2966 | struct brcmnand_host *host; |
| 2967 | |
| 2968 | host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); |
| 2969 | if (!host) { |
| 2970 | of_node_put(child); |
| 2971 | ret = -ENOMEM; |
| 2972 | goto err; |
| 2973 | } |
| 2974 | host->pdev = pdev; |
| 2975 | host->ctrl = ctrl; |
| 2976 | |
| 2977 | ret = brcmnand_init_cs(host, child); |
| 2978 | if (ret) { |
| 2979 | devm_kfree(dev, host); |
| 2980 | continue; /* Try all chip-selects */ |
| 2981 | } |
| 2982 | |
| 2983 | list_add_tail(&host->node, &ctrl->host_list); |
| 2984 | } |
| 2985 | } |
| 2986 | #else |
| 2987 | ofnode_for_each_subnode(child, dev_ofnode(dev)) { |
| 2988 | if (ofnode_device_is_compatible(child, "brcm,nandcs")) { |
| 2989 | struct brcmnand_host *host; |
| 2990 | |
| 2991 | host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); |
| 2992 | if (!host) { |
| 2993 | ret = -ENOMEM; |
| 2994 | goto err; |
| 2995 | } |
| 2996 | host->pdev = pdev; |
| 2997 | host->ctrl = ctrl; |
| 2998 | |
| 2999 | ret = brcmnand_init_cs(host, child); |
| 3000 | if (ret) { |
| 3001 | devm_kfree(dev, host); |
| 3002 | continue; /* Try all chip-selects */ |
| 3003 | } |
| 3004 | |
| 3005 | list_add_tail(&host->node, &ctrl->host_list); |
| 3006 | } |
| 3007 | } |
| 3008 | #endif /* __UBOOT__ */ |
| 3009 | |
Álvaro Fernández Rojas | d8ae875 | 2020-04-02 10:37:52 +0200 | [diff] [blame] | 3010 | /* No chip-selects could initialize properly */ |
| 3011 | if (list_empty(&ctrl->host_list)) { |
| 3012 | ret = -ENODEV; |
| 3013 | goto err; |
| 3014 | } |
| 3015 | |
| 3016 | return 0; |
| 3017 | |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 3018 | err: |
| 3019 | #ifndef __UBOOT__ |
| 3020 | clk_disable_unprepare(ctrl->clk); |
| 3021 | #else |
| 3022 | if (ctrl->clk) |
| 3023 | clk_disable(ctrl->clk); |
| 3024 | #endif /* __UBOOT__ */ |
| 3025 | return ret; |
Philippe Reynes | 5aa6cfb | 2019-03-15 15:14:36 +0100 | [diff] [blame] | 3026 | } |
| 3027 | EXPORT_SYMBOL_GPL(brcmnand_probe); |
| 3028 | |
| 3029 | #ifndef __UBOOT__ |
| 3030 | int brcmnand_remove(struct platform_device *pdev) |
| 3031 | { |
| 3032 | struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev); |
| 3033 | struct brcmnand_host *host; |
| 3034 | |
| 3035 | list_for_each_entry(host, &ctrl->host_list, node) |
| 3036 | nand_release(nand_to_mtd(&host->chip)); |
| 3037 | |
| 3038 | clk_disable_unprepare(ctrl->clk); |
| 3039 | |
| 3040 | dev_set_drvdata(&pdev->dev, NULL); |
| 3041 | |
| 3042 | return 0; |
| 3043 | } |
| 3044 | #else |
| 3045 | int brcmnand_remove(struct udevice *pdev) |
| 3046 | { |
| 3047 | return 0; |
| 3048 | } |
| 3049 | #endif /* __UBOOT__ */ |
| 3050 | EXPORT_SYMBOL_GPL(brcmnand_remove); |
| 3051 | |
| 3052 | MODULE_LICENSE("GPL v2"); |
| 3053 | MODULE_AUTHOR("Kevin Cernekee"); |
| 3054 | MODULE_AUTHOR("Brian Norris"); |
| 3055 | MODULE_DESCRIPTION("NAND driver for Broadcom chips"); |
| 3056 | MODULE_ALIAS("platform:brcmnand"); |