1 // SPDX-License-Identifier: GPL-2.0+
3 * uniphier_spi.c - Socionext UniPhier SPI driver
4 * Copyright 2019 Socionext, Inc.
12 #include <dm/device_compat.h>
13 #include <linux/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 #define SSI_CTL_EN BIT(0)
26 #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
27 #define SSI_CKS_CKPHS BIT(14)
28 #define SSI_CKS_CKINIT BIT(13)
29 #define SSI_CKS_CKDLY BIT(12)
31 #define SSI_TXWDS 0x08
32 #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
33 #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
34 #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
36 #define SSI_RXWDS 0x0c
37 #define SSI_RXWDS_RDTF_MASK GENMASK(7, 6)
38 #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
41 #define SSI_FPS_FSPOL BIT(15)
42 #define SSI_FPS_FSTRT BIT(14)
45 #define SSI_SR_BUSY BIT(7)
46 #define SSI_SR_TNF BIT(5)
47 #define SSI_SR_RNE BIT(0)
52 #define SSI_IC_TCIC BIT(4)
53 #define SSI_IC_RCIC BIT(3)
54 #define SSI_IC_RORIC BIT(0)
57 #define SSI_FC_TXFFL BIT(12)
58 #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
59 #define SSI_FC_RXFFL BIT(4)
60 #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
62 #define SSI_XDR 0x24 /* TXDR for write, RXDR for read */
64 #define SSI_FIFO_DEPTH 8U
66 #define SSI_REG_TIMEOUT (CONFIG_SYS_HZ / 100) /* 10 ms */
67 #define SSI_XFER_TIMEOUT (CONFIG_SYS_HZ) /* 1 sec */
69 #define SSI_CLK 50000000 /* internal I/O clock: 50MHz */
71 struct uniphier_spi_platdata {
73 u32 frequency; /* input frequency */
75 uint deactivate_delay_us; /* Delay to wait after deactivate */
76 uint activate_delay_us; /* Delay to wait after activate */
79 struct uniphier_spi_priv {
84 ulong last_transaction_us; /* Time of last transaction end */
87 static void uniphier_spi_enable(struct uniphier_spi_priv *priv, int enable)
91 val = readl(priv->base + SSI_CTL);
96 writel(val, priv->base + SSI_CTL);
99 static void uniphier_spi_regdump(struct uniphier_spi_priv *priv)
101 pr_debug("CTL %08x\n", readl(priv->base + SSI_CTL));
102 pr_debug("CKS %08x\n", readl(priv->base + SSI_CKS));
103 pr_debug("TXWDS %08x\n", readl(priv->base + SSI_TXWDS));
104 pr_debug("RXWDS %08x\n", readl(priv->base + SSI_RXWDS));
105 pr_debug("FPS %08x\n", readl(priv->base + SSI_FPS));
106 pr_debug("SR %08x\n", readl(priv->base + SSI_SR));
107 pr_debug("IE %08x\n", readl(priv->base + SSI_IE));
108 pr_debug("IC %08x\n", readl(priv->base + SSI_IC));
109 pr_debug("FC %08x\n", readl(priv->base + SSI_FC));
110 pr_debug("XDR %08x\n", readl(priv->base + SSI_XDR));
113 static void spi_cs_activate(struct udevice *dev)
115 struct udevice *bus = dev->parent;
116 struct uniphier_spi_platdata *plat = bus->platdata;
117 struct uniphier_spi_priv *priv = dev_get_priv(bus);
118 ulong delay_us; /* The delay completed so far */
121 /* If it's too soon to do another transaction, wait */
122 if (plat->deactivate_delay_us && priv->last_transaction_us) {
123 delay_us = timer_get_us() - priv->last_transaction_us;
124 if (delay_us < plat->deactivate_delay_us)
125 udelay(plat->deactivate_delay_us - delay_us);
128 val = readl(priv->base + SSI_FPS);
129 if (priv->mode & SPI_CS_HIGH)
130 val |= SSI_FPS_FSPOL;
132 val &= ~SSI_FPS_FSPOL;
133 writel(val, priv->base + SSI_FPS);
135 if (plat->activate_delay_us)
136 udelay(plat->activate_delay_us);
139 static void spi_cs_deactivate(struct udevice *dev)
141 struct udevice *bus = dev->parent;
142 struct uniphier_spi_platdata *plat = bus->platdata;
143 struct uniphier_spi_priv *priv = dev_get_priv(bus);
146 val = readl(priv->base + SSI_FPS);
147 if (priv->mode & SPI_CS_HIGH)
148 val &= ~SSI_FPS_FSPOL;
150 val |= SSI_FPS_FSPOL;
151 writel(val, priv->base + SSI_FPS);
153 /* Remember time of this transaction so we can honour the bus delay */
154 if (plat->deactivate_delay_us)
155 priv->last_transaction_us = timer_get_us();
158 static int uniphier_spi_claim_bus(struct udevice *dev)
160 struct udevice *bus = dev->parent;
161 struct uniphier_spi_priv *priv = dev_get_priv(bus);
164 uniphier_spi_enable(priv, false);
166 /* disable interrupts */
167 writel(0, priv->base + SSI_IE);
170 size = priv->bits_per_word;
171 val = readl(priv->base + SSI_TXWDS);
172 val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
173 val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
174 val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
175 writel(val, priv->base + SSI_TXWDS);
177 val = readl(priv->base + SSI_RXWDS);
178 val &= ~SSI_RXWDS_DTLEN_MASK;
179 val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
180 writel(val, priv->base + SSI_RXWDS);
183 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
184 writel(val, priv->base + SSI_FC);
187 val = readl(priv->base + SSI_FC);
188 val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
189 val |= FIELD_PREP(SSI_FC_TXFTH_MASK, priv->fifo_depth);
190 val |= FIELD_PREP(SSI_FC_RXFTH_MASK, priv->fifo_depth);
191 writel(val, priv->base + SSI_FC);
193 /* clear interrupts */
194 writel(SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC,
195 priv->base + SSI_IC);
197 uniphier_spi_enable(priv, true);
202 static int uniphier_spi_release_bus(struct udevice *dev)
204 struct udevice *bus = dev->parent;
205 struct uniphier_spi_priv *priv = dev_get_priv(bus);
207 uniphier_spi_enable(priv, false);
212 static int uniphier_spi_xfer(struct udevice *dev, unsigned int bitlen,
213 const void *dout, void *din, unsigned long flags)
215 struct udevice *bus = dev->parent;
216 struct uniphier_spi_priv *priv = dev_get_priv(bus);
217 const u8 *tx_buf = dout;
218 u8 *rx_buf = din, buf;
219 u32 len = bitlen / 8;
225 dev_err(dev, "Non byte aligned SPI transfer\n");
229 if (flags & SPI_XFER_BEGIN)
230 spi_cs_activate(dev);
232 uniphier_spi_enable(priv, true);
238 uniphier_spi_regdump(priv);
240 while (tx_len || rx_len) {
241 ret = wait_for_bit_le32(priv->base + SSI_SR, SSI_SR_BUSY, false,
242 SSI_REG_TIMEOUT * 1000, false);
244 if (ret == -ETIMEDOUT)
245 dev_err(dev, "access timeout\n");
249 status = readl(priv->base + SSI_SR);
250 /* write the data into TX */
251 if (tx_len && (status & SSI_SR_TNF)) {
252 buf = tx_buf ? *tx_buf++ : 0;
253 writel(buf, priv->base + SSI_XDR);
257 /* read the data from RX */
258 if (rx_len && (status & SSI_SR_RNE)) {
259 buf = readl(priv->base + SSI_XDR);
265 if (get_timer(ts) >= SSI_XFER_TIMEOUT) {
266 dev_err(dev, "transfer timeout\n");
272 if (flags & SPI_XFER_END)
273 spi_cs_deactivate(dev);
275 uniphier_spi_enable(priv, false);
280 static int uniphier_spi_set_speed(struct udevice *bus, uint speed)
282 struct uniphier_spi_platdata *plat = bus->platdata;
283 struct uniphier_spi_priv *priv = dev_get_priv(bus);
286 if (speed > plat->frequency)
287 speed = plat->frequency;
290 ckdiv = DIV_ROUND_UP(SSI_CLK, speed);
291 ckdiv = round_up(ckdiv, 2);
293 val = readl(priv->base + SSI_CKS);
294 val &= ~SSI_CKS_CKRAT_MASK;
295 val |= ckdiv & SSI_CKS_CKRAT_MASK;
296 writel(val, priv->base + SSI_CKS);
301 static int uniphier_spi_set_mode(struct udevice *bus, uint mode)
303 struct uniphier_spi_priv *priv = dev_get_priv(bus);
308 * CKPHS capture timing. 0:rising edge, 1:falling edge
309 * CKINIT clock initial level. 0:low, 1:high
310 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
311 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
314 * FSPOL frame signal porarity. 0: low, 1: high
315 * FSTRT start frame timing
316 * 0: rising edge of clock, 1: falling edge of clock
318 val1 = readl(priv->base + SSI_CKS);
319 val2 = readl(priv->base + SSI_FPS);
321 switch (mode & (SPI_CPOL | SPI_CPHA)) {
323 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
324 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKDLY;
325 val1 &= ~SSI_CKS_CKINIT;
326 val2 &= ~SSI_FPS_FSTRT;
329 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
330 val1 &= ~(SSI_CKS_CKPHS | SSI_CKS_CKINIT | SSI_CKS_CKDLY);
331 val2 |= SSI_FPS_FSTRT;
334 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
335 val1 |= SSI_CKS_CKINIT | SSI_CKS_CKDLY;
336 val1 &= ~SSI_CKS_CKPHS;
337 val2 |= SSI_FPS_FSTRT;
340 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
341 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKINIT;
342 val1 &= ~SSI_CKS_CKDLY;
343 val2 &= ~SSI_FPS_FSTRT;
347 writel(val1, priv->base + SSI_CKS);
348 writel(val2, priv->base + SSI_FPS);
351 val1 = readl(priv->base + SSI_TXWDS);
352 val2 = readl(priv->base + SSI_RXWDS);
353 if (mode & SPI_LSB_FIRST) {
354 val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
355 val2 |= FIELD_PREP(SSI_RXWDS_RDTF_MASK, 1);
357 writel(val1, priv->base + SSI_TXWDS);
358 writel(val2, priv->base + SSI_RXWDS);
365 static int uniphier_spi_ofdata_to_platdata(struct udevice *bus)
367 struct uniphier_spi_platdata *plat = bus->platdata;
368 const void *blob = gd->fdt_blob;
369 int node = dev_of_offset(bus);
371 plat->base = devfdt_get_addr_ptr(bus);
374 fdtdec_get_int(blob, node, "spi-max-frequency", 12500000);
375 plat->deactivate_delay_us =
376 fdtdec_get_int(blob, node, "spi-deactivate-delay", 0);
377 plat->activate_delay_us =
378 fdtdec_get_int(blob, node, "spi-activate-delay", 0);
379 plat->speed_hz = plat->frequency / 2;
384 static int uniphier_spi_probe(struct udevice *bus)
386 struct uniphier_spi_platdata *plat = dev_get_platdata(bus);
387 struct uniphier_spi_priv *priv = dev_get_priv(bus);
389 priv->base = plat->base;
390 priv->fifo_depth = SSI_FIFO_DEPTH;
391 priv->bits_per_word = 8;
396 static const struct dm_spi_ops uniphier_spi_ops = {
397 .claim_bus = uniphier_spi_claim_bus,
398 .release_bus = uniphier_spi_release_bus,
399 .xfer = uniphier_spi_xfer,
400 .set_speed = uniphier_spi_set_speed,
401 .set_mode = uniphier_spi_set_mode,
404 static const struct udevice_id uniphier_spi_ids[] = {
405 { .compatible = "socionext,uniphier-scssi" },
409 U_BOOT_DRIVER(uniphier_spi) = {
410 .name = "uniphier_spi",
412 .of_match = uniphier_spi_ids,
413 .ops = &uniphier_spi_ops,
414 .ofdata_to_platdata = uniphier_spi_ofdata_to_platdata,
415 .platdata_auto_alloc_size = sizeof(struct uniphier_spi_platdata),
416 .priv_auto_alloc_size = sizeof(struct uniphier_spi_priv),
417 .probe = uniphier_spi_probe,