2 * Copyright (C) 2012-2013 Uwe Kleine-Koenig for Pengutronix
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License version 2 as published by the
6 * Free Software Foundation.
8 #include <linux/kernel.h>
10 #include <linux/spi/spi.h>
11 #include <linux/spi/spi_bitbang.h>
12 #include <linux/gpio.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/clk.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_data/efm32-spi.h>
21 #define DRIVER_NAME "efm32-spi"
23 #define MASK_VAL(mask, val) ((val << __ffs(mask)) & mask)
26 #define REG_CTRL_SYNC 0x0001
27 #define REG_CTRL_CLKPOL 0x0100
28 #define REG_CTRL_CLKPHA 0x0200
29 #define REG_CTRL_MSBF 0x0400
30 #define REG_CTRL_TXBIL 0x1000
32 #define REG_FRAME 0x04
33 #define REG_FRAME_DATABITS__MASK 0x000f
34 #define REG_FRAME_DATABITS(n) ((n) - 3)
37 #define REG_CMD_RXEN 0x0001
38 #define REG_CMD_RXDIS 0x0002
39 #define REG_CMD_TXEN 0x0004
40 #define REG_CMD_TXDIS 0x0008
41 #define REG_CMD_MASTEREN 0x0010
43 #define REG_STATUS 0x10
44 #define REG_STATUS_TXENS 0x0002
45 #define REG_STATUS_TXC 0x0020
46 #define REG_STATUS_TXBL 0x0040
47 #define REG_STATUS_RXDATAV 0x0080
49 #define REG_CLKDIV 0x14
51 #define REG_RXDATAX 0x18
52 #define REG_RXDATAX_RXDATA__MASK 0x01ff
53 #define REG_RXDATAX_PERR 0x4000
54 #define REG_RXDATAX_FERR 0x8000
56 #define REG_TXDATA 0x34
59 #define REG_IF_TXBL 0x0002
60 #define REG_IF_RXDATAV 0x0004
66 #define REG_ROUTE 0x54
67 #define REG_ROUTE_RXPEN 0x0001
68 #define REG_ROUTE_TXPEN 0x0002
69 #define REG_ROUTE_CLKPEN 0x0008
70 #define REG_ROUTE_LOCATION__MASK 0x0700
71 #define REG_ROUTE_LOCATION(n) MASK_VAL(REG_ROUTE_LOCATION__MASK, (n))
73 struct efm32_spi_ddata
{
74 struct spi_bitbang bitbang
;
80 unsigned int rxirq
, txirq
;
81 struct efm32_spi_pdata pdata
;
84 struct completion done
;
87 unsigned tx_len
, rx_len
;
93 #define ddata_to_dev(ddata) (&(ddata->bitbang.master->dev))
94 #define efm32_spi_vdbg(ddata, format, arg...) \
95 dev_vdbg(ddata_to_dev(ddata), format, ##arg)
97 static void efm32_spi_write32(struct efm32_spi_ddata
*ddata
,
98 u32 value
, unsigned offset
)
100 writel_relaxed(value
, ddata
->base
+ offset
);
103 static u32
efm32_spi_read32(struct efm32_spi_ddata
*ddata
, unsigned offset
)
105 return readl_relaxed(ddata
->base
+ offset
);
108 static void efm32_spi_chipselect(struct spi_device
*spi
, int is_on
)
110 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(spi
->master
);
111 int value
= !(spi
->mode
& SPI_CS_HIGH
) == !(is_on
== BITBANG_CS_ACTIVE
);
113 gpio_set_value(ddata
->csgpio
[spi
->chip_select
], value
);
116 static int efm32_spi_setup_transfer(struct spi_device
*spi
,
117 struct spi_transfer
*t
)
119 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(spi
->master
);
121 unsigned bpw
= t
->bits_per_word
?: spi
->bits_per_word
;
122 unsigned speed
= t
->speed_hz
?: spi
->max_speed_hz
;
123 unsigned long clkfreq
= clk_get_rate(ddata
->clk
);
126 efm32_spi_write32(ddata
, REG_CTRL_SYNC
| REG_CTRL_MSBF
|
127 (spi
->mode
& SPI_CPHA
? REG_CTRL_CLKPHA
: 0) |
128 (spi
->mode
& SPI_CPOL
? REG_CTRL_CLKPOL
: 0), REG_CTRL
);
130 efm32_spi_write32(ddata
,
131 REG_FRAME_DATABITS(bpw
), REG_FRAME
);
133 if (2 * speed
>= clkfreq
)
136 clkdiv
= 64 * (DIV_ROUND_UP(2 * clkfreq
, speed
) - 4);
138 if (clkdiv
> (1U << 21))
141 efm32_spi_write32(ddata
, clkdiv
, REG_CLKDIV
);
142 efm32_spi_write32(ddata
, REG_CMD_MASTEREN
, REG_CMD
);
143 efm32_spi_write32(ddata
, REG_CMD_RXEN
| REG_CMD_TXEN
, REG_CMD
);
148 static void efm32_spi_tx_u8(struct efm32_spi_ddata
*ddata
)
153 val
= *ddata
->tx_buf
;
158 efm32_spi_write32(ddata
, val
, REG_TXDATA
);
159 efm32_spi_vdbg(ddata
, "%s: tx 0x%x\n", __func__
, val
);
162 static void efm32_spi_rx_u8(struct efm32_spi_ddata
*ddata
)
164 u32 rxdata
= efm32_spi_read32(ddata
, REG_RXDATAX
);
165 efm32_spi_vdbg(ddata
, "%s: rx 0x%x\n", __func__
, rxdata
);
168 *ddata
->rx_buf
= rxdata
;
175 static void efm32_spi_filltx(struct efm32_spi_ddata
*ddata
)
177 while (ddata
->tx_len
&&
178 ddata
->tx_len
+ 2 > ddata
->rx_len
&&
179 efm32_spi_read32(ddata
, REG_STATUS
) & REG_STATUS_TXBL
) {
180 efm32_spi_tx_u8(ddata
);
184 static int efm32_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
186 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(spi
->master
);
189 spin_lock_irq(&ddata
->lock
);
191 if (ddata
->tx_buf
|| ddata
->rx_buf
)
194 ddata
->tx_buf
= t
->tx_buf
;
195 ddata
->rx_buf
= t
->rx_buf
;
196 ddata
->tx_len
= ddata
->rx_len
=
197 t
->len
* DIV_ROUND_UP(t
->bits_per_word
, 8);
199 efm32_spi_filltx(ddata
);
201 reinit_completion(&ddata
->done
);
203 efm32_spi_write32(ddata
, REG_IF_TXBL
| REG_IF_RXDATAV
, REG_IEN
);
205 spin_unlock_irq(&ddata
->lock
);
207 wait_for_completion(&ddata
->done
);
209 spin_lock_irq(&ddata
->lock
);
211 ret
= t
->len
- max(ddata
->tx_len
, ddata
->rx_len
);
213 efm32_spi_write32(ddata
, 0, REG_IEN
);
214 ddata
->tx_buf
= ddata
->rx_buf
= NULL
;
217 spin_unlock_irq(&ddata
->lock
);
222 static irqreturn_t
efm32_spi_rxirq(int irq
, void *data
)
224 struct efm32_spi_ddata
*ddata
= data
;
225 irqreturn_t ret
= IRQ_NONE
;
227 spin_lock(&ddata
->lock
);
229 while (ddata
->rx_len
> 0 &&
230 efm32_spi_read32(ddata
, REG_STATUS
) &
231 REG_STATUS_RXDATAV
) {
232 efm32_spi_rx_u8(ddata
);
237 if (!ddata
->rx_len
) {
238 u32 ien
= efm32_spi_read32(ddata
, REG_IEN
);
240 ien
&= ~REG_IF_RXDATAV
;
242 efm32_spi_write32(ddata
, ien
, REG_IEN
);
244 complete(&ddata
->done
);
247 spin_unlock(&ddata
->lock
);
252 static irqreturn_t
efm32_spi_txirq(int irq
, void *data
)
254 struct efm32_spi_ddata
*ddata
= data
;
256 efm32_spi_vdbg(ddata
,
257 "%s: txlen = %u, rxlen = %u, if=0x%08x, stat=0x%08x\n",
258 __func__
, ddata
->tx_len
, ddata
->rx_len
,
259 efm32_spi_read32(ddata
, REG_IF
),
260 efm32_spi_read32(ddata
, REG_STATUS
));
262 spin_lock(&ddata
->lock
);
264 efm32_spi_filltx(ddata
);
266 efm32_spi_vdbg(ddata
, "%s: txlen = %u, rxlen = %u\n",
267 __func__
, ddata
->tx_len
, ddata
->rx_len
);
269 if (!ddata
->tx_len
) {
270 u32 ien
= efm32_spi_read32(ddata
, REG_IEN
);
274 efm32_spi_write32(ddata
, ien
, REG_IEN
);
275 efm32_spi_vdbg(ddata
, "disable TXBL\n");
278 spin_unlock(&ddata
->lock
);
283 static u32
efm32_spi_get_configured_location(struct efm32_spi_ddata
*ddata
)
285 u32 reg
= efm32_spi_read32(ddata
, REG_ROUTE
);
287 return (reg
& REG_ROUTE_LOCATION__MASK
) >> __ffs(REG_ROUTE_LOCATION__MASK
);
290 static void efm32_spi_probe_dt(struct platform_device
*pdev
,
291 struct spi_master
*master
, struct efm32_spi_ddata
*ddata
)
293 struct device_node
*np
= pdev
->dev
.of_node
;
297 ret
= of_property_read_u32(np
, "energymicro,location", &location
);
300 /* fall back to wrongly namespaced property */
301 ret
= of_property_read_u32(np
, "efm32,location", &location
);
304 /* fall back to old and (wrongly) generic property "location" */
305 ret
= of_property_read_u32(np
, "location", &location
);
308 dev_dbg(&pdev
->dev
, "using location %u\n", location
);
310 /* default to location configured in hardware */
311 location
= efm32_spi_get_configured_location(ddata
);
313 dev_info(&pdev
->dev
, "fall back to location %u\n", location
);
316 ddata
->pdata
.location
= location
;
319 static int efm32_spi_probe(struct platform_device
*pdev
)
321 struct efm32_spi_ddata
*ddata
;
322 struct resource
*res
;
324 struct spi_master
*master
;
325 struct device_node
*np
= pdev
->dev
.of_node
;
331 num_cs
= of_gpio_named_count(np
, "cs-gpios");
335 master
= spi_alloc_master(&pdev
->dev
,
336 sizeof(*ddata
) + num_cs
* sizeof(unsigned));
339 "failed to allocate spi master controller\n");
342 platform_set_drvdata(pdev
, master
);
344 master
->dev
.of_node
= pdev
->dev
.of_node
;
346 master
->num_chipselect
= num_cs
;
347 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
348 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
350 ddata
= spi_master_get_devdata(master
);
352 ddata
->bitbang
.master
= master
;
353 ddata
->bitbang
.chipselect
= efm32_spi_chipselect
;
354 ddata
->bitbang
.setup_transfer
= efm32_spi_setup_transfer
;
355 ddata
->bitbang
.txrx_bufs
= efm32_spi_txrx_bufs
;
357 spin_lock_init(&ddata
->lock
);
358 init_completion(&ddata
->done
);
360 ddata
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
361 if (IS_ERR(ddata
->clk
)) {
362 ret
= PTR_ERR(ddata
->clk
);
363 dev_err(&pdev
->dev
, "failed to get clock: %d\n", ret
);
367 for (i
= 0; i
< num_cs
; ++i
) {
368 ret
= of_get_named_gpio(np
, "cs-gpios", i
);
370 dev_err(&pdev
->dev
, "failed to get csgpio#%u (%d)\n",
374 ddata
->csgpio
[i
] = ret
;
375 dev_dbg(&pdev
->dev
, "csgpio#%u = %u\n", i
, ddata
->csgpio
[i
]);
376 ret
= devm_gpio_request_one(&pdev
->dev
, ddata
->csgpio
[i
],
377 GPIOF_OUT_INIT_LOW
, DRIVER_NAME
);
380 "failed to configure csgpio#%u (%d)\n",
386 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
389 dev_err(&pdev
->dev
, "failed to determine base address\n");
393 if (resource_size(res
) < 0x60) {
395 dev_err(&pdev
->dev
, "memory resource too small\n");
399 ddata
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
400 if (IS_ERR(ddata
->base
)) {
401 ret
= PTR_ERR(ddata
->base
);
405 ret
= platform_get_irq(pdev
, 0);
407 dev_err(&pdev
->dev
, "failed to get rx irq (%d)\n", ret
);
413 ret
= platform_get_irq(pdev
, 1);
415 ret
= ddata
->rxirq
+ 1;
419 ret
= clk_prepare_enable(ddata
->clk
);
421 dev_err(&pdev
->dev
, "failed to enable clock (%d)\n", ret
);
425 efm32_spi_probe_dt(pdev
, master
, ddata
);
427 efm32_spi_write32(ddata
, 0, REG_IEN
);
428 efm32_spi_write32(ddata
, REG_ROUTE_TXPEN
| REG_ROUTE_RXPEN
|
430 REG_ROUTE_LOCATION(ddata
->pdata
.location
), REG_ROUTE
);
432 ret
= request_irq(ddata
->rxirq
, efm32_spi_rxirq
,
433 0, DRIVER_NAME
" rx", ddata
);
435 dev_err(&pdev
->dev
, "failed to register rxirq (%d)\n", ret
);
436 goto err_disable_clk
;
439 ret
= request_irq(ddata
->txirq
, efm32_spi_txirq
,
440 0, DRIVER_NAME
" tx", ddata
);
442 dev_err(&pdev
->dev
, "failed to register txirq (%d)\n", ret
);
443 goto err_free_rx_irq
;
446 ret
= spi_bitbang_start(&ddata
->bitbang
);
448 dev_err(&pdev
->dev
, "spi_bitbang_start failed (%d)\n", ret
);
450 free_irq(ddata
->txirq
, ddata
);
452 free_irq(ddata
->rxirq
, ddata
);
454 clk_disable_unprepare(ddata
->clk
);
456 spi_master_put(master
);
462 static int efm32_spi_remove(struct platform_device
*pdev
)
464 struct spi_master
*master
= platform_get_drvdata(pdev
);
465 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(master
);
467 spi_bitbang_stop(&ddata
->bitbang
);
469 efm32_spi_write32(ddata
, 0, REG_IEN
);
471 free_irq(ddata
->txirq
, ddata
);
472 free_irq(ddata
->rxirq
, ddata
);
473 clk_disable_unprepare(ddata
->clk
);
474 spi_master_put(master
);
479 static const struct of_device_id efm32_spi_dt_ids
[] = {
481 .compatible
= "energymicro,efm32-spi",
483 /* doesn't follow the "vendor,device" scheme, don't use */
484 .compatible
= "efm32,spi",
489 MODULE_DEVICE_TABLE(of
, efm32_spi_dt_ids
);
491 static struct platform_driver efm32_spi_driver
= {
492 .probe
= efm32_spi_probe
,
493 .remove
= efm32_spi_remove
,
497 .of_match_table
= efm32_spi_dt_ids
,
500 module_platform_driver(efm32_spi_driver
);
502 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
503 MODULE_DESCRIPTION("EFM32 SPI driver");
504 MODULE_LICENSE("GPL v2");
505 MODULE_ALIAS("platform:" DRIVER_NAME
);