1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2014 STMicroelectronics Limited
5 * Author: Angus Clark <Angus.Clark@st.com>
6 * Patrice Chotard <patrice.chotard@st.com>
7 * Lee Jones <lee.jones@linaro.org>
9 * SPI master mode controller driver, used in STMicroelectronics devices.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/consumer.h>
18 #include <linux/platform_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/of_irq.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/spi/spi.h>
24 #include <linux/spi/spi_bitbang.h>
28 #define SSC_TBUF 0x004
29 #define SSC_RBUF 0x008
35 #define SSC_CTL_DATA_WIDTH_9 0x8
36 #define SSC_CTL_DATA_WIDTH_MSK 0xf
37 #define SSC_CTL_BM 0xf
38 #define SSC_CTL_HB BIT(4)
39 #define SSC_CTL_PH BIT(5)
40 #define SSC_CTL_PO BIT(6)
41 #define SSC_CTL_SR BIT(7)
42 #define SSC_CTL_MS BIT(8)
43 #define SSC_CTL_EN BIT(9)
44 #define SSC_CTL_LPB BIT(10)
45 #define SSC_CTL_EN_TX_FIFO BIT(11)
46 #define SSC_CTL_EN_RX_FIFO BIT(12)
47 #define SSC_CTL_EN_CLST_RX BIT(13)
49 /* SSC Interrupt Enable */
50 #define SSC_IEN_TEEN BIT(2)
55 /* SSC SPI Controller */
60 /* SSC SPI current transaction */
64 unsigned int words_remaining
;
66 struct completion done
;
69 /* Load the TX FIFO */
70 static void ssc_write_tx_fifo(struct spi_st
*spi_st
)
72 unsigned int count
, i
;
75 if (spi_st
->words_remaining
> FIFO_SIZE
)
78 count
= spi_st
->words_remaining
;
80 for (i
= 0; i
< count
; i
++) {
82 if (spi_st
->bytes_per_word
== 1) {
83 word
= *spi_st
->tx_ptr
++;
85 word
= *spi_st
->tx_ptr
++;
86 word
= *spi_st
->tx_ptr
++ | (word
<< 8);
89 writel_relaxed(word
, spi_st
->base
+ SSC_TBUF
);
93 /* Read the RX FIFO */
94 static void ssc_read_rx_fifo(struct spi_st
*spi_st
)
96 unsigned int count
, i
;
99 if (spi_st
->words_remaining
> FIFO_SIZE
)
102 count
= spi_st
->words_remaining
;
104 for (i
= 0; i
< count
; i
++) {
105 word
= readl_relaxed(spi_st
->base
+ SSC_RBUF
);
107 if (spi_st
->rx_ptr
) {
108 if (spi_st
->bytes_per_word
== 1) {
109 *spi_st
->rx_ptr
++ = (uint8_t)word
;
111 *spi_st
->rx_ptr
++ = (word
>> 8);
112 *spi_st
->rx_ptr
++ = word
& 0xff;
116 spi_st
->words_remaining
-= count
;
119 static int spi_st_transfer_one(struct spi_master
*master
,
120 struct spi_device
*spi
, struct spi_transfer
*t
)
122 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
126 spi_st
->tx_ptr
= t
->tx_buf
;
127 spi_st
->rx_ptr
= t
->rx_buf
;
129 if (spi
->bits_per_word
> 8) {
131 * Anything greater than 8 bits-per-word requires 2
132 * bytes-per-word in the RX/TX buffers
134 spi_st
->bytes_per_word
= 2;
135 spi_st
->words_remaining
= t
->len
/ 2;
137 } else if (spi
->bits_per_word
== 8 && !(t
->len
& 0x1)) {
139 * If transfer is even-length, and 8 bits-per-word, then
140 * implement as half-length 16 bits-per-word transfer
142 spi_st
->bytes_per_word
= 2;
143 spi_st
->words_remaining
= t
->len
/ 2;
145 /* Set SSC_CTL to 16 bits-per-word */
146 ctl
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
147 writel_relaxed((ctl
| 0xf), spi_st
->base
+ SSC_CTL
);
149 readl_relaxed(spi_st
->base
+ SSC_RBUF
);
152 spi_st
->bytes_per_word
= 1;
153 spi_st
->words_remaining
= t
->len
;
156 reinit_completion(&spi_st
->done
);
158 /* Start transfer by writing to the TX FIFO */
159 ssc_write_tx_fifo(spi_st
);
160 writel_relaxed(SSC_IEN_TEEN
, spi_st
->base
+ SSC_IEN
);
162 /* Wait for transfer to complete */
163 wait_for_completion(&spi_st
->done
);
165 /* Restore SSC_CTL if necessary */
167 writel_relaxed(ctl
, spi_st
->base
+ SSC_CTL
);
169 spi_finalize_current_transfer(spi
->master
);
174 static void spi_st_cleanup(struct spi_device
*spi
)
176 gpio_free(spi
->cs_gpio
);
179 /* the spi->mode bits understood by this driver: */
180 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
181 static int spi_st_setup(struct spi_device
*spi
)
183 struct spi_st
*spi_st
= spi_master_get_devdata(spi
->master
);
184 u32 spi_st_clk
, sscbrg
, var
;
185 u32 hz
= spi
->max_speed_hz
;
186 int cs
= spi
->cs_gpio
;
190 dev_err(&spi
->dev
, "max_speed_hz unspecified\n");
194 if (!gpio_is_valid(cs
)) {
195 dev_err(&spi
->dev
, "%d is not a valid gpio\n", cs
);
199 ret
= gpio_request(cs
, dev_name(&spi
->dev
));
201 dev_err(&spi
->dev
, "could not request gpio:%d\n", cs
);
205 ret
= gpio_direction_output(cs
, spi
->mode
& SPI_CS_HIGH
);
209 spi_st_clk
= clk_get_rate(spi_st
->clk
);
212 sscbrg
= spi_st_clk
/ (2 * hz
);
213 if (sscbrg
< 0x07 || sscbrg
> BIT(16)) {
215 "baudrate %d outside valid range %d\n", sscbrg
, hz
);
220 spi_st
->baud
= spi_st_clk
/ (2 * sscbrg
);
221 if (sscbrg
== BIT(16)) /* 16-bit counter wraps */
224 writel_relaxed(sscbrg
, spi_st
->base
+ SSC_BRG
);
227 "setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
228 hz
, spi_st
->baud
, sscbrg
);
230 /* Set SSC_CTL and enable SSC */
231 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
234 if (spi
->mode
& SPI_CPOL
)
239 if (spi
->mode
& SPI_CPHA
)
244 if ((spi
->mode
& SPI_LSB_FIRST
) == 0)
249 if (spi
->mode
& SPI_LOOP
)
254 var
&= ~SSC_CTL_DATA_WIDTH_MSK
;
255 var
|= (spi
->bits_per_word
- 1);
257 var
|= SSC_CTL_EN_TX_FIFO
| SSC_CTL_EN_RX_FIFO
;
260 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
262 /* Clear the status register */
263 readl_relaxed(spi_st
->base
+ SSC_RBUF
);
272 /* Interrupt fired when TX shift register becomes empty */
273 static irqreturn_t
spi_st_irq(int irq
, void *dev_id
)
275 struct spi_st
*spi_st
= (struct spi_st
*)dev_id
;
278 ssc_read_rx_fifo(spi_st
);
281 if (spi_st
->words_remaining
) {
282 ssc_write_tx_fifo(spi_st
);
285 writel_relaxed(0x0, spi_st
->base
+ SSC_IEN
);
287 * read SSC_IEN to ensure that this bit is set
288 * before re-enabling interrupt
290 readl(spi_st
->base
+ SSC_IEN
);
291 complete(&spi_st
->done
);
297 static int spi_st_probe(struct platform_device
*pdev
)
299 struct device_node
*np
= pdev
->dev
.of_node
;
300 struct spi_master
*master
;
301 struct spi_st
*spi_st
;
305 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spi_st
));
309 master
->dev
.of_node
= np
;
310 master
->mode_bits
= MODEBITS
;
311 master
->setup
= spi_st_setup
;
312 master
->cleanup
= spi_st_cleanup
;
313 master
->transfer_one
= spi_st_transfer_one
;
314 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
315 master
->auto_runtime_pm
= true;
316 master
->bus_num
= pdev
->id
;
317 spi_st
= spi_master_get_devdata(master
);
319 spi_st
->clk
= devm_clk_get(&pdev
->dev
, "ssc");
320 if (IS_ERR(spi_st
->clk
)) {
321 dev_err(&pdev
->dev
, "Unable to request clock\n");
322 ret
= PTR_ERR(spi_st
->clk
);
326 ret
= clk_prepare_enable(spi_st
->clk
);
330 init_completion(&spi_st
->done
);
333 spi_st
->base
= devm_platform_ioremap_resource(pdev
, 0);
334 if (IS_ERR(spi_st
->base
)) {
335 ret
= PTR_ERR(spi_st
->base
);
339 /* Disable I2C and Reset SSC */
340 writel_relaxed(0x0, spi_st
->base
+ SSC_I2C
);
341 var
= readw_relaxed(spi_st
->base
+ SSC_CTL
);
343 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
346 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
348 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
350 /* Set SSC into slave mode before reconfiguring PIO pins */
351 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
353 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
355 irq
= irq_of_parse_and_map(np
, 0);
357 dev_err(&pdev
->dev
, "IRQ missing or invalid\n");
362 ret
= devm_request_irq(&pdev
->dev
, irq
, spi_st_irq
, 0,
365 dev_err(&pdev
->dev
, "Failed to request irq %d\n", irq
);
369 /* by default the device is on */
370 pm_runtime_set_active(&pdev
->dev
);
371 pm_runtime_enable(&pdev
->dev
);
373 platform_set_drvdata(pdev
, master
);
375 ret
= devm_spi_register_master(&pdev
->dev
, master
);
377 dev_err(&pdev
->dev
, "Failed to register master\n");
384 pm_runtime_disable(&pdev
->dev
);
386 clk_disable_unprepare(spi_st
->clk
);
388 spi_master_put(master
);
392 static int spi_st_remove(struct platform_device
*pdev
)
394 struct spi_master
*master
= platform_get_drvdata(pdev
);
395 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
397 pm_runtime_disable(&pdev
->dev
);
399 clk_disable_unprepare(spi_st
->clk
);
401 pinctrl_pm_select_sleep_state(&pdev
->dev
);
407 static int spi_st_runtime_suspend(struct device
*dev
)
409 struct spi_master
*master
= dev_get_drvdata(dev
);
410 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
412 writel_relaxed(0, spi_st
->base
+ SSC_IEN
);
413 pinctrl_pm_select_sleep_state(dev
);
415 clk_disable_unprepare(spi_st
->clk
);
420 static int spi_st_runtime_resume(struct device
*dev
)
422 struct spi_master
*master
= dev_get_drvdata(dev
);
423 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
426 ret
= clk_prepare_enable(spi_st
->clk
);
427 pinctrl_pm_select_default_state(dev
);
433 #ifdef CONFIG_PM_SLEEP
434 static int spi_st_suspend(struct device
*dev
)
436 struct spi_master
*master
= dev_get_drvdata(dev
);
439 ret
= spi_master_suspend(master
);
443 return pm_runtime_force_suspend(dev
);
446 static int spi_st_resume(struct device
*dev
)
448 struct spi_master
*master
= dev_get_drvdata(dev
);
451 ret
= spi_master_resume(master
);
455 return pm_runtime_force_resume(dev
);
459 static const struct dev_pm_ops spi_st_pm
= {
460 SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend
, spi_st_resume
)
461 SET_RUNTIME_PM_OPS(spi_st_runtime_suspend
, spi_st_runtime_resume
, NULL
)
464 static const struct of_device_id stm_spi_match
[] = {
465 { .compatible
= "st,comms-ssc4-spi", },
468 MODULE_DEVICE_TABLE(of
, stm_spi_match
);
470 static struct platform_driver spi_st_driver
= {
474 .of_match_table
= of_match_ptr(stm_spi_match
),
476 .probe
= spi_st_probe
,
477 .remove
= spi_st_remove
,
479 module_platform_driver(spi_st_driver
);
481 MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
482 MODULE_DESCRIPTION("STM SSC SPI driver");
483 MODULE_LICENSE("GPL v2");