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 | |
| 77 | /* |
| 78 | * Register: SPI_FAST_SEQ_TRANSFER_SIZE |
| 79 | */ |
| 80 | #define TRANSFER_SIZE(x) ((x) * 8) |
| 81 | |
| 82 | /* |
| 83 | * Register: SPI_FAST_SEQ_ADD_CFG |
| 84 | */ |
| 85 | #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0) |
| 86 | #define ADR_CFG_PADS_1_ADD1 (0x0 << 6) |
| 87 | #define ADR_CFG_PADS_2_ADD1 (0x1 << 6) |
| 88 | #define ADR_CFG_PADS_4_ADD1 (0x3 << 6) |
| 89 | #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8) |
| 90 | #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16)) |
| 91 | #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16)) |
| 92 | #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16)) |
| 93 | #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16)) |
| 94 | #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16)) |
| 95 | |
| 96 | /* |
| 97 | * Register: SPI_FAST_SEQ_n |
| 98 | */ |
| 99 | #define SEQ_OPC_OPCODE(x) ((x) << 0) |
| 100 | #define SEQ_OPC_CYCLES(x) ((x) << 8) |
| 101 | #define SEQ_OPC_PADS_1 (0x0 << 14) |
| 102 | #define SEQ_OPC_PADS_2 (0x1 << 14) |
| 103 | #define SEQ_OPC_PADS_4 (0x3 << 14) |
| 104 | #define SEQ_OPC_CSDEASSERT (1 << 16) |
| 105 | |
| 106 | /* |
| 107 | * Register: SPI_FAST_SEQ_CFG |
| 108 | */ |
| 109 | #define SEQ_CFG_STARTSEQ (1 << 0) |
| 110 | #define SEQ_CFG_SWRESET (1 << 5) |
| 111 | #define SEQ_CFG_CSDEASSERT (1 << 6) |
| 112 | #define SEQ_CFG_READNOTWRITE (1 << 7) |
| 113 | #define SEQ_CFG_ERASE (1 << 8) |
| 114 | #define SEQ_CFG_PADS_1 (0x0 << 16) |
| 115 | #define SEQ_CFG_PADS_2 (0x1 << 16) |
| 116 | #define SEQ_CFG_PADS_4 (0x3 << 16) |
| 117 | |
| 118 | /* |
| 119 | * Register: SPI_MODE_BITS |
| 120 | */ |
| 121 | #define MODE_DATA(x) (x & 0xff) |
| 122 | #define MODE_CYCLES(x) ((x & 0x3f) << 16) |
| 123 | #define MODE_PADS_1 (0x0 << 22) |
| 124 | #define MODE_PADS_2 (0x1 << 22) |
| 125 | #define MODE_PADS_4 (0x3 << 22) |
| 126 | #define DUMMY_CSDEASSERT (1 << 24) |
| 127 | |
| 128 | /* |
| 129 | * Register: SPI_DUMMY_BITS |
| 130 | */ |
| 131 | #define DUMMY_CYCLES(x) ((x & 0x3f) << 16) |
| 132 | #define DUMMY_PADS_1 (0x0 << 22) |
| 133 | #define DUMMY_PADS_2 (0x1 << 22) |
| 134 | #define DUMMY_PADS_4 (0x3 << 22) |
| 135 | #define DUMMY_CSDEASSERT (1 << 24) |
| 136 | |
| 137 | /* |
| 138 | * Register: SPI_FAST_SEQ_FLASH_STA_DATA |
| 139 | */ |
| 140 | #define STA_DATA_BYTE1(x) ((x & 0xff) << 0) |
| 141 | #define STA_DATA_BYTE2(x) ((x & 0xff) << 8) |
| 142 | #define STA_PADS_1 (0x0 << 16) |
| 143 | #define STA_PADS_2 (0x1 << 16) |
| 144 | #define STA_PADS_4 (0x3 << 16) |
| 145 | #define STA_CSDEASSERT (0x1 << 20) |
| 146 | #define STA_RDNOTWR (0x1 << 21) |
| 147 | |
| 148 | /* |
| 149 | * FSM SPI Instruction Opcodes |
| 150 | */ |
| 151 | #define STFSM_OPC_CMD 0x1 |
| 152 | #define STFSM_OPC_ADD 0x2 |
| 153 | #define STFSM_OPC_STA 0x3 |
| 154 | #define STFSM_OPC_MODE 0x4 |
| 155 | #define STFSM_OPC_DUMMY 0x5 |
| 156 | #define STFSM_OPC_DATA 0x6 |
| 157 | #define STFSM_OPC_WAIT 0x7 |
| 158 | #define STFSM_OPC_JUMP 0x8 |
| 159 | #define STFSM_OPC_GOTO 0x9 |
| 160 | #define STFSM_OPC_STOP 0xF |
| 161 | |
| 162 | /* |
| 163 | * FSM SPI Instructions (== opcode + operand). |
| 164 | */ |
| 165 | #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4)) |
| 166 | |
| 167 | #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1) |
| 168 | #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2) |
| 169 | #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3) |
| 170 | #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4) |
| 171 | #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5) |
| 172 | #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1) |
| 173 | #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2) |
| 174 | |
| 175 | #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1) |
| 176 | #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2) |
| 177 | |
| 178 | #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1) |
| 179 | #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1) |
| 180 | #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2) |
| 181 | #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3) |
| 182 | |
| 183 | #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0) |
| 184 | #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0) |
| 185 | #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0) |
| 186 | #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0) |
| 187 | |
Lee Jones | d90db4a | 2014-03-20 09:20:33 +0000 | [diff] [blame] | 188 | struct stfsm { |
| 189 | struct device *dev; |
| 190 | void __iomem *base; |
| 191 | struct resource *region; |
| 192 | struct mtd_info mtd; |
| 193 | struct mutex lock; |
| 194 | }; |
| 195 | |
| 196 | static int stfsm_probe(struct platform_device *pdev) |
| 197 | { |
| 198 | struct device_node *np = pdev->dev.of_node; |
| 199 | struct resource *res; |
| 200 | struct stfsm *fsm; |
| 201 | |
| 202 | if (!np) { |
| 203 | dev_err(&pdev->dev, "No DT found\n"); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL); |
| 208 | if (!fsm) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | fsm->dev = &pdev->dev; |
| 212 | |
| 213 | platform_set_drvdata(pdev, fsm); |
| 214 | |
| 215 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 216 | if (!res) { |
| 217 | dev_err(&pdev->dev, "Resource not found\n"); |
| 218 | return -ENODEV; |
| 219 | } |
| 220 | |
| 221 | fsm->base = devm_ioremap_resource(&pdev->dev, res); |
| 222 | if (IS_ERR(fsm->base)) { |
| 223 | dev_err(&pdev->dev, |
| 224 | "Failed to reserve memory region %pR\n", res); |
| 225 | return PTR_ERR(fsm->base); |
| 226 | } |
| 227 | |
| 228 | mutex_init(&fsm->lock); |
| 229 | |
| 230 | fsm->mtd.dev.parent = &pdev->dev; |
| 231 | fsm->mtd.type = MTD_NORFLASH; |
| 232 | fsm->mtd.writesize = 4; |
| 233 | fsm->mtd.writebufsize = fsm->mtd.writesize; |
| 234 | fsm->mtd.flags = MTD_CAP_NORFLASH; |
| 235 | |
| 236 | return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0); |
| 237 | } |
| 238 | |
| 239 | static int stfsm_remove(struct platform_device *pdev) |
| 240 | { |
| 241 | struct stfsm *fsm = platform_get_drvdata(pdev); |
| 242 | int err; |
| 243 | |
| 244 | err = mtd_device_unregister(&fsm->mtd); |
| 245 | if (err) |
| 246 | return err; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static struct of_device_id stfsm_match[] = { |
| 252 | { .compatible = "st,spi-fsm", }, |
| 253 | {}, |
| 254 | }; |
| 255 | MODULE_DEVICE_TABLE(of, stfsm_match); |
| 256 | |
| 257 | static struct platform_driver stfsm_driver = { |
| 258 | .probe = stfsm_probe, |
| 259 | .remove = stfsm_remove, |
| 260 | .driver = { |
| 261 | .name = "st-spi-fsm", |
| 262 | .owner = THIS_MODULE, |
| 263 | .of_match_table = stfsm_match, |
| 264 | }, |
| 265 | }; |
| 266 | module_platform_driver(stfsm_driver); |
| 267 | |
| 268 | MODULE_AUTHOR("Angus Clark <angus.clark@st.com>"); |
| 269 | MODULE_DESCRIPTION("ST SPI FSM driver"); |
| 270 | MODULE_LICENSE("GPL"); |