1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
12 #include <asm/addrspace.h>
13 #include <asm/types.h>
14 #include <dm/pinctrl.h>
15 #include <mach/ar71xx_regs.h>
17 /* CLOCK_DIVIDER = 3 (SPI clock = 200 / 8 ~ 25 MHz) */
18 #define ATH79_SPI_CLK_DIV(x) (((x) >> 1) - 1)
19 #define ATH79_SPI_RRW_DELAY_FACTOR 12000
20 #define ATH79_SPI_MHZ (1000 * 1000)
22 struct ath79_spi_priv {
27 static void spi_cs_activate(struct udevice *dev)
29 struct udevice *bus = dev_get_parent(dev);
30 struct ath79_spi_priv *priv = dev_get_priv(bus);
32 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
33 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
36 static void spi_cs_deactivate(struct udevice *dev)
38 struct udevice *bus = dev_get_parent(dev);
39 struct ath79_spi_priv *priv = dev_get_priv(bus);
41 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
42 writel(0, priv->regs + AR71XX_SPI_REG_FS);
45 static int ath79_spi_claim_bus(struct udevice *dev)
50 static int ath79_spi_release_bus(struct udevice *dev)
55 static int ath79_spi_xfer(struct udevice *dev, unsigned int bitlen,
56 const void *dout, void *din, unsigned long flags)
58 struct udevice *bus = dev_get_parent(dev);
59 struct ath79_spi_priv *priv = dev_get_priv(bus);
60 struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
63 u8 curbyte, curbitlen, restbits;
64 u32 bytes = bitlen / 8;
68 if (flags & SPI_XFER_BEGIN)
71 restbits = (bitlen % 8);
75 out = AR71XX_SPI_IOC_CS_ALL & ~(AR71XX_SPI_IOC_CS(slave->cs));
82 if (restbits && !bytes) {
84 curbyte <<= 8 - restbits;
89 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
91 out |= AR71XX_SPI_IOC_DO;
93 out &= ~(AR71XX_SPI_IOC_DO);
95 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
97 /* delay for low level */
98 if (priv->rrw_delay) {
99 tick = get_ticks() + priv->rrw_delay;
100 while (get_ticks() < tick)
104 writel(out | AR71XX_SPI_IOC_CLK,
105 priv->regs + AR71XX_SPI_REG_IOC);
107 /* delay for high level */
108 if (priv->rrw_delay) {
109 tick = get_ticks() + priv->rrw_delay;
110 while (get_ticks() < tick)
118 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
120 in = readl(priv->regs + AR71XX_SPI_REG_RDS);
122 if (restbits && !bytes)
123 *rx++ = (in << (8 - restbits));
129 if (flags & SPI_XFER_END)
130 spi_cs_deactivate(dev);
136 static int ath79_spi_set_speed(struct udevice *bus, uint speed)
138 struct ath79_spi_priv *priv = dev_get_priv(bus);
143 div = get_bus_freq(0) / speed;
151 /* calculate delay */
153 do_div(time, speed / 2);
154 val = get_bus_freq(0) / ATH79_SPI_MHZ;
155 val = ATH79_SPI_RRW_DELAY_FACTOR / val;
157 priv->rrw_delay = time - val + 1;
161 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
162 clrsetbits_be32(priv->regs + AR71XX_SPI_REG_CTRL,
163 AR71XX_SPI_CTRL_DIV_MASK,
164 ATH79_SPI_CLK_DIV(div));
165 writel(0, priv->regs + AR71XX_SPI_REG_FS);
169 static int ath79_spi_set_mode(struct udevice *bus, uint mode)
174 static int ath79_spi_probe(struct udevice *bus)
176 struct ath79_spi_priv *priv = dev_get_priv(bus);
179 addr = devfdt_get_addr(bus);
180 if (addr == FDT_ADDR_T_NONE)
183 priv->regs = map_physmem(addr,
187 /* Init SPI Hardware, disable remap, set clock */
188 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
189 writel(AR71XX_SPI_CTRL_RD | ATH79_SPI_CLK_DIV(8),
190 priv->regs + AR71XX_SPI_REG_CTRL);
191 writel(0, priv->regs + AR71XX_SPI_REG_FS);
196 static int ath79_cs_info(struct udevice *bus, uint cs,
197 struct spi_cs_info *info)
199 /* Always allow activity on CS 0/1/2 */
206 static const struct dm_spi_ops ath79_spi_ops = {
207 .claim_bus = ath79_spi_claim_bus,
208 .release_bus = ath79_spi_release_bus,
209 .xfer = ath79_spi_xfer,
210 .set_speed = ath79_spi_set_speed,
211 .set_mode = ath79_spi_set_mode,
212 .cs_info = ath79_cs_info,
215 static const struct udevice_id ath79_spi_ids[] = {
216 { .compatible = "qca,ar7100-spi" },
220 U_BOOT_DRIVER(ath79_spi) = {
223 .of_match = ath79_spi_ids,
224 .ops = &ath79_spi_ops,
225 .priv_auto_alloc_size = sizeof(struct ath79_spi_priv),
226 .probe = ath79_spi_probe,