blob: d4b8e1236612e192576ff44f6d941bbc553b3ecd [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
99static int ath79_spi_setup_cs(struct spi_device *spi)
100{
101 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
102 struct ath79_spi_controller_data *cdata;
103
104 cdata = spi->controller_data;
105 if (spi->chip_select && !cdata)
106 return -EINVAL;
107
108 /* enable GPIO mode */
109 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
110
111 /* save CTRL register */
112 sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
113 sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
114
115 /* TODO: setup speed? */
116 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
117
118 if (spi->chip_select) {
119 int status = 0;
120
121 status = gpio_request(cdata->gpio, dev_name(&spi->dev));
122 if (status)
123 return status;
124
125 status = gpio_direction_output(cdata->gpio,
126 spi->mode & SPI_CS_HIGH);
127 if (status) {
128 gpio_free(cdata->gpio);
129 return status;
130 }
131 } else {
132 if (spi->mode & SPI_CS_HIGH)
133 sp->ioc_base |= AR71XX_SPI_IOC_CS0;
134 else
135 sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
136 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
137 }
138
139 return 0;
140}
141
142static void ath79_spi_cleanup_cs(struct spi_device *spi)
143{
144 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
145
146 if (spi->chip_select) {
147 struct ath79_spi_controller_data *cdata = spi->controller_data;
148 gpio_free(cdata->gpio);
149 }
150
151 /* restore CTRL register */
152 ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
153 /* disable GPIO mode */
154 ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
155}
156
157static int ath79_spi_setup(struct spi_device *spi)
158{
159 int status = 0;
160
161 if (spi->bits_per_word > 32)
162 return -EINVAL;
163
164 if (!spi->controller_state) {
165 status = ath79_spi_setup_cs(spi);
166 if (status)
167 return status;
168 }
169
170 status = spi_bitbang_setup(spi);
171 if (status && !spi->controller_state)
172 ath79_spi_cleanup_cs(spi);
173
174 return status;
175}
176
177static void ath79_spi_cleanup(struct spi_device *spi)
178{
179 ath79_spi_cleanup_cs(spi);
180 spi_bitbang_cleanup(spi);
181}
182
183static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned nsecs,
184 u32 word, u8 bits)
185{
186 struct ath79_spi *sp = ath79_spidev_to_sp(spi);
187 u32 ioc = sp->ioc_base;
188
189 /* clock starts at inactive polarity */
190 for (word <<= (32 - bits); likely(bits); bits--) {
191 u32 out;
192
193 if (word & (1 << 31))
194 out = ioc | AR71XX_SPI_IOC_DO;
195 else
196 out = ioc & ~AR71XX_SPI_IOC_DO;
197
198 /* setup MSB (to slave) on trailing edge */
199 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos440114f2012-12-27 10:42:24 +0100200 ath79_spi_delay(sp, nsecs);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100201 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
Gabor Juhos440114f2012-12-27 10:42:24 +0100202 ath79_spi_delay(sp, nsecs);
Gabor Juhos72611db2012-12-27 10:42:25 +0100203 if (bits == 1)
204 ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100205
206 word <<= 1;
207 }
208
209 return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
210}
211
Grant Likelyfd4a3192012-12-07 16:57:14 +0000212static int ath79_spi_probe(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100213{
214 struct spi_master *master;
215 struct ath79_spi *sp;
216 struct ath79_spi_platform_data *pdata;
217 struct resource *r;
Gabor Juhos440114f2012-12-27 10:42:24 +0100218 unsigned long rate;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100219 int ret;
220
221 master = spi_alloc_master(&pdev->dev, sizeof(*sp));
222 if (master == NULL) {
223 dev_err(&pdev->dev, "failed to allocate spi master\n");
224 return -ENOMEM;
225 }
226
227 sp = spi_master_get_devdata(master);
228 platform_set_drvdata(pdev, sp);
229
230 pdata = pdev->dev.platform_data;
231
232 master->setup = ath79_spi_setup;
233 master->cleanup = ath79_spi_cleanup;
234 if (pdata) {
235 master->bus_num = pdata->bus_num;
236 master->num_chipselect = pdata->num_chipselect;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100237 }
238
239 sp->bitbang.master = spi_master_get(master);
240 sp->bitbang.chipselect = ath79_spi_chipselect;
241 sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
242 sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
243 sp->bitbang.flags = SPI_CS_HIGH;
244
245 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
246 if (r == NULL) {
247 ret = -ENOENT;
248 goto err_put_master;
249 }
250
Joe Perches8e2943c2011-06-10 18:11:25 -0700251 sp->base = ioremap(r->start, resource_size(r));
Gabor Juhos8efaef42011-01-04 21:28:22 +0100252 if (!sp->base) {
253 ret = -ENXIO;
254 goto err_put_master;
255 }
256
Gabor Juhos440114f2012-12-27 10:42:24 +0100257 sp->clk = clk_get(&pdev->dev, "ahb");
258 if (IS_ERR(sp->clk)) {
259 ret = PTR_ERR(sp->clk);
260 goto err_unmap;
261 }
262
263 ret = clk_enable(sp->clk);
264 if (ret)
265 goto err_clk_put;
266
267 rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
268 if (!rate) {
269 ret = -EINVAL;
270 goto err_clk_disable;
271 }
272
273 sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
274 dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n",
275 sp->rrw_delay);
276
Gabor Juhos8efaef42011-01-04 21:28:22 +0100277 ret = spi_bitbang_start(&sp->bitbang);
278 if (ret)
Gabor Juhos440114f2012-12-27 10:42:24 +0100279 goto err_clk_disable;
Gabor Juhos8efaef42011-01-04 21:28:22 +0100280
281 return 0;
282
Gabor Juhos440114f2012-12-27 10:42:24 +0100283err_clk_disable:
284 clk_disable(sp->clk);
285err_clk_put:
286 clk_put(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100287err_unmap:
288 iounmap(sp->base);
289err_put_master:
290 platform_set_drvdata(pdev, NULL);
291 spi_master_put(sp->bitbang.master);
292
293 return ret;
294}
295
Grant Likelyfd4a3192012-12-07 16:57:14 +0000296static int ath79_spi_remove(struct platform_device *pdev)
Gabor Juhos8efaef42011-01-04 21:28:22 +0100297{
298 struct ath79_spi *sp = platform_get_drvdata(pdev);
299
300 spi_bitbang_stop(&sp->bitbang);
Gabor Juhos440114f2012-12-27 10:42:24 +0100301 clk_disable(sp->clk);
302 clk_put(sp->clk);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100303 iounmap(sp->base);
304 platform_set_drvdata(pdev, NULL);
305 spi_master_put(sp->bitbang.master);
306
307 return 0;
308}
309
310static struct platform_driver ath79_spi_driver = {
311 .probe = ath79_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000312 .remove = ath79_spi_remove,
Gabor Juhos8efaef42011-01-04 21:28:22 +0100313 .driver = {
314 .name = DRV_NAME,
315 .owner = THIS_MODULE,
316 },
317};
Grant Likely940ab882011-10-05 11:29:49 -0600318module_platform_driver(ath79_spi_driver);
Gabor Juhos8efaef42011-01-04 21:28:22 +0100319
320MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
321MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
322MODULE_LICENSE("GPL v2");
323MODULE_ALIAS("platform:" DRV_NAME);