2 * OpenCores tiny SPI master driver
4 * http://opencores.org/project,tiny_spi
6 * Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
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>
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.
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #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>
27 #include <linux/gpio.h>
30 #define DRV_NAME "spi_oc_tiny"
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
38 #define TINY_SPI_STATUS_TXE 0x1
39 #define TINY_SPI_STATUS_TXR 0x2
42 /* bitbang has to be first */
43 struct spi_bitbang bitbang
;
44 struct completion done
;
49 unsigned int baudwidth
;
51 unsigned int speed_hz
;
54 unsigned int txc
, rxc
;
61 static inline struct tiny_spi
*tiny_spi_to_hw(struct spi_device
*sdev
)
63 return spi_master_get_devdata(sdev
->master
);
66 static unsigned int tiny_spi_baud(struct spi_device
*spi
, unsigned int hz
)
68 struct tiny_spi
*hw
= tiny_spi_to_hw(spi
);
70 return min(DIV_ROUND_UP(hw
->freq
, hz
* 2), (1U << hw
->baudwidth
)) - 1;
73 static void tiny_spi_chipselect(struct spi_device
*spi
, int is_active
)
75 struct tiny_spi
*hw
= tiny_spi_to_hw(spi
);
77 if (hw
->gpio_cs_count
> 0) {
78 gpio_set_value(hw
->gpio_cs
[spi
->chip_select
],
79 (spi
->mode
& SPI_CS_HIGH
) ? is_active
: !is_active
);
83 static int tiny_spi_setup_transfer(struct spi_device
*spi
,
84 struct spi_transfer
*t
)
86 struct tiny_spi
*hw
= tiny_spi_to_hw(spi
);
87 unsigned int baud
= hw
->baud
;
90 if (t
->speed_hz
&& t
->speed_hz
!= hw
->speed_hz
)
91 baud
= tiny_spi_baud(spi
, t
->speed_hz
);
93 writel(baud
, hw
->base
+ TINY_SPI_BAUD
);
94 writel(hw
->mode
, hw
->base
+ TINY_SPI_CONTROL
);
98 static int tiny_spi_setup(struct spi_device
*spi
)
100 struct tiny_spi
*hw
= tiny_spi_to_hw(spi
);
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
);
106 hw
->mode
= spi
->mode
& (SPI_CPOL
| SPI_CPHA
);
110 static inline void tiny_spi_wait_txr(struct tiny_spi
*hw
)
112 while (!(readb(hw
->base
+ TINY_SPI_STATUS
) &
113 TINY_SPI_STATUS_TXR
))
117 static inline void tiny_spi_wait_txe(struct tiny_spi
*hw
)
119 while (!(readb(hw
->base
+ TINY_SPI_STATUS
) &
120 TINY_SPI_STATUS_TXE
))
124 static int tiny_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
126 struct tiny_spi
*hw
= tiny_spi_to_hw(spi
);
127 const u8
*txp
= t
->tx_buf
;
132 /* use interrupt driven data transfer */
139 /* send the first byte */
141 writeb(hw
->txp
? *hw
->txp
++ : 0,
142 hw
->base
+ TINY_SPI_TXDATA
);
144 writeb(hw
->txp
? *hw
->txp
++ : 0,
145 hw
->base
+ TINY_SPI_TXDATA
);
147 writeb(TINY_SPI_STATUS_TXR
, hw
->base
+ TINY_SPI_STATUS
);
149 writeb(hw
->txp
? *hw
->txp
++ : 0,
150 hw
->base
+ TINY_SPI_TXDATA
);
152 writeb(TINY_SPI_STATUS_TXE
, hw
->base
+ TINY_SPI_STATUS
);
155 wait_for_completion(&hw
->done
);
156 } else if (txp
&& rxp
) {
157 /* we need to tighten the transfer loop */
158 writeb(*txp
++, hw
->base
+ TINY_SPI_TXDATA
);
160 writeb(*txp
++, hw
->base
+ TINY_SPI_TXDATA
);
161 for (i
= 2; i
< t
->len
; i
++) {
163 tiny_spi_wait_txr(hw
);
164 rx
= readb(hw
->base
+ TINY_SPI_TXDATA
);
165 writeb(tx
, hw
->base
+ TINY_SPI_TXDATA
);
168 tiny_spi_wait_txr(hw
);
169 *rxp
++ = readb(hw
->base
+ TINY_SPI_TXDATA
);
171 tiny_spi_wait_txe(hw
);
172 *rxp
++ = readb(hw
->base
+ TINY_SPI_RXDATA
);
174 writeb(0, hw
->base
+ TINY_SPI_TXDATA
);
177 hw
->base
+ TINY_SPI_TXDATA
);
178 for (i
= 2; i
< t
->len
; i
++) {
180 tiny_spi_wait_txr(hw
);
181 rx
= readb(hw
->base
+ TINY_SPI_TXDATA
);
182 writeb(0, hw
->base
+ TINY_SPI_TXDATA
);
185 tiny_spi_wait_txr(hw
);
186 *rxp
++ = readb(hw
->base
+ TINY_SPI_TXDATA
);
188 tiny_spi_wait_txe(hw
);
189 *rxp
++ = readb(hw
->base
+ TINY_SPI_RXDATA
);
191 writeb(*txp
++, hw
->base
+ TINY_SPI_TXDATA
);
193 writeb(*txp
++, hw
->base
+ TINY_SPI_TXDATA
);
194 for (i
= 2; i
< t
->len
; i
++) {
196 tiny_spi_wait_txr(hw
);
197 writeb(tx
, hw
->base
+ TINY_SPI_TXDATA
);
200 tiny_spi_wait_txe(hw
);
202 writeb(0, hw
->base
+ TINY_SPI_TXDATA
);
204 writeb(0, hw
->base
+ TINY_SPI_TXDATA
);
205 for (i
= 2; i
< t
->len
; i
++) {
206 tiny_spi_wait_txr(hw
);
207 writeb(0, hw
->base
+ TINY_SPI_TXDATA
);
210 tiny_spi_wait_txe(hw
);
215 static irqreturn_t
tiny_spi_irq(int irq
, void *dev
)
217 struct tiny_spi
*hw
= dev
;
219 writeb(0, hw
->base
+ TINY_SPI_STATUS
);
220 if (hw
->rxc
+ 1 == hw
->len
) {
222 *hw
->rxp
++ = readb(hw
->base
+ TINY_SPI_RXDATA
);
227 *hw
->rxp
++ = readb(hw
->base
+ TINY_SPI_TXDATA
);
229 if (hw
->txc
< hw
->len
) {
230 writeb(hw
->txp
? *hw
->txp
++ : 0,
231 hw
->base
+ TINY_SPI_TXDATA
);
233 writeb(TINY_SPI_STATUS_TXR
,
234 hw
->base
+ TINY_SPI_STATUS
);
236 writeb(TINY_SPI_STATUS_TXE
,
237 hw
->base
+ TINY_SPI_STATUS
);
244 #include <linux/of_gpio.h>
246 static int tiny_spi_of_probe(struct platform_device
*pdev
)
248 struct tiny_spi
*hw
= platform_get_drvdata(pdev
);
249 struct device_node
*np
= pdev
->dev
.of_node
;
256 hw
->gpio_cs_count
= of_gpio_count(np
);
257 if (hw
->gpio_cs_count
> 0) {
258 hw
->gpio_cs
= devm_kzalloc(&pdev
->dev
,
259 hw
->gpio_cs_count
* sizeof(unsigned int),
264 for (i
= 0; i
< hw
->gpio_cs_count
; i
++) {
265 hw
->gpio_cs
[i
] = of_get_gpio_flags(np
, i
, NULL
);
266 if (hw
->gpio_cs
[i
] < 0)
269 hw
->bitbang
.master
->dev
.of_node
= pdev
->dev
.of_node
;
270 val
= of_get_property(pdev
->dev
.of_node
,
271 "clock-frequency", &len
);
272 if (val
&& len
>= sizeof(__be32
))
273 hw
->freq
= be32_to_cpup(val
);
274 val
= of_get_property(pdev
->dev
.of_node
, "baud-width", &len
);
275 if (val
&& len
>= sizeof(__be32
))
276 hw
->baudwidth
= be32_to_cpup(val
);
279 #else /* !CONFIG_OF */
280 static int tiny_spi_of_probe(struct platform_device
*pdev
)
284 #endif /* CONFIG_OF */
286 static int tiny_spi_probe(struct platform_device
*pdev
)
288 struct tiny_spi_platform_data
*platp
= pdev
->dev
.platform_data
;
290 struct spi_master
*master
;
291 struct resource
*res
;
295 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct tiny_spi
));
299 /* setup the master state. */
300 master
->bus_num
= pdev
->id
;
301 master
->num_chipselect
= 255;
302 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
303 master
->setup
= tiny_spi_setup
;
305 hw
= spi_master_get_devdata(master
);
306 platform_set_drvdata(pdev
, hw
);
308 /* setup the state for the bitbang driver */
309 hw
->bitbang
.master
= spi_master_get(master
);
310 if (!hw
->bitbang
.master
)
312 hw
->bitbang
.setup_transfer
= tiny_spi_setup_transfer
;
313 hw
->bitbang
.chipselect
= tiny_spi_chipselect
;
314 hw
->bitbang
.txrx_bufs
= tiny_spi_txrx_bufs
;
316 /* find and map our resources */
317 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
320 if (!devm_request_mem_region(&pdev
->dev
, res
->start
, resource_size(res
),
323 hw
->base
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
327 /* irq is optional */
328 hw
->irq
= platform_get_irq(pdev
, 0);
330 init_completion(&hw
->done
);
331 err
= devm_request_irq(&pdev
->dev
, hw
->irq
, tiny_spi_irq
, 0,
336 /* find platform data */
338 hw
->gpio_cs_count
= platp
->gpio_cs_count
;
339 hw
->gpio_cs
= platp
->gpio_cs
;
340 if (platp
->gpio_cs_count
&& !platp
->gpio_cs
)
342 hw
->freq
= platp
->freq
;
343 hw
->baudwidth
= platp
->baudwidth
;
345 err
= tiny_spi_of_probe(pdev
);
349 for (i
= 0; i
< hw
->gpio_cs_count
; i
++) {
350 err
= gpio_request(hw
->gpio_cs
[i
], dev_name(&pdev
->dev
));
353 gpio_direction_output(hw
->gpio_cs
[i
], 1);
355 hw
->bitbang
.master
->num_chipselect
= max(1, hw
->gpio_cs_count
);
357 /* register our spi controller */
358 err
= spi_bitbang_start(&hw
->bitbang
);
361 dev_info(&pdev
->dev
, "base %p, irq %d\n", hw
->base
, hw
->irq
);
367 gpio_free(hw
->gpio_cs
[i
]);
371 platform_set_drvdata(pdev
, NULL
);
372 spi_master_put(master
);
376 static int tiny_spi_remove(struct platform_device
*pdev
)
378 struct tiny_spi
*hw
= platform_get_drvdata(pdev
);
379 struct spi_master
*master
= hw
->bitbang
.master
;
382 spi_bitbang_stop(&hw
->bitbang
);
383 for (i
= 0; i
< hw
->gpio_cs_count
; i
++)
384 gpio_free(hw
->gpio_cs
[i
]);
385 platform_set_drvdata(pdev
, NULL
);
386 spi_master_put(master
);
391 static const struct of_device_id tiny_spi_match
[] = {
392 { .compatible
= "opencores,tiny-spi-rtlsvn2", },
395 MODULE_DEVICE_TABLE(of
, tiny_spi_match
);
396 #endif /* CONFIG_OF */
398 static struct platform_driver tiny_spi_driver
= {
399 .probe
= tiny_spi_probe
,
400 .remove
= tiny_spi_remove
,
403 .owner
= THIS_MODULE
,
405 .of_match_table
= of_match_ptr(tiny_spi_match
),
408 module_platform_driver(tiny_spi_driver
);
410 MODULE_DESCRIPTION("OpenCores tiny SPI driver");
411 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
412 MODULE_LICENSE("GPL");
413 MODULE_ALIAS("platform:" DRV_NAME
);