1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 MediaTek Inc.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
7 * Generic SPI driver for MediaTek MT7620 SoC
12 #include <dm/device_compat.h>
14 #include <linux/bitops.h>
15 #include <linux/iopoll.h>
17 #include <linux/log2.h>
19 #define MT7620_SPI_NUM_CS 2
20 #define MT7620_SPI_MASTER1_OFF 0x00
21 #define MT7620_SPI_MASTER2_OFF 0x40
24 #define SPI_BUSY BIT(0)
27 #define MSB_FIRST BIT(8)
28 #define SPI_CLK_POL BIT(6)
29 #define RX_CLK_EDGE BIT(5)
30 #define TX_CLK_EDGE BIT(4)
32 #define SPI_CLK_M GENMASK(2, 0)
35 #define START_WR BIT(2)
36 #define START_RD BIT(1)
37 #define SPI_HIGH BIT(0)
40 #define ARB_EN BIT(31)
42 #define POLLING_SCALE 10
43 #define POLLING_FRAC_USEC 100
45 struct mt7620_spi_master_regs {
56 struct mt7620_spi_master_regs *m[MT7620_SPI_NUM_CS];
57 unsigned int sys_freq;
63 static void mt7620_spi_master_setup(struct mt7620_spi *ms, int cs)
65 u32 rate, prescale, freq, tmo, cfg;
67 /* Calculate the clock divsior */
68 rate = DIV_ROUND_UP(ms->sys_freq, ms->speed);
69 rate = roundup_pow_of_two(rate);
71 prescale = ilog2(rate / 2);
75 /* Calculate the real clock, and usecs for one byte transaction */
76 freq = ms->sys_freq >> (prescale + 1);
77 tmo = DIV_ROUND_UP(8 * 1000000, freq);
79 /* 10 times tolerance plus 100us */
80 ms->wait_us = POLLING_SCALE * tmo + POLLING_FRAC_USEC;
83 cfg = prescale << SPI_CLK_S;
85 switch (ms->mode & (SPI_CPOL | SPI_CPHA)) {
93 cfg |= SPI_CLK_POL | RX_CLK_EDGE;
96 cfg |= SPI_CLK_POL | TX_CLK_EDGE;
100 if (!(ms->mode & SPI_LSB_FIRST))
103 writel(cfg, &ms->m[cs]->cfg);
105 writel(SPI_HIGH, &ms->m[cs]->ctl);
108 static void mt7620_spi_set_cs(struct mt7620_spi *ms, int cs, bool enable)
111 mt7620_spi_master_setup(ms, cs);
113 if (ms->mode & SPI_CS_HIGH)
117 clrbits_32(&ms->m[cs]->ctl, SPI_HIGH);
119 setbits_32(&ms->m[cs]->ctl, SPI_HIGH);
122 static int mt7620_spi_set_mode(struct udevice *bus, uint mode)
124 struct mt7620_spi *ms = dev_get_priv(bus);
128 /* Mode 0 is buggy. Force to use mode 3 */
129 if ((mode & SPI_MODE_3) == SPI_MODE_0)
130 ms->mode |= SPI_MODE_3;
135 static int mt7620_spi_set_speed(struct udevice *bus, uint speed)
137 struct mt7620_spi *ms = dev_get_priv(bus);
144 static inline int mt7620_spi_busy_poll(struct mt7620_spi *ms, int cs)
148 return readl_poll_timeout(&ms->m[cs]->stat, val, !(val & SPI_BUSY),
152 static int mt7620_spi_read(struct mt7620_spi *ms, int cs, u8 *buf, size_t len)
157 setbits_32(&ms->m[cs]->ctl, START_RD);
159 ret = mt7620_spi_busy_poll(ms, cs);
163 *buf++ = (u8)readl(&ms->m[cs]->data);
171 static int mt7620_spi_write(struct mt7620_spi *ms, int cs, const u8 *buf,
177 writel(*buf++, &ms->m[cs]->data);
178 setbits_32(&ms->m[cs]->ctl, START_WR);
180 ret = mt7620_spi_busy_poll(ms, cs);
190 static int mt7620_spi_xfer(struct udevice *dev, unsigned int bitlen,
191 const void *dout, void *din, unsigned long flags)
193 struct udevice *bus = dev->parent;
194 struct mt7620_spi *ms = dev_get_priv(bus);
195 int total_size = bitlen >> 3;
199 * This driver only supports half-duplex, so complain and bail out
200 * upon full-duplex messages
203 dev_err(dev, "mt7620_spi: Only half-duplex is supported\n");
207 cs = spi_chip_select(dev);
208 if (cs < 0 || cs >= MT7620_SPI_NUM_CS) {
209 dev_err(dev, "mt7620_spi: Invalid chip select %d\n", cs);
213 if (flags & SPI_XFER_BEGIN)
214 mt7620_spi_set_cs(ms, cs, true);
217 ret = mt7620_spi_read(ms, cs, din, total_size);
219 ret = mt7620_spi_write(ms, cs, dout, total_size);
222 dev_err(dev, "mt7620_spi: %s transaction timeout\n",
223 din ? "read" : "write");
225 if (flags & SPI_XFER_END)
226 mt7620_spi_set_cs(ms, cs, false);
231 static int mt7620_spi_probe(struct udevice *dev)
233 struct mt7620_spi *ms = dev_get_priv(dev);
237 ms->regs = dev_remap_addr(dev);
241 ms->m[0] = ms->regs + MT7620_SPI_MASTER1_OFF;
242 ms->m[1] = ms->regs + MT7620_SPI_MASTER2_OFF;
244 ret = clk_get_by_index(dev, 0, &clk);
246 dev_err(dev, "mt7620_spi: Please provide a clock!\n");
252 ms->sys_freq = clk_get_rate(&clk);
254 dev_err(dev, "mt7620_spi: Please provide a valid bus clock!\n");
258 writel(ARB_EN, ms->regs + SPI_ARB);
263 static const struct dm_spi_ops mt7620_spi_ops = {
264 .set_mode = mt7620_spi_set_mode,
265 .set_speed = mt7620_spi_set_speed,
266 .xfer = mt7620_spi_xfer,
269 static const struct udevice_id mt7620_spi_ids[] = {
270 { .compatible = "mediatek,mt7620-spi" },
274 U_BOOT_DRIVER(mt7620_spi) = {
275 .name = "mt7620_spi",
277 .of_match = mt7620_spi_ids,
278 .ops = &mt7620_spi_ops,
279 .priv_auto = sizeof(struct mt7620_spi),
280 .probe = mt7620_spi_probe,