1 // SPDX-License-Identifier: GPL-2.0
2 // spi-uniphier.c - Socionext UniPhier SPI controller driver
3 // Copyright 2012 Panasonic Corporation
4 // Copyright 2016-2018 Socionext Inc.
6 #include <linux/kernel.h>
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
10 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/spi/spi.h>
16 #include <asm/unaligned.h>
18 #define SSI_TIMEOUT_MS 2000
19 #define SSI_MAX_CLK_DIVIDER 254
20 #define SSI_MIN_CLK_DIVIDER 4
22 struct uniphier_spi_priv
{
25 struct spi_master
*master
;
26 struct completion xfer_done
;
29 unsigned int tx_bytes
;
30 unsigned int rx_bytes
;
41 #define SSI_CTL_EN BIT(0)
44 #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
45 #define SSI_CKS_CKPHS BIT(14)
46 #define SSI_CKS_CKINIT BIT(13)
47 #define SSI_CKS_CKDLY BIT(12)
49 #define SSI_TXWDS 0x08
50 #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
51 #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
52 #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
54 #define SSI_RXWDS 0x0c
55 #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
58 #define SSI_FPS_FSPOL BIT(15)
59 #define SSI_FPS_FSTRT BIT(14)
62 #define SSI_SR_RNE BIT(0)
65 #define SSI_IE_RCIE BIT(3)
66 #define SSI_IE_RORIE BIT(0)
69 #define SSI_IS_RXRS BIT(9)
70 #define SSI_IS_RCID BIT(3)
71 #define SSI_IS_RORID BIT(0)
74 #define SSI_IC_TCIC BIT(4)
75 #define SSI_IC_RCIC BIT(3)
76 #define SSI_IC_RORIC BIT(0)
79 #define SSI_FC_TXFFL BIT(12)
80 #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
81 #define SSI_FC_RXFFL BIT(4)
82 #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
87 #define SSI_FIFO_DEPTH 8U
89 static inline unsigned int bytes_per_word(unsigned int bits
)
91 return bits
<= 8 ? 1 : (bits
<= 16 ? 2 : 4);
94 static inline void uniphier_spi_irq_enable(struct spi_device
*spi
, u32 mask
)
96 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
99 val
= readl(priv
->base
+ SSI_IE
);
101 writel(val
, priv
->base
+ SSI_IE
);
104 static inline void uniphier_spi_irq_disable(struct spi_device
*spi
, u32 mask
)
106 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
109 val
= readl(priv
->base
+ SSI_IE
);
111 writel(val
, priv
->base
+ SSI_IE
);
114 static void uniphier_spi_set_mode(struct spi_device
*spi
)
116 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
121 * CKPHS capture timing. 0:rising edge, 1:falling edge
122 * CKINIT clock initial level. 0:low, 1:high
123 * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
124 * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
127 * FSPOL frame signal porarity. 0: low, 1: high
128 * FSTRT start frame timing
129 * 0: rising edge of clock, 1: falling edge of clock
131 switch (spi
->mode
& (SPI_CPOL
| SPI_CPHA
)) {
133 /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
134 val1
= SSI_CKS_CKPHS
| SSI_CKS_CKDLY
;
138 /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
140 val2
= SSI_FPS_FSTRT
;
143 /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
144 val1
= SSI_CKS_CKINIT
| SSI_CKS_CKDLY
;
145 val2
= SSI_FPS_FSTRT
;
148 /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
149 val1
= SSI_CKS_CKPHS
| SSI_CKS_CKINIT
;
154 if (!(spi
->mode
& SPI_CS_HIGH
))
155 val2
|= SSI_FPS_FSPOL
;
157 writel(val1
, priv
->base
+ SSI_CKS
);
158 writel(val2
, priv
->base
+ SSI_FPS
);
161 if (spi
->mode
& SPI_LSB_FIRST
)
162 val1
|= FIELD_PREP(SSI_TXWDS_TDTF_MASK
, 1);
163 writel(val1
, priv
->base
+ SSI_TXWDS
);
164 writel(val1
, priv
->base
+ SSI_RXWDS
);
167 static void uniphier_spi_set_transfer_size(struct spi_device
*spi
, int size
)
169 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
172 val
= readl(priv
->base
+ SSI_TXWDS
);
173 val
&= ~(SSI_TXWDS_WDLEN_MASK
| SSI_TXWDS_DTLEN_MASK
);
174 val
|= FIELD_PREP(SSI_TXWDS_WDLEN_MASK
, size
);
175 val
|= FIELD_PREP(SSI_TXWDS_DTLEN_MASK
, size
);
176 writel(val
, priv
->base
+ SSI_TXWDS
);
178 val
= readl(priv
->base
+ SSI_RXWDS
);
179 val
&= ~SSI_RXWDS_DTLEN_MASK
;
180 val
|= FIELD_PREP(SSI_RXWDS_DTLEN_MASK
, size
);
181 writel(val
, priv
->base
+ SSI_RXWDS
);
184 static void uniphier_spi_set_baudrate(struct spi_device
*spi
,
187 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
191 * the supported rates are even numbers from 4 to 254. (4,6,8...254)
192 * round up as we look for equal or less speed
194 ckdiv
= DIV_ROUND_UP(clk_get_rate(priv
->clk
), speed
);
195 ckdiv
= round_up(ckdiv
, 2);
197 val
= readl(priv
->base
+ SSI_CKS
);
198 val
&= ~SSI_CKS_CKRAT_MASK
;
199 val
|= ckdiv
& SSI_CKS_CKRAT_MASK
;
200 writel(val
, priv
->base
+ SSI_CKS
);
203 static void uniphier_spi_setup_transfer(struct spi_device
*spi
,
204 struct spi_transfer
*t
)
206 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
210 priv
->tx_buf
= t
->tx_buf
;
211 priv
->rx_buf
= t
->rx_buf
;
212 priv
->tx_bytes
= priv
->rx_bytes
= t
->len
;
214 if (!priv
->is_save_param
|| priv
->mode
!= spi
->mode
) {
215 uniphier_spi_set_mode(spi
);
216 priv
->mode
= spi
->mode
;
219 if (!priv
->is_save_param
|| priv
->bits_per_word
!= t
->bits_per_word
) {
220 uniphier_spi_set_transfer_size(spi
, t
->bits_per_word
);
221 priv
->bits_per_word
= t
->bits_per_word
;
224 if (!priv
->is_save_param
|| priv
->speed_hz
!= t
->speed_hz
) {
225 uniphier_spi_set_baudrate(spi
, t
->speed_hz
);
226 priv
->speed_hz
= t
->speed_hz
;
229 if (!priv
->is_save_param
)
230 priv
->is_save_param
= true;
233 val
= SSI_FC_TXFFL
| SSI_FC_RXFFL
;
234 writel(val
, priv
->base
+ SSI_FC
);
237 static void uniphier_spi_send(struct uniphier_spi_priv
*priv
)
242 wsize
= min(bytes_per_word(priv
->bits_per_word
), priv
->tx_bytes
);
243 priv
->tx_bytes
-= wsize
;
251 val
= get_unaligned_le16(priv
->tx_buf
);
254 val
= get_unaligned_le32(priv
->tx_buf
);
258 priv
->tx_buf
+= wsize
;
261 writel(val
, priv
->base
+ SSI_TXDR
);
264 static void uniphier_spi_recv(struct uniphier_spi_priv
*priv
)
269 rsize
= min(bytes_per_word(priv
->bits_per_word
), priv
->rx_bytes
);
270 priv
->rx_bytes
-= rsize
;
272 val
= readl(priv
->base
+ SSI_RXDR
);
280 put_unaligned_le16(val
, priv
->rx_buf
);
283 put_unaligned_le32(val
, priv
->rx_buf
);
287 priv
->rx_buf
+= rsize
;
291 static void uniphier_spi_fill_tx_fifo(struct uniphier_spi_priv
*priv
)
293 unsigned int tx_count
;
296 tx_count
= DIV_ROUND_UP(priv
->tx_bytes
,
297 bytes_per_word(priv
->bits_per_word
));
298 tx_count
= min(tx_count
, SSI_FIFO_DEPTH
);
300 /* set fifo threshold */
301 val
= readl(priv
->base
+ SSI_FC
);
302 val
&= ~(SSI_FC_TXFTH_MASK
| SSI_FC_RXFTH_MASK
);
303 val
|= FIELD_PREP(SSI_FC_TXFTH_MASK
, tx_count
);
304 val
|= FIELD_PREP(SSI_FC_RXFTH_MASK
, tx_count
);
305 writel(val
, priv
->base
+ SSI_FC
);
308 uniphier_spi_send(priv
);
311 static void uniphier_spi_set_cs(struct spi_device
*spi
, bool enable
)
313 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(spi
->master
);
316 val
= readl(priv
->base
+ SSI_FPS
);
319 val
|= SSI_FPS_FSPOL
;
321 val
&= ~SSI_FPS_FSPOL
;
323 writel(val
, priv
->base
+ SSI_FPS
);
326 static int uniphier_spi_transfer_one(struct spi_master
*master
,
327 struct spi_device
*spi
,
328 struct spi_transfer
*t
)
330 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(master
);
333 uniphier_spi_setup_transfer(spi
, t
);
335 reinit_completion(&priv
->xfer_done
);
337 uniphier_spi_fill_tx_fifo(priv
);
339 uniphier_spi_irq_enable(spi
, SSI_IE_RCIE
| SSI_IE_RORIE
);
341 status
= wait_for_completion_timeout(&priv
->xfer_done
,
342 msecs_to_jiffies(SSI_TIMEOUT_MS
));
344 uniphier_spi_irq_disable(spi
, SSI_IE_RCIE
| SSI_IE_RORIE
);
352 static int uniphier_spi_prepare_transfer_hardware(struct spi_master
*master
)
354 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(master
);
356 writel(SSI_CTL_EN
, priv
->base
+ SSI_CTL
);
361 static int uniphier_spi_unprepare_transfer_hardware(struct spi_master
*master
)
363 struct uniphier_spi_priv
*priv
= spi_master_get_devdata(master
);
365 writel(0, priv
->base
+ SSI_CTL
);
370 static irqreturn_t
uniphier_spi_handler(int irq
, void *dev_id
)
372 struct uniphier_spi_priv
*priv
= dev_id
;
375 stat
= readl(priv
->base
+ SSI_IS
);
376 val
= SSI_IC_TCIC
| SSI_IC_RCIC
| SSI_IC_RORIC
;
377 writel(val
, priv
->base
+ SSI_IC
);
379 /* rx fifo overrun */
380 if (stat
& SSI_IS_RORID
) {
386 if ((stat
& SSI_IS_RCID
) && (stat
& SSI_IS_RXRS
)) {
387 while ((readl(priv
->base
+ SSI_SR
) & SSI_SR_RNE
) &&
388 (priv
->rx_bytes
- priv
->tx_bytes
) > 0)
389 uniphier_spi_recv(priv
);
391 if ((readl(priv
->base
+ SSI_SR
) & SSI_SR_RNE
) ||
392 (priv
->rx_bytes
!= priv
->tx_bytes
)) {
395 } else if (priv
->rx_bytes
== 0)
398 /* next tx transfer */
399 uniphier_spi_fill_tx_fifo(priv
);
407 complete(&priv
->xfer_done
);
411 static int uniphier_spi_probe(struct platform_device
*pdev
)
413 struct uniphier_spi_priv
*priv
;
414 struct spi_master
*master
;
415 struct resource
*res
;
416 unsigned long clk_rate
;
420 master
= spi_alloc_master(&pdev
->dev
, sizeof(*priv
));
424 platform_set_drvdata(pdev
, master
);
426 priv
= spi_master_get_devdata(master
);
427 priv
->master
= master
;
428 priv
->is_save_param
= false;
430 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
431 priv
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
432 if (IS_ERR(priv
->base
)) {
433 ret
= PTR_ERR(priv
->base
);
437 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
438 if (IS_ERR(priv
->clk
)) {
439 dev_err(&pdev
->dev
, "failed to get clock\n");
440 ret
= PTR_ERR(priv
->clk
);
444 ret
= clk_prepare_enable(priv
->clk
);
448 irq
= platform_get_irq(pdev
, 0);
450 dev_err(&pdev
->dev
, "failed to get IRQ\n");
452 goto out_disable_clk
;
455 ret
= devm_request_irq(&pdev
->dev
, irq
, uniphier_spi_handler
,
456 0, "uniphier-spi", priv
);
458 dev_err(&pdev
->dev
, "failed to request IRQ\n");
459 goto out_disable_clk
;
462 init_completion(&priv
->xfer_done
);
464 clk_rate
= clk_get_rate(priv
->clk
);
466 master
->max_speed_hz
= DIV_ROUND_UP(clk_rate
, SSI_MIN_CLK_DIVIDER
);
467 master
->min_speed_hz
= DIV_ROUND_UP(clk_rate
, SSI_MAX_CLK_DIVIDER
);
468 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
| SPI_LSB_FIRST
;
469 master
->dev
.of_node
= pdev
->dev
.of_node
;
470 master
->bus_num
= pdev
->id
;
471 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(1, 32);
473 master
->set_cs
= uniphier_spi_set_cs
;
474 master
->transfer_one
= uniphier_spi_transfer_one
;
475 master
->prepare_transfer_hardware
476 = uniphier_spi_prepare_transfer_hardware
;
477 master
->unprepare_transfer_hardware
478 = uniphier_spi_unprepare_transfer_hardware
;
479 master
->num_chipselect
= 1;
481 ret
= devm_spi_register_master(&pdev
->dev
, master
);
483 goto out_disable_clk
;
488 clk_disable_unprepare(priv
->clk
);
491 spi_master_put(master
);
495 static int uniphier_spi_remove(struct platform_device
*pdev
)
497 struct uniphier_spi_priv
*priv
= platform_get_drvdata(pdev
);
499 clk_disable_unprepare(priv
->clk
);
504 static const struct of_device_id uniphier_spi_match
[] = {
505 { .compatible
= "socionext,uniphier-scssi" },
508 MODULE_DEVICE_TABLE(of
, uniphier_spi_match
);
510 static struct platform_driver uniphier_spi_driver
= {
511 .probe
= uniphier_spi_probe
,
512 .remove
= uniphier_spi_remove
,
514 .name
= "uniphier-spi",
515 .of_match_table
= uniphier_spi_match
,
518 module_platform_driver(uniphier_spi_driver
);
520 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
521 MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
522 MODULE_DESCRIPTION("Socionext UniPhier SPI controller driver");
523 MODULE_LICENSE("GPL v2");