blob: f7c896e2981ef684d339f8e97f2b021ff468c35d [file] [log] [blame]
Thomas Chouce792582011-02-14 10:20:39 +08001/*
2 * OpenCores tiny SPI master driver
3 *
4 * http://opencores.org/project,tiny_spi
5 *
6 * Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
7 *
8 * Based on spi_s3c24xx.c, which is:
9 * Copyright (c) 2006 Ben Dooks
10 * Copyright (c) 2006 Simtec Electronics
11 * Ben Dooks <ben@simtec.co.uk>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/errno.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040021#include <linux/module.h>
Thomas Chouce792582011-02-14 10:20:39 +080022#include <linux/platform_device.h>
23#include <linux/spi/spi.h>
24#include <linux/spi/spi_bitbang.h>
25#include <linux/spi/spi_oc_tiny.h>
26#include <linux/io.h>
27#include <linux/gpio.h>
28#include <linux/of.h>
29
30#define DRV_NAME "spi_oc_tiny"
31
32#define TINY_SPI_RXDATA 0
33#define TINY_SPI_TXDATA 4
34#define TINY_SPI_STATUS 8
35#define TINY_SPI_CONTROL 12
36#define TINY_SPI_BAUD 16
37
38#define TINY_SPI_STATUS_TXE 0x1
39#define TINY_SPI_STATUS_TXR 0x2
40
41struct tiny_spi {
42 /* bitbang has to be first */
43 struct spi_bitbang bitbang;
44 struct completion done;
45
46 void __iomem *base;
47 int irq;
48 unsigned int freq;
49 unsigned int baudwidth;
50 unsigned int baud;
51 unsigned int speed_hz;
52 unsigned int mode;
53 unsigned int len;
54 unsigned int txc, rxc;
55 const u8 *txp;
56 u8 *rxp;
Grant Likelye80beb22013-02-12 17:48:37 +000057 int gpio_cs_count;
Thomas Chouce792582011-02-14 10:20:39 +080058 int *gpio_cs;
59};
60
61static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
62{
63 return spi_master_get_devdata(sdev->master);
64}
65
66static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
67{
68 struct tiny_spi *hw = tiny_spi_to_hw(spi);
69
70 return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
71}
72
73static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
74{
75 struct tiny_spi *hw = tiny_spi_to_hw(spi);
76
Grant Likelye80beb22013-02-12 17:48:37 +000077 if (hw->gpio_cs_count > 0) {
Thomas Chouce792582011-02-14 10:20:39 +080078 gpio_set_value(hw->gpio_cs[spi->chip_select],
79 (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
80 }
81}
82
83static int tiny_spi_setup_transfer(struct spi_device *spi,
84 struct spi_transfer *t)
85{
86 struct tiny_spi *hw = tiny_spi_to_hw(spi);
87 unsigned int baud = hw->baud;
88
89 if (t) {
90 if (t->speed_hz && t->speed_hz != hw->speed_hz)
91 baud = tiny_spi_baud(spi, t->speed_hz);
92 }
93 writel(baud, hw->base + TINY_SPI_BAUD);
94 writel(hw->mode, hw->base + TINY_SPI_CONTROL);
95 return 0;
96}
97
98static int tiny_spi_setup(struct spi_device *spi)
99{
100 struct tiny_spi *hw = tiny_spi_to_hw(spi);
101
102 if (spi->max_speed_hz != hw->speed_hz) {
103 hw->speed_hz = spi->max_speed_hz;
104 hw->baud = tiny_spi_baud(spi, hw->speed_hz);
105 }
106 hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
107 return 0;
108}
109
110static inline void tiny_spi_wait_txr(struct tiny_spi *hw)
111{
112 while (!(readb(hw->base + TINY_SPI_STATUS) &
113 TINY_SPI_STATUS_TXR))
114 cpu_relax();
115}
116
117static inline void tiny_spi_wait_txe(struct tiny_spi *hw)
118{
119 while (!(readb(hw->base + TINY_SPI_STATUS) &
120 TINY_SPI_STATUS_TXE))
121 cpu_relax();
122}
123
124static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
125{
126 struct tiny_spi *hw = tiny_spi_to_hw(spi);
127 const u8 *txp = t->tx_buf;
128 u8 *rxp = t->rx_buf;
129 unsigned int i;
130
131 if (hw->irq >= 0) {
Masanari Iida886db6ac2012-07-30 23:16:14 +0900132 /* use interrupt driven data transfer */
Thomas Chouce792582011-02-14 10:20:39 +0800133 hw->len = t->len;
134 hw->txp = t->tx_buf;
135 hw->rxp = t->rx_buf;
136 hw->txc = 0;
137 hw->rxc = 0;
138
139 /* send the first byte */
140 if (t->len > 1) {
141 writeb(hw->txp ? *hw->txp++ : 0,
142 hw->base + TINY_SPI_TXDATA);
143 hw->txc++;
144 writeb(hw->txp ? *hw->txp++ : 0,
145 hw->base + TINY_SPI_TXDATA);
146 hw->txc++;
147 writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
148 } else {
149 writeb(hw->txp ? *hw->txp++ : 0,
150 hw->base + TINY_SPI_TXDATA);
151 hw->txc++;
152 writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
153 }
154
155 wait_for_completion(&hw->done);
Thomas Chouce792582011-02-14 10:20:39 +0800156 } else {
Axel Line826a7f2014-01-08 16:00:04 +0800157 /* we need to tighten the transfer loop */
158 writeb(txp ? *txp++ : 0, hw->base + TINY_SPI_TXDATA);
159 for (i = 1; i < t->len; i++) {
160 writeb(txp ? *txp++ : 0, hw->base + TINY_SPI_TXDATA);
161
162 if (rxp || (i != t->len - 1))
Thomas Chouce792582011-02-14 10:20:39 +0800163 tiny_spi_wait_txr(hw);
Axel Line826a7f2014-01-08 16:00:04 +0800164 if (rxp)
165 *rxp++ = readb(hw->base + TINY_SPI_TXDATA);
Thomas Chouce792582011-02-14 10:20:39 +0800166 }
167 tiny_spi_wait_txe(hw);
Axel Line826a7f2014-01-08 16:00:04 +0800168 if (rxp)
169 *rxp++ = readb(hw->base + TINY_SPI_RXDATA);
Thomas Chouce792582011-02-14 10:20:39 +0800170 }
Axel Line826a7f2014-01-08 16:00:04 +0800171
Thomas Chouce792582011-02-14 10:20:39 +0800172 return t->len;
173}
174
175static irqreturn_t tiny_spi_irq(int irq, void *dev)
176{
177 struct tiny_spi *hw = dev;
178
179 writeb(0, hw->base + TINY_SPI_STATUS);
180 if (hw->rxc + 1 == hw->len) {
181 if (hw->rxp)
182 *hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
183 hw->rxc++;
184 complete(&hw->done);
185 } else {
186 if (hw->rxp)
187 *hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
188 hw->rxc++;
189 if (hw->txc < hw->len) {
190 writeb(hw->txp ? *hw->txp++ : 0,
191 hw->base + TINY_SPI_TXDATA);
192 hw->txc++;
193 writeb(TINY_SPI_STATUS_TXR,
194 hw->base + TINY_SPI_STATUS);
195 } else {
196 writeb(TINY_SPI_STATUS_TXE,
197 hw->base + TINY_SPI_STATUS);
198 }
199 }
200 return IRQ_HANDLED;
201}
202
203#ifdef CONFIG_OF
204#include <linux/of_gpio.h>
205
Grant Likelyfd4a3192012-12-07 16:57:14 +0000206static int tiny_spi_of_probe(struct platform_device *pdev)
Thomas Chouce792582011-02-14 10:20:39 +0800207{
208 struct tiny_spi *hw = platform_get_drvdata(pdev);
209 struct device_node *np = pdev->dev.of_node;
210 unsigned int i;
211 const __be32 *val;
212 int len;
213
214 if (!np)
215 return 0;
216 hw->gpio_cs_count = of_gpio_count(np);
Grant Likelye80beb22013-02-12 17:48:37 +0000217 if (hw->gpio_cs_count > 0) {
Thomas Chouce792582011-02-14 10:20:39 +0800218 hw->gpio_cs = devm_kzalloc(&pdev->dev,
219 hw->gpio_cs_count * sizeof(unsigned int),
220 GFP_KERNEL);
221 if (!hw->gpio_cs)
222 return -ENOMEM;
223 }
224 for (i = 0; i < hw->gpio_cs_count; i++) {
225 hw->gpio_cs[i] = of_get_gpio_flags(np, i, NULL);
226 if (hw->gpio_cs[i] < 0)
227 return -ENODEV;
228 }
229 hw->bitbang.master->dev.of_node = pdev->dev.of_node;
230 val = of_get_property(pdev->dev.of_node,
231 "clock-frequency", &len);
232 if (val && len >= sizeof(__be32))
233 hw->freq = be32_to_cpup(val);
234 val = of_get_property(pdev->dev.of_node, "baud-width", &len);
235 if (val && len >= sizeof(__be32))
236 hw->baudwidth = be32_to_cpup(val);
237 return 0;
238}
239#else /* !CONFIG_OF */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000240static int tiny_spi_of_probe(struct platform_device *pdev)
Thomas Chouce792582011-02-14 10:20:39 +0800241{
242 return 0;
243}
244#endif /* CONFIG_OF */
245
Grant Likelyfd4a3192012-12-07 16:57:14 +0000246static int tiny_spi_probe(struct platform_device *pdev)
Thomas Chouce792582011-02-14 10:20:39 +0800247{
Jingoo Han8074cf02013-07-30 16:58:59 +0900248 struct tiny_spi_platform_data *platp = dev_get_platdata(&pdev->dev);
Thomas Chouce792582011-02-14 10:20:39 +0800249 struct tiny_spi *hw;
250 struct spi_master *master;
251 struct resource *res;
252 unsigned int i;
253 int err = -ENODEV;
254
255 master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
256 if (!master)
257 return err;
258
259 /* setup the master state. */
260 master->bus_num = pdev->id;
261 master->num_chipselect = 255;
262 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
263 master->setup = tiny_spi_setup;
264
265 hw = spi_master_get_devdata(master);
266 platform_set_drvdata(pdev, hw);
267
268 /* setup the state for the bitbang driver */
Axel Lin94c69f72013-09-10 15:43:41 +0800269 hw->bitbang.master = master;
Thomas Chouce792582011-02-14 10:20:39 +0800270 if (!hw->bitbang.master)
271 return err;
272 hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
273 hw->bitbang.chipselect = tiny_spi_chipselect;
274 hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
275
276 /* find and map our resources */
277 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawallb3136f82013-08-24 19:13:15 +0200278 hw->base = devm_ioremap_resource(&pdev->dev, res);
279 if (IS_ERR(hw->base)) {
280 err = PTR_ERR(hw->base);
281 goto exit;
282 }
Thomas Chouce792582011-02-14 10:20:39 +0800283 /* irq is optional */
284 hw->irq = platform_get_irq(pdev, 0);
285 if (hw->irq >= 0) {
286 init_completion(&hw->done);
287 err = devm_request_irq(&pdev->dev, hw->irq, tiny_spi_irq, 0,
288 pdev->name, hw);
289 if (err)
290 goto exit;
291 }
292 /* find platform data */
293 if (platp) {
294 hw->gpio_cs_count = platp->gpio_cs_count;
295 hw->gpio_cs = platp->gpio_cs;
Julia Lawallb3136f82013-08-24 19:13:15 +0200296 if (platp->gpio_cs_count && !platp->gpio_cs) {
297 err = -EBUSY;
298 goto exit;
299 }
Thomas Chouce792582011-02-14 10:20:39 +0800300 hw->freq = platp->freq;
301 hw->baudwidth = platp->baudwidth;
302 } else {
303 err = tiny_spi_of_probe(pdev);
304 if (err)
305 goto exit;
306 }
307 for (i = 0; i < hw->gpio_cs_count; i++) {
308 err = gpio_request(hw->gpio_cs[i], dev_name(&pdev->dev));
309 if (err)
310 goto exit_gpio;
311 gpio_direction_output(hw->gpio_cs[i], 1);
312 }
Grant Likelye80beb22013-02-12 17:48:37 +0000313 hw->bitbang.master->num_chipselect = max(1, hw->gpio_cs_count);
Thomas Chouce792582011-02-14 10:20:39 +0800314
315 /* register our spi controller */
316 err = spi_bitbang_start(&hw->bitbang);
317 if (err)
318 goto exit;
319 dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
320
321 return 0;
322
323exit_gpio:
324 while (i-- > 0)
325 gpio_free(hw->gpio_cs[i]);
Thomas Chouce792582011-02-14 10:20:39 +0800326exit:
Thomas Chouce792582011-02-14 10:20:39 +0800327 spi_master_put(master);
328 return err;
329}
330
Grant Likelyfd4a3192012-12-07 16:57:14 +0000331static int tiny_spi_remove(struct platform_device *pdev)
Thomas Chouce792582011-02-14 10:20:39 +0800332{
333 struct tiny_spi *hw = platform_get_drvdata(pdev);
334 struct spi_master *master = hw->bitbang.master;
335 unsigned int i;
336
337 spi_bitbang_stop(&hw->bitbang);
338 for (i = 0; i < hw->gpio_cs_count; i++)
339 gpio_free(hw->gpio_cs[i]);
Thomas Chouce792582011-02-14 10:20:39 +0800340 spi_master_put(master);
341 return 0;
342}
343
344#ifdef CONFIG_OF
345static const struct of_device_id tiny_spi_match[] = {
346 { .compatible = "opencores,tiny-spi-rtlsvn2", },
347 {},
348};
349MODULE_DEVICE_TABLE(of, tiny_spi_match);
Thomas Chouce792582011-02-14 10:20:39 +0800350#endif /* CONFIG_OF */
351
352static struct platform_driver tiny_spi_driver = {
353 .probe = tiny_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000354 .remove = tiny_spi_remove,
Thomas Chouce792582011-02-14 10:20:39 +0800355 .driver = {
356 .name = DRV_NAME,
357 .owner = THIS_MODULE,
358 .pm = NULL,
Sachin Kamat9547acc2013-03-14 15:31:50 +0530359 .of_match_table = of_match_ptr(tiny_spi_match),
Thomas Chouce792582011-02-14 10:20:39 +0800360 },
361};
Grant Likely940ab882011-10-05 11:29:49 -0600362module_platform_driver(tiny_spi_driver);
Thomas Chouce792582011-02-14 10:20:39 +0800363
364MODULE_DESCRIPTION("OpenCores tiny SPI driver");
365MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
366MODULE_LICENSE("GPL");
367MODULE_ALIAS("platform:" DRV_NAME);