1 // SPDX-License-Identifier: GPL-2.0-only
3 * J-Core SPI controller driver
5 * Copyright (C) 2012-2016 Smart Energy Instruments, Inc.
7 * Current version by Rich Felker
8 * Based loosely on initial version by Oleksandr G Zhadan
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
21 #include <linux/delay.h>
23 #define DRV_NAME "jcore_spi"
28 #define JCORE_SPI_CTRL_XMIT 0x02
29 #define JCORE_SPI_STAT_BUSY 0x02
30 #define JCORE_SPI_CTRL_LOOP 0x08
31 #define JCORE_SPI_CTRL_CS_BITS 0x15
33 #define JCORE_SPI_WAIT_RDY_MAX_LOOP 2000000
36 struct spi_controller
*host
;
39 unsigned int speed_reg
;
40 unsigned int speed_hz
;
41 unsigned int clock_freq
;
44 static int jcore_spi_wait(void __iomem
*ctrl_reg
)
46 unsigned timeout
= JCORE_SPI_WAIT_RDY_MAX_LOOP
;
49 if (!(readl(ctrl_reg
) & JCORE_SPI_STAT_BUSY
))
57 static void jcore_spi_program(struct jcore_spi
*hw
)
59 void __iomem
*ctrl_reg
= hw
->base
+ CTRL_REG
;
61 if (jcore_spi_wait(ctrl_reg
))
62 dev_err(hw
->host
->dev
.parent
,
63 "timeout waiting to program ctrl reg.\n");
65 writel(hw
->cs_reg
| hw
->speed_reg
, ctrl_reg
);
68 static void jcore_spi_chipsel(struct spi_device
*spi
, bool value
)
70 struct jcore_spi
*hw
= spi_controller_get_devdata(spi
->controller
);
71 u32 csbit
= 1U << (2 * spi_get_chipselect(spi
, 0));
73 dev_dbg(hw
->host
->dev
.parent
, "chipselect %d\n", spi_get_chipselect(spi
, 0));
80 jcore_spi_program(hw
);
83 static void jcore_spi_baudrate(struct jcore_spi
*hw
, int speed
)
85 if (speed
== hw
->speed_hz
)
88 if (speed
>= hw
->clock_freq
/ 2)
91 hw
->speed_reg
= ((hw
->clock_freq
/ 2 / speed
) - 1) << 27;
92 jcore_spi_program(hw
);
93 dev_dbg(hw
->host
->dev
.parent
, "speed=%d reg=0x%x\n",
94 speed
, hw
->speed_reg
);
97 static int jcore_spi_txrx(struct spi_controller
*host
, struct spi_device
*spi
,
98 struct spi_transfer
*t
)
100 struct jcore_spi
*hw
= spi_controller_get_devdata(host
);
102 void __iomem
*ctrl_reg
= hw
->base
+ CTRL_REG
;
103 void __iomem
*data_reg
= hw
->base
+ DATA_REG
;
107 const unsigned char *tx
;
112 jcore_spi_baudrate(hw
, t
->speed_hz
);
114 xmit
= hw
->cs_reg
| hw
->speed_reg
| JCORE_SPI_CTRL_XMIT
;
119 for (count
= 0; count
< len
; count
++) {
120 if (jcore_spi_wait(ctrl_reg
))
123 writel(tx
? *tx
++ : 0, data_reg
);
124 writel(xmit
, ctrl_reg
);
126 if (jcore_spi_wait(ctrl_reg
))
130 *rx
++ = readl(data_reg
);
133 spi_finalize_current_transfer(host
);
141 static int jcore_spi_probe(struct platform_device
*pdev
)
143 struct device_node
*node
= pdev
->dev
.of_node
;
144 struct jcore_spi
*hw
;
145 struct spi_controller
*host
;
146 struct resource
*res
;
151 host
= spi_alloc_host(&pdev
->dev
, sizeof(struct jcore_spi
));
155 /* Setup the host state. */
156 host
->num_chipselect
= 3;
157 host
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
158 host
->transfer_one
= jcore_spi_txrx
;
159 host
->set_cs
= jcore_spi_chipsel
;
160 host
->dev
.of_node
= node
;
161 host
->bus_num
= pdev
->id
;
163 hw
= spi_controller_get_devdata(host
);
165 platform_set_drvdata(pdev
, hw
);
167 /* Find and map our resources */
168 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
171 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
172 resource_size(res
), pdev
->name
))
174 hw
->base
= devm_ioremap(&pdev
->dev
, res
->start
,
180 * The SPI clock rate controlled via a configurable clock divider
181 * which is applied to the reference clock. A 50 MHz reference is
182 * most suitable for obtaining standard SPI clock rates, but some
183 * designs may have a different reference clock, and the DT must
184 * make the driver aware so that it can properly program the
185 * requested rate. If the clock is omitted, 50 MHz is assumed.
187 clock_freq
= 50000000;
188 clk
= devm_clk_get(&pdev
->dev
, "ref_clk");
190 if (clk_prepare_enable(clk
) == 0) {
191 clock_freq
= clk_get_rate(clk
);
192 clk_disable_unprepare(clk
);
194 dev_warn(&pdev
->dev
, "could not enable ref_clk\n");
196 hw
->clock_freq
= clock_freq
;
198 /* Initialize all CS bits to high. */
199 hw
->cs_reg
= JCORE_SPI_CTRL_CS_BITS
;
200 jcore_spi_baudrate(hw
, 400000);
202 /* Register our spi controller */
203 err
= devm_spi_register_controller(&pdev
->dev
, host
);
212 spi_controller_put(host
);
216 static const struct of_device_id jcore_spi_of_match
[] = {
217 { .compatible
= "jcore,spi2" },
220 MODULE_DEVICE_TABLE(of
, jcore_spi_of_match
);
222 static struct platform_driver jcore_spi_driver
= {
223 .probe
= jcore_spi_probe
,
226 .of_match_table
= jcore_spi_of_match
,
230 module_platform_driver(jcore_spi_driver
);
232 MODULE_DESCRIPTION("J-Core SPI driver");
233 MODULE_AUTHOR("Rich Felker <dalias@libc.org>");
234 MODULE_LICENSE("GPL");
235 MODULE_ALIAS("platform:" DRV_NAME
);