090847baad7da5ea590869a5c518e2257747c3a1
[openwrt/staging/neocturne.git] /
1 From a8af5cc2ff1e804694629a8ef320935629dd15ba Mon Sep 17 00:00:00 2001
2 From: Bert Vermeulen <bert@biot.com>
3 Date: Wed, 20 Jan 2021 14:59:28 +0100
4 Subject: spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SPI
5 controllers
6
7 This driver likely also supports earlier (RTL8196) and later (RTL93xx)
8 SoCs.
9
10 The SPI hardware in these SoCs is specifically intended for connecting NOR
11 bootflash chips, and only used for that in dozens of examined devices.
12 However boiled down to basics, it's really just a half-duplex SPI
13 controller.
14
15 The hardware appears to have a vestigial second chip-select control, but
16 it hasn't been seen in the wild and is thus not supported.
17
18 Signed-off-by: Bert Vermeulen <bert@biot.com>
19 Link: https://lore.kernel.org/r/20210120135928.246054-3-bert@biot.com
20 Signed-off-by: Mark Brown <broonie@kernel.org>
21 ---
22 drivers/spi/Makefile | 1 +
23 drivers/spi/spi-realtek-rtl.c | 209 ++++++++++++++++++++++++++++++++++++++++++
24 2 files changed, 210 insertions(+)
25 create mode 100644 drivers/spi/spi-realtek-rtl.c
26
27 --- a/drivers/spi/Makefile
28 +++ b/drivers/spi/Makefile
29 @@ -94,6 +94,7 @@ obj-$(CONFIG_SPI_QCOM_QSPI) += spi-qcom
30 obj-$(CONFIG_SPI_QUP) += spi-qup.o
31 obj-$(CONFIG_SPI_ROCKCHIP) += spi-rockchip.o
32 obj-$(CONFIG_SPI_RB4XX) += spi-rb4xx.o
33 +obj-$(CONFIG_MACH_REALTEK_RTL) += spi-realtek-rtl.o
34 obj-$(CONFIG_SPI_RPCIF) += spi-rpc-if.o
35 obj-$(CONFIG_SPI_RSPI) += spi-rspi.o
36 obj-$(CONFIG_SPI_S3C24XX) += spi-s3c24xx-hw.o
37 --- /dev/null
38 +++ b/drivers/spi/spi-realtek-rtl.c
39 @@ -0,0 +1,209 @@
40 +// SPDX-License-Identifier: GPL-2.0-only
41 +
42 +#include <linux/module.h>
43 +#include <linux/platform_device.h>
44 +#include <linux/mod_devicetable.h>
45 +#include <linux/spi/spi.h>
46 +
47 +struct rtspi {
48 + void __iomem *base;
49 +};
50 +
51 +/* SPI Flash Configuration Register */
52 +#define RTL_SPI_SFCR 0x00
53 +#define RTL_SPI_SFCR_RBO BIT(28)
54 +#define RTL_SPI_SFCR_WBO BIT(27)
55 +
56 +/* SPI Flash Control and Status Register */
57 +#define RTL_SPI_SFCSR 0x08
58 +#define RTL_SPI_SFCSR_CSB0 BIT(31)
59 +#define RTL_SPI_SFCSR_CSB1 BIT(30)
60 +#define RTL_SPI_SFCSR_RDY BIT(27)
61 +#define RTL_SPI_SFCSR_CS BIT(24)
62 +#define RTL_SPI_SFCSR_LEN_MASK ~(0x03 << 28)
63 +#define RTL_SPI_SFCSR_LEN1 (0x00 << 28)
64 +#define RTL_SPI_SFCSR_LEN4 (0x03 << 28)
65 +
66 +/* SPI Flash Data Register */
67 +#define RTL_SPI_SFDR 0x0c
68 +
69 +#define REG(x) (rtspi->base + x)
70 +
71 +
72 +static void rt_set_cs(struct spi_device *spi, bool active)
73 +{
74 + struct rtspi *rtspi = spi_controller_get_devdata(spi->controller);
75 + u32 value;
76 +
77 + /* CS0 bit is active low */
78 + value = readl(REG(RTL_SPI_SFCSR));
79 + if (active)
80 + value |= RTL_SPI_SFCSR_CSB0;
81 + else
82 + value &= ~RTL_SPI_SFCSR_CSB0;
83 + writel(value, REG(RTL_SPI_SFCSR));
84 +}
85 +
86 +static void set_size(struct rtspi *rtspi, int size)
87 +{
88 + u32 value;
89 +
90 + value = readl(REG(RTL_SPI_SFCSR));
91 + value &= RTL_SPI_SFCSR_LEN_MASK;
92 + if (size == 4)
93 + value |= RTL_SPI_SFCSR_LEN4;
94 + else if (size == 1)
95 + value |= RTL_SPI_SFCSR_LEN1;
96 + writel(value, REG(RTL_SPI_SFCSR));
97 +}
98 +
99 +static inline void wait_ready(struct rtspi *rtspi)
100 +{
101 + while (!(readl(REG(RTL_SPI_SFCSR)) & RTL_SPI_SFCSR_RDY))
102 + cpu_relax();
103 +}
104 +static void send4(struct rtspi *rtspi, const u32 *buf)
105 +{
106 + wait_ready(rtspi);
107 + set_size(rtspi, 4);
108 + writel(*buf, REG(RTL_SPI_SFDR));
109 +}
110 +
111 +static void send1(struct rtspi *rtspi, const u8 *buf)
112 +{
113 + wait_ready(rtspi);
114 + set_size(rtspi, 1);
115 + writel(buf[0] << 24, REG(RTL_SPI_SFDR));
116 +}
117 +
118 +static void rcv4(struct rtspi *rtspi, u32 *buf)
119 +{
120 + wait_ready(rtspi);
121 + set_size(rtspi, 4);
122 + *buf = readl(REG(RTL_SPI_SFDR));
123 +}
124 +
125 +static void rcv1(struct rtspi *rtspi, u8 *buf)
126 +{
127 + wait_ready(rtspi);
128 + set_size(rtspi, 1);
129 + *buf = readl(REG(RTL_SPI_SFDR)) >> 24;
130 +}
131 +
132 +static int transfer_one(struct spi_controller *ctrl, struct spi_device *spi,
133 + struct spi_transfer *xfer)
134 +{
135 + struct rtspi *rtspi = spi_controller_get_devdata(ctrl);
136 + void *rx_buf;
137 + const void *tx_buf;
138 + int cnt;
139 +
140 + tx_buf = xfer->tx_buf;
141 + rx_buf = xfer->rx_buf;
142 + cnt = xfer->len;
143 + if (tx_buf) {
144 + while (cnt >= 4) {
145 + send4(rtspi, tx_buf);
146 + tx_buf += 4;
147 + cnt -= 4;
148 + }
149 + while (cnt) {
150 + send1(rtspi, tx_buf);
151 + tx_buf++;
152 + cnt--;
153 + }
154 + } else if (rx_buf) {
155 + while (cnt >= 4) {
156 + rcv4(rtspi, rx_buf);
157 + rx_buf += 4;
158 + cnt -= 4;
159 + }
160 + while (cnt) {
161 + rcv1(rtspi, rx_buf);
162 + rx_buf++;
163 + cnt--;
164 + }
165 + }
166 +
167 + spi_finalize_current_transfer(ctrl);
168 +
169 + return 0;
170 +}
171 +
172 +static void init_hw(struct rtspi *rtspi)
173 +{
174 + u32 value;
175 +
176 + /* Turn on big-endian byte ordering */
177 + value = readl(REG(RTL_SPI_SFCR));
178 + value |= RTL_SPI_SFCR_RBO | RTL_SPI_SFCR_WBO;
179 + writel(value, REG(RTL_SPI_SFCR));
180 +
181 + value = readl(REG(RTL_SPI_SFCSR));
182 + /* Permanently disable CS1, since it's never used */
183 + value |= RTL_SPI_SFCSR_CSB1;
184 + /* Select CS0 for use */
185 + value &= RTL_SPI_SFCSR_CS;
186 + writel(value, REG(RTL_SPI_SFCSR));
187 +}
188 +
189 +static int realtek_rtl_spi_probe(struct platform_device *pdev)
190 +{
191 + struct spi_controller *ctrl;
192 + struct rtspi *rtspi;
193 + int err;
194 +
195 + ctrl = devm_spi_alloc_master(&pdev->dev, sizeof(*rtspi));
196 + if (!ctrl) {
197 + dev_err(&pdev->dev, "Error allocating SPI controller\n");
198 + return -ENOMEM;
199 + }
200 + platform_set_drvdata(pdev, ctrl);
201 + rtspi = spi_controller_get_devdata(ctrl);
202 +
203 + rtspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
204 + if (IS_ERR(rtspi->base)) {
205 + dev_err(&pdev->dev, "Could not map SPI register address");
206 + return -ENOMEM;
207 + }
208 +
209 + init_hw(rtspi);
210 +
211 + ctrl->dev.of_node = pdev->dev.of_node;
212 + ctrl->flags = SPI_CONTROLLER_HALF_DUPLEX;
213 + ctrl->set_cs = rt_set_cs;
214 + ctrl->transfer_one = transfer_one;
215 +
216 + err = devm_spi_register_controller(&pdev->dev, ctrl);
217 + if (err) {
218 + dev_err(&pdev->dev, "Could not register SPI controller\n");
219 + return -ENODEV;
220 + }
221 +
222 + return 0;
223 +}
224 +
225 +
226 +static const struct of_device_id realtek_rtl_spi_of_ids[] = {
227 + { .compatible = "realtek,rtl8380-spi" },
228 + { .compatible = "realtek,rtl8382-spi" },
229 + { .compatible = "realtek,rtl8391-spi" },
230 + { .compatible = "realtek,rtl8392-spi" },
231 + { .compatible = "realtek,rtl8393-spi" },
232 + { /* sentinel */ }
233 +};
234 +MODULE_DEVICE_TABLE(of, realtek_rtl_spi_of_ids);
235 +
236 +static struct platform_driver realtek_rtl_spi_driver = {
237 + .probe = realtek_rtl_spi_probe,
238 + .driver = {
239 + .name = "realtek-rtl-spi",
240 + .of_match_table = realtek_rtl_spi_of_ids,
241 + },
242 +};
243 +
244 +module_platform_driver(realtek_rtl_spi_driver);
245 +
246 +MODULE_LICENSE("GPL v2");
247 +MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
248 +MODULE_DESCRIPTION("Realtek RTL SPI driver");