blob: b0ed14f0cfc933d9f647c038038ceed078ccbb93 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wills Wangf5021482016-03-16 16:59:58 +08002/*
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
Wills Wangf5021482016-03-16 16:59:58 +08004 */
5
Simon Glass85d65312019-12-28 10:44:58 -07006#include <clock_legacy.h>
Wills Wangf5021482016-03-16 16:59:58 +08007#include <spi.h>
8#include <dm.h>
9#include <div64.h>
10#include <errno.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Wills Wangf5021482016-03-16 16:59:58 +080012#include <asm/io.h>
13#include <asm/addrspace.h>
14#include <asm/types.h>
15#include <dm/pinctrl.h>
16#include <mach/ar71xx_regs.h>
17
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)
22
23struct ath79_spi_priv {
24 void __iomem *regs;
25 u32 rrw_delay;
26};
27
28static void spi_cs_activate(struct udevice *dev)
29{
30 struct udevice *bus = dev_get_parent(dev);
31 struct ath79_spi_priv *priv = dev_get_priv(bus);
32
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);
35}
36
37static void spi_cs_deactivate(struct udevice *dev)
38{
39 struct udevice *bus = dev_get_parent(dev);
40 struct ath79_spi_priv *priv = dev_get_priv(bus);
41
42 writel(AR71XX_SPI_IOC_CS_ALL, priv->regs + AR71XX_SPI_REG_IOC);
43 writel(0, priv->regs + AR71XX_SPI_REG_FS);
44}
45
46static int ath79_spi_claim_bus(struct udevice *dev)
47{
48 return 0;
49}
50
51static int ath79_spi_release_bus(struct udevice *dev)
52{
53 return 0;
54}
55
56static int ath79_spi_xfer(struct udevice *dev, unsigned int bitlen,
57 const void *dout, void *din, unsigned long flags)
58{
59 struct udevice *bus = dev_get_parent(dev);
60 struct ath79_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -070061 struct dm_spi_slave_plat *slave = dev_get_parent_plat(dev);
Wills Wangf5021482016-03-16 16:59:58 +080062 u8 *rx = din;
63 const u8 *tx = dout;
64 u8 curbyte, curbitlen, restbits;
65 u32 bytes = bitlen / 8;
66 u32 out, in;
67 u64 tick;
68
69 if (flags & SPI_XFER_BEGIN)
70 spi_cs_activate(dev);
71
72 restbits = (bitlen % 8);
73 if (restbits)
74 bytes++;
75
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +053076 out = AR71XX_SPI_IOC_CS_ALL & ~(AR71XX_SPI_IOC_CS(slave->cs[0]));
Wills Wangf5021482016-03-16 16:59:58 +080077 while (bytes > 0) {
78 bytes--;
79 curbyte = 0;
80 if (tx)
81 curbyte = *tx++;
82
83 if (restbits && !bytes) {
84 curbitlen = restbits;
85 curbyte <<= 8 - restbits;
86 } else {
87 curbitlen = 8;
88 }
89
90 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
91 if (curbyte & 0x80)
92 out |= AR71XX_SPI_IOC_DO;
93 else
94 out &= ~(AR71XX_SPI_IOC_DO);
95
96 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
97
98 /* delay for low level */
99 if (priv->rrw_delay) {
100 tick = get_ticks() + priv->rrw_delay;
101 while (get_ticks() < tick)
102 /*NOP*/;
103 }
104
105 writel(out | AR71XX_SPI_IOC_CLK,
106 priv->regs + AR71XX_SPI_REG_IOC);
107
108 /* delay for high level */
109 if (priv->rrw_delay) {
110 tick = get_ticks() + priv->rrw_delay;
111 while (get_ticks() < tick)
112 /*NOP*/;
113 }
114
115 curbyte <<= 1;
116 }
117
118 if (!bytes)
119 writel(out, priv->regs + AR71XX_SPI_REG_IOC);
120
121 in = readl(priv->regs + AR71XX_SPI_REG_RDS);
122 if (rx) {
123 if (restbits && !bytes)
124 *rx++ = (in << (8 - restbits));
125 else
126 *rx++ = in;
127 }
128 }
129
130 if (flags & SPI_XFER_END)
131 spi_cs_deactivate(dev);
132
133 return 0;
134}
135
Wills Wangf5021482016-03-16 16:59:58 +0800136static int ath79_spi_set_speed(struct udevice *bus, uint speed)
137{
138 struct ath79_spi_priv *priv = dev_get_priv(bus);
139 u32 val, div = 0;
140 u64 time;
141
142 if (speed)
143 div = get_bus_freq(0) / speed;
144
145 if (div > 63)
146 div = 63;
147
148 if (div < 5)
149 div = 5;
150
151 /* calculate delay */
152 time = get_tbclk();
153 do_div(time, speed / 2);
154 val = get_bus_freq(0) / ATH79_SPI_MHZ;
155 val = ATH79_SPI_RRW_DELAY_FACTOR / val;
156 if (time > val)
157 priv->rrw_delay = time - val + 1;
158 else
159 priv->rrw_delay = 0;
160
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);
166 return 0;
167}
168
169static int ath79_spi_set_mode(struct udevice *bus, uint mode)
170{
171 return 0;
172}
173
174static int ath79_spi_probe(struct udevice *bus)
175{
176 struct ath79_spi_priv *priv = dev_get_priv(bus);
Wills Wangf5021482016-03-16 16:59:58 +0800177 fdt_addr_t addr;
Wills Wangf5021482016-03-16 16:59:58 +0800178
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900179 addr = dev_read_addr(bus);
Wills Wangf5021482016-03-16 16:59:58 +0800180 if (addr == FDT_ADDR_T_NONE)
181 return -EINVAL;
182
183 priv->regs = map_physmem(addr,
184 AR71XX_SPI_SIZE,
185 MAP_NOCACHE);
186
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);
192
193 return 0;
194}
195
196static int ath79_cs_info(struct udevice *bus, uint cs,
197 struct spi_cs_info *info)
198{
199 /* Always allow activity on CS 0/1/2 */
200 if (cs >= 3)
Bin Mengf8586f62019-09-09 06:00:01 -0700201 return -EINVAL;
Wills Wangf5021482016-03-16 16:59:58 +0800202
203 return 0;
204}
205
206static 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,
213};
214
215static const struct udevice_id ath79_spi_ids[] = {
216 { .compatible = "qca,ar7100-spi" },
217 {}
218};
219
220U_BOOT_DRIVER(ath79_spi) = {
221 .name = "ath79_spi",
222 .id = UCLASS_SPI,
223 .of_match = ath79_spi_ids,
224 .ops = &ath79_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700225 .priv_auto = sizeof(struct ath79_spi_priv),
Wills Wangf5021482016-03-16 16:59:58 +0800226 .probe = ath79_spi_probe,
227};