1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
7 #include <clock_legacy.h>
14 #include <asm/addrspace.h>
15 #include <asm/types.h>
16 #include <dm/pinctrl.h>
17 #include <mach/ar71xx_regs.h>
19 /* CLOCK_DIVIDER = 3 (SPI clock = 200 / 8 ~ 25 MHz) */
20 #define ATH79_SPI_CLK_DIV(x) (((x) >> 1) - 1)
21 #define ATH79_SPI_RRW_DELAY_FACTOR 12000
22 #define ATH79_SPI_MHZ (1000 * 1000)
24 struct ath79_spi_priv {
29 static void spi_cs_activate(struct udevice *dev)
31 struct udevice *bus = dev_get_parent(dev);
32 struct ath79_spi_priv *priv = dev_get_priv(bus);
34 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
35 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
38 static void spi_cs_deactivate(struct udevice *dev)
40 struct udevice *bus = dev_get_parent(dev);
41 struct ath79_spi_priv *priv = dev_get_priv(bus);
43 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
44 writel(0, priv->regs + AR71XX_SPI_REG_FS);
47 static int ath79_spi_claim_bus(struct udevice *dev)
52 static int ath79_spi_release_bus(struct udevice *dev)
57 static int ath79_spi_xfer(struct udevice *dev, unsigned int bitlen,
58 const void *dout, void *din, unsigned long flags)
60 struct udevice *bus = dev_get_parent(dev);
61 struct ath79_spi_priv *priv = dev_get_priv(bus);
62 struct dm_spi_slave_platdata *slave = dev_get_parent_platdata(dev);
65 u8 curbyte, curbitlen, restbits;
66 u32 bytes = bitlen / 8;
70 if (flags & SPI_XFER_BEGIN)
73 restbits = (bitlen % 8);
77 out = AR71XX_SPI_IOC_CS_ALL & ~(AR71XX_SPI_IOC_CS(slave->cs));
84 if (restbits && !bytes) {
86 curbyte <<= 8 - restbits;
91 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
93 out |= AR71XX_SPI_IOC_DO;
95 out &= ~(AR71XX_SPI_IOC_DO);
97 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
99 /* delay for low level */
100 if (priv->rrw_delay) {
101 tick = get_ticks() + priv->rrw_delay;
102 while (get_ticks() < tick)
106 writel(out | AR71XX_SPI_IOC_CLK,
107 priv->regs + AR71XX_SPI_REG_IOC);
109 /* delay for high level */
110 if (priv->rrw_delay) {
111 tick = get_ticks() + priv->rrw_delay;
112 while (get_ticks() < tick)
120 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
122 in = readl(priv->regs + AR71XX_SPI_REG_RDS);
124 if (restbits && !bytes)
125 *rx++ = (in << (8 - restbits));
131 if (flags & SPI_XFER_END)
132 spi_cs_deactivate(dev);
138 static int ath79_spi_set_speed(struct udevice *bus, uint speed)
140 struct ath79_spi_priv *priv = dev_get_priv(bus);
145 div = get_bus_freq(0) / speed;
153 /* calculate delay */
155 do_div(time, speed / 2);
156 val = get_bus_freq(0) / ATH79_SPI_MHZ;
157 val = ATH79_SPI_RRW_DELAY_FACTOR / val;
159 priv->rrw_delay = time - val + 1;
163 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
164 clrsetbits_be32(priv->regs + AR71XX_SPI_REG_CTRL,
165 AR71XX_SPI_CTRL_DIV_MASK,
166 ATH79_SPI_CLK_DIV(div));
167 writel(0, priv->regs + AR71XX_SPI_REG_FS);
171 static int ath79_spi_set_mode(struct udevice *bus, uint mode)
176 static int ath79_spi_probe(struct udevice *bus)
178 struct ath79_spi_priv *priv = dev_get_priv(bus);
181 addr = dev_read_addr(bus);
182 if (addr == FDT_ADDR_T_NONE)
185 priv->regs = map_physmem(addr,
189 /* Init SPI Hardware, disable remap, set clock */
190 writel(AR71XX_SPI_FS_GPIO, priv->regs + AR71XX_SPI_REG_FS);
191 writel(AR71XX_SPI_CTRL_RD | ATH79_SPI_CLK_DIV(8),
192 priv->regs + AR71XX_SPI_REG_CTRL);
193 writel(0, priv->regs + AR71XX_SPI_REG_FS);
198 static int ath79_cs_info(struct udevice *bus, uint cs,
199 struct spi_cs_info *info)
201 /* Always allow activity on CS 0/1/2 */
208 static const struct dm_spi_ops ath79_spi_ops = {
209 .claim_bus = ath79_spi_claim_bus,
210 .release_bus = ath79_spi_release_bus,
211 .xfer = ath79_spi_xfer,
212 .set_speed = ath79_spi_set_speed,
213 .set_mode = ath79_spi_set_mode,
214 .cs_info = ath79_cs_info,
217 static const struct udevice_id ath79_spi_ids[] = {
218 { .compatible = "qca,ar7100-spi" },
222 U_BOOT_DRIVER(ath79_spi) = {
225 .of_match = ath79_spi_ids,
226 .ops = &ath79_spi_ops,
227 .priv_auto_alloc_size = sizeof(struct ath79_spi_priv),
228 .probe = ath79_spi_probe,