Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ASPEED Static Memory Controller driver |
| 3 | * |
| 4 | * Copyright (c) 2015-2016, IBM Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/bug.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/mutex.h> |
| 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/partitions.h> |
| 19 | #include <linux/mtd/spi-nor.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/sysfs.h> |
| 23 | |
| 24 | #define DEVICE_NAME "aspeed-smc" |
| 25 | |
| 26 | /* |
| 27 | * The driver only support SPI flash |
| 28 | */ |
| 29 | enum aspeed_smc_flash_type { |
| 30 | smc_type_nor = 0, |
| 31 | smc_type_nand = 1, |
| 32 | smc_type_spi = 2, |
| 33 | }; |
| 34 | |
| 35 | struct aspeed_smc_chip; |
| 36 | |
| 37 | struct aspeed_smc_info { |
| 38 | u32 maxsize; /* maximum size of chip window */ |
| 39 | u8 nce; /* number of chip enables */ |
| 40 | bool hastype; /* flash type field exists in config reg */ |
| 41 | u8 we0; /* shift for write enable bit for CE0 */ |
| 42 | u8 ctl0; /* offset in regs of ctl for CE0 */ |
| 43 | |
| 44 | void (*set_4b)(struct aspeed_smc_chip *chip); |
| 45 | }; |
| 46 | |
Cédric Le Goater | e56beeb | 2016-12-21 17:57:18 +0100 | [diff] [blame] | 47 | static void aspeed_smc_chip_set_4b_spi_2400(struct aspeed_smc_chip *chip); |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 48 | static void aspeed_smc_chip_set_4b(struct aspeed_smc_chip *chip); |
| 49 | |
Cédric Le Goater | e56beeb | 2016-12-21 17:57:18 +0100 | [diff] [blame] | 50 | static const struct aspeed_smc_info fmc_2400_info = { |
| 51 | .maxsize = 64 * 1024 * 1024, |
| 52 | .nce = 5, |
| 53 | .hastype = true, |
| 54 | .we0 = 16, |
| 55 | .ctl0 = 0x10, |
| 56 | .set_4b = aspeed_smc_chip_set_4b, |
| 57 | }; |
| 58 | |
| 59 | static const struct aspeed_smc_info spi_2400_info = { |
| 60 | .maxsize = 64 * 1024 * 1024, |
| 61 | .nce = 1, |
| 62 | .hastype = false, |
| 63 | .we0 = 0, |
| 64 | .ctl0 = 0x04, |
| 65 | .set_4b = aspeed_smc_chip_set_4b_spi_2400, |
| 66 | }; |
| 67 | |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 68 | static const struct aspeed_smc_info fmc_2500_info = { |
| 69 | .maxsize = 256 * 1024 * 1024, |
| 70 | .nce = 3, |
| 71 | .hastype = true, |
| 72 | .we0 = 16, |
| 73 | .ctl0 = 0x10, |
| 74 | .set_4b = aspeed_smc_chip_set_4b, |
| 75 | }; |
| 76 | |
| 77 | static const struct aspeed_smc_info spi_2500_info = { |
| 78 | .maxsize = 128 * 1024 * 1024, |
| 79 | .nce = 2, |
| 80 | .hastype = false, |
| 81 | .we0 = 16, |
| 82 | .ctl0 = 0x10, |
| 83 | .set_4b = aspeed_smc_chip_set_4b, |
| 84 | }; |
| 85 | |
| 86 | enum aspeed_smc_ctl_reg_value { |
| 87 | smc_base, /* base value without mode for other commands */ |
| 88 | smc_read, /* command reg for (maybe fast) reads */ |
| 89 | smc_write, /* command reg for writes */ |
| 90 | smc_max, |
| 91 | }; |
| 92 | |
| 93 | struct aspeed_smc_controller; |
| 94 | |
| 95 | struct aspeed_smc_chip { |
| 96 | int cs; |
| 97 | struct aspeed_smc_controller *controller; |
| 98 | void __iomem *ctl; /* control register */ |
| 99 | void __iomem *ahb_base; /* base of chip window */ |
| 100 | u32 ctl_val[smc_max]; /* control settings */ |
| 101 | enum aspeed_smc_flash_type type; /* what type of flash */ |
| 102 | struct spi_nor nor; |
| 103 | }; |
| 104 | |
| 105 | struct aspeed_smc_controller { |
| 106 | struct device *dev; |
| 107 | |
| 108 | struct mutex mutex; /* controller access mutex */ |
| 109 | const struct aspeed_smc_info *info; /* type info of controller */ |
| 110 | void __iomem *regs; /* controller registers */ |
| 111 | void __iomem *ahb_base; /* per-chip windows resource */ |
| 112 | |
| 113 | struct aspeed_smc_chip *chips[0]; /* pointers to attached chips */ |
| 114 | }; |
| 115 | |
| 116 | /* |
| 117 | * SPI Flash Configuration Register (AST2500 SPI) |
| 118 | * or |
| 119 | * Type setting Register (AST2500 FMC). |
| 120 | * CE0 and CE1 can only be of type SPI. CE2 can be of type NOR but the |
| 121 | * driver does not support it. |
| 122 | */ |
| 123 | #define CONFIG_REG 0x0 |
| 124 | #define CONFIG_DISABLE_LEGACY BIT(31) /* 1 */ |
| 125 | |
| 126 | #define CONFIG_CE2_WRITE BIT(18) |
| 127 | #define CONFIG_CE1_WRITE BIT(17) |
| 128 | #define CONFIG_CE0_WRITE BIT(16) |
| 129 | |
| 130 | #define CONFIG_CE2_TYPE BIT(4) /* AST2500 FMC only */ |
| 131 | #define CONFIG_CE1_TYPE BIT(2) /* AST2500 FMC only */ |
| 132 | #define CONFIG_CE0_TYPE BIT(0) /* AST2500 FMC only */ |
| 133 | |
| 134 | /* |
| 135 | * CE Control Register |
| 136 | */ |
| 137 | #define CE_CONTROL_REG 0x4 |
| 138 | |
| 139 | /* |
| 140 | * CEx Control Register |
| 141 | */ |
| 142 | #define CONTROL_AAF_MODE BIT(31) |
| 143 | #define CONTROL_IO_MODE_MASK GENMASK(30, 28) |
| 144 | #define CONTROL_IO_DUAL_DATA BIT(29) |
| 145 | #define CONTROL_IO_DUAL_ADDR_DATA (BIT(29) | BIT(28)) |
| 146 | #define CONTROL_IO_QUAD_DATA BIT(30) |
| 147 | #define CONTROL_IO_QUAD_ADDR_DATA (BIT(30) | BIT(28)) |
| 148 | #define CONTROL_CE_INACTIVE_SHIFT 24 |
| 149 | #define CONTROL_CE_INACTIVE_MASK GENMASK(27, \ |
| 150 | CONTROL_CE_INACTIVE_SHIFT) |
| 151 | /* 0 = 16T ... 15 = 1T T=HCLK */ |
| 152 | #define CONTROL_COMMAND_SHIFT 16 |
| 153 | #define CONTROL_DUMMY_COMMAND_OUT BIT(15) |
| 154 | #define CONTROL_IO_DUMMY_HI BIT(14) |
| 155 | #define CONTROL_IO_DUMMY_HI_SHIFT 14 |
| 156 | #define CONTROL_CLK_DIV4 BIT(13) /* others */ |
Cédric Le Goater | e56beeb | 2016-12-21 17:57:18 +0100 | [diff] [blame] | 157 | #define CONTROL_IO_ADDRESS_4B BIT(13) /* AST2400 SPI */ |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 158 | #define CONTROL_RW_MERGE BIT(12) |
| 159 | #define CONTROL_IO_DUMMY_LO_SHIFT 6 |
| 160 | #define CONTROL_IO_DUMMY_LO GENMASK(7, \ |
| 161 | CONTROL_IO_DUMMY_LO_SHIFT) |
| 162 | #define CONTROL_IO_DUMMY_MASK (CONTROL_IO_DUMMY_HI | \ |
| 163 | CONTROL_IO_DUMMY_LO) |
| 164 | #define CONTROL_IO_DUMMY_SET(dummy) \ |
| 165 | (((((dummy) >> 2) & 0x1) << CONTROL_IO_DUMMY_HI_SHIFT) | \ |
| 166 | (((dummy) & 0x3) << CONTROL_IO_DUMMY_LO_SHIFT)) |
| 167 | |
| 168 | #define CONTROL_CLOCK_FREQ_SEL_SHIFT 8 |
| 169 | #define CONTROL_CLOCK_FREQ_SEL_MASK GENMASK(11, \ |
| 170 | CONTROL_CLOCK_FREQ_SEL_SHIFT) |
| 171 | #define CONTROL_LSB_FIRST BIT(5) |
| 172 | #define CONTROL_CLOCK_MODE_3 BIT(4) |
| 173 | #define CONTROL_IN_DUAL_DATA BIT(3) |
| 174 | #define CONTROL_CE_STOP_ACTIVE_CONTROL BIT(2) |
| 175 | #define CONTROL_COMMAND_MODE_MASK GENMASK(1, 0) |
| 176 | #define CONTROL_COMMAND_MODE_NORMAL 0 |
| 177 | #define CONTROL_COMMAND_MODE_FREAD 1 |
| 178 | #define CONTROL_COMMAND_MODE_WRITE 2 |
| 179 | #define CONTROL_COMMAND_MODE_USER 3 |
| 180 | |
| 181 | #define CONTROL_KEEP_MASK \ |
| 182 | (CONTROL_AAF_MODE | CONTROL_CE_INACTIVE_MASK | CONTROL_CLK_DIV4 | \ |
| 183 | CONTROL_IO_DUMMY_MASK | CONTROL_CLOCK_FREQ_SEL_MASK | \ |
| 184 | CONTROL_LSB_FIRST | CONTROL_CLOCK_MODE_3) |
| 185 | |
| 186 | /* |
| 187 | * The Segment Register uses a 8MB unit to encode the start address |
| 188 | * and the end address of the mapping window of a flash SPI slave : |
| 189 | * |
| 190 | * | byte 1 | byte 2 | byte 3 | byte 4 | |
| 191 | * +--------+--------+--------+--------+ |
| 192 | * | end | start | 0 | 0 | |
| 193 | */ |
| 194 | #define SEGMENT_ADDR_REG0 0x30 |
| 195 | #define SEGMENT_ADDR_START(_r) ((((_r) >> 16) & 0xFF) << 23) |
| 196 | #define SEGMENT_ADDR_END(_r) ((((_r) >> 24) & 0xFF) << 23) |
| 197 | |
| 198 | /* |
| 199 | * In user mode all data bytes read or written to the chip decode address |
| 200 | * range are transferred to or from the SPI bus. The range is treated as a |
| 201 | * fifo of arbitratry 1, 2, or 4 byte width but each write has to be aligned |
| 202 | * to its size. The address within the multiple 8kB range is ignored when |
| 203 | * sending bytes to the SPI bus. |
| 204 | * |
| 205 | * On the arm architecture, as of Linux version 4.3, memcpy_fromio and |
| 206 | * memcpy_toio on little endian targets use the optimized memcpy routines |
| 207 | * that were designed for well behavied memory storage. These routines |
| 208 | * have a stutter if the source and destination are not both word aligned, |
| 209 | * once with a duplicate access to the source after aligning to the |
| 210 | * destination to a word boundary, and again with a duplicate access to |
| 211 | * the source when the final byte count is not word aligned. |
| 212 | * |
| 213 | * When writing or reading the fifo this stutter discards data or sends |
| 214 | * too much data to the fifo and can not be used by this driver. |
| 215 | * |
| 216 | * While the low level io string routines that implement the insl family do |
| 217 | * the desired accesses and memory increments, the cross architecture io |
| 218 | * macros make them essentially impossible to use on a memory mapped address |
| 219 | * instead of a a token from the call to iomap of an io port. |
| 220 | * |
| 221 | * These fifo routines use readl and friends to a constant io port and update |
| 222 | * the memory buffer pointer and count via explicit code. The final updates |
| 223 | * to len are optimistically suppressed. |
| 224 | */ |
Cédric Le Goater | f40a272 | 2017-01-19 10:13:18 +0100 | [diff] [blame] | 225 | static int aspeed_smc_read_from_ahb(void *buf, void __iomem *src, size_t len) |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 226 | { |
| 227 | size_t offset = 0; |
| 228 | |
| 229 | if (IS_ALIGNED((uintptr_t)src, sizeof(uintptr_t)) && |
| 230 | IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) { |
| 231 | ioread32_rep(src, buf, len >> 2); |
| 232 | offset = len & ~0x3; |
| 233 | len -= offset; |
| 234 | } |
| 235 | ioread8_rep(src, (u8 *)buf + offset, len); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int aspeed_smc_write_to_ahb(void __iomem *dst, const void *buf, |
| 240 | size_t len) |
| 241 | { |
| 242 | size_t offset = 0; |
| 243 | |
| 244 | if (IS_ALIGNED((uintptr_t)dst, sizeof(uintptr_t)) && |
| 245 | IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) { |
| 246 | iowrite32_rep(dst, buf, len >> 2); |
| 247 | offset = len & ~0x3; |
| 248 | len -= offset; |
| 249 | } |
| 250 | iowrite8_rep(dst, (const u8 *)buf + offset, len); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static inline u32 aspeed_smc_chip_write_bit(struct aspeed_smc_chip *chip) |
| 255 | { |
| 256 | return BIT(chip->controller->info->we0 + chip->cs); |
| 257 | } |
| 258 | |
| 259 | static void aspeed_smc_chip_check_config(struct aspeed_smc_chip *chip) |
| 260 | { |
| 261 | struct aspeed_smc_controller *controller = chip->controller; |
| 262 | u32 reg; |
| 263 | |
| 264 | reg = readl(controller->regs + CONFIG_REG); |
| 265 | |
| 266 | if (reg & aspeed_smc_chip_write_bit(chip)) |
| 267 | return; |
| 268 | |
| 269 | dev_dbg(controller->dev, "config write is not set ! @%p: 0x%08x\n", |
| 270 | controller->regs + CONFIG_REG, reg); |
| 271 | reg |= aspeed_smc_chip_write_bit(chip); |
| 272 | writel(reg, controller->regs + CONFIG_REG); |
| 273 | } |
| 274 | |
| 275 | static void aspeed_smc_start_user(struct spi_nor *nor) |
| 276 | { |
| 277 | struct aspeed_smc_chip *chip = nor->priv; |
| 278 | u32 ctl = chip->ctl_val[smc_base]; |
| 279 | |
| 280 | /* |
| 281 | * When the chip is controlled in user mode, we need write |
| 282 | * access to send the opcodes to it. So check the config. |
| 283 | */ |
| 284 | aspeed_smc_chip_check_config(chip); |
| 285 | |
| 286 | ctl |= CONTROL_COMMAND_MODE_USER | |
| 287 | CONTROL_CE_STOP_ACTIVE_CONTROL; |
| 288 | writel(ctl, chip->ctl); |
| 289 | |
| 290 | ctl &= ~CONTROL_CE_STOP_ACTIVE_CONTROL; |
| 291 | writel(ctl, chip->ctl); |
| 292 | } |
| 293 | |
| 294 | static void aspeed_smc_stop_user(struct spi_nor *nor) |
| 295 | { |
| 296 | struct aspeed_smc_chip *chip = nor->priv; |
| 297 | |
| 298 | u32 ctl = chip->ctl_val[smc_read]; |
| 299 | u32 ctl2 = ctl | CONTROL_COMMAND_MODE_USER | |
| 300 | CONTROL_CE_STOP_ACTIVE_CONTROL; |
| 301 | |
| 302 | writel(ctl2, chip->ctl); /* stop user CE control */ |
| 303 | writel(ctl, chip->ctl); /* default to fread or read mode */ |
| 304 | } |
| 305 | |
| 306 | static int aspeed_smc_prep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 307 | { |
| 308 | struct aspeed_smc_chip *chip = nor->priv; |
| 309 | |
| 310 | mutex_lock(&chip->controller->mutex); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | static void aspeed_smc_unprep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 315 | { |
| 316 | struct aspeed_smc_chip *chip = nor->priv; |
| 317 | |
| 318 | mutex_unlock(&chip->controller->mutex); |
| 319 | } |
| 320 | |
| 321 | static int aspeed_smc_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len) |
| 322 | { |
| 323 | struct aspeed_smc_chip *chip = nor->priv; |
| 324 | |
| 325 | aspeed_smc_start_user(nor); |
| 326 | aspeed_smc_write_to_ahb(chip->ahb_base, &opcode, 1); |
| 327 | aspeed_smc_read_from_ahb(buf, chip->ahb_base, len); |
| 328 | aspeed_smc_stop_user(nor); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int aspeed_smc_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, |
| 333 | int len) |
| 334 | { |
| 335 | struct aspeed_smc_chip *chip = nor->priv; |
| 336 | |
| 337 | aspeed_smc_start_user(nor); |
| 338 | aspeed_smc_write_to_ahb(chip->ahb_base, &opcode, 1); |
| 339 | aspeed_smc_write_to_ahb(chip->ahb_base, buf, len); |
| 340 | aspeed_smc_stop_user(nor); |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static void aspeed_smc_send_cmd_addr(struct spi_nor *nor, u8 cmd, u32 addr) |
| 345 | { |
| 346 | struct aspeed_smc_chip *chip = nor->priv; |
| 347 | __be32 temp; |
| 348 | u32 cmdaddr; |
| 349 | |
| 350 | switch (nor->addr_width) { |
| 351 | default: |
| 352 | WARN_ONCE(1, "Unexpected address width %u, defaulting to 3\n", |
| 353 | nor->addr_width); |
| 354 | /* FALLTHROUGH */ |
| 355 | case 3: |
| 356 | cmdaddr = addr & 0xFFFFFF; |
| 357 | cmdaddr |= cmd << 24; |
| 358 | |
| 359 | temp = cpu_to_be32(cmdaddr); |
| 360 | aspeed_smc_write_to_ahb(chip->ahb_base, &temp, 4); |
| 361 | break; |
| 362 | case 4: |
| 363 | temp = cpu_to_be32(addr); |
| 364 | aspeed_smc_write_to_ahb(chip->ahb_base, &cmd, 1); |
| 365 | aspeed_smc_write_to_ahb(chip->ahb_base, &temp, 4); |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static ssize_t aspeed_smc_read_user(struct spi_nor *nor, loff_t from, |
| 371 | size_t len, u_char *read_buf) |
| 372 | { |
| 373 | struct aspeed_smc_chip *chip = nor->priv; |
| 374 | int i; |
| 375 | u8 dummy = 0xFF; |
| 376 | |
| 377 | aspeed_smc_start_user(nor); |
| 378 | aspeed_smc_send_cmd_addr(nor, nor->read_opcode, from); |
| 379 | for (i = 0; i < chip->nor.read_dummy / 8; i++) |
| 380 | aspeed_smc_write_to_ahb(chip->ahb_base, &dummy, sizeof(dummy)); |
| 381 | |
| 382 | aspeed_smc_read_from_ahb(read_buf, chip->ahb_base, len); |
| 383 | aspeed_smc_stop_user(nor); |
| 384 | return len; |
| 385 | } |
| 386 | |
| 387 | static ssize_t aspeed_smc_write_user(struct spi_nor *nor, loff_t to, |
| 388 | size_t len, const u_char *write_buf) |
| 389 | { |
| 390 | struct aspeed_smc_chip *chip = nor->priv; |
| 391 | |
| 392 | aspeed_smc_start_user(nor); |
| 393 | aspeed_smc_send_cmd_addr(nor, nor->program_opcode, to); |
| 394 | aspeed_smc_write_to_ahb(chip->ahb_base, write_buf, len); |
| 395 | aspeed_smc_stop_user(nor); |
| 396 | return len; |
| 397 | } |
| 398 | |
| 399 | static int aspeed_smc_unregister(struct aspeed_smc_controller *controller) |
| 400 | { |
| 401 | struct aspeed_smc_chip *chip; |
| 402 | int n; |
| 403 | |
| 404 | for (n = 0; n < controller->info->nce; n++) { |
| 405 | chip = controller->chips[n]; |
| 406 | if (chip) |
| 407 | mtd_device_unregister(&chip->nor.mtd); |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int aspeed_smc_remove(struct platform_device *dev) |
| 414 | { |
| 415 | return aspeed_smc_unregister(platform_get_drvdata(dev)); |
| 416 | } |
| 417 | |
| 418 | static const struct of_device_id aspeed_smc_matches[] = { |
Cédric Le Goater | e56beeb | 2016-12-21 17:57:18 +0100 | [diff] [blame] | 419 | { .compatible = "aspeed,ast2400-fmc", .data = &fmc_2400_info }, |
| 420 | { .compatible = "aspeed,ast2400-spi", .data = &spi_2400_info }, |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 421 | { .compatible = "aspeed,ast2500-fmc", .data = &fmc_2500_info }, |
| 422 | { .compatible = "aspeed,ast2500-spi", .data = &spi_2500_info }, |
| 423 | { } |
| 424 | }; |
| 425 | MODULE_DEVICE_TABLE(of, aspeed_smc_matches); |
| 426 | |
| 427 | /* |
| 428 | * Each chip has a mapping window defined by a segment address |
| 429 | * register defining a start and an end address on the AHB bus. These |
| 430 | * addresses can be configured to fit the chip size and offer a |
| 431 | * contiguous memory region across chips. For the moment, we only |
| 432 | * check that each chip segment is valid. |
| 433 | */ |
| 434 | static void __iomem *aspeed_smc_chip_base(struct aspeed_smc_chip *chip, |
| 435 | struct resource *res) |
| 436 | { |
| 437 | struct aspeed_smc_controller *controller = chip->controller; |
| 438 | u32 offset = 0; |
| 439 | u32 reg; |
| 440 | |
| 441 | if (controller->info->nce > 1) { |
| 442 | reg = readl(controller->regs + SEGMENT_ADDR_REG0 + |
| 443 | chip->cs * 4); |
| 444 | |
| 445 | if (SEGMENT_ADDR_START(reg) >= SEGMENT_ADDR_END(reg)) |
| 446 | return NULL; |
| 447 | |
| 448 | offset = SEGMENT_ADDR_START(reg) - res->start; |
| 449 | } |
| 450 | |
| 451 | return controller->ahb_base + offset; |
| 452 | } |
| 453 | |
| 454 | static void aspeed_smc_chip_enable_write(struct aspeed_smc_chip *chip) |
| 455 | { |
| 456 | struct aspeed_smc_controller *controller = chip->controller; |
| 457 | u32 reg; |
| 458 | |
| 459 | reg = readl(controller->regs + CONFIG_REG); |
| 460 | |
| 461 | reg |= aspeed_smc_chip_write_bit(chip); |
| 462 | writel(reg, controller->regs + CONFIG_REG); |
| 463 | } |
| 464 | |
| 465 | static void aspeed_smc_chip_set_type(struct aspeed_smc_chip *chip, int type) |
| 466 | { |
| 467 | struct aspeed_smc_controller *controller = chip->controller; |
| 468 | u32 reg; |
| 469 | |
| 470 | chip->type = type; |
| 471 | |
| 472 | reg = readl(controller->regs + CONFIG_REG); |
| 473 | reg &= ~(3 << (chip->cs * 2)); |
| 474 | reg |= chip->type << (chip->cs * 2); |
| 475 | writel(reg, controller->regs + CONFIG_REG); |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * The AST2500 FMC flash controller should be strapped by hardware, or |
| 480 | * autodetected, but the AST2500 SPI flash needs to be set. |
| 481 | */ |
| 482 | static void aspeed_smc_chip_set_4b(struct aspeed_smc_chip *chip) |
| 483 | { |
| 484 | struct aspeed_smc_controller *controller = chip->controller; |
| 485 | u32 reg; |
| 486 | |
| 487 | if (chip->controller->info == &spi_2500_info) { |
| 488 | reg = readl(controller->regs + CE_CONTROL_REG); |
| 489 | reg |= 1 << chip->cs; |
| 490 | writel(reg, controller->regs + CE_CONTROL_REG); |
| 491 | } |
| 492 | } |
| 493 | |
Cédric Le Goater | e56beeb | 2016-12-21 17:57:18 +0100 | [diff] [blame] | 494 | /* |
| 495 | * The AST2400 SPI flash controller does not have a CE Control |
| 496 | * register. It uses the CE0 control register to set 4Byte mode at the |
| 497 | * controller level. |
| 498 | */ |
| 499 | static void aspeed_smc_chip_set_4b_spi_2400(struct aspeed_smc_chip *chip) |
| 500 | { |
| 501 | chip->ctl_val[smc_base] |= CONTROL_IO_ADDRESS_4B; |
| 502 | chip->ctl_val[smc_read] |= CONTROL_IO_ADDRESS_4B; |
| 503 | } |
| 504 | |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 505 | static int aspeed_smc_chip_setup_init(struct aspeed_smc_chip *chip, |
| 506 | struct resource *res) |
| 507 | { |
| 508 | struct aspeed_smc_controller *controller = chip->controller; |
| 509 | const struct aspeed_smc_info *info = controller->info; |
| 510 | u32 reg, base_reg; |
| 511 | |
| 512 | /* |
| 513 | * Always turn on the write enable bit to allow opcodes to be |
| 514 | * sent in user mode. |
| 515 | */ |
| 516 | aspeed_smc_chip_enable_write(chip); |
| 517 | |
| 518 | /* The driver only supports SPI type flash */ |
| 519 | if (info->hastype) |
| 520 | aspeed_smc_chip_set_type(chip, smc_type_spi); |
| 521 | |
| 522 | /* |
| 523 | * Configure chip base address in memory |
| 524 | */ |
| 525 | chip->ahb_base = aspeed_smc_chip_base(chip, res); |
| 526 | if (!chip->ahb_base) { |
| 527 | dev_warn(chip->nor.dev, "CE segment window closed.\n"); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Get value of the inherited control register. U-Boot usually |
| 533 | * does some timing calibration on the FMC chip, so it's good |
| 534 | * to keep them. In the future, we should handle calibration |
| 535 | * from Linux. |
| 536 | */ |
| 537 | reg = readl(chip->ctl); |
| 538 | dev_dbg(controller->dev, "control register: %08x\n", reg); |
| 539 | |
| 540 | base_reg = reg & CONTROL_KEEP_MASK; |
| 541 | if (base_reg != reg) { |
| 542 | dev_dbg(controller->dev, |
| 543 | "control register changed to: %08x\n", |
| 544 | base_reg); |
| 545 | } |
| 546 | chip->ctl_val[smc_base] = base_reg; |
| 547 | |
| 548 | /* |
| 549 | * Retain the prior value of the control register as the |
| 550 | * default if it was normal access mode. Otherwise start with |
| 551 | * the sanitized base value set to read mode. |
| 552 | */ |
| 553 | if ((reg & CONTROL_COMMAND_MODE_MASK) == |
| 554 | CONTROL_COMMAND_MODE_NORMAL) |
| 555 | chip->ctl_val[smc_read] = reg; |
| 556 | else |
| 557 | chip->ctl_val[smc_read] = chip->ctl_val[smc_base] | |
| 558 | CONTROL_COMMAND_MODE_NORMAL; |
| 559 | |
| 560 | dev_dbg(controller->dev, "default control register: %08x\n", |
| 561 | chip->ctl_val[smc_read]); |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int aspeed_smc_chip_setup_finish(struct aspeed_smc_chip *chip) |
| 566 | { |
| 567 | struct aspeed_smc_controller *controller = chip->controller; |
| 568 | const struct aspeed_smc_info *info = controller->info; |
| 569 | u32 cmd; |
| 570 | |
| 571 | if (chip->nor.addr_width == 4 && info->set_4b) |
| 572 | info->set_4b(chip); |
| 573 | |
| 574 | /* |
| 575 | * base mode has not been optimized yet. use it for writes. |
| 576 | */ |
| 577 | chip->ctl_val[smc_write] = chip->ctl_val[smc_base] | |
| 578 | chip->nor.program_opcode << CONTROL_COMMAND_SHIFT | |
| 579 | CONTROL_COMMAND_MODE_WRITE; |
| 580 | |
| 581 | dev_dbg(controller->dev, "write control register: %08x\n", |
| 582 | chip->ctl_val[smc_write]); |
| 583 | |
| 584 | /* |
| 585 | * TODO: Adjust clocks if fast read is supported and interpret |
| 586 | * SPI-NOR flags to adjust controller settings. |
| 587 | */ |
| 588 | switch (chip->nor.flash_read) { |
| 589 | case SPI_NOR_NORMAL: |
| 590 | cmd = CONTROL_COMMAND_MODE_NORMAL; |
| 591 | break; |
| 592 | case SPI_NOR_FAST: |
| 593 | cmd = CONTROL_COMMAND_MODE_FREAD; |
| 594 | break; |
| 595 | default: |
| 596 | dev_err(chip->nor.dev, "unsupported SPI read mode\n"); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
| 600 | chip->ctl_val[smc_read] |= cmd | |
| 601 | CONTROL_IO_DUMMY_SET(chip->nor.read_dummy / 8); |
| 602 | |
| 603 | dev_dbg(controller->dev, "base control register: %08x\n", |
| 604 | chip->ctl_val[smc_read]); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int aspeed_smc_setup_flash(struct aspeed_smc_controller *controller, |
| 609 | struct device_node *np, struct resource *r) |
| 610 | { |
| 611 | const struct aspeed_smc_info *info = controller->info; |
| 612 | struct device *dev = controller->dev; |
| 613 | struct device_node *child; |
| 614 | unsigned int cs; |
| 615 | int ret = -ENODEV; |
| 616 | |
| 617 | for_each_available_child_of_node(np, child) { |
| 618 | struct aspeed_smc_chip *chip; |
| 619 | struct spi_nor *nor; |
| 620 | struct mtd_info *mtd; |
| 621 | |
| 622 | /* This driver does not support NAND or NOR flash devices. */ |
| 623 | if (!of_device_is_compatible(child, "jedec,spi-nor")) |
| 624 | continue; |
| 625 | |
| 626 | ret = of_property_read_u32(child, "reg", &cs); |
| 627 | if (ret) { |
| 628 | dev_err(dev, "Couldn't not read chip select.\n"); |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | if (cs >= info->nce) { |
| 633 | dev_err(dev, "Chip select %d out of range.\n", |
| 634 | cs); |
| 635 | ret = -ERANGE; |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | if (controller->chips[cs]) { |
| 640 | dev_err(dev, "Chip select %d already in use by %s\n", |
| 641 | cs, dev_name(controller->chips[cs]->nor.dev)); |
| 642 | ret = -EBUSY; |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | chip = devm_kzalloc(controller->dev, sizeof(*chip), GFP_KERNEL); |
| 647 | if (!chip) { |
| 648 | ret = -ENOMEM; |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | chip->controller = controller; |
| 653 | chip->ctl = controller->regs + info->ctl0 + cs * 4; |
| 654 | chip->cs = cs; |
| 655 | |
| 656 | nor = &chip->nor; |
| 657 | mtd = &nor->mtd; |
| 658 | |
| 659 | nor->dev = dev; |
| 660 | nor->priv = chip; |
| 661 | spi_nor_set_flash_node(nor, child); |
| 662 | nor->read = aspeed_smc_read_user; |
| 663 | nor->write = aspeed_smc_write_user; |
| 664 | nor->read_reg = aspeed_smc_read_reg; |
| 665 | nor->write_reg = aspeed_smc_write_reg; |
| 666 | nor->prepare = aspeed_smc_prep; |
| 667 | nor->unprepare = aspeed_smc_unprep; |
| 668 | |
| 669 | ret = aspeed_smc_chip_setup_init(chip, r); |
| 670 | if (ret) |
| 671 | break; |
| 672 | |
| 673 | /* |
| 674 | * TODO: Add support for SPI_NOR_QUAD and SPI_NOR_DUAL |
| 675 | * attach when board support is present as determined |
| 676 | * by of property. |
| 677 | */ |
| 678 | ret = spi_nor_scan(nor, NULL, SPI_NOR_NORMAL); |
| 679 | if (ret) |
| 680 | break; |
| 681 | |
| 682 | ret = aspeed_smc_chip_setup_finish(chip); |
| 683 | if (ret) |
| 684 | break; |
| 685 | |
| 686 | ret = mtd_device_register(mtd, NULL, 0); |
| 687 | if (ret) |
| 688 | break; |
| 689 | |
| 690 | controller->chips[cs] = chip; |
| 691 | } |
| 692 | |
| 693 | if (ret) |
| 694 | aspeed_smc_unregister(controller); |
| 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | static int aspeed_smc_probe(struct platform_device *pdev) |
| 700 | { |
| 701 | struct device_node *np = pdev->dev.of_node; |
| 702 | struct device *dev = &pdev->dev; |
| 703 | struct aspeed_smc_controller *controller; |
| 704 | const struct of_device_id *match; |
| 705 | const struct aspeed_smc_info *info; |
| 706 | struct resource *res; |
| 707 | int ret; |
| 708 | |
| 709 | match = of_match_device(aspeed_smc_matches, &pdev->dev); |
| 710 | if (!match || !match->data) |
| 711 | return -ENODEV; |
| 712 | info = match->data; |
| 713 | |
| 714 | controller = devm_kzalloc(&pdev->dev, sizeof(*controller) + |
| 715 | info->nce * sizeof(controller->chips[0]), GFP_KERNEL); |
| 716 | if (!controller) |
| 717 | return -ENOMEM; |
| 718 | controller->info = info; |
| 719 | controller->dev = dev; |
| 720 | |
| 721 | mutex_init(&controller->mutex); |
| 722 | platform_set_drvdata(pdev, controller); |
| 723 | |
| 724 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 725 | controller->regs = devm_ioremap_resource(dev, res); |
Wei Yongjun | d91f6ce | 2017-02-08 16:23:10 +0000 | [diff] [blame] | 726 | if (IS_ERR(controller->regs)) |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 727 | return PTR_ERR(controller->regs); |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 728 | |
| 729 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 730 | controller->ahb_base = devm_ioremap_resource(dev, res); |
Wei Yongjun | d91f6ce | 2017-02-08 16:23:10 +0000 | [diff] [blame] | 731 | if (IS_ERR(controller->ahb_base)) |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 732 | return PTR_ERR(controller->ahb_base); |
Cédric Le Goater | ceb720c | 2016-12-21 17:57:17 +0100 | [diff] [blame] | 733 | |
| 734 | ret = aspeed_smc_setup_flash(controller, np, res); |
| 735 | if (ret) |
| 736 | dev_err(dev, "Aspeed SMC probe failed %d\n", ret); |
| 737 | |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | static struct platform_driver aspeed_smc_driver = { |
| 742 | .probe = aspeed_smc_probe, |
| 743 | .remove = aspeed_smc_remove, |
| 744 | .driver = { |
| 745 | .name = DEVICE_NAME, |
| 746 | .of_match_table = aspeed_smc_matches, |
| 747 | } |
| 748 | }; |
| 749 | |
| 750 | module_platform_driver(aspeed_smc_driver); |
| 751 | |
| 752 | MODULE_DESCRIPTION("ASPEED Static Memory Controller Driver"); |
| 753 | MODULE_AUTHOR("Cedric Le Goater <clg@kaod.org>"); |
| 754 | MODULE_LICENSE("GPL v2"); |