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 {
75 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
83 * ARM PL022 exists in different 'flavors'.
84 * This drivers currently support the standard variant (0x00041022), that has a
85 * 16bit wide and 8 locations deep TX/RX FIFO.
87 static int pl022_is_supported(struct pl022_spi_slave *ps)
89 /* PL022 version is 0x00041022 */
90 if ((readw(ps->base + SSP_PID0) == 0x22) &&
91 (readw(ps->base + SSP_PID1) == 0x10) &&
92 ((readw(ps->base + SSP_PID2) & 0xf) == 0x04) &&
93 (readw(ps->base + SSP_PID3) == 0x00))
99 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
100 static int pl022_spi_ofdata_to_platdata(struct udevice *bus)
102 struct pl022_spi_pdata *plat = bus->platdata;
103 const void *fdt = gd->fdt_blob;
104 int node = dev_of_offset(bus);
106 plat->addr = fdtdec_get_addr_size(fdt, node, "reg", &plat->size);
108 return clk_get_by_index(bus, 0, &plat->clk);
112 static int pl022_spi_probe(struct udevice *bus)
114 struct pl022_spi_pdata *plat = dev_get_platdata(bus);
115 struct pl022_spi_slave *ps = dev_get_priv(bus);
117 ps->base = ioremap(plat->addr, plat->size);
118 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
121 ps->freq = plat->freq;
124 /* Check the PL022 version */
125 if (!pl022_is_supported(ps))
128 /* 8 bits per word, high polarity and default clock rate */
129 writew(SSP_CR0_BIT_MODE(8), ps->base + SSP_CR0);
130 writew(DFLT_PRESCALE, ps->base + SSP_CPSR);
135 static void flush(struct pl022_spi_slave *ps)
138 while (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE)
139 readw(ps->base + SSP_DR);
140 } while (readw(ps->base + SSP_SR) & SSP_SR_MASK_BSY);
143 static int pl022_spi_claim_bus(struct udevice *dev)
145 struct udevice *bus = dev->parent;
146 struct pl022_spi_slave *ps = dev_get_priv(bus);
149 /* Enable the SPI hardware */
150 reg = readw(ps->base + SSP_CR1);
151 reg |= SSP_CR1_MASK_SSE;
152 writew(reg, ps->base + SSP_CR1);
159 static int pl022_spi_release_bus(struct udevice *dev)
161 struct udevice *bus = dev->parent;
162 struct pl022_spi_slave *ps = dev_get_priv(bus);
167 /* Disable the SPI hardware */
168 reg = readw(ps->base + SSP_CR1);
169 reg &= ~SSP_CR1_MASK_SSE;
170 writew(reg, ps->base + SSP_CR1);
175 static int pl022_spi_xfer(struct udevice *dev, unsigned int bitlen,
176 const void *dout, void *din, unsigned long flags)
178 struct udevice *bus = dev->parent;
179 struct pl022_spi_slave *ps = dev_get_priv(bus);
180 u32 len_tx = 0, len_rx = 0, len;
182 const u8 *txp = dout;
183 u8 *rxp = din, value;
186 /* Finish any previously submitted transfers */
190 * TODO: The controller can do non-multiple-of-8 bit
191 * transfers, but this driver currently doesn't support it.
193 * It's also not clear how such transfers are supposed to be
194 * represented as a stream of bytes...this is a limitation of
195 * the current SPI interface.
198 /* Errors always terminate an ongoing transfer */
199 flags |= SPI_XFER_END;
205 while (len_tx < len) {
206 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_TNF) {
207 value = txp ? *txp++ : 0;
208 writew(value, ps->base + SSP_DR);
212 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
213 value = readw(ps->base + SSP_DR);
220 while (len_rx < len_tx) {
221 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
222 value = readw(ps->base + SSP_DR);
232 static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
234 return rate / (cpsdvsr * (1 + scr));
237 static int pl022_spi_set_speed(struct udevice *bus, uint speed)
239 struct pl022_spi_slave *ps = dev_get_priv(bus);
240 u16 scr = SSP_SCR_MIN, cr0 = 0, cpsr = SSP_CPSR_MIN, best_scr = scr,
242 u32 min, max, best_freq = 0, tmp;
243 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
244 u32 rate = clk_get_rate(&ps->clk);
250 max = spi_rate(rate, SSP_CPSR_MIN, SSP_SCR_MIN);
251 min = spi_rate(rate, SSP_CPSR_MAX, SSP_SCR_MAX);
253 if (speed > max || speed < min) {
254 pr_err("Tried to set speed to %dHz but min=%d and max=%d\n",
259 while (cpsr <= SSP_CPSR_MAX && !found) {
260 while (scr <= SSP_SCR_MAX) {
261 tmp = spi_rate(rate, cpsr, scr);
263 if (abs(speed - tmp) < abs(speed - best_freq)) {
280 writew(best_cpsr, ps->base + SSP_CPSR);
281 cr0 = readw(ps->base + SSP_CR0);
282 writew(cr0 | (best_scr << SSP_SCR_SHFT), ps->base + SSP_CR0);
287 static int pl022_spi_set_mode(struct udevice *bus, uint mode)
289 struct pl022_spi_slave *ps = dev_get_priv(bus);
292 reg = readw(ps->base + SSP_CR0);
293 reg &= ~(SSP_CR0_SPH | SSP_CR0_SPO);
298 writew(reg, ps->base + SSP_CR0);
303 static int pl022_cs_info(struct udevice *bus, uint cs,
304 struct spi_cs_info *info)
309 static const struct dm_spi_ops pl022_spi_ops = {
310 .claim_bus = pl022_spi_claim_bus,
311 .release_bus = pl022_spi_release_bus,
312 .xfer = pl022_spi_xfer,
313 .set_speed = pl022_spi_set_speed,
314 .set_mode = pl022_spi_set_mode,
315 .cs_info = pl022_cs_info,
318 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
319 static const struct udevice_id pl022_spi_ids[] = {
320 { .compatible = "arm,pl022-spi" },
325 U_BOOT_DRIVER(pl022_spi) = {
328 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
329 .of_match = pl022_spi_ids,
331 .ops = &pl022_spi_ops,
332 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
333 .ofdata_to_platdata = pl022_spi_ofdata_to_platdata,
335 .platdata_auto_alloc_size = sizeof(struct pl022_spi_pdata),
336 .priv_auto_alloc_size = sizeof(struct pl022_spi_slave),
337 .probe = pl022_spi_probe,