Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2012 |
Patrick Delaunay | eae488b | 2022-05-20 18:38:10 +0200 | [diff] [blame] | 4 | * Armando Visconti, STMicroelectronics, armando.visconti@st.com. |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2018 |
| 7 | * Quentin Schulz, Bootlin, quentin.schulz@bootlin.com |
| 8 | * |
| 9 | * Driver for ARM PL022 SPI Controller. |
| 10 | */ |
| 11 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 12 | #include <clk.h> |
| 13 | #include <common.h> |
| 14 | #include <dm.h> |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Stefan Herbrechtsmeier | 0361286 | 2023-04-28 14:38:49 +0200 | [diff] [blame] | 16 | #include <fdtdec.h> |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 17 | #include <linux/io.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 19 | #include <asm/gpio.h> |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 20 | #include <spi.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 21 | #include <linux/printk.h> |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 22 | |
| 23 | #define SSP_CR0 0x000 |
| 24 | #define SSP_CR1 0x004 |
| 25 | #define SSP_DR 0x008 |
| 26 | #define SSP_SR 0x00C |
| 27 | #define SSP_CPSR 0x010 |
| 28 | #define SSP_IMSC 0x014 |
| 29 | #define SSP_RIS 0x018 |
| 30 | #define SSP_MIS 0x01C |
| 31 | #define SSP_ICR 0x020 |
| 32 | #define SSP_DMACR 0x024 |
| 33 | #define SSP_CSR 0x030 /* vendor extension */ |
| 34 | #define SSP_ITCR 0x080 |
| 35 | #define SSP_ITIP 0x084 |
| 36 | #define SSP_ITOP 0x088 |
| 37 | #define SSP_TDR 0x08C |
| 38 | |
| 39 | #define SSP_PID0 0xFE0 |
| 40 | #define SSP_PID1 0xFE4 |
| 41 | #define SSP_PID2 0xFE8 |
| 42 | #define SSP_PID3 0xFEC |
| 43 | |
| 44 | #define SSP_CID0 0xFF0 |
| 45 | #define SSP_CID1 0xFF4 |
| 46 | #define SSP_CID2 0xFF8 |
| 47 | #define SSP_CID3 0xFFC |
| 48 | |
| 49 | /* SSP Control Register 0 - SSP_CR0 */ |
| 50 | #define SSP_CR0_SPO (0x1 << 6) |
| 51 | #define SSP_CR0_SPH (0x1 << 7) |
| 52 | #define SSP_CR0_BIT_MODE(x) ((x) - 1) |
| 53 | #define SSP_SCR_MIN (0x00) |
| 54 | #define SSP_SCR_MAX (0xFF) |
| 55 | #define SSP_SCR_SHFT 8 |
| 56 | #define DFLT_CLKRATE 2 |
| 57 | |
| 58 | /* SSP Control Register 1 - SSP_CR1 */ |
| 59 | #define SSP_CR1_MASK_SSE (0x1 << 1) |
| 60 | |
| 61 | #define SSP_CPSR_MIN (0x02) |
| 62 | #define SSP_CPSR_MAX (0xFE) |
| 63 | #define DFLT_PRESCALE (0x40) |
| 64 | |
| 65 | /* SSP Status Register - SSP_SR */ |
| 66 | #define SSP_SR_MASK_TFE (0x1 << 0) /* Transmit FIFO empty */ |
| 67 | #define SSP_SR_MASK_TNF (0x1 << 1) /* Transmit FIFO not full */ |
| 68 | #define SSP_SR_MASK_RNE (0x1 << 2) /* Receive FIFO not empty */ |
| 69 | #define SSP_SR_MASK_RFF (0x1 << 3) /* Receive FIFO full */ |
| 70 | #define SSP_SR_MASK_BSY (0x1 << 4) /* Busy Flag */ |
| 71 | |
Stefan Herbrechtsmeier | 0361286 | 2023-04-28 14:38:49 +0200 | [diff] [blame] | 72 | struct pl022_spi_pdata { |
| 73 | fdt_addr_t addr; |
| 74 | fdt_size_t size; |
| 75 | unsigned int freq; |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 76 | #if CONFIG_IS_ENABLED(DM_GPIO) |
| 77 | struct gpio_desc cs_gpio; |
| 78 | #endif |
Stefan Herbrechtsmeier | 0361286 | 2023-04-28 14:38:49 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 81 | struct pl022_spi_slave { |
| 82 | void *base; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 83 | unsigned int freq; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * ARM PL022 exists in different 'flavors'. |
| 88 | * This drivers currently support the standard variant (0x00041022), that has a |
| 89 | * 16bit wide and 8 locations deep TX/RX FIFO. |
| 90 | */ |
| 91 | static int pl022_is_supported(struct pl022_spi_slave *ps) |
| 92 | { |
| 93 | /* PL022 version is 0x00041022 */ |
| 94 | if ((readw(ps->base + SSP_PID0) == 0x22) && |
| 95 | (readw(ps->base + SSP_PID1) == 0x10) && |
| 96 | ((readw(ps->base + SSP_PID2) & 0xf) == 0x04) && |
| 97 | (readw(ps->base + SSP_PID3) == 0x00)) |
| 98 | return 1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 103 | static int pl022_spi_probe(struct udevice *bus) |
| 104 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 105 | struct pl022_spi_pdata *plat = dev_get_plat(bus); |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 106 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 107 | |
| 108 | ps->base = ioremap(plat->addr, plat->size); |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 109 | ps->freq = plat->freq; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 110 | |
| 111 | /* Check the PL022 version */ |
| 112 | if (!pl022_is_supported(ps)) |
| 113 | return -ENOTSUPP; |
| 114 | |
| 115 | /* 8 bits per word, high polarity and default clock rate */ |
| 116 | writew(SSP_CR0_BIT_MODE(8), ps->base + SSP_CR0); |
| 117 | writew(DFLT_PRESCALE, ps->base + SSP_CPSR); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Stefan Herbrechtsmeier | 47c3273 | 2023-04-28 14:38:48 +0200 | [diff] [blame] | 122 | static void pl022_spi_flush(struct pl022_spi_slave *ps) |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 123 | { |
| 124 | do { |
| 125 | while (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) |
| 126 | readw(ps->base + SSP_DR); |
| 127 | } while (readw(ps->base + SSP_SR) & SSP_SR_MASK_BSY); |
| 128 | } |
| 129 | |
| 130 | static int pl022_spi_claim_bus(struct udevice *dev) |
| 131 | { |
| 132 | struct udevice *bus = dev->parent; |
| 133 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 134 | u16 reg; |
| 135 | |
| 136 | /* Enable the SPI hardware */ |
| 137 | reg = readw(ps->base + SSP_CR1); |
| 138 | reg |= SSP_CR1_MASK_SSE; |
| 139 | writew(reg, ps->base + SSP_CR1); |
| 140 | |
Stefan Herbrechtsmeier | 47c3273 | 2023-04-28 14:38:48 +0200 | [diff] [blame] | 141 | pl022_spi_flush(ps); |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int pl022_spi_release_bus(struct udevice *dev) |
| 147 | { |
| 148 | struct udevice *bus = dev->parent; |
| 149 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 150 | u16 reg; |
| 151 | |
Stefan Herbrechtsmeier | 47c3273 | 2023-04-28 14:38:48 +0200 | [diff] [blame] | 152 | pl022_spi_flush(ps); |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 153 | |
| 154 | /* Disable the SPI hardware */ |
| 155 | reg = readw(ps->base + SSP_CR1); |
| 156 | reg &= ~SSP_CR1_MASK_SSE; |
| 157 | writew(reg, ps->base + SSP_CR1); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 162 | static void pl022_spi_set_cs(struct udevice *dev, bool on) |
| 163 | { |
| 164 | #if CONFIG_IS_ENABLED(DM_GPIO) |
| 165 | struct udevice *bus = dev->parent; |
| 166 | struct pl022_spi_pdata *plat = dev_get_plat(bus); |
| 167 | |
| 168 | if (dm_gpio_is_valid(&plat->cs_gpio)) |
| 169 | dm_gpio_set_value(&plat->cs_gpio, on ? 1 : 0); |
| 170 | #endif |
| 171 | } |
| 172 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 173 | static int pl022_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 174 | const void *dout, void *din, unsigned long flags) |
| 175 | { |
| 176 | struct udevice *bus = dev->parent; |
| 177 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 178 | u32 len_tx = 0, len_rx = 0, len; |
| 179 | u32 ret = 0; |
| 180 | const u8 *txp = dout; |
| 181 | u8 *rxp = din, value; |
| 182 | |
| 183 | if (bitlen == 0) |
| 184 | /* Finish any previously submitted transfers */ |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 185 | goto done; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * TODO: The controller can do non-multiple-of-8 bit |
| 189 | * transfers, but this driver currently doesn't support it. |
| 190 | * |
| 191 | * It's also not clear how such transfers are supposed to be |
| 192 | * represented as a stream of bytes...this is a limitation of |
| 193 | * the current SPI interface. |
| 194 | */ |
| 195 | if (bitlen % 8) { |
| 196 | /* Errors always terminate an ongoing transfer */ |
| 197 | flags |= SPI_XFER_END; |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 198 | ret = -1; |
| 199 | goto done; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 202 | if (flags & SPI_XFER_BEGIN) |
| 203 | pl022_spi_set_cs(dev, true); |
| 204 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 205 | len = bitlen / 8; |
| 206 | |
| 207 | while (len_tx < len) { |
| 208 | if (readw(ps->base + SSP_SR) & SSP_SR_MASK_TNF) { |
| 209 | value = txp ? *txp++ : 0; |
| 210 | writew(value, ps->base + SSP_DR); |
| 211 | len_tx++; |
| 212 | } |
| 213 | |
| 214 | if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) { |
| 215 | value = readw(ps->base + SSP_DR); |
| 216 | if (rxp) |
| 217 | *rxp++ = value; |
| 218 | len_rx++; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | while (len_rx < len_tx) { |
| 223 | if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) { |
| 224 | value = readw(ps->base + SSP_DR); |
| 225 | if (rxp) |
| 226 | *rxp++ = value; |
| 227 | len_rx++; |
| 228 | } |
| 229 | } |
| 230 | |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 231 | done: |
| 232 | if (flags & SPI_XFER_END) |
| 233 | pl022_spi_set_cs(dev, false); |
| 234 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr) |
| 239 | { |
| 240 | return rate / (cpsdvsr * (1 + scr)); |
| 241 | } |
| 242 | |
| 243 | static int pl022_spi_set_speed(struct udevice *bus, uint speed) |
| 244 | { |
| 245 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 246 | u16 scr = SSP_SCR_MIN, cr0 = 0, cpsr = SSP_CPSR_MIN, best_scr = scr, |
| 247 | best_cpsr = cpsr; |
| 248 | u32 min, max, best_freq = 0, tmp; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 249 | u32 rate = ps->freq; |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 250 | bool found = false; |
| 251 | |
| 252 | max = spi_rate(rate, SSP_CPSR_MIN, SSP_SCR_MIN); |
| 253 | min = spi_rate(rate, SSP_CPSR_MAX, SSP_SCR_MAX); |
| 254 | |
| 255 | if (speed > max || speed < min) { |
| 256 | pr_err("Tried to set speed to %dHz but min=%d and max=%d\n", |
| 257 | speed, min, max); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | while (cpsr <= SSP_CPSR_MAX && !found) { |
| 262 | while (scr <= SSP_SCR_MAX) { |
| 263 | tmp = spi_rate(rate, cpsr, scr); |
| 264 | |
| 265 | if (abs(speed - tmp) < abs(speed - best_freq)) { |
| 266 | best_freq = tmp; |
| 267 | best_cpsr = cpsr; |
| 268 | best_scr = scr; |
| 269 | |
| 270 | if (tmp == speed) { |
| 271 | found = true; |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | scr++; |
| 277 | } |
| 278 | cpsr += 2; |
| 279 | scr = SSP_SCR_MIN; |
| 280 | } |
| 281 | |
| 282 | writew(best_cpsr, ps->base + SSP_CPSR); |
| 283 | cr0 = readw(ps->base + SSP_CR0); |
| 284 | writew(cr0 | (best_scr << SSP_SCR_SHFT), ps->base + SSP_CR0); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int pl022_spi_set_mode(struct udevice *bus, uint mode) |
| 290 | { |
| 291 | struct pl022_spi_slave *ps = dev_get_priv(bus); |
| 292 | u16 reg; |
| 293 | |
| 294 | reg = readw(ps->base + SSP_CR0); |
| 295 | reg &= ~(SSP_CR0_SPH | SSP_CR0_SPO); |
| 296 | if (mode & SPI_CPHA) |
| 297 | reg |= SSP_CR0_SPH; |
| 298 | if (mode & SPI_CPOL) |
| 299 | reg |= SSP_CR0_SPO; |
| 300 | writew(reg, ps->base + SSP_CR0); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int pl022_cs_info(struct udevice *bus, uint cs, |
| 306 | struct spi_cs_info *info) |
| 307 | { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static const struct dm_spi_ops pl022_spi_ops = { |
| 312 | .claim_bus = pl022_spi_claim_bus, |
| 313 | .release_bus = pl022_spi_release_bus, |
| 314 | .xfer = pl022_spi_xfer, |
| 315 | .set_speed = pl022_spi_set_speed, |
| 316 | .set_mode = pl022_spi_set_mode, |
| 317 | .cs_info = pl022_cs_info, |
| 318 | }; |
| 319 | |
Simon Glass | 9539738 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 320 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 321 | static int pl022_spi_of_to_plat(struct udevice *bus) |
Jagan Teki | 3deb1f7 | 2018-11-14 15:28:05 +0530 | [diff] [blame] | 322 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 323 | struct pl022_spi_pdata *plat = dev_get_plat(bus); |
Jagan Teki | 3deb1f7 | 2018-11-14 15:28:05 +0530 | [diff] [blame] | 324 | const void *fdt = gd->fdt_blob; |
| 325 | int node = dev_of_offset(bus); |
| 326 | struct clk clkdev; |
| 327 | int ret; |
| 328 | |
| 329 | plat->addr = fdtdec_get_addr_size(fdt, node, "reg", &plat->size); |
| 330 | |
| 331 | ret = clk_get_by_index(bus, 0, &clkdev); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | |
| 335 | plat->freq = clk_get_rate(&clkdev); |
| 336 | |
Lukas Funke | 47d9ae5 | 2023-04-28 14:38:50 +0200 | [diff] [blame] | 337 | #if CONFIG_IS_ENABLED(DM_GPIO) |
| 338 | ret = gpio_request_by_name(bus, "cs-gpios", 0, &plat->cs_gpio, |
| 339 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 340 | if (ret < 0 && ret != -ENOENT) |
| 341 | return ret; |
| 342 | #endif |
| 343 | |
Jagan Teki | 3deb1f7 | 2018-11-14 15:28:05 +0530 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 347 | static const struct udevice_id pl022_spi_ids[] = { |
Lukas Funke | ad77009 | 2023-04-28 14:38:47 +0200 | [diff] [blame] | 348 | { .compatible = "arm,pl022" }, |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 349 | { } |
| 350 | }; |
| 351 | #endif |
| 352 | |
| 353 | U_BOOT_DRIVER(pl022_spi) = { |
| 354 | .name = "pl022_spi", |
| 355 | .id = UCLASS_SPI, |
Simon Glass | 9539738 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 356 | #if CONFIG_IS_ENABLED(OF_REAL) |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 357 | .of_match = pl022_spi_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 358 | .of_to_plat = pl022_spi_of_to_plat, |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 359 | #endif |
Jagan Teki | 3deb1f7 | 2018-11-14 15:28:05 +0530 | [diff] [blame] | 360 | .ops = &pl022_spi_ops, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 361 | .plat_auto = sizeof(struct pl022_spi_pdata), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 362 | .priv_auto = sizeof(struct pl022_spi_slave), |
Quentin Schulz | 8a4791f | 2018-08-31 16:28:29 +0200 | [diff] [blame] | 363 | .probe = pl022_spi_probe, |
| 364 | }; |