1
2
3
4
5
6
7
8
9#include <common.h>
10#include <dm.h>
11#include <malloc.h>
12#include <spi.h>
13#include <wait_bit.h>
14#include <asm/io.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18#define MVEBU_SPI_A3700_XFER_RDY BIT(1)
19#define MVEBU_SPI_A3700_FIFO_FLUSH BIT(9)
20#define MVEBU_SPI_A3700_BYTE_LEN BIT(5)
21#define MVEBU_SPI_A3700_CLK_PHA BIT(6)
22#define MVEBU_SPI_A3700_CLK_POL BIT(7)
23#define MVEBU_SPI_A3700_FIFO_EN BIT(17)
24#define MVEBU_SPI_A3700_SPI_EN_0 BIT(16)
25#define MVEBU_SPI_A3700_CLK_PRESCALE_BIT 0
26#define MVEBU_SPI_A3700_CLK_PRESCALE_MASK \
27 (0x1f << MVEBU_SPI_A3700_CLK_PRESCALE_BIT)
28
29
30struct spi_reg {
31 u32 ctrl;
32 u32 cfg;
33 u32 dout;
34 u32 din;
35};
36
37struct mvebu_spi_platdata {
38 struct spi_reg *spireg;
39 unsigned int frequency;
40 unsigned int clock;
41};
42
43static void spi_cs_activate(struct spi_reg *reg, int cs)
44{
45 setbits_le32(®->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
46}
47
48static void spi_cs_deactivate(struct spi_reg *reg, int cs)
49{
50 clrbits_le32(®->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
51}
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81static int spi_legacy_shift_byte(struct spi_reg *reg, unsigned int bytelen,
82 const void *dout, void *din)
83{
84 const u8 *dout_8;
85 u8 *din_8;
86 int ret;
87
88
89 const u8 dummy_dout = 0x0;
90 u32 pending_dout = 0x0;
91
92
93 dout_8 = dout;
94
95 din_8 = din;
96
97 while (bytelen) {
98 ret = wait_for_bit(__func__, ®->ctrl,
99 MVEBU_SPI_A3700_XFER_RDY, true, 100, false);
100 if (ret)
101 return ret;
102
103 if (dout)
104 pending_dout = (u32)*dout_8;
105 else
106 pending_dout = (u32)dummy_dout;
107
108
109 writel(pending_dout, ®->dout);
110
111 if (din) {
112 ret = wait_for_bit(__func__, ®->ctrl,
113 MVEBU_SPI_A3700_XFER_RDY,
114 true, 100, false);
115 if (ret)
116 return ret;
117
118
119 *din_8 = (u8)readl(®->din);
120 }
121
122
123 if (dout)
124 dout_8++;
125 if (din)
126 din_8++;
127
128 bytelen--;
129 }
130
131 return 0;
132}
133
134static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
135 const void *dout, void *din, unsigned long flags)
136{
137 struct udevice *bus = dev->parent;
138 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
139 struct spi_reg *reg = plat->spireg;
140 unsigned int bytelen;
141 int ret;
142
143 bytelen = bitlen / 8;
144
145 if (dout && din)
146 debug("This is a duplex transfer.\n");
147
148
149 if (flags & SPI_XFER_BEGIN) {
150 debug("SPI: activate cs.\n");
151 spi_cs_activate(reg, spi_chip_select(dev));
152 }
153
154
155 if (dout || din) {
156 ret = spi_legacy_shift_byte(reg, bytelen, dout, din);
157 if (ret)
158 return ret;
159 }
160
161
162 if (flags & SPI_XFER_END) {
163 ret = wait_for_bit(__func__, ®->ctrl,
164 MVEBU_SPI_A3700_XFER_RDY, true, 100, false);
165 if (ret)
166 return ret;
167
168 debug("SPI: deactivate cs.\n");
169 spi_cs_deactivate(reg, spi_chip_select(dev));
170 }
171
172 return 0;
173}
174
175static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
176{
177 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
178 struct spi_reg *reg = plat->spireg;
179 u32 data;
180
181 data = readl(®->cfg);
182
183
184 data &= ~MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
185
186
187 if (hz > plat->frequency)
188 hz = plat->frequency;
189 data |= plat->clock / hz;
190
191 writel(data, ®->cfg);
192
193 return 0;
194}
195
196static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
197{
198 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
199 struct spi_reg *reg = plat->spireg;
200
201
202
203
204
205
206 if (mode & SPI_CPOL)
207 setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
208 else
209 clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
210 if (mode & SPI_CPHA)
211 setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
212 else
213 clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
214
215 return 0;
216}
217
218static int mvebu_spi_probe(struct udevice *bus)
219{
220 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
221 struct spi_reg *reg = plat->spireg;
222 u32 data;
223 int ret;
224
225
226
227
228
229
230
231
232 data = readl(®->cfg);
233 writel(data | MVEBU_SPI_A3700_FIFO_FLUSH, ®->cfg);
234 ret = wait_for_bit(__func__, ®->cfg, MVEBU_SPI_A3700_FIFO_FLUSH,
235 false, 1000, false);
236 if (ret)
237 return ret;
238
239
240 data &= ~MVEBU_SPI_A3700_FIFO_EN;
241
242
243 data &= ~MVEBU_SPI_A3700_BYTE_LEN;
244
245 writel(data, ®->cfg);
246
247 return 0;
248}
249
250static int mvebu_spi_ofdata_to_platdata(struct udevice *bus)
251{
252 struct mvebu_spi_platdata *plat = dev_get_platdata(bus);
253
254 plat->spireg = (struct spi_reg *)dev_get_addr(bus);
255
256
257
258
259
260
261
262
263
264 plat->clock = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
265 "clock-frequency", 160000);
266 plat->frequency = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
267 "spi-max-frequency", 40000);
268
269 return 0;
270}
271
272static const struct dm_spi_ops mvebu_spi_ops = {
273 .xfer = mvebu_spi_xfer,
274 .set_speed = mvebu_spi_set_speed,
275 .set_mode = mvebu_spi_set_mode,
276
277
278
279
280};
281
282static const struct udevice_id mvebu_spi_ids[] = {
283 { .compatible = "marvell,armada-3700-spi" },
284 { }
285};
286
287U_BOOT_DRIVER(mvebu_spi) = {
288 .name = "mvebu_spi",
289 .id = UCLASS_SPI,
290 .of_match = mvebu_spi_ids,
291 .ops = &mvebu_spi_ops,
292 .ofdata_to_platdata = mvebu_spi_ofdata_to_platdata,
293 .platdata_auto_alloc_size = sizeof(struct mvebu_spi_platdata),
294 .probe = mvebu_spi_probe,
295};
296