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.
15 #include <dm/platform_data/spi_pl022.h>
23 #define SSP_CPSR 0x010
24 #define SSP_IMSC 0x014
28 #define SSP_DMACR 0x024
29 #define SSP_CSR 0x030 /* vendor extension */
30 #define SSP_ITCR 0x080
31 #define SSP_ITIP 0x084
32 #define SSP_ITOP 0x088
35 #define SSP_PID0 0xFE0
36 #define SSP_PID1 0xFE4
37 #define SSP_PID2 0xFE8
38 #define SSP_PID3 0xFEC
40 #define SSP_CID0 0xFF0
41 #define SSP_CID1 0xFF4
42 #define SSP_CID2 0xFF8
43 #define SSP_CID3 0xFFC
45 /* SSP Control Register 0 - SSP_CR0 */
46 #define SSP_CR0_SPO (0x1 << 6)
47 #define SSP_CR0_SPH (0x1 << 7)
48 #define SSP_CR0_BIT_MODE(x) ((x) - 1)
49 #define SSP_SCR_MIN (0x00)
50 #define SSP_SCR_MAX (0xFF)
51 #define SSP_SCR_SHFT 8
52 #define DFLT_CLKRATE 2
54 /* SSP Control Register 1 - SSP_CR1 */
55 #define SSP_CR1_MASK_SSE (0x1 << 1)
57 #define SSP_CPSR_MIN (0x02)
58 #define SSP_CPSR_MAX (0xFE)
59 #define DFLT_PRESCALE (0x40)
61 /* SSP Status Register - SSP_SR */
62 #define SSP_SR_MASK_TFE (0x1 << 0) /* Transmit FIFO empty */
63 #define SSP_SR_MASK_TNF (0x1 << 1) /* Transmit FIFO not full */
64 #define SSP_SR_MASK_RNE (0x1 << 2) /* Receive FIFO not empty */
65 #define SSP_SR_MASK_RFF (0x1 << 3) /* Receive FIFO full */
66 #define SSP_SR_MASK_BSY (0x1 << 4) /* Busy Flag */
68 struct pl022_spi_slave {
74 * ARM PL022 exists in different 'flavors'.
75 * This drivers currently support the standard variant (0x00041022), that has a
76 * 16bit wide and 8 locations deep TX/RX FIFO.
78 static int pl022_is_supported(struct pl022_spi_slave *ps)
80 /* PL022 version is 0x00041022 */
81 if ((readw(ps->base + SSP_PID0) == 0x22) &&
82 (readw(ps->base + SSP_PID1) == 0x10) &&
83 ((readw(ps->base + SSP_PID2) & 0xf) == 0x04) &&
84 (readw(ps->base + SSP_PID3) == 0x00))
90 static int pl022_spi_probe(struct udevice *bus)
92 struct pl022_spi_pdata *plat = dev_get_platdata(bus);
93 struct pl022_spi_slave *ps = dev_get_priv(bus);
95 ps->base = ioremap(plat->addr, plat->size);
96 ps->freq = plat->freq;
98 /* Check the PL022 version */
99 if (!pl022_is_supported(ps))
102 /* 8 bits per word, high polarity and default clock rate */
103 writew(SSP_CR0_BIT_MODE(8), ps->base + SSP_CR0);
104 writew(DFLT_PRESCALE, ps->base + SSP_CPSR);
109 static void flush(struct pl022_spi_slave *ps)
112 while (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE)
113 readw(ps->base + SSP_DR);
114 } while (readw(ps->base + SSP_SR) & SSP_SR_MASK_BSY);
117 static int pl022_spi_claim_bus(struct udevice *dev)
119 struct udevice *bus = dev->parent;
120 struct pl022_spi_slave *ps = dev_get_priv(bus);
123 /* Enable the SPI hardware */
124 reg = readw(ps->base + SSP_CR1);
125 reg |= SSP_CR1_MASK_SSE;
126 writew(reg, ps->base + SSP_CR1);
133 static int pl022_spi_release_bus(struct udevice *dev)
135 struct udevice *bus = dev->parent;
136 struct pl022_spi_slave *ps = dev_get_priv(bus);
141 /* Disable the SPI hardware */
142 reg = readw(ps->base + SSP_CR1);
143 reg &= ~SSP_CR1_MASK_SSE;
144 writew(reg, ps->base + SSP_CR1);
149 static int pl022_spi_xfer(struct udevice *dev, unsigned int bitlen,
150 const void *dout, void *din, unsigned long flags)
152 struct udevice *bus = dev->parent;
153 struct pl022_spi_slave *ps = dev_get_priv(bus);
154 u32 len_tx = 0, len_rx = 0, len;
156 const u8 *txp = dout;
157 u8 *rxp = din, value;
160 /* Finish any previously submitted transfers */
164 * TODO: The controller can do non-multiple-of-8 bit
165 * transfers, but this driver currently doesn't support it.
167 * It's also not clear how such transfers are supposed to be
168 * represented as a stream of bytes...this is a limitation of
169 * the current SPI interface.
172 /* Errors always terminate an ongoing transfer */
173 flags |= SPI_XFER_END;
179 while (len_tx < len) {
180 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_TNF) {
181 value = txp ? *txp++ : 0;
182 writew(value, ps->base + SSP_DR);
186 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
187 value = readw(ps->base + SSP_DR);
194 while (len_rx < len_tx) {
195 if (readw(ps->base + SSP_SR) & SSP_SR_MASK_RNE) {
196 value = readw(ps->base + SSP_DR);
206 static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
208 return rate / (cpsdvsr * (1 + scr));
211 static int pl022_spi_set_speed(struct udevice *bus, uint speed)
213 struct pl022_spi_slave *ps = dev_get_priv(bus);
214 u16 scr = SSP_SCR_MIN, cr0 = 0, cpsr = SSP_CPSR_MIN, best_scr = scr,
216 u32 min, max, best_freq = 0, tmp;
220 max = spi_rate(rate, SSP_CPSR_MIN, SSP_SCR_MIN);
221 min = spi_rate(rate, SSP_CPSR_MAX, SSP_SCR_MAX);
223 if (speed > max || speed < min) {
224 pr_err("Tried to set speed to %dHz but min=%d and max=%d\n",
229 while (cpsr <= SSP_CPSR_MAX && !found) {
230 while (scr <= SSP_SCR_MAX) {
231 tmp = spi_rate(rate, cpsr, scr);
233 if (abs(speed - tmp) < abs(speed - best_freq)) {
250 writew(best_cpsr, ps->base + SSP_CPSR);
251 cr0 = readw(ps->base + SSP_CR0);
252 writew(cr0 | (best_scr << SSP_SCR_SHFT), ps->base + SSP_CR0);
257 static int pl022_spi_set_mode(struct udevice *bus, uint mode)
259 struct pl022_spi_slave *ps = dev_get_priv(bus);
262 reg = readw(ps->base + SSP_CR0);
263 reg &= ~(SSP_CR0_SPH | SSP_CR0_SPO);
268 writew(reg, ps->base + SSP_CR0);
273 static int pl022_cs_info(struct udevice *bus, uint cs,
274 struct spi_cs_info *info)
279 static const struct dm_spi_ops pl022_spi_ops = {
280 .claim_bus = pl022_spi_claim_bus,
281 .release_bus = pl022_spi_release_bus,
282 .xfer = pl022_spi_xfer,
283 .set_speed = pl022_spi_set_speed,
284 .set_mode = pl022_spi_set_mode,
285 .cs_info = pl022_cs_info,
288 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
289 static int pl022_spi_ofdata_to_platdata(struct udevice *bus)
291 struct pl022_spi_pdata *plat = bus->platdata;
292 const void *fdt = gd->fdt_blob;
293 int node = dev_of_offset(bus);
297 plat->addr = fdtdec_get_addr_size(fdt, node, "reg", &plat->size);
299 ret = clk_get_by_index(bus, 0, &clkdev);
303 plat->freq = clk_get_rate(&clkdev);
308 static const struct udevice_id pl022_spi_ids[] = {
309 { .compatible = "arm,pl022-spi" },
314 U_BOOT_DRIVER(pl022_spi) = {
317 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
318 .of_match = pl022_spi_ids,
319 .ofdata_to_platdata = pl022_spi_ofdata_to_platdata,
321 .ops = &pl022_spi_ops,
322 .platdata_auto_alloc_size = sizeof(struct pl022_spi_pdata),
323 .priv_auto_alloc_size = sizeof(struct pl022_spi_slave),
324 .probe = pl022_spi_probe,