1 // SPDX-License-Identifier: GPL-2.0+
4 * Armando Visconti, ST Microelectronics, armando.visconti@st.com.
7 * Quentin Schulz, Bootlin, quentin.schulz@bootlin.com
9 * Driver for ARM PL022 SPI Controller.
16 #include <dm/platform_data/pl022_spi.h>
18 #include <linux/bitops.h>
19 #include <linux/bug.h>
21 #include <linux/kernel.h>
28 #define SSP_CPSR 0x010
29 #define SSP_IMSC 0x014
33 #define SSP_DMACR 0x024
34 #define SSP_CSR 0x030 /* vendor extension */
35 #define SSP_ITCR 0x080
36 #define SSP_ITIP 0x084
37 #define SSP_ITOP 0x088
40 #define SSP_PID0 0xFE0
41 #define SSP_PID1 0xFE4
42 #define SSP_PID2 0xFE8
43 #define SSP_PID3 0xFEC
45 #define SSP_CID0 0xFF0
46 #define SSP_CID1 0xFF4
47 #define SSP_CID2 0xFF8
48 #define SSP_CID3 0xFFC
50 /* SSP Control Register 0 - SSP_CR0 */
51 #define SSP_CR0_SPO (0x1 << 6)
52 #define SSP_CR0_SPH (0x1 << 7)
53 #define SSP_CR0_BIT_MODE(x) ((x) - 1)
54 #define SSP_SCR_MIN (0x00)
55 #define SSP_SCR_MAX (0xFF)
56 #define SSP_SCR_SHFT 8
57 #define DFLT_CLKRATE 2
59 /* SSP Control Register 1 - SSP_CR1 */
60 #define SSP_CR1_MASK_SSE (0x1 << 1)
62 #define SSP_CPSR_MIN (0x02)
63 #define SSP_CPSR_MAX (0xFE)
64 #define DFLT_PRESCALE (0x40)
66 /* SSP Status Register - SSP_SR */
67 #define SSP_SR_MASK_TFE (0x1 << 0) /* Transmit FIFO empty */
68 #define SSP_SR_MASK_TNF (0x1 << 1) /* Transmit FIFO not full */
69 #define SSP_SR_MASK_RNE (0x1 << 2) /* Receive FIFO not empty */
70 #define SSP_SR_MASK_RFF (0x1 << 3) /* Receive FIFO full */
71 #define SSP_SR_MASK_BSY (0x1 << 4) /* Busy Flag */
73 struct pl022_spi_slave {
79 * ARM PL022 exists in different 'flavors'.
80 * This drivers currently support the standard variant (0x00041022), that has a
81 * 16bit wide and 8 locations deep TX/RX FIFO.
83 static int pl022_is_supported(struct pl022_spi_slave *ps)
85 /* PL022 version is 0x00041022 */
86 if ((readw(ps->base + SSP_PID0) == 0x22) &&
87 (readw(ps->base + SSP_PID1) == 0x10) &&
88 ((readw(ps->base + SSP_PID2) & 0xf) == 0x04) &&
89 (readw(ps->base + SSP_PID3) == 0x00))
95 static int pl022_spi_probe(struct udevice *bus)
97 struct pl022_spi_pdata *plat = dev_get_platdata(bus);
98 struct pl022_spi_slave *ps = dev_get_priv(bus);
100 ps->base = ioremap(plat->addr, plat->size);
101 ps->freq = plat->freq;
103 /* Check the PL022 version */
104 if (!pl022_is_supported(ps))
107 /* 8 bits per word, high polarity and default clock rate */
108 writew(SSP_CR0_BIT_MODE(8), ps->base + SSP_CR0);
109 writew(DFLT_PRESCALE, ps->base + SSP_CPSR);
114 static void flush(struct pl022_spi_slave *ps)
117 while (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE)
118 readw(ps->base + SSP_DR);
119 } while (readw(ps->base + SSP_SR) & SSP_SR_MASK_BSY);
122 static int pl022_spi_claim_bus(struct udevice *dev)
124 struct udevice *bus = dev->parent;
125 struct pl022_spi_slave *ps = dev_get_priv(bus);
128 /* Enable the SPI hardware */
129 reg = readw(ps->base + SSP_CR1);
130 reg |= SSP_CR1_MASK_SSE;
131 writew(reg, ps->base + SSP_CR1);
138 static int pl022_spi_release_bus(struct udevice *dev)
140 struct udevice *bus = dev->parent;
141 struct pl022_spi_slave *ps = dev_get_priv(bus);
146 /* Disable the SPI hardware */
147 reg = readw(ps->base + SSP_CR1);
148 reg &= ~SSP_CR1_MASK_SSE;
149 writew(reg, ps->base + SSP_CR1);
154 static int pl022_spi_xfer(struct udevice *dev, unsigned int bitlen,
155 const void *dout, void *din, unsigned long flags)
157 struct udevice *bus = dev->parent;
158 struct pl022_spi_slave *ps = dev_get_priv(bus);
159 u32 len_tx = 0, len_rx = 0, len;
161 const u8 *txp = dout;
162 u8 *rxp = din, value;
165 /* Finish any previously submitted transfers */
169 * TODO: The controller can do non-multiple-of-8 bit
170 * transfers, but this driver currently doesn't support it.
172 * It's also not clear how such transfers are supposed to be
173 * represented as a stream of bytes...this is a limitation of
174 * the current SPI interface.
177 /* Errors always terminate an ongoing transfer */
178 flags |= SPI_XFER_END;
184 while (len_tx < len) {
185 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_TNF) {
186 value = txp ? *txp++ : 0;
187 writew(value, ps->base + SSP_DR);
191 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
192 value = readw(ps->base + SSP_DR);
199 while (len_rx < len_tx) {
200 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
201 value = readw(ps->base + SSP_DR);
211 static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
213 return rate / (cpsdvsr * (1 + scr));
216 static int pl022_spi_set_speed(struct udevice *bus, uint speed)
218 struct pl022_spi_slave *ps = dev_get_priv(bus);
219 u16 scr = SSP_SCR_MIN, cr0 = 0, cpsr = SSP_CPSR_MIN, best_scr = scr,
221 u32 min, max, best_freq = 0, tmp;
225 max = spi_rate(rate, SSP_CPSR_MIN, SSP_SCR_MIN);
226 min = spi_rate(rate, SSP_CPSR_MAX, SSP_SCR_MAX);
228 if (speed > max || speed < min) {
229 pr_err("Tried to set speed to %dHz but min=%d and max=%d\n",
234 while (cpsr <= SSP_CPSR_MAX && !found) {
235 while (scr <= SSP_SCR_MAX) {
236 tmp = spi_rate(rate, cpsr, scr);
238 if (abs(speed - tmp) < abs(speed - best_freq)) {
255 writew(best_cpsr, ps->base + SSP_CPSR);
256 cr0 = readw(ps->base + SSP_CR0);
257 writew(cr0 | (best_scr << SSP_SCR_SHFT), ps->base + SSP_CR0);
262 static int pl022_spi_set_mode(struct udevice *bus, uint mode)
264 struct pl022_spi_slave *ps = dev_get_priv(bus);
267 reg = readw(ps->base + SSP_CR0);
268 reg &= ~(SSP_CR0_SPH | SSP_CR0_SPO);
273 writew(reg, ps->base + SSP_CR0);
278 static int pl022_cs_info(struct udevice *bus, uint cs,
279 struct spi_cs_info *info)
284 static const struct dm_spi_ops pl022_spi_ops = {
285 .claim_bus = pl022_spi_claim_bus,
286 .release_bus = pl022_spi_release_bus,
287 .xfer = pl022_spi_xfer,
288 .set_speed = pl022_spi_set_speed,
289 .set_mode = pl022_spi_set_mode,
290 .cs_info = pl022_cs_info,
293 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
294 static int pl022_spi_ofdata_to_platdata(struct udevice *bus)
296 struct pl022_spi_pdata *plat = bus->platdata;
297 const void *fdt = gd->fdt_blob;
298 int node = dev_of_offset(bus);
302 plat->addr = fdtdec_get_addr_size(fdt, node, "reg", &plat->size);
304 ret = clk_get_by_index(bus, 0, &clkdev);
308 plat->freq = clk_get_rate(&clkdev);
313 static const struct udevice_id pl022_spi_ids[] = {
314 { .compatible = "arm,pl022-spi" },
319 U_BOOT_DRIVER(pl022_spi) = {
322 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
323 .of_match = pl022_spi_ids,
324 .ofdata_to_platdata = pl022_spi_ofdata_to_platdata,
326 .ops = &pl022_spi_ops,
327 .platdata_auto_alloc_size = sizeof(struct pl022_spi_pdata),
328 .priv_auto_alloc_size = sizeof(struct pl022_spi_slave),
329 .probe = pl022_spi_probe,