Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com> |
| 3 | * |
| 4 | * Derived from: |
| 5 | * https://github.com/yuq/sunxi-nfc-mtd |
| 6 | * Copyright (C) 2013 Qiang Yu <yuq825@gmail.com> |
| 7 | * |
| 8 | * https://github.com/hno/Allwinner-Info |
| 9 | * Copyright (C) 2013 Henrik Nordström <Henrik Nordström> |
| 10 | * |
| 11 | * Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com> |
| 12 | * Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/dma-mapping.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/of.h> |
| 31 | #include <linux/of_device.h> |
| 32 | #include <linux/of_gpio.h> |
| 33 | #include <linux/of_mtd.h> |
| 34 | #include <linux/mtd/mtd.h> |
| 35 | #include <linux/mtd/nand.h> |
| 36 | #include <linux/mtd/partitions.h> |
| 37 | #include <linux/clk.h> |
| 38 | #include <linux/delay.h> |
| 39 | #include <linux/dmaengine.h> |
| 40 | #include <linux/gpio.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/io.h> |
| 43 | |
| 44 | #define NFC_REG_CTL 0x0000 |
| 45 | #define NFC_REG_ST 0x0004 |
| 46 | #define NFC_REG_INT 0x0008 |
| 47 | #define NFC_REG_TIMING_CTL 0x000C |
| 48 | #define NFC_REG_TIMING_CFG 0x0010 |
| 49 | #define NFC_REG_ADDR_LOW 0x0014 |
| 50 | #define NFC_REG_ADDR_HIGH 0x0018 |
| 51 | #define NFC_REG_SECTOR_NUM 0x001C |
| 52 | #define NFC_REG_CNT 0x0020 |
| 53 | #define NFC_REG_CMD 0x0024 |
| 54 | #define NFC_REG_RCMD_SET 0x0028 |
| 55 | #define NFC_REG_WCMD_SET 0x002C |
| 56 | #define NFC_REG_IO_DATA 0x0030 |
| 57 | #define NFC_REG_ECC_CTL 0x0034 |
| 58 | #define NFC_REG_ECC_ST 0x0038 |
| 59 | #define NFC_REG_DEBUG 0x003C |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 60 | #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3) |
| 61 | #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4)) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 62 | #define NFC_REG_SPARE_AREA 0x00A0 |
| 63 | #define NFC_RAM0_BASE 0x0400 |
| 64 | #define NFC_RAM1_BASE 0x0800 |
| 65 | |
| 66 | /* define bit use in NFC_CTL */ |
| 67 | #define NFC_EN BIT(0) |
| 68 | #define NFC_RESET BIT(1) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 69 | #define NFC_BUS_WIDTH_MSK BIT(2) |
| 70 | #define NFC_BUS_WIDTH_8 (0 << 2) |
| 71 | #define NFC_BUS_WIDTH_16 (1 << 2) |
| 72 | #define NFC_RB_SEL_MSK BIT(3) |
| 73 | #define NFC_RB_SEL(x) ((x) << 3) |
| 74 | #define NFC_CE_SEL_MSK GENMASK(26, 24) |
| 75 | #define NFC_CE_SEL(x) ((x) << 24) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 76 | #define NFC_CE_CTL BIT(6) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 77 | #define NFC_PAGE_SHIFT_MSK GENMASK(11, 8) |
| 78 | #define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 79 | #define NFC_SAM BIT(12) |
| 80 | #define NFC_RAM_METHOD BIT(14) |
| 81 | #define NFC_DEBUG_CTL BIT(31) |
| 82 | |
| 83 | /* define bit use in NFC_ST */ |
| 84 | #define NFC_RB_B2R BIT(0) |
| 85 | #define NFC_CMD_INT_FLAG BIT(1) |
| 86 | #define NFC_DMA_INT_FLAG BIT(2) |
| 87 | #define NFC_CMD_FIFO_STATUS BIT(3) |
| 88 | #define NFC_STA BIT(4) |
| 89 | #define NFC_NATCH_INT_FLAG BIT(5) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 90 | #define NFC_RB_STATE(x) BIT(x + 8) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 91 | |
| 92 | /* define bit use in NFC_INT */ |
| 93 | #define NFC_B2R_INT_ENABLE BIT(0) |
| 94 | #define NFC_CMD_INT_ENABLE BIT(1) |
| 95 | #define NFC_DMA_INT_ENABLE BIT(2) |
| 96 | #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \ |
| 97 | NFC_CMD_INT_ENABLE | \ |
| 98 | NFC_DMA_INT_ENABLE) |
| 99 | |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 100 | /* define bit use in NFC_TIMING_CTL */ |
| 101 | #define NFC_TIMING_CTL_EDO BIT(8) |
| 102 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 103 | /* define NFC_TIMING_CFG register layout */ |
| 104 | #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \ |
| 105 | (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \ |
| 106 | (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \ |
| 107 | (((tCAD) & 0x7) << 8)) |
| 108 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 109 | /* define bit use in NFC_CMD */ |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 110 | #define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0) |
| 111 | #define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8) |
| 112 | #define NFC_CMD(x) (x) |
| 113 | #define NFC_ADR_NUM_MSK GENMASK(18, 16) |
| 114 | #define NFC_ADR_NUM(x) (((x) - 1) << 16) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 115 | #define NFC_SEND_ADR BIT(19) |
| 116 | #define NFC_ACCESS_DIR BIT(20) |
| 117 | #define NFC_DATA_TRANS BIT(21) |
| 118 | #define NFC_SEND_CMD1 BIT(22) |
| 119 | #define NFC_WAIT_FLAG BIT(23) |
| 120 | #define NFC_SEND_CMD2 BIT(24) |
| 121 | #define NFC_SEQ BIT(25) |
| 122 | #define NFC_DATA_SWAP_METHOD BIT(26) |
| 123 | #define NFC_ROW_AUTO_INC BIT(27) |
| 124 | #define NFC_SEND_CMD3 BIT(28) |
| 125 | #define NFC_SEND_CMD4 BIT(29) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 126 | #define NFC_CMD_TYPE_MSK GENMASK(31, 30) |
| 127 | #define NFC_NORMAL_OP (0 << 30) |
| 128 | #define NFC_ECC_OP (1 << 30) |
| 129 | #define NFC_PAGE_OP (2 << 30) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 130 | |
| 131 | /* define bit use in NFC_RCMD_SET */ |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 132 | #define NFC_READ_CMD_MSK GENMASK(7, 0) |
| 133 | #define NFC_RND_READ_CMD0_MSK GENMASK(15, 8) |
| 134 | #define NFC_RND_READ_CMD1_MSK GENMASK(23, 16) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 135 | |
| 136 | /* define bit use in NFC_WCMD_SET */ |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 137 | #define NFC_PROGRAM_CMD_MSK GENMASK(7, 0) |
| 138 | #define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8) |
| 139 | #define NFC_READ_CMD0_MSK GENMASK(23, 16) |
| 140 | #define NFC_READ_CMD1_MSK GENMASK(31, 24) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 141 | |
| 142 | /* define bit use in NFC_ECC_CTL */ |
| 143 | #define NFC_ECC_EN BIT(0) |
| 144 | #define NFC_ECC_PIPELINE BIT(3) |
| 145 | #define NFC_ECC_EXCEPTION BIT(4) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 146 | #define NFC_ECC_BLOCK_SIZE_MSK BIT(5) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 147 | #define NFC_RANDOM_EN BIT(9) |
| 148 | #define NFC_RANDOM_DIRECTION BIT(10) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 149 | #define NFC_ECC_MODE_MSK GENMASK(15, 12) |
| 150 | #define NFC_ECC_MODE(x) ((x) << 12) |
| 151 | #define NFC_RANDOM_SEED_MSK GENMASK(30, 16) |
| 152 | #define NFC_RANDOM_SEED(x) ((x) << 16) |
| 153 | |
| 154 | /* define bit use in NFC_ECC_ST */ |
| 155 | #define NFC_ECC_ERR(x) BIT(x) |
| 156 | #define NFC_ECC_PAT_FOUND(x) BIT(x + 16) |
| 157 | #define NFC_ECC_ERR_CNT(b, x) (((x) >> ((b) * 8)) & 0xff) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 158 | |
| 159 | #define NFC_DEFAULT_TIMEOUT_MS 1000 |
| 160 | |
| 161 | #define NFC_SRAM_SIZE 1024 |
| 162 | |
| 163 | #define NFC_MAX_CS 7 |
| 164 | |
| 165 | /* |
| 166 | * Ready/Busy detection type: describes the Ready/Busy detection modes |
| 167 | * |
| 168 | * @RB_NONE: no external detection available, rely on STATUS command |
| 169 | * and software timeouts |
| 170 | * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy |
| 171 | * pin of the NAND flash chip must be connected to one of the |
| 172 | * native NAND R/B pins (those which can be muxed to the NAND |
| 173 | * Controller) |
| 174 | * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy |
| 175 | * pin of the NAND flash chip must be connected to a GPIO capable |
| 176 | * pin. |
| 177 | */ |
| 178 | enum sunxi_nand_rb_type { |
| 179 | RB_NONE, |
| 180 | RB_NATIVE, |
| 181 | RB_GPIO, |
| 182 | }; |
| 183 | |
| 184 | /* |
| 185 | * Ready/Busy structure: stores information related to Ready/Busy detection |
| 186 | * |
| 187 | * @type: the Ready/Busy detection mode |
| 188 | * @info: information related to the R/B detection mode. Either a gpio |
| 189 | * id or a native R/B id (those supported by the NAND controller). |
| 190 | */ |
| 191 | struct sunxi_nand_rb { |
| 192 | enum sunxi_nand_rb_type type; |
| 193 | union { |
| 194 | int gpio; |
| 195 | int nativeid; |
| 196 | } info; |
| 197 | }; |
| 198 | |
| 199 | /* |
| 200 | * Chip Select structure: stores information related to NAND Chip Select |
| 201 | * |
| 202 | * @cs: the NAND CS id used to communicate with a NAND Chip |
| 203 | * @rb: the Ready/Busy description |
| 204 | */ |
| 205 | struct sunxi_nand_chip_sel { |
| 206 | u8 cs; |
| 207 | struct sunxi_nand_rb rb; |
| 208 | }; |
| 209 | |
| 210 | /* |
| 211 | * sunxi HW ECC infos: stores information related to HW ECC support |
| 212 | * |
| 213 | * @mode: the sunxi ECC mode field deduced from ECC requirements |
| 214 | * @layout: the OOB layout depending on the ECC requirements and the |
| 215 | * selected ECC mode |
| 216 | */ |
| 217 | struct sunxi_nand_hw_ecc { |
| 218 | int mode; |
| 219 | struct nand_ecclayout layout; |
| 220 | }; |
| 221 | |
| 222 | /* |
| 223 | * NAND chip structure: stores NAND chip device related information |
| 224 | * |
| 225 | * @node: used to store NAND chips into a list |
| 226 | * @nand: base NAND chip structure |
| 227 | * @mtd: base MTD structure |
| 228 | * @clk_rate: clk_rate required for this NAND chip |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 229 | * @timing_cfg TIMING_CFG register value for this NAND chip |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 230 | * @selected: current active CS |
| 231 | * @nsels: number of CS lines required by the NAND chip |
| 232 | * @sels: array of CS lines descriptions |
| 233 | */ |
| 234 | struct sunxi_nand_chip { |
| 235 | struct list_head node; |
| 236 | struct nand_chip nand; |
| 237 | struct mtd_info mtd; |
| 238 | unsigned long clk_rate; |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 239 | u32 timing_cfg; |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 240 | u32 timing_ctl; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 241 | int selected; |
| 242 | int nsels; |
| 243 | struct sunxi_nand_chip_sel sels[0]; |
| 244 | }; |
| 245 | |
| 246 | static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand) |
| 247 | { |
| 248 | return container_of(nand, struct sunxi_nand_chip, nand); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * NAND Controller structure: stores sunxi NAND controller information |
| 253 | * |
| 254 | * @controller: base controller structure |
| 255 | * @dev: parent device (used to print error messages) |
| 256 | * @regs: NAND controller registers |
| 257 | * @ahb_clk: NAND Controller AHB clock |
| 258 | * @mod_clk: NAND Controller mod clock |
| 259 | * @assigned_cs: bitmask describing already assigned CS lines |
| 260 | * @clk_rate: NAND controller current clock rate |
| 261 | * @chips: a list containing all the NAND chips attached to |
| 262 | * this NAND controller |
| 263 | * @complete: a completion object used to wait for NAND |
| 264 | * controller events |
| 265 | */ |
| 266 | struct sunxi_nfc { |
| 267 | struct nand_hw_control controller; |
| 268 | struct device *dev; |
| 269 | void __iomem *regs; |
| 270 | struct clk *ahb_clk; |
| 271 | struct clk *mod_clk; |
| 272 | unsigned long assigned_cs; |
| 273 | unsigned long clk_rate; |
| 274 | struct list_head chips; |
| 275 | struct completion complete; |
| 276 | }; |
| 277 | |
| 278 | static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl) |
| 279 | { |
| 280 | return container_of(ctrl, struct sunxi_nfc, controller); |
| 281 | } |
| 282 | |
| 283 | static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id) |
| 284 | { |
| 285 | struct sunxi_nfc *nfc = dev_id; |
| 286 | u32 st = readl(nfc->regs + NFC_REG_ST); |
| 287 | u32 ien = readl(nfc->regs + NFC_REG_INT); |
| 288 | |
| 289 | if (!(ien & st)) |
| 290 | return IRQ_NONE; |
| 291 | |
| 292 | if ((ien & st) == ien) |
| 293 | complete(&nfc->complete); |
| 294 | |
| 295 | writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST); |
| 296 | writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT); |
| 297 | |
| 298 | return IRQ_HANDLED; |
| 299 | } |
| 300 | |
| 301 | static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags, |
| 302 | unsigned int timeout_ms) |
| 303 | { |
| 304 | init_completion(&nfc->complete); |
| 305 | |
| 306 | writel(flags, nfc->regs + NFC_REG_INT); |
| 307 | |
| 308 | if (!timeout_ms) |
| 309 | timeout_ms = NFC_DEFAULT_TIMEOUT_MS; |
| 310 | |
| 311 | if (!wait_for_completion_timeout(&nfc->complete, |
| 312 | msecs_to_jiffies(timeout_ms))) { |
| 313 | dev_err(nfc->dev, "wait interrupt timedout\n"); |
| 314 | return -ETIMEDOUT; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc) |
| 321 | { |
| 322 | unsigned long timeout = jiffies + |
| 323 | msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS); |
| 324 | |
| 325 | do { |
| 326 | if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS)) |
| 327 | return 0; |
| 328 | } while (time_before(jiffies, timeout)); |
| 329 | |
| 330 | dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n"); |
| 331 | return -ETIMEDOUT; |
| 332 | } |
| 333 | |
| 334 | static int sunxi_nfc_rst(struct sunxi_nfc *nfc) |
| 335 | { |
| 336 | unsigned long timeout = jiffies + |
| 337 | msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS); |
| 338 | |
| 339 | writel(0, nfc->regs + NFC_REG_ECC_CTL); |
| 340 | writel(NFC_RESET, nfc->regs + NFC_REG_CTL); |
| 341 | |
| 342 | do { |
| 343 | if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET)) |
| 344 | return 0; |
| 345 | } while (time_before(jiffies, timeout)); |
| 346 | |
| 347 | dev_err(nfc->dev, "wait for NAND controller reset timedout\n"); |
| 348 | return -ETIMEDOUT; |
| 349 | } |
| 350 | |
| 351 | static int sunxi_nfc_dev_ready(struct mtd_info *mtd) |
| 352 | { |
| 353 | struct nand_chip *nand = mtd->priv; |
| 354 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 355 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 356 | struct sunxi_nand_rb *rb; |
| 357 | unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20); |
| 358 | int ret; |
| 359 | |
| 360 | if (sunxi_nand->selected < 0) |
| 361 | return 0; |
| 362 | |
| 363 | rb = &sunxi_nand->sels[sunxi_nand->selected].rb; |
| 364 | |
| 365 | switch (rb->type) { |
| 366 | case RB_NATIVE: |
| 367 | ret = !!(readl(nfc->regs + NFC_REG_ST) & |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 368 | NFC_RB_STATE(rb->info.nativeid)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 369 | if (ret) |
| 370 | break; |
| 371 | |
| 372 | sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo); |
| 373 | ret = !!(readl(nfc->regs + NFC_REG_ST) & |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 374 | NFC_RB_STATE(rb->info.nativeid)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 375 | break; |
| 376 | case RB_GPIO: |
| 377 | ret = gpio_get_value(rb->info.gpio); |
| 378 | break; |
| 379 | case RB_NONE: |
| 380 | default: |
| 381 | ret = 0; |
| 382 | dev_err(nfc->dev, "cannot check R/B NAND status!\n"); |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip) |
| 390 | { |
| 391 | struct nand_chip *nand = mtd->priv; |
| 392 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 393 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 394 | struct sunxi_nand_chip_sel *sel; |
| 395 | u32 ctl; |
| 396 | |
| 397 | if (chip > 0 && chip >= sunxi_nand->nsels) |
| 398 | return; |
| 399 | |
| 400 | if (chip == sunxi_nand->selected) |
| 401 | return; |
| 402 | |
| 403 | ctl = readl(nfc->regs + NFC_REG_CTL) & |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 404 | ~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 405 | |
| 406 | if (chip >= 0) { |
| 407 | sel = &sunxi_nand->sels[chip]; |
| 408 | |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 409 | ctl |= NFC_CE_SEL(sel->cs) | NFC_EN | |
| 410 | NFC_PAGE_SHIFT(nand->page_shift - 10); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 411 | if (sel->rb.type == RB_NONE) { |
| 412 | nand->dev_ready = NULL; |
| 413 | } else { |
| 414 | nand->dev_ready = sunxi_nfc_dev_ready; |
| 415 | if (sel->rb.type == RB_NATIVE) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 416 | ctl |= NFC_RB_SEL(sel->rb.info.nativeid); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA); |
| 420 | |
| 421 | if (nfc->clk_rate != sunxi_nand->clk_rate) { |
| 422 | clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate); |
| 423 | nfc->clk_rate = sunxi_nand->clk_rate; |
| 424 | } |
| 425 | } |
| 426 | |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 427 | writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL); |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 428 | writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 429 | writel(ctl, nfc->regs + NFC_REG_CTL); |
| 430 | |
| 431 | sunxi_nand->selected = chip; |
| 432 | } |
| 433 | |
| 434 | static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 435 | { |
| 436 | struct nand_chip *nand = mtd->priv; |
| 437 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 438 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 439 | int ret; |
| 440 | int cnt; |
| 441 | int offs = 0; |
| 442 | u32 tmp; |
| 443 | |
| 444 | while (len > offs) { |
| 445 | cnt = min(len - offs, NFC_SRAM_SIZE); |
| 446 | |
| 447 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 448 | if (ret) |
| 449 | break; |
| 450 | |
| 451 | writel(cnt, nfc->regs + NFC_REG_CNT); |
| 452 | tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD; |
| 453 | writel(tmp, nfc->regs + NFC_REG_CMD); |
| 454 | |
| 455 | ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0); |
| 456 | if (ret) |
| 457 | break; |
| 458 | |
| 459 | if (buf) |
| 460 | memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE, |
| 461 | cnt); |
| 462 | offs += cnt; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 467 | int len) |
| 468 | { |
| 469 | struct nand_chip *nand = mtd->priv; |
| 470 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 471 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 472 | int ret; |
| 473 | int cnt; |
| 474 | int offs = 0; |
| 475 | u32 tmp; |
| 476 | |
| 477 | while (len > offs) { |
| 478 | cnt = min(len - offs, NFC_SRAM_SIZE); |
| 479 | |
| 480 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 481 | if (ret) |
| 482 | break; |
| 483 | |
| 484 | writel(cnt, nfc->regs + NFC_REG_CNT); |
| 485 | memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt); |
| 486 | tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 487 | NFC_ACCESS_DIR; |
| 488 | writel(tmp, nfc->regs + NFC_REG_CMD); |
| 489 | |
| 490 | ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0); |
| 491 | if (ret) |
| 492 | break; |
| 493 | |
| 494 | offs += cnt; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd) |
| 499 | { |
| 500 | uint8_t ret; |
| 501 | |
| 502 | sunxi_nfc_read_buf(mtd, &ret, 1); |
| 503 | |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat, |
| 508 | unsigned int ctrl) |
| 509 | { |
| 510 | struct nand_chip *nand = mtd->priv; |
| 511 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 512 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 513 | int ret; |
| 514 | u32 tmp; |
| 515 | |
| 516 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 517 | if (ret) |
| 518 | return; |
| 519 | |
| 520 | if (ctrl & NAND_CTRL_CHANGE) { |
| 521 | tmp = readl(nfc->regs + NFC_REG_CTL); |
| 522 | if (ctrl & NAND_NCE) |
| 523 | tmp |= NFC_CE_CTL; |
| 524 | else |
| 525 | tmp &= ~NFC_CE_CTL; |
| 526 | writel(tmp, nfc->regs + NFC_REG_CTL); |
| 527 | } |
| 528 | |
| 529 | if (dat == NAND_CMD_NONE) |
| 530 | return; |
| 531 | |
| 532 | if (ctrl & NAND_CLE) { |
| 533 | writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD); |
| 534 | } else { |
| 535 | writel(dat, nfc->regs + NFC_REG_ADDR_LOW); |
| 536 | writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD); |
| 537 | } |
| 538 | |
| 539 | sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0); |
| 540 | } |
| 541 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 542 | static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd) |
| 543 | { |
| 544 | struct nand_chip *nand = mtd->priv; |
| 545 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 546 | struct sunxi_nand_hw_ecc *data = nand->ecc.priv; |
| 547 | u32 ecc_ctl; |
| 548 | |
| 549 | ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); |
| 550 | ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | |
| 551 | NFC_ECC_BLOCK_SIZE_MSK); |
| 552 | ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION; |
| 553 | |
| 554 | writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL); |
| 555 | } |
| 556 | |
| 557 | static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd) |
| 558 | { |
| 559 | struct nand_chip *nand = mtd->priv; |
| 560 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 561 | |
| 562 | writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN, |
| 563 | nfc->regs + NFC_REG_ECC_CTL); |
| 564 | } |
| 565 | |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 566 | static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf) |
| 567 | { |
| 568 | buf[0] = user_data; |
| 569 | buf[1] = user_data >> 8; |
| 570 | buf[2] = user_data >> 16; |
| 571 | buf[3] = user_data >> 24; |
| 572 | } |
| 573 | |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 574 | static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd, |
| 575 | u8 *data, int data_off, |
| 576 | u8 *oob, int oob_off, |
| 577 | int *cur_off, |
| 578 | unsigned int *max_bitflips) |
| 579 | { |
| 580 | struct nand_chip *nand = mtd->priv; |
| 581 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 582 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 583 | u32 status; |
| 584 | int ret; |
| 585 | |
| 586 | if (*cur_off != data_off) |
| 587 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1); |
| 588 | |
| 589 | sunxi_nfc_read_buf(mtd, data, ecc->size); |
| 590 | |
| 591 | if (data_off + ecc->bytes != oob_off) |
| 592 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1); |
| 593 | |
| 594 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 595 | if (ret) |
| 596 | return ret; |
| 597 | |
| 598 | writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP, |
| 599 | nfc->regs + NFC_REG_CMD); |
| 600 | |
| 601 | ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | |
| 605 | status = readl(nfc->regs + NFC_REG_ECC_ST); |
| 606 | ret = NFC_ECC_ERR_CNT(0, readl(nfc->regs + NFC_REG_ECC_ERR_CNT(0))); |
| 607 | |
| 608 | memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size); |
| 609 | |
| 610 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1); |
| 611 | sunxi_nfc_read_buf(mtd, oob, ecc->bytes + 4); |
| 612 | |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 613 | if (status & NFC_ECC_ERR(0)) { |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 614 | ret = -EIO; |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 615 | } else { |
| 616 | /* |
| 617 | * The engine protects 4 bytes of OOB data per chunk. |
| 618 | * Retrieve the corrected OOB bytes. |
| 619 | */ |
| 620 | sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(0)), |
| 621 | oob); |
| 622 | } |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 623 | |
| 624 | if (ret < 0) { |
| 625 | mtd->ecc_stats.failed++; |
| 626 | } else { |
| 627 | mtd->ecc_stats.corrected += ret; |
| 628 | *max_bitflips = max_t(unsigned int, *max_bitflips, ret); |
| 629 | } |
| 630 | |
| 631 | *cur_off = oob_off + ecc->bytes + 4; |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 636 | static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd, |
| 637 | u8 *oob, int *cur_off) |
| 638 | { |
| 639 | struct nand_chip *nand = mtd->priv; |
| 640 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 641 | int offset = ((ecc->bytes + 4) * ecc->steps); |
| 642 | int len = mtd->oobsize - offset; |
| 643 | |
| 644 | if (len <= 0) |
| 645 | return; |
| 646 | |
| 647 | if (*cur_off != offset) |
| 648 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 649 | offset + mtd->writesize, -1); |
| 650 | |
| 651 | sunxi_nfc_read_buf(mtd, oob + offset, len); |
| 652 | |
| 653 | *cur_off = mtd->oobsize + mtd->writesize; |
| 654 | } |
| 655 | |
Boris BREZILLON | 23151fd | 2015-09-30 23:45:28 +0200 | [diff] [blame^] | 656 | static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf) |
| 657 | { |
| 658 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 659 | } |
| 660 | |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 661 | static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd, |
| 662 | const u8 *data, int data_off, |
| 663 | const u8 *oob, int oob_off, |
| 664 | int *cur_off) |
| 665 | { |
| 666 | struct nand_chip *nand = mtd->priv; |
| 667 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 668 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 669 | int ret; |
| 670 | |
| 671 | if (data_off != *cur_off) |
| 672 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, data_off, -1); |
| 673 | |
| 674 | sunxi_nfc_write_buf(mtd, data, ecc->size); |
| 675 | |
| 676 | /* Fill OOB data in */ |
Boris BREZILLON | 23151fd | 2015-09-30 23:45:28 +0200 | [diff] [blame^] | 677 | writel(sunxi_nfc_buf_to_user_data(oob), |
| 678 | nfc->regs + NFC_REG_USER_DATA(0)); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 679 | |
| 680 | if (data_off + ecc->bytes != oob_off) |
| 681 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, oob_off, -1); |
| 682 | |
| 683 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 684 | if (ret) |
| 685 | return ret; |
| 686 | |
| 687 | writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 688 | NFC_ACCESS_DIR | NFC_ECC_OP, |
| 689 | nfc->regs + NFC_REG_CMD); |
| 690 | |
| 691 | ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0); |
| 692 | if (ret) |
| 693 | return ret; |
| 694 | |
| 695 | *cur_off = oob_off + ecc->bytes + 4; |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 700 | static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd, |
| 701 | u8 *oob, int *cur_off) |
| 702 | { |
| 703 | struct nand_chip *nand = mtd->priv; |
| 704 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 705 | int offset = ((ecc->bytes + 4) * ecc->steps); |
| 706 | int len = mtd->oobsize - offset; |
| 707 | |
| 708 | if (len <= 0) |
| 709 | return; |
| 710 | |
| 711 | if (*cur_off != offset) |
| 712 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, |
| 713 | offset + mtd->writesize, -1); |
| 714 | |
| 715 | sunxi_nfc_write_buf(mtd, oob + offset, len); |
| 716 | |
| 717 | *cur_off = mtd->oobsize + mtd->writesize; |
| 718 | } |
| 719 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 720 | static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd, |
| 721 | struct nand_chip *chip, uint8_t *buf, |
| 722 | int oob_required, int page) |
| 723 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 724 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 725 | unsigned int max_bitflips = 0; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 726 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 727 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 728 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 729 | |
| 730 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 731 | int data_off = i * ecc->size; |
| 732 | int oob_off = i * (ecc->bytes + 4); |
| 733 | u8 *data = buf + data_off; |
| 734 | u8 *oob = chip->oob_poi + oob_off; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 735 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 736 | ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob, |
| 737 | oob_off + mtd->writesize, |
| 738 | &cur_off, &max_bitflips); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 739 | if (ret) |
| 740 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 743 | if (oob_required) |
| 744 | sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 745 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 746 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 747 | |
| 748 | return max_bitflips; |
| 749 | } |
| 750 | |
| 751 | static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, |
| 752 | struct nand_chip *chip, |
| 753 | const uint8_t *buf, int oob_required) |
| 754 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 755 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 756 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 757 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 758 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 759 | |
| 760 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 761 | int data_off = i * ecc->size; |
| 762 | int oob_off = i * (ecc->bytes + 4); |
| 763 | const u8 *data = buf + data_off; |
| 764 | const u8 *oob = chip->oob_poi + oob_off; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 765 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 766 | ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob, |
| 767 | oob_off + mtd->writesize, |
| 768 | &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 769 | if (ret) |
| 770 | return ret; |
| 771 | } |
| 772 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 773 | if (oob_required) |
| 774 | sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi, &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 775 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 776 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd, |
| 782 | struct nand_chip *chip, |
| 783 | uint8_t *buf, int oob_required, |
| 784 | int page) |
| 785 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 786 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 787 | unsigned int max_bitflips = 0; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 788 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 789 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 790 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 791 | |
| 792 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 793 | int data_off = i * (ecc->size + ecc->bytes + 4); |
| 794 | int oob_off = data_off + ecc->size; |
| 795 | u8 *data = buf + (i * ecc->size); |
| 796 | u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 797 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 798 | ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob, |
| 799 | oob_off, &cur_off, |
| 800 | &max_bitflips); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 801 | if (ret) |
| 802 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 805 | if (oob_required) |
| 806 | sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 807 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 808 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 809 | |
| 810 | return max_bitflips; |
| 811 | } |
| 812 | |
| 813 | static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd, |
| 814 | struct nand_chip *chip, |
| 815 | const uint8_t *buf, |
| 816 | int oob_required) |
| 817 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 818 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 819 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 820 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 821 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 822 | |
| 823 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 824 | int data_off = i * (ecc->size + ecc->bytes + 4); |
| 825 | int oob_off = data_off + ecc->size; |
| 826 | const u8 *data = buf + (i * ecc->size); |
| 827 | const u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 828 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 829 | ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, |
| 830 | oob, oob_off, &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 831 | if (ret) |
| 832 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 835 | if (oob_required) |
| 836 | sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi, &cur_off); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 837 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 838 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 843 | static const s32 tWB_lut[] = {6, 12, 16, 20}; |
| 844 | static const s32 tRHW_lut[] = {4, 8, 12, 20}; |
| 845 | |
| 846 | static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration, |
| 847 | u32 clk_period) |
| 848 | { |
| 849 | u32 clk_cycles = DIV_ROUND_UP(duration, clk_period); |
| 850 | int i; |
| 851 | |
| 852 | for (i = 0; i < lut_size; i++) { |
| 853 | if (clk_cycles <= lut[i]) |
| 854 | return i; |
| 855 | } |
| 856 | |
| 857 | /* Doesn't fit */ |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | #define sunxi_nand_lookup_timing(l, p, c) \ |
| 862 | _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c) |
| 863 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 864 | static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip, |
| 865 | const struct nand_sdr_timings *timings) |
| 866 | { |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 867 | struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 868 | u32 min_clk_period = 0; |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 869 | s32 tWB, tADL, tWHR, tRHW, tCAD; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 870 | |
| 871 | /* T1 <=> tCLS */ |
| 872 | if (timings->tCLS_min > min_clk_period) |
| 873 | min_clk_period = timings->tCLS_min; |
| 874 | |
| 875 | /* T2 <=> tCLH */ |
| 876 | if (timings->tCLH_min > min_clk_period) |
| 877 | min_clk_period = timings->tCLH_min; |
| 878 | |
| 879 | /* T3 <=> tCS */ |
| 880 | if (timings->tCS_min > min_clk_period) |
| 881 | min_clk_period = timings->tCS_min; |
| 882 | |
| 883 | /* T4 <=> tCH */ |
| 884 | if (timings->tCH_min > min_clk_period) |
| 885 | min_clk_period = timings->tCH_min; |
| 886 | |
| 887 | /* T5 <=> tWP */ |
| 888 | if (timings->tWP_min > min_clk_period) |
| 889 | min_clk_period = timings->tWP_min; |
| 890 | |
| 891 | /* T6 <=> tWH */ |
| 892 | if (timings->tWH_min > min_clk_period) |
| 893 | min_clk_period = timings->tWH_min; |
| 894 | |
| 895 | /* T7 <=> tALS */ |
| 896 | if (timings->tALS_min > min_clk_period) |
| 897 | min_clk_period = timings->tALS_min; |
| 898 | |
| 899 | /* T8 <=> tDS */ |
| 900 | if (timings->tDS_min > min_clk_period) |
| 901 | min_clk_period = timings->tDS_min; |
| 902 | |
| 903 | /* T9 <=> tDH */ |
| 904 | if (timings->tDH_min > min_clk_period) |
| 905 | min_clk_period = timings->tDH_min; |
| 906 | |
| 907 | /* T10 <=> tRR */ |
| 908 | if (timings->tRR_min > (min_clk_period * 3)) |
| 909 | min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3); |
| 910 | |
| 911 | /* T11 <=> tALH */ |
| 912 | if (timings->tALH_min > min_clk_period) |
| 913 | min_clk_period = timings->tALH_min; |
| 914 | |
| 915 | /* T12 <=> tRP */ |
| 916 | if (timings->tRP_min > min_clk_period) |
| 917 | min_clk_period = timings->tRP_min; |
| 918 | |
| 919 | /* T13 <=> tREH */ |
| 920 | if (timings->tREH_min > min_clk_period) |
| 921 | min_clk_period = timings->tREH_min; |
| 922 | |
| 923 | /* T14 <=> tRC */ |
| 924 | if (timings->tRC_min > (min_clk_period * 2)) |
| 925 | min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2); |
| 926 | |
| 927 | /* T15 <=> tWC */ |
| 928 | if (timings->tWC_min > (min_clk_period * 2)) |
| 929 | min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2); |
| 930 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 931 | /* T16 - T19 + tCAD */ |
| 932 | tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max, |
| 933 | min_clk_period); |
| 934 | if (tWB < 0) { |
| 935 | dev_err(nfc->dev, "unsupported tWB\n"); |
| 936 | return tWB; |
| 937 | } |
| 938 | |
| 939 | tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3; |
| 940 | if (tADL > 3) { |
| 941 | dev_err(nfc->dev, "unsupported tADL\n"); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | |
| 945 | tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3; |
| 946 | if (tWHR > 3) { |
| 947 | dev_err(nfc->dev, "unsupported tWHR\n"); |
| 948 | return -EINVAL; |
| 949 | } |
| 950 | |
| 951 | tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min, |
| 952 | min_clk_period); |
| 953 | if (tRHW < 0) { |
| 954 | dev_err(nfc->dev, "unsupported tRHW\n"); |
| 955 | return tRHW; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * TODO: according to ONFI specs this value only applies for DDR NAND, |
| 960 | * but Allwinner seems to set this to 0x7. Mimic them for now. |
| 961 | */ |
| 962 | tCAD = 0x7; |
| 963 | |
| 964 | /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */ |
| 965 | chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 966 | |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 967 | /* |
| 968 | * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data |
| 969 | * output cycle timings shall be used if the host drives tRC less than |
| 970 | * 30 ns. |
| 971 | */ |
| 972 | chip->timing_ctl = (timings->tRC_min < 30000) ? NFC_TIMING_CTL_EDO : 0; |
| 973 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 974 | /* Convert min_clk_period from picoseconds to nanoseconds */ |
| 975 | min_clk_period = DIV_ROUND_UP(min_clk_period, 1000); |
| 976 | |
| 977 | /* |
| 978 | * Convert min_clk_period into a clk frequency, then get the |
| 979 | * appropriate rate for the NAND controller IP given this formula |
| 980 | * (specified in the datasheet): |
| 981 | * nand clk_rate = 2 * min_clk_rate |
| 982 | */ |
| 983 | chip->clk_rate = (2 * NSEC_PER_SEC) / min_clk_period; |
| 984 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip, |
| 989 | struct device_node *np) |
| 990 | { |
| 991 | const struct nand_sdr_timings *timings; |
| 992 | int ret; |
| 993 | int mode; |
| 994 | |
| 995 | mode = onfi_get_async_timing_mode(&chip->nand); |
| 996 | if (mode == ONFI_TIMING_MODE_UNKNOWN) { |
| 997 | mode = chip->nand.onfi_timing_mode_default; |
| 998 | } else { |
| 999 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {}; |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1000 | int i; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1001 | |
| 1002 | mode = fls(mode) - 1; |
| 1003 | if (mode < 0) |
| 1004 | mode = 0; |
| 1005 | |
| 1006 | feature[0] = mode; |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1007 | for (i = 0; i < chip->nsels; i++) { |
| 1008 | chip->nand.select_chip(&chip->mtd, i); |
| 1009 | ret = chip->nand.onfi_set_features(&chip->mtd, |
| 1010 | &chip->nand, |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1011 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1012 | feature); |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1013 | chip->nand.select_chip(&chip->mtd, -1); |
| 1014 | if (ret) |
| 1015 | return ret; |
| 1016 | } |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | timings = onfi_async_timing_mode_to_sdr_timings(mode); |
| 1020 | if (IS_ERR(timings)) |
| 1021 | return PTR_ERR(timings); |
| 1022 | |
| 1023 | return sunxi_nand_chip_set_timings(chip, timings); |
| 1024 | } |
| 1025 | |
| 1026 | static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd, |
| 1027 | struct nand_ecc_ctrl *ecc, |
| 1028 | struct device_node *np) |
| 1029 | { |
| 1030 | static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 }; |
| 1031 | struct nand_chip *nand = mtd->priv; |
| 1032 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 1033 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 1034 | struct sunxi_nand_hw_ecc *data; |
| 1035 | struct nand_ecclayout *layout; |
| 1036 | int nsectors; |
| 1037 | int ret; |
| 1038 | int i; |
| 1039 | |
| 1040 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1041 | if (!data) |
| 1042 | return -ENOMEM; |
| 1043 | |
| 1044 | /* Add ECC info retrieval from DT */ |
| 1045 | for (i = 0; i < ARRAY_SIZE(strengths); i++) { |
| 1046 | if (ecc->strength <= strengths[i]) |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | if (i >= ARRAY_SIZE(strengths)) { |
| 1051 | dev_err(nfc->dev, "unsupported strength\n"); |
| 1052 | ret = -ENOTSUPP; |
| 1053 | goto err; |
| 1054 | } |
| 1055 | |
| 1056 | data->mode = i; |
| 1057 | |
| 1058 | /* HW ECC always request ECC bytes for 1024 bytes blocks */ |
| 1059 | ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8); |
| 1060 | |
| 1061 | /* HW ECC always work with even numbers of ECC bytes */ |
| 1062 | ecc->bytes = ALIGN(ecc->bytes, 2); |
| 1063 | |
| 1064 | layout = &data->layout; |
| 1065 | nsectors = mtd->writesize / ecc->size; |
| 1066 | |
| 1067 | if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) { |
| 1068 | ret = -EINVAL; |
| 1069 | goto err; |
| 1070 | } |
| 1071 | |
| 1072 | layout->eccbytes = (ecc->bytes * nsectors); |
| 1073 | |
| 1074 | ecc->layout = layout; |
| 1075 | ecc->priv = data; |
| 1076 | |
| 1077 | return 0; |
| 1078 | |
| 1079 | err: |
| 1080 | kfree(data); |
| 1081 | |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc) |
| 1086 | { |
| 1087 | kfree(ecc->priv); |
| 1088 | } |
| 1089 | |
| 1090 | static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd, |
| 1091 | struct nand_ecc_ctrl *ecc, |
| 1092 | struct device_node *np) |
| 1093 | { |
| 1094 | struct nand_ecclayout *layout; |
| 1095 | int nsectors; |
| 1096 | int i, j; |
| 1097 | int ret; |
| 1098 | |
| 1099 | ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np); |
| 1100 | if (ret) |
| 1101 | return ret; |
| 1102 | |
| 1103 | ecc->read_page = sunxi_nfc_hw_ecc_read_page; |
| 1104 | ecc->write_page = sunxi_nfc_hw_ecc_write_page; |
| 1105 | layout = ecc->layout; |
| 1106 | nsectors = mtd->writesize / ecc->size; |
| 1107 | |
| 1108 | for (i = 0; i < nsectors; i++) { |
| 1109 | if (i) { |
| 1110 | layout->oobfree[i].offset = |
| 1111 | layout->oobfree[i - 1].offset + |
| 1112 | layout->oobfree[i - 1].length + |
| 1113 | ecc->bytes; |
| 1114 | layout->oobfree[i].length = 4; |
| 1115 | } else { |
| 1116 | /* |
| 1117 | * The first 2 bytes are used for BB markers, hence we |
| 1118 | * only have 2 bytes available in the first user data |
| 1119 | * section. |
| 1120 | */ |
| 1121 | layout->oobfree[i].length = 2; |
| 1122 | layout->oobfree[i].offset = 2; |
| 1123 | } |
| 1124 | |
| 1125 | for (j = 0; j < ecc->bytes; j++) |
| 1126 | layout->eccpos[(ecc->bytes * i) + j] = |
| 1127 | layout->oobfree[i].offset + |
| 1128 | layout->oobfree[i].length + j; |
| 1129 | } |
| 1130 | |
| 1131 | if (mtd->oobsize > (ecc->bytes + 4) * nsectors) { |
| 1132 | layout->oobfree[nsectors].offset = |
| 1133 | layout->oobfree[nsectors - 1].offset + |
| 1134 | layout->oobfree[nsectors - 1].length + |
| 1135 | ecc->bytes; |
| 1136 | layout->oobfree[nsectors].length = mtd->oobsize - |
| 1137 | ((ecc->bytes + 4) * nsectors); |
| 1138 | } |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd, |
| 1144 | struct nand_ecc_ctrl *ecc, |
| 1145 | struct device_node *np) |
| 1146 | { |
| 1147 | struct nand_ecclayout *layout; |
| 1148 | int nsectors; |
| 1149 | int i; |
| 1150 | int ret; |
| 1151 | |
| 1152 | ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np); |
| 1153 | if (ret) |
| 1154 | return ret; |
| 1155 | |
| 1156 | ecc->prepad = 4; |
| 1157 | ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page; |
| 1158 | ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page; |
| 1159 | |
| 1160 | layout = ecc->layout; |
| 1161 | nsectors = mtd->writesize / ecc->size; |
| 1162 | |
| 1163 | for (i = 0; i < (ecc->bytes * nsectors); i++) |
| 1164 | layout->eccpos[i] = i; |
| 1165 | |
| 1166 | layout->oobfree[0].length = mtd->oobsize - i; |
| 1167 | layout->oobfree[0].offset = i; |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc) |
| 1173 | { |
| 1174 | switch (ecc->mode) { |
| 1175 | case NAND_ECC_HW: |
| 1176 | case NAND_ECC_HW_SYNDROME: |
| 1177 | sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc); |
| 1178 | break; |
| 1179 | case NAND_ECC_NONE: |
| 1180 | kfree(ecc->layout); |
| 1181 | default: |
| 1182 | break; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc, |
| 1187 | struct device_node *np) |
| 1188 | { |
| 1189 | struct nand_chip *nand = mtd->priv; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1190 | int ret; |
| 1191 | |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1192 | if (!ecc->size) { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1193 | ecc->size = nand->ecc_step_ds; |
| 1194 | ecc->strength = nand->ecc_strength_ds; |
| 1195 | } |
| 1196 | |
| 1197 | if (!ecc->size || !ecc->strength) |
| 1198 | return -EINVAL; |
| 1199 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1200 | switch (ecc->mode) { |
| 1201 | case NAND_ECC_SOFT_BCH: |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | case NAND_ECC_HW: |
| 1204 | ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np); |
| 1205 | if (ret) |
| 1206 | return ret; |
| 1207 | break; |
| 1208 | case NAND_ECC_HW_SYNDROME: |
| 1209 | ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np); |
| 1210 | if (ret) |
| 1211 | return ret; |
| 1212 | break; |
| 1213 | case NAND_ECC_NONE: |
| 1214 | ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL); |
| 1215 | if (!ecc->layout) |
| 1216 | return -ENOMEM; |
| 1217 | ecc->layout->oobfree[0].length = mtd->oobsize; |
| 1218 | case NAND_ECC_SOFT: |
| 1219 | break; |
| 1220 | default: |
| 1221 | return -EINVAL; |
| 1222 | } |
| 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc, |
| 1228 | struct device_node *np) |
| 1229 | { |
| 1230 | const struct nand_sdr_timings *timings; |
| 1231 | struct sunxi_nand_chip *chip; |
| 1232 | struct mtd_part_parser_data ppdata; |
| 1233 | struct mtd_info *mtd; |
| 1234 | struct nand_chip *nand; |
| 1235 | int nsels; |
| 1236 | int ret; |
| 1237 | int i; |
| 1238 | u32 tmp; |
| 1239 | |
| 1240 | if (!of_get_property(np, "reg", &nsels)) |
| 1241 | return -EINVAL; |
| 1242 | |
| 1243 | nsels /= sizeof(u32); |
| 1244 | if (!nsels) { |
| 1245 | dev_err(dev, "invalid reg property size\n"); |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | |
| 1249 | chip = devm_kzalloc(dev, |
| 1250 | sizeof(*chip) + |
| 1251 | (nsels * sizeof(struct sunxi_nand_chip_sel)), |
| 1252 | GFP_KERNEL); |
| 1253 | if (!chip) { |
| 1254 | dev_err(dev, "could not allocate chip\n"); |
| 1255 | return -ENOMEM; |
| 1256 | } |
| 1257 | |
| 1258 | chip->nsels = nsels; |
| 1259 | chip->selected = -1; |
| 1260 | |
| 1261 | for (i = 0; i < nsels; i++) { |
| 1262 | ret = of_property_read_u32_index(np, "reg", i, &tmp); |
| 1263 | if (ret) { |
| 1264 | dev_err(dev, "could not retrieve reg property: %d\n", |
| 1265 | ret); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | if (tmp > NFC_MAX_CS) { |
| 1270 | dev_err(dev, |
| 1271 | "invalid reg value: %u (max CS = 7)\n", |
| 1272 | tmp); |
| 1273 | return -EINVAL; |
| 1274 | } |
| 1275 | |
| 1276 | if (test_and_set_bit(tmp, &nfc->assigned_cs)) { |
| 1277 | dev_err(dev, "CS %d already assigned\n", tmp); |
| 1278 | return -EINVAL; |
| 1279 | } |
| 1280 | |
| 1281 | chip->sels[i].cs = tmp; |
| 1282 | |
| 1283 | if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) && |
| 1284 | tmp < 2) { |
| 1285 | chip->sels[i].rb.type = RB_NATIVE; |
| 1286 | chip->sels[i].rb.info.nativeid = tmp; |
| 1287 | } else { |
| 1288 | ret = of_get_named_gpio(np, "rb-gpios", i); |
| 1289 | if (ret >= 0) { |
| 1290 | tmp = ret; |
| 1291 | chip->sels[i].rb.type = RB_GPIO; |
| 1292 | chip->sels[i].rb.info.gpio = tmp; |
| 1293 | ret = devm_gpio_request(dev, tmp, "nand-rb"); |
| 1294 | if (ret) |
| 1295 | return ret; |
| 1296 | |
| 1297 | ret = gpio_direction_input(tmp); |
| 1298 | if (ret) |
| 1299 | return ret; |
| 1300 | } else { |
| 1301 | chip->sels[i].rb.type = RB_NONE; |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | timings = onfi_async_timing_mode_to_sdr_timings(0); |
| 1307 | if (IS_ERR(timings)) { |
| 1308 | ret = PTR_ERR(timings); |
| 1309 | dev_err(dev, |
| 1310 | "could not retrieve timings for ONFI mode 0: %d\n", |
| 1311 | ret); |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | ret = sunxi_nand_chip_set_timings(chip, timings); |
| 1316 | if (ret) { |
| 1317 | dev_err(dev, "could not configure chip timings: %d\n", ret); |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | nand = &chip->nand; |
| 1322 | /* Default tR value specified in the ONFI spec (chapter 4.15.1) */ |
| 1323 | nand->chip_delay = 200; |
| 1324 | nand->controller = &nfc->controller; |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1325 | /* |
| 1326 | * Set the ECC mode to the default value in case nothing is specified |
| 1327 | * in the DT. |
| 1328 | */ |
| 1329 | nand->ecc.mode = NAND_ECC_HW; |
| 1330 | nand->flash_node = np; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1331 | nand->select_chip = sunxi_nfc_select_chip; |
| 1332 | nand->cmd_ctrl = sunxi_nfc_cmd_ctrl; |
| 1333 | nand->read_buf = sunxi_nfc_read_buf; |
| 1334 | nand->write_buf = sunxi_nfc_write_buf; |
| 1335 | nand->read_byte = sunxi_nfc_read_byte; |
| 1336 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1337 | mtd = &chip->mtd; |
| 1338 | mtd->dev.parent = dev; |
| 1339 | mtd->priv = nand; |
| 1340 | mtd->owner = THIS_MODULE; |
| 1341 | |
| 1342 | ret = nand_scan_ident(mtd, nsels, NULL); |
| 1343 | if (ret) |
| 1344 | return ret; |
| 1345 | |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1346 | if (nand->bbt_options & NAND_BBT_USE_FLASH) |
| 1347 | nand->bbt_options |= NAND_BBT_NO_OOB; |
| 1348 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1349 | ret = sunxi_nand_chip_init_timings(chip, np); |
| 1350 | if (ret) { |
| 1351 | dev_err(dev, "could not configure chip timings: %d\n", ret); |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np); |
| 1356 | if (ret) { |
| 1357 | dev_err(dev, "ECC init failed: %d\n", ret); |
| 1358 | return ret; |
| 1359 | } |
| 1360 | |
| 1361 | ret = nand_scan_tail(mtd); |
| 1362 | if (ret) { |
| 1363 | dev_err(dev, "nand_scan_tail failed: %d\n", ret); |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
| 1367 | ppdata.of_node = np; |
| 1368 | ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0); |
| 1369 | if (ret) { |
| 1370 | dev_err(dev, "failed to register mtd device: %d\n", ret); |
| 1371 | nand_release(mtd); |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
| 1375 | list_add_tail(&chip->node, &nfc->chips); |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc) |
| 1381 | { |
| 1382 | struct device_node *np = dev->of_node; |
| 1383 | struct device_node *nand_np; |
| 1384 | int nchips = of_get_child_count(np); |
| 1385 | int ret; |
| 1386 | |
| 1387 | if (nchips > 8) { |
| 1388 | dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips); |
| 1389 | return -EINVAL; |
| 1390 | } |
| 1391 | |
| 1392 | for_each_child_of_node(np, nand_np) { |
| 1393 | ret = sunxi_nand_chip_init(dev, nfc, nand_np); |
| 1394 | if (ret) |
| 1395 | return ret; |
| 1396 | } |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc) |
| 1402 | { |
| 1403 | struct sunxi_nand_chip *chip; |
| 1404 | |
| 1405 | while (!list_empty(&nfc->chips)) { |
| 1406 | chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip, |
| 1407 | node); |
| 1408 | nand_release(&chip->mtd); |
| 1409 | sunxi_nand_ecc_cleanup(&chip->nand.ecc); |
Boris BREZILLON | 8e375cc | 2015-09-13 18:14:43 +0200 | [diff] [blame] | 1410 | list_del(&chip->node); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | static int sunxi_nfc_probe(struct platform_device *pdev) |
| 1415 | { |
| 1416 | struct device *dev = &pdev->dev; |
| 1417 | struct resource *r; |
| 1418 | struct sunxi_nfc *nfc; |
| 1419 | int irq; |
| 1420 | int ret; |
| 1421 | |
| 1422 | nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL); |
| 1423 | if (!nfc) |
| 1424 | return -ENOMEM; |
| 1425 | |
| 1426 | nfc->dev = dev; |
| 1427 | spin_lock_init(&nfc->controller.lock); |
| 1428 | init_waitqueue_head(&nfc->controller.wq); |
| 1429 | INIT_LIST_HEAD(&nfc->chips); |
| 1430 | |
| 1431 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1432 | nfc->regs = devm_ioremap_resource(dev, r); |
| 1433 | if (IS_ERR(nfc->regs)) |
| 1434 | return PTR_ERR(nfc->regs); |
| 1435 | |
| 1436 | irq = platform_get_irq(pdev, 0); |
| 1437 | if (irq < 0) { |
| 1438 | dev_err(dev, "failed to retrieve irq\n"); |
| 1439 | return irq; |
| 1440 | } |
| 1441 | |
| 1442 | nfc->ahb_clk = devm_clk_get(dev, "ahb"); |
| 1443 | if (IS_ERR(nfc->ahb_clk)) { |
| 1444 | dev_err(dev, "failed to retrieve ahb clk\n"); |
| 1445 | return PTR_ERR(nfc->ahb_clk); |
| 1446 | } |
| 1447 | |
| 1448 | ret = clk_prepare_enable(nfc->ahb_clk); |
| 1449 | if (ret) |
| 1450 | return ret; |
| 1451 | |
| 1452 | nfc->mod_clk = devm_clk_get(dev, "mod"); |
| 1453 | if (IS_ERR(nfc->mod_clk)) { |
| 1454 | dev_err(dev, "failed to retrieve mod clk\n"); |
| 1455 | ret = PTR_ERR(nfc->mod_clk); |
| 1456 | goto out_ahb_clk_unprepare; |
| 1457 | } |
| 1458 | |
| 1459 | ret = clk_prepare_enable(nfc->mod_clk); |
| 1460 | if (ret) |
| 1461 | goto out_ahb_clk_unprepare; |
| 1462 | |
| 1463 | ret = sunxi_nfc_rst(nfc); |
| 1464 | if (ret) |
| 1465 | goto out_mod_clk_unprepare; |
| 1466 | |
| 1467 | writel(0, nfc->regs + NFC_REG_INT); |
| 1468 | ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt, |
| 1469 | 0, "sunxi-nand", nfc); |
| 1470 | if (ret) |
| 1471 | goto out_mod_clk_unprepare; |
| 1472 | |
| 1473 | platform_set_drvdata(pdev, nfc); |
| 1474 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1475 | ret = sunxi_nand_chips_init(dev, nfc); |
| 1476 | if (ret) { |
| 1477 | dev_err(dev, "failed to init nand chips\n"); |
| 1478 | goto out_mod_clk_unprepare; |
| 1479 | } |
| 1480 | |
| 1481 | return 0; |
| 1482 | |
| 1483 | out_mod_clk_unprepare: |
| 1484 | clk_disable_unprepare(nfc->mod_clk); |
| 1485 | out_ahb_clk_unprepare: |
| 1486 | clk_disable_unprepare(nfc->ahb_clk); |
| 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | static int sunxi_nfc_remove(struct platform_device *pdev) |
| 1492 | { |
| 1493 | struct sunxi_nfc *nfc = platform_get_drvdata(pdev); |
| 1494 | |
| 1495 | sunxi_nand_chips_cleanup(nfc); |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static const struct of_device_id sunxi_nfc_ids[] = { |
| 1501 | { .compatible = "allwinner,sun4i-a10-nand" }, |
| 1502 | { /* sentinel */ } |
| 1503 | }; |
| 1504 | MODULE_DEVICE_TABLE(of, sunxi_nfc_ids); |
| 1505 | |
| 1506 | static struct platform_driver sunxi_nfc_driver = { |
| 1507 | .driver = { |
| 1508 | .name = "sunxi_nand", |
| 1509 | .of_match_table = sunxi_nfc_ids, |
| 1510 | }, |
| 1511 | .probe = sunxi_nfc_probe, |
| 1512 | .remove = sunxi_nfc_remove, |
| 1513 | }; |
| 1514 | module_platform_driver(sunxi_nfc_driver); |
| 1515 | |
| 1516 | MODULE_LICENSE("GPL v2"); |
| 1517 | MODULE_AUTHOR("Boris BREZILLON"); |
| 1518 | MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver"); |
| 1519 | MODULE_ALIAS("platform:sunxi_nand"); |