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_irq.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/spi/spi.h>
23 #include <linux/spi/spi_bitbang.h>
27 #define SSC_TBUF 0x004
28 #define SSC_RBUF 0x008
34 #define SSC_CTL_DATA_WIDTH_9 0x8
35 #define SSC_CTL_DATA_WIDTH_MSK 0xf
36 #define SSC_CTL_BM 0xf
37 #define SSC_CTL_HB BIT(4)
38 #define SSC_CTL_PH BIT(5)
39 #define SSC_CTL_PO BIT(6)
40 #define SSC_CTL_SR BIT(7)
41 #define SSC_CTL_MS BIT(8)
42 #define SSC_CTL_EN BIT(9)
43 #define SSC_CTL_LPB BIT(10)
44 #define SSC_CTL_EN_TX_FIFO BIT(11)
45 #define SSC_CTL_EN_RX_FIFO BIT(12)
46 #define SSC_CTL_EN_CLST_RX BIT(13)
48 /* SSC Interrupt Enable */
49 #define SSC_IEN_TEEN BIT(2)
54 /* SSC SPI Controller */
59 /* SSC SPI current transaction */
63 unsigned int words_remaining;
65 struct completion done;
68 /* Load the TX FIFO */
69 static void ssc_write_tx_fifo(struct spi_st *spi_st)
71 unsigned int count, i;
74 if (spi_st->words_remaining > FIFO_SIZE)
77 count = spi_st->words_remaining;
79 for (i = 0; i < count; i++) {
81 if (spi_st->bytes_per_word == 1) {
82 word = *spi_st->tx_ptr++;
84 word = *spi_st->tx_ptr++;
85 word = *spi_st->tx_ptr++ | (word << 8);
88 writel_relaxed(word, spi_st->base + SSC_TBUF);
92 /* Read the RX FIFO */
93 static void ssc_read_rx_fifo(struct spi_st *spi_st)
95 unsigned int count, i;
98 if (spi_st->words_remaining > FIFO_SIZE)
101 count = spi_st->words_remaining;
103 for (i = 0; i < count; i++) {
104 word = readl_relaxed(spi_st->base + SSC_RBUF);
106 if (spi_st->rx_ptr) {
107 if (spi_st->bytes_per_word == 1) {
108 *spi_st->rx_ptr++ = (uint8_t)word;
110 *spi_st->rx_ptr++ = (word >> 8);
111 *spi_st->rx_ptr++ = word & 0xff;
115 spi_st->words_remaining -= count;
118 static int spi_st_transfer_one(struct spi_master *master,
119 struct spi_device *spi, struct spi_transfer *t)
121 struct spi_st *spi_st = spi_master_get_devdata(master);
125 spi_st->tx_ptr = t->tx_buf;
126 spi_st->rx_ptr = t->rx_buf;
128 if (spi->bits_per_word > 8) {
130 * Anything greater than 8 bits-per-word requires 2
131 * bytes-per-word in the RX/TX buffers
133 spi_st->bytes_per_word = 2;
134 spi_st->words_remaining = t->len / 2;
136 } else if (spi->bits_per_word == 8 && !(t->len & 0x1)) {
138 * If transfer is even-length, and 8 bits-per-word, then
139 * implement as half-length 16 bits-per-word transfer
141 spi_st->bytes_per_word = 2;
142 spi_st->words_remaining = t->len / 2;
144 /* Set SSC_CTL to 16 bits-per-word */
145 ctl = readl_relaxed(spi_st->base + SSC_CTL);
146 writel_relaxed((ctl | 0xf), spi_st->base + SSC_CTL);
148 readl_relaxed(spi_st->base + SSC_RBUF);
151 spi_st->bytes_per_word = 1;
152 spi_st->words_remaining = t->len;
155 reinit_completion(&spi_st->done);
157 /* Start transfer by writing to the TX FIFO */
158 ssc_write_tx_fifo(spi_st);
159 writel_relaxed(SSC_IEN_TEEN, spi_st->base + SSC_IEN);
161 /* Wait for transfer to complete */
162 wait_for_completion(&spi_st->done);
164 /* Restore SSC_CTL if necessary */
166 writel_relaxed(ctl, spi_st->base + SSC_CTL);
168 spi_finalize_current_transfer(spi->master);
173 /* the spi->mode bits understood by this driver: */
174 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
175 static int spi_st_setup(struct spi_device *spi)
177 struct spi_st *spi_st = spi_master_get_devdata(spi->master);
178 u32 spi_st_clk, sscbrg, var;
179 u32 hz = spi->max_speed_hz;
182 dev_err(&spi->dev, "max_speed_hz unspecified\n");
186 if (!spi_get_csgpiod(spi, 0)) {
187 dev_err(&spi->dev, "no valid gpio assigned\n");
191 spi_st_clk = clk_get_rate(spi_st->clk);
194 sscbrg = spi_st_clk / (2 * hz);
195 if (sscbrg < 0x07 || sscbrg > BIT(16)) {
197 "baudrate %d outside valid range %d\n", sscbrg, hz);
201 spi_st->baud = spi_st_clk / (2 * sscbrg);
202 if (sscbrg == BIT(16)) /* 16-bit counter wraps */
205 writel_relaxed(sscbrg, spi_st->base + SSC_BRG);
208 "setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
209 hz, spi_st->baud, sscbrg);
211 /* Set SSC_CTL and enable SSC */
212 var = readl_relaxed(spi_st->base + SSC_CTL);
215 if (spi->mode & SPI_CPOL)
220 if (spi->mode & SPI_CPHA)
225 if ((spi->mode & SPI_LSB_FIRST) == 0)
230 if (spi->mode & SPI_LOOP)
235 var &= ~SSC_CTL_DATA_WIDTH_MSK;
236 var |= (spi->bits_per_word - 1);
238 var |= SSC_CTL_EN_TX_FIFO | SSC_CTL_EN_RX_FIFO;
241 writel_relaxed(var, spi_st->base + SSC_CTL);
243 /* Clear the status register */
244 readl_relaxed(spi_st->base + SSC_RBUF);
249 /* Interrupt fired when TX shift register becomes empty */
250 static irqreturn_t spi_st_irq(int irq, void *dev_id)
252 struct spi_st *spi_st = (struct spi_st *)dev_id;
255 ssc_read_rx_fifo(spi_st);
258 if (spi_st->words_remaining) {
259 ssc_write_tx_fifo(spi_st);
262 writel_relaxed(0x0, spi_st->base + SSC_IEN);
264 * read SSC_IEN to ensure that this bit is set
265 * before re-enabling interrupt
267 readl(spi_st->base + SSC_IEN);
268 complete(&spi_st->done);
274 static int spi_st_probe(struct platform_device *pdev)
276 struct device_node *np = pdev->dev.of_node;
277 struct spi_master *master;
278 struct spi_st *spi_st;
282 master = spi_alloc_master(&pdev->dev, sizeof(*spi_st));
286 master->dev.of_node = np;
287 master->mode_bits = MODEBITS;
288 master->setup = spi_st_setup;
289 master->transfer_one = spi_st_transfer_one;
290 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
291 master->auto_runtime_pm = true;
292 master->bus_num = pdev->id;
293 master->use_gpio_descriptors = true;
294 spi_st = spi_master_get_devdata(master);
296 spi_st->clk = devm_clk_get(&pdev->dev, "ssc");
297 if (IS_ERR(spi_st->clk)) {
298 dev_err(&pdev->dev, "Unable to request clock\n");
299 ret = PTR_ERR(spi_st->clk);
303 ret = clk_prepare_enable(spi_st->clk);
307 init_completion(&spi_st->done);
310 spi_st->base = devm_platform_ioremap_resource(pdev, 0);
311 if (IS_ERR(spi_st->base)) {
312 ret = PTR_ERR(spi_st->base);
316 /* Disable I2C and Reset SSC */
317 writel_relaxed(0x0, spi_st->base + SSC_I2C);
318 var = readw_relaxed(spi_st->base + SSC_CTL);
320 writel_relaxed(var, spi_st->base + SSC_CTL);
323 var = readl_relaxed(spi_st->base + SSC_CTL);
325 writel_relaxed(var, spi_st->base + SSC_CTL);
327 /* Set SSC into slave mode before reconfiguring PIO pins */
328 var = readl_relaxed(spi_st->base + SSC_CTL);
330 writel_relaxed(var, spi_st->base + SSC_CTL);
332 irq = irq_of_parse_and_map(np, 0);
334 dev_err(&pdev->dev, "IRQ missing or invalid\n");
339 ret = devm_request_irq(&pdev->dev, irq, spi_st_irq, 0,
342 dev_err(&pdev->dev, "Failed to request irq %d\n", irq);
346 /* by default the device is on */
347 pm_runtime_set_active(&pdev->dev);
348 pm_runtime_enable(&pdev->dev);
350 platform_set_drvdata(pdev, master);
352 ret = devm_spi_register_master(&pdev->dev, master);
354 dev_err(&pdev->dev, "Failed to register master\n");
361 pm_runtime_disable(&pdev->dev);
363 clk_disable_unprepare(spi_st->clk);
365 spi_master_put(master);
369 static void spi_st_remove(struct platform_device *pdev)
371 struct spi_master *master = platform_get_drvdata(pdev);
372 struct spi_st *spi_st = spi_master_get_devdata(master);
374 pm_runtime_disable(&pdev->dev);
376 clk_disable_unprepare(spi_st->clk);
378 pinctrl_pm_select_sleep_state(&pdev->dev);
382 static int spi_st_runtime_suspend(struct device *dev)
384 struct spi_master *master = dev_get_drvdata(dev);
385 struct spi_st *spi_st = spi_master_get_devdata(master);
387 writel_relaxed(0, spi_st->base + SSC_IEN);
388 pinctrl_pm_select_sleep_state(dev);
390 clk_disable_unprepare(spi_st->clk);
395 static int spi_st_runtime_resume(struct device *dev)
397 struct spi_master *master = dev_get_drvdata(dev);
398 struct spi_st *spi_st = spi_master_get_devdata(master);
401 ret = clk_prepare_enable(spi_st->clk);
402 pinctrl_pm_select_default_state(dev);
408 #ifdef CONFIG_PM_SLEEP
409 static int spi_st_suspend(struct device *dev)
411 struct spi_master *master = dev_get_drvdata(dev);
414 ret = spi_master_suspend(master);
418 return pm_runtime_force_suspend(dev);
421 static int spi_st_resume(struct device *dev)
423 struct spi_master *master = dev_get_drvdata(dev);
426 ret = spi_master_resume(master);
430 return pm_runtime_force_resume(dev);
434 static const struct dev_pm_ops spi_st_pm = {
435 SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend, spi_st_resume)
436 SET_RUNTIME_PM_OPS(spi_st_runtime_suspend, spi_st_runtime_resume, NULL)
439 static const struct of_device_id stm_spi_match[] = {
440 { .compatible = "st,comms-ssc4-spi", },
443 MODULE_DEVICE_TABLE(of, stm_spi_match);
445 static struct platform_driver spi_st_driver = {
449 .of_match_table = of_match_ptr(stm_spi_match),
451 .probe = spi_st_probe,
452 .remove_new = spi_st_remove,
454 module_platform_driver(spi_st_driver);
456 MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
457 MODULE_DESCRIPTION("STM SSC SPI driver");
458 MODULE_LICENSE("GPL v2");