Yifeng Zhao | 9e9021e | 2021-06-07 16:40:29 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Rockchip NAND Flash controller driver. |
| 4 | * Copyright (C) 2021 Rockchip Inc. |
| 5 | * Author: Yifeng Zhao <yifeng.zhao@rock-chips.com> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/device_compat.h> |
| 13 | #include <dm/devres.h> |
| 14 | #include <fdtdec.h> |
| 15 | #include <inttypes.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/dma-direction.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mtd/mtd.h> |
| 22 | #include <linux/mtd/nand.h> |
| 23 | #include <linux/mtd/partitions.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 24 | #include <linux/mtd/rawnand.h> |
Yifeng Zhao | 9e9021e | 2021-06-07 16:40:29 +0800 | [diff] [blame] | 25 | #include <memalign.h> |
| 26 | #include <nand.h> |
| 27 | |
| 28 | /* |
| 29 | * NFC Page Data Layout: |
| 30 | * 1024 bytes data + 4Bytes sys data + 28Bytes~124Bytes ECC data + |
| 31 | * 1024 bytes data + 4Bytes sys data + 28Bytes~124Bytes ECC data + |
| 32 | * ...... |
| 33 | * NAND Page Data Layout: |
| 34 | * 1024 * n data + m Bytes oob |
| 35 | * Original Bad Block Mask Location: |
| 36 | * First byte of oob(spare). |
| 37 | * nand_chip->oob_poi data layout: |
| 38 | * 4Bytes sys data + .... + 4Bytes sys data + ECC data. |
| 39 | */ |
| 40 | |
| 41 | /* NAND controller register definition */ |
| 42 | #define NFC_READ (0) |
| 43 | #define NFC_WRITE (1) |
| 44 | |
| 45 | #define NFC_FMCTL (0x00) |
| 46 | #define FMCTL_CE_SEL_M 0xFF |
| 47 | #define FMCTL_CE_SEL(x) (1 << (x)) |
| 48 | #define FMCTL_WP BIT(8) |
| 49 | #define FMCTL_RDY BIT(9) |
| 50 | |
| 51 | #define NFC_FMWAIT (0x04) |
| 52 | #define FLCTL_RST BIT(0) |
| 53 | #define FLCTL_WR (1) /* 0: read, 1: write */ |
| 54 | #define FLCTL_XFER_ST BIT(2) |
| 55 | #define FLCTL_XFER_EN BIT(3) |
| 56 | #define FLCTL_ACORRECT BIT(10) /* Auto correct error bits. */ |
| 57 | #define FLCTL_XFER_READY BIT(20) |
| 58 | #define FLCTL_XFER_SECTOR (22) |
| 59 | #define FLCTL_TOG_FIX BIT(29) |
| 60 | |
| 61 | #define BCHCTL_BANK_M (7 << 5) |
| 62 | #define BCHCTL_BANK (5) |
| 63 | |
| 64 | #define DMA_ST BIT(0) |
| 65 | #define DMA_WR (1) /* 0: write, 1: read */ |
| 66 | #define DMA_EN BIT(2) |
| 67 | #define DMA_AHB_SIZE (3) /* 0: 1, 1: 2, 2: 4 */ |
| 68 | #define DMA_BURST_SIZE (6) /* 0: 1, 3: 4, 5: 8, 7: 16 */ |
| 69 | #define DMA_INC_NUM (9) /* 1 - 16 */ |
| 70 | |
| 71 | #define ECC_ERR_CNT(x, e) ((((x) >> (e).low) & (e).low_mask) |\ |
| 72 | (((x) >> (e).high) & (e).high_mask) << (e).low_bn) |
| 73 | #define INT_DMA BIT(0) |
| 74 | #define NFC_BANK (0x800) |
| 75 | #define NFC_BANK_STEP (0x100) |
| 76 | #define BANK_DATA (0x00) |
| 77 | #define BANK_ADDR (0x04) |
| 78 | #define BANK_CMD (0x08) |
| 79 | #define NFC_SRAM0 (0x1000) |
| 80 | #define NFC_SRAM1 (0x1400) |
| 81 | #define NFC_SRAM_SIZE (0x400) |
| 82 | #define NFC_TIMEOUT_MS (500) |
| 83 | #define NFC_MAX_OOB_PER_STEP 128 |
| 84 | #define NFC_MIN_OOB_PER_STEP 64 |
| 85 | #define MAX_DATA_SIZE 0xFFFC |
| 86 | #define MAX_ADDRESS_CYC 6 |
| 87 | #define NFC_ECC_MAX_MODES 4 |
| 88 | #define NFC_RB_DELAY_US 50 |
| 89 | #define NFC_MAX_PAGE_SIZE (16 * 1024) |
| 90 | #define NFC_MAX_OOB_SIZE (16 * 128) |
| 91 | #define NFC_MAX_NSELS (8) /* Some Socs only have 1 or 2 CSs. */ |
| 92 | #define NFC_SYS_DATA_SIZE (4) /* 4 bytes sys data in oob pre 1024 data.*/ |
| 93 | #define RK_DEFAULT_CLOCK_RATE (150 * 1000 * 1000) /* 150 Mhz */ |
| 94 | #define ACCTIMING(csrw, rwpw, rwcs) ((csrw) << 12 | (rwpw) << 5 | (rwcs)) |
| 95 | |
| 96 | enum nfc_type { |
| 97 | NFC_V6, |
| 98 | NFC_V8, |
| 99 | NFC_V9, |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * struct rk_ecc_cnt_status: represent a ecc status data. |
| 104 | * @err_flag_bit: error flag bit index at register. |
| 105 | * @low: ECC count low bit index at register. |
| 106 | * @low_mask: mask bit. |
| 107 | * @low_bn: ECC count low bit number. |
| 108 | * @high: ECC count high bit index at register. |
| 109 | * @high_mask: mask bit |
| 110 | */ |
| 111 | struct ecc_cnt_status { |
| 112 | u8 err_flag_bit; |
| 113 | u8 low; |
| 114 | u8 low_mask; |
| 115 | u8 low_bn; |
| 116 | u8 high; |
| 117 | u8 high_mask; |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * @type: NFC version |
| 122 | * @ecc_strengths: ECC strengths |
| 123 | * @ecc_cfgs: ECC config values |
| 124 | * @flctl_off: FLCTL register offset |
| 125 | * @bchctl_off: BCHCTL register offset |
| 126 | * @dma_data_buf_off: DMA_DATA_BUF register offset |
| 127 | * @dma_oob_buf_off: DMA_OOB_BUF register offset |
| 128 | * @dma_cfg_off: DMA_CFG register offset |
| 129 | * @dma_st_off: DMA_ST register offset |
| 130 | * @bch_st_off: BCG_ST register offset |
| 131 | * @randmz_off: RANDMZ register offset |
| 132 | * @int_en_off: interrupt enable register offset |
| 133 | * @int_clr_off: interrupt clean register offset |
| 134 | * @int_st_off: interrupt status register offset |
| 135 | * @oob0_off: oob0 register offset |
| 136 | * @oob1_off: oob1 register offset |
| 137 | * @ecc0: represent ECC0 status data |
| 138 | * @ecc1: represent ECC1 status data |
| 139 | */ |
| 140 | struct nfc_cfg { |
| 141 | enum nfc_type type; |
| 142 | u8 ecc_strengths[NFC_ECC_MAX_MODES]; |
| 143 | u32 ecc_cfgs[NFC_ECC_MAX_MODES]; |
| 144 | u32 flctl_off; |
| 145 | u32 bchctl_off; |
| 146 | u32 dma_cfg_off; |
| 147 | u32 dma_data_buf_off; |
| 148 | u32 dma_oob_buf_off; |
| 149 | u32 dma_st_off; |
| 150 | u32 bch_st_off; |
| 151 | u32 randmz_off; |
| 152 | u32 int_en_off; |
| 153 | u32 int_clr_off; |
| 154 | u32 int_st_off; |
| 155 | u32 oob0_off; |
| 156 | u32 oob1_off; |
| 157 | struct ecc_cnt_status ecc0; |
| 158 | struct ecc_cnt_status ecc1; |
| 159 | }; |
| 160 | |
| 161 | struct rk_nfc_nand_chip { |
| 162 | struct nand_chip chip; |
| 163 | |
| 164 | u16 boot_blks; |
| 165 | u16 metadata_size; |
| 166 | u32 boot_ecc; |
| 167 | u32 timing; |
| 168 | |
| 169 | u8 nsels; |
| 170 | u8 sels[0]; |
| 171 | /* Nothing after this field. */ |
| 172 | }; |
| 173 | |
| 174 | struct rk_nfc { |
| 175 | struct nand_hw_control controller; |
| 176 | const struct nfc_cfg *cfg; |
| 177 | struct udevice *dev; |
| 178 | |
| 179 | struct clk *nfc_clk; |
| 180 | struct clk *ahb_clk; |
| 181 | void __iomem *regs; |
| 182 | |
| 183 | int selected_bank; |
| 184 | u32 band_offset; |
| 185 | u32 cur_ecc; |
| 186 | u32 cur_timing; |
| 187 | |
| 188 | u8 *page_buf; |
| 189 | u32 *oob_buf; |
| 190 | |
| 191 | unsigned long assigned_cs; |
| 192 | }; |
| 193 | |
| 194 | static inline struct rk_nfc_nand_chip *rk_nfc_to_rknand(struct nand_chip *chip) |
| 195 | { |
| 196 | return container_of(chip, struct rk_nfc_nand_chip, chip); |
| 197 | } |
| 198 | |
| 199 | static inline u8 *rk_nfc_buf_to_data_ptr(struct nand_chip *chip, const u8 *p, int i) |
| 200 | { |
| 201 | return (u8 *)p + i * chip->ecc.size; |
| 202 | } |
| 203 | |
| 204 | static inline u8 *rk_nfc_buf_to_oob_ptr(struct nand_chip *chip, int i) |
| 205 | { |
| 206 | u8 *poi; |
| 207 | |
| 208 | poi = chip->oob_poi + i * NFC_SYS_DATA_SIZE; |
| 209 | |
| 210 | return poi; |
| 211 | } |
| 212 | |
| 213 | static inline u8 *rk_nfc_buf_to_oob_ecc_ptr(struct nand_chip *chip, int i) |
| 214 | { |
| 215 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 216 | u8 *poi; |
| 217 | |
| 218 | poi = chip->oob_poi + rknand->metadata_size + chip->ecc.bytes * i; |
| 219 | |
| 220 | return poi; |
| 221 | } |
| 222 | |
| 223 | static inline int rk_nfc_data_len(struct nand_chip *chip) |
| 224 | { |
| 225 | return chip->ecc.size + chip->ecc.bytes + NFC_SYS_DATA_SIZE; |
| 226 | } |
| 227 | |
| 228 | static inline u8 *rk_nfc_data_ptr(struct nand_chip *chip, int i) |
| 229 | { |
| 230 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 231 | |
| 232 | return nfc->page_buf + i * rk_nfc_data_len(chip); |
| 233 | } |
| 234 | |
| 235 | static inline u8 *rk_nfc_oob_ptr(struct nand_chip *chip, int i) |
| 236 | { |
| 237 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 238 | |
| 239 | return nfc->page_buf + i * rk_nfc_data_len(chip) + chip->ecc.size; |
| 240 | } |
| 241 | |
| 242 | static int rk_nfc_hw_ecc_setup(struct nand_chip *chip, u32 strength) |
| 243 | { |
| 244 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 245 | u32 reg, i; |
| 246 | |
| 247 | for (i = 0; i < NFC_ECC_MAX_MODES; i++) { |
| 248 | if (strength == nfc->cfg->ecc_strengths[i]) { |
| 249 | reg = nfc->cfg->ecc_cfgs[i]; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (i >= NFC_ECC_MAX_MODES) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | writel(reg, nfc->regs + nfc->cfg->bchctl_off); |
| 258 | |
| 259 | /* Save chip ECC setting */ |
| 260 | nfc->cur_ecc = strength; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static void rk_nfc_select_chip(struct mtd_info *mtd, int cs) |
| 266 | { |
| 267 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 268 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 269 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 270 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 271 | u32 val; |
| 272 | |
| 273 | if (cs < 0) { |
| 274 | nfc->selected_bank = -1; |
| 275 | /* Deselect the currently selected target. */ |
| 276 | val = readl(nfc->regs + NFC_FMCTL); |
| 277 | val &= ~FMCTL_CE_SEL_M; |
| 278 | writel(val, nfc->regs + NFC_FMCTL); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | nfc->selected_bank = rknand->sels[cs]; |
| 283 | nfc->band_offset = NFC_BANK + nfc->selected_bank * NFC_BANK_STEP; |
| 284 | |
| 285 | val = readl(nfc->regs + NFC_FMCTL); |
| 286 | val &= ~FMCTL_CE_SEL_M; |
| 287 | val |= FMCTL_CE_SEL(nfc->selected_bank); |
| 288 | |
| 289 | writel(val, nfc->regs + NFC_FMCTL); |
| 290 | |
| 291 | /* |
| 292 | * Compare current chip timing with selected chip timing and |
| 293 | * change if needed. |
| 294 | */ |
| 295 | if (nfc->cur_timing != rknand->timing) { |
| 296 | writel(rknand->timing, nfc->regs + NFC_FMWAIT); |
| 297 | nfc->cur_timing = rknand->timing; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Compare current chip ECC setting with selected chip ECC setting and |
| 302 | * change if needed. |
| 303 | */ |
| 304 | if (nfc->cur_ecc != ecc->strength) |
| 305 | rk_nfc_hw_ecc_setup(chip, ecc->strength); |
| 306 | } |
| 307 | |
| 308 | static inline int rk_nfc_wait_ioready(struct rk_nfc *nfc) |
| 309 | { |
| 310 | u32 timeout = (CONFIG_SYS_HZ * NFC_TIMEOUT_MS) / 1000; |
| 311 | u32 time_start; |
| 312 | |
| 313 | time_start = get_timer(0); |
| 314 | do { |
| 315 | if (readl(nfc->regs + NFC_FMCTL) & FMCTL_RDY) |
| 316 | return 0; |
| 317 | } while (get_timer(time_start) < timeout); |
| 318 | |
| 319 | dev_err(nfc->dev, "wait for io ready timedout\n"); |
| 320 | return -ETIMEDOUT; |
| 321 | } |
| 322 | |
| 323 | static void rk_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 324 | { |
| 325 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 326 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 327 | void __iomem *bank_base; |
| 328 | int i = 0; |
| 329 | |
| 330 | bank_base = nfc->regs + nfc->band_offset + BANK_DATA; |
| 331 | |
| 332 | for (i = 0; i < len; i++) |
| 333 | buf[i] = readl(bank_base); |
| 334 | } |
| 335 | |
| 336 | static void rk_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 337 | { |
| 338 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 339 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 340 | void __iomem *bank_base; |
| 341 | int i = 0; |
| 342 | |
| 343 | bank_base = nfc->regs + nfc->band_offset + BANK_DATA; |
| 344 | |
| 345 | for (i = 0; i < len; i++) |
| 346 | writel(buf[i], bank_base); |
| 347 | } |
| 348 | |
| 349 | static void rk_nfc_cmd(struct mtd_info *mtd, int dat, unsigned int ctrl) |
| 350 | { |
| 351 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 352 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 353 | void __iomem *bank_base; |
| 354 | |
| 355 | bank_base = nfc->regs + nfc->band_offset; |
| 356 | |
| 357 | if (ctrl & NAND_CTRL_CHANGE) { |
| 358 | if (ctrl & NAND_ALE) |
| 359 | bank_base += BANK_ADDR; |
| 360 | else if (ctrl & NAND_CLE) |
| 361 | bank_base += BANK_CMD; |
| 362 | chip->IO_ADDR_W = bank_base; |
| 363 | } |
| 364 | |
| 365 | if (dat != NAND_CMD_NONE) |
| 366 | writel(dat & 0xFF, chip->IO_ADDR_W); |
| 367 | } |
| 368 | |
| 369 | static uint8_t rockchip_nand_read_byte(struct mtd_info *mtd) |
| 370 | { |
| 371 | uint8_t ret; |
| 372 | |
| 373 | rk_nfc_read_buf(mtd, &ret, 1); |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static int rockchip_nand_dev_ready(struct mtd_info *mtd) |
| 379 | { |
| 380 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 381 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 382 | |
| 383 | if (readl(nfc->regs + NFC_FMCTL) & FMCTL_RDY) |
| 384 | return 1; |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void rk_nfc_xfer_start(struct rk_nfc *nfc, u8 rw, u8 n_KB, |
| 390 | dma_addr_t dma_data, dma_addr_t dma_oob) |
| 391 | { |
| 392 | u32 dma_reg, fl_reg, bch_reg; |
| 393 | |
| 394 | dma_reg = DMA_ST | ((!rw) << DMA_WR) | DMA_EN | (2 << DMA_AHB_SIZE) | |
| 395 | (7 << DMA_BURST_SIZE) | (16 << DMA_INC_NUM); |
| 396 | |
| 397 | fl_reg = (rw << FLCTL_WR) | FLCTL_XFER_EN | FLCTL_ACORRECT | |
| 398 | (n_KB << FLCTL_XFER_SECTOR) | FLCTL_TOG_FIX; |
| 399 | |
| 400 | if (nfc->cfg->type == NFC_V6 || nfc->cfg->type == NFC_V8) { |
| 401 | bch_reg = readl_relaxed(nfc->regs + nfc->cfg->bchctl_off); |
| 402 | bch_reg = (bch_reg & (~BCHCTL_BANK_M)) | |
| 403 | (nfc->selected_bank << BCHCTL_BANK); |
| 404 | writel(bch_reg, nfc->regs + nfc->cfg->bchctl_off); |
| 405 | } |
| 406 | |
| 407 | writel(dma_reg, nfc->regs + nfc->cfg->dma_cfg_off); |
| 408 | writel((u32)dma_data, nfc->regs + nfc->cfg->dma_data_buf_off); |
| 409 | writel((u32)dma_oob, nfc->regs + nfc->cfg->dma_oob_buf_off); |
| 410 | writel(fl_reg, nfc->regs + nfc->cfg->flctl_off); |
| 411 | fl_reg |= FLCTL_XFER_ST; |
| 412 | writel(fl_reg, nfc->regs + nfc->cfg->flctl_off); |
| 413 | } |
| 414 | |
| 415 | static int rk_nfc_wait_for_xfer_done(struct rk_nfc *nfc) |
| 416 | { |
| 417 | unsigned long timeout = (CONFIG_SYS_HZ * NFC_TIMEOUT_MS) / 1000; |
| 418 | void __iomem *ptr = nfc->regs + nfc->cfg->flctl_off; |
| 419 | u32 time_start; |
| 420 | |
| 421 | time_start = get_timer(0); |
| 422 | |
| 423 | do { |
| 424 | if (readl(ptr) & FLCTL_XFER_READY) |
| 425 | return 0; |
| 426 | } while (get_timer(time_start) < timeout); |
| 427 | |
| 428 | dev_err(nfc->dev, "wait for io ready timedout\n"); |
| 429 | return -ETIMEDOUT; |
| 430 | } |
| 431 | |
| 432 | static int rk_nfc_write_page_raw(struct mtd_info *mtd, |
| 433 | struct nand_chip *chip, |
| 434 | const u8 *buf, |
| 435 | int oob_required, |
| 436 | int page) |
| 437 | { |
| 438 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 439 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 440 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 441 | int i, pages_per_blk; |
| 442 | |
| 443 | pages_per_blk = mtd->erasesize / mtd->writesize; |
| 444 | if ((page < (pages_per_blk * rknand->boot_blks)) && |
| 445 | rknand->boot_ecc != ecc->strength) { |
| 446 | /* |
| 447 | * There's currently no method to notify the MTD framework that |
| 448 | * a different ECC strength is in use for the boot blocks. |
| 449 | */ |
| 450 | return -EIO; |
| 451 | } |
| 452 | |
| 453 | if (!buf) |
| 454 | memset(nfc->page_buf, 0xff, mtd->writesize + mtd->oobsize); |
| 455 | |
| 456 | for (i = 0; i < ecc->steps; i++) { |
| 457 | /* Copy data to the NFC buffer. */ |
| 458 | if (buf) |
| 459 | memcpy(rk_nfc_data_ptr(chip, i), |
| 460 | rk_nfc_buf_to_data_ptr(chip, buf, i), |
| 461 | ecc->size); |
| 462 | /* |
| 463 | * The first four bytes of OOB are reserved for the |
| 464 | * boot ROM. In some debugging cases, such as with a |
| 465 | * read, erase and write back test these 4 bytes stored |
| 466 | * in OOB also need to be written back. |
| 467 | * |
| 468 | * The function nand_block_bad detects bad blocks like: |
| 469 | * |
| 470 | * bad = chip->oob_poi[chip->badblockpos]; |
| 471 | * |
| 472 | * chip->badblockpos == 0 for a large page NAND Flash, |
| 473 | * so chip->oob_poi[0] is the bad block mask (BBM). |
| 474 | * |
| 475 | * The OOB data layout on the NFC is: |
| 476 | * |
| 477 | * PA0 PA1 PA2 PA3 | BBM OOB1 OOB2 OOB3 | ... |
| 478 | * |
| 479 | * or |
| 480 | * |
| 481 | * 0xFF 0xFF 0xFF 0xFF | BBM OOB1 OOB2 OOB3 | ... |
| 482 | * |
| 483 | * The code here just swaps the first 4 bytes with the last |
| 484 | * 4 bytes without losing any data. |
| 485 | * |
| 486 | * The chip->oob_poi data layout: |
| 487 | * |
| 488 | * BBM OOB1 OOB2 OOB3 |......| PA0 PA1 PA2 PA3 |
| 489 | * |
| 490 | * The rk_nfc_ooblayout_free() function already has reserved |
| 491 | * these 4 bytes with: |
| 492 | * |
| 493 | * oob_region->offset = NFC_SYS_DATA_SIZE + 2; |
| 494 | */ |
| 495 | if (!i) |
| 496 | memcpy(rk_nfc_oob_ptr(chip, i), |
| 497 | rk_nfc_buf_to_oob_ptr(chip, ecc->steps - 1), |
| 498 | NFC_SYS_DATA_SIZE); |
| 499 | else |
| 500 | memcpy(rk_nfc_oob_ptr(chip, i), |
| 501 | rk_nfc_buf_to_oob_ptr(chip, i - 1), |
| 502 | NFC_SYS_DATA_SIZE); |
| 503 | /* Copy ECC data to the NFC buffer. */ |
| 504 | memcpy(rk_nfc_oob_ptr(chip, i) + NFC_SYS_DATA_SIZE, |
| 505 | rk_nfc_buf_to_oob_ecc_ptr(chip, i), |
| 506 | ecc->bytes); |
| 507 | } |
| 508 | |
| 509 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 510 | rk_nfc_write_buf(mtd, buf, mtd->writesize + mtd->oobsize); |
| 511 | return nand_prog_page_end_op(chip); |
| 512 | } |
| 513 | |
| 514 | static int rk_nfc_write_page_hwecc(struct mtd_info *mtd, |
| 515 | struct nand_chip *chip, |
| 516 | const u8 *buf, |
| 517 | int oob_required, |
| 518 | int page) |
| 519 | { |
| 520 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 521 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 522 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 523 | int oob_step = (ecc->bytes > 60) ? NFC_MAX_OOB_PER_STEP : |
| 524 | NFC_MIN_OOB_PER_STEP; |
| 525 | int pages_per_blk = mtd->erasesize / mtd->writesize; |
| 526 | int ret = 0, i, boot_rom_mode = 0; |
| 527 | dma_addr_t dma_data, dma_oob; |
| 528 | u32 reg; |
| 529 | u8 *oob; |
| 530 | |
| 531 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 532 | |
| 533 | if (buf) |
| 534 | memcpy(nfc->page_buf, buf, mtd->writesize); |
| 535 | else |
| 536 | memset(nfc->page_buf, 0xFF, mtd->writesize); |
| 537 | |
| 538 | /* |
| 539 | * The first blocks (4, 8 or 16 depending on the device) are used |
| 540 | * by the boot ROM and the first 32 bits of OOB need to link to |
| 541 | * the next page address in the same block. We can't directly copy |
| 542 | * OOB data from the MTD framework, because this page address |
| 543 | * conflicts for example with the bad block marker (BBM), |
| 544 | * so we shift all OOB data including the BBM with 4 byte positions. |
| 545 | * As a consequence the OOB size available to the MTD framework is |
| 546 | * also reduced with 4 bytes. |
| 547 | * |
| 548 | * PA0 PA1 PA2 PA3 | BBM OOB1 OOB2 OOB3 | ... |
| 549 | * |
| 550 | * If a NAND is not a boot medium or the page is not a boot block, |
| 551 | * the first 4 bytes are left untouched by writing 0xFF to them. |
| 552 | * |
| 553 | * 0xFF 0xFF 0xFF 0xFF | BBM OOB1 OOB2 OOB3 | ... |
| 554 | * |
| 555 | * Configure the ECC algorithm supported by the boot ROM. |
| 556 | */ |
| 557 | if (page < (pages_per_blk * rknand->boot_blks)) { |
| 558 | boot_rom_mode = 1; |
| 559 | if (rknand->boot_ecc != ecc->strength) |
| 560 | rk_nfc_hw_ecc_setup(chip, rknand->boot_ecc); |
| 561 | } |
| 562 | |
| 563 | for (i = 0; i < ecc->steps; i++) { |
| 564 | if (!i) { |
| 565 | reg = 0xFFFFFFFF; |
| 566 | } else { |
| 567 | oob = chip->oob_poi + (i - 1) * NFC_SYS_DATA_SIZE; |
| 568 | reg = oob[0] | oob[1] << 8 | oob[2] << 16 | |
| 569 | oob[3] << 24; |
| 570 | } |
| 571 | |
| 572 | if (!i && boot_rom_mode) |
| 573 | reg = (page & (pages_per_blk - 1)) * 4; |
| 574 | |
| 575 | if (nfc->cfg->type == NFC_V9) |
| 576 | nfc->oob_buf[i] = reg; |
| 577 | else |
| 578 | nfc->oob_buf[i * (oob_step / 4)] = reg; |
| 579 | } |
| 580 | |
| 581 | dma_data = dma_map_single((void *)nfc->page_buf, |
| 582 | mtd->writesize, DMA_TO_DEVICE); |
| 583 | dma_oob = dma_map_single(nfc->oob_buf, |
| 584 | ecc->steps * oob_step, |
| 585 | DMA_TO_DEVICE); |
| 586 | |
| 587 | rk_nfc_xfer_start(nfc, NFC_WRITE, ecc->steps, dma_data, |
| 588 | dma_oob); |
| 589 | ret = rk_nfc_wait_for_xfer_done(nfc); |
| 590 | |
| 591 | dma_unmap_single(dma_data, mtd->writesize, |
| 592 | DMA_TO_DEVICE); |
| 593 | dma_unmap_single(dma_oob, ecc->steps * oob_step, |
| 594 | DMA_TO_DEVICE); |
| 595 | |
| 596 | if (boot_rom_mode && rknand->boot_ecc != ecc->strength) |
| 597 | rk_nfc_hw_ecc_setup(chip, ecc->strength); |
| 598 | |
| 599 | if (ret) { |
| 600 | dev_err(nfc->dev, "write: wait transfer done timeout.\n"); |
| 601 | return -ETIMEDOUT; |
| 602 | } |
| 603 | |
| 604 | return nand_prog_page_end_op(chip); |
| 605 | } |
| 606 | |
| 607 | static int rk_nfc_write_oob(struct mtd_info *mtd, |
| 608 | struct nand_chip *chip, int page) |
| 609 | { |
| 610 | return rk_nfc_write_page_hwecc(mtd, chip, NULL, 1, page); |
| 611 | } |
| 612 | |
| 613 | static int rk_nfc_read_page_raw(struct mtd_info *mtd, |
| 614 | struct nand_chip *chip, |
| 615 | u8 *buf, |
| 616 | int oob_required, |
| 617 | int page) |
| 618 | { |
| 619 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 620 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 621 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 622 | int i, pages_per_blk; |
| 623 | |
| 624 | pages_per_blk = mtd->erasesize / mtd->writesize; |
| 625 | if ((page < (pages_per_blk * rknand->boot_blks)) && |
| 626 | nfc->selected_bank == 0 && |
| 627 | rknand->boot_ecc != ecc->strength) { |
| 628 | /* |
| 629 | * There's currently no method to notify the MTD framework that |
| 630 | * a different ECC strength is in use for the boot blocks. |
| 631 | */ |
| 632 | return -EIO; |
| 633 | } |
| 634 | |
| 635 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 636 | rk_nfc_read_buf(mtd, nfc->page_buf, mtd->writesize + mtd->oobsize); |
| 637 | for (i = 0; i < ecc->steps; i++) { |
| 638 | /* |
| 639 | * The first four bytes of OOB are reserved for the |
| 640 | * boot ROM. In some debugging cases, such as with a read, |
| 641 | * erase and write back test, these 4 bytes also must be |
| 642 | * saved somewhere, otherwise this information will be |
| 643 | * lost during a write back. |
| 644 | */ |
| 645 | if (!i) |
| 646 | memcpy(rk_nfc_buf_to_oob_ptr(chip, ecc->steps - 1), |
| 647 | rk_nfc_oob_ptr(chip, i), |
| 648 | NFC_SYS_DATA_SIZE); |
| 649 | else |
| 650 | memcpy(rk_nfc_buf_to_oob_ptr(chip, i - 1), |
| 651 | rk_nfc_oob_ptr(chip, i), |
| 652 | NFC_SYS_DATA_SIZE); |
| 653 | |
| 654 | /* Copy ECC data from the NFC buffer. */ |
| 655 | memcpy(rk_nfc_buf_to_oob_ecc_ptr(chip, i), |
| 656 | rk_nfc_oob_ptr(chip, i) + NFC_SYS_DATA_SIZE, |
| 657 | ecc->bytes); |
| 658 | |
| 659 | /* Copy data from the NFC buffer. */ |
| 660 | if (buf) |
| 661 | memcpy(rk_nfc_buf_to_data_ptr(chip, buf, i), |
| 662 | rk_nfc_data_ptr(chip, i), |
| 663 | ecc->size); |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int rk_nfc_read_page_hwecc(struct mtd_info *mtd, |
| 670 | struct nand_chip *chip, |
| 671 | u8 *buf, |
| 672 | int oob_required, |
| 673 | int page) |
| 674 | { |
| 675 | struct rk_nfc *nfc = nand_get_controller_data(chip); |
| 676 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 677 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 678 | int oob_step = (ecc->bytes > 60) ? NFC_MAX_OOB_PER_STEP : |
| 679 | NFC_MIN_OOB_PER_STEP; |
| 680 | int pages_per_blk = mtd->erasesize / mtd->writesize; |
| 681 | dma_addr_t dma_data, dma_oob; |
| 682 | int ret = 0, i, cnt, boot_rom_mode = 0; |
| 683 | int max_bitflips = 0, bch_st, ecc_fail = 0; |
| 684 | u8 *oob; |
| 685 | u32 tmp; |
| 686 | |
| 687 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 688 | |
| 689 | dma_data = dma_map_single(nfc->page_buf, |
| 690 | mtd->writesize, |
| 691 | DMA_FROM_DEVICE); |
| 692 | dma_oob = dma_map_single(nfc->oob_buf, |
| 693 | ecc->steps * oob_step, |
| 694 | DMA_FROM_DEVICE); |
| 695 | |
| 696 | /* |
| 697 | * The first blocks (4, 8 or 16 depending on the device) |
| 698 | * are used by the boot ROM. |
| 699 | * Configure the ECC algorithm supported by the boot ROM. |
| 700 | */ |
| 701 | if (page < (pages_per_blk * rknand->boot_blks) && |
| 702 | nfc->selected_bank == 0) { |
| 703 | boot_rom_mode = 1; |
| 704 | if (rknand->boot_ecc != ecc->strength) |
| 705 | rk_nfc_hw_ecc_setup(chip, rknand->boot_ecc); |
| 706 | } |
| 707 | |
| 708 | rk_nfc_xfer_start(nfc, NFC_READ, ecc->steps, dma_data, |
| 709 | dma_oob); |
| 710 | ret = rk_nfc_wait_for_xfer_done(nfc); |
| 711 | |
| 712 | dma_unmap_single(dma_data, mtd->writesize, |
| 713 | DMA_FROM_DEVICE); |
| 714 | dma_unmap_single(dma_oob, ecc->steps * oob_step, |
| 715 | DMA_FROM_DEVICE); |
| 716 | |
| 717 | if (ret) { |
| 718 | ret = -ETIMEDOUT; |
| 719 | dev_err(nfc->dev, "read: wait transfer done timeout.\n"); |
| 720 | goto timeout_err; |
| 721 | } |
| 722 | |
| 723 | for (i = 1; i < ecc->steps; i++) { |
| 724 | oob = chip->oob_poi + (i - 1) * NFC_SYS_DATA_SIZE; |
| 725 | if (nfc->cfg->type == NFC_V9) |
| 726 | tmp = nfc->oob_buf[i]; |
| 727 | else |
| 728 | tmp = nfc->oob_buf[i * (oob_step / 4)]; |
| 729 | *oob++ = (u8)tmp; |
| 730 | *oob++ = (u8)(tmp >> 8); |
| 731 | *oob++ = (u8)(tmp >> 16); |
| 732 | *oob++ = (u8)(tmp >> 24); |
| 733 | } |
| 734 | |
| 735 | for (i = 0; i < (ecc->steps / 2); i++) { |
| 736 | bch_st = readl_relaxed(nfc->regs + |
| 737 | nfc->cfg->bch_st_off + i * 4); |
| 738 | if (bch_st & BIT(nfc->cfg->ecc0.err_flag_bit) || |
| 739 | bch_st & BIT(nfc->cfg->ecc1.err_flag_bit)) { |
| 740 | mtd->ecc_stats.failed++; |
| 741 | ecc_fail = 1; |
| 742 | } else { |
| 743 | cnt = ECC_ERR_CNT(bch_st, nfc->cfg->ecc0); |
| 744 | mtd->ecc_stats.corrected += cnt; |
| 745 | max_bitflips = max_t(u32, max_bitflips, cnt); |
| 746 | |
| 747 | cnt = ECC_ERR_CNT(bch_st, nfc->cfg->ecc1); |
| 748 | mtd->ecc_stats.corrected += cnt; |
| 749 | max_bitflips = max_t(u32, max_bitflips, cnt); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (buf) |
| 754 | memcpy(buf, nfc->page_buf, mtd->writesize); |
| 755 | |
| 756 | timeout_err: |
| 757 | if (boot_rom_mode && rknand->boot_ecc != ecc->strength) |
| 758 | rk_nfc_hw_ecc_setup(chip, ecc->strength); |
| 759 | |
| 760 | if (ret) |
| 761 | return ret; |
| 762 | |
| 763 | if (ecc_fail) { |
| 764 | dev_err(nfc->dev, "read page: %x ecc error!\n", page); |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | return max_bitflips; |
| 769 | } |
| 770 | |
| 771 | static int rk_nfc_read_oob(struct mtd_info *mtd, |
| 772 | struct nand_chip *chip, int page) |
| 773 | { |
| 774 | return rk_nfc_read_page_hwecc(mtd, chip, NULL, 1, page); |
| 775 | } |
| 776 | |
| 777 | static inline void rk_nfc_hw_init(struct rk_nfc *nfc) |
| 778 | { |
| 779 | /* Disable flash wp. */ |
| 780 | writel(FMCTL_WP, nfc->regs + NFC_FMCTL); |
| 781 | /* Config default timing 40ns at 150 Mhz NFC clock. */ |
| 782 | writel(0x1081, nfc->regs + NFC_FMWAIT); |
| 783 | nfc->cur_timing = 0x1081; |
| 784 | /* Disable randomizer and DMA. */ |
| 785 | writel(0, nfc->regs + nfc->cfg->randmz_off); |
| 786 | writel(0, nfc->regs + nfc->cfg->dma_cfg_off); |
| 787 | writel(FLCTL_RST, nfc->regs + nfc->cfg->flctl_off); |
| 788 | } |
| 789 | |
| 790 | static int rk_nfc_enable_clks(struct udevice *dev, struct rk_nfc *nfc) |
| 791 | { |
| 792 | int ret; |
| 793 | |
| 794 | if (!IS_ERR(nfc->nfc_clk)) { |
| 795 | ret = clk_prepare_enable(nfc->nfc_clk); |
| 796 | if (ret) |
| 797 | dev_err(dev, "failed to enable NFC clk\n"); |
| 798 | } |
| 799 | |
| 800 | ret = clk_prepare_enable(nfc->ahb_clk); |
| 801 | if (ret) { |
| 802 | dev_err(dev, "failed to enable ahb clk\n"); |
| 803 | if (!IS_ERR(nfc->nfc_clk)) |
| 804 | clk_disable_unprepare(nfc->nfc_clk); |
| 805 | } |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static void rk_nfc_disable_clks(struct rk_nfc *nfc) |
| 811 | { |
| 812 | if (!IS_ERR(nfc->nfc_clk)) |
| 813 | clk_disable_unprepare(nfc->nfc_clk); |
| 814 | clk_disable_unprepare(nfc->ahb_clk); |
| 815 | } |
| 816 | |
| 817 | static int rk_nfc_ooblayout_free(struct mtd_info *mtd, int section, |
| 818 | struct mtd_oob_region *oob_region) |
| 819 | { |
| 820 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 821 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 822 | |
| 823 | if (section) |
| 824 | return -ERANGE; |
| 825 | |
| 826 | /* |
| 827 | * The beginning of the OOB area stores the reserved data for the NFC, |
| 828 | * the size of the reserved data is NFC_SYS_DATA_SIZE bytes. |
| 829 | */ |
| 830 | oob_region->length = rknand->metadata_size - NFC_SYS_DATA_SIZE - 2; |
| 831 | oob_region->offset = NFC_SYS_DATA_SIZE + 2; |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | static int rk_nfc_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 837 | struct mtd_oob_region *oob_region) |
| 838 | { |
| 839 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 840 | struct rk_nfc_nand_chip *rknand = rk_nfc_to_rknand(chip); |
| 841 | |
| 842 | if (section) |
| 843 | return -ERANGE; |
| 844 | |
| 845 | oob_region->length = mtd->oobsize - rknand->metadata_size; |
| 846 | oob_region->offset = rknand->metadata_size; |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static const struct mtd_ooblayout_ops rk_nfc_ooblayout_ops = { |
| 852 | .rfree = rk_nfc_ooblayout_free, |
| 853 | .ecc = rk_nfc_ooblayout_ecc, |
| 854 | }; |
| 855 | |
| 856 | static int rk_nfc_ecc_init(struct rk_nfc *nfc, struct nand_chip *chip) |
| 857 | { |
| 858 | const u8 *strengths = nfc->cfg->ecc_strengths; |
| 859 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 860 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 861 | u8 max_strength, nfc_max_strength; |
| 862 | int i; |
| 863 | |
| 864 | nfc_max_strength = nfc->cfg->ecc_strengths[0]; |
| 865 | /* If optional dt settings not present. */ |
| 866 | if (!ecc->size || !ecc->strength || |
| 867 | ecc->strength > nfc_max_strength) { |
| 868 | chip->ecc.size = 1024; |
| 869 | ecc->steps = mtd->writesize / ecc->size; |
| 870 | |
| 871 | /* |
| 872 | * HW ECC always requests the number of ECC bytes per 1024 byte |
| 873 | * blocks. The first 4 OOB bytes are reserved for sys data. |
| 874 | */ |
| 875 | max_strength = ((mtd->oobsize / ecc->steps) - 4) * 8 / |
| 876 | fls(8 * 1024); |
| 877 | if (max_strength > nfc_max_strength) |
| 878 | max_strength = nfc_max_strength; |
| 879 | |
| 880 | for (i = 0; i < 4; i++) { |
| 881 | if (max_strength >= strengths[i]) |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | if (i >= 4) { |
| 886 | dev_err(nfc->dev, "unsupported ECC strength\n"); |
| 887 | return -EOPNOTSUPP; |
| 888 | } |
| 889 | |
| 890 | ecc->strength = strengths[i]; |
| 891 | } |
| 892 | ecc->steps = mtd->writesize / ecc->size; |
| 893 | ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * chip->ecc.size), 8); |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | static int rk_nfc_nand_chip_init(ofnode node, struct rk_nfc *nfc, int devnum) |
| 899 | { |
| 900 | struct rk_nfc_nand_chip *rknand; |
| 901 | struct udevice *dev = nfc->dev; |
| 902 | struct nand_ecc_ctrl *ecc; |
| 903 | struct nand_chip *chip; |
| 904 | struct mtd_info *mtd; |
| 905 | u32 cs[NFC_MAX_NSELS]; |
| 906 | int nsels, i, ret; |
| 907 | u32 tmp; |
| 908 | |
| 909 | if (!ofnode_get_property(node, "reg", &nsels)) |
| 910 | return -ENODEV; |
| 911 | nsels /= sizeof(u32); |
| 912 | if (!nsels || nsels > NFC_MAX_NSELS) { |
| 913 | dev_err(dev, "invalid reg property size %d\n", nsels); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | rknand = kzalloc(sizeof(*rknand) + nsels * sizeof(u8), GFP_KERNEL); |
| 918 | if (!rknand) |
| 919 | return -ENOMEM; |
| 920 | |
| 921 | rknand->nsels = nsels; |
| 922 | rknand->timing = nfc->cur_timing; |
| 923 | |
| 924 | ret = ofnode_read_u32_array(node, "reg", cs, nsels); |
| 925 | if (ret < 0) { |
| 926 | dev_err(dev, "Could not retrieve reg property\n"); |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | for (i = 0; i < nsels; i++) { |
| 931 | if (cs[i] >= NFC_MAX_NSELS) { |
| 932 | dev_err(dev, "invalid CS: %u\n", cs[i]); |
| 933 | return -EINVAL; |
| 934 | } |
| 935 | |
| 936 | if (test_and_set_bit(cs[i], &nfc->assigned_cs)) { |
| 937 | dev_err(dev, "CS %u already assigned\n", cs[i]); |
| 938 | return -EINVAL; |
| 939 | } |
| 940 | |
| 941 | rknand->sels[i] = cs[i]; |
| 942 | } |
| 943 | |
| 944 | chip = &rknand->chip; |
| 945 | ecc = &chip->ecc; |
| 946 | ecc->mode = NAND_ECC_HW_SYNDROME; |
| 947 | |
| 948 | ret = ofnode_read_u32(node, "nand-ecc-strength", &tmp); |
| 949 | ecc->strength = ret ? 0 : tmp; |
| 950 | |
| 951 | ret = ofnode_read_u32(node, "nand-ecc-step-size", &tmp); |
| 952 | ecc->size = ret ? 0 : tmp; |
| 953 | |
| 954 | mtd = nand_to_mtd(chip); |
| 955 | mtd->owner = THIS_MODULE; |
| 956 | mtd->dev->parent = dev; |
| 957 | |
| 958 | nand_set_controller_data(chip, nfc); |
| 959 | |
| 960 | chip->chip_delay = NFC_RB_DELAY_US; |
| 961 | chip->select_chip = rk_nfc_select_chip; |
| 962 | chip->cmd_ctrl = rk_nfc_cmd; |
| 963 | chip->read_buf = rk_nfc_read_buf; |
| 964 | chip->write_buf = rk_nfc_write_buf; |
| 965 | chip->read_byte = rockchip_nand_read_byte; |
| 966 | chip->dev_ready = rockchip_nand_dev_ready; |
| 967 | chip->controller = &nfc->controller; |
| 968 | |
| 969 | chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB; |
| 970 | chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER; |
| 971 | |
| 972 | mtd_set_ooblayout(mtd, &rk_nfc_ooblayout_ops); |
| 973 | rk_nfc_hw_init(nfc); |
| 974 | ret = nand_scan_ident(mtd, nsels, NULL); |
| 975 | if (ret) |
| 976 | return ret; |
| 977 | |
| 978 | ret = rk_nfc_ecc_init(nfc, chip); |
| 979 | if (ret) { |
| 980 | dev_err(dev, "rk_nfc_ecc_init failed: %d\n", ret); |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | ret = ofnode_read_u32(node, "rockchip,boot-blks", &tmp); |
| 985 | rknand->boot_blks = ret ? 0 : tmp; |
| 986 | |
| 987 | ret = ofnode_read_u32(node, "rockchip,boot-ecc-strength", &tmp); |
| 988 | rknand->boot_ecc = ret ? ecc->strength : tmp; |
| 989 | |
| 990 | rknand->metadata_size = NFC_SYS_DATA_SIZE * ecc->steps; |
| 991 | |
| 992 | if (rknand->metadata_size < NFC_SYS_DATA_SIZE + 2) { |
| 993 | dev_err(dev, |
| 994 | "driver needs at least %d bytes of meta data\n", |
| 995 | NFC_SYS_DATA_SIZE + 2); |
| 996 | return -EIO; |
| 997 | } |
| 998 | |
| 999 | if (!nfc->page_buf) { |
| 1000 | nfc->page_buf = kzalloc(NFC_MAX_PAGE_SIZE, GFP_KERNEL); |
| 1001 | if (!nfc->page_buf) |
| 1002 | return -ENOMEM; |
| 1003 | } |
| 1004 | |
| 1005 | if (!nfc->oob_buf) { |
| 1006 | nfc->oob_buf = kzalloc(NFC_MAX_OOB_SIZE, GFP_KERNEL); |
| 1007 | if (!nfc->oob_buf) { |
| 1008 | kfree(nfc->page_buf); |
| 1009 | nfc->page_buf = NULL; |
| 1010 | return -ENOMEM; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | ecc->read_page = rk_nfc_read_page_hwecc; |
| 1015 | ecc->read_page_raw = rk_nfc_read_page_raw; |
| 1016 | ecc->read_oob = rk_nfc_read_oob; |
| 1017 | ecc->write_page = rk_nfc_write_page_hwecc; |
| 1018 | ecc->write_page_raw = rk_nfc_write_page_raw; |
| 1019 | ecc->write_oob = rk_nfc_write_oob; |
| 1020 | |
| 1021 | ret = nand_scan_tail(mtd); |
| 1022 | if (ret) { |
| 1023 | dev_err(dev, "nand_scan_tail failed: %d\n", ret); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | return nand_register(devnum, mtd); |
| 1028 | } |
| 1029 | |
| 1030 | static int rk_nfc_nand_chips_init(struct udevice *dev, struct rk_nfc *nfc) |
| 1031 | { |
| 1032 | int ret, i = 0; |
| 1033 | ofnode child; |
| 1034 | |
| 1035 | ofnode_for_each_subnode(child, dev_ofnode(dev)) { |
| 1036 | ret = rk_nfc_nand_chip_init(child, nfc, i++); |
| 1037 | if (ret) |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static struct nfc_cfg nfc_v6_cfg = { |
| 1045 | .type = NFC_V6, |
| 1046 | .ecc_strengths = {60, 40, 24, 16}, |
| 1047 | .ecc_cfgs = { |
| 1048 | 0x00040011, 0x00040001, 0x00000011, 0x00000001, |
| 1049 | }, |
| 1050 | .flctl_off = 0x08, |
| 1051 | .bchctl_off = 0x0C, |
| 1052 | .dma_cfg_off = 0x10, |
| 1053 | .dma_data_buf_off = 0x14, |
| 1054 | .dma_oob_buf_off = 0x18, |
| 1055 | .dma_st_off = 0x1C, |
| 1056 | .bch_st_off = 0x20, |
| 1057 | .randmz_off = 0x150, |
| 1058 | .int_en_off = 0x16C, |
| 1059 | .int_clr_off = 0x170, |
| 1060 | .int_st_off = 0x174, |
| 1061 | .oob0_off = 0x200, |
| 1062 | .oob1_off = 0x230, |
| 1063 | .ecc0 = { |
| 1064 | .err_flag_bit = 2, |
| 1065 | .low = 3, |
| 1066 | .low_mask = 0x1F, |
| 1067 | .low_bn = 5, |
| 1068 | .high = 27, |
| 1069 | .high_mask = 0x1, |
| 1070 | }, |
| 1071 | .ecc1 = { |
| 1072 | .err_flag_bit = 15, |
| 1073 | .low = 16, |
| 1074 | .low_mask = 0x1F, |
| 1075 | .low_bn = 5, |
| 1076 | .high = 29, |
| 1077 | .high_mask = 0x1, |
| 1078 | }, |
| 1079 | }; |
| 1080 | |
| 1081 | static struct nfc_cfg nfc_v8_cfg = { |
| 1082 | .type = NFC_V8, |
| 1083 | .ecc_strengths = {16, 16, 16, 16}, |
| 1084 | .ecc_cfgs = { |
| 1085 | 0x00000001, 0x00000001, 0x00000001, 0x00000001, |
| 1086 | }, |
| 1087 | .flctl_off = 0x08, |
| 1088 | .bchctl_off = 0x0C, |
| 1089 | .dma_cfg_off = 0x10, |
| 1090 | .dma_data_buf_off = 0x14, |
| 1091 | .dma_oob_buf_off = 0x18, |
| 1092 | .dma_st_off = 0x1C, |
| 1093 | .bch_st_off = 0x20, |
| 1094 | .randmz_off = 0x150, |
| 1095 | .int_en_off = 0x16C, |
| 1096 | .int_clr_off = 0x170, |
| 1097 | .int_st_off = 0x174, |
| 1098 | .oob0_off = 0x200, |
| 1099 | .oob1_off = 0x230, |
| 1100 | .ecc0 = { |
| 1101 | .err_flag_bit = 2, |
| 1102 | .low = 3, |
| 1103 | .low_mask = 0x1F, |
| 1104 | .low_bn = 5, |
| 1105 | .high = 27, |
| 1106 | .high_mask = 0x1, |
| 1107 | }, |
| 1108 | .ecc1 = { |
| 1109 | .err_flag_bit = 15, |
| 1110 | .low = 16, |
| 1111 | .low_mask = 0x1F, |
| 1112 | .low_bn = 5, |
| 1113 | .high = 29, |
| 1114 | .high_mask = 0x1, |
| 1115 | }, |
| 1116 | }; |
| 1117 | |
| 1118 | static struct nfc_cfg nfc_v9_cfg = { |
| 1119 | .type = NFC_V9, |
| 1120 | .ecc_strengths = {70, 60, 40, 16}, |
| 1121 | .ecc_cfgs = { |
| 1122 | 0x00000001, 0x06000001, 0x04000001, 0x02000001, |
| 1123 | }, |
| 1124 | .flctl_off = 0x10, |
| 1125 | .bchctl_off = 0x20, |
| 1126 | .dma_cfg_off = 0x30, |
| 1127 | .dma_data_buf_off = 0x34, |
| 1128 | .dma_oob_buf_off = 0x38, |
| 1129 | .dma_st_off = 0x3C, |
| 1130 | .bch_st_off = 0x150, |
| 1131 | .randmz_off = 0x208, |
| 1132 | .int_en_off = 0x120, |
| 1133 | .int_clr_off = 0x124, |
| 1134 | .int_st_off = 0x128, |
| 1135 | .oob0_off = 0x200, |
| 1136 | .oob1_off = 0x204, |
| 1137 | .ecc0 = { |
| 1138 | .err_flag_bit = 2, |
| 1139 | .low = 3, |
| 1140 | .low_mask = 0x7F, |
| 1141 | .low_bn = 7, |
| 1142 | .high = 0, |
| 1143 | .high_mask = 0x0, |
| 1144 | }, |
| 1145 | .ecc1 = { |
| 1146 | .err_flag_bit = 18, |
| 1147 | .low = 19, |
| 1148 | .low_mask = 0x7F, |
| 1149 | .low_bn = 7, |
| 1150 | .high = 0, |
| 1151 | .high_mask = 0x0, |
| 1152 | }, |
| 1153 | }; |
| 1154 | |
| 1155 | static const struct udevice_id rk_nfc_id_table[] = { |
| 1156 | { |
| 1157 | .compatible = "rockchip,px30-nfc", |
| 1158 | .data = (unsigned long)&nfc_v9_cfg |
| 1159 | }, |
| 1160 | { |
| 1161 | .compatible = "rockchip,rk2928-nfc", |
| 1162 | .data = (unsigned long)&nfc_v6_cfg |
| 1163 | }, |
| 1164 | { |
| 1165 | .compatible = "rockchip,rv1108-nfc", |
| 1166 | .data = (unsigned long)&nfc_v8_cfg |
| 1167 | }, |
| 1168 | { |
| 1169 | .compatible = "rockchip,rk3308-nfc", |
| 1170 | .data = (unsigned long)&nfc_v8_cfg |
| 1171 | }, |
| 1172 | { /* sentinel */ } |
| 1173 | }; |
| 1174 | |
| 1175 | static int rk_nfc_probe(struct udevice *dev) |
| 1176 | { |
| 1177 | struct rk_nfc *nfc = dev_get_priv(dev); |
| 1178 | int ret = 0; |
| 1179 | |
| 1180 | nfc->cfg = (void *)dev_get_driver_data(dev); |
| 1181 | nfc->dev = dev; |
| 1182 | |
| 1183 | nfc->regs = (void *)dev_read_addr(dev); |
| 1184 | if (IS_ERR(nfc->regs)) { |
| 1185 | ret = PTR_ERR(nfc->regs); |
| 1186 | goto release_nfc; |
| 1187 | } |
| 1188 | |
| 1189 | nfc->nfc_clk = devm_clk_get(dev, "nfc"); |
| 1190 | if (IS_ERR(nfc->nfc_clk)) { |
| 1191 | dev_dbg(dev, "no NFC clk\n"); |
| 1192 | /* Some earlier models, such as rk3066, have no NFC clk. */ |
| 1193 | } |
| 1194 | |
| 1195 | nfc->ahb_clk = devm_clk_get(dev, "ahb"); |
| 1196 | if (IS_ERR(nfc->ahb_clk)) { |
| 1197 | dev_err(dev, "no ahb clk\n"); |
| 1198 | ret = PTR_ERR(nfc->ahb_clk); |
| 1199 | goto release_nfc; |
| 1200 | } |
| 1201 | |
| 1202 | ret = rk_nfc_enable_clks(dev, nfc); |
| 1203 | if (ret) |
| 1204 | goto release_nfc; |
| 1205 | |
| 1206 | spin_lock_init(&nfc->controller.lock); |
| 1207 | init_waitqueue_head(&nfc->controller.wq); |
| 1208 | |
| 1209 | rk_nfc_hw_init(nfc); |
| 1210 | |
| 1211 | ret = rk_nfc_nand_chips_init(dev, nfc); |
| 1212 | if (ret) { |
| 1213 | dev_err(dev, "failed to init NAND chips\n"); |
| 1214 | goto clk_disable; |
| 1215 | } |
| 1216 | return 0; |
| 1217 | |
| 1218 | clk_disable: |
| 1219 | rk_nfc_disable_clks(nfc); |
| 1220 | release_nfc: |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
| 1224 | U_BOOT_DRIVER(rockchip_nfc) = { |
| 1225 | .name = "rockchip_nfc", |
| 1226 | .id = UCLASS_MTD, |
| 1227 | .of_match = rk_nfc_id_table, |
| 1228 | .probe = rk_nfc_probe, |
| 1229 | .priv_auto = sizeof(struct rk_nfc), |
| 1230 | }; |
| 1231 | |
| 1232 | void board_nand_init(void) |
| 1233 | { |
| 1234 | struct udevice *dev; |
| 1235 | int ret; |
| 1236 | |
| 1237 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
| 1238 | DM_DRIVER_GET(rockchip_nfc), |
| 1239 | &dev); |
| 1240 | if (ret && ret != -ENODEV) |
| 1241 | log_err("Failed to initialize ROCKCHIP NAND controller. (error %d)\n", |
| 1242 | ret); |
| 1243 | } |
| 1244 | |
| 1245 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) |
| 1246 | { |
| 1247 | struct mtd_info *mtd; |
| 1248 | size_t length = size; |
| 1249 | |
| 1250 | mtd = get_nand_dev_by_index(0); |
| 1251 | return nand_read_skip_bad(mtd, offs, &length, NULL, size, (u_char *)dst); |
| 1252 | } |
| 1253 | |
| 1254 | void nand_deselect(void) {} |