2 * drivers/spi/spi-fsl-dspi.c
4 * Copyright 2013 Freescale Semiconductor, Inc.
6 * Freescale DSPI driver
7 * This file contains a driver for the Freescale DSPI
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/errno.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/spi_bitbang.h>
28 #include <linux/pm_runtime.h>
30 #include <linux/of_device.h>
32 #define DRIVER_NAME "fsl-dspi"
34 #define TRAN_STATE_RX_VOID 0x01
35 #define TRAN_STATE_TX_VOID 0x02
36 #define TRAN_STATE_WORD_ODD_NUM 0x04
38 #define DSPI_FIFO_SIZE 4
41 #define SPI_MCR_MASTER (1 << 31)
42 #define SPI_MCR_PCSIS (0x3F << 16)
43 #define SPI_MCR_CLR_TXF (1 << 11)
44 #define SPI_MCR_CLR_RXF (1 << 10)
48 #define SPI_CTAR(x) (0x0c + (x * 4))
49 #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
50 #define SPI_CTAR_CPOL(x) ((x) << 26)
51 #define SPI_CTAR_CPHA(x) ((x) << 25)
52 #define SPI_CTAR_LSBFE(x) ((x) << 24)
53 #define SPI_CTAR_PCSSCR(x) (((x) & 0x00000003) << 22)
54 #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
55 #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
56 #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
57 #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
58 #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
59 #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
60 #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
62 #define SPI_CTAR0_SLAVE 0x0c
65 #define SPI_SR_EOQF 0x10000000
68 #define SPI_RSER_EOQFE 0x10000000
70 #define SPI_PUSHR 0x34
71 #define SPI_PUSHR_CONT (1 << 31)
72 #define SPI_PUSHR_CTAS(x) (((x) & 0x00000007) << 28)
73 #define SPI_PUSHR_EOQ (1 << 27)
74 #define SPI_PUSHR_CTCNT (1 << 26)
75 #define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
76 #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
78 #define SPI_PUSHR_SLAVE 0x34
81 #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
83 #define SPI_TXFR0 0x3c
84 #define SPI_TXFR1 0x40
85 #define SPI_TXFR2 0x44
86 #define SPI_TXFR3 0x48
87 #define SPI_RXFR0 0x7c
88 #define SPI_RXFR1 0x80
89 #define SPI_RXFR2 0x84
90 #define SPI_RXFR3 0x88
92 #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
93 #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
94 #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
95 #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
97 #define SPI_CS_INIT 0x01
98 #define SPI_CS_ASSERT 0x02
99 #define SPI_CS_DROP 0x04
108 struct spi_bitbang bitbang
;
109 struct platform_device
*pdev
;
115 struct spi_transfer
*cur_transfer
;
116 struct chip_data
*cur_chip
;
126 wait_queue_head_t waitq
;
130 static inline int is_double_byte_mode(struct fsl_dspi
*dspi
)
132 return ((readl(dspi
->base
+ SPI_CTAR(dspi
->cs
)) & SPI_FRAME_BITS_MASK
)
133 == SPI_FRAME_BITS(8)) ? 0 : 1;
136 static void set_bit_mode(struct fsl_dspi
*dspi
, unsigned char bits
)
140 temp
= readl(dspi
->base
+ SPI_CTAR(dspi
->cs
));
141 temp
&= ~SPI_FRAME_BITS_MASK
;
142 temp
|= SPI_FRAME_BITS(bits
);
143 writel(temp
, dspi
->base
+ SPI_CTAR(dspi
->cs
));
146 static void hz_to_spi_baud(char *pbr
, char *br
, int speed_hz
,
147 unsigned long clkrate
)
149 /* Valid baud rate pre-scaler values */
150 int pbr_tbl
[4] = {2, 3, 5, 7};
151 int brs
[16] = { 2, 4, 6, 8,
153 256, 512, 1024, 2048,
154 4096, 8192, 16384, 32768 };
155 int temp
, i
= 0, j
= 0;
157 temp
= clkrate
/ 2 / speed_hz
;
159 for (i
= 0; i
< ARRAY_SIZE(pbr_tbl
); i
++)
160 for (j
= 0; j
< ARRAY_SIZE(brs
); j
++) {
161 if (pbr_tbl
[i
] * brs
[j
] >= temp
) {
168 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld\
169 ,we use the max prescaler value.\n", speed_hz
, clkrate
);
170 *pbr
= ARRAY_SIZE(pbr_tbl
) - 1;
171 *br
= ARRAY_SIZE(brs
) - 1;
174 static int dspi_transfer_write(struct fsl_dspi
*dspi
)
183 tx_word
= is_double_byte_mode(dspi
);
185 /* If we are in word mode, but only have a single byte to transfer
186 * then switch to byte mode temporarily. Will switch back at the
187 * end of the transfer.
189 if (tx_word
&& (dspi
->len
== 1)) {
190 dspi
->dataflags
|= TRAN_STATE_WORD_ODD_NUM
;
191 set_bit_mode(dspi
, 8);
195 while (dspi
->len
&& (tx_count
< DSPI_FIFO_SIZE
)) {
200 if (!(dspi
->dataflags
& TRAN_STATE_TX_VOID
)) {
201 d16
= *(u16
*)dspi
->tx
;
204 d16
= dspi
->void_write_data
;
207 dspi_pushr
= SPI_PUSHR_TXDATA(d16
) |
208 SPI_PUSHR_PCS(dspi
->cs
) |
209 SPI_PUSHR_CTAS(dspi
->cs
) |
214 if (!(dspi
->dataflags
& TRAN_STATE_TX_VOID
)) {
216 d8
= *(u8
*)dspi
->tx
;
219 d8
= (u8
)dspi
->void_write_data
;
222 dspi_pushr
= SPI_PUSHR_TXDATA(d8
) |
223 SPI_PUSHR_PCS(dspi
->cs
) |
224 SPI_PUSHR_CTAS(dspi
->cs
) |
230 if (dspi
->len
== 0 || tx_count
== DSPI_FIFO_SIZE
- 1) {
231 /* last transfer in the transfer */
232 dspi_pushr
|= SPI_PUSHR_EOQ
;
233 } else if (tx_word
&& (dspi
->len
== 1))
234 dspi_pushr
|= SPI_PUSHR_EOQ
;
238 dspi_pushr
|= SPI_PUSHR_CTCNT
; /* clear counter */
241 writel(dspi_pushr
, dspi
->base
+ SPI_PUSHR
);
245 return tx_count
* (tx_word
+ 1);
248 static int dspi_transfer_read(struct fsl_dspi
*dspi
)
251 int rx_word
= is_double_byte_mode(dspi
);
253 while ((dspi
->rx
< dspi
->rx_end
)
254 && (rx_count
< DSPI_FIFO_SIZE
)) {
256 if ((dspi
->rx_end
- dspi
->rx
) == 1)
259 d
= SPI_POPR_RXDATA(readl(dspi
->base
+ SPI_POPR
));
261 if (!(dspi
->dataflags
& TRAN_STATE_RX_VOID
))
262 *(u16
*)dspi
->rx
= d
;
266 d
= SPI_POPR_RXDATA(readl(dspi
->base
+ SPI_POPR
));
267 if (!(dspi
->dataflags
& TRAN_STATE_RX_VOID
))
277 static int dspi_txrx_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
279 struct fsl_dspi
*dspi
= spi_master_get_devdata(spi
->master
);
280 dspi
->cur_transfer
= t
;
281 dspi
->cur_chip
= spi_get_ctldata(spi
);
282 dspi
->cs
= spi
->chip_select
;
283 dspi
->void_write_data
= dspi
->cur_chip
->void_write_data
;
286 dspi
->tx
= (void *)t
->tx_buf
;
287 dspi
->tx_end
= dspi
->tx
+ t
->len
;
288 dspi
->rx
= t
->rx_buf
;
289 dspi
->rx_end
= dspi
->rx
+ t
->len
;
293 dspi
->dataflags
|= TRAN_STATE_RX_VOID
;
296 dspi
->dataflags
|= TRAN_STATE_TX_VOID
;
298 writel(dspi
->cur_chip
->mcr_val
, dspi
->base
+ SPI_MCR
);
299 writel(dspi
->cur_chip
->ctar_val
, dspi
->base
+ SPI_CTAR(dspi
->cs
));
300 writel(SPI_RSER_EOQFE
, dspi
->base
+ SPI_RSER
);
303 writel(dspi
->cur_chip
->ctar_val
,
304 dspi
->base
+ SPI_CTAR(dspi
->cs
));
306 dspi_transfer_write(dspi
);
308 if (wait_event_interruptible(dspi
->waitq
, dspi
->waitflags
))
309 dev_err(&dspi
->pdev
->dev
, "wait transfer complete fail!\n");
312 return t
->len
- dspi
->len
;
315 static void dspi_chipselect(struct spi_device
*spi
, int value
)
317 struct fsl_dspi
*dspi
= spi_master_get_devdata(spi
->master
);
318 u32 pushr
= readl(dspi
->base
+ SPI_PUSHR
);
321 case BITBANG_CS_ACTIVE
:
322 pushr
|= SPI_PUSHR_CONT
;
324 case BITBANG_CS_INACTIVE
:
325 pushr
&= ~SPI_PUSHR_CONT
;
329 writel(pushr
, dspi
->base
+ SPI_PUSHR
);
332 static int dspi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
334 struct chip_data
*chip
;
335 struct fsl_dspi
*dspi
= spi_master_get_devdata(spi
->master
);
336 unsigned char br
= 0, pbr
= 0, fmsz
= 0;
338 /* Only alloc on first setup */
339 chip
= spi_get_ctldata(spi
);
341 chip
= kcalloc(1, sizeof(struct chip_data
), GFP_KERNEL
);
346 chip
->mcr_val
= SPI_MCR_MASTER
| SPI_MCR_PCSIS
|
347 SPI_MCR_CLR_TXF
| SPI_MCR_CLR_RXF
;
348 if ((spi
->bits_per_word
>= 4) && (spi
->bits_per_word
<= 16)) {
349 fmsz
= spi
->bits_per_word
- 1;
351 pr_err("Invalid wordsize\n");
356 chip
->void_write_data
= 0;
358 hz_to_spi_baud(&pbr
, &br
,
359 spi
->max_speed_hz
, clk_get_rate(dspi
->clk
));
361 chip
->ctar_val
= SPI_CTAR_FMSZ(fmsz
)
362 | SPI_CTAR_CPOL(spi
->mode
& SPI_CPOL
? 1 : 0)
363 | SPI_CTAR_CPHA(spi
->mode
& SPI_CPHA
? 1 : 0)
364 | SPI_CTAR_LSBFE(spi
->mode
& SPI_LSB_FIRST
? 1 : 0)
368 spi_set_ctldata(spi
, chip
);
373 static int dspi_setup(struct spi_device
*spi
)
375 if (!spi
->max_speed_hz
)
378 return dspi_setup_transfer(spi
, NULL
);
381 static irqreturn_t
dspi_interrupt(int irq
, void *dev_id
)
383 struct fsl_dspi
*dspi
= (struct fsl_dspi
*)dev_id
;
385 writel(SPI_SR_EOQF
, dspi
->base
+ SPI_SR
);
387 dspi_transfer_read(dspi
);
390 if (dspi
->dataflags
& TRAN_STATE_WORD_ODD_NUM
)
391 set_bit_mode(dspi
, 16);
393 wake_up_interruptible(&dspi
->waitq
);
395 dspi_transfer_write(dspi
);
403 static struct of_device_id fsl_dspi_dt_ids
[] = {
404 { .compatible
= "fsl,vf610-dspi", .data
= NULL
, },
407 MODULE_DEVICE_TABLE(of
, fsl_dspi_dt_ids
);
409 #ifdef CONFIG_PM_SLEEP
410 static int dspi_suspend(struct device
*dev
)
412 struct spi_master
*master
= dev_get_drvdata(dev
);
413 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
415 spi_master_suspend(master
);
416 clk_disable_unprepare(dspi
->clk
);
421 static int dspi_resume(struct device
*dev
)
424 struct spi_master
*master
= dev_get_drvdata(dev
);
425 struct fsl_dspi
*dspi
= spi_master_get_devdata(master
);
427 clk_prepare_enable(dspi
->clk
);
428 spi_master_resume(master
);
432 #endif /* CONFIG_PM_SLEEP */
434 static const struct dev_pm_ops dspi_pm
= {
435 SET_SYSTEM_SLEEP_PM_OPS(dspi_suspend
, dspi_resume
)
438 static int dspi_probe(struct platform_device
*pdev
)
440 struct device_node
*np
= pdev
->dev
.of_node
;
441 struct spi_master
*master
;
442 struct fsl_dspi
*dspi
;
443 struct resource
*res
;
444 int ret
= 0, cs_num
, bus_num
;
446 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct fsl_dspi
));
450 dspi
= spi_master_get_devdata(master
);
452 dspi
->bitbang
.master
= master
;
453 dspi
->bitbang
.chipselect
= dspi_chipselect
;
454 dspi
->bitbang
.setup_transfer
= dspi_setup_transfer
;
455 dspi
->bitbang
.txrx_bufs
= dspi_txrx_transfer
;
456 dspi
->bitbang
.master
->setup
= dspi_setup
;
457 dspi
->bitbang
.master
->dev
.of_node
= pdev
->dev
.of_node
;
459 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
460 master
->bits_per_word_mask
= SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
463 ret
= of_property_read_u32(np
, "spi-num-chipselects", &cs_num
);
465 dev_err(&pdev
->dev
, "can't get spi-num-chipselects\n");
468 master
->num_chipselect
= cs_num
;
470 ret
= of_property_read_u32(np
, "bus-num", &bus_num
);
472 dev_err(&pdev
->dev
, "can't get bus-num\n");
475 master
->bus_num
= bus_num
;
477 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
478 dspi
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
479 if (IS_ERR(dspi
->base
)) {
480 ret
= PTR_ERR(dspi
->base
);
484 dspi
->irq
= platform_get_irq(pdev
, 0);
486 dev_err(&pdev
->dev
, "can't get platform irq\n");
491 ret
= devm_request_irq(&pdev
->dev
, dspi
->irq
, dspi_interrupt
, 0,
494 dev_err(&pdev
->dev
, "Unable to attach DSPI interrupt\n");
498 dspi
->clk
= devm_clk_get(&pdev
->dev
, "dspi");
499 if (IS_ERR(dspi
->clk
)) {
500 ret
= PTR_ERR(dspi
->clk
);
501 dev_err(&pdev
->dev
, "unable to get clock\n");
504 clk_prepare_enable(dspi
->clk
);
506 init_waitqueue_head(&dspi
->waitq
);
507 platform_set_drvdata(pdev
, dspi
);
509 ret
= spi_bitbang_start(&dspi
->bitbang
);
511 dev_err(&pdev
->dev
, "Problem registering DSPI master\n");
515 pr_info(KERN_INFO
"Freescale DSPI master initialized\n");
519 clk_disable_unprepare(dspi
->clk
);
521 spi_master_put(master
);
526 static int dspi_remove(struct platform_device
*pdev
)
528 struct fsl_dspi
*dspi
= platform_get_drvdata(pdev
);
530 /* Disconnect from the SPI framework */
531 spi_bitbang_stop(&dspi
->bitbang
);
532 clk_disable_unprepare(dspi
->clk
);
533 spi_master_put(dspi
->bitbang
.master
);
538 static struct platform_driver fsl_dspi_driver
= {
539 .driver
.name
= DRIVER_NAME
,
540 .driver
.of_match_table
= fsl_dspi_dt_ids
,
541 .driver
.owner
= THIS_MODULE
,
542 .driver
.pm
= &dspi_pm
,
544 .remove
= dspi_remove
,
546 module_platform_driver(fsl_dspi_driver
);
548 MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
549 MODULE_LICENSE("GPL");
550 MODULE_ALIAS("platform:" DRIVER_NAME
);