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/delay.h>
19 DECLARE_GLOBAL_DATA_PTR;
22 #define SSI_CTL_EN BIT(0)
25 #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
26 #define SSI_CKS_CKPHS BIT(14)
27 #define SSI_CKS_CKINIT BIT(13)
28 #define SSI_CKS_CKDLY BIT(12)
30 #define SSI_TXWDS 0x08
31 #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
32 #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
33 #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
35 #define SSI_RXWDS 0x0c
36 #define SSI_RXWDS_RDTF_MASK GENMASK(7, 6)
37 #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
40 #define SSI_FPS_FSPOL BIT(15)
41 #define SSI_FPS_FSTRT BIT(14)
44 #define SSI_SR_BUSY BIT(7)
45 #define SSI_SR_TNF BIT(5)
46 #define SSI_SR_RNE BIT(0)
51 #define SSI_IC_TCIC BIT(4)
52 #define SSI_IC_RCIC BIT(3)
53 #define SSI_IC_RORIC BIT(0)
56 #define SSI_FC_TXFFL BIT(12)
57 #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
58 #define SSI_FC_RXFFL BIT(4)
59 #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
61 #define SSI_XDR 0x24 /* TXDR for write, RXDR for read */
63 #define SSI_FIFO_DEPTH 8U
65 #define SSI_REG_TIMEOUT (CONFIG_SYS_HZ / 100) /* 10 ms */
66 #define SSI_XFER_TIMEOUT (CONFIG_SYS_HZ) /* 1 sec */
68 #define SSI_CLK 50000000 /* internal I/O clock: 50MHz */
70 struct uniphier_spi_platdata {
72 u32 frequency; /* input frequency */
74 uint deactivate_delay_us; /* Delay to wait after deactivate */
75 uint activate_delay_us; /* Delay to wait after activate */
78 struct uniphier_spi_priv {
83 ulong last_transaction_us; /* Time of last transaction end */
86 static void uniphier_spi_enable(struct uniphier_spi_priv *priv, int enable)
90 val = readl(priv->base + SSI_CTL);
95 writel(val, priv->base + SSI_CTL);
98 static void uniphier_spi_regdump(struct uniphier_spi_priv *priv)
100 pr_debug("CTL %08x\n", readl(priv->base + SSI_CTL));
101 pr_debug("CKS %08x\n", readl(priv->base + SSI_CKS));
102 pr_debug("TXWDS %08x\n", readl(priv->base + SSI_TXWDS));
103 pr_debug("RXWDS %08x\n", readl(priv->base + SSI_RXWDS));
104 pr_debug("FPS %08x\n", readl(priv->base + SSI_FPS));
105 pr_debug("SR %08x\n", readl(priv->base + SSI_SR));
106 pr_debug("IE %08x\n", readl(priv->base + SSI_IE));
107 pr_debug("IC %08x\n", readl(priv->base + SSI_IC));
108 pr_debug("FC %08x\n", readl(priv->base + SSI_FC));
109 pr_debug("XDR %08x\n", readl(priv->base + SSI_XDR));
112 static void spi_cs_activate(struct udevice *dev)
114 struct udevice *bus = dev->parent;
115 struct uniphier_spi_platdata *plat = bus->platdata;
116 struct uniphier_spi_priv *priv = dev_get_priv(bus);
117 ulong delay_us; /* The delay completed so far */
120 /* If it's too soon to do another transaction, wait */
121 if (plat->deactivate_delay_us && priv->last_transaction_us) {
122 delay_us = timer_get_us() - priv->last_transaction_us;
123 if (delay_us < plat->deactivate_delay_us)
124 udelay(plat->deactivate_delay_us - delay_us);
127 val = readl(priv->base + SSI_FPS);
128 if (priv->mode & SPI_CS_HIGH)
129 val |= SSI_FPS_FSPOL;
131 val &= ~SSI_FPS_FSPOL;
132 writel(val, priv->base + SSI_FPS);
134 if (plat->activate_delay_us)
135 udelay(plat->activate_delay_us);
138 static void spi_cs_deactivate(struct udevice *dev)
140 struct udevice *bus = dev->parent;
141 struct uniphier_spi_platdata *plat = bus->platdata;
142 struct uniphier_spi_priv *priv = dev_get_priv(bus);
145 val = readl(priv->base + SSI_FPS);
146 if (priv->mode & SPI_CS_HIGH)
147 val &= ~SSI_FPS_FSPOL;
149 val |= SSI_FPS_FSPOL;
150 writel(val, priv->base + SSI_FPS);
152 /* Remember time of this transaction so we can honour the bus delay */
153 if (plat->deactivate_delay_us)
154 priv->last_transaction_us = timer_get_us();
157 static int uniphier_spi_claim_bus(struct udevice *dev)
159 struct udevice *bus = dev->parent;
160 struct uniphier_spi_priv *priv = dev_get_priv(bus);
163 uniphier_spi_enable(priv, false);
165 /* disable interrupts */
166 writel(0, priv->base + SSI_IE);
169 size = priv->bits_per_word;
170 val = readl(priv->base + SSI_TXWDS);
171 val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
172 val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
173 val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
174 writel(val, priv->base + SSI_TXWDS);
176 val = readl(priv->base + SSI_RXWDS);
177 val &= ~SSI_RXWDS_DTLEN_MASK;
178 val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
179 writel(val, priv->base + SSI_RXWDS);
182 val = SSI_FC_TXFFL | SSI_FC_RXFFL;
183 writel(val, priv->base + SSI_FC);
186 val = readl(priv->base + SSI_FC);
187 val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
188 val |= FIELD_PREP(SSI_FC_TXFTH_MASK, priv->fifo_depth);
189 val |= FIELD_PREP(SSI_FC_RXFTH_MASK, priv->fifo_depth);
190 writel(val, priv->base + SSI_FC);
192 /* clear interrupts */
193 writel(SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC,
194 priv->base + SSI_IC);
196 uniphier_spi_enable(priv, true);
201 static int uniphier_spi_release_bus(struct udevice *dev)
203 struct udevice *bus = dev->parent;
204 struct uniphier_spi_priv *priv = dev_get_priv(bus);
206 uniphier_spi_enable(priv, false);
211 static int uniphier_spi_xfer(struct udevice *dev, unsigned int bitlen,
212 const void *dout, void *din, unsigned long flags)
214 struct udevice *bus = dev->parent;
215 struct uniphier_spi_priv *priv = dev_get_priv(bus);
216 const u8 *tx_buf = dout;
217 u8 *rx_buf = din, buf;
218 u32 len = bitlen / 8;
224 dev_err(dev, "Non byte aligned SPI transfer\n");
228 if (flags & SPI_XFER_BEGIN)
229 spi_cs_activate(dev);
231 uniphier_spi_enable(priv, true);
237 uniphier_spi_regdump(priv);
239 while (tx_len || rx_len) {
240 ret = wait_for_bit_le32(priv->base + SSI_SR, SSI_SR_BUSY, false,
241 SSI_REG_TIMEOUT * 1000, false);
243 if (ret == -ETIMEDOUT)
244 dev_err(dev, "access timeout\n");
248 status = readl(priv->base + SSI_SR);
249 /* write the data into TX */
250 if (tx_len && (status & SSI_SR_TNF)) {
251 buf = tx_buf ? *tx_buf++ : 0;
252 writel(buf, priv->base + SSI_XDR);
256 /* read the data from RX */
257 if (rx_len && (status & SSI_SR_RNE)) {
258 buf = readl(priv->base + SSI_XDR);
264 if (get_timer(ts) >= SSI_XFER_TIMEOUT) {
265 dev_err(dev, "transfer timeout\n");
271 if (flags & SPI_XFER_END)
272 spi_cs_deactivate(dev);
274 uniphier_spi_enable(priv, false);
279 static int uniphier_spi_set_speed(struct udevice *bus, uint speed)
281 struct uniphier_spi_platdata *plat = bus->platdata;
282 struct uniphier_spi_priv *priv = dev_get_priv(bus);
285 if (speed > plat->frequency)
286 speed = plat->frequency;
289 ckdiv = DIV_ROUND_UP(SSI_CLK, speed);
290 ckdiv = round_up(ckdiv, 2);
292 val = readl(priv->base + SSI_CKS);
293 val &= ~SSI_CKS_CKRAT_MASK;
294 val |= ckdiv & SSI_CKS_CKRAT_MASK;
295 writel(val, priv->base + SSI_CKS);
300 static int uniphier_spi_set_mode(struct udevice *bus, uint mode)
302 struct uniphier_spi_priv *priv = dev_get_priv(bus);
307 * CKPHS capture timing. 0:rising edge, 1:falling edge
308 * CKINIT clock initial level. 0:low, 1:high
309 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
310 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
313 * FSPOL frame signal porarity. 0: low, 1: high
314 * FSTRT start frame timing
315 * 0: rising edge of clock, 1: falling edge of clock
317 val1 = readl(priv->base + SSI_CKS);
318 val2 = readl(priv->base + SSI_FPS);
320 switch (mode & (SPI_CPOL | SPI_CPHA)) {
322 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
323 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKDLY;
324 val1 &= ~SSI_CKS_CKINIT;
325 val2 &= ~SSI_FPS_FSTRT;
328 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
329 val1 &= ~(SSI_CKS_CKPHS | SSI_CKS_CKINIT | SSI_CKS_CKDLY);
330 val2 |= SSI_FPS_FSTRT;
333 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
334 val1 |= SSI_CKS_CKINIT | SSI_CKS_CKDLY;
335 val1 &= ~SSI_CKS_CKPHS;
336 val2 |= SSI_FPS_FSTRT;
339 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
340 val1 |= SSI_CKS_CKPHS | SSI_CKS_CKINIT;
341 val1 &= ~SSI_CKS_CKDLY;
342 val2 &= ~SSI_FPS_FSTRT;
346 writel(val1, priv->base + SSI_CKS);
347 writel(val2, priv->base + SSI_FPS);
350 val1 = readl(priv->base + SSI_TXWDS);
351 val2 = readl(priv->base + SSI_RXWDS);
352 if (mode & SPI_LSB_FIRST) {
353 val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
354 val2 |= FIELD_PREP(SSI_RXWDS_RDTF_MASK, 1);
356 writel(val1, priv->base + SSI_TXWDS);
357 writel(val2, priv->base + SSI_RXWDS);
364 static int uniphier_spi_ofdata_to_platdata(struct udevice *bus)
366 struct uniphier_spi_platdata *plat = bus->platdata;
367 const void *blob = gd->fdt_blob;
368 int node = dev_of_offset(bus);
370 plat->base = devfdt_get_addr_ptr(bus);
373 fdtdec_get_int(blob, node, "spi-max-frequency", 12500000);
374 plat->deactivate_delay_us =
375 fdtdec_get_int(blob, node, "spi-deactivate-delay", 0);
376 plat->activate_delay_us =
377 fdtdec_get_int(blob, node, "spi-activate-delay", 0);
378 plat->speed_hz = plat->frequency / 2;
383 static int uniphier_spi_probe(struct udevice *bus)
385 struct uniphier_spi_platdata *plat = dev_get_platdata(bus);
386 struct uniphier_spi_priv *priv = dev_get_priv(bus);
388 priv->base = plat->base;
389 priv->fifo_depth = SSI_FIFO_DEPTH;
390 priv->bits_per_word = 8;
395 static const struct dm_spi_ops uniphier_spi_ops = {
396 .claim_bus = uniphier_spi_claim_bus,
397 .release_bus = uniphier_spi_release_bus,
398 .xfer = uniphier_spi_xfer,
399 .set_speed = uniphier_spi_set_speed,
400 .set_mode = uniphier_spi_set_mode,
403 static const struct udevice_id uniphier_spi_ids[] = {
404 { .compatible = "socionext,uniphier-scssi" },
408 U_BOOT_DRIVER(uniphier_spi) = {
409 .name = "uniphier_spi",
411 .of_match = uniphier_spi_ids,
412 .ops = &uniphier_spi_ops,
413 .ofdata_to_platdata = uniphier_spi_ofdata_to_platdata,
414 .platdata_auto_alloc_size = sizeof(struct uniphier_spi_platdata),
415 .priv_auto_alloc_size = sizeof(struct uniphier_spi_priv),
416 .probe = uniphier_spi_probe,