2 * Copyright (c) 2008-2014 STMicroelectronics Limited
4 * Author: Angus Clark <Angus.Clark@st.com>
5 * Patrice Chotard <patrice.chotard@st.com>
6 * Lee Jones <lee.jones@linaro.org>
8 * SPI master mode controller driver, used in STMicroelectronics devices.
10 * May be copied or modified under the terms of the GNU General Public
11 * License Version 2.0 only. See linux/COPYING for more information.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
22 #include <linux/of_gpio.h>
23 #include <linux/of_irq.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/spi_bitbang.h>
30 #define SSC_TBUF 0x004
31 #define SSC_RBUF 0x008
37 #define SSC_CTL_DATA_WIDTH_9 0x8
38 #define SSC_CTL_DATA_WIDTH_MSK 0xf
39 #define SSC_CTL_BM 0xf
40 #define SSC_CTL_HB BIT(4)
41 #define SSC_CTL_PH BIT(5)
42 #define SSC_CTL_PO BIT(6)
43 #define SSC_CTL_SR BIT(7)
44 #define SSC_CTL_MS BIT(8)
45 #define SSC_CTL_EN BIT(9)
46 #define SSC_CTL_LPB BIT(10)
47 #define SSC_CTL_EN_TX_FIFO BIT(11)
48 #define SSC_CTL_EN_RX_FIFO BIT(12)
49 #define SSC_CTL_EN_CLST_RX BIT(13)
51 /* SSC Interrupt Enable */
52 #define SSC_IEN_TEEN BIT(2)
57 /* SSC SPI Controller */
62 /* SSC SPI current transaction */
66 unsigned int words_remaining
;
68 struct completion done
;
71 static int spi_st_clk_enable(struct spi_st
*spi_st
)
74 * Current platforms use one of the core clocks for SPI and I2C.
75 * If we attempt to disable the clock, the system will hang.
77 * TODO: Remove this when platform supports power domains.
81 return clk_prepare_enable(spi_st
->clk
);
84 static void spi_st_clk_disable(struct spi_st
*spi_st
)
87 * Current platforms use one of the core clocks for SPI and I2C.
88 * If we attempt to disable the clock, the system will hang.
90 * TODO: Remove this when platform supports power domains.
94 clk_disable_unprepare(spi_st
->clk
);
97 /* Load the TX FIFO */
98 static void ssc_write_tx_fifo(struct spi_st
*spi_st
)
100 unsigned int count
, i
;
103 if (spi_st
->words_remaining
> FIFO_SIZE
)
106 count
= spi_st
->words_remaining
;
108 for (i
= 0; i
< count
; i
++) {
109 if (spi_st
->tx_ptr
) {
110 if (spi_st
->bytes_per_word
== 1) {
111 word
= *spi_st
->tx_ptr
++;
113 word
= *spi_st
->tx_ptr
++;
114 word
= *spi_st
->tx_ptr
++ | (word
<< 8);
117 writel_relaxed(word
, spi_st
->base
+ SSC_TBUF
);
121 /* Read the RX FIFO */
122 static void ssc_read_rx_fifo(struct spi_st
*spi_st
)
124 unsigned int count
, i
;
127 if (spi_st
->words_remaining
> FIFO_SIZE
)
130 count
= spi_st
->words_remaining
;
132 for (i
= 0; i
< count
; i
++) {
133 word
= readl_relaxed(spi_st
->base
+ SSC_RBUF
);
135 if (spi_st
->rx_ptr
) {
136 if (spi_st
->bytes_per_word
== 1) {
137 *spi_st
->rx_ptr
++ = (uint8_t)word
;
139 *spi_st
->rx_ptr
++ = (word
>> 8);
140 *spi_st
->rx_ptr
++ = word
& 0xff;
144 spi_st
->words_remaining
-= count
;
147 static int spi_st_transfer_one(struct spi_master
*master
,
148 struct spi_device
*spi
, struct spi_transfer
*t
)
150 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
154 spi_st
->tx_ptr
= t
->tx_buf
;
155 spi_st
->rx_ptr
= t
->rx_buf
;
157 if (spi
->bits_per_word
> 8) {
159 * Anything greater than 8 bits-per-word requires 2
160 * bytes-per-word in the RX/TX buffers
162 spi_st
->bytes_per_word
= 2;
163 spi_st
->words_remaining
= t
->len
/ 2;
165 } else if (spi
->bits_per_word
== 8 && !(t
->len
& 0x1)) {
167 * If transfer is even-length, and 8 bits-per-word, then
168 * implement as half-length 16 bits-per-word transfer
170 spi_st
->bytes_per_word
= 2;
171 spi_st
->words_remaining
= t
->len
/ 2;
173 /* Set SSC_CTL to 16 bits-per-word */
174 ctl
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
175 writel_relaxed((ctl
| 0xf), spi_st
->base
+ SSC_CTL
);
177 readl_relaxed(spi_st
->base
+ SSC_RBUF
);
180 spi_st
->bytes_per_word
= 1;
181 spi_st
->words_remaining
= t
->len
;
184 reinit_completion(&spi_st
->done
);
186 /* Start transfer by writing to the TX FIFO */
187 ssc_write_tx_fifo(spi_st
);
188 writel_relaxed(SSC_IEN_TEEN
, spi_st
->base
+ SSC_IEN
);
190 /* Wait for transfer to complete */
191 wait_for_completion(&spi_st
->done
);
193 /* Restore SSC_CTL if necessary */
195 writel_relaxed(ctl
, spi_st
->base
+ SSC_CTL
);
197 spi_finalize_current_transfer(spi
->master
);
202 static void spi_st_cleanup(struct spi_device
*spi
)
204 int cs
= spi
->cs_gpio
;
206 if (gpio_is_valid(cs
))
207 devm_gpio_free(&spi
->dev
, cs
);
210 /* the spi->mode bits understood by this driver: */
211 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
212 static int spi_st_setup(struct spi_device
*spi
)
214 struct spi_st
*spi_st
= spi_master_get_devdata(spi
->master
);
215 u32 spi_st_clk
, sscbrg
, var
;
216 u32 hz
= spi
->max_speed_hz
;
217 int cs
= spi
->cs_gpio
;
221 dev_err(&spi
->dev
, "max_speed_hz unspecified\n");
225 if (!gpio_is_valid(cs
)) {
226 dev_err(&spi
->dev
, "%d is not a valid gpio\n", cs
);
230 if (devm_gpio_request(&spi
->dev
, cs
, dev_name(&spi
->dev
))) {
231 dev_err(&spi
->dev
, "could not request gpio:%d\n", cs
);
235 ret
= gpio_direction_output(cs
, spi
->mode
& SPI_CS_HIGH
);
239 spi_st_clk
= clk_get_rate(spi_st
->clk
);
242 sscbrg
= spi_st_clk
/ (2 * hz
);
243 if (sscbrg
< 0x07 || sscbrg
> BIT(16)) {
245 "baudrate %d outside valid range %d\n", sscbrg
, hz
);
249 spi_st
->baud
= spi_st_clk
/ (2 * sscbrg
);
250 if (sscbrg
== BIT(16)) /* 16-bit counter wraps */
253 writel_relaxed(sscbrg
, spi_st
->base
+ SSC_BRG
);
256 "setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
257 hz
, spi_st
->baud
, sscbrg
);
259 /* Set SSC_CTL and enable SSC */
260 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
263 if (spi
->mode
& SPI_CPOL
)
268 if (spi
->mode
& SPI_CPHA
)
273 if ((spi
->mode
& SPI_LSB_FIRST
) == 0)
278 if (spi
->mode
& SPI_LOOP
)
283 var
&= ~SSC_CTL_DATA_WIDTH_MSK
;
284 var
|= (spi
->bits_per_word
- 1);
286 var
|= SSC_CTL_EN_TX_FIFO
| SSC_CTL_EN_RX_FIFO
;
289 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
291 /* Clear the status register */
292 readl_relaxed(spi_st
->base
+ SSC_RBUF
);
297 /* Interrupt fired when TX shift register becomes empty */
298 static irqreturn_t
spi_st_irq(int irq
, void *dev_id
)
300 struct spi_st
*spi_st
= (struct spi_st
*)dev_id
;
303 ssc_read_rx_fifo(spi_st
);
306 if (spi_st
->words_remaining
) {
307 ssc_write_tx_fifo(spi_st
);
310 writel_relaxed(0x0, spi_st
->base
+ SSC_IEN
);
312 * read SSC_IEN to ensure that this bit is set
313 * before re-enabling interrupt
315 readl(spi_st
->base
+ SSC_IEN
);
316 complete(&spi_st
->done
);
322 static int spi_st_probe(struct platform_device
*pdev
)
324 struct device_node
*np
= pdev
->dev
.of_node
;
325 struct spi_master
*master
;
326 struct resource
*res
;
327 struct spi_st
*spi_st
;
331 master
= spi_alloc_master(&pdev
->dev
, sizeof(*spi_st
));
335 master
->dev
.of_node
= np
;
336 master
->mode_bits
= MODEBITS
;
337 master
->setup
= spi_st_setup
;
338 master
->cleanup
= spi_st_cleanup
;
339 master
->transfer_one
= spi_st_transfer_one
;
340 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
341 master
->auto_runtime_pm
= true;
342 master
->bus_num
= pdev
->id
;
343 spi_st
= spi_master_get_devdata(master
);
345 spi_st
->clk
= devm_clk_get(&pdev
->dev
, "ssc");
346 if (IS_ERR(spi_st
->clk
)) {
347 dev_err(&pdev
->dev
, "Unable to request clock\n");
348 return PTR_ERR(spi_st
->clk
);
351 ret
= spi_st_clk_enable(spi_st
);
355 init_completion(&spi_st
->done
);
358 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
359 spi_st
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
360 if (IS_ERR(spi_st
->base
)) {
361 ret
= PTR_ERR(spi_st
->base
);
365 /* Disable I2C and Reset SSC */
366 writel_relaxed(0x0, spi_st
->base
+ SSC_I2C
);
367 var
= readw_relaxed(spi_st
->base
+ SSC_CTL
);
369 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
372 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
374 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
376 /* Set SSC into slave mode before reconfiguring PIO pins */
377 var
= readl_relaxed(spi_st
->base
+ SSC_CTL
);
379 writel_relaxed(var
, spi_st
->base
+ SSC_CTL
);
381 irq
= irq_of_parse_and_map(np
, 0);
383 dev_err(&pdev
->dev
, "IRQ missing or invalid\n");
388 ret
= devm_request_irq(&pdev
->dev
, irq
, spi_st_irq
, 0,
391 dev_err(&pdev
->dev
, "Failed to request irq %d\n", irq
);
395 /* by default the device is on */
396 pm_runtime_set_active(&pdev
->dev
);
397 pm_runtime_enable(&pdev
->dev
);
399 platform_set_drvdata(pdev
, master
);
401 ret
= devm_spi_register_master(&pdev
->dev
, master
);
403 dev_err(&pdev
->dev
, "Failed to register master\n");
410 spi_st_clk_disable(spi_st
);
415 static int spi_st_remove(struct platform_device
*pdev
)
417 struct spi_master
*master
= platform_get_drvdata(pdev
);
418 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
420 spi_st_clk_disable(spi_st
);
422 pinctrl_pm_select_sleep_state(&pdev
->dev
);
428 static int spi_st_runtime_suspend(struct device
*dev
)
430 struct spi_master
*master
= dev_get_drvdata(dev
);
431 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
433 writel_relaxed(0, spi_st
->base
+ SSC_IEN
);
434 pinctrl_pm_select_sleep_state(dev
);
436 spi_st_clk_disable(spi_st
);
441 static int spi_st_runtime_resume(struct device
*dev
)
443 struct spi_master
*master
= dev_get_drvdata(dev
);
444 struct spi_st
*spi_st
= spi_master_get_devdata(master
);
447 ret
= spi_st_clk_enable(spi_st
);
448 pinctrl_pm_select_default_state(dev
);
454 #ifdef CONFIG_PM_SLEEP
455 static int spi_st_suspend(struct device
*dev
)
457 struct spi_master
*master
= dev_get_drvdata(dev
);
460 ret
= spi_master_suspend(master
);
464 return pm_runtime_force_suspend(dev
);
467 static int spi_st_resume(struct device
*dev
)
469 struct spi_master
*master
= dev_get_drvdata(dev
);
472 ret
= spi_master_resume(master
);
476 return pm_runtime_force_resume(dev
);
480 static const struct dev_pm_ops spi_st_pm
= {
481 SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend
, spi_st_resume
)
482 SET_RUNTIME_PM_OPS(spi_st_runtime_suspend
, spi_st_runtime_resume
, NULL
)
485 static const struct of_device_id stm_spi_match
[] = {
486 { .compatible
= "st,comms-ssc4-spi", },
489 MODULE_DEVICE_TABLE(of
, stm_spi_match
);
491 static struct platform_driver spi_st_driver
= {
495 .of_match_table
= of_match_ptr(stm_spi_match
),
497 .probe
= spi_st_probe
,
498 .remove
= spi_st_remove
,
500 module_platform_driver(spi_st_driver
);
502 MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
503 MODULE_DESCRIPTION("STM SSC SPI driver");
504 MODULE_LICENSE("GPL v2");