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