1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/module.h>
4 #include <linux/platform_device.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/spi/spi.h>
12 /* SPI Flash Configuration Register */
13 #define RTL_SPI_SFCR 0x00
14 #define RTL_SPI_SFCR_RBO BIT(28)
15 #define RTL_SPI_SFCR_WBO BIT(27)
17 /* SPI Flash Control and Status Register */
18 #define RTL_SPI_SFCSR 0x08
19 #define RTL_SPI_SFCSR_CSB0 BIT(31)
20 #define RTL_SPI_SFCSR_CSB1 BIT(30)
21 #define RTL_SPI_SFCSR_RDY BIT(27)
22 #define RTL_SPI_SFCSR_CS BIT(24)
23 #define RTL_SPI_SFCSR_LEN_MASK ~(0x03 << 28)
24 #define RTL_SPI_SFCSR_LEN1 (0x00 << 28)
25 #define RTL_SPI_SFCSR_LEN4 (0x03 << 28)
27 /* SPI Flash Data Register */
28 #define RTL_SPI_SFDR 0x0c
30 #define REG(x) (rtspi->base + x)
33 static void rt_set_cs(struct spi_device *spi, bool active)
35 struct rtspi *rtspi = spi_controller_get_devdata(spi->controller);
38 /* CS0 bit is active low */
39 value = readl(REG(RTL_SPI_SFCSR));
41 value |= RTL_SPI_SFCSR_CSB0;
43 value &= ~RTL_SPI_SFCSR_CSB0;
44 writel(value, REG(RTL_SPI_SFCSR));
47 static void set_size(struct rtspi *rtspi, int size)
51 value = readl(REG(RTL_SPI_SFCSR));
52 value &= RTL_SPI_SFCSR_LEN_MASK;
54 value |= RTL_SPI_SFCSR_LEN4;
56 value |= RTL_SPI_SFCSR_LEN1;
57 writel(value, REG(RTL_SPI_SFCSR));
60 static inline void wait_ready(struct rtspi *rtspi)
62 while (!(readl(REG(RTL_SPI_SFCSR)) & RTL_SPI_SFCSR_RDY))
65 static void send4(struct rtspi *rtspi, const u32 *buf)
69 writel(*buf, REG(RTL_SPI_SFDR));
72 static void send1(struct rtspi *rtspi, const u8 *buf)
76 writel(buf[0] << 24, REG(RTL_SPI_SFDR));
79 static void rcv4(struct rtspi *rtspi, u32 *buf)
83 *buf = readl(REG(RTL_SPI_SFDR));
86 static void rcv1(struct rtspi *rtspi, u8 *buf)
90 *buf = readl(REG(RTL_SPI_SFDR)) >> 24;
93 static int transfer_one(struct spi_controller *ctrl, struct spi_device *spi,
94 struct spi_transfer *xfer)
96 struct rtspi *rtspi = spi_controller_get_devdata(ctrl);
101 tx_buf = xfer->tx_buf;
102 rx_buf = xfer->rx_buf;
106 send4(rtspi, tx_buf);
111 send1(rtspi, tx_buf);
128 spi_finalize_current_transfer(ctrl);
133 static void init_hw(struct rtspi *rtspi)
137 /* Turn on big-endian byte ordering */
138 value = readl(REG(RTL_SPI_SFCR));
139 value |= RTL_SPI_SFCR_RBO | RTL_SPI_SFCR_WBO;
140 writel(value, REG(RTL_SPI_SFCR));
142 value = readl(REG(RTL_SPI_SFCSR));
143 /* Permanently disable CS1, since it's never used */
144 value |= RTL_SPI_SFCSR_CSB1;
145 /* Select CS0 for use */
146 value &= RTL_SPI_SFCSR_CS;
147 writel(value, REG(RTL_SPI_SFCSR));
150 static int realtek_rtl_spi_probe(struct platform_device *pdev)
152 struct spi_controller *ctrl;
156 ctrl = devm_spi_alloc_host(&pdev->dev, sizeof(*rtspi));
158 dev_err(&pdev->dev, "Error allocating SPI controller\n");
161 platform_set_drvdata(pdev, ctrl);
162 rtspi = spi_controller_get_devdata(ctrl);
164 rtspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
165 if (IS_ERR(rtspi->base)) {
166 dev_err(&pdev->dev, "Could not map SPI register address");
172 ctrl->dev.of_node = pdev->dev.of_node;
173 ctrl->flags = SPI_CONTROLLER_HALF_DUPLEX;
174 ctrl->set_cs = rt_set_cs;
175 ctrl->transfer_one = transfer_one;
177 err = devm_spi_register_controller(&pdev->dev, ctrl);
179 dev_err(&pdev->dev, "Could not register SPI controller\n");
187 static const struct of_device_id realtek_rtl_spi_of_ids[] = {
188 { .compatible = "realtek,rtl8380-spi" },
189 { .compatible = "realtek,rtl8382-spi" },
190 { .compatible = "realtek,rtl8391-spi" },
191 { .compatible = "realtek,rtl8392-spi" },
192 { .compatible = "realtek,rtl8393-spi" },
195 MODULE_DEVICE_TABLE(of, realtek_rtl_spi_of_ids);
197 static struct platform_driver realtek_rtl_spi_driver = {
198 .probe = realtek_rtl_spi_probe,
200 .name = "realtek-rtl-spi",
201 .of_match_table = realtek_rtl_spi_of_ids,
205 module_platform_driver(realtek_rtl_spi_driver);
207 MODULE_LICENSE("GPL v2");
208 MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
209 MODULE_DESCRIPTION("Realtek RTL SPI driver");