1 // SPDX-License-Identifier: GPL-2.0+
3 // Freescale i.MX7ULP LPSPI driver
5 // Copyright 2016 Freescale Semiconductor, Inc.
6 // Copyright 2018 NXP Semiconductors
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spi/spi_bitbang.h>
23 #include <linux/types.h>
25 #define DRIVER_NAME "fsl_lpspi"
27 /* i.MX7ULP LPSPI registers */
28 #define IMX7ULP_VERID 0x0
29 #define IMX7ULP_PARAM 0x4
30 #define IMX7ULP_CR 0x10
31 #define IMX7ULP_SR 0x14
32 #define IMX7ULP_IER 0x18
33 #define IMX7ULP_DER 0x1c
34 #define IMX7ULP_CFGR0 0x20
35 #define IMX7ULP_CFGR1 0x24
36 #define IMX7ULP_DMR0 0x30
37 #define IMX7ULP_DMR1 0x34
38 #define IMX7ULP_CCR 0x40
39 #define IMX7ULP_FCR 0x58
40 #define IMX7ULP_FSR 0x5c
41 #define IMX7ULP_TCR 0x60
42 #define IMX7ULP_TDR 0x64
43 #define IMX7ULP_RSR 0x70
44 #define IMX7ULP_RDR 0x74
46 /* General control register field define */
51 #define SR_MBF BIT(24)
52 #define SR_TCF BIT(10)
56 #define IER_TCIE BIT(10)
57 #define IER_FCIE BIT(9)
58 #define IER_RDIE BIT(1)
59 #define IER_TDIE BIT(0)
60 #define CFGR1_PCSCFG BIT(27)
61 #define CFGR1_PINCFG (BIT(24)|BIT(25))
62 #define CFGR1_PCSPOL BIT(8)
63 #define CFGR1_NOSTALL BIT(3)
64 #define CFGR1_MASTER BIT(0)
65 #define FSR_RXCOUNT (BIT(16)|BIT(17)|BIT(18))
66 #define RSR_RXEMPTY BIT(1)
67 #define TCR_CPOL BIT(31)
68 #define TCR_CPHA BIT(30)
69 #define TCR_CONT BIT(21)
70 #define TCR_CONTC BIT(20)
71 #define TCR_RXMSK BIT(19)
72 #define TCR_TXMSK BIT(18)
74 static int clkdivs
[] = {1, 2, 4, 8, 16, 32, 64, 128};
84 struct fsl_lpspi_data
{
92 void (*tx
)(struct fsl_lpspi_data
*);
93 void (*rx
)(struct fsl_lpspi_data
*);
100 struct lpspi_config config
;
101 struct completion xfer_done
;
106 static const struct of_device_id fsl_lpspi_dt_ids
[] = {
107 { .compatible
= "fsl,imx7ulp-spi", },
110 MODULE_DEVICE_TABLE(of
, fsl_lpspi_dt_ids
);
112 #define LPSPI_BUF_RX(type) \
113 static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
115 unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
117 if (fsl_lpspi->rx_buf) { \
118 *(type *)fsl_lpspi->rx_buf = val; \
119 fsl_lpspi->rx_buf += sizeof(type); \
123 #define LPSPI_BUF_TX(type) \
124 static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
128 if (fsl_lpspi->tx_buf) { \
129 val = *(type *)fsl_lpspi->tx_buf; \
130 fsl_lpspi->tx_buf += sizeof(type); \
133 fsl_lpspi->remain -= sizeof(type); \
134 writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
144 static void fsl_lpspi_intctrl(struct fsl_lpspi_data
*fsl_lpspi
,
147 writel(enable
, fsl_lpspi
->base
+ IMX7ULP_IER
);
150 static int lpspi_prepare_xfer_hardware(struct spi_controller
*controller
)
152 struct fsl_lpspi_data
*fsl_lpspi
=
153 spi_controller_get_devdata(controller
);
155 return clk_prepare_enable(fsl_lpspi
->clk
);
158 static int lpspi_unprepare_xfer_hardware(struct spi_controller
*controller
)
160 struct fsl_lpspi_data
*fsl_lpspi
=
161 spi_controller_get_devdata(controller
);
163 clk_disable_unprepare(fsl_lpspi
->clk
);
168 static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data
*fsl_lpspi
)
173 txfifo_cnt
= readl(fsl_lpspi
->base
+ IMX7ULP_FSR
) & 0xff;
175 while (txfifo_cnt
< fsl_lpspi
->txfifosize
) {
176 if (!fsl_lpspi
->remain
)
178 fsl_lpspi
->tx(fsl_lpspi
);
182 if (txfifo_cnt
< fsl_lpspi
->txfifosize
) {
183 if (!fsl_lpspi
->is_slave
) {
184 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_TCR
);
186 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_TCR
);
189 fsl_lpspi_intctrl(fsl_lpspi
, IER_FCIE
);
191 fsl_lpspi_intctrl(fsl_lpspi
, IER_TDIE
);
194 static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data
*fsl_lpspi
)
196 while (!(readl(fsl_lpspi
->base
+ IMX7ULP_RSR
) & RSR_RXEMPTY
))
197 fsl_lpspi
->rx(fsl_lpspi
);
200 static void fsl_lpspi_set_cmd(struct fsl_lpspi_data
*fsl_lpspi
,
205 temp
|= fsl_lpspi
->config
.bpw
- 1;
206 temp
|= (fsl_lpspi
->config
.mode
& 0x3) << 30;
207 if (!fsl_lpspi
->is_slave
) {
208 temp
|= fsl_lpspi
->config
.prescale
<< 27;
209 temp
|= (fsl_lpspi
->config
.chip_select
& 0x3) << 24;
212 * Set TCR_CONT will keep SS asserted after current transfer.
213 * For the first transfer, clear TCR_CONTC to assert SS.
214 * For subsequent transfer, set TCR_CONTC to keep SS asserted.
222 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_TCR
);
224 dev_dbg(fsl_lpspi
->dev
, "TCR=0x%x\n", temp
);
227 static void fsl_lpspi_set_watermark(struct fsl_lpspi_data
*fsl_lpspi
)
231 temp
= fsl_lpspi
->watermark
>> 1 | (fsl_lpspi
->watermark
>> 1) << 16;
233 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_FCR
);
235 dev_dbg(fsl_lpspi
->dev
, "FCR=0x%x\n", temp
);
238 static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data
*fsl_lpspi
)
240 struct lpspi_config config
= fsl_lpspi
->config
;
241 unsigned int perclk_rate
, scldiv
;
244 perclk_rate
= clk_get_rate(fsl_lpspi
->clk
);
245 for (prescale
= 0; prescale
< 8; prescale
++) {
246 scldiv
= perclk_rate
/
247 (clkdivs
[prescale
] * config
.speed_hz
) - 2;
249 fsl_lpspi
->config
.prescale
= prescale
;
254 if (prescale
== 8 && scldiv
>= 256)
257 writel(scldiv
| (scldiv
<< 8) | ((scldiv
>> 1) << 16),
258 fsl_lpspi
->base
+ IMX7ULP_CCR
);
260 dev_dbg(fsl_lpspi
->dev
, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",
261 perclk_rate
, config
.speed_hz
, prescale
, scldiv
);
266 static int fsl_lpspi_config(struct fsl_lpspi_data
*fsl_lpspi
)
271 if (!fsl_lpspi
->is_slave
) {
272 ret
= fsl_lpspi_set_bitrate(fsl_lpspi
);
277 fsl_lpspi_set_watermark(fsl_lpspi
);
279 if (!fsl_lpspi
->is_slave
)
283 if (fsl_lpspi
->config
.mode
& SPI_CS_HIGH
)
284 temp
|= CFGR1_PCSPOL
;
285 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CFGR1
);
287 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_CR
);
288 temp
|= CR_RRF
| CR_RTF
| CR_MEN
;
289 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CR
);
294 static void fsl_lpspi_setup_transfer(struct spi_device
*spi
,
295 struct spi_transfer
*t
)
297 struct fsl_lpspi_data
*fsl_lpspi
=
298 spi_controller_get_devdata(spi
->controller
);
300 fsl_lpspi
->config
.mode
= spi
->mode
;
301 fsl_lpspi
->config
.bpw
= t
? t
->bits_per_word
: spi
->bits_per_word
;
302 fsl_lpspi
->config
.speed_hz
= t
? t
->speed_hz
: spi
->max_speed_hz
;
303 fsl_lpspi
->config
.chip_select
= spi
->chip_select
;
305 if (!fsl_lpspi
->config
.speed_hz
)
306 fsl_lpspi
->config
.speed_hz
= spi
->max_speed_hz
;
307 if (!fsl_lpspi
->config
.bpw
)
308 fsl_lpspi
->config
.bpw
= spi
->bits_per_word
;
310 /* Initialize the functions for transfer */
311 if (fsl_lpspi
->config
.bpw
<= 8) {
312 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u8
;
313 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u8
;
314 } else if (fsl_lpspi
->config
.bpw
<= 16) {
315 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u16
;
316 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u16
;
318 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u32
;
319 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u32
;
322 if (t
->len
<= fsl_lpspi
->txfifosize
)
323 fsl_lpspi
->watermark
= t
->len
;
325 fsl_lpspi
->watermark
= fsl_lpspi
->txfifosize
;
327 fsl_lpspi_config(fsl_lpspi
);
330 static int fsl_lpspi_slave_abort(struct spi_controller
*controller
)
332 struct fsl_lpspi_data
*fsl_lpspi
=
333 spi_controller_get_devdata(controller
);
335 fsl_lpspi
->slave_aborted
= true;
336 complete(&fsl_lpspi
->xfer_done
);
340 static int fsl_lpspi_wait_for_completion(struct spi_controller
*controller
)
342 struct fsl_lpspi_data
*fsl_lpspi
=
343 spi_controller_get_devdata(controller
);
345 if (fsl_lpspi
->is_slave
) {
346 if (wait_for_completion_interruptible(&fsl_lpspi
->xfer_done
) ||
347 fsl_lpspi
->slave_aborted
) {
348 dev_dbg(fsl_lpspi
->dev
, "interrupted\n");
352 if (!wait_for_completion_timeout(&fsl_lpspi
->xfer_done
, HZ
)) {
353 dev_dbg(fsl_lpspi
->dev
, "wait for completion timeout\n");
361 static int fsl_lpspi_reset(struct fsl_lpspi_data
*fsl_lpspi
)
365 /* Disable all interrupt */
366 fsl_lpspi_intctrl(fsl_lpspi
, 0);
368 /* W1C for all flags in SR */
370 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_SR
);
372 /* Clear FIFO and disable module */
373 temp
= CR_RRF
| CR_RTF
;
374 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CR
);
379 static int fsl_lpspi_transfer_one(struct spi_controller
*controller
,
380 struct spi_device
*spi
,
381 struct spi_transfer
*t
)
383 struct fsl_lpspi_data
*fsl_lpspi
=
384 spi_controller_get_devdata(controller
);
387 fsl_lpspi
->tx_buf
= t
->tx_buf
;
388 fsl_lpspi
->rx_buf
= t
->rx_buf
;
389 fsl_lpspi
->remain
= t
->len
;
391 reinit_completion(&fsl_lpspi
->xfer_done
);
392 fsl_lpspi
->slave_aborted
= false;
394 fsl_lpspi_write_tx_fifo(fsl_lpspi
);
396 ret
= fsl_lpspi_wait_for_completion(controller
);
400 fsl_lpspi_reset(fsl_lpspi
);
405 static int fsl_lpspi_transfer_one_msg(struct spi_controller
*controller
,
406 struct spi_message
*msg
)
408 struct fsl_lpspi_data
*fsl_lpspi
=
409 spi_controller_get_devdata(controller
);
410 struct spi_device
*spi
= msg
->spi
;
411 struct spi_transfer
*xfer
;
412 bool is_first_xfer
= true;
416 msg
->actual_length
= 0;
418 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
419 fsl_lpspi_setup_transfer(spi
, xfer
);
420 fsl_lpspi_set_cmd(fsl_lpspi
, is_first_xfer
);
422 is_first_xfer
= false;
424 ret
= fsl_lpspi_transfer_one(controller
, spi
, xfer
);
428 msg
->actual_length
+= xfer
->len
;
433 spi_finalize_current_message(controller
);
438 static irqreturn_t
fsl_lpspi_isr(int irq
, void *dev_id
)
440 u32 temp_SR
, temp_IER
;
441 struct fsl_lpspi_data
*fsl_lpspi
= dev_id
;
443 temp_IER
= readl(fsl_lpspi
->base
+ IMX7ULP_IER
);
444 fsl_lpspi_intctrl(fsl_lpspi
, 0);
445 temp_SR
= readl(fsl_lpspi
->base
+ IMX7ULP_SR
);
447 fsl_lpspi_read_rx_fifo(fsl_lpspi
);
449 if ((temp_SR
& SR_TDF
) && (temp_IER
& IER_TDIE
)) {
450 fsl_lpspi_write_tx_fifo(fsl_lpspi
);
454 if (temp_SR
& SR_MBF
||
455 readl(fsl_lpspi
->base
+ IMX7ULP_FSR
) & FSR_RXCOUNT
) {
456 writel(SR_FCF
, fsl_lpspi
->base
+ IMX7ULP_SR
);
457 fsl_lpspi_intctrl(fsl_lpspi
, IER_FCIE
);
461 if (temp_SR
& SR_FCF
&& (temp_IER
& IER_FCIE
)) {
462 writel(SR_FCF
, fsl_lpspi
->base
+ IMX7ULP_SR
);
463 complete(&fsl_lpspi
->xfer_done
);
470 static int fsl_lpspi_probe(struct platform_device
*pdev
)
472 struct fsl_lpspi_data
*fsl_lpspi
;
473 struct spi_controller
*controller
;
474 struct resource
*res
;
478 if (of_property_read_bool((&pdev
->dev
)->of_node
, "spi-slave"))
479 controller
= spi_alloc_slave(&pdev
->dev
,
480 sizeof(struct fsl_lpspi_data
));
482 controller
= spi_alloc_master(&pdev
->dev
,
483 sizeof(struct fsl_lpspi_data
));
488 platform_set_drvdata(pdev
, controller
);
490 controller
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(8, 32);
491 controller
->bus_num
= pdev
->id
;
493 fsl_lpspi
= spi_controller_get_devdata(controller
);
494 fsl_lpspi
->dev
= &pdev
->dev
;
495 fsl_lpspi
->is_slave
= of_property_read_bool((&pdev
->dev
)->of_node
,
498 controller
->transfer_one_message
= fsl_lpspi_transfer_one_msg
;
499 controller
->prepare_transfer_hardware
= lpspi_prepare_xfer_hardware
;
500 controller
->unprepare_transfer_hardware
= lpspi_unprepare_xfer_hardware
;
501 controller
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
502 controller
->flags
= SPI_MASTER_MUST_RX
| SPI_MASTER_MUST_TX
;
503 controller
->dev
.of_node
= pdev
->dev
.of_node
;
504 controller
->bus_num
= pdev
->id
;
505 controller
->slave_abort
= fsl_lpspi_slave_abort
;
507 init_completion(&fsl_lpspi
->xfer_done
);
509 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
510 fsl_lpspi
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
511 if (IS_ERR(fsl_lpspi
->base
)) {
512 ret
= PTR_ERR(fsl_lpspi
->base
);
513 goto out_controller_put
;
516 irq
= platform_get_irq(pdev
, 0);
519 goto out_controller_put
;
522 ret
= devm_request_irq(&pdev
->dev
, irq
, fsl_lpspi_isr
, 0,
523 dev_name(&pdev
->dev
), fsl_lpspi
);
525 dev_err(&pdev
->dev
, "can't get irq%d: %d\n", irq
, ret
);
526 goto out_controller_put
;
529 fsl_lpspi
->clk
= devm_clk_get(&pdev
->dev
, "ipg");
530 if (IS_ERR(fsl_lpspi
->clk
)) {
531 ret
= PTR_ERR(fsl_lpspi
->clk
);
532 goto out_controller_put
;
535 ret
= clk_prepare_enable(fsl_lpspi
->clk
);
537 dev_err(&pdev
->dev
, "can't enable lpspi clock, ret=%d\n", ret
);
538 goto out_controller_put
;
541 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_PARAM
);
542 fsl_lpspi
->txfifosize
= 1 << (temp
& 0x0f);
543 fsl_lpspi
->rxfifosize
= 1 << ((temp
>> 8) & 0x0f);
545 clk_disable_unprepare(fsl_lpspi
->clk
);
547 ret
= devm_spi_register_controller(&pdev
->dev
, controller
);
549 dev_err(&pdev
->dev
, "spi_register_controller error.\n");
550 goto out_controller_put
;
556 spi_controller_put(controller
);
561 static int fsl_lpspi_remove(struct platform_device
*pdev
)
563 struct spi_controller
*controller
= platform_get_drvdata(pdev
);
564 struct fsl_lpspi_data
*fsl_lpspi
=
565 spi_controller_get_devdata(controller
);
567 clk_disable_unprepare(fsl_lpspi
->clk
);
572 static struct platform_driver fsl_lpspi_driver
= {
575 .of_match_table
= fsl_lpspi_dt_ids
,
577 .probe
= fsl_lpspi_probe
,
578 .remove
= fsl_lpspi_remove
,
580 module_platform_driver(fsl_lpspi_driver
);
582 MODULE_DESCRIPTION("LPSPI Controller driver");
583 MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
584 MODULE_LICENSE("GPL");