blob: 09c4fb7fcf7a85fba94c616337d7907659543630 [file] [log] [blame]
Gabor Juhos8efaef42011-01-04 21:28:22 +01001/*
2 * SPI controller driver for the Atheros AR71XX/AR724X/AR913X SoCs
3 *
4 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This driver has been based on the spi-gpio.c:
7 * Copyright (C) 2006,2008 David Brownell
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/kernel.h>
Gabor Juhos807cc4b2011-11-16 20:01:43 +010016#include <linux/module.h>
Gabor Juhos8efaef42011-01-04 21:28:22 +010017#include <linux/delay.h>
18#include <linux/spinlock.h>
Gabor Juhos8efaef42011-01-04 21:28:22 +010019#include <linux/platform_device.h>
20#include <linux/io.h>
21#include <linux/spi/spi.h>
22#include <linux/spi/spi_bitbang.h>
23#include <linux/bitops.h>
Gabor Juhos440114f2012-12-27 10:42:24 +010024#include <linux/clk.h>
25#include <linux/err.h>
Alban Bedelb172fd02019-01-16 19:55:46 +010026#include <linux/platform_data/spi-ath79.h>
Gabor Juhos8efaef42011-01-04 21:28:22 +010027
28#define DRV_NAME "ath79-spi"
29
Gabor Juhos440114f2012-12-27 10:42:24 +010030#define ATH79_SPI_RRW_DELAY_FACTOR 12000
31#define MHZ (1000 * 1000)
32
Alban Bedelb172fd02019-01-16 19:55:46 +010033#define AR71XX_SPI_REG_FS 0x00 /* Function Select */
34#define AR71XX_SPI_REG_CTRL 0x04 /* SPI Control */
35#define AR71XX_SPI_REG_IOC 0x08 /* SPI I/O Control */
36#define AR71XX_SPI_REG_RDS 0x0c /* Read Data Shift */
37
38#define AR71XX_SPI_FS_GPIO BIT(0) /* Enable GPIO mode */
39
40#define AR71XX_SPI_IOC_DO BIT(0) /* Data Out pin */
41#define AR71XX_SPI_IOC_CLK BIT(8) /* CLK pin */
42#define AR71XX_SPI_IOC_CS(n) BIT(16 + (n))
43
Gabor Juhos8efaef42011-01-04 21:28:22 +010044struct ath79_spi {
45 struct spi_bitbang bitbang;
46 u32 ioc_base;
47 u32 reg_ctrl;
48 void __iomem *base;
Gabor Juhos440114f2012-12-27 10:42:24 +010049 struct clk *clk;
Aravind Thokalada470d62017-06-27 22:01:11 +053050 unsigned int rrw_delay;
Gabor Juhos8efaef42011-01-04 21:28:22 +010051};
52
Aravind Thokalada470d62017-06-27 22:01:11 +053053static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned int reg)
Gabor Juhos8efaef42011-01-04 21:28:22 +010054{
55 return ioread32(sp->base + reg);
56}
57
Aravind Thokalada470d62017-06-27 22:01:11 +053058static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned int reg, u32 val)
Gabor Juhos8efaef42011-01-04 21:28:22 +010059{
60 iowrite32(val, sp->base + reg);
61}
62
63static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
64{
65 return spi_master_get_devdata(spi->master);
66}
67
Aravind Thokalada470d62017-06-27 22:01:11 +053068static inline void ath79_spi_delay(struct ath79_spi *sp, unsigned int nsecs)
Gabor Juhos440114f2012-12-27 10:42:24 +010069{
70 if (nsecs > sp->rrw_delay)
71 ndelay(nsecs - sp->rrw_delay);
72}
73
Gabor Juhos8efaef42011-01-04 21:28:22 +010074static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
75{
76 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
77 int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
Alban Bedel797622d2019-01-16 19:55:45 +010078 u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
Gabor Juhos8efaef42011-01-04 21:28:22 +010079
Alban Bedel797622d2019-01-16 19:55:45 +010080 if (cs_high)
81 sp->ioc_base |= cs_bit;
82 else
83 sp->ioc_base &= ~cs_bit;
Gabor Juhos8efaef42011-01-04 21:28:22 +010084
Alban Bedel797622d2019-01-16 19:55:45 +010085 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
Gabor Juhos8efaef42011-01-04 21:28:22 +010086}
87
Gabor Juhosc4a31f42012-12-27 10:42:28 +010088static void ath79_spi_enable(struct ath79_spi *sp)
Gabor Juhos8efaef42011-01-04 21:28:22 +010089{
Gabor Juhos8efaef42011-01-04 21:28:22 +010090 /* enable GPIO mode */
91 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
92
93 /* save CTRL register */
94 sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
95 sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
96
Alban Bedel797622d2019-01-16 19:55:45 +010097 /* clear clk and mosi in the base state */
98 sp->ioc_base &= ~(AR71XX_SPI_IOC_DO | AR71XX_SPI_IOC_CLK);
99
Gabor Juhos8efaef42011-01-04 21:28:22 +0100100 /* TODO: setup speed? */
101 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100102}
103
104static void ath79_spi_disable(struct ath79_spi *sp)
105{
106 /* restore CTRL register */
107 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
108 /* disable GPIO mode */
109 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
110}
111
112static int ath79_spi_setup_cs(struct spi_device *spi)
113{
Alban Bedel83f0f392015-04-24 16:19:24 +0200114 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100115
Linus Walleij8db79542019-01-07 16:51:51 +0100116 if (!spi->cs_gpiod) {
Felix Fietkau22c76322016-12-09 20:48:52 +0100117 u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
118
Alban Bedel83f0f392015-04-24 16:19:24 +0200119 if (spi->mode & SPI_CS_HIGH)
Felix Fietkau22c76322016-12-09 20:48:52 +0100120 sp->ioc_base &= ~cs_bit;
Alban Bedel83f0f392015-04-24 16:19:24 +0200121 else
Felix Fietkau22c76322016-12-09 20:48:52 +0100122 sp->ioc_base |= cs_bit;
Alban Bedel83f0f392015-04-24 16:19:24 +0200123
124 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100125 }
126
Linus Walleij8db79542019-01-07 16:51:51 +0100127 return 0;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100128}
129
130static int ath79_spi_setup(struct spi_device *spi)
131{
132 int status = 0;
133
Gabor Juhos8efaef42011-01-04 21:28:22 +0100134 if (!spi->controller_state) {
135 status = ath79_spi_setup_cs(spi);
136 if (status)
137 return status;
138 }
139
140 status = spi_bitbang_setup(spi);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100141
142 return status;
143}
144
145static void ath79_spi_cleanup(struct spi_device *spi)
146{
Gabor Juhos8efaef42011-01-04 21:28:22 +0100147 spi_bitbang_cleanup(spi);
148}
149
Aravind Thokalada470d62017-06-27 22:01:11 +0530150static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned int nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200151 u32 word, u8 bits, unsigned flags)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100152{
153 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
154 u32 ioc = sp->ioc_base;
155
156 /* clock starts at inactive polarity */
157 for (word <<= (32 - bits); likely(bits); bits--) {
158 u32 out;
159
160 if (word & (1 << 31))
161 out = ioc | AR71XX_SPI_IOC_DO;
162 else
163 out = ioc & ~AR71XX_SPI_IOC_DO;
164
165 /* setup MSB (to slave) on trailing edge */
166 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos440114f2012-12-27 10:42:24 +0100167 ath79_spi_delay(sp, nsecs);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100168 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
Gabor Juhos440114f2012-12-27 10:42:24 +0100169 ath79_spi_delay(sp, nsecs);
Gabor Juhos72611db2012-12-27 10:42:25 +0100170 if (bits == 1)
171 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100172
173 word <<= 1;
174 }
175
176 return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
177}
178
Grant Likelyfd4a3192012-12-07 16:57:14 +0000179static int ath79_spi_probe(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100180{
181 struct spi_master *master;
182 struct ath79_spi *sp;
183 struct ath79_spi_platform_data *pdata;
184 struct resource *r;
Gabor Juhos440114f2012-12-27 10:42:24 +0100185 unsigned long rate;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100186 int ret;
187
188 master = spi_alloc_master(&pdev->dev, sizeof(*sp));
189 if (master == NULL) {
190 dev_err(&pdev->dev, "failed to allocate spi master\n");
191 return -ENOMEM;
192 }
193
194 sp = spi_master_get_devdata(master);
Alban Bedel85f62472015-04-24 16:19:22 +0200195 master->dev.of_node = pdev->dev.of_node;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100196 platform_set_drvdata(pdev, sp);
197
Jingoo Han8074cf02013-07-30 16:58:59 +0900198 pdata = dev_get_platdata(&pdev->dev);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100199
Linus Walleij8db79542019-01-07 16:51:51 +0100200 master->use_gpio_descriptors = true;
Stephen Warren24778be2013-05-21 20:36:35 -0600201 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100202 master->setup = ath79_spi_setup;
203 master->cleanup = ath79_spi_cleanup;
204 if (pdata) {
205 master->bus_num = pdata->bus_num;
206 master->num_chipselect = pdata->num_chipselect;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100207 }
208
Axel Lin94c69f72013-09-10 15:43:41 +0800209 sp->bitbang.master = master;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100210 sp->bitbang.chipselect = ath79_spi_chipselect;
211 sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
212 sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
213 sp->bitbang.flags = SPI_CS_HIGH;
214
215 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Heiner Kallweitb7a2a1c2015-09-27 18:47:35 +0200216 sp->base = devm_ioremap_resource(&pdev->dev, r);
217 if (IS_ERR(sp->base)) {
218 ret = PTR_ERR(sp->base);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100219 goto err_put_master;
220 }
221
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900222 sp->clk = devm_clk_get(&pdev->dev, "ahb");
Gabor Juhos440114f2012-12-27 10:42:24 +0100223 if (IS_ERR(sp->clk)) {
224 ret = PTR_ERR(sp->clk);
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900225 goto err_put_master;
Gabor Juhos440114f2012-12-27 10:42:24 +0100226 }
227
Alban Bedel3e19acd2015-04-24 16:19:23 +0200228 ret = clk_prepare_enable(sp->clk);
Gabor Juhos440114f2012-12-27 10:42:24 +0100229 if (ret)
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900230 goto err_put_master;
Gabor Juhos440114f2012-12-27 10:42:24 +0100231
232 rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
233 if (!rate) {
234 ret = -EINVAL;
235 goto err_clk_disable;
236 }
237
238 sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
239 dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n",
240 sp->rrw_delay);
241
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100242 ath79_spi_enable(sp);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100243 ret = spi_bitbang_start(&sp->bitbang);
244 if (ret)
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100245 goto err_disable;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100246
247 return 0;
248
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100249err_disable:
250 ath79_spi_disable(sp);
Gabor Juhos440114f2012-12-27 10:42:24 +0100251err_clk_disable:
Alban Bedel3e19acd2015-04-24 16:19:23 +0200252 clk_disable_unprepare(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100253err_put_master:
Gabor Juhos8efaef42011-01-04 21:28:22 +0100254 spi_master_put(sp->bitbang.master);
255
256 return ret;
257}
258
Grant Likelyfd4a3192012-12-07 16:57:14 +0000259static int ath79_spi_remove(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100260{
261 struct ath79_spi *sp = platform_get_drvdata(pdev);
262
263 spi_bitbang_stop(&sp->bitbang);
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100264 ath79_spi_disable(sp);
Alban Bedel3e19acd2015-04-24 16:19:23 +0200265 clk_disable_unprepare(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100266 spi_master_put(sp->bitbang.master);
267
268 return 0;
269}
270
Gabor Juhos7410e842013-02-05 20:57:55 +0100271static void ath79_spi_shutdown(struct platform_device *pdev)
272{
273 ath79_spi_remove(pdev);
274}
275
Alban Bedel85f62472015-04-24 16:19:22 +0200276static const struct of_device_id ath79_spi_of_match[] = {
277 { .compatible = "qca,ar7100-spi", },
278 { },
279};
Javier Martinez Canillasd7a32392016-11-23 13:37:11 -0300280MODULE_DEVICE_TABLE(of, ath79_spi_of_match);
Alban Bedel85f62472015-04-24 16:19:22 +0200281
Gabor Juhos8efaef42011-01-04 21:28:22 +0100282static struct platform_driver ath79_spi_driver = {
283 .probe = ath79_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000284 .remove = ath79_spi_remove,
Gabor Juhos7410e842013-02-05 20:57:55 +0100285 .shutdown = ath79_spi_shutdown,
Gabor Juhos8efaef42011-01-04 21:28:22 +0100286 .driver = {
287 .name = DRV_NAME,
Alban Bedel85f62472015-04-24 16:19:22 +0200288 .of_match_table = ath79_spi_of_match,
Gabor Juhos8efaef42011-01-04 21:28:22 +0100289 },
290};
Grant Likely940ab882011-10-05 11:29:49 -0600291module_platform_driver(ath79_spi_driver);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100292
293MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
294MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
295MODULE_LICENSE("GPL v2");
296MODULE_ALIAS("platform:" DRV_NAME);