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