1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012-2013 Uwe Kleine-Koenig for Pengutronix
5 #include <linux/kernel.h>
7 #include <linux/spi/spi.h>
8 #include <linux/spi/spi_bitbang.h>
9 #include <linux/interrupt.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/module.h>
14 #include <linux/platform_data/efm32-spi.h>
17 #define DRIVER_NAME "efm32-spi"
19 #define MASK_VAL(mask, val) ((val << __ffs(mask)) & mask)
22 #define REG_CTRL_SYNC 0x0001
23 #define REG_CTRL_CLKPOL 0x0100
24 #define REG_CTRL_CLKPHA 0x0200
25 #define REG_CTRL_MSBF 0x0400
26 #define REG_CTRL_TXBIL 0x1000
28 #define REG_FRAME 0x04
29 #define REG_FRAME_DATABITS__MASK 0x000f
30 #define REG_FRAME_DATABITS(n) ((n) - 3)
33 #define REG_CMD_RXEN 0x0001
34 #define REG_CMD_RXDIS 0x0002
35 #define REG_CMD_TXEN 0x0004
36 #define REG_CMD_TXDIS 0x0008
37 #define REG_CMD_MASTEREN 0x0010
39 #define REG_STATUS 0x10
40 #define REG_STATUS_TXENS 0x0002
41 #define REG_STATUS_TXC 0x0020
42 #define REG_STATUS_TXBL 0x0040
43 #define REG_STATUS_RXDATAV 0x0080
45 #define REG_CLKDIV 0x14
47 #define REG_RXDATAX 0x18
48 #define REG_RXDATAX_RXDATA__MASK 0x01ff
49 #define REG_RXDATAX_PERR 0x4000
50 #define REG_RXDATAX_FERR 0x8000
52 #define REG_TXDATA 0x34
55 #define REG_IF_TXBL 0x0002
56 #define REG_IF_RXDATAV 0x0004
62 #define REG_ROUTE 0x54
63 #define REG_ROUTE_RXPEN 0x0001
64 #define REG_ROUTE_TXPEN 0x0002
65 #define REG_ROUTE_CLKPEN 0x0008
66 #define REG_ROUTE_LOCATION__MASK 0x0700
67 #define REG_ROUTE_LOCATION(n) MASK_VAL(REG_ROUTE_LOCATION__MASK, (n))
69 struct efm32_spi_ddata
{
70 struct spi_bitbang bitbang
;
76 unsigned int rxirq
, txirq
;
77 struct efm32_spi_pdata pdata
;
80 struct completion done
;
83 unsigned tx_len
, rx_len
;
86 #define ddata_to_dev(ddata) (&(ddata->bitbang.master->dev))
87 #define efm32_spi_vdbg(ddata, format, arg...) \
88 dev_vdbg(ddata_to_dev(ddata), format, ##arg)
90 static void efm32_spi_write32(struct efm32_spi_ddata
*ddata
,
91 u32 value
, unsigned offset
)
93 writel_relaxed(value
, ddata
->base
+ offset
);
96 static u32
efm32_spi_read32(struct efm32_spi_ddata
*ddata
, unsigned offset
)
98 return readl_relaxed(ddata
->base
+ offset
);
101 static int efm32_spi_setup_transfer(struct spi_device
*spi
,
102 struct spi_transfer
*t
)
104 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(spi
->master
);
106 unsigned bpw
= t
->bits_per_word
?: spi
->bits_per_word
;
107 unsigned speed
= t
->speed_hz
?: spi
->max_speed_hz
;
108 unsigned long clkfreq
= clk_get_rate(ddata
->clk
);
111 efm32_spi_write32(ddata
, REG_CTRL_SYNC
| REG_CTRL_MSBF
|
112 (spi
->mode
& SPI_CPHA
? REG_CTRL_CLKPHA
: 0) |
113 (spi
->mode
& SPI_CPOL
? REG_CTRL_CLKPOL
: 0), REG_CTRL
);
115 efm32_spi_write32(ddata
,
116 REG_FRAME_DATABITS(bpw
), REG_FRAME
);
118 if (2 * speed
>= clkfreq
)
121 clkdiv
= 64 * (DIV_ROUND_UP(2 * clkfreq
, speed
) - 4);
123 if (clkdiv
> (1U << 21))
126 efm32_spi_write32(ddata
, clkdiv
, REG_CLKDIV
);
127 efm32_spi_write32(ddata
, REG_CMD_MASTEREN
, REG_CMD
);
128 efm32_spi_write32(ddata
, REG_CMD_RXEN
| REG_CMD_TXEN
, REG_CMD
);
133 static void efm32_spi_tx_u8(struct efm32_spi_ddata
*ddata
)
138 val
= *ddata
->tx_buf
;
143 efm32_spi_write32(ddata
, val
, REG_TXDATA
);
144 efm32_spi_vdbg(ddata
, "%s: tx 0x%x\n", __func__
, val
);
147 static void efm32_spi_rx_u8(struct efm32_spi_ddata
*ddata
)
149 u32 rxdata
= efm32_spi_read32(ddata
, REG_RXDATAX
);
150 efm32_spi_vdbg(ddata
, "%s: rx 0x%x\n", __func__
, rxdata
);
153 *ddata
->rx_buf
= rxdata
;
160 static void efm32_spi_filltx(struct efm32_spi_ddata
*ddata
)
162 while (ddata
->tx_len
&&
163 ddata
->tx_len
+ 2 > ddata
->rx_len
&&
164 efm32_spi_read32(ddata
, REG_STATUS
) & REG_STATUS_TXBL
) {
165 efm32_spi_tx_u8(ddata
);
169 static int efm32_spi_txrx_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
171 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(spi
->master
);
174 spin_lock_irq(&ddata
->lock
);
176 if (ddata
->tx_buf
|| ddata
->rx_buf
)
179 ddata
->tx_buf
= t
->tx_buf
;
180 ddata
->rx_buf
= t
->rx_buf
;
181 ddata
->tx_len
= ddata
->rx_len
=
182 t
->len
* DIV_ROUND_UP(t
->bits_per_word
, 8);
184 efm32_spi_filltx(ddata
);
186 reinit_completion(&ddata
->done
);
188 efm32_spi_write32(ddata
, REG_IF_TXBL
| REG_IF_RXDATAV
, REG_IEN
);
190 spin_unlock_irq(&ddata
->lock
);
192 wait_for_completion(&ddata
->done
);
194 spin_lock_irq(&ddata
->lock
);
196 ret
= t
->len
- max(ddata
->tx_len
, ddata
->rx_len
);
198 efm32_spi_write32(ddata
, 0, REG_IEN
);
199 ddata
->tx_buf
= ddata
->rx_buf
= NULL
;
202 spin_unlock_irq(&ddata
->lock
);
207 static irqreturn_t
efm32_spi_rxirq(int irq
, void *data
)
209 struct efm32_spi_ddata
*ddata
= data
;
210 irqreturn_t ret
= IRQ_NONE
;
212 spin_lock(&ddata
->lock
);
214 while (ddata
->rx_len
> 0 &&
215 efm32_spi_read32(ddata
, REG_STATUS
) &
216 REG_STATUS_RXDATAV
) {
217 efm32_spi_rx_u8(ddata
);
222 if (!ddata
->rx_len
) {
223 u32 ien
= efm32_spi_read32(ddata
, REG_IEN
);
225 ien
&= ~REG_IF_RXDATAV
;
227 efm32_spi_write32(ddata
, ien
, REG_IEN
);
229 complete(&ddata
->done
);
232 spin_unlock(&ddata
->lock
);
237 static irqreturn_t
efm32_spi_txirq(int irq
, void *data
)
239 struct efm32_spi_ddata
*ddata
= data
;
241 efm32_spi_vdbg(ddata
,
242 "%s: txlen = %u, rxlen = %u, if=0x%08x, stat=0x%08x\n",
243 __func__
, ddata
->tx_len
, ddata
->rx_len
,
244 efm32_spi_read32(ddata
, REG_IF
),
245 efm32_spi_read32(ddata
, REG_STATUS
));
247 spin_lock(&ddata
->lock
);
249 efm32_spi_filltx(ddata
);
251 efm32_spi_vdbg(ddata
, "%s: txlen = %u, rxlen = %u\n",
252 __func__
, ddata
->tx_len
, ddata
->rx_len
);
254 if (!ddata
->tx_len
) {
255 u32 ien
= efm32_spi_read32(ddata
, REG_IEN
);
259 efm32_spi_write32(ddata
, ien
, REG_IEN
);
260 efm32_spi_vdbg(ddata
, "disable TXBL\n");
263 spin_unlock(&ddata
->lock
);
268 static u32
efm32_spi_get_configured_location(struct efm32_spi_ddata
*ddata
)
270 u32 reg
= efm32_spi_read32(ddata
, REG_ROUTE
);
272 return (reg
& REG_ROUTE_LOCATION__MASK
) >> __ffs(REG_ROUTE_LOCATION__MASK
);
275 static void efm32_spi_probe_dt(struct platform_device
*pdev
,
276 struct spi_master
*master
, struct efm32_spi_ddata
*ddata
)
278 struct device_node
*np
= pdev
->dev
.of_node
;
282 ret
= of_property_read_u32(np
, "energymicro,location", &location
);
285 /* fall back to wrongly namespaced property */
286 ret
= of_property_read_u32(np
, "efm32,location", &location
);
289 /* fall back to old and (wrongly) generic property "location" */
290 ret
= of_property_read_u32(np
, "location", &location
);
293 dev_dbg(&pdev
->dev
, "using location %u\n", location
);
295 /* default to location configured in hardware */
296 location
= efm32_spi_get_configured_location(ddata
);
298 dev_info(&pdev
->dev
, "fall back to location %u\n", location
);
301 ddata
->pdata
.location
= location
;
304 static int efm32_spi_probe(struct platform_device
*pdev
)
306 struct efm32_spi_ddata
*ddata
;
307 struct resource
*res
;
309 struct spi_master
*master
;
310 struct device_node
*np
= pdev
->dev
.of_node
;
315 master
= spi_alloc_master(&pdev
->dev
, sizeof(*ddata
));
318 "failed to allocate spi master controller\n");
321 platform_set_drvdata(pdev
, master
);
323 master
->dev
.of_node
= pdev
->dev
.of_node
;
325 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
326 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
327 master
->use_gpio_descriptors
= true;
329 ddata
= spi_master_get_devdata(master
);
331 ddata
->bitbang
.master
= master
;
332 ddata
->bitbang
.setup_transfer
= efm32_spi_setup_transfer
;
333 ddata
->bitbang
.txrx_bufs
= efm32_spi_txrx_bufs
;
335 spin_lock_init(&ddata
->lock
);
336 init_completion(&ddata
->done
);
338 ddata
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
339 if (IS_ERR(ddata
->clk
)) {
340 ret
= PTR_ERR(ddata
->clk
);
341 dev_err(&pdev
->dev
, "failed to get clock: %d\n", ret
);
345 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
348 dev_err(&pdev
->dev
, "failed to determine base address\n");
352 if (resource_size(res
) < 0x60) {
354 dev_err(&pdev
->dev
, "memory resource too small\n");
358 ddata
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
359 if (IS_ERR(ddata
->base
)) {
360 ret
= PTR_ERR(ddata
->base
);
364 ret
= platform_get_irq(pdev
, 0);
370 ret
= platform_get_irq(pdev
, 1);
372 ret
= ddata
->rxirq
+ 1;
376 ret
= clk_prepare_enable(ddata
->clk
);
378 dev_err(&pdev
->dev
, "failed to enable clock (%d)\n", ret
);
382 efm32_spi_probe_dt(pdev
, master
, ddata
);
384 efm32_spi_write32(ddata
, 0, REG_IEN
);
385 efm32_spi_write32(ddata
, REG_ROUTE_TXPEN
| REG_ROUTE_RXPEN
|
387 REG_ROUTE_LOCATION(ddata
->pdata
.location
), REG_ROUTE
);
389 ret
= request_irq(ddata
->rxirq
, efm32_spi_rxirq
,
390 0, DRIVER_NAME
" rx", ddata
);
392 dev_err(&pdev
->dev
, "failed to register rxirq (%d)\n", ret
);
393 goto err_disable_clk
;
396 ret
= request_irq(ddata
->txirq
, efm32_spi_txirq
,
397 0, DRIVER_NAME
" tx", ddata
);
399 dev_err(&pdev
->dev
, "failed to register txirq (%d)\n", ret
);
400 goto err_free_rx_irq
;
403 ret
= spi_bitbang_start(&ddata
->bitbang
);
405 dev_err(&pdev
->dev
, "spi_bitbang_start failed (%d)\n", ret
);
407 free_irq(ddata
->txirq
, ddata
);
409 free_irq(ddata
->rxirq
, ddata
);
411 clk_disable_unprepare(ddata
->clk
);
413 spi_master_put(master
);
419 static int efm32_spi_remove(struct platform_device
*pdev
)
421 struct spi_master
*master
= platform_get_drvdata(pdev
);
422 struct efm32_spi_ddata
*ddata
= spi_master_get_devdata(master
);
424 spi_bitbang_stop(&ddata
->bitbang
);
426 efm32_spi_write32(ddata
, 0, REG_IEN
);
428 free_irq(ddata
->txirq
, ddata
);
429 free_irq(ddata
->rxirq
, ddata
);
430 clk_disable_unprepare(ddata
->clk
);
431 spi_master_put(master
);
436 static const struct of_device_id efm32_spi_dt_ids
[] = {
438 .compatible
= "energymicro,efm32-spi",
440 /* doesn't follow the "vendor,device" scheme, don't use */
441 .compatible
= "efm32,spi",
446 MODULE_DEVICE_TABLE(of
, efm32_spi_dt_ids
);
448 static struct platform_driver efm32_spi_driver
= {
449 .probe
= efm32_spi_probe
,
450 .remove
= efm32_spi_remove
,
454 .of_match_table
= efm32_spi_dt_ids
,
457 module_platform_driver(efm32_spi_driver
);
459 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
460 MODULE_DESCRIPTION("EFM32 SPI driver");
461 MODULE_LICENSE("GPL v2");
462 MODULE_ALIAS("platform:" DRIVER_NAME
);