Commit | Line | Data |
---|---|---|
8efaef4d GJ |
1 | /* |
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> | |
807cc4b1 | 16 | #include <linux/module.h> |
8efaef4d GJ |
17 | #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> | |
440114fd GJ |
27 | #include <linux/clk.h> |
28 | #include <linux/err.h> | |
8efaef4d GJ |
29 | |
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 | ||
440114fd GJ |
35 | #define ATH79_SPI_RRW_DELAY_FACTOR 12000 |
36 | #define MHZ (1000 * 1000) | |
37 | ||
8efaef4d GJ |
38 | struct ath79_spi { |
39 | struct spi_bitbang bitbang; | |
40 | u32 ioc_base; | |
41 | u32 reg_ctrl; | |
42 | void __iomem *base; | |
440114fd GJ |
43 | struct clk *clk; |
44 | unsigned rrw_delay; | |
8efaef4d GJ |
45 | }; |
46 | ||
47 | static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned reg) | |
48 | { | |
49 | return ioread32(sp->base + reg); | |
50 | } | |
51 | ||
52 | static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned reg, u32 val) | |
53 | { | |
54 | iowrite32(val, sp->base + reg); | |
55 | } | |
56 | ||
57 | static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi) | |
58 | { | |
59 | return spi_master_get_devdata(spi->master); | |
60 | } | |
61 | ||
440114fd GJ |
62 | static 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 | ||
8efaef4d GJ |
68 | static 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 | ||
99 | static 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; | |
95d79419 | 103 | int status; |
8efaef4d GJ |
104 | |
105 | cdata = spi->controller_data; | |
106 | if (spi->chip_select && !cdata) | |
107 | return -EINVAL; | |
108 | ||
109 | /* enable GPIO mode */ | |
110 | ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO); | |
111 | ||
112 | /* save CTRL register */ | |
113 | sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL); | |
114 | sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC); | |
115 | ||
116 | /* TODO: setup speed? */ | |
117 | ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43); | |
118 | ||
95d79419 | 119 | status = 0; |
8efaef4d | 120 | if (spi->chip_select) { |
95d79419 GJ |
121 | unsigned long flags; |
122 | ||
123 | flags = GPIOF_DIR_OUT; | |
124 | if (spi->mode & SPI_CS_HIGH) | |
125 | flags |= GPIOF_INIT_HIGH; | |
126 | else | |
127 | flags |= GPIOF_INIT_LOW; | |
128 | ||
129 | status = gpio_request_one(cdata->gpio, flags, | |
130 | dev_name(&spi->dev)); | |
8efaef4d GJ |
131 | } |
132 | ||
95d79419 | 133 | return status; |
8efaef4d GJ |
134 | } |
135 | ||
136 | static void ath79_spi_cleanup_cs(struct spi_device *spi) | |
137 | { | |
138 | struct ath79_spi *sp = ath79_spidev_to_sp(spi); | |
139 | ||
140 | if (spi->chip_select) { | |
141 | struct ath79_spi_controller_data *cdata = spi->controller_data; | |
142 | gpio_free(cdata->gpio); | |
143 | } | |
144 | ||
145 | /* restore CTRL register */ | |
146 | ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl); | |
147 | /* disable GPIO mode */ | |
148 | ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0); | |
149 | } | |
150 | ||
151 | static int ath79_spi_setup(struct spi_device *spi) | |
152 | { | |
153 | int status = 0; | |
154 | ||
155 | if (spi->bits_per_word > 32) | |
156 | return -EINVAL; | |
157 | ||
158 | 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 | ||
171 | static void ath79_spi_cleanup(struct spi_device *spi) | |
172 | { | |
173 | ath79_spi_cleanup_cs(spi); | |
174 | spi_bitbang_cleanup(spi); | |
175 | } | |
176 | ||
177 | static 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); | |
440114fd | 194 | ath79_spi_delay(sp, nsecs); |
8efaef4d | 195 | ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK); |
440114fd | 196 | ath79_spi_delay(sp, nsecs); |
72611db0 GJ |
197 | if (bits == 1) |
198 | ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out); | |
8efaef4d GJ |
199 | |
200 | word <<= 1; | |
201 | } | |
202 | ||
203 | return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS); | |
204 | } | |
205 | ||
fd4a319b | 206 | static int ath79_spi_probe(struct platform_device *pdev) |
8efaef4d GJ |
207 | { |
208 | struct spi_master *master; | |
209 | struct ath79_spi *sp; | |
210 | struct ath79_spi_platform_data *pdata; | |
211 | struct resource *r; | |
440114fd | 212 | unsigned long rate; |
8efaef4d GJ |
213 | 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 | ||
224 | pdata = pdev->dev.platform_data; | |
225 | ||
226 | master->setup = ath79_spi_setup; | |
227 | master->cleanup = ath79_spi_cleanup; | |
228 | if (pdata) { | |
229 | master->bus_num = pdata->bus_num; | |
230 | master->num_chipselect = pdata->num_chipselect; | |
8efaef4d GJ |
231 | } |
232 | ||
233 | sp->bitbang.master = spi_master_get(master); | |
234 | sp->bitbang.chipselect = ath79_spi_chipselect; | |
235 | sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0; | |
236 | sp->bitbang.setup_transfer = spi_bitbang_setup_transfer; | |
237 | sp->bitbang.flags = SPI_CS_HIGH; | |
238 | ||
239 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
240 | if (r == NULL) { | |
241 | ret = -ENOENT; | |
242 | goto err_put_master; | |
243 | } | |
244 | ||
8e2943c0 | 245 | sp->base = ioremap(r->start, resource_size(r)); |
8efaef4d GJ |
246 | if (!sp->base) { |
247 | ret = -ENXIO; | |
248 | goto err_put_master; | |
249 | } | |
250 | ||
440114fd GJ |
251 | sp->clk = clk_get(&pdev->dev, "ahb"); |
252 | if (IS_ERR(sp->clk)) { | |
253 | ret = PTR_ERR(sp->clk); | |
254 | goto err_unmap; | |
255 | } | |
256 | ||
257 | ret = clk_enable(sp->clk); | |
258 | if (ret) | |
259 | goto err_clk_put; | |
260 | ||
261 | rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ); | |
262 | if (!rate) { | |
263 | ret = -EINVAL; | |
264 | goto err_clk_disable; | |
265 | } | |
266 | ||
267 | sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate; | |
268 | dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n", | |
269 | sp->rrw_delay); | |
270 | ||
8efaef4d GJ |
271 | ret = spi_bitbang_start(&sp->bitbang); |
272 | if (ret) | |
440114fd | 273 | goto err_clk_disable; |
8efaef4d GJ |
274 | |
275 | return 0; | |
276 | ||
440114fd GJ |
277 | err_clk_disable: |
278 | clk_disable(sp->clk); | |
279 | err_clk_put: | |
280 | clk_put(sp->clk); | |
8efaef4d GJ |
281 | err_unmap: |
282 | iounmap(sp->base); | |
283 | err_put_master: | |
284 | platform_set_drvdata(pdev, NULL); | |
285 | spi_master_put(sp->bitbang.master); | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
fd4a319b | 290 | static int ath79_spi_remove(struct platform_device *pdev) |
8efaef4d GJ |
291 | { |
292 | struct ath79_spi *sp = platform_get_drvdata(pdev); | |
293 | ||
294 | spi_bitbang_stop(&sp->bitbang); | |
440114fd GJ |
295 | clk_disable(sp->clk); |
296 | clk_put(sp->clk); | |
8efaef4d GJ |
297 | iounmap(sp->base); |
298 | platform_set_drvdata(pdev, NULL); | |
299 | spi_master_put(sp->bitbang.master); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static struct platform_driver ath79_spi_driver = { | |
305 | .probe = ath79_spi_probe, | |
fd4a319b | 306 | .remove = ath79_spi_remove, |
8efaef4d GJ |
307 | .driver = { |
308 | .name = DRV_NAME, | |
309 | .owner = THIS_MODULE, | |
310 | }, | |
311 | }; | |
940ab889 | 312 | module_platform_driver(ath79_spi_driver); |
8efaef4d GJ |
313 | |
314 | MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X"); | |
315 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); | |
316 | MODULE_LICENSE("GPL v2"); | |
317 | MODULE_ALIAS("platform:" DRV_NAME); |