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> |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 33 | #include <linux/mtd/mtd.h> |
| 34 | #include <linux/mtd/nand.h> |
| 35 | #include <linux/mtd/partitions.h> |
| 36 | #include <linux/clk.h> |
| 37 | #include <linux/delay.h> |
| 38 | #include <linux/dmaengine.h> |
| 39 | #include <linux/gpio.h> |
| 40 | #include <linux/interrupt.h> |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 41 | #include <linux/iopoll.h> |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 42 | |
| 43 | #define NFC_REG_CTL 0x0000 |
| 44 | #define NFC_REG_ST 0x0004 |
| 45 | #define NFC_REG_INT 0x0008 |
| 46 | #define NFC_REG_TIMING_CTL 0x000C |
| 47 | #define NFC_REG_TIMING_CFG 0x0010 |
| 48 | #define NFC_REG_ADDR_LOW 0x0014 |
| 49 | #define NFC_REG_ADDR_HIGH 0x0018 |
| 50 | #define NFC_REG_SECTOR_NUM 0x001C |
| 51 | #define NFC_REG_CNT 0x0020 |
| 52 | #define NFC_REG_CMD 0x0024 |
| 53 | #define NFC_REG_RCMD_SET 0x0028 |
| 54 | #define NFC_REG_WCMD_SET 0x002C |
| 55 | #define NFC_REG_IO_DATA 0x0030 |
| 56 | #define NFC_REG_ECC_CTL 0x0034 |
| 57 | #define NFC_REG_ECC_ST 0x0038 |
| 58 | #define NFC_REG_DEBUG 0x003C |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 59 | #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3) |
| 60 | #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4)) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 61 | #define NFC_REG_SPARE_AREA 0x00A0 |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 62 | #define NFC_REG_PAT_ID 0x00A4 |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 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) |
Boris Brezillon | f8b0474 | 2016-03-04 17:25:08 +0100 | [diff] [blame] | 157 | #define NFC_ECC_ERR_CNT(b, x) (((x) >> (((b) % 4) * 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 |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 214 | */ |
| 215 | struct sunxi_nand_hw_ecc { |
| 216 | int mode; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | /* |
| 220 | * NAND chip structure: stores NAND chip device related information |
| 221 | * |
| 222 | * @node: used to store NAND chips into a list |
| 223 | * @nand: base NAND chip structure |
| 224 | * @mtd: base MTD structure |
| 225 | * @clk_rate: clk_rate required for this NAND chip |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 226 | * @timing_cfg TIMING_CFG register value for this NAND chip |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 227 | * @selected: current active CS |
| 228 | * @nsels: number of CS lines required by the NAND chip |
| 229 | * @sels: array of CS lines descriptions |
| 230 | */ |
| 231 | struct sunxi_nand_chip { |
| 232 | struct list_head node; |
| 233 | struct nand_chip nand; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 234 | unsigned long clk_rate; |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 235 | u32 timing_cfg; |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 236 | u32 timing_ctl; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 237 | int selected; |
Boris Brezillon | e9aa671 | 2015-09-16 09:05:31 +0200 | [diff] [blame] | 238 | int addr_cycles; |
| 239 | u32 addr[2]; |
| 240 | int cmd_cycles; |
| 241 | u8 cmd[2]; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 301 | static int sunxi_nfc_wait_events(struct sunxi_nfc *nfc, u32 events, |
| 302 | bool use_polling, unsigned int timeout_ms) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 303 | { |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 304 | int ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 305 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 306 | if (events & ~NFC_INT_MASK) |
| 307 | return -EINVAL; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 308 | |
| 309 | if (!timeout_ms) |
| 310 | timeout_ms = NFC_DEFAULT_TIMEOUT_MS; |
| 311 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 312 | if (!use_polling) { |
| 313 | init_completion(&nfc->complete); |
| 314 | |
| 315 | writel(events, nfc->regs + NFC_REG_INT); |
| 316 | |
| 317 | ret = wait_for_completion_timeout(&nfc->complete, |
| 318 | msecs_to_jiffies(timeout_ms)); |
| 319 | |
| 320 | writel(0, nfc->regs + NFC_REG_INT); |
| 321 | } else { |
| 322 | u32 status; |
| 323 | |
| 324 | ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, |
| 325 | (status & events) == events, 1, |
| 326 | timeout_ms * 1000); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 329 | writel(events & NFC_INT_MASK, nfc->regs + NFC_REG_ST); |
| 330 | |
| 331 | if (ret) |
| 332 | dev_err(nfc->dev, "wait interrupt timedout\n"); |
| 333 | |
| 334 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc) |
| 338 | { |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 339 | u32 status; |
| 340 | int ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 341 | |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 342 | ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status, |
| 343 | !(status & NFC_CMD_FIFO_STATUS), 1, |
| 344 | NFC_DEFAULT_TIMEOUT_MS * 1000); |
| 345 | if (ret) |
| 346 | dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n"); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 347 | |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 348 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int sunxi_nfc_rst(struct sunxi_nfc *nfc) |
| 352 | { |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 353 | u32 ctl; |
| 354 | int ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 355 | |
| 356 | writel(0, nfc->regs + NFC_REG_ECC_CTL); |
| 357 | writel(NFC_RESET, nfc->regs + NFC_REG_CTL); |
| 358 | |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 359 | ret = readl_poll_timeout(nfc->regs + NFC_REG_CTL, ctl, |
| 360 | !(ctl & NFC_RESET), 1, |
| 361 | NFC_DEFAULT_TIMEOUT_MS * 1000); |
| 362 | if (ret) |
| 363 | dev_err(nfc->dev, "wait for NAND controller reset timedout\n"); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 364 | |
Boris Brezillon | 166f08c | 2016-03-07 15:25:17 +0100 | [diff] [blame] | 365 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int sunxi_nfc_dev_ready(struct mtd_info *mtd) |
| 369 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 370 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 371 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 372 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 373 | struct sunxi_nand_rb *rb; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 374 | int ret; |
| 375 | |
| 376 | if (sunxi_nand->selected < 0) |
| 377 | return 0; |
| 378 | |
| 379 | rb = &sunxi_nand->sels[sunxi_nand->selected].rb; |
| 380 | |
| 381 | switch (rb->type) { |
| 382 | case RB_NATIVE: |
| 383 | ret = !!(readl(nfc->regs + NFC_REG_ST) & |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 384 | NFC_RB_STATE(rb->info.nativeid)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 385 | break; |
| 386 | case RB_GPIO: |
| 387 | ret = gpio_get_value(rb->info.gpio); |
| 388 | break; |
| 389 | case RB_NONE: |
| 390 | default: |
| 391 | ret = 0; |
| 392 | dev_err(nfc->dev, "cannot check R/B NAND status!\n"); |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip) |
| 400 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 401 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 402 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 403 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 404 | struct sunxi_nand_chip_sel *sel; |
| 405 | u32 ctl; |
| 406 | |
| 407 | if (chip > 0 && chip >= sunxi_nand->nsels) |
| 408 | return; |
| 409 | |
| 410 | if (chip == sunxi_nand->selected) |
| 411 | return; |
| 412 | |
| 413 | ctl = readl(nfc->regs + NFC_REG_CTL) & |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 414 | ~(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] | 415 | |
| 416 | if (chip >= 0) { |
| 417 | sel = &sunxi_nand->sels[chip]; |
| 418 | |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 419 | ctl |= NFC_CE_SEL(sel->cs) | NFC_EN | |
Boris Brezillon | 68ffbf7 | 2016-03-04 17:29:20 +0100 | [diff] [blame] | 420 | NFC_PAGE_SHIFT(nand->page_shift); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 421 | if (sel->rb.type == RB_NONE) { |
| 422 | nand->dev_ready = NULL; |
| 423 | } else { |
| 424 | nand->dev_ready = sunxi_nfc_dev_ready; |
| 425 | if (sel->rb.type == RB_NATIVE) |
Boris BREZILLON | b6a02c0 | 2015-09-16 09:46:36 +0200 | [diff] [blame] | 426 | ctl |= NFC_RB_SEL(sel->rb.info.nativeid); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA); |
| 430 | |
| 431 | if (nfc->clk_rate != sunxi_nand->clk_rate) { |
| 432 | clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate); |
| 433 | nfc->clk_rate = sunxi_nand->clk_rate; |
| 434 | } |
| 435 | } |
| 436 | |
Roy Spliet | d052e50 | 2015-06-26 11:00:11 +0200 | [diff] [blame] | 437 | writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL); |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 438 | writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 439 | writel(ctl, nfc->regs + NFC_REG_CTL); |
| 440 | |
| 441 | sunxi_nand->selected = chip; |
| 442 | } |
| 443 | |
| 444 | static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 445 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 446 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 447 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 448 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 449 | int ret; |
| 450 | int cnt; |
| 451 | int offs = 0; |
| 452 | u32 tmp; |
| 453 | |
| 454 | while (len > offs) { |
| 455 | cnt = min(len - offs, NFC_SRAM_SIZE); |
| 456 | |
| 457 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 458 | if (ret) |
| 459 | break; |
| 460 | |
| 461 | writel(cnt, nfc->regs + NFC_REG_CNT); |
| 462 | tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD; |
| 463 | writel(tmp, nfc->regs + NFC_REG_CMD); |
| 464 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 465 | ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 466 | if (ret) |
| 467 | break; |
| 468 | |
| 469 | if (buf) |
| 470 | memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE, |
| 471 | cnt); |
| 472 | offs += cnt; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 477 | int len) |
| 478 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 479 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 480 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 481 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 482 | int ret; |
| 483 | int cnt; |
| 484 | int offs = 0; |
| 485 | u32 tmp; |
| 486 | |
| 487 | while (len > offs) { |
| 488 | cnt = min(len - offs, NFC_SRAM_SIZE); |
| 489 | |
| 490 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 491 | if (ret) |
| 492 | break; |
| 493 | |
| 494 | writel(cnt, nfc->regs + NFC_REG_CNT); |
| 495 | memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt); |
| 496 | tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 497 | NFC_ACCESS_DIR; |
| 498 | writel(tmp, nfc->regs + NFC_REG_CMD); |
| 499 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 500 | ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 501 | if (ret) |
| 502 | break; |
| 503 | |
| 504 | offs += cnt; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd) |
| 509 | { |
| 510 | uint8_t ret; |
| 511 | |
| 512 | sunxi_nfc_read_buf(mtd, &ret, 1); |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat, |
| 518 | unsigned int ctrl) |
| 519 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 520 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 521 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 522 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 523 | int ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 524 | |
| 525 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 526 | if (ret) |
| 527 | return; |
| 528 | |
Boris Brezillon | e9aa671 | 2015-09-16 09:05:31 +0200 | [diff] [blame] | 529 | if (dat == NAND_CMD_NONE && (ctrl & NAND_NCE) && |
| 530 | !(ctrl & (NAND_CLE | NAND_ALE))) { |
| 531 | u32 cmd = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 532 | |
Boris Brezillon | e9aa671 | 2015-09-16 09:05:31 +0200 | [diff] [blame] | 533 | if (!sunxi_nand->addr_cycles && !sunxi_nand->cmd_cycles) |
| 534 | return; |
| 535 | |
| 536 | if (sunxi_nand->cmd_cycles--) |
| 537 | cmd |= NFC_SEND_CMD1 | sunxi_nand->cmd[0]; |
| 538 | |
| 539 | if (sunxi_nand->cmd_cycles--) { |
| 540 | cmd |= NFC_SEND_CMD2; |
| 541 | writel(sunxi_nand->cmd[1], |
| 542 | nfc->regs + NFC_REG_RCMD_SET); |
| 543 | } |
| 544 | |
| 545 | sunxi_nand->cmd_cycles = 0; |
| 546 | |
| 547 | if (sunxi_nand->addr_cycles) { |
| 548 | cmd |= NFC_SEND_ADR | |
| 549 | NFC_ADR_NUM(sunxi_nand->addr_cycles); |
| 550 | writel(sunxi_nand->addr[0], |
| 551 | nfc->regs + NFC_REG_ADDR_LOW); |
| 552 | } |
| 553 | |
| 554 | if (sunxi_nand->addr_cycles > 4) |
| 555 | writel(sunxi_nand->addr[1], |
| 556 | nfc->regs + NFC_REG_ADDR_HIGH); |
| 557 | |
| 558 | writel(cmd, nfc->regs + NFC_REG_CMD); |
| 559 | sunxi_nand->addr[0] = 0; |
| 560 | sunxi_nand->addr[1] = 0; |
| 561 | sunxi_nand->addr_cycles = 0; |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 562 | sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Boris Brezillon | e9aa671 | 2015-09-16 09:05:31 +0200 | [diff] [blame] | 565 | if (ctrl & NAND_CLE) { |
| 566 | sunxi_nand->cmd[sunxi_nand->cmd_cycles++] = dat; |
| 567 | } else if (ctrl & NAND_ALE) { |
| 568 | sunxi_nand->addr[sunxi_nand->addr_cycles / 4] |= |
| 569 | dat << ((sunxi_nand->addr_cycles % 4) * 8); |
| 570 | sunxi_nand->addr_cycles++; |
| 571 | } |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 574 | /* These seed values have been extracted from Allwinner's BSP */ |
| 575 | static const u16 sunxi_nfc_randomizer_page_seeds[] = { |
| 576 | 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72, |
| 577 | 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436, |
| 578 | 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d, |
| 579 | 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130, |
| 580 | 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56, |
| 581 | 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55, |
| 582 | 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb, |
| 583 | 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17, |
| 584 | 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62, |
| 585 | 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064, |
| 586 | 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126, |
| 587 | 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e, |
| 588 | 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3, |
| 589 | 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b, |
| 590 | 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d, |
| 591 | 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db, |
| 592 | }; |
| 593 | |
| 594 | /* |
| 595 | * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds |
| 596 | * have been generated using |
| 597 | * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what |
| 598 | * the randomizer engine does internally before de/scrambling OOB data. |
| 599 | * |
| 600 | * Those tables are statically defined to avoid calculating randomizer state |
| 601 | * at runtime. |
| 602 | */ |
| 603 | static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = { |
| 604 | 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64, |
| 605 | 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409, |
| 606 | 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617, |
| 607 | 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d, |
| 608 | 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91, |
| 609 | 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d, |
| 610 | 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab, |
| 611 | 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8, |
| 612 | 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8, |
| 613 | 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b, |
| 614 | 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5, |
| 615 | 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a, |
| 616 | 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891, |
| 617 | 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36, |
| 618 | 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd, |
| 619 | 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0, |
| 620 | }; |
| 621 | |
| 622 | static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = { |
| 623 | 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6, |
| 624 | 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982, |
| 625 | 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9, |
| 626 | 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07, |
| 627 | 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e, |
| 628 | 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2, |
| 629 | 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c, |
| 630 | 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f, |
| 631 | 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc, |
| 632 | 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e, |
| 633 | 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8, |
| 634 | 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68, |
| 635 | 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d, |
| 636 | 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179, |
| 637 | 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601, |
| 638 | 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd, |
| 639 | }; |
| 640 | |
| 641 | static u16 sunxi_nfc_randomizer_step(u16 state, int count) |
| 642 | { |
| 643 | state &= 0x7fff; |
| 644 | |
| 645 | /* |
| 646 | * This loop is just a simple implementation of a Fibonacci LFSR using |
| 647 | * the x16 + x15 + 1 polynomial. |
| 648 | */ |
| 649 | while (count--) |
| 650 | state = ((state >> 1) | |
| 651 | (((state ^ (state >> 1)) & 1) << 14)) & 0x7fff; |
| 652 | |
| 653 | return state; |
| 654 | } |
| 655 | |
| 656 | static u16 sunxi_nfc_randomizer_state(struct mtd_info *mtd, int page, bool ecc) |
| 657 | { |
| 658 | const u16 *seeds = sunxi_nfc_randomizer_page_seeds; |
Brian Norris | 46c135c | 2016-01-22 18:57:13 -0800 | [diff] [blame] | 659 | int mod = mtd_div_by_ws(mtd->erasesize, mtd); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 660 | |
| 661 | if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds)) |
| 662 | mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds); |
| 663 | |
| 664 | if (ecc) { |
| 665 | if (mtd->ecc_step_size == 512) |
| 666 | seeds = sunxi_nfc_randomizer_ecc512_seeds; |
| 667 | else |
| 668 | seeds = sunxi_nfc_randomizer_ecc1024_seeds; |
| 669 | } |
| 670 | |
| 671 | return seeds[page % mod]; |
| 672 | } |
| 673 | |
| 674 | static void sunxi_nfc_randomizer_config(struct mtd_info *mtd, |
| 675 | int page, bool ecc) |
| 676 | { |
Boris BREZILLON | f671a1f | 2016-03-05 00:21:20 +0100 | [diff] [blame] | 677 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 678 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 679 | u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); |
| 680 | u16 state; |
| 681 | |
| 682 | if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 683 | return; |
| 684 | |
| 685 | ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); |
| 686 | state = sunxi_nfc_randomizer_state(mtd, page, ecc); |
| 687 | ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK; |
| 688 | writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL); |
| 689 | } |
| 690 | |
| 691 | static void sunxi_nfc_randomizer_enable(struct mtd_info *mtd) |
| 692 | { |
Boris BREZILLON | f671a1f | 2016-03-05 00:21:20 +0100 | [diff] [blame] | 693 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 694 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 695 | |
| 696 | if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 697 | return; |
| 698 | |
| 699 | writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN, |
| 700 | nfc->regs + NFC_REG_ECC_CTL); |
| 701 | } |
| 702 | |
| 703 | static void sunxi_nfc_randomizer_disable(struct mtd_info *mtd) |
| 704 | { |
Boris BREZILLON | f671a1f | 2016-03-05 00:21:20 +0100 | [diff] [blame] | 705 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 706 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 707 | |
| 708 | if (!(nand->options & NAND_NEED_SCRAMBLING)) |
| 709 | return; |
| 710 | |
| 711 | writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN, |
| 712 | nfc->regs + NFC_REG_ECC_CTL); |
| 713 | } |
| 714 | |
| 715 | static void sunxi_nfc_randomize_bbm(struct mtd_info *mtd, int page, u8 *bbm) |
| 716 | { |
| 717 | u16 state = sunxi_nfc_randomizer_state(mtd, page, true); |
| 718 | |
| 719 | bbm[0] ^= state; |
| 720 | bbm[1] ^= sunxi_nfc_randomizer_step(state, 8); |
| 721 | } |
| 722 | |
| 723 | static void sunxi_nfc_randomizer_write_buf(struct mtd_info *mtd, |
| 724 | const uint8_t *buf, int len, |
| 725 | bool ecc, int page) |
| 726 | { |
| 727 | sunxi_nfc_randomizer_config(mtd, page, ecc); |
| 728 | sunxi_nfc_randomizer_enable(mtd); |
| 729 | sunxi_nfc_write_buf(mtd, buf, len); |
| 730 | sunxi_nfc_randomizer_disable(mtd); |
| 731 | } |
| 732 | |
| 733 | static void sunxi_nfc_randomizer_read_buf(struct mtd_info *mtd, uint8_t *buf, |
| 734 | int len, bool ecc, int page) |
| 735 | { |
| 736 | sunxi_nfc_randomizer_config(mtd, page, ecc); |
| 737 | sunxi_nfc_randomizer_enable(mtd); |
| 738 | sunxi_nfc_read_buf(mtd, buf, len); |
| 739 | sunxi_nfc_randomizer_disable(mtd); |
| 740 | } |
| 741 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 742 | static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd) |
| 743 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 744 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 745 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 746 | struct sunxi_nand_hw_ecc *data = nand->ecc.priv; |
| 747 | u32 ecc_ctl; |
| 748 | |
| 749 | ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL); |
| 750 | ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | |
| 751 | NFC_ECC_BLOCK_SIZE_MSK); |
Boris Brezillon | 336de7b | 2016-03-04 17:33:10 +0100 | [diff] [blame] | 752 | ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION | |
| 753 | NFC_ECC_PIPELINE; |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 754 | |
| 755 | writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL); |
| 756 | } |
| 757 | |
| 758 | static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd) |
| 759 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 760 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 761 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 762 | |
| 763 | writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN, |
| 764 | nfc->regs + NFC_REG_ECC_CTL); |
| 765 | } |
| 766 | |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 767 | static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf) |
| 768 | { |
| 769 | buf[0] = user_data; |
| 770 | buf[1] = user_data >> 8; |
| 771 | buf[2] = user_data >> 16; |
| 772 | buf[3] = user_data >> 24; |
| 773 | } |
| 774 | |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 775 | static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf) |
| 776 | { |
| 777 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 778 | } |
| 779 | |
| 780 | static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct mtd_info *mtd, u8 *oob, |
| 781 | int step, bool bbm, int page) |
| 782 | { |
| 783 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 784 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 785 | |
| 786 | sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(step)), |
| 787 | oob); |
| 788 | |
| 789 | /* De-randomize the Bad Block Marker. */ |
| 790 | if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) |
| 791 | sunxi_nfc_randomize_bbm(mtd, page, oob); |
| 792 | } |
| 793 | |
| 794 | static void sunxi_nfc_hw_ecc_set_prot_oob_bytes(struct mtd_info *mtd, |
| 795 | const u8 *oob, int step, |
| 796 | bool bbm, int page) |
| 797 | { |
| 798 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 799 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 800 | u8 user_data[4]; |
| 801 | |
| 802 | /* Randomize the Bad Block Marker. */ |
| 803 | if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) { |
| 804 | memcpy(user_data, oob, sizeof(user_data)); |
| 805 | sunxi_nfc_randomize_bbm(mtd, page, user_data); |
| 806 | oob = user_data; |
| 807 | } |
| 808 | |
| 809 | writel(sunxi_nfc_buf_to_user_data(oob), |
| 810 | nfc->regs + NFC_REG_USER_DATA(step)); |
| 811 | } |
| 812 | |
| 813 | static void sunxi_nfc_hw_ecc_update_stats(struct mtd_info *mtd, |
| 814 | unsigned int *max_bitflips, int ret) |
| 815 | { |
| 816 | if (ret < 0) { |
| 817 | mtd->ecc_stats.failed++; |
| 818 | } else { |
| 819 | mtd->ecc_stats.corrected += ret; |
| 820 | *max_bitflips = max_t(unsigned int, *max_bitflips, ret); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | static int sunxi_nfc_hw_ecc_correct(struct mtd_info *mtd, u8 *data, u8 *oob, |
| 825 | int step, bool *erased) |
| 826 | { |
| 827 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 828 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 829 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 830 | u32 status, tmp; |
| 831 | |
| 832 | *erased = false; |
| 833 | |
| 834 | status = readl(nfc->regs + NFC_REG_ECC_ST); |
| 835 | |
| 836 | if (status & NFC_ECC_ERR(step)) |
| 837 | return -EBADMSG; |
| 838 | |
| 839 | if (status & NFC_ECC_PAT_FOUND(step)) { |
| 840 | u8 pattern; |
| 841 | |
| 842 | if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1))) { |
| 843 | pattern = 0x0; |
| 844 | } else { |
| 845 | pattern = 0xff; |
| 846 | *erased = true; |
| 847 | } |
| 848 | |
| 849 | if (data) |
| 850 | memset(data, pattern, ecc->size); |
| 851 | |
| 852 | if (oob) |
| 853 | memset(oob, pattern, ecc->bytes + 4); |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | tmp = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(step)); |
| 859 | |
| 860 | return NFC_ECC_ERR_CNT(step, tmp); |
| 861 | } |
| 862 | |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 863 | static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd, |
| 864 | u8 *data, int data_off, |
| 865 | u8 *oob, int oob_off, |
| 866 | int *cur_off, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 867 | unsigned int *max_bitflips, |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 868 | bool bbm, bool oob_required, int page) |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 869 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 870 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 871 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 872 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 873 | int raw_mode = 0; |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 874 | bool erased; |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 875 | int ret; |
| 876 | |
| 877 | if (*cur_off != data_off) |
| 878 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1); |
| 879 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 880 | sunxi_nfc_randomizer_read_buf(mtd, NULL, ecc->size, false, page); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 881 | |
Boris BREZILLON | 74eb9ff | 2015-10-20 22:16:00 +0200 | [diff] [blame] | 882 | if (data_off + ecc->size != oob_off) |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 883 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1); |
| 884 | |
| 885 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 889 | sunxi_nfc_randomizer_enable(mtd); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 890 | writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP, |
| 891 | nfc->regs + NFC_REG_CMD); |
| 892 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 893 | ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 894 | sunxi_nfc_randomizer_disable(mtd); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 895 | if (ret) |
| 896 | return ret; |
| 897 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 898 | *cur_off = oob_off + ecc->bytes + 4; |
| 899 | |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 900 | ret = sunxi_nfc_hw_ecc_correct(mtd, data, oob_required ? oob : NULL, 0, |
| 901 | &erased); |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 902 | if (erased) |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 903 | return 1; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 904 | |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 905 | if (ret < 0) { |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 906 | /* |
| 907 | * Re-read the data with the randomizer disabled to identify |
| 908 | * bitflips in erased pages. |
| 909 | */ |
| 910 | if (nand->options & NAND_NEED_SCRAMBLING) { |
| 911 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1); |
| 912 | nand->read_buf(mtd, data, ecc->size); |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 913 | } else { |
| 914 | memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, |
| 915 | ecc->size); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 918 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1); |
| 919 | nand->read_buf(mtd, oob, ecc->bytes + 4); |
| 920 | |
Boris BREZILLON | 146b503 | 2015-09-30 23:45:29 +0200 | [diff] [blame] | 921 | ret = nand_check_erased_ecc_chunk(data, ecc->size, |
| 922 | oob, ecc->bytes + 4, |
| 923 | NULL, 0, ecc->strength); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 924 | if (ret >= 0) |
| 925 | raw_mode = 1; |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 926 | } else { |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 927 | memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 928 | |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 929 | if (oob_required) { |
| 930 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1); |
| 931 | sunxi_nfc_randomizer_read_buf(mtd, oob, ecc->bytes + 4, |
| 932 | true, page); |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 933 | |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 934 | sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd, oob, 0, |
| 935 | bbm, page); |
| 936 | } |
Boris BREZILLON | f363e0f | 2015-09-30 23:45:27 +0200 | [diff] [blame] | 937 | } |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 938 | |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 939 | sunxi_nfc_hw_ecc_update_stats(mtd, max_bitflips, ret); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 940 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 941 | return raw_mode; |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 944 | static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 945 | u8 *oob, int *cur_off, |
| 946 | bool randomize, int page) |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 947 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 948 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 949 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 950 | int offset = ((ecc->bytes + 4) * ecc->steps); |
| 951 | int len = mtd->oobsize - offset; |
| 952 | |
| 953 | if (len <= 0) |
| 954 | return; |
| 955 | |
Boris Brezillon | c4f3ef2 | 2016-03-04 18:13:10 +0100 | [diff] [blame] | 956 | if (!cur_off || *cur_off != offset) |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 957 | nand->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 958 | offset + mtd->writesize, -1); |
| 959 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 960 | if (!randomize) |
| 961 | sunxi_nfc_read_buf(mtd, oob + offset, len); |
| 962 | else |
| 963 | sunxi_nfc_randomizer_read_buf(mtd, oob + offset, len, |
| 964 | false, page); |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 965 | |
Boris Brezillon | c4f3ef2 | 2016-03-04 18:13:10 +0100 | [diff] [blame] | 966 | if (cur_off) |
| 967 | *cur_off = mtd->oobsize + mtd->writesize; |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 970 | static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd, |
| 971 | const u8 *data, int data_off, |
| 972 | const u8 *oob, int oob_off, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 973 | int *cur_off, bool bbm, |
| 974 | int page) |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 975 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 976 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 977 | struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller); |
| 978 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 979 | int ret; |
| 980 | |
| 981 | if (data_off != *cur_off) |
| 982 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, data_off, -1); |
| 983 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 984 | sunxi_nfc_randomizer_write_buf(mtd, data, ecc->size, false, page); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 985 | |
Boris BREZILLON | 74eb9ff | 2015-10-20 22:16:00 +0200 | [diff] [blame] | 986 | if (data_off + ecc->size != oob_off) |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 987 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, oob_off, -1); |
| 988 | |
| 989 | ret = sunxi_nfc_wait_cmd_fifo_empty(nfc); |
| 990 | if (ret) |
| 991 | return ret; |
| 992 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 993 | sunxi_nfc_randomizer_enable(mtd); |
Boris Brezillon | cc6822f | 2016-03-04 17:56:47 +0100 | [diff] [blame] | 994 | sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd, oob, 0, bbm, page); |
| 995 | |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 996 | writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | |
| 997 | NFC_ACCESS_DIR | NFC_ECC_OP, |
| 998 | nfc->regs + NFC_REG_CMD); |
| 999 | |
Boris Brezillon | c0c9dfa | 2016-03-07 15:34:39 +0100 | [diff] [blame] | 1000 | ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1001 | sunxi_nfc_randomizer_disable(mtd); |
Boris BREZILLON | 913821b | 2015-09-30 23:45:24 +0200 | [diff] [blame] | 1002 | if (ret) |
| 1003 | return ret; |
| 1004 | |
| 1005 | *cur_off = oob_off + ecc->bytes + 4; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1010 | static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1011 | u8 *oob, int *cur_off, |
| 1012 | int page) |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1013 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1014 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1015 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1016 | int offset = ((ecc->bytes + 4) * ecc->steps); |
| 1017 | int len = mtd->oobsize - offset; |
| 1018 | |
| 1019 | if (len <= 0) |
| 1020 | return; |
| 1021 | |
Boris Brezillon | c4f3ef2 | 2016-03-04 18:13:10 +0100 | [diff] [blame] | 1022 | if (!cur_off || *cur_off != offset) |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1023 | nand->cmdfunc(mtd, NAND_CMD_RNDIN, |
| 1024 | offset + mtd->writesize, -1); |
| 1025 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1026 | sunxi_nfc_randomizer_write_buf(mtd, oob + offset, len, false, page); |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1027 | |
Boris Brezillon | c4f3ef2 | 2016-03-04 18:13:10 +0100 | [diff] [blame] | 1028 | if (cur_off) |
| 1029 | *cur_off = mtd->oobsize + mtd->writesize; |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1032 | static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd, |
| 1033 | struct nand_chip *chip, uint8_t *buf, |
| 1034 | int oob_required, int page) |
| 1035 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1036 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1037 | unsigned int max_bitflips = 0; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1038 | int ret, i, cur_off = 0; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1039 | bool raw_mode = false; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1040 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1041 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1042 | |
| 1043 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1044 | int data_off = i * ecc->size; |
| 1045 | int oob_off = i * (ecc->bytes + 4); |
| 1046 | u8 *data = buf + data_off; |
| 1047 | u8 *oob = chip->oob_poi + oob_off; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1048 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1049 | ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob, |
| 1050 | oob_off + mtd->writesize, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1051 | &cur_off, &max_bitflips, |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 1052 | !i, oob_required, page); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1053 | if (ret < 0) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1054 | return ret; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1055 | else if (ret) |
| 1056 | raw_mode = true; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1059 | if (oob_required) |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1060 | sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off, |
| 1061 | !raw_mode, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1062 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1063 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1064 | |
| 1065 | return max_bitflips; |
| 1066 | } |
| 1067 | |
Boris Brezillon | fe82cce | 2015-09-16 09:01:45 +0200 | [diff] [blame] | 1068 | static int sunxi_nfc_hw_ecc_read_subpage(struct mtd_info *mtd, |
| 1069 | struct nand_chip *chip, |
| 1070 | u32 data_offs, u32 readlen, |
| 1071 | u8 *bufpoi, int page) |
| 1072 | { |
| 1073 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1074 | int ret, i, cur_off = 0; |
| 1075 | unsigned int max_bitflips = 0; |
| 1076 | |
| 1077 | sunxi_nfc_hw_ecc_enable(mtd); |
| 1078 | |
| 1079 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1080 | for (i = data_offs / ecc->size; |
| 1081 | i < DIV_ROUND_UP(data_offs + readlen, ecc->size); i++) { |
| 1082 | int data_off = i * ecc->size; |
| 1083 | int oob_off = i * (ecc->bytes + 4); |
| 1084 | u8 *data = bufpoi + data_off; |
| 1085 | u8 *oob = chip->oob_poi + oob_off; |
| 1086 | |
| 1087 | ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, |
| 1088 | oob, |
| 1089 | oob_off + mtd->writesize, |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 1090 | &cur_off, &max_bitflips, !i, |
| 1091 | false, page); |
Boris Brezillon | fe82cce | 2015-09-16 09:01:45 +0200 | [diff] [blame] | 1092 | if (ret < 0) |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | sunxi_nfc_hw_ecc_disable(mtd); |
| 1097 | |
| 1098 | return max_bitflips; |
| 1099 | } |
| 1100 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1101 | static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, |
| 1102 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1103 | const uint8_t *buf, int oob_required, |
| 1104 | int page) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1105 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1106 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1107 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1108 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1109 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1110 | |
| 1111 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1112 | int data_off = i * ecc->size; |
| 1113 | int oob_off = i * (ecc->bytes + 4); |
| 1114 | const u8 *data = buf + data_off; |
| 1115 | const u8 *oob = chip->oob_poi + oob_off; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1116 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1117 | ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob, |
| 1118 | oob_off + mtd->writesize, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1119 | &cur_off, !i, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1120 | if (ret) |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1124 | if (oob_required || (chip->options & NAND_NEED_SCRAMBLING)) |
| 1125 | sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi, |
| 1126 | &cur_off, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1127 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1128 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd, |
| 1134 | struct nand_chip *chip, |
| 1135 | uint8_t *buf, int oob_required, |
| 1136 | int page) |
| 1137 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1138 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1139 | unsigned int max_bitflips = 0; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1140 | int ret, i, cur_off = 0; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1141 | bool raw_mode = false; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1142 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1143 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1144 | |
| 1145 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1146 | int data_off = i * (ecc->size + ecc->bytes + 4); |
| 1147 | int oob_off = data_off + ecc->size; |
| 1148 | u8 *data = buf + (i * ecc->size); |
| 1149 | u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1150 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1151 | ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob, |
| 1152 | oob_off, &cur_off, |
Boris Brezillon | 828dec1 | 2016-03-04 18:09:21 +0100 | [diff] [blame] | 1153 | &max_bitflips, !i, |
| 1154 | oob_required, |
| 1155 | page); |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1156 | if (ret < 0) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1157 | return ret; |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1158 | else if (ret) |
| 1159 | raw_mode = true; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
Boris BREZILLON | 35d0e24 | 2015-09-30 23:45:26 +0200 | [diff] [blame] | 1162 | if (oob_required) |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1163 | sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off, |
| 1164 | !raw_mode, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1165 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1166 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1167 | |
| 1168 | return max_bitflips; |
| 1169 | } |
| 1170 | |
| 1171 | static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd, |
| 1172 | struct nand_chip *chip, |
| 1173 | const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1174 | int oob_required, int page) |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1175 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1176 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1177 | int ret, i, cur_off = 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1178 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1179 | sunxi_nfc_hw_ecc_enable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1180 | |
| 1181 | for (i = 0; i < ecc->steps; i++) { |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1182 | int data_off = i * (ecc->size + ecc->bytes + 4); |
| 1183 | int oob_off = data_off + ecc->size; |
| 1184 | const u8 *data = buf + (i * ecc->size); |
| 1185 | const u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1186 | |
Boris BREZILLON | b462551 | 2015-09-30 23:45:25 +0200 | [diff] [blame] | 1187 | ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1188 | oob, oob_off, &cur_off, |
| 1189 | false, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1190 | if (ret) |
| 1191 | return ret; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1194 | if (oob_required || (chip->options & NAND_NEED_SCRAMBLING)) |
| 1195 | sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi, |
| 1196 | &cur_off, page); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1197 | |
Boris BREZILLON | c9118ec | 2015-09-30 23:45:23 +0200 | [diff] [blame] | 1198 | sunxi_nfc_hw_ecc_disable(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Boris Brezillon | 1c1bdd6 | 2015-09-02 15:05:52 +0200 | [diff] [blame] | 1203 | static int sunxi_nfc_hw_common_ecc_read_oob(struct mtd_info *mtd, |
| 1204 | struct nand_chip *chip, |
| 1205 | int page) |
| 1206 | { |
| 1207 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1208 | |
| 1209 | chip->pagebuf = -1; |
| 1210 | |
| 1211 | return chip->ecc.read_page(mtd, chip, chip->buffers->databuf, 1, page); |
| 1212 | } |
| 1213 | |
| 1214 | static int sunxi_nfc_hw_common_ecc_write_oob(struct mtd_info *mtd, |
| 1215 | struct nand_chip *chip, |
| 1216 | int page) |
| 1217 | { |
| 1218 | int ret, status; |
| 1219 | |
| 1220 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page); |
| 1221 | |
| 1222 | chip->pagebuf = -1; |
| 1223 | |
| 1224 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 1225 | ret = chip->ecc.write_page(mtd, chip, chip->buffers->databuf, 1, page); |
| 1226 | if (ret) |
| 1227 | return ret; |
| 1228 | |
| 1229 | /* Send command to program the OOB data */ |
| 1230 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1231 | |
| 1232 | status = chip->waitfunc(mtd, chip); |
| 1233 | |
| 1234 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 1235 | } |
| 1236 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 1237 | static const s32 tWB_lut[] = {6, 12, 16, 20}; |
| 1238 | static const s32 tRHW_lut[] = {4, 8, 12, 20}; |
| 1239 | |
| 1240 | static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration, |
| 1241 | u32 clk_period) |
| 1242 | { |
| 1243 | u32 clk_cycles = DIV_ROUND_UP(duration, clk_period); |
| 1244 | int i; |
| 1245 | |
| 1246 | for (i = 0; i < lut_size; i++) { |
| 1247 | if (clk_cycles <= lut[i]) |
| 1248 | return i; |
| 1249 | } |
| 1250 | |
| 1251 | /* Doesn't fit */ |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
| 1255 | #define sunxi_nand_lookup_timing(l, p, c) \ |
| 1256 | _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c) |
| 1257 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1258 | static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip, |
| 1259 | const struct nand_sdr_timings *timings) |
| 1260 | { |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 1261 | struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1262 | u32 min_clk_period = 0; |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 1263 | s32 tWB, tADL, tWHR, tRHW, tCAD; |
Boris Brezillon | 2d43457 | 2015-12-02 15:57:20 +0100 | [diff] [blame] | 1264 | long real_clk_rate; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1265 | |
| 1266 | /* T1 <=> tCLS */ |
| 1267 | if (timings->tCLS_min > min_clk_period) |
| 1268 | min_clk_period = timings->tCLS_min; |
| 1269 | |
| 1270 | /* T2 <=> tCLH */ |
| 1271 | if (timings->tCLH_min > min_clk_period) |
| 1272 | min_clk_period = timings->tCLH_min; |
| 1273 | |
| 1274 | /* T3 <=> tCS */ |
| 1275 | if (timings->tCS_min > min_clk_period) |
| 1276 | min_clk_period = timings->tCS_min; |
| 1277 | |
| 1278 | /* T4 <=> tCH */ |
| 1279 | if (timings->tCH_min > min_clk_period) |
| 1280 | min_clk_period = timings->tCH_min; |
| 1281 | |
| 1282 | /* T5 <=> tWP */ |
| 1283 | if (timings->tWP_min > min_clk_period) |
| 1284 | min_clk_period = timings->tWP_min; |
| 1285 | |
| 1286 | /* T6 <=> tWH */ |
| 1287 | if (timings->tWH_min > min_clk_period) |
| 1288 | min_clk_period = timings->tWH_min; |
| 1289 | |
| 1290 | /* T7 <=> tALS */ |
| 1291 | if (timings->tALS_min > min_clk_period) |
| 1292 | min_clk_period = timings->tALS_min; |
| 1293 | |
| 1294 | /* T8 <=> tDS */ |
| 1295 | if (timings->tDS_min > min_clk_period) |
| 1296 | min_clk_period = timings->tDS_min; |
| 1297 | |
| 1298 | /* T9 <=> tDH */ |
| 1299 | if (timings->tDH_min > min_clk_period) |
| 1300 | min_clk_period = timings->tDH_min; |
| 1301 | |
| 1302 | /* T10 <=> tRR */ |
| 1303 | if (timings->tRR_min > (min_clk_period * 3)) |
| 1304 | min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3); |
| 1305 | |
| 1306 | /* T11 <=> tALH */ |
| 1307 | if (timings->tALH_min > min_clk_period) |
| 1308 | min_clk_period = timings->tALH_min; |
| 1309 | |
| 1310 | /* T12 <=> tRP */ |
| 1311 | if (timings->tRP_min > min_clk_period) |
| 1312 | min_clk_period = timings->tRP_min; |
| 1313 | |
| 1314 | /* T13 <=> tREH */ |
| 1315 | if (timings->tREH_min > min_clk_period) |
| 1316 | min_clk_period = timings->tREH_min; |
| 1317 | |
| 1318 | /* T14 <=> tRC */ |
| 1319 | if (timings->tRC_min > (min_clk_period * 2)) |
| 1320 | min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2); |
| 1321 | |
| 1322 | /* T15 <=> tWC */ |
| 1323 | if (timings->tWC_min > (min_clk_period * 2)) |
| 1324 | min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2); |
| 1325 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 1326 | /* T16 - T19 + tCAD */ |
Boris Brezillon | 5abcd95 | 2015-11-11 22:30:30 +0100 | [diff] [blame] | 1327 | if (timings->tWB_max > (min_clk_period * 20)) |
| 1328 | min_clk_period = DIV_ROUND_UP(timings->tWB_max, 20); |
| 1329 | |
| 1330 | if (timings->tADL_min > (min_clk_period * 32)) |
| 1331 | min_clk_period = DIV_ROUND_UP(timings->tADL_min, 32); |
| 1332 | |
| 1333 | if (timings->tWHR_min > (min_clk_period * 32)) |
| 1334 | min_clk_period = DIV_ROUND_UP(timings->tWHR_min, 32); |
| 1335 | |
| 1336 | if (timings->tRHW_min > (min_clk_period * 20)) |
| 1337 | min_clk_period = DIV_ROUND_UP(timings->tRHW_min, 20); |
| 1338 | |
Roy Spliet | 9c61829 | 2015-06-26 11:00:10 +0200 | [diff] [blame] | 1339 | tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max, |
| 1340 | min_clk_period); |
| 1341 | if (tWB < 0) { |
| 1342 | dev_err(nfc->dev, "unsupported tWB\n"); |
| 1343 | return tWB; |
| 1344 | } |
| 1345 | |
| 1346 | tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3; |
| 1347 | if (tADL > 3) { |
| 1348 | dev_err(nfc->dev, "unsupported tADL\n"); |
| 1349 | return -EINVAL; |
| 1350 | } |
| 1351 | |
| 1352 | tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3; |
| 1353 | if (tWHR > 3) { |
| 1354 | dev_err(nfc->dev, "unsupported tWHR\n"); |
| 1355 | return -EINVAL; |
| 1356 | } |
| 1357 | |
| 1358 | tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min, |
| 1359 | min_clk_period); |
| 1360 | if (tRHW < 0) { |
| 1361 | dev_err(nfc->dev, "unsupported tRHW\n"); |
| 1362 | return tRHW; |
| 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * TODO: according to ONFI specs this value only applies for DDR NAND, |
| 1367 | * but Allwinner seems to set this to 0x7. Mimic them for now. |
| 1368 | */ |
| 1369 | tCAD = 0x7; |
| 1370 | |
| 1371 | /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */ |
| 1372 | chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1373 | |
| 1374 | /* Convert min_clk_period from picoseconds to nanoseconds */ |
| 1375 | min_clk_period = DIV_ROUND_UP(min_clk_period, 1000); |
| 1376 | |
| 1377 | /* |
Boris Brezillon | 2f9992e | 2015-12-02 15:10:40 +0100 | [diff] [blame] | 1378 | * Unlike what is stated in Allwinner datasheet, the clk_rate should |
| 1379 | * be set to (1 / min_clk_period), and not (2 / min_clk_period). |
| 1380 | * This new formula was verified with a scope and validated by |
| 1381 | * Allwinner engineers. |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1382 | */ |
Boris Brezillon | 2f9992e | 2015-12-02 15:10:40 +0100 | [diff] [blame] | 1383 | chip->clk_rate = NSEC_PER_SEC / min_clk_period; |
Boris Brezillon | 2d43457 | 2015-12-02 15:57:20 +0100 | [diff] [blame] | 1384 | real_clk_rate = clk_round_rate(nfc->mod_clk, chip->clk_rate); |
| 1385 | |
| 1386 | /* |
| 1387 | * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data |
| 1388 | * output cycle timings shall be used if the host drives tRC less than |
| 1389 | * 30 ns. |
| 1390 | */ |
| 1391 | min_clk_period = NSEC_PER_SEC / real_clk_rate; |
| 1392 | chip->timing_ctl = ((min_clk_period * 2) < 30) ? |
| 1393 | NFC_TIMING_CTL_EDO : 0; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1394 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | |
| 1398 | static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip, |
| 1399 | struct device_node *np) |
| 1400 | { |
Boris BREZILLON | 32e9f2d | 2015-12-10 09:00:26 +0100 | [diff] [blame] | 1401 | struct mtd_info *mtd = nand_to_mtd(&chip->nand); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1402 | const struct nand_sdr_timings *timings; |
| 1403 | int ret; |
| 1404 | int mode; |
| 1405 | |
| 1406 | mode = onfi_get_async_timing_mode(&chip->nand); |
| 1407 | if (mode == ONFI_TIMING_MODE_UNKNOWN) { |
| 1408 | mode = chip->nand.onfi_timing_mode_default; |
| 1409 | } else { |
| 1410 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {}; |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1411 | int i; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1412 | |
| 1413 | mode = fls(mode) - 1; |
| 1414 | if (mode < 0) |
| 1415 | mode = 0; |
| 1416 | |
| 1417 | feature[0] = mode; |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1418 | for (i = 0; i < chip->nsels; i++) { |
Boris BREZILLON | 32e9f2d | 2015-12-10 09:00:26 +0100 | [diff] [blame] | 1419 | chip->nand.select_chip(mtd, i); |
| 1420 | ret = chip->nand.onfi_set_features(mtd, &chip->nand, |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1421 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1422 | feature); |
Boris BREZILLON | 32e9f2d | 2015-12-10 09:00:26 +0100 | [diff] [blame] | 1423 | chip->nand.select_chip(mtd, -1); |
Stefan Roese | 7eadd47 | 2015-08-28 14:45:21 +0200 | [diff] [blame] | 1424 | if (ret) |
| 1425 | return ret; |
| 1426 | } |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | timings = onfi_async_timing_mode_to_sdr_timings(mode); |
| 1430 | if (IS_ERR(timings)) |
| 1431 | return PTR_ERR(timings); |
| 1432 | |
| 1433 | return sunxi_nand_chip_set_timings(chip, timings); |
| 1434 | } |
| 1435 | |
Boris Brezillon | c66811e | 2016-02-03 20:05:13 +0100 | [diff] [blame] | 1436 | static int sunxi_nand_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 1437 | struct mtd_oob_region *oobregion) |
| 1438 | { |
| 1439 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1440 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1441 | |
| 1442 | if (section >= ecc->steps) |
| 1443 | return -ERANGE; |
| 1444 | |
| 1445 | oobregion->offset = section * (ecc->bytes + 4) + 4; |
| 1446 | oobregion->length = ecc->bytes; |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
| 1450 | |
| 1451 | static int sunxi_nand_ooblayout_free(struct mtd_info *mtd, int section, |
| 1452 | struct mtd_oob_region *oobregion) |
| 1453 | { |
| 1454 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 1455 | struct nand_ecc_ctrl *ecc = &nand->ecc; |
| 1456 | |
| 1457 | if (section > ecc->steps) |
| 1458 | return -ERANGE; |
| 1459 | |
| 1460 | /* |
| 1461 | * The first 2 bytes are used for BB markers, hence we |
| 1462 | * only have 2 bytes available in the first user data |
| 1463 | * section. |
| 1464 | */ |
| 1465 | if (!section && ecc->mode == NAND_ECC_HW) { |
| 1466 | oobregion->offset = 2; |
| 1467 | oobregion->length = 2; |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | oobregion->offset = section * (ecc->bytes + 4); |
| 1473 | |
| 1474 | if (section < ecc->steps) |
| 1475 | oobregion->length = 4; |
| 1476 | else |
| 1477 | oobregion->offset = mtd->oobsize - oobregion->offset; |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops = { |
| 1483 | .ecc = sunxi_nand_ooblayout_ecc, |
| 1484 | .free = sunxi_nand_ooblayout_free, |
| 1485 | }; |
| 1486 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1487 | static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd, |
| 1488 | struct nand_ecc_ctrl *ecc, |
| 1489 | struct device_node *np) |
| 1490 | { |
| 1491 | static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 }; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1492 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1493 | struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand); |
| 1494 | struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller); |
| 1495 | struct sunxi_nand_hw_ecc *data; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1496 | int nsectors; |
| 1497 | int ret; |
| 1498 | int i; |
| 1499 | |
| 1500 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1501 | if (!data) |
| 1502 | return -ENOMEM; |
| 1503 | |
| 1504 | /* Add ECC info retrieval from DT */ |
| 1505 | for (i = 0; i < ARRAY_SIZE(strengths); i++) { |
| 1506 | if (ecc->strength <= strengths[i]) |
| 1507 | break; |
| 1508 | } |
| 1509 | |
| 1510 | if (i >= ARRAY_SIZE(strengths)) { |
| 1511 | dev_err(nfc->dev, "unsupported strength\n"); |
| 1512 | ret = -ENOTSUPP; |
| 1513 | goto err; |
| 1514 | } |
| 1515 | |
| 1516 | data->mode = i; |
| 1517 | |
| 1518 | /* HW ECC always request ECC bytes for 1024 bytes blocks */ |
| 1519 | ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8); |
| 1520 | |
| 1521 | /* HW ECC always work with even numbers of ECC bytes */ |
| 1522 | ecc->bytes = ALIGN(ecc->bytes, 2); |
| 1523 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1524 | nsectors = mtd->writesize / ecc->size; |
| 1525 | |
| 1526 | if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) { |
| 1527 | ret = -EINVAL; |
| 1528 | goto err; |
| 1529 | } |
| 1530 | |
Boris Brezillon | 1c1bdd6 | 2015-09-02 15:05:52 +0200 | [diff] [blame] | 1531 | ecc->read_oob = sunxi_nfc_hw_common_ecc_read_oob; |
| 1532 | ecc->write_oob = sunxi_nfc_hw_common_ecc_write_oob; |
Boris Brezillon | c66811e | 2016-02-03 20:05:13 +0100 | [diff] [blame] | 1533 | mtd_set_ooblayout(mtd, &sunxi_nand_ooblayout_ops); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1534 | ecc->priv = data; |
| 1535 | |
| 1536 | return 0; |
| 1537 | |
| 1538 | err: |
| 1539 | kfree(data); |
| 1540 | |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc) |
| 1545 | { |
| 1546 | kfree(ecc->priv); |
| 1547 | } |
| 1548 | |
| 1549 | static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd, |
| 1550 | struct nand_ecc_ctrl *ecc, |
| 1551 | struct device_node *np) |
| 1552 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1553 | int ret; |
| 1554 | |
| 1555 | ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np); |
| 1556 | if (ret) |
| 1557 | return ret; |
| 1558 | |
| 1559 | ecc->read_page = sunxi_nfc_hw_ecc_read_page; |
| 1560 | ecc->write_page = sunxi_nfc_hw_ecc_write_page; |
Boris Brezillon | 1c1bdd6 | 2015-09-02 15:05:52 +0200 | [diff] [blame] | 1561 | ecc->read_oob_raw = nand_read_oob_std; |
| 1562 | ecc->write_oob_raw = nand_write_oob_std; |
Boris Brezillon | fe82cce | 2015-09-16 09:01:45 +0200 | [diff] [blame] | 1563 | ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1564 | |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
| 1568 | static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd, |
| 1569 | struct nand_ecc_ctrl *ecc, |
| 1570 | struct device_node *np) |
| 1571 | { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1572 | int ret; |
| 1573 | |
| 1574 | ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np); |
| 1575 | if (ret) |
| 1576 | return ret; |
| 1577 | |
| 1578 | ecc->prepad = 4; |
| 1579 | ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page; |
| 1580 | ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page; |
Boris Brezillon | 1c1bdd6 | 2015-09-02 15:05:52 +0200 | [diff] [blame] | 1581 | ecc->read_oob_raw = nand_read_oob_syndrome; |
| 1582 | ecc->write_oob_raw = nand_write_oob_syndrome; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1583 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc) |
| 1588 | { |
| 1589 | switch (ecc->mode) { |
| 1590 | case NAND_ECC_HW: |
| 1591 | case NAND_ECC_HW_SYNDROME: |
| 1592 | sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc); |
| 1593 | break; |
| 1594 | case NAND_ECC_NONE: |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1595 | default: |
| 1596 | break; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc, |
| 1601 | struct device_node *np) |
| 1602 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1603 | struct nand_chip *nand = mtd_to_nand(mtd); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1604 | int ret; |
| 1605 | |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1606 | if (!ecc->size) { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1607 | ecc->size = nand->ecc_step_ds; |
| 1608 | ecc->strength = nand->ecc_strength_ds; |
| 1609 | } |
| 1610 | |
| 1611 | if (!ecc->size || !ecc->strength) |
| 1612 | return -EINVAL; |
| 1613 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1614 | switch (ecc->mode) { |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1615 | case NAND_ECC_HW: |
| 1616 | ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np); |
| 1617 | if (ret) |
| 1618 | return ret; |
| 1619 | break; |
| 1620 | case NAND_ECC_HW_SYNDROME: |
| 1621 | ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np); |
| 1622 | if (ret) |
| 1623 | return ret; |
| 1624 | break; |
| 1625 | case NAND_ECC_NONE: |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1626 | case NAND_ECC_SOFT: |
| 1627 | break; |
| 1628 | default: |
| 1629 | return -EINVAL; |
| 1630 | } |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc, |
| 1636 | struct device_node *np) |
| 1637 | { |
| 1638 | const struct nand_sdr_timings *timings; |
| 1639 | struct sunxi_nand_chip *chip; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1640 | struct mtd_info *mtd; |
| 1641 | struct nand_chip *nand; |
| 1642 | int nsels; |
| 1643 | int ret; |
| 1644 | int i; |
| 1645 | u32 tmp; |
| 1646 | |
| 1647 | if (!of_get_property(np, "reg", &nsels)) |
| 1648 | return -EINVAL; |
| 1649 | |
| 1650 | nsels /= sizeof(u32); |
| 1651 | if (!nsels) { |
| 1652 | dev_err(dev, "invalid reg property size\n"); |
| 1653 | return -EINVAL; |
| 1654 | } |
| 1655 | |
| 1656 | chip = devm_kzalloc(dev, |
| 1657 | sizeof(*chip) + |
| 1658 | (nsels * sizeof(struct sunxi_nand_chip_sel)), |
| 1659 | GFP_KERNEL); |
| 1660 | if (!chip) { |
| 1661 | dev_err(dev, "could not allocate chip\n"); |
| 1662 | return -ENOMEM; |
| 1663 | } |
| 1664 | |
| 1665 | chip->nsels = nsels; |
| 1666 | chip->selected = -1; |
| 1667 | |
| 1668 | for (i = 0; i < nsels; i++) { |
| 1669 | ret = of_property_read_u32_index(np, "reg", i, &tmp); |
| 1670 | if (ret) { |
| 1671 | dev_err(dev, "could not retrieve reg property: %d\n", |
| 1672 | ret); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
| 1676 | if (tmp > NFC_MAX_CS) { |
| 1677 | dev_err(dev, |
| 1678 | "invalid reg value: %u (max CS = 7)\n", |
| 1679 | tmp); |
| 1680 | return -EINVAL; |
| 1681 | } |
| 1682 | |
| 1683 | if (test_and_set_bit(tmp, &nfc->assigned_cs)) { |
| 1684 | dev_err(dev, "CS %d already assigned\n", tmp); |
| 1685 | return -EINVAL; |
| 1686 | } |
| 1687 | |
| 1688 | chip->sels[i].cs = tmp; |
| 1689 | |
| 1690 | if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) && |
| 1691 | tmp < 2) { |
| 1692 | chip->sels[i].rb.type = RB_NATIVE; |
| 1693 | chip->sels[i].rb.info.nativeid = tmp; |
| 1694 | } else { |
| 1695 | ret = of_get_named_gpio(np, "rb-gpios", i); |
| 1696 | if (ret >= 0) { |
| 1697 | tmp = ret; |
| 1698 | chip->sels[i].rb.type = RB_GPIO; |
| 1699 | chip->sels[i].rb.info.gpio = tmp; |
| 1700 | ret = devm_gpio_request(dev, tmp, "nand-rb"); |
| 1701 | if (ret) |
| 1702 | return ret; |
| 1703 | |
| 1704 | ret = gpio_direction_input(tmp); |
| 1705 | if (ret) |
| 1706 | return ret; |
| 1707 | } else { |
| 1708 | chip->sels[i].rb.type = RB_NONE; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1713 | nand = &chip->nand; |
| 1714 | /* Default tR value specified in the ONFI spec (chapter 4.15.1) */ |
| 1715 | nand->chip_delay = 200; |
| 1716 | nand->controller = &nfc->controller; |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1717 | /* |
| 1718 | * Set the ECC mode to the default value in case nothing is specified |
| 1719 | * in the DT. |
| 1720 | */ |
| 1721 | nand->ecc.mode = NAND_ECC_HW; |
Brian Norris | 6375219 | 2015-10-30 20:33:23 -0700 | [diff] [blame] | 1722 | nand_set_flash_node(nand, np); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1723 | nand->select_chip = sunxi_nfc_select_chip; |
| 1724 | nand->cmd_ctrl = sunxi_nfc_cmd_ctrl; |
| 1725 | nand->read_buf = sunxi_nfc_read_buf; |
| 1726 | nand->write_buf = sunxi_nfc_write_buf; |
| 1727 | nand->read_byte = sunxi_nfc_read_byte; |
| 1728 | |
Boris BREZILLON | 32e9f2d | 2015-12-10 09:00:26 +0100 | [diff] [blame] | 1729 | mtd = nand_to_mtd(nand); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1730 | mtd->dev.parent = dev; |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1731 | |
Boris Brezillon | 9edb470 | 2015-12-02 16:00:57 +0100 | [diff] [blame] | 1732 | timings = onfi_async_timing_mode_to_sdr_timings(0); |
| 1733 | if (IS_ERR(timings)) { |
| 1734 | ret = PTR_ERR(timings); |
| 1735 | dev_err(dev, |
| 1736 | "could not retrieve timings for ONFI mode 0: %d\n", |
| 1737 | ret); |
| 1738 | return ret; |
| 1739 | } |
| 1740 | |
| 1741 | ret = sunxi_nand_chip_set_timings(chip, timings); |
| 1742 | if (ret) { |
| 1743 | dev_err(dev, "could not configure chip timings: %d\n", ret); |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1747 | ret = nand_scan_ident(mtd, nsels, NULL); |
| 1748 | if (ret) |
| 1749 | return ret; |
| 1750 | |
Boris BREZILLON | a3d22a5 | 2015-09-02 10:30:25 +0200 | [diff] [blame] | 1751 | if (nand->bbt_options & NAND_BBT_USE_FLASH) |
| 1752 | nand->bbt_options |= NAND_BBT_NO_OOB; |
| 1753 | |
Boris BREZILLON | 4be4e03 | 2015-12-02 12:01:07 +0100 | [diff] [blame] | 1754 | if (nand->options & NAND_NEED_SCRAMBLING) |
| 1755 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 1756 | |
Boris Brezillon | fe82cce | 2015-09-16 09:01:45 +0200 | [diff] [blame] | 1757 | nand->options |= NAND_SUBPAGE_READ; |
| 1758 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1759 | ret = sunxi_nand_chip_init_timings(chip, np); |
| 1760 | if (ret) { |
| 1761 | dev_err(dev, "could not configure chip timings: %d\n", ret); |
| 1762 | return ret; |
| 1763 | } |
| 1764 | |
| 1765 | ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np); |
| 1766 | if (ret) { |
| 1767 | dev_err(dev, "ECC init failed: %d\n", ret); |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
| 1771 | ret = nand_scan_tail(mtd); |
| 1772 | if (ret) { |
| 1773 | dev_err(dev, "nand_scan_tail failed: %d\n", ret); |
| 1774 | return ret; |
| 1775 | } |
| 1776 | |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1777 | ret = mtd_device_register(mtd, NULL, 0); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1778 | if (ret) { |
| 1779 | dev_err(dev, "failed to register mtd device: %d\n", ret); |
| 1780 | nand_release(mtd); |
| 1781 | return ret; |
| 1782 | } |
| 1783 | |
| 1784 | list_add_tail(&chip->node, &nfc->chips); |
| 1785 | |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc) |
| 1790 | { |
| 1791 | struct device_node *np = dev->of_node; |
| 1792 | struct device_node *nand_np; |
| 1793 | int nchips = of_get_child_count(np); |
| 1794 | int ret; |
| 1795 | |
| 1796 | if (nchips > 8) { |
| 1797 | dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips); |
| 1798 | return -EINVAL; |
| 1799 | } |
| 1800 | |
| 1801 | for_each_child_of_node(np, nand_np) { |
| 1802 | ret = sunxi_nand_chip_init(dev, nfc, nand_np); |
Julia Lawall | a81c0f0 | 2015-11-18 23:04:12 +0100 | [diff] [blame] | 1803 | if (ret) { |
| 1804 | of_node_put(nand_np); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1805 | return ret; |
Julia Lawall | a81c0f0 | 2015-11-18 23:04:12 +0100 | [diff] [blame] | 1806 | } |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc) |
| 1813 | { |
| 1814 | struct sunxi_nand_chip *chip; |
| 1815 | |
| 1816 | while (!list_empty(&nfc->chips)) { |
| 1817 | chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip, |
| 1818 | node); |
Boris BREZILLON | 32e9f2d | 2015-12-10 09:00:26 +0100 | [diff] [blame] | 1819 | nand_release(nand_to_mtd(&chip->nand)); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1820 | sunxi_nand_ecc_cleanup(&chip->nand.ecc); |
Boris BREZILLON | 8e375cc | 2015-09-13 18:14:43 +0200 | [diff] [blame] | 1821 | list_del(&chip->node); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | static int sunxi_nfc_probe(struct platform_device *pdev) |
| 1826 | { |
| 1827 | struct device *dev = &pdev->dev; |
| 1828 | struct resource *r; |
| 1829 | struct sunxi_nfc *nfc; |
| 1830 | int irq; |
| 1831 | int ret; |
| 1832 | |
| 1833 | nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL); |
| 1834 | if (!nfc) |
| 1835 | return -ENOMEM; |
| 1836 | |
| 1837 | nfc->dev = dev; |
| 1838 | spin_lock_init(&nfc->controller.lock); |
| 1839 | init_waitqueue_head(&nfc->controller.wq); |
| 1840 | INIT_LIST_HEAD(&nfc->chips); |
| 1841 | |
| 1842 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1843 | nfc->regs = devm_ioremap_resource(dev, r); |
| 1844 | if (IS_ERR(nfc->regs)) |
| 1845 | return PTR_ERR(nfc->regs); |
| 1846 | |
| 1847 | irq = platform_get_irq(pdev, 0); |
| 1848 | if (irq < 0) { |
| 1849 | dev_err(dev, "failed to retrieve irq\n"); |
| 1850 | return irq; |
| 1851 | } |
| 1852 | |
| 1853 | nfc->ahb_clk = devm_clk_get(dev, "ahb"); |
| 1854 | if (IS_ERR(nfc->ahb_clk)) { |
| 1855 | dev_err(dev, "failed to retrieve ahb clk\n"); |
| 1856 | return PTR_ERR(nfc->ahb_clk); |
| 1857 | } |
| 1858 | |
| 1859 | ret = clk_prepare_enable(nfc->ahb_clk); |
| 1860 | if (ret) |
| 1861 | return ret; |
| 1862 | |
| 1863 | nfc->mod_clk = devm_clk_get(dev, "mod"); |
| 1864 | if (IS_ERR(nfc->mod_clk)) { |
| 1865 | dev_err(dev, "failed to retrieve mod clk\n"); |
| 1866 | ret = PTR_ERR(nfc->mod_clk); |
| 1867 | goto out_ahb_clk_unprepare; |
| 1868 | } |
| 1869 | |
| 1870 | ret = clk_prepare_enable(nfc->mod_clk); |
| 1871 | if (ret) |
| 1872 | goto out_ahb_clk_unprepare; |
| 1873 | |
| 1874 | ret = sunxi_nfc_rst(nfc); |
| 1875 | if (ret) |
| 1876 | goto out_mod_clk_unprepare; |
| 1877 | |
| 1878 | writel(0, nfc->regs + NFC_REG_INT); |
| 1879 | ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt, |
| 1880 | 0, "sunxi-nand", nfc); |
| 1881 | if (ret) |
| 1882 | goto out_mod_clk_unprepare; |
| 1883 | |
| 1884 | platform_set_drvdata(pdev, nfc); |
| 1885 | |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1886 | ret = sunxi_nand_chips_init(dev, nfc); |
| 1887 | if (ret) { |
| 1888 | dev_err(dev, "failed to init nand chips\n"); |
| 1889 | goto out_mod_clk_unprepare; |
| 1890 | } |
| 1891 | |
| 1892 | return 0; |
| 1893 | |
| 1894 | out_mod_clk_unprepare: |
| 1895 | clk_disable_unprepare(nfc->mod_clk); |
| 1896 | out_ahb_clk_unprepare: |
| 1897 | clk_disable_unprepare(nfc->ahb_clk); |
| 1898 | |
| 1899 | return ret; |
| 1900 | } |
| 1901 | |
| 1902 | static int sunxi_nfc_remove(struct platform_device *pdev) |
| 1903 | { |
| 1904 | struct sunxi_nfc *nfc = platform_get_drvdata(pdev); |
| 1905 | |
| 1906 | sunxi_nand_chips_cleanup(nfc); |
Boris Brezillon | dd26a45 | 2016-03-04 18:26:40 +0100 | [diff] [blame] | 1907 | clk_disable_unprepare(nfc->mod_clk); |
| 1908 | clk_disable_unprepare(nfc->ahb_clk); |
Boris BREZILLON | 1fef62c | 2014-10-21 15:08:41 +0200 | [diff] [blame] | 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | static const struct of_device_id sunxi_nfc_ids[] = { |
| 1914 | { .compatible = "allwinner,sun4i-a10-nand" }, |
| 1915 | { /* sentinel */ } |
| 1916 | }; |
| 1917 | MODULE_DEVICE_TABLE(of, sunxi_nfc_ids); |
| 1918 | |
| 1919 | static struct platform_driver sunxi_nfc_driver = { |
| 1920 | .driver = { |
| 1921 | .name = "sunxi_nand", |
| 1922 | .of_match_table = sunxi_nfc_ids, |
| 1923 | }, |
| 1924 | .probe = sunxi_nfc_probe, |
| 1925 | .remove = sunxi_nfc_remove, |
| 1926 | }; |
| 1927 | module_platform_driver(sunxi_nfc_driver); |
| 1928 | |
| 1929 | MODULE_LICENSE("GPL v2"); |
| 1930 | MODULE_AUTHOR("Boris BREZILLON"); |
| 1931 | MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver"); |
| 1932 | MODULE_ALIAS("platform:sunxi_nand"); |