Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 ATMEL |
| 3 | * Copyright 2017 Free Electrons |
| 4 | * |
| 5 | * Author: Boris Brezillon <boris.brezillon@free-electrons.com> |
| 6 | * |
| 7 | * Derived from the atmel_nand.c driver which contained the following |
| 8 | * copyrights: |
| 9 | * |
| 10 | * Copyright 2003 Rick Bronson |
| 11 | * |
Boris Brezillon | 187c5448 | 2018-02-05 23:02:02 +0100 | [diff] [blame] | 12 | * Derived from drivers/mtd/nand/autcpu12.c (removed in v3.8) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 13 | * Copyright 2001 Thomas Gleixner (gleixner@autronix.de) |
| 14 | * |
Boris Brezillon | 187c5448 | 2018-02-05 23:02:02 +0100 | [diff] [blame] | 15 | * Derived from drivers/mtd/spia.c (removed in v3.8) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 16 | * Copyright 2000 Steven J. Hill (sjhill@cotw.com) |
| 17 | * |
| 18 | * |
| 19 | * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263 |
| 20 | * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright 2007 |
| 21 | * |
| 22 | * Derived from Das U-Boot source code |
| 23 | * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c) |
| 24 | * Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas |
| 25 | * |
| 26 | * Add Programmable Multibit ECC support for various AT91 SoC |
| 27 | * Copyright 2012 ATMEL, Hong Xu |
| 28 | * |
| 29 | * Add Nand Flash Controller support for SAMA5 SoC |
| 30 | * Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com) |
| 31 | * |
| 32 | * This program is free software; you can redistribute it and/or modify |
| 33 | * it under the terms of the GNU General Public License version 2 as |
| 34 | * published by the Free Software Foundation. |
| 35 | * |
| 36 | * A few words about the naming convention in this file. This convention |
| 37 | * applies to structure and function names. |
| 38 | * |
| 39 | * Prefixes: |
| 40 | * |
| 41 | * - atmel_nand_: all generic structures/functions |
| 42 | * - atmel_smc_nand_: all structures/functions specific to the SMC interface |
| 43 | * (at91sam9 and avr32 SoCs) |
| 44 | * - atmel_hsmc_nand_: all structures/functions specific to the HSMC interface |
| 45 | * (sama5 SoCs and later) |
| 46 | * - atmel_nfc_: all structures/functions used to manipulate the NFC sub-block |
| 47 | * that is available in the HSMC block |
| 48 | * - <soc>_nand_: all SoC specific structures/functions |
| 49 | */ |
| 50 | |
| 51 | #include <linux/clk.h> |
| 52 | #include <linux/dma-mapping.h> |
| 53 | #include <linux/dmaengine.h> |
| 54 | #include <linux/genalloc.h> |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 55 | #include <linux/gpio/consumer.h> |
| 56 | #include <linux/interrupt.h> |
| 57 | #include <linux/mfd/syscon.h> |
| 58 | #include <linux/mfd/syscon/atmel-matrix.h> |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 59 | #include <linux/mfd/syscon/atmel-smc.h> |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 60 | #include <linux/module.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 61 | #include <linux/mtd/rawnand.h> |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 62 | #include <linux/of_address.h> |
| 63 | #include <linux/of_irq.h> |
| 64 | #include <linux/of_platform.h> |
| 65 | #include <linux/iopoll.h> |
| 66 | #include <linux/platform_device.h> |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 67 | #include <linux/regmap.h> |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 68 | #include <soc/at91/atmel-sfr.h> |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 69 | |
| 70 | #include "pmecc.h" |
| 71 | |
| 72 | #define ATMEL_HSMC_NFC_CFG 0x0 |
| 73 | #define ATMEL_HSMC_NFC_CFG_SPARESIZE(x) (((x) / 4) << 24) |
| 74 | #define ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK GENMASK(30, 24) |
| 75 | #define ATMEL_HSMC_NFC_CFG_DTO(cyc, mul) (((cyc) << 16) | ((mul) << 20)) |
| 76 | #define ATMEL_HSMC_NFC_CFG_DTO_MAX GENMASK(22, 16) |
| 77 | #define ATMEL_HSMC_NFC_CFG_RBEDGE BIT(13) |
| 78 | #define ATMEL_HSMC_NFC_CFG_FALLING_EDGE BIT(12) |
| 79 | #define ATMEL_HSMC_NFC_CFG_RSPARE BIT(9) |
| 80 | #define ATMEL_HSMC_NFC_CFG_WSPARE BIT(8) |
| 81 | #define ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK GENMASK(2, 0) |
| 82 | #define ATMEL_HSMC_NFC_CFG_PAGESIZE(x) (fls((x) / 512) - 1) |
| 83 | |
| 84 | #define ATMEL_HSMC_NFC_CTRL 0x4 |
| 85 | #define ATMEL_HSMC_NFC_CTRL_EN BIT(0) |
| 86 | #define ATMEL_HSMC_NFC_CTRL_DIS BIT(1) |
| 87 | |
| 88 | #define ATMEL_HSMC_NFC_SR 0x8 |
| 89 | #define ATMEL_HSMC_NFC_IER 0xc |
| 90 | #define ATMEL_HSMC_NFC_IDR 0x10 |
| 91 | #define ATMEL_HSMC_NFC_IMR 0x14 |
| 92 | #define ATMEL_HSMC_NFC_SR_ENABLED BIT(1) |
| 93 | #define ATMEL_HSMC_NFC_SR_RB_RISE BIT(4) |
| 94 | #define ATMEL_HSMC_NFC_SR_RB_FALL BIT(5) |
| 95 | #define ATMEL_HSMC_NFC_SR_BUSY BIT(8) |
| 96 | #define ATMEL_HSMC_NFC_SR_WR BIT(11) |
| 97 | #define ATMEL_HSMC_NFC_SR_CSID GENMASK(14, 12) |
| 98 | #define ATMEL_HSMC_NFC_SR_XFRDONE BIT(16) |
| 99 | #define ATMEL_HSMC_NFC_SR_CMDDONE BIT(17) |
| 100 | #define ATMEL_HSMC_NFC_SR_DTOE BIT(20) |
| 101 | #define ATMEL_HSMC_NFC_SR_UNDEF BIT(21) |
| 102 | #define ATMEL_HSMC_NFC_SR_AWB BIT(22) |
| 103 | #define ATMEL_HSMC_NFC_SR_NFCASE BIT(23) |
| 104 | #define ATMEL_HSMC_NFC_SR_ERRORS (ATMEL_HSMC_NFC_SR_DTOE | \ |
| 105 | ATMEL_HSMC_NFC_SR_UNDEF | \ |
| 106 | ATMEL_HSMC_NFC_SR_AWB | \ |
| 107 | ATMEL_HSMC_NFC_SR_NFCASE) |
| 108 | #define ATMEL_HSMC_NFC_SR_RBEDGE(x) BIT((x) + 24) |
| 109 | |
| 110 | #define ATMEL_HSMC_NFC_ADDR 0x18 |
| 111 | #define ATMEL_HSMC_NFC_BANK 0x1c |
| 112 | |
| 113 | #define ATMEL_NFC_MAX_RB_ID 7 |
| 114 | |
| 115 | #define ATMEL_NFC_SRAM_SIZE 0x2400 |
| 116 | |
| 117 | #define ATMEL_NFC_CMD(pos, cmd) ((cmd) << (((pos) * 8) + 2)) |
| 118 | #define ATMEL_NFC_VCMD2 BIT(18) |
| 119 | #define ATMEL_NFC_ACYCLE(naddrs) ((naddrs) << 19) |
| 120 | #define ATMEL_NFC_CSID(cs) ((cs) << 22) |
| 121 | #define ATMEL_NFC_DATAEN BIT(25) |
| 122 | #define ATMEL_NFC_NFCWR BIT(26) |
| 123 | |
| 124 | #define ATMEL_NFC_MAX_ADDR_CYCLES 5 |
| 125 | |
| 126 | #define ATMEL_NAND_ALE_OFFSET BIT(21) |
| 127 | #define ATMEL_NAND_CLE_OFFSET BIT(22) |
| 128 | |
| 129 | #define DEFAULT_TIMEOUT_MS 1000 |
| 130 | #define MIN_DMA_LEN 128 |
| 131 | |
Peter Rosin | efc6362 | 2018-03-29 15:10:54 +0200 | [diff] [blame] | 132 | static bool atmel_nand_avoid_dma __read_mostly; |
| 133 | |
| 134 | MODULE_PARM_DESC(avoiddma, "Avoid using DMA"); |
| 135 | module_param_named(avoiddma, atmel_nand_avoid_dma, bool, 0400); |
| 136 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 137 | enum atmel_nand_rb_type { |
| 138 | ATMEL_NAND_NO_RB, |
| 139 | ATMEL_NAND_NATIVE_RB, |
| 140 | ATMEL_NAND_GPIO_RB, |
| 141 | }; |
| 142 | |
| 143 | struct atmel_nand_rb { |
| 144 | enum atmel_nand_rb_type type; |
| 145 | union { |
| 146 | struct gpio_desc *gpio; |
| 147 | int id; |
| 148 | }; |
| 149 | }; |
| 150 | |
| 151 | struct atmel_nand_cs { |
| 152 | int id; |
| 153 | struct atmel_nand_rb rb; |
| 154 | struct gpio_desc *csgpio; |
| 155 | struct { |
| 156 | void __iomem *virt; |
| 157 | dma_addr_t dma; |
| 158 | } io; |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 159 | |
| 160 | struct atmel_smc_cs_conf smcconf; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | struct atmel_nand { |
| 164 | struct list_head node; |
| 165 | struct device *dev; |
| 166 | struct nand_chip base; |
| 167 | struct atmel_nand_cs *activecs; |
| 168 | struct atmel_pmecc_user *pmecc; |
| 169 | struct gpio_desc *cdgpio; |
| 170 | int numcs; |
| 171 | struct atmel_nand_cs cs[]; |
| 172 | }; |
| 173 | |
| 174 | static inline struct atmel_nand *to_atmel_nand(struct nand_chip *chip) |
| 175 | { |
| 176 | return container_of(chip, struct atmel_nand, base); |
| 177 | } |
| 178 | |
| 179 | enum atmel_nfc_data_xfer { |
| 180 | ATMEL_NFC_NO_DATA, |
| 181 | ATMEL_NFC_READ_DATA, |
| 182 | ATMEL_NFC_WRITE_DATA, |
| 183 | }; |
| 184 | |
| 185 | struct atmel_nfc_op { |
| 186 | u8 cs; |
| 187 | u8 ncmds; |
| 188 | u8 cmds[2]; |
| 189 | u8 naddrs; |
| 190 | u8 addrs[5]; |
| 191 | enum atmel_nfc_data_xfer data; |
| 192 | u32 wait; |
| 193 | u32 errors; |
| 194 | }; |
| 195 | |
| 196 | struct atmel_nand_controller; |
| 197 | struct atmel_nand_controller_caps; |
| 198 | |
| 199 | struct atmel_nand_controller_ops { |
| 200 | int (*probe)(struct platform_device *pdev, |
| 201 | const struct atmel_nand_controller_caps *caps); |
| 202 | int (*remove)(struct atmel_nand_controller *nc); |
| 203 | void (*nand_init)(struct atmel_nand_controller *nc, |
| 204 | struct atmel_nand *nand); |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 205 | int (*ecc_init)(struct nand_chip *chip); |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 206 | int (*setup_data_interface)(struct atmel_nand *nand, int csline, |
| 207 | const struct nand_data_interface *conf); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | struct atmel_nand_controller_caps { |
| 211 | bool has_dma; |
| 212 | bool legacy_of_bindings; |
| 213 | u32 ale_offs; |
| 214 | u32 cle_offs; |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 215 | const char *ebi_csa_regmap_name; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 216 | const struct atmel_nand_controller_ops *ops; |
| 217 | }; |
| 218 | |
| 219 | struct atmel_nand_controller { |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 220 | struct nand_controller base; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 221 | const struct atmel_nand_controller_caps *caps; |
| 222 | struct device *dev; |
| 223 | struct regmap *smc; |
| 224 | struct dma_chan *dmac; |
| 225 | struct atmel_pmecc *pmecc; |
| 226 | struct list_head chips; |
| 227 | struct clk *mck; |
| 228 | }; |
| 229 | |
| 230 | static inline struct atmel_nand_controller * |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 231 | to_nand_controller(struct nand_controller *ctl) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 232 | { |
| 233 | return container_of(ctl, struct atmel_nand_controller, base); |
| 234 | } |
| 235 | |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 236 | struct atmel_smc_nand_ebi_csa_cfg { |
| 237 | u32 offs; |
| 238 | u32 nfd0_on_d16; |
| 239 | }; |
| 240 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 241 | struct atmel_smc_nand_controller { |
| 242 | struct atmel_nand_controller base; |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 243 | struct regmap *ebi_csa_regmap; |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 244 | struct atmel_smc_nand_ebi_csa_cfg *ebi_csa; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static inline struct atmel_smc_nand_controller * |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 248 | to_smc_nand_controller(struct nand_controller *ctl) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 249 | { |
| 250 | return container_of(to_nand_controller(ctl), |
| 251 | struct atmel_smc_nand_controller, base); |
| 252 | } |
| 253 | |
| 254 | struct atmel_hsmc_nand_controller { |
| 255 | struct atmel_nand_controller base; |
| 256 | struct { |
| 257 | struct gen_pool *pool; |
| 258 | void __iomem *virt; |
| 259 | dma_addr_t dma; |
| 260 | } sram; |
Ludovic Desroches | b0f3ab2 | 2017-07-18 15:22:19 +0200 | [diff] [blame] | 261 | const struct atmel_hsmc_reg_layout *hsmc_layout; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 262 | struct regmap *io; |
| 263 | struct atmel_nfc_op op; |
| 264 | struct completion complete; |
| 265 | int irq; |
| 266 | |
| 267 | /* Only used when instantiating from legacy DT bindings. */ |
| 268 | struct clk *clk; |
| 269 | }; |
| 270 | |
| 271 | static inline struct atmel_hsmc_nand_controller * |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 272 | to_hsmc_nand_controller(struct nand_controller *ctl) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 273 | { |
| 274 | return container_of(to_nand_controller(ctl), |
| 275 | struct atmel_hsmc_nand_controller, base); |
| 276 | } |
| 277 | |
| 278 | static bool atmel_nfc_op_done(struct atmel_nfc_op *op, u32 status) |
| 279 | { |
| 280 | op->errors |= status & ATMEL_HSMC_NFC_SR_ERRORS; |
| 281 | op->wait ^= status & op->wait; |
| 282 | |
| 283 | return !op->wait || op->errors; |
| 284 | } |
| 285 | |
| 286 | static irqreturn_t atmel_nfc_interrupt(int irq, void *data) |
| 287 | { |
| 288 | struct atmel_hsmc_nand_controller *nc = data; |
| 289 | u32 sr, rcvd; |
| 290 | bool done; |
| 291 | |
| 292 | regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &sr); |
| 293 | |
| 294 | rcvd = sr & (nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS); |
| 295 | done = atmel_nfc_op_done(&nc->op, sr); |
| 296 | |
| 297 | if (rcvd) |
| 298 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, rcvd); |
| 299 | |
| 300 | if (done) |
| 301 | complete(&nc->complete); |
| 302 | |
| 303 | return rcvd ? IRQ_HANDLED : IRQ_NONE; |
| 304 | } |
| 305 | |
| 306 | static int atmel_nfc_wait(struct atmel_hsmc_nand_controller *nc, bool poll, |
| 307 | unsigned int timeout_ms) |
| 308 | { |
| 309 | int ret; |
| 310 | |
| 311 | if (!timeout_ms) |
| 312 | timeout_ms = DEFAULT_TIMEOUT_MS; |
| 313 | |
| 314 | if (poll) { |
| 315 | u32 status; |
| 316 | |
| 317 | ret = regmap_read_poll_timeout(nc->base.smc, |
| 318 | ATMEL_HSMC_NFC_SR, status, |
| 319 | atmel_nfc_op_done(&nc->op, |
| 320 | status), |
| 321 | 0, timeout_ms * 1000); |
| 322 | } else { |
| 323 | init_completion(&nc->complete); |
| 324 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IER, |
| 325 | nc->op.wait | ATMEL_HSMC_NFC_SR_ERRORS); |
| 326 | ret = wait_for_completion_timeout(&nc->complete, |
| 327 | msecs_to_jiffies(timeout_ms)); |
| 328 | if (!ret) |
| 329 | ret = -ETIMEDOUT; |
| 330 | else |
| 331 | ret = 0; |
| 332 | |
| 333 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff); |
| 334 | } |
| 335 | |
| 336 | if (nc->op.errors & ATMEL_HSMC_NFC_SR_DTOE) { |
| 337 | dev_err(nc->base.dev, "Waiting NAND R/B Timeout\n"); |
| 338 | ret = -ETIMEDOUT; |
| 339 | } |
| 340 | |
| 341 | if (nc->op.errors & ATMEL_HSMC_NFC_SR_UNDEF) { |
| 342 | dev_err(nc->base.dev, "Access to an undefined area\n"); |
| 343 | ret = -EIO; |
| 344 | } |
| 345 | |
| 346 | if (nc->op.errors & ATMEL_HSMC_NFC_SR_AWB) { |
| 347 | dev_err(nc->base.dev, "Access while busy\n"); |
| 348 | ret = -EIO; |
| 349 | } |
| 350 | |
| 351 | if (nc->op.errors & ATMEL_HSMC_NFC_SR_NFCASE) { |
| 352 | dev_err(nc->base.dev, "Wrong access size\n"); |
| 353 | ret = -EIO; |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static void atmel_nand_dma_transfer_finished(void *data) |
| 360 | { |
| 361 | struct completion *finished = data; |
| 362 | |
| 363 | complete(finished); |
| 364 | } |
| 365 | |
| 366 | static int atmel_nand_dma_transfer(struct atmel_nand_controller *nc, |
| 367 | void *buf, dma_addr_t dev_dma, size_t len, |
| 368 | enum dma_data_direction dir) |
| 369 | { |
| 370 | DECLARE_COMPLETION_ONSTACK(finished); |
| 371 | dma_addr_t src_dma, dst_dma, buf_dma; |
| 372 | struct dma_async_tx_descriptor *tx; |
| 373 | dma_cookie_t cookie; |
| 374 | |
| 375 | buf_dma = dma_map_single(nc->dev, buf, len, dir); |
| 376 | if (dma_mapping_error(nc->dev, dev_dma)) { |
| 377 | dev_err(nc->dev, |
| 378 | "Failed to prepare a buffer for DMA access\n"); |
| 379 | goto err; |
| 380 | } |
| 381 | |
| 382 | if (dir == DMA_FROM_DEVICE) { |
| 383 | src_dma = dev_dma; |
| 384 | dst_dma = buf_dma; |
| 385 | } else { |
| 386 | src_dma = buf_dma; |
| 387 | dst_dma = dev_dma; |
| 388 | } |
| 389 | |
| 390 | tx = dmaengine_prep_dma_memcpy(nc->dmac, dst_dma, src_dma, len, |
| 391 | DMA_CTRL_ACK | DMA_PREP_INTERRUPT); |
| 392 | if (!tx) { |
| 393 | dev_err(nc->dev, "Failed to prepare DMA memcpy\n"); |
| 394 | goto err_unmap; |
| 395 | } |
| 396 | |
| 397 | tx->callback = atmel_nand_dma_transfer_finished; |
| 398 | tx->callback_param = &finished; |
| 399 | |
| 400 | cookie = dmaengine_submit(tx); |
| 401 | if (dma_submit_error(cookie)) { |
| 402 | dev_err(nc->dev, "Failed to do DMA tx_submit\n"); |
| 403 | goto err_unmap; |
| 404 | } |
| 405 | |
| 406 | dma_async_issue_pending(nc->dmac); |
| 407 | wait_for_completion(&finished); |
| 408 | |
| 409 | return 0; |
| 410 | |
| 411 | err_unmap: |
| 412 | dma_unmap_single(nc->dev, buf_dma, len, dir); |
| 413 | |
| 414 | err: |
| 415 | dev_dbg(nc->dev, "Fall back to CPU I/O\n"); |
| 416 | |
| 417 | return -EIO; |
| 418 | } |
| 419 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 420 | static u8 atmel_nand_read_byte(struct nand_chip *chip) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 421 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 422 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 423 | |
| 424 | return ioread8(nand->activecs->io.virt); |
| 425 | } |
| 426 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 427 | static void atmel_nand_write_byte(struct nand_chip *chip, u8 byte) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 428 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 429 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 430 | |
| 431 | if (chip->options & NAND_BUSWIDTH_16) |
| 432 | iowrite16(byte | (byte << 8), nand->activecs->io.virt); |
| 433 | else |
| 434 | iowrite8(byte, nand->activecs->io.virt); |
| 435 | } |
| 436 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 437 | static void atmel_nand_read_buf(struct nand_chip *chip, u8 *buf, int len) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 438 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 439 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 440 | struct atmel_nand_controller *nc; |
| 441 | |
| 442 | nc = to_nand_controller(chip->controller); |
| 443 | |
| 444 | /* |
| 445 | * If the controller supports DMA, the buffer address is DMA-able and |
| 446 | * len is long enough to make DMA transfers profitable, let's trigger |
| 447 | * a DMA transfer. If it fails, fallback to PIO mode. |
| 448 | */ |
| 449 | if (nc->dmac && virt_addr_valid(buf) && |
| 450 | len >= MIN_DMA_LEN && |
| 451 | !atmel_nand_dma_transfer(nc, buf, nand->activecs->io.dma, len, |
| 452 | DMA_FROM_DEVICE)) |
| 453 | return; |
| 454 | |
| 455 | if (chip->options & NAND_BUSWIDTH_16) |
| 456 | ioread16_rep(nand->activecs->io.virt, buf, len / 2); |
| 457 | else |
| 458 | ioread8_rep(nand->activecs->io.virt, buf, len); |
| 459 | } |
| 460 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 461 | static void atmel_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 462 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 463 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 464 | struct atmel_nand_controller *nc; |
| 465 | |
| 466 | nc = to_nand_controller(chip->controller); |
| 467 | |
| 468 | /* |
| 469 | * If the controller supports DMA, the buffer address is DMA-able and |
| 470 | * len is long enough to make DMA transfers profitable, let's trigger |
| 471 | * a DMA transfer. If it fails, fallback to PIO mode. |
| 472 | */ |
| 473 | if (nc->dmac && virt_addr_valid(buf) && |
| 474 | len >= MIN_DMA_LEN && |
| 475 | !atmel_nand_dma_transfer(nc, (void *)buf, nand->activecs->io.dma, |
| 476 | len, DMA_TO_DEVICE)) |
| 477 | return; |
| 478 | |
| 479 | if (chip->options & NAND_BUSWIDTH_16) |
| 480 | iowrite16_rep(nand->activecs->io.virt, buf, len / 2); |
| 481 | else |
| 482 | iowrite8_rep(nand->activecs->io.virt, buf, len); |
| 483 | } |
| 484 | |
Boris Brezillon | 50a487e | 2018-09-06 14:05:27 +0200 | [diff] [blame] | 485 | static int atmel_nand_dev_ready(struct nand_chip *chip) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 486 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 487 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 488 | |
| 489 | return gpiod_get_value(nand->activecs->rb.gpio); |
| 490 | } |
| 491 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 492 | static void atmel_nand_select_chip(struct nand_chip *chip, int cs) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 493 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 494 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 495 | |
| 496 | if (cs < 0 || cs >= nand->numcs) { |
| 497 | nand->activecs = NULL; |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 498 | chip->legacy.dev_ready = NULL; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
| 502 | nand->activecs = &nand->cs[cs]; |
| 503 | |
| 504 | if (nand->activecs->rb.type == ATMEL_NAND_GPIO_RB) |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 505 | chip->legacy.dev_ready = atmel_nand_dev_ready; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Boris Brezillon | 50a487e | 2018-09-06 14:05:27 +0200 | [diff] [blame] | 508 | static int atmel_hsmc_nand_dev_ready(struct nand_chip *chip) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 509 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 510 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 511 | struct atmel_hsmc_nand_controller *nc; |
| 512 | u32 status; |
| 513 | |
| 514 | nc = to_hsmc_nand_controller(chip->controller); |
| 515 | |
| 516 | regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &status); |
| 517 | |
| 518 | return status & ATMEL_HSMC_NFC_SR_RBEDGE(nand->activecs->rb.id); |
| 519 | } |
| 520 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 521 | static void atmel_hsmc_nand_select_chip(struct nand_chip *chip, int cs) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 522 | { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 523 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 524 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 525 | struct atmel_hsmc_nand_controller *nc; |
| 526 | |
| 527 | nc = to_hsmc_nand_controller(chip->controller); |
| 528 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 529 | atmel_nand_select_chip(chip, cs); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 530 | |
| 531 | if (!nand->activecs) { |
| 532 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL, |
| 533 | ATMEL_HSMC_NFC_CTRL_DIS); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | if (nand->activecs->rb.type == ATMEL_NAND_NATIVE_RB) |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 538 | chip->legacy.dev_ready = atmel_hsmc_nand_dev_ready; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 539 | |
| 540 | regmap_update_bits(nc->base.smc, ATMEL_HSMC_NFC_CFG, |
| 541 | ATMEL_HSMC_NFC_CFG_PAGESIZE_MASK | |
| 542 | ATMEL_HSMC_NFC_CFG_SPARESIZE_MASK | |
| 543 | ATMEL_HSMC_NFC_CFG_RSPARE | |
| 544 | ATMEL_HSMC_NFC_CFG_WSPARE, |
| 545 | ATMEL_HSMC_NFC_CFG_PAGESIZE(mtd->writesize) | |
| 546 | ATMEL_HSMC_NFC_CFG_SPARESIZE(mtd->oobsize) | |
| 547 | ATMEL_HSMC_NFC_CFG_RSPARE); |
| 548 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CTRL, |
| 549 | ATMEL_HSMC_NFC_CTRL_EN); |
| 550 | } |
| 551 | |
| 552 | static int atmel_nfc_exec_op(struct atmel_hsmc_nand_controller *nc, bool poll) |
| 553 | { |
| 554 | u8 *addrs = nc->op.addrs; |
| 555 | unsigned int op = 0; |
| 556 | u32 addr, val; |
| 557 | int i, ret; |
| 558 | |
| 559 | nc->op.wait = ATMEL_HSMC_NFC_SR_CMDDONE; |
| 560 | |
| 561 | for (i = 0; i < nc->op.ncmds; i++) |
| 562 | op |= ATMEL_NFC_CMD(i, nc->op.cmds[i]); |
| 563 | |
| 564 | if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES) |
| 565 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_ADDR, *addrs++); |
| 566 | |
| 567 | op |= ATMEL_NFC_CSID(nc->op.cs) | |
| 568 | ATMEL_NFC_ACYCLE(nc->op.naddrs); |
| 569 | |
| 570 | if (nc->op.ncmds > 1) |
| 571 | op |= ATMEL_NFC_VCMD2; |
| 572 | |
| 573 | addr = addrs[0] | (addrs[1] << 8) | (addrs[2] << 16) | |
| 574 | (addrs[3] << 24); |
| 575 | |
| 576 | if (nc->op.data != ATMEL_NFC_NO_DATA) { |
| 577 | op |= ATMEL_NFC_DATAEN; |
| 578 | nc->op.wait |= ATMEL_HSMC_NFC_SR_XFRDONE; |
| 579 | |
| 580 | if (nc->op.data == ATMEL_NFC_WRITE_DATA) |
| 581 | op |= ATMEL_NFC_NFCWR; |
| 582 | } |
| 583 | |
| 584 | /* Clear all flags. */ |
| 585 | regmap_read(nc->base.smc, ATMEL_HSMC_NFC_SR, &val); |
| 586 | |
| 587 | /* Send the command. */ |
| 588 | regmap_write(nc->io, op, addr); |
| 589 | |
| 590 | ret = atmel_nfc_wait(nc, poll, 0); |
| 591 | if (ret) |
| 592 | dev_err(nc->base.dev, |
| 593 | "Failed to send NAND command (err = %d)!", |
| 594 | ret); |
| 595 | |
| 596 | /* Reset the op state. */ |
| 597 | memset(&nc->op, 0, sizeof(nc->op)); |
| 598 | |
| 599 | return ret; |
| 600 | } |
| 601 | |
Boris Brezillon | 0f808c1 | 2018-09-06 14:05:26 +0200 | [diff] [blame] | 602 | static void atmel_hsmc_nand_cmd_ctrl(struct nand_chip *chip, int dat, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 603 | unsigned int ctrl) |
| 604 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 605 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 606 | struct atmel_hsmc_nand_controller *nc; |
| 607 | |
| 608 | nc = to_hsmc_nand_controller(chip->controller); |
| 609 | |
| 610 | if (ctrl & NAND_ALE) { |
| 611 | if (nc->op.naddrs == ATMEL_NFC_MAX_ADDR_CYCLES) |
| 612 | return; |
| 613 | |
| 614 | nc->op.addrs[nc->op.naddrs++] = dat; |
| 615 | } else if (ctrl & NAND_CLE) { |
| 616 | if (nc->op.ncmds > 1) |
| 617 | return; |
| 618 | |
| 619 | nc->op.cmds[nc->op.ncmds++] = dat; |
| 620 | } |
| 621 | |
| 622 | if (dat == NAND_CMD_NONE) { |
| 623 | nc->op.cs = nand->activecs->id; |
| 624 | atmel_nfc_exec_op(nc, true); |
| 625 | } |
| 626 | } |
| 627 | |
Boris Brezillon | 0f808c1 | 2018-09-06 14:05:26 +0200 | [diff] [blame] | 628 | static void atmel_nand_cmd_ctrl(struct nand_chip *chip, int cmd, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 629 | unsigned int ctrl) |
| 630 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 631 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 632 | struct atmel_nand_controller *nc; |
| 633 | |
| 634 | nc = to_nand_controller(chip->controller); |
| 635 | |
| 636 | if ((ctrl & NAND_CTRL_CHANGE) && nand->activecs->csgpio) { |
| 637 | if (ctrl & NAND_NCE) |
| 638 | gpiod_set_value(nand->activecs->csgpio, 0); |
| 639 | else |
| 640 | gpiod_set_value(nand->activecs->csgpio, 1); |
| 641 | } |
| 642 | |
| 643 | if (ctrl & NAND_ALE) |
| 644 | writeb(cmd, nand->activecs->io.virt + nc->caps->ale_offs); |
| 645 | else if (ctrl & NAND_CLE) |
| 646 | writeb(cmd, nand->activecs->io.virt + nc->caps->cle_offs); |
| 647 | } |
| 648 | |
| 649 | static void atmel_nfc_copy_to_sram(struct nand_chip *chip, const u8 *buf, |
| 650 | bool oob_required) |
| 651 | { |
| 652 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 653 | struct atmel_hsmc_nand_controller *nc; |
| 654 | int ret = -EIO; |
| 655 | |
| 656 | nc = to_hsmc_nand_controller(chip->controller); |
| 657 | |
| 658 | if (nc->base.dmac) |
| 659 | ret = atmel_nand_dma_transfer(&nc->base, (void *)buf, |
| 660 | nc->sram.dma, mtd->writesize, |
| 661 | DMA_TO_DEVICE); |
| 662 | |
| 663 | /* Falling back to CPU copy. */ |
| 664 | if (ret) |
| 665 | memcpy_toio(nc->sram.virt, buf, mtd->writesize); |
| 666 | |
| 667 | if (oob_required) |
| 668 | memcpy_toio(nc->sram.virt + mtd->writesize, chip->oob_poi, |
| 669 | mtd->oobsize); |
| 670 | } |
| 671 | |
| 672 | static void atmel_nfc_copy_from_sram(struct nand_chip *chip, u8 *buf, |
| 673 | bool oob_required) |
| 674 | { |
| 675 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 676 | struct atmel_hsmc_nand_controller *nc; |
| 677 | int ret = -EIO; |
| 678 | |
| 679 | nc = to_hsmc_nand_controller(chip->controller); |
| 680 | |
| 681 | if (nc->base.dmac) |
| 682 | ret = atmel_nand_dma_transfer(&nc->base, buf, nc->sram.dma, |
| 683 | mtd->writesize, DMA_FROM_DEVICE); |
| 684 | |
| 685 | /* Falling back to CPU copy. */ |
| 686 | if (ret) |
| 687 | memcpy_fromio(buf, nc->sram.virt, mtd->writesize); |
| 688 | |
| 689 | if (oob_required) |
| 690 | memcpy_fromio(chip->oob_poi, nc->sram.virt + mtd->writesize, |
| 691 | mtd->oobsize); |
| 692 | } |
| 693 | |
| 694 | static void atmel_nfc_set_op_addr(struct nand_chip *chip, int page, int column) |
| 695 | { |
| 696 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 697 | struct atmel_hsmc_nand_controller *nc; |
| 698 | |
| 699 | nc = to_hsmc_nand_controller(chip->controller); |
| 700 | |
| 701 | if (column >= 0) { |
| 702 | nc->op.addrs[nc->op.naddrs++] = column; |
| 703 | |
| 704 | /* |
| 705 | * 2 address cycles for the column offset on large page NANDs. |
| 706 | */ |
| 707 | if (mtd->writesize > 512) |
| 708 | nc->op.addrs[nc->op.naddrs++] = column >> 8; |
| 709 | } |
| 710 | |
| 711 | if (page >= 0) { |
| 712 | nc->op.addrs[nc->op.naddrs++] = page; |
| 713 | nc->op.addrs[nc->op.naddrs++] = page >> 8; |
| 714 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 715 | if (chip->options & NAND_ROW_ADDR_3) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 716 | nc->op.addrs[nc->op.naddrs++] = page >> 16; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | static int atmel_nand_pmecc_enable(struct nand_chip *chip, int op, bool raw) |
| 721 | { |
| 722 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 723 | struct atmel_nand_controller *nc; |
| 724 | int ret; |
| 725 | |
| 726 | nc = to_nand_controller(chip->controller); |
| 727 | |
| 728 | if (raw) |
| 729 | return 0; |
| 730 | |
| 731 | ret = atmel_pmecc_enable(nand->pmecc, op); |
| 732 | if (ret) |
| 733 | dev_err(nc->dev, |
| 734 | "Failed to enable ECC engine (err = %d)\n", ret); |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | static void atmel_nand_pmecc_disable(struct nand_chip *chip, bool raw) |
| 740 | { |
| 741 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 742 | |
| 743 | if (!raw) |
| 744 | atmel_pmecc_disable(nand->pmecc); |
| 745 | } |
| 746 | |
| 747 | static int atmel_nand_pmecc_generate_eccbytes(struct nand_chip *chip, bool raw) |
| 748 | { |
| 749 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 750 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 751 | struct atmel_nand_controller *nc; |
| 752 | struct mtd_oob_region oobregion; |
| 753 | void *eccbuf; |
| 754 | int ret, i; |
| 755 | |
| 756 | nc = to_nand_controller(chip->controller); |
| 757 | |
| 758 | if (raw) |
| 759 | return 0; |
| 760 | |
| 761 | ret = atmel_pmecc_wait_rdy(nand->pmecc); |
| 762 | if (ret) { |
| 763 | dev_err(nc->dev, |
| 764 | "Failed to transfer NAND page data (err = %d)\n", |
| 765 | ret); |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 770 | eccbuf = chip->oob_poi + oobregion.offset; |
| 771 | |
| 772 | for (i = 0; i < chip->ecc.steps; i++) { |
| 773 | atmel_pmecc_get_generated_eccbytes(nand->pmecc, i, |
| 774 | eccbuf); |
| 775 | eccbuf += chip->ecc.bytes; |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static int atmel_nand_pmecc_correct_data(struct nand_chip *chip, void *buf, |
| 782 | bool raw) |
| 783 | { |
| 784 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 785 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 786 | struct atmel_nand_controller *nc; |
| 787 | struct mtd_oob_region oobregion; |
| 788 | int ret, i, max_bitflips = 0; |
| 789 | void *databuf, *eccbuf; |
| 790 | |
| 791 | nc = to_nand_controller(chip->controller); |
| 792 | |
| 793 | if (raw) |
| 794 | return 0; |
| 795 | |
| 796 | ret = atmel_pmecc_wait_rdy(nand->pmecc); |
| 797 | if (ret) { |
| 798 | dev_err(nc->dev, |
| 799 | "Failed to read NAND page data (err = %d)\n", |
| 800 | ret); |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 805 | eccbuf = chip->oob_poi + oobregion.offset; |
| 806 | databuf = buf; |
| 807 | |
| 808 | for (i = 0; i < chip->ecc.steps; i++) { |
| 809 | ret = atmel_pmecc_correct_sector(nand->pmecc, i, databuf, |
| 810 | eccbuf); |
| 811 | if (ret < 0 && !atmel_pmecc_correct_erased_chunks(nand->pmecc)) |
| 812 | ret = nand_check_erased_ecc_chunk(databuf, |
| 813 | chip->ecc.size, |
| 814 | eccbuf, |
| 815 | chip->ecc.bytes, |
| 816 | NULL, 0, |
| 817 | chip->ecc.strength); |
| 818 | |
| 819 | if (ret >= 0) |
| 820 | max_bitflips = max(ret, max_bitflips); |
| 821 | else |
| 822 | mtd->ecc_stats.failed++; |
| 823 | |
| 824 | databuf += chip->ecc.size; |
| 825 | eccbuf += chip->ecc.bytes; |
| 826 | } |
| 827 | |
| 828 | return max_bitflips; |
| 829 | } |
| 830 | |
| 831 | static int atmel_nand_pmecc_write_pg(struct nand_chip *chip, const u8 *buf, |
| 832 | bool oob_required, int page, bool raw) |
| 833 | { |
| 834 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 835 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 836 | int ret; |
| 837 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 838 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 839 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 840 | ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw); |
| 841 | if (ret) |
| 842 | return ret; |
| 843 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 844 | atmel_nand_write_buf(chip, buf, mtd->writesize); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 845 | |
| 846 | ret = atmel_nand_pmecc_generate_eccbytes(chip, raw); |
| 847 | if (ret) { |
| 848 | atmel_pmecc_disable(nand->pmecc); |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | atmel_nand_pmecc_disable(chip, raw); |
| 853 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 854 | atmel_nand_write_buf(chip, chip->oob_poi, mtd->oobsize); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 855 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 856 | return nand_prog_page_end_op(chip); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 859 | static int atmel_nand_pmecc_write_page(struct nand_chip *chip, const u8 *buf, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 860 | int oob_required, int page) |
| 861 | { |
| 862 | return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, false); |
| 863 | } |
| 864 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 865 | static int atmel_nand_pmecc_write_page_raw(struct nand_chip *chip, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 866 | const u8 *buf, int oob_required, |
| 867 | int page) |
| 868 | { |
| 869 | return atmel_nand_pmecc_write_pg(chip, buf, oob_required, page, true); |
| 870 | } |
| 871 | |
| 872 | static int atmel_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf, |
| 873 | bool oob_required, int page, bool raw) |
| 874 | { |
| 875 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 876 | int ret; |
| 877 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 878 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 879 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 880 | ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw); |
| 881 | if (ret) |
| 882 | return ret; |
| 883 | |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 884 | atmel_nand_read_buf(chip, buf, mtd->writesize); |
| 885 | atmel_nand_read_buf(chip, chip->oob_poi, mtd->oobsize); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 886 | |
| 887 | ret = atmel_nand_pmecc_correct_data(chip, buf, raw); |
| 888 | |
| 889 | atmel_nand_pmecc_disable(chip, raw); |
| 890 | |
| 891 | return ret; |
| 892 | } |
| 893 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 894 | static int atmel_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 895 | int oob_required, int page) |
| 896 | { |
| 897 | return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, false); |
| 898 | } |
| 899 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 900 | static int atmel_nand_pmecc_read_page_raw(struct nand_chip *chip, u8 *buf, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 901 | int oob_required, int page) |
| 902 | { |
| 903 | return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, true); |
| 904 | } |
| 905 | |
| 906 | static int atmel_hsmc_nand_pmecc_write_pg(struct nand_chip *chip, |
| 907 | const u8 *buf, bool oob_required, |
| 908 | int page, bool raw) |
| 909 | { |
| 910 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 911 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 912 | struct atmel_hsmc_nand_controller *nc; |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 913 | int ret, status; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 914 | |
| 915 | nc = to_hsmc_nand_controller(chip->controller); |
| 916 | |
| 917 | atmel_nfc_copy_to_sram(chip, buf, false); |
| 918 | |
| 919 | nc->op.cmds[0] = NAND_CMD_SEQIN; |
| 920 | nc->op.ncmds = 1; |
| 921 | atmel_nfc_set_op_addr(chip, page, 0x0); |
| 922 | nc->op.cs = nand->activecs->id; |
| 923 | nc->op.data = ATMEL_NFC_WRITE_DATA; |
| 924 | |
| 925 | ret = atmel_nand_pmecc_enable(chip, NAND_ECC_WRITE, raw); |
| 926 | if (ret) |
| 927 | return ret; |
| 928 | |
| 929 | ret = atmel_nfc_exec_op(nc, false); |
| 930 | if (ret) { |
| 931 | atmel_nand_pmecc_disable(chip, raw); |
| 932 | dev_err(nc->base.dev, |
| 933 | "Failed to transfer NAND page data (err = %d)\n", |
| 934 | ret); |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | ret = atmel_nand_pmecc_generate_eccbytes(chip, raw); |
| 939 | |
| 940 | atmel_nand_pmecc_disable(chip, raw); |
| 941 | |
| 942 | if (ret) |
| 943 | return ret; |
| 944 | |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 945 | atmel_nand_write_buf(chip, chip->oob_poi, mtd->oobsize); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 946 | |
| 947 | nc->op.cmds[0] = NAND_CMD_PAGEPROG; |
| 948 | nc->op.ncmds = 1; |
| 949 | nc->op.cs = nand->activecs->id; |
| 950 | ret = atmel_nfc_exec_op(nc, false); |
| 951 | if (ret) |
| 952 | dev_err(nc->base.dev, "Failed to program NAND page (err = %d)\n", |
| 953 | ret); |
| 954 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 955 | status = chip->legacy.waitfunc(chip); |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 956 | if (status & NAND_STATUS_FAIL) |
| 957 | return -EIO; |
| 958 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 959 | return ret; |
| 960 | } |
| 961 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 962 | static int atmel_hsmc_nand_pmecc_write_page(struct nand_chip *chip, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 963 | const u8 *buf, int oob_required, |
| 964 | int page) |
| 965 | { |
| 966 | return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page, |
| 967 | false); |
| 968 | } |
| 969 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 970 | static int atmel_hsmc_nand_pmecc_write_page_raw(struct nand_chip *chip, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 971 | const u8 *buf, |
| 972 | int oob_required, int page) |
| 973 | { |
| 974 | return atmel_hsmc_nand_pmecc_write_pg(chip, buf, oob_required, page, |
| 975 | true); |
| 976 | } |
| 977 | |
| 978 | static int atmel_hsmc_nand_pmecc_read_pg(struct nand_chip *chip, u8 *buf, |
| 979 | bool oob_required, int page, |
| 980 | bool raw) |
| 981 | { |
| 982 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 983 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 984 | struct atmel_hsmc_nand_controller *nc; |
| 985 | int ret; |
| 986 | |
| 987 | nc = to_hsmc_nand_controller(chip->controller); |
| 988 | |
| 989 | /* |
| 990 | * Optimized read page accessors only work when the NAND R/B pin is |
| 991 | * connected to a native SoC R/B pin. If that's not the case, fallback |
| 992 | * to the non-optimized one. |
| 993 | */ |
| 994 | if (nand->activecs->rb.type != ATMEL_NAND_NATIVE_RB) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 995 | nand_read_page_op(chip, page, 0, NULL, 0); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 996 | |
| 997 | return atmel_nand_pmecc_read_pg(chip, buf, oob_required, page, |
| 998 | raw); |
| 999 | } |
| 1000 | |
| 1001 | nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READ0; |
| 1002 | |
| 1003 | if (mtd->writesize > 512) |
| 1004 | nc->op.cmds[nc->op.ncmds++] = NAND_CMD_READSTART; |
| 1005 | |
| 1006 | atmel_nfc_set_op_addr(chip, page, 0x0); |
| 1007 | nc->op.cs = nand->activecs->id; |
| 1008 | nc->op.data = ATMEL_NFC_READ_DATA; |
| 1009 | |
| 1010 | ret = atmel_nand_pmecc_enable(chip, NAND_ECC_READ, raw); |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | ret = atmel_nfc_exec_op(nc, false); |
| 1015 | if (ret) { |
| 1016 | atmel_nand_pmecc_disable(chip, raw); |
| 1017 | dev_err(nc->base.dev, |
| 1018 | "Failed to load NAND page data (err = %d)\n", |
| 1019 | ret); |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | atmel_nfc_copy_from_sram(chip, buf, true); |
| 1024 | |
| 1025 | ret = atmel_nand_pmecc_correct_data(chip, buf, raw); |
| 1026 | |
| 1027 | atmel_nand_pmecc_disable(chip, raw); |
| 1028 | |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1032 | static int atmel_hsmc_nand_pmecc_read_page(struct nand_chip *chip, u8 *buf, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1033 | int oob_required, int page) |
| 1034 | { |
| 1035 | return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page, |
| 1036 | false); |
| 1037 | } |
| 1038 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1039 | static int atmel_hsmc_nand_pmecc_read_page_raw(struct nand_chip *chip, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1040 | u8 *buf, int oob_required, |
| 1041 | int page) |
| 1042 | { |
| 1043 | return atmel_hsmc_nand_pmecc_read_pg(chip, buf, oob_required, page, |
| 1044 | true); |
| 1045 | } |
| 1046 | |
| 1047 | static int atmel_nand_pmecc_init(struct nand_chip *chip) |
| 1048 | { |
| 1049 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1050 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 1051 | struct atmel_nand_controller *nc; |
| 1052 | struct atmel_pmecc_user_req req; |
| 1053 | |
| 1054 | nc = to_nand_controller(chip->controller); |
| 1055 | |
| 1056 | if (!nc->pmecc) { |
| 1057 | dev_err(nc->dev, "HW ECC not supported\n"); |
| 1058 | return -ENOTSUPP; |
| 1059 | } |
| 1060 | |
| 1061 | if (nc->caps->legacy_of_bindings) { |
| 1062 | u32 val; |
| 1063 | |
| 1064 | if (!of_property_read_u32(nc->dev->of_node, "atmel,pmecc-cap", |
| 1065 | &val)) |
| 1066 | chip->ecc.strength = val; |
| 1067 | |
| 1068 | if (!of_property_read_u32(nc->dev->of_node, |
| 1069 | "atmel,pmecc-sector-size", |
| 1070 | &val)) |
| 1071 | chip->ecc.size = val; |
| 1072 | } |
| 1073 | |
| 1074 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 1075 | req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH; |
| 1076 | else if (chip->ecc.strength) |
| 1077 | req.ecc.strength = chip->ecc.strength; |
| 1078 | else if (chip->ecc_strength_ds) |
| 1079 | req.ecc.strength = chip->ecc_strength_ds; |
| 1080 | else |
| 1081 | req.ecc.strength = ATMEL_PMECC_MAXIMIZE_ECC_STRENGTH; |
| 1082 | |
| 1083 | if (chip->ecc.size) |
| 1084 | req.ecc.sectorsize = chip->ecc.size; |
| 1085 | else if (chip->ecc_step_ds) |
| 1086 | req.ecc.sectorsize = chip->ecc_step_ds; |
| 1087 | else |
| 1088 | req.ecc.sectorsize = ATMEL_PMECC_SECTOR_SIZE_AUTO; |
| 1089 | |
| 1090 | req.pagesize = mtd->writesize; |
| 1091 | req.oobsize = mtd->oobsize; |
| 1092 | |
| 1093 | if (mtd->writesize <= 512) { |
| 1094 | req.ecc.bytes = 4; |
| 1095 | req.ecc.ooboffset = 0; |
| 1096 | } else { |
| 1097 | req.ecc.bytes = mtd->oobsize - 2; |
| 1098 | req.ecc.ooboffset = ATMEL_PMECC_OOBOFFSET_AUTO; |
| 1099 | } |
| 1100 | |
| 1101 | nand->pmecc = atmel_pmecc_create_user(nc->pmecc, &req); |
| 1102 | if (IS_ERR(nand->pmecc)) |
| 1103 | return PTR_ERR(nand->pmecc); |
| 1104 | |
| 1105 | chip->ecc.algo = NAND_ECC_BCH; |
| 1106 | chip->ecc.size = req.ecc.sectorsize; |
| 1107 | chip->ecc.bytes = req.ecc.bytes / req.ecc.nsectors; |
| 1108 | chip->ecc.strength = req.ecc.strength; |
| 1109 | |
| 1110 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
| 1111 | |
| 1112 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1117 | static int atmel_nand_ecc_init(struct nand_chip *chip) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1118 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1119 | struct atmel_nand_controller *nc; |
| 1120 | int ret; |
| 1121 | |
| 1122 | nc = to_nand_controller(chip->controller); |
| 1123 | |
| 1124 | switch (chip->ecc.mode) { |
| 1125 | case NAND_ECC_NONE: |
| 1126 | case NAND_ECC_SOFT: |
| 1127 | /* |
| 1128 | * Nothing to do, the core will initialize everything for us. |
| 1129 | */ |
| 1130 | break; |
| 1131 | |
| 1132 | case NAND_ECC_HW: |
| 1133 | ret = atmel_nand_pmecc_init(chip); |
| 1134 | if (ret) |
| 1135 | return ret; |
| 1136 | |
| 1137 | chip->ecc.read_page = atmel_nand_pmecc_read_page; |
| 1138 | chip->ecc.write_page = atmel_nand_pmecc_write_page; |
| 1139 | chip->ecc.read_page_raw = atmel_nand_pmecc_read_page_raw; |
| 1140 | chip->ecc.write_page_raw = atmel_nand_pmecc_write_page_raw; |
| 1141 | break; |
| 1142 | |
| 1143 | default: |
| 1144 | /* Other modes are not supported. */ |
| 1145 | dev_err(nc->dev, "Unsupported ECC mode: %d\n", |
| 1146 | chip->ecc.mode); |
| 1147 | return -ENOTSUPP; |
| 1148 | } |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1153 | static int atmel_hsmc_nand_ecc_init(struct nand_chip *chip) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1154 | { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1155 | int ret; |
| 1156 | |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1157 | ret = atmel_nand_ecc_init(chip); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1158 | if (ret) |
| 1159 | return ret; |
| 1160 | |
| 1161 | if (chip->ecc.mode != NAND_ECC_HW) |
| 1162 | return 0; |
| 1163 | |
| 1164 | /* Adjust the ECC operations for the HSMC IP. */ |
| 1165 | chip->ecc.read_page = atmel_hsmc_nand_pmecc_read_page; |
| 1166 | chip->ecc.write_page = atmel_hsmc_nand_pmecc_write_page; |
| 1167 | chip->ecc.read_page_raw = atmel_hsmc_nand_pmecc_read_page_raw; |
| 1168 | chip->ecc.write_page_raw = atmel_hsmc_nand_pmecc_write_page_raw; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1173 | static int atmel_smc_nand_prepare_smcconf(struct atmel_nand *nand, |
| 1174 | const struct nand_data_interface *conf, |
| 1175 | struct atmel_smc_cs_conf *smcconf) |
| 1176 | { |
| 1177 | u32 ncycles, totalcycles, timeps, mckperiodps; |
| 1178 | struct atmel_nand_controller *nc; |
| 1179 | int ret; |
| 1180 | |
| 1181 | nc = to_nand_controller(nand->base.controller); |
| 1182 | |
| 1183 | /* DDR interface not supported. */ |
| 1184 | if (conf->type != NAND_SDR_IFACE) |
| 1185 | return -ENOTSUPP; |
| 1186 | |
| 1187 | /* |
| 1188 | * tRC < 30ns implies EDO mode. This controller does not support this |
| 1189 | * mode. |
| 1190 | */ |
Boris Brezillon | ee02f73 | 2017-07-31 10:32:21 +0200 | [diff] [blame] | 1191 | if (conf->timings.sdr.tRC_min < 30000) |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1192 | return -ENOTSUPP; |
| 1193 | |
| 1194 | atmel_smc_cs_conf_init(smcconf); |
| 1195 | |
| 1196 | mckperiodps = NSEC_PER_SEC / clk_get_rate(nc->mck); |
| 1197 | mckperiodps *= 1000; |
| 1198 | |
| 1199 | /* |
| 1200 | * Set write pulse timing. This one is easy to extract: |
| 1201 | * |
| 1202 | * NWE_PULSE = tWP |
| 1203 | */ |
| 1204 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tWP_min, mckperiodps); |
| 1205 | totalcycles = ncycles; |
| 1206 | ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NWE_SHIFT, |
| 1207 | ncycles); |
| 1208 | if (ret) |
| 1209 | return ret; |
| 1210 | |
| 1211 | /* |
| 1212 | * The write setup timing depends on the operation done on the NAND. |
| 1213 | * All operations goes through the same data bus, but the operation |
| 1214 | * type depends on the address we are writing to (ALE/CLE address |
| 1215 | * lines). |
| 1216 | * Since we have no way to differentiate the different operations at |
| 1217 | * the SMC level, we must consider the worst case (the biggest setup |
| 1218 | * time among all operation types): |
| 1219 | * |
| 1220 | * NWE_SETUP = max(tCLS, tCS, tALS, tDS) - NWE_PULSE |
| 1221 | */ |
| 1222 | timeps = max3(conf->timings.sdr.tCLS_min, conf->timings.sdr.tCS_min, |
| 1223 | conf->timings.sdr.tALS_min); |
| 1224 | timeps = max(timeps, conf->timings.sdr.tDS_min); |
| 1225 | ncycles = DIV_ROUND_UP(timeps, mckperiodps); |
| 1226 | ncycles = ncycles > totalcycles ? ncycles - totalcycles : 0; |
| 1227 | totalcycles += ncycles; |
| 1228 | ret = atmel_smc_cs_conf_set_setup(smcconf, ATMEL_SMC_NWE_SHIFT, |
| 1229 | ncycles); |
| 1230 | if (ret) |
| 1231 | return ret; |
| 1232 | |
| 1233 | /* |
| 1234 | * As for the write setup timing, the write hold timing depends on the |
| 1235 | * operation done on the NAND: |
| 1236 | * |
| 1237 | * NWE_HOLD = max(tCLH, tCH, tALH, tDH, tWH) |
| 1238 | */ |
| 1239 | timeps = max3(conf->timings.sdr.tCLH_min, conf->timings.sdr.tCH_min, |
| 1240 | conf->timings.sdr.tALH_min); |
| 1241 | timeps = max3(timeps, conf->timings.sdr.tDH_min, |
| 1242 | conf->timings.sdr.tWH_min); |
| 1243 | ncycles = DIV_ROUND_UP(timeps, mckperiodps); |
| 1244 | totalcycles += ncycles; |
| 1245 | |
| 1246 | /* |
| 1247 | * The write cycle timing is directly matching tWC, but is also |
| 1248 | * dependent on the other timings on the setup and hold timings we |
| 1249 | * calculated earlier, which gives: |
| 1250 | * |
| 1251 | * NWE_CYCLE = max(tWC, NWE_SETUP + NWE_PULSE + NWE_HOLD) |
| 1252 | */ |
| 1253 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tWC_min, mckperiodps); |
| 1254 | ncycles = max(totalcycles, ncycles); |
| 1255 | ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NWE_SHIFT, |
| 1256 | ncycles); |
| 1257 | if (ret) |
| 1258 | return ret; |
| 1259 | |
| 1260 | /* |
| 1261 | * We don't want the CS line to be toggled between each byte/word |
| 1262 | * transfer to the NAND. The only way to guarantee that is to have the |
| 1263 | * NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means: |
| 1264 | * |
| 1265 | * NCS_WR_PULSE = NWE_CYCLE |
| 1266 | */ |
| 1267 | ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_WR_SHIFT, |
| 1268 | ncycles); |
| 1269 | if (ret) |
| 1270 | return ret; |
| 1271 | |
| 1272 | /* |
| 1273 | * As for the write setup timing, the read hold timing depends on the |
| 1274 | * operation done on the NAND: |
| 1275 | * |
| 1276 | * NRD_HOLD = max(tREH, tRHOH) |
| 1277 | */ |
| 1278 | timeps = max(conf->timings.sdr.tREH_min, conf->timings.sdr.tRHOH_min); |
| 1279 | ncycles = DIV_ROUND_UP(timeps, mckperiodps); |
| 1280 | totalcycles = ncycles; |
| 1281 | |
| 1282 | /* |
| 1283 | * TDF = tRHZ - NRD_HOLD |
| 1284 | */ |
| 1285 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tRHZ_max, mckperiodps); |
| 1286 | ncycles -= totalcycles; |
| 1287 | |
| 1288 | /* |
| 1289 | * In ONFI 4.0 specs, tRHZ has been increased to support EDO NANDs and |
| 1290 | * we might end up with a config that does not fit in the TDF field. |
| 1291 | * Just take the max value in this case and hope that the NAND is more |
| 1292 | * tolerant than advertised. |
| 1293 | */ |
| 1294 | if (ncycles > ATMEL_SMC_MODE_TDF_MAX) |
| 1295 | ncycles = ATMEL_SMC_MODE_TDF_MAX; |
| 1296 | else if (ncycles < ATMEL_SMC_MODE_TDF_MIN) |
| 1297 | ncycles = ATMEL_SMC_MODE_TDF_MIN; |
| 1298 | |
| 1299 | smcconf->mode |= ATMEL_SMC_MODE_TDF(ncycles) | |
| 1300 | ATMEL_SMC_MODE_TDFMODE_OPTIMIZED; |
| 1301 | |
| 1302 | /* |
| 1303 | * Read pulse timing directly matches tRP: |
| 1304 | * |
| 1305 | * NRD_PULSE = tRP |
| 1306 | */ |
| 1307 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tRP_min, mckperiodps); |
| 1308 | totalcycles += ncycles; |
| 1309 | ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NRD_SHIFT, |
| 1310 | ncycles); |
| 1311 | if (ret) |
| 1312 | return ret; |
| 1313 | |
| 1314 | /* |
| 1315 | * The write cycle timing is directly matching tWC, but is also |
| 1316 | * dependent on the setup and hold timings we calculated earlier, |
| 1317 | * which gives: |
| 1318 | * |
| 1319 | * NRD_CYCLE = max(tRC, NRD_PULSE + NRD_HOLD) |
| 1320 | * |
| 1321 | * NRD_SETUP is always 0. |
| 1322 | */ |
| 1323 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tRC_min, mckperiodps); |
| 1324 | ncycles = max(totalcycles, ncycles); |
| 1325 | ret = atmel_smc_cs_conf_set_cycle(smcconf, ATMEL_SMC_NRD_SHIFT, |
| 1326 | ncycles); |
| 1327 | if (ret) |
| 1328 | return ret; |
| 1329 | |
| 1330 | /* |
| 1331 | * We don't want the CS line to be toggled between each byte/word |
| 1332 | * transfer from the NAND. The only way to guarantee that is to have |
| 1333 | * the NCS_{WR,RD}_{SETUP,HOLD} timings set to 0, which in turn means: |
| 1334 | * |
| 1335 | * NCS_RD_PULSE = NRD_CYCLE |
| 1336 | */ |
| 1337 | ret = atmel_smc_cs_conf_set_pulse(smcconf, ATMEL_SMC_NCS_RD_SHIFT, |
| 1338 | ncycles); |
| 1339 | if (ret) |
| 1340 | return ret; |
| 1341 | |
| 1342 | /* Txxx timings are directly matching tXXX ones. */ |
| 1343 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tCLR_min, mckperiodps); |
| 1344 | ret = atmel_smc_cs_conf_set_timing(smcconf, |
| 1345 | ATMEL_HSMC_TIMINGS_TCLR_SHIFT, |
| 1346 | ncycles); |
| 1347 | if (ret) |
| 1348 | return ret; |
| 1349 | |
| 1350 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tADL_min, mckperiodps); |
| 1351 | ret = atmel_smc_cs_conf_set_timing(smcconf, |
| 1352 | ATMEL_HSMC_TIMINGS_TADL_SHIFT, |
| 1353 | ncycles); |
Boris Brezillon | be3e83e | 2017-08-23 20:45:01 +0200 | [diff] [blame] | 1354 | /* |
| 1355 | * Version 4 of the ONFI spec mandates that tADL be at least 400 |
| 1356 | * nanoseconds, but, depending on the master clock rate, 400 ns may not |
| 1357 | * fit in the tADL field of the SMC reg. We need to relax the check and |
| 1358 | * accept the -ERANGE return code. |
| 1359 | * |
| 1360 | * Note that previous versions of the ONFI spec had a lower tADL_min |
| 1361 | * (100 or 200 ns). It's not clear why this timing constraint got |
| 1362 | * increased but it seems most NANDs are fine with values lower than |
| 1363 | * 400ns, so we should be safe. |
| 1364 | */ |
| 1365 | if (ret && ret != -ERANGE) |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1366 | return ret; |
| 1367 | |
| 1368 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tAR_min, mckperiodps); |
| 1369 | ret = atmel_smc_cs_conf_set_timing(smcconf, |
| 1370 | ATMEL_HSMC_TIMINGS_TAR_SHIFT, |
| 1371 | ncycles); |
| 1372 | if (ret) |
| 1373 | return ret; |
| 1374 | |
| 1375 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tRR_min, mckperiodps); |
| 1376 | ret = atmel_smc_cs_conf_set_timing(smcconf, |
| 1377 | ATMEL_HSMC_TIMINGS_TRR_SHIFT, |
| 1378 | ncycles); |
| 1379 | if (ret) |
| 1380 | return ret; |
| 1381 | |
| 1382 | ncycles = DIV_ROUND_UP(conf->timings.sdr.tWB_max, mckperiodps); |
| 1383 | ret = atmel_smc_cs_conf_set_timing(smcconf, |
| 1384 | ATMEL_HSMC_TIMINGS_TWB_SHIFT, |
| 1385 | ncycles); |
| 1386 | if (ret) |
| 1387 | return ret; |
| 1388 | |
| 1389 | /* Attach the CS line to the NFC logic. */ |
| 1390 | smcconf->timings |= ATMEL_HSMC_TIMINGS_NFSEL; |
| 1391 | |
| 1392 | /* Set the appropriate data bus width. */ |
| 1393 | if (nand->base.options & NAND_BUSWIDTH_16) |
| 1394 | smcconf->mode |= ATMEL_SMC_MODE_DBW_16; |
| 1395 | |
| 1396 | /* Operate in NRD/NWE READ/WRITEMODE. */ |
| 1397 | smcconf->mode |= ATMEL_SMC_MODE_READMODE_NRD | |
| 1398 | ATMEL_SMC_MODE_WRITEMODE_NWE; |
| 1399 | |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | static int atmel_smc_nand_setup_data_interface(struct atmel_nand *nand, |
| 1404 | int csline, |
| 1405 | const struct nand_data_interface *conf) |
| 1406 | { |
| 1407 | struct atmel_nand_controller *nc; |
| 1408 | struct atmel_smc_cs_conf smcconf; |
| 1409 | struct atmel_nand_cs *cs; |
| 1410 | int ret; |
| 1411 | |
| 1412 | nc = to_nand_controller(nand->base.controller); |
| 1413 | |
| 1414 | ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf); |
| 1415 | if (ret) |
| 1416 | return ret; |
| 1417 | |
| 1418 | if (csline == NAND_DATA_IFACE_CHECK_ONLY) |
| 1419 | return 0; |
| 1420 | |
| 1421 | cs = &nand->cs[csline]; |
| 1422 | cs->smcconf = smcconf; |
| 1423 | atmel_smc_cs_conf_apply(nc->smc, cs->id, &cs->smcconf); |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | static int atmel_hsmc_nand_setup_data_interface(struct atmel_nand *nand, |
| 1429 | int csline, |
| 1430 | const struct nand_data_interface *conf) |
| 1431 | { |
Ludovic Desroches | b0f3ab2 | 2017-07-18 15:22:19 +0200 | [diff] [blame] | 1432 | struct atmel_hsmc_nand_controller *nc; |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1433 | struct atmel_smc_cs_conf smcconf; |
| 1434 | struct atmel_nand_cs *cs; |
| 1435 | int ret; |
| 1436 | |
Ludovic Desroches | b0f3ab2 | 2017-07-18 15:22:19 +0200 | [diff] [blame] | 1437 | nc = to_hsmc_nand_controller(nand->base.controller); |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1438 | |
| 1439 | ret = atmel_smc_nand_prepare_smcconf(nand, conf, &smcconf); |
| 1440 | if (ret) |
| 1441 | return ret; |
| 1442 | |
| 1443 | if (csline == NAND_DATA_IFACE_CHECK_ONLY) |
| 1444 | return 0; |
| 1445 | |
| 1446 | cs = &nand->cs[csline]; |
| 1447 | cs->smcconf = smcconf; |
| 1448 | |
| 1449 | if (cs->rb.type == ATMEL_NAND_NATIVE_RB) |
| 1450 | cs->smcconf.timings |= ATMEL_HSMC_TIMINGS_RBNSEL(cs->rb.id); |
| 1451 | |
Ludovic Desroches | b0f3ab2 | 2017-07-18 15:22:19 +0200 | [diff] [blame] | 1452 | atmel_hsmc_cs_conf_apply(nc->base.smc, nc->hsmc_layout, cs->id, |
| 1453 | &cs->smcconf); |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1454 | |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 1458 | static int atmel_nand_setup_data_interface(struct nand_chip *chip, int csline, |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1459 | const struct nand_data_interface *conf) |
| 1460 | { |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1461 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 1462 | struct atmel_nand_controller *nc; |
| 1463 | |
| 1464 | nc = to_nand_controller(nand->base.controller); |
| 1465 | |
| 1466 | if (csline >= nand->numcs || |
| 1467 | (csline < 0 && csline != NAND_DATA_IFACE_CHECK_ONLY)) |
| 1468 | return -EINVAL; |
| 1469 | |
| 1470 | return nc->caps->ops->setup_data_interface(nand, csline, conf); |
| 1471 | } |
| 1472 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1473 | static void atmel_nand_init(struct atmel_nand_controller *nc, |
| 1474 | struct atmel_nand *nand) |
| 1475 | { |
| 1476 | struct nand_chip *chip = &nand->base; |
| 1477 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1478 | |
| 1479 | mtd->dev.parent = nc->dev; |
| 1480 | nand->base.controller = &nc->base; |
| 1481 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1482 | chip->legacy.cmd_ctrl = atmel_nand_cmd_ctrl; |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1483 | chip->legacy.read_byte = atmel_nand_read_byte; |
| 1484 | chip->legacy.write_byte = atmel_nand_write_byte; |
| 1485 | chip->legacy.read_buf = atmel_nand_read_buf; |
| 1486 | chip->legacy.write_buf = atmel_nand_write_buf; |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 1487 | chip->legacy.select_chip = atmel_nand_select_chip; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1488 | |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 1489 | if (!nc->mck || !nc->caps->ops->setup_data_interface) |
| 1490 | chip->options |= NAND_KEEP_TIMINGS; |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 1491 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1492 | /* Some NANDs require a longer delay than the default one (20us). */ |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1493 | chip->legacy.chip_delay = 40; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1494 | |
| 1495 | /* |
| 1496 | * Use a bounce buffer when the buffer passed by the MTD user is not |
| 1497 | * suitable for DMA. |
| 1498 | */ |
| 1499 | if (nc->dmac) |
| 1500 | chip->options |= NAND_USE_BOUNCE_BUFFER; |
| 1501 | |
| 1502 | /* Default to HW ECC if pmecc is available. */ |
| 1503 | if (nc->pmecc) |
| 1504 | chip->ecc.mode = NAND_ECC_HW; |
| 1505 | } |
| 1506 | |
| 1507 | static void atmel_smc_nand_init(struct atmel_nand_controller *nc, |
| 1508 | struct atmel_nand *nand) |
| 1509 | { |
| 1510 | struct nand_chip *chip = &nand->base; |
| 1511 | struct atmel_smc_nand_controller *smc_nc; |
| 1512 | int i; |
| 1513 | |
| 1514 | atmel_nand_init(nc, nand); |
| 1515 | |
| 1516 | smc_nc = to_smc_nand_controller(chip->controller); |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 1517 | if (!smc_nc->ebi_csa_regmap) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1518 | return; |
| 1519 | |
| 1520 | /* Attach the CS to the NAND Flash logic. */ |
| 1521 | for (i = 0; i < nand->numcs; i++) |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1522 | regmap_update_bits(smc_nc->ebi_csa_regmap, |
| 1523 | smc_nc->ebi_csa->offs, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1524 | BIT(nand->cs[i].id), BIT(nand->cs[i].id)); |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1525 | |
| 1526 | if (smc_nc->ebi_csa->nfd0_on_d16) |
| 1527 | regmap_update_bits(smc_nc->ebi_csa_regmap, |
| 1528 | smc_nc->ebi_csa->offs, |
| 1529 | smc_nc->ebi_csa->nfd0_on_d16, |
| 1530 | smc_nc->ebi_csa->nfd0_on_d16); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void atmel_hsmc_nand_init(struct atmel_nand_controller *nc, |
| 1534 | struct atmel_nand *nand) |
| 1535 | { |
| 1536 | struct nand_chip *chip = &nand->base; |
| 1537 | |
| 1538 | atmel_nand_init(nc, nand); |
| 1539 | |
| 1540 | /* Overload some methods for the HSMC controller. */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1541 | chip->legacy.cmd_ctrl = atmel_hsmc_nand_cmd_ctrl; |
Boris Brezillon | 7d6c37e | 2018-11-11 08:55:22 +0100 | [diff] [blame] | 1542 | chip->legacy.select_chip = atmel_hsmc_nand_select_chip; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
Miquel Raynal | 7928225 | 2018-07-25 15:31:40 +0200 | [diff] [blame] | 1545 | static int atmel_nand_controller_remove_nand(struct atmel_nand *nand) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1546 | { |
| 1547 | struct nand_chip *chip = &nand->base; |
| 1548 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1549 | int ret; |
| 1550 | |
| 1551 | ret = mtd_device_unregister(mtd); |
| 1552 | if (ret) |
| 1553 | return ret; |
| 1554 | |
| 1555 | nand_cleanup(chip); |
| 1556 | list_del(&nand->node); |
| 1557 | |
| 1558 | return 0; |
| 1559 | } |
| 1560 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1561 | static struct atmel_nand *atmel_nand_create(struct atmel_nand_controller *nc, |
| 1562 | struct device_node *np, |
| 1563 | int reg_cells) |
| 1564 | { |
| 1565 | struct atmel_nand *nand; |
| 1566 | struct gpio_desc *gpio; |
| 1567 | int numcs, ret, i; |
| 1568 | |
| 1569 | numcs = of_property_count_elems_of_size(np, "reg", |
| 1570 | reg_cells * sizeof(u32)); |
| 1571 | if (numcs < 1) { |
| 1572 | dev_err(nc->dev, "Missing or invalid reg property\n"); |
| 1573 | return ERR_PTR(-EINVAL); |
| 1574 | } |
| 1575 | |
Gustavo A. R. Silva | 2f91eb6 | 2018-08-23 20:09:38 -0500 | [diff] [blame] | 1576 | nand = devm_kzalloc(nc->dev, struct_size(nand, cs, numcs), GFP_KERNEL); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1577 | if (!nand) { |
| 1578 | dev_err(nc->dev, "Failed to allocate NAND object\n"); |
| 1579 | return ERR_PTR(-ENOMEM); |
| 1580 | } |
| 1581 | |
| 1582 | nand->numcs = numcs; |
| 1583 | |
| 1584 | gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "det", 0, |
| 1585 | &np->fwnode, GPIOD_IN, |
| 1586 | "nand-det"); |
| 1587 | if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) { |
| 1588 | dev_err(nc->dev, |
| 1589 | "Failed to get detect gpio (err = %ld)\n", |
| 1590 | PTR_ERR(gpio)); |
| 1591 | return ERR_CAST(gpio); |
| 1592 | } |
| 1593 | |
| 1594 | if (!IS_ERR(gpio)) |
| 1595 | nand->cdgpio = gpio; |
| 1596 | |
| 1597 | for (i = 0; i < numcs; i++) { |
| 1598 | struct resource res; |
| 1599 | u32 val; |
| 1600 | |
| 1601 | ret = of_address_to_resource(np, 0, &res); |
| 1602 | if (ret) { |
| 1603 | dev_err(nc->dev, "Invalid reg property (err = %d)\n", |
| 1604 | ret); |
| 1605 | return ERR_PTR(ret); |
| 1606 | } |
| 1607 | |
| 1608 | ret = of_property_read_u32_index(np, "reg", i * reg_cells, |
| 1609 | &val); |
| 1610 | if (ret) { |
| 1611 | dev_err(nc->dev, "Invalid reg property (err = %d)\n", |
| 1612 | ret); |
| 1613 | return ERR_PTR(ret); |
| 1614 | } |
| 1615 | |
| 1616 | nand->cs[i].id = val; |
| 1617 | |
| 1618 | nand->cs[i].io.dma = res.start; |
| 1619 | nand->cs[i].io.virt = devm_ioremap_resource(nc->dev, &res); |
| 1620 | if (IS_ERR(nand->cs[i].io.virt)) |
| 1621 | return ERR_CAST(nand->cs[i].io.virt); |
| 1622 | |
| 1623 | if (!of_property_read_u32(np, "atmel,rb", &val)) { |
| 1624 | if (val > ATMEL_NFC_MAX_RB_ID) |
| 1625 | return ERR_PTR(-EINVAL); |
| 1626 | |
| 1627 | nand->cs[i].rb.type = ATMEL_NAND_NATIVE_RB; |
| 1628 | nand->cs[i].rb.id = val; |
| 1629 | } else { |
| 1630 | gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, |
| 1631 | "rb", i, &np->fwnode, |
| 1632 | GPIOD_IN, "nand-rb"); |
| 1633 | if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) { |
| 1634 | dev_err(nc->dev, |
| 1635 | "Failed to get R/B gpio (err = %ld)\n", |
| 1636 | PTR_ERR(gpio)); |
| 1637 | return ERR_CAST(gpio); |
| 1638 | } |
| 1639 | |
| 1640 | if (!IS_ERR(gpio)) { |
| 1641 | nand->cs[i].rb.type = ATMEL_NAND_GPIO_RB; |
| 1642 | nand->cs[i].rb.gpio = gpio; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | gpio = devm_fwnode_get_index_gpiod_from_child(nc->dev, "cs", |
| 1647 | i, &np->fwnode, |
| 1648 | GPIOD_OUT_HIGH, |
| 1649 | "nand-cs"); |
| 1650 | if (IS_ERR(gpio) && PTR_ERR(gpio) != -ENOENT) { |
| 1651 | dev_err(nc->dev, |
| 1652 | "Failed to get CS gpio (err = %ld)\n", |
| 1653 | PTR_ERR(gpio)); |
| 1654 | return ERR_CAST(gpio); |
| 1655 | } |
| 1656 | |
| 1657 | if (!IS_ERR(gpio)) |
| 1658 | nand->cs[i].csgpio = gpio; |
| 1659 | } |
| 1660 | |
| 1661 | nand_set_flash_node(&nand->base, np); |
| 1662 | |
| 1663 | return nand; |
| 1664 | } |
| 1665 | |
| 1666 | static int |
| 1667 | atmel_nand_controller_add_nand(struct atmel_nand_controller *nc, |
| 1668 | struct atmel_nand *nand) |
| 1669 | { |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1670 | struct nand_chip *chip = &nand->base; |
| 1671 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1672 | int ret; |
| 1673 | |
| 1674 | /* No card inserted, skip this NAND. */ |
| 1675 | if (nand->cdgpio && gpiod_get_value(nand->cdgpio)) { |
| 1676 | dev_info(nc->dev, "No SmartMedia card inserted.\n"); |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
| 1680 | nc->caps->ops->nand_init(nc, nand); |
| 1681 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 1682 | ret = nand_scan(chip, nand->numcs); |
Miquel Raynal | 7928225 | 2018-07-25 15:31:40 +0200 | [diff] [blame] | 1683 | if (ret) { |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1684 | dev_err(nc->dev, "NAND scan failed: %d\n", ret); |
Miquel Raynal | 7928225 | 2018-07-25 15:31:40 +0200 | [diff] [blame] | 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | ret = mtd_device_register(mtd, NULL, 0); |
| 1689 | if (ret) { |
| 1690 | dev_err(nc->dev, "Failed to register mtd device: %d\n", ret); |
| 1691 | nand_cleanup(chip); |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
| 1695 | list_add_tail(&nand->node, &nc->chips); |
| 1696 | |
| 1697 | return 0; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | static int |
| 1701 | atmel_nand_controller_remove_nands(struct atmel_nand_controller *nc) |
| 1702 | { |
| 1703 | struct atmel_nand *nand, *tmp; |
| 1704 | int ret; |
| 1705 | |
| 1706 | list_for_each_entry_safe(nand, tmp, &nc->chips, node) { |
Miquel Raynal | 7928225 | 2018-07-25 15:31:40 +0200 | [diff] [blame] | 1707 | ret = atmel_nand_controller_remove_nand(nand); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1708 | if (ret) |
| 1709 | return ret; |
| 1710 | } |
| 1711 | |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | static int |
| 1716 | atmel_nand_controller_legacy_add_nands(struct atmel_nand_controller *nc) |
| 1717 | { |
| 1718 | struct device *dev = nc->dev; |
| 1719 | struct platform_device *pdev = to_platform_device(dev); |
| 1720 | struct atmel_nand *nand; |
| 1721 | struct gpio_desc *gpio; |
| 1722 | struct resource *res; |
| 1723 | |
| 1724 | /* |
| 1725 | * Legacy bindings only allow connecting a single NAND with a unique CS |
| 1726 | * line to the controller. |
| 1727 | */ |
| 1728 | nand = devm_kzalloc(nc->dev, sizeof(*nand) + sizeof(*nand->cs), |
| 1729 | GFP_KERNEL); |
| 1730 | if (!nand) |
| 1731 | return -ENOMEM; |
| 1732 | |
| 1733 | nand->numcs = 1; |
| 1734 | |
| 1735 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1736 | nand->cs[0].io.virt = devm_ioremap_resource(dev, res); |
| 1737 | if (IS_ERR(nand->cs[0].io.virt)) |
| 1738 | return PTR_ERR(nand->cs[0].io.virt); |
| 1739 | |
| 1740 | nand->cs[0].io.dma = res->start; |
| 1741 | |
| 1742 | /* |
| 1743 | * The old driver was hardcoding the CS id to 3 for all sama5 |
| 1744 | * controllers. Since this id is only meaningful for the sama5 |
| 1745 | * controller we can safely assign this id to 3 no matter the |
| 1746 | * controller. |
| 1747 | * If one wants to connect a NAND to a different CS line, he will |
| 1748 | * have to use the new bindings. |
| 1749 | */ |
| 1750 | nand->cs[0].id = 3; |
| 1751 | |
| 1752 | /* R/B GPIO. */ |
| 1753 | gpio = devm_gpiod_get_index_optional(dev, NULL, 0, GPIOD_IN); |
| 1754 | if (IS_ERR(gpio)) { |
| 1755 | dev_err(dev, "Failed to get R/B gpio (err = %ld)\n", |
| 1756 | PTR_ERR(gpio)); |
| 1757 | return PTR_ERR(gpio); |
| 1758 | } |
| 1759 | |
| 1760 | if (gpio) { |
| 1761 | nand->cs[0].rb.type = ATMEL_NAND_GPIO_RB; |
| 1762 | nand->cs[0].rb.gpio = gpio; |
| 1763 | } |
| 1764 | |
| 1765 | /* CS GPIO. */ |
| 1766 | gpio = devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_HIGH); |
| 1767 | if (IS_ERR(gpio)) { |
| 1768 | dev_err(dev, "Failed to get CS gpio (err = %ld)\n", |
| 1769 | PTR_ERR(gpio)); |
| 1770 | return PTR_ERR(gpio); |
| 1771 | } |
| 1772 | |
| 1773 | nand->cs[0].csgpio = gpio; |
| 1774 | |
| 1775 | /* Card detect GPIO. */ |
| 1776 | gpio = devm_gpiod_get_index_optional(nc->dev, NULL, 2, GPIOD_IN); |
| 1777 | if (IS_ERR(gpio)) { |
| 1778 | dev_err(dev, |
| 1779 | "Failed to get detect gpio (err = %ld)\n", |
| 1780 | PTR_ERR(gpio)); |
| 1781 | return PTR_ERR(gpio); |
| 1782 | } |
| 1783 | |
| 1784 | nand->cdgpio = gpio; |
| 1785 | |
| 1786 | nand_set_flash_node(&nand->base, nc->dev->of_node); |
| 1787 | |
| 1788 | return atmel_nand_controller_add_nand(nc, nand); |
| 1789 | } |
| 1790 | |
| 1791 | static int atmel_nand_controller_add_nands(struct atmel_nand_controller *nc) |
| 1792 | { |
| 1793 | struct device_node *np, *nand_np; |
| 1794 | struct device *dev = nc->dev; |
| 1795 | int ret, reg_cells; |
| 1796 | u32 val; |
| 1797 | |
| 1798 | /* We do not retrieve the SMC syscon when parsing old DTs. */ |
| 1799 | if (nc->caps->legacy_of_bindings) |
| 1800 | return atmel_nand_controller_legacy_add_nands(nc); |
| 1801 | |
| 1802 | np = dev->of_node; |
| 1803 | |
| 1804 | ret = of_property_read_u32(np, "#address-cells", &val); |
| 1805 | if (ret) { |
| 1806 | dev_err(dev, "missing #address-cells property\n"); |
| 1807 | return ret; |
| 1808 | } |
| 1809 | |
| 1810 | reg_cells = val; |
| 1811 | |
| 1812 | ret = of_property_read_u32(np, "#size-cells", &val); |
| 1813 | if (ret) { |
| 1814 | dev_err(dev, "missing #address-cells property\n"); |
| 1815 | return ret; |
| 1816 | } |
| 1817 | |
| 1818 | reg_cells += val; |
| 1819 | |
| 1820 | for_each_child_of_node(np, nand_np) { |
| 1821 | struct atmel_nand *nand; |
| 1822 | |
| 1823 | nand = atmel_nand_create(nc, nand_np, reg_cells); |
| 1824 | if (IS_ERR(nand)) { |
| 1825 | ret = PTR_ERR(nand); |
| 1826 | goto err; |
| 1827 | } |
| 1828 | |
| 1829 | ret = atmel_nand_controller_add_nand(nc, nand); |
| 1830 | if (ret) |
| 1831 | goto err; |
| 1832 | } |
| 1833 | |
| 1834 | return 0; |
| 1835 | |
| 1836 | err: |
| 1837 | atmel_nand_controller_remove_nands(nc); |
| 1838 | |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
| 1842 | static void atmel_nand_controller_cleanup(struct atmel_nand_controller *nc) |
| 1843 | { |
| 1844 | if (nc->dmac) |
| 1845 | dma_release_channel(nc->dmac); |
| 1846 | |
| 1847 | clk_put(nc->mck); |
| 1848 | } |
| 1849 | |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1850 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9260_ebi_csa = { |
| 1851 | .offs = AT91SAM9260_MATRIX_EBICSA, |
| 1852 | }; |
| 1853 | |
| 1854 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9261_ebi_csa = { |
| 1855 | .offs = AT91SAM9261_MATRIX_EBICSA, |
| 1856 | }; |
| 1857 | |
| 1858 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9263_ebi_csa = { |
| 1859 | .offs = AT91SAM9263_MATRIX_EBI0CSA, |
| 1860 | }; |
| 1861 | |
| 1862 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9rl_ebi_csa = { |
| 1863 | .offs = AT91SAM9RL_MATRIX_EBICSA, |
| 1864 | }; |
| 1865 | |
| 1866 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9g45_ebi_csa = { |
| 1867 | .offs = AT91SAM9G45_MATRIX_EBICSA, |
| 1868 | }; |
| 1869 | |
| 1870 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9n12_ebi_csa = { |
| 1871 | .offs = AT91SAM9N12_MATRIX_EBICSA, |
| 1872 | }; |
| 1873 | |
| 1874 | static const struct atmel_smc_nand_ebi_csa_cfg at91sam9x5_ebi_csa = { |
| 1875 | .offs = AT91SAM9X5_MATRIX_EBICSA, |
| 1876 | }; |
| 1877 | |
| 1878 | static const struct atmel_smc_nand_ebi_csa_cfg sam9x60_ebi_csa = { |
| 1879 | .offs = AT91_SFR_CCFG_EBICSA, |
| 1880 | .nfd0_on_d16 = AT91_SFR_CCFG_NFD0_ON_D16, |
| 1881 | }; |
| 1882 | |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 1883 | static const struct of_device_id atmel_ebi_csa_regmap_of_ids[] = { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1884 | { |
| 1885 | .compatible = "atmel,at91sam9260-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1886 | .data = &at91sam9260_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1887 | }, |
| 1888 | { |
| 1889 | .compatible = "atmel,at91sam9261-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1890 | .data = &at91sam9261_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1891 | }, |
| 1892 | { |
| 1893 | .compatible = "atmel,at91sam9263-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1894 | .data = &at91sam9263_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1895 | }, |
| 1896 | { |
| 1897 | .compatible = "atmel,at91sam9rl-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1898 | .data = &at91sam9rl_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1899 | }, |
| 1900 | { |
| 1901 | .compatible = "atmel,at91sam9g45-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1902 | .data = &at91sam9g45_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1903 | }, |
| 1904 | { |
| 1905 | .compatible = "atmel,at91sam9n12-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1906 | .data = &at91sam9n12_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1907 | }, |
| 1908 | { |
| 1909 | .compatible = "atmel,at91sam9x5-matrix", |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 1910 | .data = &at91sam9x5_ebi_csa, |
| 1911 | }, |
| 1912 | { |
| 1913 | .compatible = "microchip,sam9x60-sfr", |
| 1914 | .data = &sam9x60_ebi_csa, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1915 | }, |
Christophe Jaillet | 038e8ad6e | 2017-04-11 07:22:52 +0200 | [diff] [blame] | 1916 | { /* sentinel */ }, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1917 | }; |
| 1918 | |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1919 | static int atmel_nand_attach_chip(struct nand_chip *chip) |
| 1920 | { |
| 1921 | struct atmel_nand_controller *nc = to_nand_controller(chip->controller); |
| 1922 | struct atmel_nand *nand = to_atmel_nand(chip); |
| 1923 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1924 | int ret; |
| 1925 | |
| 1926 | ret = nc->caps->ops->ecc_init(chip); |
| 1927 | if (ret) |
| 1928 | return ret; |
| 1929 | |
| 1930 | if (nc->caps->legacy_of_bindings || !nc->dev->of_node) { |
| 1931 | /* |
| 1932 | * We keep the MTD name unchanged to avoid breaking platforms |
| 1933 | * where the MTD cmdline parser is used and the bootloader |
| 1934 | * has not been updated to use the new naming scheme. |
| 1935 | */ |
| 1936 | mtd->name = "atmel_nand"; |
| 1937 | } else if (!mtd->name) { |
| 1938 | /* |
| 1939 | * If the new bindings are used and the bootloader has not been |
| 1940 | * updated to pass a new mtdparts parameter on the cmdline, you |
| 1941 | * should define the following property in your nand node: |
| 1942 | * |
| 1943 | * label = "atmel_nand"; |
| 1944 | * |
| 1945 | * This way, mtd->name will be set by the core when |
| 1946 | * nand_set_flash_node() is called. |
| 1947 | */ |
| 1948 | mtd->name = devm_kasprintf(nc->dev, GFP_KERNEL, |
| 1949 | "%s:nand.%d", dev_name(nc->dev), |
| 1950 | nand->cs[0].id); |
| 1951 | if (!mtd->name) { |
| 1952 | dev_err(nc->dev, "Failed to allocate mtd->name\n"); |
| 1953 | return -ENOMEM; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | static const struct nand_controller_ops atmel_nand_controller_ops = { |
| 1961 | .attach_chip = atmel_nand_attach_chip, |
Boris Brezillon | 7a08dba | 2018-11-11 08:55:24 +0100 | [diff] [blame] | 1962 | .setup_data_interface = atmel_nand_setup_data_interface, |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1963 | }; |
| 1964 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1965 | static int atmel_nand_controller_init(struct atmel_nand_controller *nc, |
| 1966 | struct platform_device *pdev, |
| 1967 | const struct atmel_nand_controller_caps *caps) |
| 1968 | { |
| 1969 | struct device *dev = &pdev->dev; |
| 1970 | struct device_node *np = dev->of_node; |
| 1971 | int ret; |
| 1972 | |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 1973 | nand_controller_init(&nc->base); |
Miquel Raynal | 577e010 | 2018-07-25 15:31:41 +0200 | [diff] [blame] | 1974 | nc->base.ops = &atmel_nand_controller_ops; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1975 | INIT_LIST_HEAD(&nc->chips); |
| 1976 | nc->dev = dev; |
| 1977 | nc->caps = caps; |
| 1978 | |
| 1979 | platform_set_drvdata(pdev, nc); |
| 1980 | |
| 1981 | nc->pmecc = devm_atmel_pmecc_get(dev); |
| 1982 | if (IS_ERR(nc->pmecc)) { |
| 1983 | ret = PTR_ERR(nc->pmecc); |
| 1984 | if (ret != -EPROBE_DEFER) |
| 1985 | dev_err(dev, "Could not get PMECC object (err = %d)\n", |
| 1986 | ret); |
| 1987 | return ret; |
| 1988 | } |
| 1989 | |
Peter Rosin | efc6362 | 2018-03-29 15:10:54 +0200 | [diff] [blame] | 1990 | if (nc->caps->has_dma && !atmel_nand_avoid_dma) { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 1991 | dma_cap_mask_t mask; |
| 1992 | |
| 1993 | dma_cap_zero(mask); |
| 1994 | dma_cap_set(DMA_MEMCPY, mask); |
| 1995 | |
| 1996 | nc->dmac = dma_request_channel(mask, NULL, NULL); |
| 1997 | if (!nc->dmac) |
| 1998 | dev_err(nc->dev, "Failed to request DMA channel\n"); |
| 1999 | } |
| 2000 | |
| 2001 | /* We do not retrieve the SMC syscon when parsing old DTs. */ |
| 2002 | if (nc->caps->legacy_of_bindings) |
| 2003 | return 0; |
| 2004 | |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 2005 | nc->mck = of_clk_get(dev->parent->of_node, 0); |
| 2006 | if (IS_ERR(nc->mck)) { |
| 2007 | dev_err(dev, "Failed to retrieve MCK clk\n"); |
| 2008 | return PTR_ERR(nc->mck); |
| 2009 | } |
| 2010 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2011 | np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0); |
| 2012 | if (!np) { |
| 2013 | dev_err(dev, "Missing or invalid atmel,smc property\n"); |
| 2014 | return -EINVAL; |
| 2015 | } |
| 2016 | |
| 2017 | nc->smc = syscon_node_to_regmap(np); |
| 2018 | of_node_put(np); |
| 2019 | if (IS_ERR(nc->smc)) { |
Dan Carpenter | 70106dd | 2017-04-04 11:15:46 +0300 | [diff] [blame] | 2020 | ret = PTR_ERR(nc->smc); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2021 | dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret); |
| 2022 | return ret; |
| 2023 | } |
| 2024 | |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | static int |
| 2029 | atmel_smc_nand_controller_init(struct atmel_smc_nand_controller *nc) |
| 2030 | { |
| 2031 | struct device *dev = nc->base.dev; |
| 2032 | const struct of_device_id *match; |
| 2033 | struct device_node *np; |
| 2034 | int ret; |
| 2035 | |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2036 | /* We do not retrieve the EBICSA regmap when parsing old DTs. */ |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2037 | if (nc->base.caps->legacy_of_bindings) |
| 2038 | return 0; |
| 2039 | |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2040 | np = of_parse_phandle(dev->parent->of_node, |
| 2041 | nc->base.caps->ebi_csa_regmap_name, 0); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2042 | if (!np) |
| 2043 | return 0; |
| 2044 | |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2045 | match = of_match_node(atmel_ebi_csa_regmap_of_ids, np); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2046 | if (!match) { |
| 2047 | of_node_put(np); |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2051 | nc->ebi_csa_regmap = syscon_node_to_regmap(np); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2052 | of_node_put(np); |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2053 | if (IS_ERR(nc->ebi_csa_regmap)) { |
| 2054 | ret = PTR_ERR(nc->ebi_csa_regmap); |
| 2055 | dev_err(dev, "Could not get EBICSA regmap (err = %d)\n", ret); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2056 | return ret; |
| 2057 | } |
| 2058 | |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 2059 | nc->ebi_csa = (struct atmel_smc_nand_ebi_csa_cfg *)match->data; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2060 | |
| 2061 | /* |
| 2062 | * The at91sam9263 has 2 EBIs, if the NAND controller is under EBI1 |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 2063 | * add 4 to ->ebi_csa->offs. |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2064 | */ |
| 2065 | if (of_device_is_compatible(dev->parent->of_node, |
| 2066 | "atmel,at91sam9263-ebi1")) |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 2067 | nc->ebi_csa->offs += 4; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2068 | |
| 2069 | return 0; |
| 2070 | } |
| 2071 | |
| 2072 | static int |
| 2073 | atmel_hsmc_nand_controller_legacy_init(struct atmel_hsmc_nand_controller *nc) |
| 2074 | { |
| 2075 | struct regmap_config regmap_conf = { |
| 2076 | .reg_bits = 32, |
| 2077 | .val_bits = 32, |
| 2078 | .reg_stride = 4, |
| 2079 | }; |
| 2080 | |
| 2081 | struct device *dev = nc->base.dev; |
| 2082 | struct device_node *nand_np, *nfc_np; |
| 2083 | void __iomem *iomem; |
| 2084 | struct resource res; |
| 2085 | int ret; |
| 2086 | |
| 2087 | nand_np = dev->of_node; |
Johan Hovold | 5d1e9c2 | 2018-08-27 10:21:49 +0200 | [diff] [blame] | 2088 | nfc_np = of_get_compatible_child(dev->of_node, "atmel,sama5d3-nfc"); |
Gustavo A. R. Silva | fbed202 | 2018-09-18 08:55:55 -0500 | [diff] [blame] | 2089 | if (!nfc_np) { |
| 2090 | dev_err(dev, "Could not find device node for sama5d3-nfc\n"); |
| 2091 | return -ENODEV; |
| 2092 | } |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2093 | |
| 2094 | nc->clk = of_clk_get(nfc_np, 0); |
| 2095 | if (IS_ERR(nc->clk)) { |
| 2096 | ret = PTR_ERR(nc->clk); |
| 2097 | dev_err(dev, "Failed to retrieve HSMC clock (err = %d)\n", |
| 2098 | ret); |
| 2099 | goto out; |
| 2100 | } |
| 2101 | |
| 2102 | ret = clk_prepare_enable(nc->clk); |
| 2103 | if (ret) { |
| 2104 | dev_err(dev, "Failed to enable the HSMC clock (err = %d)\n", |
| 2105 | ret); |
| 2106 | goto out; |
| 2107 | } |
| 2108 | |
| 2109 | nc->irq = of_irq_get(nand_np, 0); |
Sergei Shtylyov | 892dd18 | 2017-08-06 00:14:28 +0300 | [diff] [blame] | 2110 | if (nc->irq <= 0) { |
| 2111 | ret = nc->irq ?: -ENXIO; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2112 | if (ret != -EPROBE_DEFER) |
| 2113 | dev_err(dev, "Failed to get IRQ number (err = %d)\n", |
| 2114 | ret); |
| 2115 | goto out; |
| 2116 | } |
| 2117 | |
| 2118 | ret = of_address_to_resource(nfc_np, 0, &res); |
| 2119 | if (ret) { |
| 2120 | dev_err(dev, "Invalid or missing NFC IO resource (err = %d)\n", |
| 2121 | ret); |
| 2122 | goto out; |
| 2123 | } |
| 2124 | |
| 2125 | iomem = devm_ioremap_resource(dev, &res); |
| 2126 | if (IS_ERR(iomem)) { |
| 2127 | ret = PTR_ERR(iomem); |
| 2128 | goto out; |
| 2129 | } |
| 2130 | |
| 2131 | regmap_conf.name = "nfc-io"; |
| 2132 | regmap_conf.max_register = resource_size(&res) - 4; |
| 2133 | nc->io = devm_regmap_init_mmio(dev, iomem, ®map_conf); |
| 2134 | if (IS_ERR(nc->io)) { |
| 2135 | ret = PTR_ERR(nc->io); |
| 2136 | dev_err(dev, "Could not create NFC IO regmap (err = %d)\n", |
| 2137 | ret); |
| 2138 | goto out; |
| 2139 | } |
| 2140 | |
| 2141 | ret = of_address_to_resource(nfc_np, 1, &res); |
| 2142 | if (ret) { |
| 2143 | dev_err(dev, "Invalid or missing HSMC resource (err = %d)\n", |
| 2144 | ret); |
| 2145 | goto out; |
| 2146 | } |
| 2147 | |
| 2148 | iomem = devm_ioremap_resource(dev, &res); |
| 2149 | if (IS_ERR(iomem)) { |
| 2150 | ret = PTR_ERR(iomem); |
| 2151 | goto out; |
| 2152 | } |
| 2153 | |
| 2154 | regmap_conf.name = "smc"; |
| 2155 | regmap_conf.max_register = resource_size(&res) - 4; |
| 2156 | nc->base.smc = devm_regmap_init_mmio(dev, iomem, ®map_conf); |
| 2157 | if (IS_ERR(nc->base.smc)) { |
| 2158 | ret = PTR_ERR(nc->base.smc); |
| 2159 | dev_err(dev, "Could not create NFC IO regmap (err = %d)\n", |
| 2160 | ret); |
| 2161 | goto out; |
| 2162 | } |
| 2163 | |
| 2164 | ret = of_address_to_resource(nfc_np, 2, &res); |
| 2165 | if (ret) { |
| 2166 | dev_err(dev, "Invalid or missing SRAM resource (err = %d)\n", |
| 2167 | ret); |
| 2168 | goto out; |
| 2169 | } |
| 2170 | |
| 2171 | nc->sram.virt = devm_ioremap_resource(dev, &res); |
| 2172 | if (IS_ERR(nc->sram.virt)) { |
| 2173 | ret = PTR_ERR(nc->sram.virt); |
| 2174 | goto out; |
| 2175 | } |
| 2176 | |
| 2177 | nc->sram.dma = res.start; |
| 2178 | |
| 2179 | out: |
| 2180 | of_node_put(nfc_np); |
| 2181 | |
| 2182 | return ret; |
| 2183 | } |
| 2184 | |
| 2185 | static int |
| 2186 | atmel_hsmc_nand_controller_init(struct atmel_hsmc_nand_controller *nc) |
| 2187 | { |
| 2188 | struct device *dev = nc->base.dev; |
| 2189 | struct device_node *np; |
| 2190 | int ret; |
| 2191 | |
| 2192 | np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0); |
| 2193 | if (!np) { |
| 2194 | dev_err(dev, "Missing or invalid atmel,smc property\n"); |
| 2195 | return -EINVAL; |
| 2196 | } |
| 2197 | |
Ludovic Desroches | b0f3ab2 | 2017-07-18 15:22:19 +0200 | [diff] [blame] | 2198 | nc->hsmc_layout = atmel_hsmc_get_reg_layout(np); |
| 2199 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2200 | nc->irq = of_irq_get(np, 0); |
| 2201 | of_node_put(np); |
Sergei Shtylyov | 892dd18 | 2017-08-06 00:14:28 +0300 | [diff] [blame] | 2202 | if (nc->irq <= 0) { |
| 2203 | ret = nc->irq ?: -ENXIO; |
| 2204 | if (ret != -EPROBE_DEFER) |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2205 | dev_err(dev, "Failed to get IRQ number (err = %d)\n", |
Sergei Shtylyov | 892dd18 | 2017-08-06 00:14:28 +0300 | [diff] [blame] | 2206 | ret); |
| 2207 | return ret; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | np = of_parse_phandle(dev->of_node, "atmel,nfc-io", 0); |
| 2211 | if (!np) { |
| 2212 | dev_err(dev, "Missing or invalid atmel,nfc-io property\n"); |
| 2213 | return -EINVAL; |
| 2214 | } |
| 2215 | |
| 2216 | nc->io = syscon_node_to_regmap(np); |
| 2217 | of_node_put(np); |
| 2218 | if (IS_ERR(nc->io)) { |
| 2219 | ret = PTR_ERR(nc->io); |
| 2220 | dev_err(dev, "Could not get NFC IO regmap (err = %d)\n", ret); |
| 2221 | return ret; |
| 2222 | } |
| 2223 | |
| 2224 | nc->sram.pool = of_gen_pool_get(nc->base.dev->of_node, |
| 2225 | "atmel,nfc-sram", 0); |
| 2226 | if (!nc->sram.pool) { |
| 2227 | dev_err(nc->base.dev, "Missing SRAM\n"); |
| 2228 | return -ENOMEM; |
| 2229 | } |
| 2230 | |
Boris Brezillon | d28395c | 2018-07-09 22:09:23 +0200 | [diff] [blame] | 2231 | nc->sram.virt = (void __iomem *)gen_pool_dma_alloc(nc->sram.pool, |
| 2232 | ATMEL_NFC_SRAM_SIZE, |
| 2233 | &nc->sram.dma); |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2234 | if (!nc->sram.virt) { |
| 2235 | dev_err(nc->base.dev, |
| 2236 | "Could not allocate memory from the NFC SRAM pool\n"); |
| 2237 | return -ENOMEM; |
| 2238 | } |
| 2239 | |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | static int |
| 2244 | atmel_hsmc_nand_controller_remove(struct atmel_nand_controller *nc) |
| 2245 | { |
| 2246 | struct atmel_hsmc_nand_controller *hsmc_nc; |
| 2247 | int ret; |
| 2248 | |
| 2249 | ret = atmel_nand_controller_remove_nands(nc); |
| 2250 | if (ret) |
| 2251 | return ret; |
| 2252 | |
| 2253 | hsmc_nc = container_of(nc, struct atmel_hsmc_nand_controller, base); |
| 2254 | if (hsmc_nc->sram.pool) |
| 2255 | gen_pool_free(hsmc_nc->sram.pool, |
| 2256 | (unsigned long)hsmc_nc->sram.virt, |
| 2257 | ATMEL_NFC_SRAM_SIZE); |
| 2258 | |
| 2259 | if (hsmc_nc->clk) { |
| 2260 | clk_disable_unprepare(hsmc_nc->clk); |
| 2261 | clk_put(hsmc_nc->clk); |
| 2262 | } |
| 2263 | |
| 2264 | atmel_nand_controller_cleanup(nc); |
| 2265 | |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
| 2269 | static int atmel_hsmc_nand_controller_probe(struct platform_device *pdev, |
| 2270 | const struct atmel_nand_controller_caps *caps) |
| 2271 | { |
| 2272 | struct device *dev = &pdev->dev; |
| 2273 | struct atmel_hsmc_nand_controller *nc; |
| 2274 | int ret; |
| 2275 | |
| 2276 | nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL); |
| 2277 | if (!nc) |
| 2278 | return -ENOMEM; |
| 2279 | |
| 2280 | ret = atmel_nand_controller_init(&nc->base, pdev, caps); |
| 2281 | if (ret) |
| 2282 | return ret; |
| 2283 | |
| 2284 | if (caps->legacy_of_bindings) |
| 2285 | ret = atmel_hsmc_nand_controller_legacy_init(nc); |
| 2286 | else |
| 2287 | ret = atmel_hsmc_nand_controller_init(nc); |
| 2288 | |
| 2289 | if (ret) |
| 2290 | return ret; |
| 2291 | |
| 2292 | /* Make sure all irqs are masked before registering our IRQ handler. */ |
| 2293 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_IDR, 0xffffffff); |
| 2294 | ret = devm_request_irq(dev, nc->irq, atmel_nfc_interrupt, |
| 2295 | IRQF_SHARED, "nfc", nc); |
| 2296 | if (ret) { |
| 2297 | dev_err(dev, |
| 2298 | "Could not get register NFC interrupt handler (err = %d)\n", |
| 2299 | ret); |
| 2300 | goto err; |
| 2301 | } |
| 2302 | |
| 2303 | /* Initial NFC configuration. */ |
| 2304 | regmap_write(nc->base.smc, ATMEL_HSMC_NFC_CFG, |
| 2305 | ATMEL_HSMC_NFC_CFG_DTO_MAX); |
| 2306 | |
| 2307 | ret = atmel_nand_controller_add_nands(&nc->base); |
| 2308 | if (ret) |
| 2309 | goto err; |
| 2310 | |
| 2311 | return 0; |
| 2312 | |
| 2313 | err: |
| 2314 | atmel_hsmc_nand_controller_remove(&nc->base); |
| 2315 | |
| 2316 | return ret; |
| 2317 | } |
| 2318 | |
| 2319 | static const struct atmel_nand_controller_ops atmel_hsmc_nc_ops = { |
| 2320 | .probe = atmel_hsmc_nand_controller_probe, |
| 2321 | .remove = atmel_hsmc_nand_controller_remove, |
| 2322 | .ecc_init = atmel_hsmc_nand_ecc_init, |
| 2323 | .nand_init = atmel_hsmc_nand_init, |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 2324 | .setup_data_interface = atmel_hsmc_nand_setup_data_interface, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2325 | }; |
| 2326 | |
| 2327 | static const struct atmel_nand_controller_caps atmel_sama5_nc_caps = { |
| 2328 | .has_dma = true, |
| 2329 | .ale_offs = BIT(21), |
| 2330 | .cle_offs = BIT(22), |
| 2331 | .ops = &atmel_hsmc_nc_ops, |
| 2332 | }; |
| 2333 | |
| 2334 | /* Only used to parse old bindings. */ |
| 2335 | static const struct atmel_nand_controller_caps atmel_sama5_nand_caps = { |
| 2336 | .has_dma = true, |
| 2337 | .ale_offs = BIT(21), |
| 2338 | .cle_offs = BIT(22), |
| 2339 | .ops = &atmel_hsmc_nc_ops, |
| 2340 | .legacy_of_bindings = true, |
| 2341 | }; |
| 2342 | |
| 2343 | static int atmel_smc_nand_controller_probe(struct platform_device *pdev, |
| 2344 | const struct atmel_nand_controller_caps *caps) |
| 2345 | { |
| 2346 | struct device *dev = &pdev->dev; |
| 2347 | struct atmel_smc_nand_controller *nc; |
| 2348 | int ret; |
| 2349 | |
| 2350 | nc = devm_kzalloc(dev, sizeof(*nc), GFP_KERNEL); |
| 2351 | if (!nc) |
| 2352 | return -ENOMEM; |
| 2353 | |
| 2354 | ret = atmel_nand_controller_init(&nc->base, pdev, caps); |
| 2355 | if (ret) |
| 2356 | return ret; |
| 2357 | |
| 2358 | ret = atmel_smc_nand_controller_init(nc); |
| 2359 | if (ret) |
| 2360 | return ret; |
| 2361 | |
| 2362 | return atmel_nand_controller_add_nands(&nc->base); |
| 2363 | } |
| 2364 | |
| 2365 | static int |
| 2366 | atmel_smc_nand_controller_remove(struct atmel_nand_controller *nc) |
| 2367 | { |
| 2368 | int ret; |
| 2369 | |
| 2370 | ret = atmel_nand_controller_remove_nands(nc); |
| 2371 | if (ret) |
| 2372 | return ret; |
| 2373 | |
| 2374 | atmel_nand_controller_cleanup(nc); |
| 2375 | |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 2379 | /* |
| 2380 | * The SMC reg layout of at91rm9200 is completely different which prevents us |
| 2381 | * from re-using atmel_smc_nand_setup_data_interface() for the |
| 2382 | * ->setup_data_interface() hook. |
| 2383 | * At this point, there's no support for the at91rm9200 SMC IP, so we leave |
| 2384 | * ->setup_data_interface() unassigned. |
| 2385 | */ |
| 2386 | static const struct atmel_nand_controller_ops at91rm9200_nc_ops = { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2387 | .probe = atmel_smc_nand_controller_probe, |
| 2388 | .remove = atmel_smc_nand_controller_remove, |
| 2389 | .ecc_init = atmel_nand_ecc_init, |
| 2390 | .nand_init = atmel_smc_nand_init, |
| 2391 | }; |
| 2392 | |
| 2393 | static const struct atmel_nand_controller_caps atmel_rm9200_nc_caps = { |
| 2394 | .ale_offs = BIT(21), |
| 2395 | .cle_offs = BIT(22), |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2396 | .ebi_csa_regmap_name = "atmel,matrix", |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 2397 | .ops = &at91rm9200_nc_ops, |
| 2398 | }; |
| 2399 | |
| 2400 | static const struct atmel_nand_controller_ops atmel_smc_nc_ops = { |
| 2401 | .probe = atmel_smc_nand_controller_probe, |
| 2402 | .remove = atmel_smc_nand_controller_remove, |
| 2403 | .ecc_init = atmel_nand_ecc_init, |
| 2404 | .nand_init = atmel_smc_nand_init, |
| 2405 | .setup_data_interface = atmel_smc_nand_setup_data_interface, |
| 2406 | }; |
| 2407 | |
| 2408 | static const struct atmel_nand_controller_caps atmel_sam9260_nc_caps = { |
| 2409 | .ale_offs = BIT(21), |
| 2410 | .cle_offs = BIT(22), |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2411 | .ebi_csa_regmap_name = "atmel,matrix", |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2412 | .ops = &atmel_smc_nc_ops, |
| 2413 | }; |
| 2414 | |
| 2415 | static const struct atmel_nand_controller_caps atmel_sam9261_nc_caps = { |
| 2416 | .ale_offs = BIT(22), |
| 2417 | .cle_offs = BIT(21), |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2418 | .ebi_csa_regmap_name = "atmel,matrix", |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2419 | .ops = &atmel_smc_nc_ops, |
| 2420 | }; |
| 2421 | |
| 2422 | static const struct atmel_nand_controller_caps atmel_sam9g45_nc_caps = { |
| 2423 | .has_dma = true, |
| 2424 | .ale_offs = BIT(21), |
| 2425 | .cle_offs = BIT(22), |
Tudor Ambarus | e2c19c5 | 2019-02-13 08:59:58 +0000 | [diff] [blame] | 2426 | .ebi_csa_regmap_name = "atmel,matrix", |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2427 | .ops = &atmel_smc_nc_ops, |
| 2428 | }; |
| 2429 | |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 2430 | static const struct atmel_nand_controller_caps microchip_sam9x60_nc_caps = { |
| 2431 | .has_dma = true, |
| 2432 | .ale_offs = BIT(21), |
| 2433 | .cle_offs = BIT(22), |
| 2434 | .ebi_csa_regmap_name = "microchip,sfr", |
| 2435 | .ops = &atmel_smc_nc_ops, |
| 2436 | }; |
| 2437 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2438 | /* Only used to parse old bindings. */ |
| 2439 | static const struct atmel_nand_controller_caps atmel_rm9200_nand_caps = { |
| 2440 | .ale_offs = BIT(21), |
| 2441 | .cle_offs = BIT(22), |
| 2442 | .ops = &atmel_smc_nc_ops, |
| 2443 | .legacy_of_bindings = true, |
| 2444 | }; |
| 2445 | |
| 2446 | static const struct atmel_nand_controller_caps atmel_sam9261_nand_caps = { |
| 2447 | .ale_offs = BIT(22), |
| 2448 | .cle_offs = BIT(21), |
| 2449 | .ops = &atmel_smc_nc_ops, |
| 2450 | .legacy_of_bindings = true, |
| 2451 | }; |
| 2452 | |
| 2453 | static const struct atmel_nand_controller_caps atmel_sam9g45_nand_caps = { |
| 2454 | .has_dma = true, |
| 2455 | .ale_offs = BIT(21), |
| 2456 | .cle_offs = BIT(22), |
| 2457 | .ops = &atmel_smc_nc_ops, |
| 2458 | .legacy_of_bindings = true, |
| 2459 | }; |
| 2460 | |
| 2461 | static const struct of_device_id atmel_nand_controller_of_ids[] = { |
| 2462 | { |
| 2463 | .compatible = "atmel,at91rm9200-nand-controller", |
| 2464 | .data = &atmel_rm9200_nc_caps, |
| 2465 | }, |
| 2466 | { |
| 2467 | .compatible = "atmel,at91sam9260-nand-controller", |
Boris Brezillon | f9ce2ed | 2017-03-16 09:35:59 +0100 | [diff] [blame] | 2468 | .data = &atmel_sam9260_nc_caps, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2469 | }, |
| 2470 | { |
| 2471 | .compatible = "atmel,at91sam9261-nand-controller", |
| 2472 | .data = &atmel_sam9261_nc_caps, |
| 2473 | }, |
| 2474 | { |
| 2475 | .compatible = "atmel,at91sam9g45-nand-controller", |
| 2476 | .data = &atmel_sam9g45_nc_caps, |
| 2477 | }, |
| 2478 | { |
| 2479 | .compatible = "atmel,sama5d3-nand-controller", |
| 2480 | .data = &atmel_sama5_nc_caps, |
| 2481 | }, |
Tudor Ambarus | ccf20cc | 2019-02-13 09:00:05 +0000 | [diff] [blame^] | 2482 | { |
| 2483 | .compatible = "microchip,sam9x60-nand-controller", |
| 2484 | .data = µchip_sam9x60_nc_caps, |
| 2485 | }, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2486 | /* Support for old/deprecated bindings: */ |
| 2487 | { |
| 2488 | .compatible = "atmel,at91rm9200-nand", |
| 2489 | .data = &atmel_rm9200_nand_caps, |
| 2490 | }, |
| 2491 | { |
| 2492 | .compatible = "atmel,sama5d4-nand", |
| 2493 | .data = &atmel_rm9200_nand_caps, |
| 2494 | }, |
| 2495 | { |
| 2496 | .compatible = "atmel,sama5d2-nand", |
| 2497 | .data = &atmel_rm9200_nand_caps, |
| 2498 | }, |
| 2499 | { /* sentinel */ }, |
| 2500 | }; |
| 2501 | MODULE_DEVICE_TABLE(of, atmel_nand_controller_of_ids); |
| 2502 | |
| 2503 | static int atmel_nand_controller_probe(struct platform_device *pdev) |
| 2504 | { |
| 2505 | const struct atmel_nand_controller_caps *caps; |
| 2506 | |
| 2507 | if (pdev->id_entry) |
| 2508 | caps = (void *)pdev->id_entry->driver_data; |
| 2509 | else |
| 2510 | caps = of_device_get_match_data(&pdev->dev); |
| 2511 | |
| 2512 | if (!caps) { |
| 2513 | dev_err(&pdev->dev, "Could not retrieve NFC caps\n"); |
| 2514 | return -EINVAL; |
| 2515 | } |
| 2516 | |
| 2517 | if (caps->legacy_of_bindings) { |
Johan Hovold | 5d1e9c2 | 2018-08-27 10:21:49 +0200 | [diff] [blame] | 2518 | struct device_node *nfc_node; |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2519 | u32 ale_offs = 21; |
| 2520 | |
| 2521 | /* |
| 2522 | * If we are parsing legacy DT props and the DT contains a |
| 2523 | * valid NFC node, forward the request to the sama5 logic. |
| 2524 | */ |
Johan Hovold | 5d1e9c2 | 2018-08-27 10:21:49 +0200 | [diff] [blame] | 2525 | nfc_node = of_get_compatible_child(pdev->dev.of_node, |
| 2526 | "atmel,sama5d3-nfc"); |
| 2527 | if (nfc_node) { |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2528 | caps = &atmel_sama5_nand_caps; |
Johan Hovold | 5d1e9c2 | 2018-08-27 10:21:49 +0200 | [diff] [blame] | 2529 | of_node_put(nfc_node); |
| 2530 | } |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2531 | |
| 2532 | /* |
| 2533 | * Even if the compatible says we are dealing with an |
| 2534 | * at91rm9200 controller, the atmel,nand-has-dma specify that |
| 2535 | * this controller supports DMA, which means we are in fact |
| 2536 | * dealing with an at91sam9g45+ controller. |
| 2537 | */ |
| 2538 | if (!caps->has_dma && |
| 2539 | of_property_read_bool(pdev->dev.of_node, |
| 2540 | "atmel,nand-has-dma")) |
| 2541 | caps = &atmel_sam9g45_nand_caps; |
| 2542 | |
| 2543 | /* |
| 2544 | * All SoCs except the at91sam9261 are assigning ALE to A21 and |
| 2545 | * CLE to A22. If atmel,nand-addr-offset != 21 this means we're |
| 2546 | * actually dealing with an at91sam9261 controller. |
| 2547 | */ |
| 2548 | of_property_read_u32(pdev->dev.of_node, |
| 2549 | "atmel,nand-addr-offset", &ale_offs); |
| 2550 | if (ale_offs != 21) |
| 2551 | caps = &atmel_sam9261_nand_caps; |
| 2552 | } |
| 2553 | |
| 2554 | return caps->ops->probe(pdev, caps); |
| 2555 | } |
| 2556 | |
| 2557 | static int atmel_nand_controller_remove(struct platform_device *pdev) |
| 2558 | { |
| 2559 | struct atmel_nand_controller *nc = platform_get_drvdata(pdev); |
| 2560 | |
| 2561 | return nc->caps->ops->remove(nc); |
| 2562 | } |
| 2563 | |
Arnd Bergmann | 05b6c23 | 2017-05-31 10:19:26 +0200 | [diff] [blame] | 2564 | static __maybe_unused int atmel_nand_controller_resume(struct device *dev) |
Boris Brezillon | 6e532af | 2017-03-16 09:36:00 +0100 | [diff] [blame] | 2565 | { |
| 2566 | struct atmel_nand_controller *nc = dev_get_drvdata(dev); |
| 2567 | struct atmel_nand *nand; |
| 2568 | |
Romain Izard | 143b0ab | 2017-09-28 11:46:23 +0200 | [diff] [blame] | 2569 | if (nc->pmecc) |
| 2570 | atmel_pmecc_reset(nc->pmecc); |
| 2571 | |
Boris Brezillon | 6e532af | 2017-03-16 09:36:00 +0100 | [diff] [blame] | 2572 | list_for_each_entry(nand, &nc->chips, node) { |
| 2573 | int i; |
| 2574 | |
| 2575 | for (i = 0; i < nand->numcs; i++) |
| 2576 | nand_reset(&nand->base, i); |
| 2577 | } |
| 2578 | |
| 2579 | return 0; |
| 2580 | } |
| 2581 | |
| 2582 | static SIMPLE_DEV_PM_OPS(atmel_nand_controller_pm_ops, NULL, |
| 2583 | atmel_nand_controller_resume); |
| 2584 | |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2585 | static struct platform_driver atmel_nand_controller_driver = { |
| 2586 | .driver = { |
| 2587 | .name = "atmel-nand-controller", |
| 2588 | .of_match_table = of_match_ptr(atmel_nand_controller_of_ids), |
Boris Brezillon | 1533bfa | 2017-10-05 18:57:24 +0200 | [diff] [blame] | 2589 | .pm = &atmel_nand_controller_pm_ops, |
Boris Brezillon | f88fc12 | 2017-03-16 09:02:40 +0100 | [diff] [blame] | 2590 | }, |
| 2591 | .probe = atmel_nand_controller_probe, |
| 2592 | .remove = atmel_nand_controller_remove, |
| 2593 | }; |
| 2594 | module_platform_driver(atmel_nand_controller_driver); |
| 2595 | |
| 2596 | MODULE_LICENSE("GPL"); |
| 2597 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); |
| 2598 | MODULE_DESCRIPTION("NAND Flash Controller driver for Atmel SoCs"); |
| 2599 | MODULE_ALIAS("platform:atmel-nand-controller"); |