Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 2 | /* |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 3 | * Copyright 2009-2015 Freescale Semiconductor, Inc. and others |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 4 | * |
| 5 | * Description: MPC5125, VF610, MCF54418 and Kinetis K70 Nand driver. |
| 6 | * Ported to U-Boot by Stefan Agner |
| 7 | * Based on RFC driver posted on Kernel Mailing list by Bill Pringlemeir |
| 8 | * Jason ported to M54418TWR and MVFA5. |
| 9 | * Authors: Stefan Agner <stefan.agner@toradex.com> |
| 10 | * Bill Pringlemeir <bpringlemeir@nbsps.com> |
| 11 | * Shaohui Xie <b21989@freescale.com> |
| 12 | * Jason Jin <Jason.jin@freescale.com> |
| 13 | * |
| 14 | * Based on original driver mpc5121_nfc.c. |
| 15 | * |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 16 | * Limitations: |
| 17 | * - Untested on MPC5125 and M54418. |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 18 | * - DMA and pipelining not used. |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 19 | * - 2K pages or less. |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 20 | * - HW ECC: Only 2K page with 64+ OOB. |
| 21 | * - HW ECC: Only 24 and 32-bit error correction implemented. |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <malloc.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 26 | #include <dm/device_compat.h> |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 27 | |
| 28 | #include <linux/mtd/mtd.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 29 | #include <linux/mtd/rawnand.h> |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 30 | #include <linux/mtd/partitions.h> |
| 31 | |
| 32 | #include <nand.h> |
| 33 | #include <errno.h> |
| 34 | #include <asm/io.h> |
Lukasz Majewski | a8f1a67 | 2018-12-03 10:24:51 +0100 | [diff] [blame] | 35 | #if CONFIG_NAND_VF610_NFC_DT |
| 36 | #include <dm.h> |
| 37 | #include <linux/io.h> |
| 38 | #include <linux/ioport.h> |
| 39 | #endif |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 40 | |
| 41 | /* Register Offsets */ |
| 42 | #define NFC_FLASH_CMD1 0x3F00 |
| 43 | #define NFC_FLASH_CMD2 0x3F04 |
| 44 | #define NFC_COL_ADDR 0x3F08 |
| 45 | #define NFC_ROW_ADDR 0x3F0c |
| 46 | #define NFC_ROW_ADDR_INC 0x3F14 |
| 47 | #define NFC_FLASH_STATUS1 0x3F18 |
| 48 | #define NFC_FLASH_STATUS2 0x3F1c |
| 49 | #define NFC_CACHE_SWAP 0x3F28 |
| 50 | #define NFC_SECTOR_SIZE 0x3F2c |
| 51 | #define NFC_FLASH_CONFIG 0x3F30 |
| 52 | #define NFC_IRQ_STATUS 0x3F38 |
| 53 | |
| 54 | /* Addresses for NFC MAIN RAM BUFFER areas */ |
| 55 | #define NFC_MAIN_AREA(n) ((n) * 0x1000) |
| 56 | |
| 57 | #define PAGE_2K 0x0800 |
| 58 | #define OOB_64 0x0040 |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 59 | #define OOB_MAX 0x0100 |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * NFC_CMD2[CODE] values. See section: |
| 63 | * - 31.4.7 Flash Command Code Description, Vybrid manual |
| 64 | * - 23.8.6 Flash Command Sequencer, MPC5125 manual |
| 65 | * |
| 66 | * Briefly these are bitmasks of controller cycles. |
| 67 | */ |
| 68 | #define READ_PAGE_CMD_CODE 0x7EE0 |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 69 | #define READ_ONFI_PARAM_CMD_CODE 0x4860 |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 70 | #define PROGRAM_PAGE_CMD_CODE 0x7FC0 |
| 71 | #define ERASE_CMD_CODE 0x4EC0 |
| 72 | #define READ_ID_CMD_CODE 0x4804 |
| 73 | #define RESET_CMD_CODE 0x4040 |
| 74 | #define STATUS_READ_CMD_CODE 0x4068 |
| 75 | |
| 76 | /* NFC ECC mode define */ |
| 77 | #define ECC_BYPASS 0 |
| 78 | #define ECC_45_BYTE 6 |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 79 | #define ECC_60_BYTE 7 |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 80 | |
| 81 | /*** Register Mask and bit definitions */ |
| 82 | |
| 83 | /* NFC_FLASH_CMD1 Field */ |
| 84 | #define CMD_BYTE2_MASK 0xFF000000 |
| 85 | #define CMD_BYTE2_SHIFT 24 |
| 86 | |
| 87 | /* NFC_FLASH_CM2 Field */ |
| 88 | #define CMD_BYTE1_MASK 0xFF000000 |
| 89 | #define CMD_BYTE1_SHIFT 24 |
| 90 | #define CMD_CODE_MASK 0x00FFFF00 |
| 91 | #define CMD_CODE_SHIFT 8 |
| 92 | #define BUFNO_MASK 0x00000006 |
| 93 | #define BUFNO_SHIFT 1 |
| 94 | #define START_BIT (1<<0) |
| 95 | |
| 96 | /* NFC_COL_ADDR Field */ |
| 97 | #define COL_ADDR_MASK 0x0000FFFF |
| 98 | #define COL_ADDR_SHIFT 0 |
| 99 | |
| 100 | /* NFC_ROW_ADDR Field */ |
| 101 | #define ROW_ADDR_MASK 0x00FFFFFF |
| 102 | #define ROW_ADDR_SHIFT 0 |
| 103 | #define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000 |
| 104 | #define ROW_ADDR_CHIP_SEL_RB_SHIFT 28 |
| 105 | #define ROW_ADDR_CHIP_SEL_MASK 0x0F000000 |
| 106 | #define ROW_ADDR_CHIP_SEL_SHIFT 24 |
| 107 | |
| 108 | /* NFC_FLASH_STATUS2 Field */ |
| 109 | #define STATUS_BYTE1_MASK 0x000000FF |
| 110 | |
| 111 | /* NFC_FLASH_CONFIG Field */ |
| 112 | #define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000 |
| 113 | #define CONFIG_ECC_SRAM_ADDR_SHIFT 22 |
| 114 | #define CONFIG_ECC_SRAM_REQ_BIT (1<<21) |
| 115 | #define CONFIG_DMA_REQ_BIT (1<<20) |
| 116 | #define CONFIG_ECC_MODE_MASK 0x000E0000 |
| 117 | #define CONFIG_ECC_MODE_SHIFT 17 |
| 118 | #define CONFIG_FAST_FLASH_BIT (1<<16) |
| 119 | #define CONFIG_16BIT (1<<7) |
| 120 | #define CONFIG_BOOT_MODE_BIT (1<<6) |
| 121 | #define CONFIG_ADDR_AUTO_INCR_BIT (1<<5) |
| 122 | #define CONFIG_BUFNO_AUTO_INCR_BIT (1<<4) |
| 123 | #define CONFIG_PAGE_CNT_MASK 0xF |
| 124 | #define CONFIG_PAGE_CNT_SHIFT 0 |
| 125 | |
| 126 | /* NFC_IRQ_STATUS Field */ |
| 127 | #define IDLE_IRQ_BIT (1<<29) |
| 128 | #define IDLE_EN_BIT (1<<20) |
| 129 | #define CMD_DONE_CLEAR_BIT (1<<18) |
| 130 | #define IDLE_CLEAR_BIT (1<<17) |
| 131 | |
| 132 | #define NFC_TIMEOUT (1000) |
| 133 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 134 | /* |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 135 | * ECC status - seems to consume 8 bytes (double word). The documented |
| 136 | * status byte is located in the lowest byte of the second word (which is |
| 137 | * the 4th or 7th byte depending on endianness). |
| 138 | * Calculate an offset to store the ECC status at the end of the buffer. |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 139 | */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 140 | #define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8) |
| 141 | |
| 142 | #define ECC_STATUS 0x4 |
| 143 | #define ECC_STATUS_MASK 0x80 |
| 144 | #define ECC_STATUS_ERR_COUNT 0x3F |
| 145 | |
| 146 | enum vf610_nfc_alt_buf { |
| 147 | ALT_BUF_DATA = 0, |
| 148 | ALT_BUF_ID = 1, |
| 149 | ALT_BUF_STAT = 2, |
| 150 | ALT_BUF_ONFI = 3, |
| 151 | }; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 152 | |
| 153 | struct vf610_nfc { |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 154 | struct nand_chip chip; |
| 155 | void __iomem *regs; |
| 156 | uint buf_offset; |
| 157 | int write_sz; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 158 | /* Status and ID are in alternate locations. */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 159 | enum vf610_nfc_alt_buf alt_buf; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 162 | #define mtd_to_nfc(_mtd) nand_get_controller_data(mtd_to_nand(_mtd)) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 163 | |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 164 | #if defined(CONFIG_SYS_NAND_VF610_NFC_45_ECC_BYTES) |
| 165 | #define ECC_HW_MODE ECC_45_BYTE |
| 166 | |
| 167 | static struct nand_ecclayout vf610_nfc_ecc = { |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 168 | .eccbytes = 45, |
| 169 | .eccpos = {19, 20, 21, 22, 23, |
| 170 | 24, 25, 26, 27, 28, 29, 30, 31, |
| 171 | 32, 33, 34, 35, 36, 37, 38, 39, |
| 172 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 173 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 174 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 175 | .oobfree = { |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 176 | {.offset = 2, |
| 177 | .length = 17} } |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 178 | }; |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 179 | #elif defined(CONFIG_SYS_NAND_VF610_NFC_60_ECC_BYTES) |
| 180 | #define ECC_HW_MODE ECC_60_BYTE |
| 181 | |
| 182 | static struct nand_ecclayout vf610_nfc_ecc = { |
| 183 | .eccbytes = 60, |
| 184 | .eccpos = { 4, 5, 6, 7, 8, 9, 10, 11, |
| 185 | 12, 13, 14, 15, 16, 17, 18, 19, |
| 186 | 20, 21, 22, 23, 24, 25, 26, 27, |
| 187 | 28, 29, 30, 31, 32, 33, 34, 35, |
| 188 | 36, 37, 38, 39, 40, 41, 42, 43, |
| 189 | 44, 45, 46, 47, 48, 49, 50, 51, |
| 190 | 52, 53, 54, 55, 56, 57, 58, 59, |
| 191 | 60, 61, 62, 63 }, |
| 192 | .oobfree = { |
| 193 | {.offset = 2, |
| 194 | .length = 2} } |
| 195 | }; |
| 196 | #endif |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 197 | |
| 198 | static inline u32 vf610_nfc_read(struct mtd_info *mtd, uint reg) |
| 199 | { |
| 200 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
| 201 | |
| 202 | return readl(nfc->regs + reg); |
| 203 | } |
| 204 | |
| 205 | static inline void vf610_nfc_write(struct mtd_info *mtd, uint reg, u32 val) |
| 206 | { |
| 207 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
| 208 | |
| 209 | writel(val, nfc->regs + reg); |
| 210 | } |
| 211 | |
| 212 | static inline void vf610_nfc_set(struct mtd_info *mtd, uint reg, u32 bits) |
| 213 | { |
| 214 | vf610_nfc_write(mtd, reg, vf610_nfc_read(mtd, reg) | bits); |
| 215 | } |
| 216 | |
| 217 | static inline void vf610_nfc_clear(struct mtd_info *mtd, uint reg, u32 bits) |
| 218 | { |
| 219 | vf610_nfc_write(mtd, reg, vf610_nfc_read(mtd, reg) & ~bits); |
| 220 | } |
| 221 | |
| 222 | static inline void vf610_nfc_set_field(struct mtd_info *mtd, u32 reg, |
| 223 | u32 mask, u32 shift, u32 val) |
| 224 | { |
| 225 | vf610_nfc_write(mtd, reg, |
| 226 | (vf610_nfc_read(mtd, reg) & (~mask)) | val << shift); |
| 227 | } |
| 228 | |
| 229 | static inline void vf610_nfc_memcpy(void *dst, const void *src, size_t n) |
| 230 | { |
| 231 | /* |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 232 | * Use this accessor for the internal SRAM buffers. On the ARM |
| 233 | * Freescale Vybrid SoC it's known that the driver can treat |
| 234 | * the SRAM buffer as if it's memory. Other platform might need |
| 235 | * to treat the buffers differently. |
| 236 | * |
| 237 | * For the time being, use memcpy |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 238 | */ |
| 239 | memcpy(dst, src, n); |
| 240 | } |
| 241 | |
| 242 | /* Clear flags for upcoming command */ |
| 243 | static inline void vf610_nfc_clear_status(void __iomem *regbase) |
| 244 | { |
| 245 | void __iomem *reg = regbase + NFC_IRQ_STATUS; |
| 246 | u32 tmp = __raw_readl(reg); |
| 247 | tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT; |
| 248 | __raw_writel(tmp, reg); |
| 249 | } |
| 250 | |
| 251 | /* Wait for complete operation */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 252 | static void vf610_nfc_done(struct mtd_info *mtd) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 253 | { |
| 254 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
| 255 | uint start; |
| 256 | |
| 257 | /* |
| 258 | * Barrier is needed after this write. This write need |
| 259 | * to be done before reading the next register the first |
| 260 | * time. |
| 261 | * vf610_nfc_set implicates such a barrier by using writel |
| 262 | * to write to the register. |
| 263 | */ |
| 264 | vf610_nfc_set(mtd, NFC_FLASH_CMD2, START_BIT); |
| 265 | |
| 266 | start = get_timer(0); |
| 267 | |
| 268 | while (!(vf610_nfc_read(mtd, NFC_IRQ_STATUS) & IDLE_IRQ_BIT)) { |
| 269 | if (get_timer(start) > NFC_TIMEOUT) { |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 270 | printf("Timeout while waiting for IDLE.\n"); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | } |
| 274 | vf610_nfc_clear_status(nfc->regs); |
| 275 | } |
| 276 | |
| 277 | static u8 vf610_nfc_get_id(struct mtd_info *mtd, int col) |
| 278 | { |
| 279 | u32 flash_id; |
| 280 | |
| 281 | if (col < 4) { |
| 282 | flash_id = vf610_nfc_read(mtd, NFC_FLASH_STATUS1); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 283 | flash_id >>= (3 - col) * 8; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 284 | } else { |
| 285 | flash_id = vf610_nfc_read(mtd, NFC_FLASH_STATUS2); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 286 | flash_id >>= 24; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 287 | } |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 288 | |
| 289 | return flash_id & 0xff; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static u8 vf610_nfc_get_status(struct mtd_info *mtd) |
| 293 | { |
| 294 | return vf610_nfc_read(mtd, NFC_FLASH_STATUS2) & STATUS_BYTE1_MASK; |
| 295 | } |
| 296 | |
| 297 | /* Single command */ |
| 298 | static void vf610_nfc_send_command(void __iomem *regbase, u32 cmd_byte1, |
| 299 | u32 cmd_code) |
| 300 | { |
| 301 | void __iomem *reg = regbase + NFC_FLASH_CMD2; |
| 302 | u32 tmp; |
| 303 | vf610_nfc_clear_status(regbase); |
| 304 | |
| 305 | tmp = __raw_readl(reg); |
| 306 | tmp &= ~(CMD_BYTE1_MASK | CMD_CODE_MASK | BUFNO_MASK); |
| 307 | tmp |= cmd_byte1 << CMD_BYTE1_SHIFT; |
| 308 | tmp |= cmd_code << CMD_CODE_SHIFT; |
| 309 | __raw_writel(tmp, reg); |
| 310 | } |
| 311 | |
| 312 | /* Two commands */ |
| 313 | static void vf610_nfc_send_commands(void __iomem *regbase, u32 cmd_byte1, |
| 314 | u32 cmd_byte2, u32 cmd_code) |
| 315 | { |
| 316 | void __iomem *reg = regbase + NFC_FLASH_CMD1; |
| 317 | u32 tmp; |
| 318 | vf610_nfc_send_command(regbase, cmd_byte1, cmd_code); |
| 319 | |
| 320 | tmp = __raw_readl(reg); |
| 321 | tmp &= ~CMD_BYTE2_MASK; |
| 322 | tmp |= cmd_byte2 << CMD_BYTE2_SHIFT; |
| 323 | __raw_writel(tmp, reg); |
| 324 | } |
| 325 | |
| 326 | static void vf610_nfc_addr_cycle(struct mtd_info *mtd, int column, int page) |
| 327 | { |
| 328 | if (column != -1) { |
| 329 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | 7056fee | 2015-05-08 19:07:09 +0200 | [diff] [blame] | 330 | if (nfc->chip.options & NAND_BUSWIDTH_16) |
| 331 | column = column / 2; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 332 | vf610_nfc_set_field(mtd, NFC_COL_ADDR, COL_ADDR_MASK, |
| 333 | COL_ADDR_SHIFT, column); |
| 334 | } |
| 335 | if (page != -1) |
| 336 | vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK, |
| 337 | ROW_ADDR_SHIFT, page); |
| 338 | } |
| 339 | |
Stefan Agner | 7056fee | 2015-05-08 19:07:09 +0200 | [diff] [blame] | 340 | static inline void vf610_nfc_ecc_mode(struct mtd_info *mtd, int ecc_mode) |
| 341 | { |
| 342 | vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG, |
| 343 | CONFIG_ECC_MODE_MASK, |
| 344 | CONFIG_ECC_MODE_SHIFT, ecc_mode); |
| 345 | } |
| 346 | |
Stefan Agner | 11f1fa0 | 2015-03-24 17:54:20 +0100 | [diff] [blame] | 347 | static inline void vf610_nfc_transfer_size(void __iomem *regbase, int size) |
| 348 | { |
| 349 | __raw_writel(size, regbase + NFC_SECTOR_SIZE); |
| 350 | } |
| 351 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 352 | /* Send command to NAND chip */ |
| 353 | static void vf610_nfc_command(struct mtd_info *mtd, unsigned command, |
| 354 | int column, int page) |
| 355 | { |
| 356 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 357 | int trfr_sz = nfc->chip.options & NAND_BUSWIDTH_16 ? 1 : 0; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 358 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 359 | nfc->buf_offset = max(column, 0); |
| 360 | nfc->alt_buf = ALT_BUF_DATA; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 361 | |
| 362 | switch (command) { |
Stefan Agner | 0421341 | 2015-05-08 19:07:07 +0200 | [diff] [blame] | 363 | case NAND_CMD_SEQIN: |
| 364 | /* Use valid column/page from preread... */ |
| 365 | vf610_nfc_addr_cycle(mtd, column, page); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 366 | nfc->buf_offset = 0; |
| 367 | |
Stefan Agner | 0421341 | 2015-05-08 19:07:07 +0200 | [diff] [blame] | 368 | /* |
| 369 | * SEQIN => data => PAGEPROG sequence is done by the controller |
| 370 | * hence we do not need to issue the command here... |
| 371 | */ |
| 372 | return; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 373 | case NAND_CMD_PAGEPROG: |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 374 | trfr_sz += nfc->write_sz; |
| 375 | vf610_nfc_ecc_mode(mtd, ECC_HW_MODE); |
| 376 | vf610_nfc_transfer_size(nfc->regs, trfr_sz); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 377 | vf610_nfc_send_commands(nfc->regs, NAND_CMD_SEQIN, |
| 378 | command, PROGRAM_PAGE_CMD_CODE); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case NAND_CMD_RESET: |
Stefan Agner | 11f1fa0 | 2015-03-24 17:54:20 +0100 | [diff] [blame] | 382 | vf610_nfc_transfer_size(nfc->regs, 0); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 383 | vf610_nfc_send_command(nfc->regs, command, RESET_CMD_CODE); |
| 384 | break; |
Stefan Agner | 7056fee | 2015-05-08 19:07:09 +0200 | [diff] [blame] | 385 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 386 | case NAND_CMD_READOOB: |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 387 | trfr_sz += mtd->oobsize; |
Stefan Agner | 7056fee | 2015-05-08 19:07:09 +0200 | [diff] [blame] | 388 | column = mtd->writesize; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 389 | vf610_nfc_transfer_size(nfc->regs, trfr_sz); |
Stefan Agner | 7056fee | 2015-05-08 19:07:09 +0200 | [diff] [blame] | 390 | vf610_nfc_send_commands(nfc->regs, NAND_CMD_READ0, |
| 391 | NAND_CMD_READSTART, READ_PAGE_CMD_CODE); |
| 392 | vf610_nfc_addr_cycle(mtd, column, page); |
| 393 | vf610_nfc_ecc_mode(mtd, ECC_BYPASS); |
| 394 | break; |
| 395 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 396 | case NAND_CMD_READ0: |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 397 | trfr_sz += mtd->writesize + mtd->oobsize; |
| 398 | vf610_nfc_transfer_size(nfc->regs, trfr_sz); |
| 399 | vf610_nfc_ecc_mode(mtd, ECC_HW_MODE); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 400 | vf610_nfc_send_commands(nfc->regs, NAND_CMD_READ0, |
| 401 | NAND_CMD_READSTART, READ_PAGE_CMD_CODE); |
| 402 | vf610_nfc_addr_cycle(mtd, column, page); |
| 403 | break; |
| 404 | |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 405 | case NAND_CMD_PARAM: |
| 406 | nfc->alt_buf = ALT_BUF_ONFI; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 407 | trfr_sz = 3 * sizeof(struct nand_onfi_params); |
| 408 | vf610_nfc_transfer_size(nfc->regs, trfr_sz); |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 409 | vf610_nfc_send_command(nfc->regs, NAND_CMD_PARAM, |
| 410 | READ_ONFI_PARAM_CMD_CODE); |
| 411 | vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK, |
| 412 | ROW_ADDR_SHIFT, column); |
| 413 | vf610_nfc_ecc_mode(mtd, ECC_BYPASS); |
| 414 | break; |
| 415 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 416 | case NAND_CMD_ERASE1: |
Stefan Agner | 11f1fa0 | 2015-03-24 17:54:20 +0100 | [diff] [blame] | 417 | vf610_nfc_transfer_size(nfc->regs, 0); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 418 | vf610_nfc_send_commands(nfc->regs, command, |
| 419 | NAND_CMD_ERASE2, ERASE_CMD_CODE); |
| 420 | vf610_nfc_addr_cycle(mtd, column, page); |
| 421 | break; |
| 422 | |
| 423 | case NAND_CMD_READID: |
| 424 | nfc->alt_buf = ALT_BUF_ID; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 425 | nfc->buf_offset = 0; |
Stefan Agner | 11f1fa0 | 2015-03-24 17:54:20 +0100 | [diff] [blame] | 426 | vf610_nfc_transfer_size(nfc->regs, 0); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 427 | vf610_nfc_send_command(nfc->regs, command, READ_ID_CMD_CODE); |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 428 | vf610_nfc_set_field(mtd, NFC_ROW_ADDR, ROW_ADDR_MASK, |
| 429 | ROW_ADDR_SHIFT, column); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 430 | break; |
| 431 | |
| 432 | case NAND_CMD_STATUS: |
| 433 | nfc->alt_buf = ALT_BUF_STAT; |
Stefan Agner | 11f1fa0 | 2015-03-24 17:54:20 +0100 | [diff] [blame] | 434 | vf610_nfc_transfer_size(nfc->regs, 0); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 435 | vf610_nfc_send_command(nfc->regs, command, STATUS_READ_CMD_CODE); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 436 | break; |
| 437 | default: |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | vf610_nfc_done(mtd); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 442 | |
| 443 | nfc->write_sz = 0; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 446 | /* Read data from NFC buffers */ |
| 447 | static void vf610_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 448 | { |
| 449 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 450 | uint c = nfc->buf_offset; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 451 | |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 452 | /* Alternate buffers are only supported through read_byte */ |
| 453 | if (nfc->alt_buf) |
| 454 | return; |
| 455 | |
| 456 | vf610_nfc_memcpy(buf, nfc->regs + NFC_MAIN_AREA(0) + c, len); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 457 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 458 | nfc->buf_offset += len; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Write data to NFC buffers */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 462 | static void vf610_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 463 | int len) |
| 464 | { |
| 465 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 466 | uint c = nfc->buf_offset; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 467 | uint l; |
| 468 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 469 | l = min_t(uint, len, mtd->writesize + mtd->oobsize - c); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 470 | vf610_nfc_memcpy(nfc->regs + NFC_MAIN_AREA(0) + c, buf, l); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 471 | |
| 472 | nfc->write_sz += l; |
| 473 | nfc->buf_offset += l; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Read byte from NFC buffers */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 477 | static uint8_t vf610_nfc_read_byte(struct mtd_info *mtd) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 478 | { |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 479 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 480 | u8 tmp; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 481 | uint c = nfc->buf_offset; |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 482 | |
| 483 | switch (nfc->alt_buf) { |
| 484 | case ALT_BUF_ID: |
| 485 | tmp = vf610_nfc_get_id(mtd, c); |
| 486 | break; |
| 487 | case ALT_BUF_STAT: |
| 488 | tmp = vf610_nfc_get_status(mtd); |
| 489 | break; |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 490 | #ifdef __LITTLE_ENDIAN |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 491 | case ALT_BUF_ONFI: |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 492 | /* Reverse byte since the controller uses big endianness */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 493 | c = nfc->buf_offset ^ 0x3; |
| 494 | /* fall-through */ |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 495 | #endif |
| 496 | default: |
| 497 | tmp = *((u8 *)(nfc->regs + NFC_MAIN_AREA(0) + c)); |
| 498 | break; |
| 499 | } |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 500 | nfc->buf_offset++; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 501 | return tmp; |
| 502 | } |
| 503 | |
| 504 | /* Read word from NFC buffers */ |
| 505 | static u16 vf610_nfc_read_word(struct mtd_info *mtd) |
| 506 | { |
| 507 | u16 tmp; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 508 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 509 | vf610_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp)); |
| 510 | return tmp; |
| 511 | } |
| 512 | |
| 513 | /* If not provided, upper layers apply a fixed delay. */ |
| 514 | static int vf610_nfc_dev_ready(struct mtd_info *mtd) |
| 515 | { |
| 516 | /* NFC handles R/B internally; always ready. */ |
| 517 | return 1; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * This function supports Vybrid only (MPC5125 would have full RB and four CS) |
| 522 | */ |
| 523 | static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip) |
| 524 | { |
| 525 | #ifdef CONFIG_VF610 |
| 526 | u32 tmp = vf610_nfc_read(mtd, NFC_ROW_ADDR); |
| 527 | tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 528 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 529 | if (chip >= 0) { |
| 530 | tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT; |
| 531 | tmp |= (1 << chip) << ROW_ADDR_CHIP_SEL_SHIFT; |
| 532 | } |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 533 | |
| 534 | vf610_nfc_write(mtd, NFC_ROW_ADDR, tmp); |
| 535 | #endif |
| 536 | } |
| 537 | |
| 538 | /* Count the number of 0's in buff upto max_bits */ |
| 539 | static inline int count_written_bits(uint8_t *buff, int size, int max_bits) |
| 540 | { |
| 541 | uint32_t *buff32 = (uint32_t *)buff; |
| 542 | int k, written_bits = 0; |
| 543 | |
| 544 | for (k = 0; k < (size / 4); k++) { |
| 545 | written_bits += hweight32(~buff32[k]); |
| 546 | if (written_bits > max_bits) |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | return written_bits; |
| 551 | } |
| 552 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 553 | static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat, |
| 554 | uint8_t *oob, int page) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 555 | { |
| 556 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 557 | u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 558 | u8 ecc_status; |
| 559 | u8 ecc_count; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 560 | int flips; |
| 561 | int flips_threshold = nfc->chip.ecc.strength / 2; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 562 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 563 | ecc_status = vf610_nfc_read(mtd, ecc_status_off) & 0xff; |
| 564 | ecc_count = ecc_status & ECC_STATUS_ERR_COUNT; |
| 565 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 566 | if (!(ecc_status & ECC_STATUS_MASK)) |
| 567 | return ecc_count; |
| 568 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 569 | /* Read OOB without ECC unit enabled */ |
| 570 | vf610_nfc_command(mtd, NAND_CMD_READOOB, 0, page); |
| 571 | vf610_nfc_read_buf(mtd, oob, mtd->oobsize); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 572 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 573 | /* |
| 574 | * On an erased page, bit count (including OOB) should be zero or |
| 575 | * at least less then half of the ECC strength. |
| 576 | */ |
| 577 | flips = count_written_bits(dat, nfc->chip.ecc.size, flips_threshold); |
| 578 | flips += count_written_bits(oob, mtd->oobsize, flips_threshold); |
| 579 | |
| 580 | if (unlikely(flips > flips_threshold)) |
| 581 | return -EINVAL; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 582 | |
| 583 | /* Erased page. */ |
| 584 | memset(dat, 0xff, nfc->chip.ecc.size); |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 585 | memset(oob, 0xff, mtd->oobsize); |
| 586 | return flips; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 589 | static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 590 | uint8_t *buf, int oob_required, int page) |
| 591 | { |
| 592 | int eccsize = chip->ecc.size; |
| 593 | int stat; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 594 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 595 | vf610_nfc_read_buf(mtd, buf, eccsize); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 596 | if (oob_required) |
| 597 | vf610_nfc_read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 598 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 599 | stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 600 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 601 | if (stat < 0) { |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 602 | mtd->ecc_stats.failed++; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 603 | return 0; |
| 604 | } else { |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 605 | mtd->ecc_stats.corrected += stat; |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 606 | return stat; |
| 607 | } |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* |
| 611 | * ECC will be calculated automatically |
| 612 | */ |
| 613 | static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 614 | const uint8_t *buf, int oob_required, int page) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 615 | { |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 616 | struct vf610_nfc *nfc = mtd_to_nfc(mtd); |
| 617 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 618 | vf610_nfc_write_buf(mtd, buf, mtd->writesize); |
| 619 | if (oob_required) |
| 620 | vf610_nfc_write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 621 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 622 | /* Always write whole page including OOB due to HW ECC */ |
| 623 | nfc->write_sz = mtd->writesize + mtd->oobsize; |
| 624 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | struct vf610_nfc_config { |
| 629 | int hardware_ecc; |
| 630 | int width; |
| 631 | int flash_bbt; |
| 632 | }; |
| 633 | |
| 634 | static int vf610_nfc_nand_init(int devnum, void __iomem *addr) |
| 635 | { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 636 | struct mtd_info *mtd; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 637 | struct nand_chip *chip; |
| 638 | struct vf610_nfc *nfc; |
| 639 | int err = 0; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 640 | struct vf610_nfc_config cfg = { |
| 641 | .hardware_ecc = 1, |
| 642 | #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT |
| 643 | .width = 16, |
| 644 | #else |
| 645 | .width = 8, |
| 646 | #endif |
| 647 | .flash_bbt = 1, |
| 648 | }; |
| 649 | |
Lukasz Majewski | e94930b | 2018-12-03 10:20:47 +0100 | [diff] [blame] | 650 | nfc = calloc(1, sizeof(*nfc)); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 651 | if (!nfc) { |
| 652 | printf(KERN_ERR "%s: Memory exhausted!\n", __func__); |
| 653 | return -ENOMEM; |
| 654 | } |
| 655 | |
| 656 | chip = &nfc->chip; |
| 657 | nfc->regs = addr; |
| 658 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 659 | mtd = nand_to_mtd(chip); |
| 660 | nand_set_controller_data(chip, nfc); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 661 | |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 662 | if (cfg.width == 16) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 663 | chip->options |= NAND_BUSWIDTH_16; |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 664 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 665 | chip->dev_ready = vf610_nfc_dev_ready; |
| 666 | chip->cmdfunc = vf610_nfc_command; |
| 667 | chip->read_byte = vf610_nfc_read_byte; |
| 668 | chip->read_word = vf610_nfc_read_word; |
| 669 | chip->read_buf = vf610_nfc_read_buf; |
| 670 | chip->write_buf = vf610_nfc_write_buf; |
| 671 | chip->select_chip = vf610_nfc_select_chip; |
| 672 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 673 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
| 674 | |
| 675 | chip->ecc.size = PAGE_2K; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 676 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 677 | /* Set configuration register. */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 678 | vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_16BIT); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 679 | vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT); |
| 680 | vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT); |
| 681 | vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT); |
| 682 | vf610_nfc_clear(mtd, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT); |
| 683 | vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT); |
| 684 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 685 | /* Disable virtual pages, only one elementary transfer unit */ |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 686 | vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK, |
| 687 | CONFIG_PAGE_CNT_SHIFT, 1); |
| 688 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 689 | /* first scan to find the device and get the page size */ |
| 690 | if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL)) { |
| 691 | err = -ENXIO; |
| 692 | goto error; |
| 693 | } |
| 694 | |
Stefan Agner | 4ce682a | 2015-05-08 19:07:13 +0200 | [diff] [blame] | 695 | if (cfg.width == 16) |
| 696 | vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_16BIT); |
| 697 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 698 | /* Bad block options. */ |
| 699 | if (cfg.flash_bbt) |
| 700 | chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB | |
| 701 | NAND_BBT_CREATE; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 702 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 703 | /* Single buffer only, max 256 OOB minus ECC status */ |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 704 | if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) { |
| 705 | dev_err(nfc->dev, "Unsupported flash page size\n"); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 706 | err = -ENXIO; |
| 707 | goto error; |
| 708 | } |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 709 | |
| 710 | if (cfg.hardware_ecc) { |
| 711 | if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) { |
| 712 | dev_err(nfc->dev, "Unsupported flash with hwecc\n"); |
| 713 | err = -ENXIO; |
| 714 | goto error; |
| 715 | } |
| 716 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 717 | if (chip->ecc.size != mtd->writesize) { |
| 718 | dev_err(nfc->dev, "ecc size: %d\n", chip->ecc.size); |
| 719 | dev_err(nfc->dev, "Step size needs to be page size\n"); |
| 720 | err = -ENXIO; |
| 721 | goto error; |
| 722 | } |
| 723 | |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 724 | /* Current HW ECC layouts only use 64 bytes of OOB */ |
| 725 | if (mtd->oobsize > 64) |
| 726 | mtd->oobsize = 64; |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 727 | |
| 728 | /* propagate ecc.layout to mtd_info */ |
| 729 | mtd->ecclayout = chip->ecc.layout; |
| 730 | chip->ecc.read_page = vf610_nfc_read_page; |
| 731 | chip->ecc.write_page = vf610_nfc_write_page; |
| 732 | chip->ecc.mode = NAND_ECC_HW; |
| 733 | |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 734 | chip->ecc.size = PAGE_2K; |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 735 | chip->ecc.layout = &vf610_nfc_ecc; |
| 736 | #if defined(CONFIG_SYS_NAND_VF610_NFC_45_ECC_BYTES) |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 737 | chip->ecc.strength = 24; |
Stefan Agner | fe10d3f | 2015-05-08 19:07:12 +0200 | [diff] [blame] | 738 | chip->ecc.bytes = 45; |
| 739 | #elif defined(CONFIG_SYS_NAND_VF610_NFC_60_ECC_BYTES) |
| 740 | chip->ecc.strength = 32; |
| 741 | chip->ecc.bytes = 60; |
| 742 | #endif |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 743 | |
Stefan Agner | 59ae13a | 2015-10-13 22:11:42 -0700 | [diff] [blame] | 744 | /* Set ECC_STATUS offset */ |
| 745 | vf610_nfc_set_field(mtd, NFC_FLASH_CONFIG, |
| 746 | CONFIG_ECC_SRAM_ADDR_MASK, |
| 747 | CONFIG_ECC_SRAM_ADDR_SHIFT, |
| 748 | ECC_SRAM_ADDR >> 3); |
| 749 | |
| 750 | /* Enable ECC status in SRAM */ |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 751 | vf610_nfc_set(mtd, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT); |
| 752 | } |
| 753 | |
| 754 | /* second phase scan */ |
| 755 | err = nand_scan_tail(mtd); |
| 756 | if (err) |
| 757 | return err; |
| 758 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 759 | err = nand_register(devnum, mtd); |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 760 | if (err) |
| 761 | return err; |
| 762 | |
| 763 | return 0; |
| 764 | |
| 765 | error: |
| 766 | return err; |
| 767 | } |
| 768 | |
Lukasz Majewski | a8f1a67 | 2018-12-03 10:24:51 +0100 | [diff] [blame] | 769 | #if CONFIG_NAND_VF610_NFC_DT |
| 770 | static const struct udevice_id vf610_nfc_dt_ids[] = { |
| 771 | { |
| 772 | .compatible = "fsl,vf610-nfc", |
| 773 | }, |
| 774 | { /* sentinel */ } |
| 775 | }; |
| 776 | |
| 777 | static int vf610_nfc_dt_probe(struct udevice *dev) |
| 778 | { |
| 779 | struct resource res; |
| 780 | int ret; |
| 781 | |
| 782 | ret = dev_read_resource(dev, 0, &res); |
| 783 | if (ret) |
| 784 | return ret; |
| 785 | |
| 786 | return vf610_nfc_nand_init(0, devm_ioremap(dev, res.start, |
| 787 | resource_size(&res))); |
| 788 | } |
| 789 | |
| 790 | U_BOOT_DRIVER(vf610_nfc_dt) = { |
| 791 | .name = "vf610-nfc-dt", |
| 792 | .id = UCLASS_MTD, |
| 793 | .of_match = vf610_nfc_dt_ids, |
| 794 | .probe = vf610_nfc_dt_probe, |
| 795 | }; |
| 796 | |
| 797 | void board_nand_init(void) |
| 798 | { |
| 799 | struct udevice *dev; |
| 800 | int ret; |
| 801 | |
| 802 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
| 803 | DM_GET_DRIVER(vf610_nfc_dt), |
| 804 | &dev); |
| 805 | if (ret && ret != -ENODEV) |
| 806 | pr_err("Failed to initialize NAND controller. (error %d)\n", |
| 807 | ret); |
| 808 | } |
| 809 | #else |
Stefan Agner | d6b1ccb | 2014-09-12 13:06:35 +0200 | [diff] [blame] | 810 | void board_nand_init(void) |
| 811 | { |
| 812 | int err = vf610_nfc_nand_init(0, (void __iomem *)CONFIG_SYS_NAND_BASE); |
| 813 | if (err) |
| 814 | printf("VF610 NAND init failed (err %d)\n", err); |
| 815 | } |
Lukasz Majewski | a8f1a67 | 2018-12-03 10:24:51 +0100 | [diff] [blame] | 816 | #endif /* CONFIG_NAND_VF610_NFC_DT */ |