blob: 31534b51715aa95e29664cc80d44d71b5131d0ab [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/init.h>
18#include <linux/delay.h>
19#include <linux/spinlock.h>
20#include <linux/workqueue.h>
21#include <linux/platform_device.h>
22#include <linux/io.h>
23#include <linux/spi/spi.h>
24#include <linux/spi/spi_bitbang.h>
25#include <linux/bitops.h>
26#include <linux/gpio.h>
Gabor Juhos440114f2012-12-27 10:42:24 +010027#include <linux/clk.h>
28#include <linux/err.h>
Gabor Juhos8efaef42011-01-04 21:28:22 +010029
30#include <asm/mach-ath79/ar71xx_regs.h>
31#include <asm/mach-ath79/ath79_spi_platform.h>
32
33#define DRV_NAME "ath79-spi"
34
Gabor Juhos440114f2012-12-27 10:42:24 +010035#define ATH79_SPI_RRW_DELAY_FACTOR 12000
36#define MHZ (1000 * 1000)
37
Gabor Juhos8efaef42011-01-04 21:28:22 +010038struct ath79_spi {
39 struct spi_bitbang bitbang;
40 u32 ioc_base;
41 u32 reg_ctrl;
42 void __iomem *base;
Gabor Juhos440114f2012-12-27 10:42:24 +010043 struct clk *clk;
44 unsigned rrw_delay;
Gabor Juhos8efaef42011-01-04 21:28:22 +010045};
46
47static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned reg)
48{
49 return ioread32(sp->base + reg);
50}
51
52static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned reg, u32 val)
53{
54 iowrite32(val, sp->base + reg);
55}
56
57static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
58{
59 return spi_master_get_devdata(spi->master);
60}
61
Gabor Juhos440114f2012-12-27 10:42:24 +010062static inline void ath79_spi_delay(struct ath79_spi *sp, unsigned nsecs)
63{
64 if (nsecs > sp->rrw_delay)
65 ndelay(nsecs - sp->rrw_delay);
66}
67
Gabor Juhos8efaef42011-01-04 21:28:22 +010068static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
69{
70 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
71 int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
72
73 if (is_active) {
74 /* set initial clock polarity */
75 if (spi->mode & SPI_CPOL)
76 sp->ioc_base |= AR71XX_SPI_IOC_CLK;
77 else
78 sp->ioc_base &= ~AR71XX_SPI_IOC_CLK;
79
80 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
81 }
82
83 if (spi->chip_select) {
84 struct ath79_spi_controller_data *cdata = spi->controller_data;
85
86 /* SPI is normally active-low */
87 gpio_set_value(cdata->gpio, cs_high);
88 } else {
89 if (cs_high)
90 sp->ioc_base |= AR71XX_SPI_IOC_CS0;
91 else
92 sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
93
94 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
95 }
96
97}
98
Gabor Juhosc4a31f42012-12-27 10:42:28 +010099static void ath79_spi_enable(struct ath79_spi *sp)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100100{
Gabor Juhos8efaef42011-01-04 21:28:22 +0100101 /* enable GPIO mode */
102 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
103
104 /* save CTRL register */
105 sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
106 sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
107
108 /* TODO: setup speed? */
109 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100110}
111
112static void ath79_spi_disable(struct ath79_spi *sp)
113{
114 /* restore CTRL register */
115 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
116 /* disable GPIO mode */
117 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
118}
119
120static int ath79_spi_setup_cs(struct spi_device *spi)
121{
122 struct ath79_spi_controller_data *cdata;
123 int status;
124
125 cdata = spi->controller_data;
126 if (spi->chip_select && !cdata)
127 return -EINVAL;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100128
Gabor Juhos95d79412012-12-27 10:42:27 +0100129 status = 0;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100130 if (spi->chip_select) {
Gabor Juhos95d79412012-12-27 10:42:27 +0100131 unsigned long flags;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100132
Gabor Juhos95d79412012-12-27 10:42:27 +0100133 flags = GPIOF_DIR_OUT;
134 if (spi->mode & SPI_CS_HIGH)
135 flags |= GPIOF_INIT_HIGH;
136 else
137 flags |= GPIOF_INIT_LOW;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100138
Gabor Juhos95d79412012-12-27 10:42:27 +0100139 status = gpio_request_one(cdata->gpio, flags,
140 dev_name(&spi->dev));
Gabor Juhos8efaef42011-01-04 21:28:22 +0100141 }
142
Gabor Juhos95d79412012-12-27 10:42:27 +0100143 return status;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100144}
145
146static void ath79_spi_cleanup_cs(struct spi_device *spi)
147{
Gabor Juhos8efaef42011-01-04 21:28:22 +0100148 if (spi->chip_select) {
149 struct ath79_spi_controller_data *cdata = spi->controller_data;
150 gpio_free(cdata->gpio);
151 }
Gabor Juhos8efaef42011-01-04 21:28:22 +0100152}
153
154static int ath79_spi_setup(struct spi_device *spi)
155{
156 int status = 0;
157
Gabor Juhos8efaef42011-01-04 21:28:22 +0100158 if (!spi->controller_state) {
159 status = ath79_spi_setup_cs(spi);
160 if (status)
161 return status;
162 }
163
164 status = spi_bitbang_setup(spi);
165 if (status && !spi->controller_state)
166 ath79_spi_cleanup_cs(spi);
167
168 return status;
169}
170
171static void ath79_spi_cleanup(struct spi_device *spi)
172{
173 ath79_spi_cleanup_cs(spi);
174 spi_bitbang_cleanup(spi);
175}
176
177static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned nsecs,
178 u32 word, u8 bits)
179{
180 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
181 u32 ioc = sp->ioc_base;
182
183 /* clock starts at inactive polarity */
184 for (word <<= (32 - bits); likely(bits); bits--) {
185 u32 out;
186
187 if (word & (1 << 31))
188 out = ioc | AR71XX_SPI_IOC_DO;
189 else
190 out = ioc & ~AR71XX_SPI_IOC_DO;
191
192 /* setup MSB (to slave) on trailing edge */
193 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos440114f2012-12-27 10:42:24 +0100194 ath79_spi_delay(sp, nsecs);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100195 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
Gabor Juhos440114f2012-12-27 10:42:24 +0100196 ath79_spi_delay(sp, nsecs);
Gabor Juhos72611db2012-12-27 10:42:25 +0100197 if (bits == 1)
198 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100199
200 word <<= 1;
201 }
202
203 return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
204}
205
Grant Likelyfd4a3192012-12-07 16:57:14 +0000206static int ath79_spi_probe(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100207{
208 struct spi_master *master;
209 struct ath79_spi *sp;
210 struct ath79_spi_platform_data *pdata;
211 struct resource *r;
Gabor Juhos440114f2012-12-27 10:42:24 +0100212 unsigned long rate;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100213 int ret;
214
215 master = spi_alloc_master(&pdev->dev, sizeof(*sp));
216 if (master == NULL) {
217 dev_err(&pdev->dev, "failed to allocate spi master\n");
218 return -ENOMEM;
219 }
220
221 sp = spi_master_get_devdata(master);
222 platform_set_drvdata(pdev, sp);
223
Jingoo Han8074cf02013-07-30 16:58:59 +0900224 pdata = dev_get_platdata(&pdev->dev);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100225
Stephen Warren24778be2013-05-21 20:36:35 -0600226 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100227 master->setup = ath79_spi_setup;
228 master->cleanup = ath79_spi_cleanup;
229 if (pdata) {
230 master->bus_num = pdata->bus_num;
231 master->num_chipselect = pdata->num_chipselect;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100232 }
233
Axel Lin94c69f72013-09-10 15:43:41 +0800234 sp->bitbang.master = master;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100235 sp->bitbang.chipselect = ath79_spi_chipselect;
236 sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
237 sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
238 sp->bitbang.flags = SPI_CS_HIGH;
239
240 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
241 if (r == NULL) {
242 ret = -ENOENT;
243 goto err_put_master;
244 }
245
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900246 sp->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
Gabor Juhos8efaef42011-01-04 21:28:22 +0100247 if (!sp->base) {
248 ret = -ENXIO;
249 goto err_put_master;
250 }
251
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900252 sp->clk = devm_clk_get(&pdev->dev, "ahb");
Gabor Juhos440114f2012-12-27 10:42:24 +0100253 if (IS_ERR(sp->clk)) {
254 ret = PTR_ERR(sp->clk);
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900255 goto err_put_master;
Gabor Juhos440114f2012-12-27 10:42:24 +0100256 }
257
258 ret = clk_enable(sp->clk);
259 if (ret)
Jingoo Hana6f4c8e2013-12-09 19:14:58 +0900260 goto err_put_master;
Gabor Juhos440114f2012-12-27 10:42:24 +0100261
262 rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
263 if (!rate) {
264 ret = -EINVAL;
265 goto err_clk_disable;
266 }
267
268 sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
269 dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n",
270 sp->rrw_delay);
271
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100272 ath79_spi_enable(sp);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100273 ret = spi_bitbang_start(&sp->bitbang);
274 if (ret)
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100275 goto err_disable;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100276
277 return 0;
278
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100279err_disable:
280 ath79_spi_disable(sp);
Gabor Juhos440114f2012-12-27 10:42:24 +0100281err_clk_disable:
282 clk_disable(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100283err_put_master:
Gabor Juhos8efaef42011-01-04 21:28:22 +0100284 spi_master_put(sp->bitbang.master);
285
286 return ret;
287}
288
Grant Likelyfd4a3192012-12-07 16:57:14 +0000289static int ath79_spi_remove(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100290{
291 struct ath79_spi *sp = platform_get_drvdata(pdev);
292
293 spi_bitbang_stop(&sp->bitbang);
Gabor Juhosc4a31f42012-12-27 10:42:28 +0100294 ath79_spi_disable(sp);
Gabor Juhos440114f2012-12-27 10:42:24 +0100295 clk_disable(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100296 spi_master_put(sp->bitbang.master);
297
298 return 0;
299}
300
Gabor Juhos7410e842013-02-05 20:57:55 +0100301static void ath79_spi_shutdown(struct platform_device *pdev)
302{
303 ath79_spi_remove(pdev);
304}
305
Gabor Juhos8efaef42011-01-04 21:28:22 +0100306static struct platform_driver ath79_spi_driver = {
307 .probe = ath79_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000308 .remove = ath79_spi_remove,
Gabor Juhos7410e842013-02-05 20:57:55 +0100309 .shutdown = ath79_spi_shutdown,
Gabor Juhos8efaef42011-01-04 21:28:22 +0100310 .driver = {
311 .name = DRV_NAME,
312 .owner = THIS_MODULE,
313 },
314};
Grant Likely940ab882011-10-05 11:29:49 -0600315module_platform_driver(ath79_spi_driver);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100316
317MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
318MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
319MODULE_LICENSE("GPL v2");
320MODULE_ALIAS("platform:" DRV_NAME);