1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003-2015 Broadcom Corporation
6 #include <linux/acpi.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/spi/spi.h>
13 #include <linux/interrupt.h>
15 /* SPI Configuration Register */
16 #define XLP_SPI_CONFIG 0x00
17 #define XLP_SPI_CPHA BIT(0)
18 #define XLP_SPI_CPOL BIT(1)
19 #define XLP_SPI_CS_POL BIT(2)
20 #define XLP_SPI_TXMISO_EN BIT(3)
21 #define XLP_SPI_TXMOSI_EN BIT(4)
22 #define XLP_SPI_RXMISO_EN BIT(5)
23 #define XLP_SPI_CS_LSBFE BIT(10)
24 #define XLP_SPI_RXCAP_EN BIT(11)
26 /* SPI Frequency Divider Register */
27 #define XLP_SPI_FDIV 0x04
29 /* SPI Command Register */
30 #define XLP_SPI_CMD 0x08
31 #define XLP_SPI_CMD_IDLE_MASK 0x0
32 #define XLP_SPI_CMD_TX_MASK 0x1
33 #define XLP_SPI_CMD_RX_MASK 0x2
34 #define XLP_SPI_CMD_TXRX_MASK 0x3
35 #define XLP_SPI_CMD_CONT BIT(4)
36 #define XLP_SPI_XFR_BITCNT_SHIFT 16
38 /* SPI Status Register */
39 #define XLP_SPI_STATUS 0x0c
40 #define XLP_SPI_XFR_PENDING BIT(0)
41 #define XLP_SPI_XFR_DONE BIT(1)
42 #define XLP_SPI_TX_INT BIT(2)
43 #define XLP_SPI_RX_INT BIT(3)
44 #define XLP_SPI_TX_UF BIT(4)
45 #define XLP_SPI_RX_OF BIT(5)
46 #define XLP_SPI_STAT_MASK 0x3f
48 /* SPI Interrupt Enable Register */
49 #define XLP_SPI_INTR_EN 0x10
50 #define XLP_SPI_INTR_DONE BIT(0)
51 #define XLP_SPI_INTR_TXTH BIT(1)
52 #define XLP_SPI_INTR_RXTH BIT(2)
53 #define XLP_SPI_INTR_TXUF BIT(3)
54 #define XLP_SPI_INTR_RXOF BIT(4)
56 /* SPI FIFO Threshold Register */
57 #define XLP_SPI_FIFO_THRESH 0x14
59 /* SPI FIFO Word Count Register */
60 #define XLP_SPI_FIFO_WCNT 0x18
61 #define XLP_SPI_RXFIFO_WCNT_MASK 0xf
62 #define XLP_SPI_TXFIFO_WCNT_MASK 0xf0
63 #define XLP_SPI_TXFIFO_WCNT_SHIFT 4
65 /* SPI Transmit Data FIFO Register */
66 #define XLP_SPI_TXDATA_FIFO 0x1c
68 /* SPI Receive Data FIFO Register */
69 #define XLP_SPI_RXDATA_FIFO 0x20
71 /* SPI System Control Register */
72 #define XLP_SPI_SYSCTRL 0x100
73 #define XLP_SPI_SYS_RESET BIT(0)
74 #define XLP_SPI_SYS_CLKDIS BIT(1)
75 #define XLP_SPI_SYS_PMEN BIT(8)
77 #define SPI_CS_OFFSET 0x40
78 #define XLP_SPI_TXRXTH 0x80
79 #define XLP_SPI_FIFO_SIZE 8
80 #define XLP_SPI_MAX_CS 4
81 #define XLP_SPI_DEFAULT_FREQ 133333333
82 #define XLP_SPI_FDIV_MIN 4
83 #define XLP_SPI_FDIV_MAX 65535
85 * SPI can transfer only 28 bytes properly at a time. So split the
86 * transfer into 28 bytes size.
88 #define XLP_SPI_XFER_SIZE 28
91 struct device dev
; /* device structure */
92 void __iomem
*base
; /* spi registers base address */
93 const u8
*tx_buf
; /* tx data buffer */
94 u8
*rx_buf
; /* rx data buffer */
95 int tx_len
; /* tx xfer length */
96 int rx_len
; /* rx xfer length */
97 int txerrors
; /* TXFIFO underflow count */
98 int rxerrors
; /* RXFIFO overflow count */
99 int cs
; /* slave device chip select */
100 u32 spi_clk
; /* spi clock frequency */
101 bool cmd_cont
; /* cs active */
102 struct completion done
; /* completion notification */
105 static inline u32
xlp_spi_reg_read(struct xlp_spi_priv
*priv
,
108 return readl(priv
->base
+ regoff
+ cs
* SPI_CS_OFFSET
);
111 static inline void xlp_spi_reg_write(struct xlp_spi_priv
*priv
, int cs
,
114 writel(val
, priv
->base
+ regoff
+ cs
* SPI_CS_OFFSET
);
117 static inline void xlp_spi_sysctl_write(struct xlp_spi_priv
*priv
,
120 writel(val
, priv
->base
+ regoff
);
124 * Setup global SPI_SYSCTRL register for all SPI channels.
126 static void xlp_spi_sysctl_setup(struct xlp_spi_priv
*xspi
)
130 for (cs
= 0; cs
< XLP_SPI_MAX_CS
; cs
++)
131 xlp_spi_sysctl_write(xspi
, XLP_SPI_SYSCTRL
,
132 XLP_SPI_SYS_RESET
<< cs
);
133 xlp_spi_sysctl_write(xspi
, XLP_SPI_SYSCTRL
, XLP_SPI_SYS_PMEN
);
136 static int xlp_spi_setup(struct spi_device
*spi
)
138 struct xlp_spi_priv
*xspi
;
142 xspi
= spi_master_get_devdata(spi
->master
);
143 cs
= spi
->chip_select
;
145 * The value of fdiv must be between 4 and 65535.
147 fdiv
= DIV_ROUND_UP(xspi
->spi_clk
, spi
->max_speed_hz
);
148 if (fdiv
> XLP_SPI_FDIV_MAX
)
149 fdiv
= XLP_SPI_FDIV_MAX
;
150 else if (fdiv
< XLP_SPI_FDIV_MIN
)
151 fdiv
= XLP_SPI_FDIV_MIN
;
153 xlp_spi_reg_write(xspi
, cs
, XLP_SPI_FDIV
, fdiv
);
154 xlp_spi_reg_write(xspi
, cs
, XLP_SPI_FIFO_THRESH
, XLP_SPI_TXRXTH
);
155 cfg
= xlp_spi_reg_read(xspi
, cs
, XLP_SPI_CONFIG
);
156 if (spi
->mode
& SPI_CPHA
)
159 cfg
&= ~XLP_SPI_CPHA
;
160 if (spi
->mode
& SPI_CPOL
)
163 cfg
&= ~XLP_SPI_CPOL
;
164 if (!(spi
->mode
& SPI_CS_HIGH
))
165 cfg
|= XLP_SPI_CS_POL
;
167 cfg
&= ~XLP_SPI_CS_POL
;
168 if (spi
->mode
& SPI_LSB_FIRST
)
169 cfg
|= XLP_SPI_CS_LSBFE
;
171 cfg
&= ~XLP_SPI_CS_LSBFE
;
173 cfg
|= XLP_SPI_TXMOSI_EN
| XLP_SPI_RXMISO_EN
;
175 cfg
|= XLP_SPI_RXCAP_EN
;
176 xlp_spi_reg_write(xspi
, cs
, XLP_SPI_CONFIG
, cfg
);
181 static void xlp_spi_read_rxfifo(struct xlp_spi_priv
*xspi
)
183 u32 rx_data
, rxfifo_cnt
;
186 rxfifo_cnt
= xlp_spi_reg_read(xspi
, xspi
->cs
, XLP_SPI_FIFO_WCNT
);
187 rxfifo_cnt
&= XLP_SPI_RXFIFO_WCNT_MASK
;
189 rx_data
= xlp_spi_reg_read(xspi
, xspi
->cs
, XLP_SPI_RXDATA_FIFO
);
191 nbytes
= min(xspi
->rx_len
, 4);
192 for (i
= nbytes
- 1; i
>= 0; i
--, j
++)
193 xspi
->rx_buf
[i
] = (rx_data
>> (j
* 8)) & 0xff;
195 xspi
->rx_len
-= nbytes
;
196 xspi
->rx_buf
+= nbytes
;
201 static void xlp_spi_fill_txfifo(struct xlp_spi_priv
*xspi
)
203 u32 tx_data
, txfifo_cnt
;
206 txfifo_cnt
= xlp_spi_reg_read(xspi
, xspi
->cs
, XLP_SPI_FIFO_WCNT
);
207 txfifo_cnt
&= XLP_SPI_TXFIFO_WCNT_MASK
;
208 txfifo_cnt
>>= XLP_SPI_TXFIFO_WCNT_SHIFT
;
209 while (xspi
->tx_len
&& (txfifo_cnt
< XLP_SPI_FIFO_SIZE
)) {
212 nbytes
= min(xspi
->tx_len
, 4);
213 for (i
= nbytes
- 1; i
>= 0; i
--, j
++)
214 tx_data
|= xspi
->tx_buf
[i
] << (j
* 8);
216 xlp_spi_reg_write(xspi
, xspi
->cs
, XLP_SPI_TXDATA_FIFO
, tx_data
);
217 xspi
->tx_len
-= nbytes
;
218 xspi
->tx_buf
+= nbytes
;
223 static irqreturn_t
xlp_spi_interrupt(int irq
, void *dev_id
)
225 struct xlp_spi_priv
*xspi
= dev_id
;
228 stat
= xlp_spi_reg_read(xspi
, xspi
->cs
, XLP_SPI_STATUS
) &
233 if (stat
& XLP_SPI_TX_INT
) {
235 xlp_spi_fill_txfifo(xspi
);
236 if (stat
& XLP_SPI_TX_UF
)
240 if (stat
& XLP_SPI_RX_INT
) {
242 xlp_spi_read_rxfifo(xspi
);
243 if (stat
& XLP_SPI_RX_OF
)
247 /* write status back to clear interrupts */
248 xlp_spi_reg_write(xspi
, xspi
->cs
, XLP_SPI_STATUS
, stat
);
249 if (stat
& XLP_SPI_XFR_DONE
)
250 complete(&xspi
->done
);
255 static void xlp_spi_send_cmd(struct xlp_spi_priv
*xspi
, int xfer_len
,
261 cmd
|= XLP_SPI_CMD_TX_MASK
;
263 cmd
|= XLP_SPI_CMD_RX_MASK
;
265 cmd
|= XLP_SPI_CMD_CONT
;
266 cmd
|= ((xfer_len
* 8 - 1) << XLP_SPI_XFR_BITCNT_SHIFT
);
267 xlp_spi_reg_write(xspi
, xspi
->cs
, XLP_SPI_CMD
, cmd
);
270 static int xlp_spi_xfer_block(struct xlp_spi_priv
*xs
,
271 const unsigned char *tx_buf
,
272 unsigned char *rx_buf
, int xfer_len
, int cmd_cont
)
279 xs
->tx_len
= (xs
->tx_buf
== NULL
) ? 0 : xfer_len
;
280 xs
->rx_len
= (xs
->rx_buf
== NULL
) ? 0 : xfer_len
;
281 xs
->txerrors
= xs
->rxerrors
= 0;
283 /* fill TXDATA_FIFO, then send the CMD */
285 xlp_spi_fill_txfifo(xs
);
287 xlp_spi_send_cmd(xs
, xfer_len
, cmd_cont
);
290 * We are getting some spurious tx interrupts, so avoid enabling
291 * tx interrupts when only rx is in process.
292 * Enable all the interrupts in tx case.
295 intr_mask
|= XLP_SPI_INTR_TXTH
| XLP_SPI_INTR_TXUF
|
296 XLP_SPI_INTR_RXTH
| XLP_SPI_INTR_RXOF
;
298 intr_mask
|= XLP_SPI_INTR_RXTH
| XLP_SPI_INTR_RXOF
;
300 intr_mask
|= XLP_SPI_INTR_DONE
;
301 xlp_spi_reg_write(xs
, xs
->cs
, XLP_SPI_INTR_EN
, intr_mask
);
303 timeout
= wait_for_completion_timeout(&xs
->done
,
304 msecs_to_jiffies(1000));
305 /* Disable interrupts */
306 xlp_spi_reg_write(xs
, xs
->cs
, XLP_SPI_INTR_EN
, 0x0);
308 dev_err(&xs
->dev
, "xfer timedout!\n");
311 if (xs
->txerrors
|| xs
->rxerrors
)
312 dev_err(&xs
->dev
, "Over/Underflow rx %d tx %d xfer %d!\n",
313 xs
->rxerrors
, xs
->txerrors
, xfer_len
);
320 static int xlp_spi_txrx_bufs(struct xlp_spi_priv
*xs
, struct spi_transfer
*t
)
323 unsigned char *rx_buf
;
324 const unsigned char *tx_buf
;
330 if (bytesleft
> XLP_SPI_XFER_SIZE
)
331 sz
= xlp_spi_xfer_block(xs
, tx_buf
, rx_buf
,
332 XLP_SPI_XFER_SIZE
, 1);
334 sz
= xlp_spi_xfer_block(xs
, tx_buf
, rx_buf
,
335 bytesleft
, xs
->cmd_cont
);
347 static int xlp_spi_transfer_one(struct spi_master
*master
,
348 struct spi_device
*spi
,
349 struct spi_transfer
*t
)
351 struct xlp_spi_priv
*xspi
= spi_master_get_devdata(master
);
354 xspi
->cs
= spi
->chip_select
;
355 xspi
->dev
= spi
->dev
;
357 if (spi_transfer_is_last(master
, t
))
362 if (xlp_spi_txrx_bufs(xspi
, t
))
365 spi_finalize_current_transfer(master
);
369 static int xlp_spi_probe(struct platform_device
*pdev
)
371 struct spi_master
*master
;
372 struct xlp_spi_priv
*xspi
;
376 xspi
= devm_kzalloc(&pdev
->dev
, sizeof(*xspi
), GFP_KERNEL
);
380 xspi
->base
= devm_platform_ioremap_resource(pdev
, 0);
381 if (IS_ERR(xspi
->base
))
382 return PTR_ERR(xspi
->base
);
384 irq
= platform_get_irq(pdev
, 0);
387 err
= devm_request_irq(&pdev
->dev
, irq
, xlp_spi_interrupt
, 0,
390 dev_err(&pdev
->dev
, "unable to request irq %d\n", irq
);
394 clk
= devm_clk_get(&pdev
->dev
, NULL
);
396 dev_err(&pdev
->dev
, "could not get spi clock\n");
400 xspi
->spi_clk
= clk_get_rate(clk
);
402 master
= spi_alloc_master(&pdev
->dev
, 0);
404 dev_err(&pdev
->dev
, "could not alloc master\n");
409 master
->num_chipselect
= XLP_SPI_MAX_CS
;
410 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
411 master
->setup
= xlp_spi_setup
;
412 master
->transfer_one
= xlp_spi_transfer_one
;
413 master
->dev
.of_node
= pdev
->dev
.of_node
;
415 init_completion(&xspi
->done
);
416 spi_master_set_devdata(master
, xspi
);
417 xlp_spi_sysctl_setup(xspi
);
419 /* register spi controller */
420 err
= devm_spi_register_master(&pdev
->dev
, master
);
422 dev_err(&pdev
->dev
, "spi register master failed!\n");
423 spi_master_put(master
);
431 static const struct acpi_device_id xlp_spi_acpi_match
[] = {
436 MODULE_DEVICE_TABLE(acpi
, xlp_spi_acpi_match
);
439 static const struct of_device_id xlp_spi_dt_id
[] = {
440 { .compatible
= "netlogic,xlp832-spi" },
443 MODULE_DEVICE_TABLE(of
, xlp_spi_dt_id
);
445 static struct platform_driver xlp_spi_driver
= {
446 .probe
= xlp_spi_probe
,
449 .of_match_table
= xlp_spi_dt_id
,
450 .acpi_match_table
= ACPI_PTR(xlp_spi_acpi_match
),
453 module_platform_driver(xlp_spi_driver
);
455 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
456 MODULE_DESCRIPTION("Netlogic XLP SPI controller driver");
457 MODULE_LICENSE("GPL v2");