2 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/addrspace.h>
14 #include <asm/types.h>
15 #include <dm/pinctrl.h>
16 #include <mach/ar71xx_regs.h>
18 /* CLOCK_DIVIDER = 3 (SPI clock = 200 / 8 ~ 25 MHz) */
19 #define ATH79_SPI_CLK_DIV(x) (((x) >> 1) - 1)
20 #define ATH79_SPI_RRW_DELAY_FACTOR 12000
21 #define ATH79_SPI_MHZ (1000 * 1000)
23 struct ath79_spi_priv {
28 static void spi_cs_activate(struct udevice *dev)
30 struct udevice *bus = dev_get_parent(dev);
31 struct ath79_spi_priv *priv = dev_get_priv(bus);
33 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
34 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
37 static void spi_cs_deactivate(struct udevice *dev)
39 struct udevice *bus = dev_get_parent(dev);
40 struct ath79_spi_priv *priv = dev_get_priv(bus);
42 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
43 writel(0, priv->regs + AR71XX_SPI_REG_FS);
46 static int ath79_spi_claim_bus(struct udevice *dev)
51 static int ath79_spi_release_bus(struct udevice *dev)
56 static int ath79_spi_xfer(struct udevice *dev, unsigned int bitlen,
57 const void *dout, void *din, unsigned long flags)
59 struct udevice *bus = dev_get_parent(dev);
60 struct ath79_spi_priv *priv = dev_get_priv(bus);
61 struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
64 u8 curbyte, curbitlen, restbits;
65 u32 bytes = bitlen / 8;
69 if (flags & SPI_XFER_BEGIN)
72 restbits = (bitlen % 8);
76 out = AR71XX_SPI_IOC_CS_ALL & ~(AR71XX_SPI_IOC_CS(slave->cs));
83 if (restbits && !bytes) {
85 curbyte <<= 8 - restbits;
90 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
92 out |= AR71XX_SPI_IOC_DO;
94 out &= ~(AR71XX_SPI_IOC_DO);
96 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
98 /* delay for low level */
99 if (priv->rrw_delay) {
100 tick = get_ticks() + priv->rrw_delay;
101 while (get_ticks() < tick)
105 writel(out | AR71XX_SPI_IOC_CLK,
106 priv->regs + AR71XX_SPI_REG_IOC);
108 /* delay for high level */
109 if (priv->rrw_delay) {
110 tick = get_ticks() + priv->rrw_delay;
111 while (get_ticks() < tick)
119 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
121 in = readl(priv->regs + AR71XX_SPI_REG_RDS);
123 if (restbits && !bytes)
124 *rx++ = (in << (8 - restbits));
130 if (flags & SPI_XFER_END)
131 spi_cs_deactivate(dev);
137 static int ath79_spi_set_speed(struct udevice *bus, uint speed)
139 struct ath79_spi_priv *priv = dev_get_priv(bus);
144 div = get_bus_freq(0) / speed;
152 /* calculate delay */
154 do_div(time, speed / 2);
155 val = get_bus_freq(0) / ATH79_SPI_MHZ;
156 val = ATH79_SPI_RRW_DELAY_FACTOR / val;
158 priv->rrw_delay = time - val + 1;
162 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
163 clrsetbits_be32(priv->regs + AR71XX_SPI_REG_CTRL,
164 AR71XX_SPI_CTRL_DIV_MASK,
165 ATH79_SPI_CLK_DIV(div));
166 writel(0, priv->regs + AR71XX_SPI_REG_FS);
170 static int ath79_spi_set_mode(struct udevice *bus, uint mode)
175 static int ath79_spi_probe(struct udevice *bus)
177 struct ath79_spi_priv *priv = dev_get_priv(bus);
180 addr = devfdt_get_addr(bus);
181 if (addr == FDT_ADDR_T_NONE)
184 priv->regs = map_physmem(addr,
188 /* Init SPI Hardware, disable remap, set clock */
189 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
190 writel(AR71XX_SPI_CTRL_RD | ATH79_SPI_CLK_DIV(8),
191 priv->regs + AR71XX_SPI_REG_CTRL);
192 writel(0, priv->regs + AR71XX_SPI_REG_FS);
197 static int ath79_cs_info(struct udevice *bus, uint cs,
198 struct spi_cs_info *info)
200 /* Always allow activity on CS 0/1/2 */
207 static const struct dm_spi_ops ath79_spi_ops = {
208 .claim_bus = ath79_spi_claim_bus,
209 .release_bus = ath79_spi_release_bus,
210 .xfer = ath79_spi_xfer,
211 .set_speed = ath79_spi_set_speed,
212 .set_mode = ath79_spi_set_mode,
213 .cs_info = ath79_cs_info,
216 static const struct udevice_id ath79_spi_ids[] = {
217 { .compatible = "qca,ar7100-spi" },
221 U_BOOT_DRIVER(ath79_spi) = {
224 .of_match = ath79_spi_ids,
225 .ops = &ath79_spi_ops,
226 .priv_auto_alloc_size = sizeof(struct ath79_spi_priv),
227 .probe = ath79_spi_probe,