Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller |
| 3 | * |
| 4 | * Author: Angus Clark <angus.clark@st.com> |
| 5 | * |
| 6 | * Copyright (C) 2010-2014 STicroelectronics Limited |
| 7 | * |
| 8 | * JEDEC probe based on drivers/mtd/devices/m25p80.c |
| 9 | * |
| 10 | * This code is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/of.h> |
| 23 | |
Lee Jones | bc09fb5 | 2014-03-20 09:20:34 +0000 | [diff] [blame] | 24 | /* |
| 25 | * FSM SPI Controller Registers |
| 26 | */ |
| 27 | #define SPI_CLOCKDIV 0x0010 |
| 28 | #define SPI_MODESELECT 0x0018 |
| 29 | #define SPI_CONFIGDATA 0x0020 |
| 30 | #define SPI_STA_MODE_CHANGE 0x0028 |
| 31 | #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100 |
| 32 | #define SPI_FAST_SEQ_ADD1 0x0104 |
| 33 | #define SPI_FAST_SEQ_ADD2 0x0108 |
| 34 | #define SPI_FAST_SEQ_ADD_CFG 0x010c |
| 35 | #define SPI_FAST_SEQ_OPC1 0x0110 |
| 36 | #define SPI_FAST_SEQ_OPC2 0x0114 |
| 37 | #define SPI_FAST_SEQ_OPC3 0x0118 |
| 38 | #define SPI_FAST_SEQ_OPC4 0x011c |
| 39 | #define SPI_FAST_SEQ_OPC5 0x0120 |
| 40 | #define SPI_MODE_BITS 0x0124 |
| 41 | #define SPI_DUMMY_BITS 0x0128 |
| 42 | #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c |
| 43 | #define SPI_FAST_SEQ_1 0x0130 |
| 44 | #define SPI_FAST_SEQ_2 0x0134 |
| 45 | #define SPI_FAST_SEQ_3 0x0138 |
| 46 | #define SPI_FAST_SEQ_4 0x013c |
| 47 | #define SPI_FAST_SEQ_CFG 0x0140 |
| 48 | #define SPI_FAST_SEQ_STA 0x0144 |
| 49 | #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148 |
| 50 | #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c |
| 51 | #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150 |
| 52 | #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154 |
| 53 | #define SPI_PROGRAM_ERASE_TIME 0x0158 |
| 54 | #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c |
| 55 | #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160 |
| 56 | #define SPI_STATUS_WR_TIME_REG 0x0164 |
| 57 | #define SPI_FAST_SEQ_DATA_REG 0x0300 |
| 58 | |
| 59 | /* |
| 60 | * Register: SPI_MODESELECT |
| 61 | */ |
| 62 | #define SPI_MODESELECT_CONTIG 0x01 |
| 63 | #define SPI_MODESELECT_FASTREAD 0x02 |
| 64 | #define SPI_MODESELECT_DUALIO 0x04 |
| 65 | #define SPI_MODESELECT_FSM 0x08 |
| 66 | #define SPI_MODESELECT_QUADBOOT 0x10 |
| 67 | |
| 68 | /* |
| 69 | * Register: SPI_CONFIGDATA |
| 70 | */ |
| 71 | #define SPI_CFG_DEVICE_ST 0x1 |
| 72 | #define SPI_CFG_DEVICE_ATMEL 0x4 |
| 73 | #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4) |
| 74 | #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16) |
| 75 | #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24) |
| 76 | |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 77 | #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA) |
| 78 | #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0) |
| 79 | #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00) |
| 80 | |
Lee Jones | bc09fb5 | 2014-03-20 09:20:34 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Register: SPI_FAST_SEQ_TRANSFER_SIZE |
| 83 | */ |
| 84 | #define TRANSFER_SIZE(x) ((x) * 8) |
| 85 | |
| 86 | /* |
| 87 | * Register: SPI_FAST_SEQ_ADD_CFG |
| 88 | */ |
| 89 | #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0) |
| 90 | #define ADR_CFG_PADS_1_ADD1 (0x0 << 6) |
| 91 | #define ADR_CFG_PADS_2_ADD1 (0x1 << 6) |
| 92 | #define ADR_CFG_PADS_4_ADD1 (0x3 << 6) |
| 93 | #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8) |
| 94 | #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16)) |
| 95 | #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16)) |
| 96 | #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16)) |
| 97 | #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16)) |
| 98 | #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16)) |
| 99 | |
| 100 | /* |
| 101 | * Register: SPI_FAST_SEQ_n |
| 102 | */ |
| 103 | #define SEQ_OPC_OPCODE(x) ((x) << 0) |
| 104 | #define SEQ_OPC_CYCLES(x) ((x) << 8) |
| 105 | #define SEQ_OPC_PADS_1 (0x0 << 14) |
| 106 | #define SEQ_OPC_PADS_2 (0x1 << 14) |
| 107 | #define SEQ_OPC_PADS_4 (0x3 << 14) |
| 108 | #define SEQ_OPC_CSDEASSERT (1 << 16) |
| 109 | |
| 110 | /* |
| 111 | * Register: SPI_FAST_SEQ_CFG |
| 112 | */ |
| 113 | #define SEQ_CFG_STARTSEQ (1 << 0) |
| 114 | #define SEQ_CFG_SWRESET (1 << 5) |
| 115 | #define SEQ_CFG_CSDEASSERT (1 << 6) |
| 116 | #define SEQ_CFG_READNOTWRITE (1 << 7) |
| 117 | #define SEQ_CFG_ERASE (1 << 8) |
| 118 | #define SEQ_CFG_PADS_1 (0x0 << 16) |
| 119 | #define SEQ_CFG_PADS_2 (0x1 << 16) |
| 120 | #define SEQ_CFG_PADS_4 (0x3 << 16) |
| 121 | |
| 122 | /* |
| 123 | * Register: SPI_MODE_BITS |
| 124 | */ |
| 125 | #define MODE_DATA(x) (x & 0xff) |
| 126 | #define MODE_CYCLES(x) ((x & 0x3f) << 16) |
| 127 | #define MODE_PADS_1 (0x0 << 22) |
| 128 | #define MODE_PADS_2 (0x1 << 22) |
| 129 | #define MODE_PADS_4 (0x3 << 22) |
| 130 | #define DUMMY_CSDEASSERT (1 << 24) |
| 131 | |
| 132 | /* |
| 133 | * Register: SPI_DUMMY_BITS |
| 134 | */ |
| 135 | #define DUMMY_CYCLES(x) ((x & 0x3f) << 16) |
| 136 | #define DUMMY_PADS_1 (0x0 << 22) |
| 137 | #define DUMMY_PADS_2 (0x1 << 22) |
| 138 | #define DUMMY_PADS_4 (0x3 << 22) |
| 139 | #define DUMMY_CSDEASSERT (1 << 24) |
| 140 | |
| 141 | /* |
| 142 | * Register: SPI_FAST_SEQ_FLASH_STA_DATA |
| 143 | */ |
| 144 | #define STA_DATA_BYTE1(x) ((x & 0xff) << 0) |
| 145 | #define STA_DATA_BYTE2(x) ((x & 0xff) << 8) |
| 146 | #define STA_PADS_1 (0x0 << 16) |
| 147 | #define STA_PADS_2 (0x1 << 16) |
| 148 | #define STA_PADS_4 (0x3 << 16) |
| 149 | #define STA_CSDEASSERT (0x1 << 20) |
| 150 | #define STA_RDNOTWR (0x1 << 21) |
| 151 | |
| 152 | /* |
| 153 | * FSM SPI Instruction Opcodes |
| 154 | */ |
| 155 | #define STFSM_OPC_CMD 0x1 |
| 156 | #define STFSM_OPC_ADD 0x2 |
| 157 | #define STFSM_OPC_STA 0x3 |
| 158 | #define STFSM_OPC_MODE 0x4 |
| 159 | #define STFSM_OPC_DUMMY 0x5 |
| 160 | #define STFSM_OPC_DATA 0x6 |
| 161 | #define STFSM_OPC_WAIT 0x7 |
| 162 | #define STFSM_OPC_JUMP 0x8 |
| 163 | #define STFSM_OPC_GOTO 0x9 |
| 164 | #define STFSM_OPC_STOP 0xF |
| 165 | |
| 166 | /* |
| 167 | * FSM SPI Instructions (== opcode + operand). |
| 168 | */ |
| 169 | #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4)) |
| 170 | |
| 171 | #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1) |
| 172 | #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2) |
| 173 | #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3) |
| 174 | #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4) |
| 175 | #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5) |
| 176 | #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1) |
| 177 | #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2) |
| 178 | |
| 179 | #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1) |
| 180 | #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2) |
| 181 | |
| 182 | #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1) |
| 183 | #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1) |
| 184 | #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2) |
| 185 | #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3) |
| 186 | |
| 187 | #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0) |
| 188 | #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0) |
| 189 | #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0) |
| 190 | #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0) |
| 191 | |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 192 | #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */ |
| 193 | #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */ |
| 194 | |
| 195 | #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */ |
| 196 | |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 197 | struct stfsm { |
| 198 | struct device *dev; |
| 199 | void __iomem *base; |
| 200 | struct resource *region; |
| 201 | struct mtd_info mtd; |
| 202 | struct mutex lock; |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 203 | |
| 204 | uint32_t fifo_dir_delay; |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 207 | static inline uint32_t stfsm_fifo_available(struct stfsm *fsm) |
| 208 | { |
| 209 | return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f; |
| 210 | } |
| 211 | |
| 212 | static void stfsm_clear_fifo(struct stfsm *fsm) |
| 213 | { |
| 214 | uint32_t avail; |
| 215 | |
| 216 | for (;;) { |
| 217 | avail = stfsm_fifo_available(fsm); |
| 218 | if (!avail) |
| 219 | break; |
| 220 | |
| 221 | while (avail) { |
| 222 | readl(fsm->base + SPI_FAST_SEQ_DATA_REG); |
| 223 | avail--; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode) |
| 229 | { |
| 230 | int ret, timeout = 10; |
| 231 | |
| 232 | /* Wait for controller to accept mode change */ |
| 233 | while (--timeout) { |
| 234 | ret = readl(fsm->base + SPI_STA_MODE_CHANGE); |
| 235 | if (ret & 0x1) |
| 236 | break; |
| 237 | udelay(1); |
| 238 | } |
| 239 | |
| 240 | if (!timeout) |
| 241 | return -EBUSY; |
| 242 | |
| 243 | writel(mode, fsm->base + SPI_MODESELECT); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq) |
| 249 | { |
| 250 | uint32_t emi_freq; |
| 251 | uint32_t clk_div; |
| 252 | |
| 253 | /* TODO: Make this dynamic */ |
| 254 | emi_freq = STFSM_DEFAULT_EMI_FREQ; |
| 255 | |
| 256 | /* |
| 257 | * Calculate clk_div - values between 2 and 128 |
| 258 | * Multiple of 2, rounded up |
| 259 | */ |
| 260 | clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq); |
| 261 | if (clk_div < 2) |
| 262 | clk_div = 2; |
| 263 | else if (clk_div > 128) |
| 264 | clk_div = 128; |
| 265 | |
| 266 | /* |
| 267 | * Determine a suitable delay for the IP to complete a change of |
| 268 | * direction of the FIFO. The required delay is related to the clock |
| 269 | * divider used. The following heuristics are based on empirical tests, |
| 270 | * using a 100MHz EMI clock. |
| 271 | */ |
| 272 | if (clk_div <= 4) |
| 273 | fsm->fifo_dir_delay = 0; |
| 274 | else if (clk_div <= 10) |
| 275 | fsm->fifo_dir_delay = 1; |
| 276 | else |
| 277 | fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10); |
| 278 | |
| 279 | dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n", |
| 280 | emi_freq, spi_freq, clk_div); |
| 281 | |
| 282 | writel(clk_div, fsm->base + SPI_CLOCKDIV); |
| 283 | } |
| 284 | |
| 285 | static int stfsm_init(struct stfsm *fsm) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | /* Perform a soft reset of the FSM controller */ |
| 290 | writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG); |
| 291 | udelay(1); |
| 292 | writel(0, fsm->base + SPI_FAST_SEQ_CFG); |
| 293 | |
| 294 | /* Set clock to 'safe' frequency initially */ |
| 295 | stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ); |
| 296 | |
| 297 | /* Switch to FSM */ |
| 298 | ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | |
| 302 | /* Set timing parameters */ |
| 303 | writel(SPI_CFG_DEVICE_ST | |
| 304 | SPI_CFG_DEFAULT_MIN_CS_HIGH | |
| 305 | SPI_CFG_DEFAULT_CS_SETUPHOLD | |
| 306 | SPI_CFG_DEFAULT_DATA_HOLD, |
| 307 | fsm->base + SPI_CONFIGDATA); |
| 308 | writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG); |
| 309 | |
| 310 | /* Clear FIFO, just in case */ |
| 311 | stfsm_clear_fifo(fsm); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 316 | static int stfsm_probe(struct platform_device *pdev) |
| 317 | { |
| 318 | struct device_node *np = pdev->dev.of_node; |
| 319 | struct resource *res; |
| 320 | struct stfsm *fsm; |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 321 | int ret; |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 322 | |
| 323 | if (!np) { |
| 324 | dev_err(&pdev->dev, "No DT found\n"); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL); |
| 329 | if (!fsm) |
| 330 | return -ENOMEM; |
| 331 | |
| 332 | fsm->dev = &pdev->dev; |
| 333 | |
| 334 | platform_set_drvdata(pdev, fsm); |
| 335 | |
| 336 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 337 | if (!res) { |
| 338 | dev_err(&pdev->dev, "Resource not found\n"); |
| 339 | return -ENODEV; |
| 340 | } |
| 341 | |
| 342 | fsm->base = devm_ioremap_resource(&pdev->dev, res); |
| 343 | if (IS_ERR(fsm->base)) { |
| 344 | dev_err(&pdev->dev, |
| 345 | "Failed to reserve memory region %pR\n", res); |
| 346 | return PTR_ERR(fsm->base); |
| 347 | } |
| 348 | |
| 349 | mutex_init(&fsm->lock); |
| 350 | |
Lee Jones | 86f309fd | 2014-03-20 09:20:35 +0000 | [diff] [blame^] | 351 | ret = stfsm_init(fsm); |
| 352 | if (ret) { |
| 353 | dev_err(&pdev->dev, "Failed to initialise FSM Controller\n"); |
| 354 | return ret; |
| 355 | } |
| 356 | |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 357 | fsm->mtd.dev.parent = &pdev->dev; |
| 358 | fsm->mtd.type = MTD_NORFLASH; |
| 359 | fsm->mtd.writesize = 4; |
| 360 | fsm->mtd.writebufsize = fsm->mtd.writesize; |
| 361 | fsm->mtd.flags = MTD_CAP_NORFLASH; |
| 362 | |
| 363 | return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0); |
| 364 | } |
| 365 | |
| 366 | static int stfsm_remove(struct platform_device *pdev) |
| 367 | { |
| 368 | struct stfsm *fsm = platform_get_drvdata(pdev); |
| 369 | int err; |
| 370 | |
| 371 | err = mtd_device_unregister(&fsm->mtd); |
| 372 | if (err) |
| 373 | return err; |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static struct of_device_id stfsm_match[] = { |
| 379 | { .compatible = "st,spi-fsm", }, |
| 380 | {}, |
| 381 | }; |
| 382 | MODULE_DEVICE_TABLE(of, stfsm_match); |
| 383 | |
| 384 | static struct platform_driver stfsm_driver = { |
| 385 | .probe = stfsm_probe, |
| 386 | .remove = stfsm_remove, |
| 387 | .driver = { |
| 388 | .name = "st-spi-fsm", |
| 389 | .owner = THIS_MODULE, |
| 390 | .of_match_table = stfsm_match, |
| 391 | }, |
| 392 | }; |
| 393 | module_platform_driver(stfsm_driver); |
| 394 | |
| 395 | MODULE_AUTHOR("Angus Clark <angus.clark@st.com>"); |
| 396 | MODULE_DESCRIPTION("ST SPI FSM driver"); |
| 397 | MODULE_LICENSE("GPL"); |