1 // SPDX-License-Identifier: GPL-2.0
3 // SPI controller driver for Qualcomm Atheros AR934x/QCA95xx SoCs
5 // Copyright (C) 2020 Chuanhong Guo <gch981213@gmail.com>
7 // Based on spi-mt7621.c:
8 // Copyright (C) 2011 Sergiy <piratfm@gmail.com>
9 // Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
10 // Copyright (C) 2014-2015 Felix Fietkau <nbd@nbd.name>
12 #include <linux/clk.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/spi/spi.h>
20 #define DRIVER_NAME "spi-ar934x"
22 #define AR934X_SPI_REG_FS 0x00
23 #define AR934X_SPI_ENABLE BIT(0)
25 #define AR934X_SPI_REG_IOC 0x08
26 #define AR934X_SPI_IOC_INITVAL 0x70000
28 #define AR934X_SPI_REG_CTRL 0x04
29 #define AR934X_SPI_CLK_MASK GENMASK(5, 0)
31 #define AR934X_SPI_DATAOUT 0x10
33 #define AR934X_SPI_REG_SHIFT_CTRL 0x14
34 #define AR934X_SPI_SHIFT_EN BIT(31)
35 #define AR934X_SPI_SHIFT_CS(n) BIT(28 + (n))
36 #define AR934X_SPI_SHIFT_TERM 26
37 #define AR934X_SPI_SHIFT_VAL(cs, term, count) \
38 (AR934X_SPI_SHIFT_EN | AR934X_SPI_SHIFT_CS(cs) | \
39 (term) << AR934X_SPI_SHIFT_TERM | (count))
41 #define AR934X_SPI_DATAIN 0x18
44 struct spi_controller *ctlr;
47 unsigned int clk_freq;
50 static inline int ar934x_spi_clk_div(struct ar934x_spi *sp, unsigned int freq)
52 int div = DIV_ROUND_UP(sp->clk_freq, freq * 2) - 1;
56 else if (div > AR934X_SPI_CLK_MASK)
62 static int ar934x_spi_setup(struct spi_device *spi)
64 struct ar934x_spi *sp = spi_controller_get_devdata(spi->master);
66 if ((spi->max_speed_hz == 0) ||
67 (spi->max_speed_hz > (sp->clk_freq / 2))) {
68 spi->max_speed_hz = sp->clk_freq / 2;
69 } else if (spi->max_speed_hz < (sp->clk_freq / 128)) {
70 dev_err(&spi->dev, "spi clock is too low\n");
77 static int ar934x_spi_transfer_one_message(struct spi_controller *master,
78 struct spi_message *m)
80 struct ar934x_spi *sp = spi_controller_get_devdata(master);
81 struct spi_transfer *t = NULL;
82 struct spi_device *spi = m->spi;
83 unsigned long trx_done, trx_cur;
92 list_for_each_entry(t, &m->transfers, transfer_list) {
93 if (t->bits_per_word >= 8 && t->bits_per_word < 32)
94 bpw = t->bits_per_word >> 3;
99 div = ar934x_spi_clk_div(sp, t->speed_hz);
101 div = ar934x_spi_clk_div(sp, spi->max_speed_hz);
107 reg = ioread32(sp->base + AR934X_SPI_REG_CTRL);
108 reg &= ~AR934X_SPI_CLK_MASK;
110 iowrite32(reg, sp->base + AR934X_SPI_REG_CTRL);
111 iowrite32(0, sp->base + AR934X_SPI_DATAOUT);
113 for (trx_done = 0; trx_done < t->len; trx_done += bpw) {
114 trx_cur = t->len - trx_done;
117 else if (list_is_last(&t->transfer_list, &m->transfers))
121 tx_buf = t->tx_buf + trx_done;
123 for (i = 1; i < trx_cur; i++)
124 reg = reg << 8 | tx_buf[i];
125 iowrite32(reg, sp->base + AR934X_SPI_DATAOUT);
128 reg = AR934X_SPI_SHIFT_VAL(spi->chip_select, term,
130 iowrite32(reg, sp->base + AR934X_SPI_REG_SHIFT_CTRL);
131 stat = readl_poll_timeout(
132 sp->base + AR934X_SPI_REG_SHIFT_CTRL, reg,
133 !(reg & AR934X_SPI_SHIFT_EN), 0, 5);
138 reg = ioread32(sp->base + AR934X_SPI_DATAIN);
139 buf = t->rx_buf + trx_done;
140 for (i = 0; i < trx_cur; i++) {
141 buf[trx_cur - i - 1] = reg & 0xff;
145 spi_delay_exec(&t->word_delay, t);
147 m->actual_length += t->len;
148 spi_transfer_delay_exec(t);
153 spi_finalize_current_message(master);
158 static const struct of_device_id ar934x_spi_match[] = {
159 { .compatible = "qca,ar934x-spi" },
162 MODULE_DEVICE_TABLE(of, ar934x_spi_match);
164 static int ar934x_spi_probe(struct platform_device *pdev)
166 struct spi_controller *ctlr;
167 struct ar934x_spi *sp;
172 base = devm_platform_ioremap_resource(pdev, 0);
174 return PTR_ERR(base);
176 clk = devm_clk_get(&pdev->dev, NULL);
178 dev_err(&pdev->dev, "failed to get clock\n");
182 ret = clk_prepare_enable(clk);
186 ctlr = devm_spi_alloc_master(&pdev->dev, sizeof(*sp));
188 dev_info(&pdev->dev, "failed to allocate spi controller\n");
190 goto err_clk_disable;
193 /* disable flash mapping and expose spi controller registers */
194 iowrite32(AR934X_SPI_ENABLE, base + AR934X_SPI_REG_FS);
195 /* restore pins to default state: CSn=1 DO=CLK=0 */
196 iowrite32(AR934X_SPI_IOC_INITVAL, base + AR934X_SPI_REG_IOC);
198 ctlr->mode_bits = SPI_LSB_FIRST;
199 ctlr->setup = ar934x_spi_setup;
200 ctlr->transfer_one_message = ar934x_spi_transfer_one_message;
201 ctlr->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(24) |
202 SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
203 ctlr->dev.of_node = pdev->dev.of_node;
204 ctlr->num_chipselect = 3;
206 dev_set_drvdata(&pdev->dev, ctlr);
208 sp = spi_controller_get_devdata(ctlr);
211 sp->clk_freq = clk_get_rate(clk);
214 ret = spi_register_controller(ctlr);
219 clk_disable_unprepare(clk);
223 static int ar934x_spi_remove(struct platform_device *pdev)
225 struct spi_controller *ctlr;
226 struct ar934x_spi *sp;
228 ctlr = dev_get_drvdata(&pdev->dev);
229 sp = spi_controller_get_devdata(ctlr);
231 spi_unregister_controller(ctlr);
232 clk_disable_unprepare(sp->clk);
237 static struct platform_driver ar934x_spi_driver = {
240 .of_match_table = ar934x_spi_match,
242 .probe = ar934x_spi_probe,
243 .remove = ar934x_spi_remove,
246 module_platform_driver(ar934x_spi_driver);
248 MODULE_DESCRIPTION("SPI controller driver for Qualcomm Atheros AR934x/QCA95xx");
249 MODULE_AUTHOR("Chuanhong Guo <gch981213@gmail.com>");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:" DRIVER_NAME);