1 // SPDX-License-Identifier: GPL-2.0+
3 // Freescale i.MX7ULP LPSPI driver
5 // Copyright 2016 Freescale Semiconductor, Inc.
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi_bitbang.h>
22 #include <linux/types.h>
24 #define DRIVER_NAME "fsl_lpspi"
26 /* i.MX7ULP LPSPI registers */
27 #define IMX7ULP_VERID 0x0
28 #define IMX7ULP_PARAM 0x4
29 #define IMX7ULP_CR 0x10
30 #define IMX7ULP_SR 0x14
31 #define IMX7ULP_IER 0x18
32 #define IMX7ULP_DER 0x1c
33 #define IMX7ULP_CFGR0 0x20
34 #define IMX7ULP_CFGR1 0x24
35 #define IMX7ULP_DMR0 0x30
36 #define IMX7ULP_DMR1 0x34
37 #define IMX7ULP_CCR 0x40
38 #define IMX7ULP_FCR 0x58
39 #define IMX7ULP_FSR 0x5c
40 #define IMX7ULP_TCR 0x60
41 #define IMX7ULP_TDR 0x64
42 #define IMX7ULP_RSR 0x70
43 #define IMX7ULP_RDR 0x74
45 /* General control register field define */
50 #define SR_TCF BIT(10)
53 #define IER_TCIE BIT(10)
54 #define IER_RDIE BIT(1)
55 #define IER_TDIE BIT(0)
56 #define CFGR1_PCSCFG BIT(27)
57 #define CFGR1_PCSPOL BIT(8)
58 #define CFGR1_NOSTALL BIT(3)
59 #define CFGR1_MASTER BIT(0)
60 #define RSR_RXEMPTY BIT(1)
61 #define TCR_CPOL BIT(31)
62 #define TCR_CPHA BIT(30)
63 #define TCR_CONT BIT(21)
64 #define TCR_CONTC BIT(20)
65 #define TCR_RXMSK BIT(19)
66 #define TCR_TXMSK BIT(18)
68 static int clkdivs
[] = {1, 2, 4, 8, 16, 32, 64, 128};
78 struct fsl_lpspi_data
{
85 void (*tx
)(struct fsl_lpspi_data
*);
86 void (*rx
)(struct fsl_lpspi_data
*);
92 struct lpspi_config config
;
93 struct completion xfer_done
;
96 static const struct of_device_id fsl_lpspi_dt_ids
[] = {
97 { .compatible
= "fsl,imx7ulp-spi", },
100 MODULE_DEVICE_TABLE(of
, fsl_lpspi_dt_ids
);
102 #define LPSPI_BUF_RX(type) \
103 static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
105 unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
107 if (fsl_lpspi->rx_buf) { \
108 *(type *)fsl_lpspi->rx_buf = val; \
109 fsl_lpspi->rx_buf += sizeof(type); \
113 #define LPSPI_BUF_TX(type) \
114 static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
118 if (fsl_lpspi->tx_buf) { \
119 val = *(type *)fsl_lpspi->tx_buf; \
120 fsl_lpspi->tx_buf += sizeof(type); \
123 fsl_lpspi->remain -= sizeof(type); \
124 writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
134 static void fsl_lpspi_intctrl(struct fsl_lpspi_data
*fsl_lpspi
,
137 writel(enable
, fsl_lpspi
->base
+ IMX7ULP_IER
);
140 static int lpspi_prepare_xfer_hardware(struct spi_master
*master
)
142 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(master
);
144 return clk_prepare_enable(fsl_lpspi
->clk
);
147 static int lpspi_unprepare_xfer_hardware(struct spi_master
*master
)
149 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(master
);
151 clk_disable_unprepare(fsl_lpspi
->clk
);
156 static int fsl_lpspi_txfifo_empty(struct fsl_lpspi_data
*fsl_lpspi
)
159 unsigned long orig_jiffies
= jiffies
;
162 txcnt
= readl(fsl_lpspi
->base
+ IMX7ULP_FSR
) & 0xff;
164 if (time_after(jiffies
, orig_jiffies
+ msecs_to_jiffies(500))) {
165 dev_dbg(fsl_lpspi
->dev
, "txfifo empty timeout\n");
175 static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data
*fsl_lpspi
)
179 txfifo_cnt
= readl(fsl_lpspi
->base
+ IMX7ULP_FSR
) & 0xff;
181 while (txfifo_cnt
< fsl_lpspi
->txfifosize
) {
182 if (!fsl_lpspi
->remain
)
184 fsl_lpspi
->tx(fsl_lpspi
);
188 if (!fsl_lpspi
->remain
&& (txfifo_cnt
< fsl_lpspi
->txfifosize
))
189 writel(0, fsl_lpspi
->base
+ IMX7ULP_TDR
);
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
.prescale
<< 27;
207 temp
|= (fsl_lpspi
->config
.mode
& 0x3) << 30;
208 temp
|= (fsl_lpspi
->config
.chip_select
& 0x3) << 24;
211 * Set TCR_CONT will keep SS asserted after current transfer.
212 * For the first transfer, clear TCR_CONTC to assert SS.
213 * For subsequent transfer, set TCR_CONTC to keep SS asserted.
221 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_TCR
);
223 dev_dbg(fsl_lpspi
->dev
, "TCR=0x%x\n", temp
);
226 static void fsl_lpspi_set_watermark(struct fsl_lpspi_data
*fsl_lpspi
)
230 temp
= fsl_lpspi
->txfifosize
>> 1 | (fsl_lpspi
->rxfifosize
>> 1) << 16;
232 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_FCR
);
234 dev_dbg(fsl_lpspi
->dev
, "FCR=0x%x\n", temp
);
237 static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data
*fsl_lpspi
)
239 struct lpspi_config config
= fsl_lpspi
->config
;
240 unsigned int perclk_rate
, scldiv
;
243 perclk_rate
= clk_get_rate(fsl_lpspi
->clk
);
244 for (prescale
= 0; prescale
< 8; prescale
++) {
245 scldiv
= perclk_rate
/
246 (clkdivs
[prescale
] * config
.speed_hz
) - 2;
248 fsl_lpspi
->config
.prescale
= prescale
;
253 if (prescale
== 8 && scldiv
>= 256)
256 writel(scldiv
, fsl_lpspi
->base
+ IMX7ULP_CCR
);
258 dev_dbg(fsl_lpspi
->dev
, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",
259 perclk_rate
, config
.speed_hz
, prescale
, scldiv
);
264 static int fsl_lpspi_config(struct fsl_lpspi_data
*fsl_lpspi
)
270 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CR
);
271 writel(0, fsl_lpspi
->base
+ IMX7ULP_CR
);
273 ret
= fsl_lpspi_set_bitrate(fsl_lpspi
);
277 fsl_lpspi_set_watermark(fsl_lpspi
);
279 temp
= CFGR1_PCSCFG
| CFGR1_MASTER
;
280 if (fsl_lpspi
->config
.mode
& SPI_CS_HIGH
)
281 temp
|= CFGR1_PCSPOL
;
282 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CFGR1
);
284 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_CR
);
285 temp
|= CR_RRF
| CR_RTF
| CR_MEN
;
286 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_CR
);
291 static void fsl_lpspi_setup_transfer(struct spi_device
*spi
,
292 struct spi_transfer
*t
)
294 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(spi
->master
);
296 fsl_lpspi
->config
.mode
= spi
->mode
;
297 fsl_lpspi
->config
.bpw
= t
? t
->bits_per_word
: spi
->bits_per_word
;
298 fsl_lpspi
->config
.speed_hz
= t
? t
->speed_hz
: spi
->max_speed_hz
;
299 fsl_lpspi
->config
.chip_select
= spi
->chip_select
;
301 if (!fsl_lpspi
->config
.speed_hz
)
302 fsl_lpspi
->config
.speed_hz
= spi
->max_speed_hz
;
303 if (!fsl_lpspi
->config
.bpw
)
304 fsl_lpspi
->config
.bpw
= spi
->bits_per_word
;
306 /* Initialize the functions for transfer */
307 if (fsl_lpspi
->config
.bpw
<= 8) {
308 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u8
;
309 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u8
;
310 } else if (fsl_lpspi
->config
.bpw
<= 16) {
311 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u16
;
312 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u16
;
314 fsl_lpspi
->rx
= fsl_lpspi_buf_rx_u32
;
315 fsl_lpspi
->tx
= fsl_lpspi_buf_tx_u32
;
318 fsl_lpspi_config(fsl_lpspi
);
321 static int fsl_lpspi_transfer_one(struct spi_master
*master
,
322 struct spi_device
*spi
,
323 struct spi_transfer
*t
)
325 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(master
);
328 fsl_lpspi
->tx_buf
= t
->tx_buf
;
329 fsl_lpspi
->rx_buf
= t
->rx_buf
;
330 fsl_lpspi
->remain
= t
->len
;
332 reinit_completion(&fsl_lpspi
->xfer_done
);
333 fsl_lpspi_write_tx_fifo(fsl_lpspi
);
335 ret
= wait_for_completion_timeout(&fsl_lpspi
->xfer_done
, HZ
);
337 dev_dbg(fsl_lpspi
->dev
, "wait for completion timeout\n");
341 ret
= fsl_lpspi_txfifo_empty(fsl_lpspi
);
345 fsl_lpspi_read_rx_fifo(fsl_lpspi
);
350 static int fsl_lpspi_transfer_one_msg(struct spi_master
*master
,
351 struct spi_message
*msg
)
353 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(master
);
354 struct spi_device
*spi
= msg
->spi
;
355 struct spi_transfer
*xfer
;
356 bool is_first_xfer
= true;
361 msg
->actual_length
= 0;
363 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
364 fsl_lpspi_setup_transfer(spi
, xfer
);
365 fsl_lpspi_set_cmd(fsl_lpspi
, is_first_xfer
);
367 is_first_xfer
= false;
369 ret
= fsl_lpspi_transfer_one(master
, spi
, xfer
);
373 msg
->actual_length
+= xfer
->len
;
377 /* de-assert SS, then finalize current message */
378 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_TCR
);
380 writel(temp
, fsl_lpspi
->base
+ IMX7ULP_TCR
);
383 spi_finalize_current_message(master
);
388 static irqreturn_t
fsl_lpspi_isr(int irq
, void *dev_id
)
390 struct fsl_lpspi_data
*fsl_lpspi
= dev_id
;
393 fsl_lpspi_intctrl(fsl_lpspi
, 0);
394 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_SR
);
396 fsl_lpspi_read_rx_fifo(fsl_lpspi
);
399 fsl_lpspi_write_tx_fifo(fsl_lpspi
);
401 if (!fsl_lpspi
->remain
)
402 complete(&fsl_lpspi
->xfer_done
);
410 static int fsl_lpspi_probe(struct platform_device
*pdev
)
412 struct fsl_lpspi_data
*fsl_lpspi
;
413 struct spi_master
*master
;
414 struct resource
*res
;
418 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct fsl_lpspi_data
));
422 platform_set_drvdata(pdev
, master
);
424 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(8, 32);
425 master
->bus_num
= pdev
->id
;
427 fsl_lpspi
= spi_master_get_devdata(master
);
428 fsl_lpspi
->dev
= &pdev
->dev
;
430 master
->transfer_one_message
= fsl_lpspi_transfer_one_msg
;
431 master
->prepare_transfer_hardware
= lpspi_prepare_xfer_hardware
;
432 master
->unprepare_transfer_hardware
= lpspi_unprepare_xfer_hardware
;
433 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
434 master
->flags
= SPI_MASTER_MUST_RX
| SPI_MASTER_MUST_TX
;
435 master
->dev
.of_node
= pdev
->dev
.of_node
;
436 master
->bus_num
= pdev
->id
;
438 init_completion(&fsl_lpspi
->xfer_done
);
440 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
441 fsl_lpspi
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
442 if (IS_ERR(fsl_lpspi
->base
)) {
443 ret
= PTR_ERR(fsl_lpspi
->base
);
447 irq
= platform_get_irq(pdev
, 0);
453 ret
= devm_request_irq(&pdev
->dev
, irq
, fsl_lpspi_isr
, 0,
454 dev_name(&pdev
->dev
), fsl_lpspi
);
456 dev_err(&pdev
->dev
, "can't get irq%d: %d\n", irq
, ret
);
460 fsl_lpspi
->clk
= devm_clk_get(&pdev
->dev
, "ipg");
461 if (IS_ERR(fsl_lpspi
->clk
)) {
462 ret
= PTR_ERR(fsl_lpspi
->clk
);
466 ret
= clk_prepare_enable(fsl_lpspi
->clk
);
468 dev_err(&pdev
->dev
, "can't enable lpspi clock, ret=%d\n", ret
);
472 temp
= readl(fsl_lpspi
->base
+ IMX7ULP_PARAM
);
473 fsl_lpspi
->txfifosize
= 1 << (temp
& 0x0f);
474 fsl_lpspi
->rxfifosize
= 1 << ((temp
>> 8) & 0x0f);
476 clk_disable_unprepare(fsl_lpspi
->clk
);
478 ret
= devm_spi_register_master(&pdev
->dev
, master
);
480 dev_err(&pdev
->dev
, "spi_register_master error.\n");
487 spi_master_put(master
);
492 static int fsl_lpspi_remove(struct platform_device
*pdev
)
494 struct spi_master
*master
= platform_get_drvdata(pdev
);
495 struct fsl_lpspi_data
*fsl_lpspi
= spi_master_get_devdata(master
);
497 clk_disable_unprepare(fsl_lpspi
->clk
);
502 static struct platform_driver fsl_lpspi_driver
= {
505 .of_match_table
= fsl_lpspi_dt_ids
,
507 .probe
= fsl_lpspi_probe
,
508 .remove
= fsl_lpspi_remove
,
510 module_platform_driver(fsl_lpspi_driver
);
512 MODULE_DESCRIPTION("LPSPI Master Controller driver");
513 MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
514 MODULE_LICENSE("GPL");