blob: 943bf6986f15f05ff1da8795d190327dd3a0e74e [file] [log] [blame]
Igor Prusov3f027752023-10-25 01:51:39 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for Amlogic A1 SPI flash controller (SPIFC)
4 *
5 * Copyright (c) 2023, SberDevices. All Rights Reserved.
6 *
7 * Author: Martin Kurbanov <mmkurbanov@sberdevices.ru>
8 *
9 * Ported to u-boot:
10 * Author: Igor Prusov <ivprusov@sberdevices.ru>
11 */
12
13#include <clk.h>
14#include <dm.h>
15#include <spi.h>
16#include <spi-mem.h>
17#include <asm/io.h>
18#include <linux/log2.h>
Igor Prusovd1076172023-11-09 20:10:03 +030019#include <linux/time.h>
Igor Prusov3f027752023-10-25 01:51:39 +030020#include <linux/iopoll.h>
21#include <linux/bitfield.h>
22
23#define SPIFC_A1_AHB_CTRL_REG 0x0
24#define SPIFC_A1_AHB_BUS_EN BIT(31)
25
26#define SPIFC_A1_USER_CTRL0_REG 0x200
27#define SPIFC_A1_USER_REQUEST_ENABLE BIT(31)
28#define SPIFC_A1_USER_REQUEST_FINISH BIT(30)
29#define SPIFC_A1_USER_DATA_UPDATED BIT(0)
30
31#define SPIFC_A1_USER_CTRL1_REG 0x204
32#define SPIFC_A1_USER_CMD_ENABLE BIT(30)
33#define SPIFC_A1_USER_CMD_MODE GENMASK(29, 28)
34#define SPIFC_A1_USER_CMD_CODE GENMASK(27, 20)
35#define SPIFC_A1_USER_ADDR_ENABLE BIT(19)
36#define SPIFC_A1_USER_ADDR_MODE GENMASK(18, 17)
37#define SPIFC_A1_USER_ADDR_BYTES GENMASK(16, 15)
38#define SPIFC_A1_USER_DOUT_ENABLE BIT(14)
39#define SPIFC_A1_USER_DOUT_MODE GENMASK(11, 10)
40#define SPIFC_A1_USER_DOUT_BYTES GENMASK(9, 0)
41
42#define SPIFC_A1_USER_CTRL2_REG 0x208
43#define SPIFC_A1_USER_DUMMY_ENABLE BIT(31)
44#define SPIFC_A1_USER_DUMMY_MODE GENMASK(30, 29)
45#define SPIFC_A1_USER_DUMMY_CLK_SYCLES GENMASK(28, 23)
46
47#define SPIFC_A1_USER_CTRL3_REG 0x20c
48#define SPIFC_A1_USER_DIN_ENABLE BIT(31)
49#define SPIFC_A1_USER_DIN_MODE GENMASK(28, 27)
50#define SPIFC_A1_USER_DIN_BYTES GENMASK(25, 16)
51
52#define SPIFC_A1_USER_ADDR_REG 0x210
53
54#define SPIFC_A1_AHB_REQ_CTRL_REG 0x214
55#define SPIFC_A1_AHB_REQ_ENABLE BIT(31)
56
57#define SPIFC_A1_ACTIMING0_REG (0x0088 << 2)
58#define SPIFC_A1_TSLCH GENMASK(31, 30)
59#define SPIFC_A1_TCLSH GENMASK(29, 28)
60#define SPIFC_A1_TSHWL GENMASK(20, 16)
61#define SPIFC_A1_TSHSL2 GENMASK(15, 12)
62#define SPIFC_A1_TSHSL1 GENMASK(11, 8)
63#define SPIFC_A1_TWHSL GENMASK(7, 0)
64
65#define SPIFC_A1_DBUF_CTRL_REG 0x240
66#define SPIFC_A1_DBUF_DIR BIT(31)
67#define SPIFC_A1_DBUF_AUTO_UPDATE_ADDR BIT(30)
68#define SPIFC_A1_DBUF_ADDR GENMASK(7, 0)
69
70#define SPIFC_A1_DBUF_DATA_REG 0x244
71
72#define SPIFC_A1_USER_DBUF_ADDR_REG 0x248
73
74#define SPIFC_A1_BUFFER_SIZE 512U
75
76#define SPIFC_A1_MAX_HZ 200000000
77#define SPIFC_A1_MIN_HZ 1000000
78
79#define SPIFC_A1_USER_CMD(op) ( \
80 SPIFC_A1_USER_CMD_ENABLE | \
81 FIELD_PREP(SPIFC_A1_USER_CMD_CODE, (op)->cmd.opcode) | \
82 FIELD_PREP(SPIFC_A1_USER_CMD_MODE, ilog2((op)->cmd.buswidth)))
83
84#define SPIFC_A1_USER_ADDR(op) ( \
85 SPIFC_A1_USER_ADDR_ENABLE | \
86 FIELD_PREP(SPIFC_A1_USER_ADDR_MODE, ilog2((op)->addr.buswidth)) | \
87 FIELD_PREP(SPIFC_A1_USER_ADDR_BYTES, (op)->addr.nbytes - 1))
88
89#define SPIFC_A1_USER_DUMMY(op) ( \
90 SPIFC_A1_USER_DUMMY_ENABLE | \
91 FIELD_PREP(SPIFC_A1_USER_DUMMY_MODE, ilog2((op)->dummy.buswidth)) | \
92 FIELD_PREP(SPIFC_A1_USER_DUMMY_CLK_SYCLES, (op)->dummy.nbytes << 3))
93
94#define SPIFC_A1_TSLCH_VAL FIELD_PREP(SPIFC_A1_TSLCH, 1)
95#define SPIFC_A1_TCLSH_VAL FIELD_PREP(SPIFC_A1_TCLSH, 1)
96#define SPIFC_A1_TSHWL_VAL FIELD_PREP(SPIFC_A1_TSHWL, 7)
97#define SPIFC_A1_TSHSL2_VAL FIELD_PREP(SPIFC_A1_TSHSL2, 7)
98#define SPIFC_A1_TSHSL1_VAL FIELD_PREP(SPIFC_A1_TSHSL1, 7)
99#define SPIFC_A1_TWHSL_VAL FIELD_PREP(SPIFC_A1_TWHSL, 2)
100#define SPIFC_A1_ACTIMING0_VAL (SPIFC_A1_TSLCH_VAL | SPIFC_A1_TCLSH_VAL | \
101 SPIFC_A1_TSHWL_VAL | SPIFC_A1_TSHSL2_VAL | \
102 SPIFC_A1_TSHSL1_VAL | SPIFC_A1_TWHSL_VAL)
103
104struct amlogic_spifc_a1 {
105 struct clk clk;
106 void __iomem *base;
107 u32 curr_speed_hz;
108};
109
110static int amlogic_spifc_a1_request(struct amlogic_spifc_a1 *spifc, bool read)
111{
112 u32 mask = SPIFC_A1_USER_REQUEST_FINISH |
113 (read ? SPIFC_A1_USER_DATA_UPDATED : 0);
114 u32 val;
115
116 writel(SPIFC_A1_USER_REQUEST_ENABLE,
117 spifc->base + SPIFC_A1_USER_CTRL0_REG);
118
119 return readl_poll_timeout(spifc->base + SPIFC_A1_USER_CTRL0_REG,
120 val, (val & mask) == mask,
Igor Prusovd1076172023-11-09 20:10:03 +0300121 200 * USEC_PER_MSEC);
Igor Prusov3f027752023-10-25 01:51:39 +0300122}
123
124static void amlogic_spifc_a1_drain_buffer(struct amlogic_spifc_a1 *spifc,
125 char *buf, u32 len)
126{
127 u32 data;
128 const u32 count = len / sizeof(data);
129 const u32 pad = len % sizeof(data);
130
131 writel(SPIFC_A1_DBUF_AUTO_UPDATE_ADDR,
132 spifc->base + SPIFC_A1_DBUF_CTRL_REG);
Igor Prusov4d3a6952023-11-14 14:02:55 +0300133 ioread32_rep(spifc->base + SPIFC_A1_DBUF_DATA_REG, buf, count);
Igor Prusov3f027752023-10-25 01:51:39 +0300134
135 if (pad) {
136 data = readl(spifc->base + SPIFC_A1_DBUF_DATA_REG);
137 memcpy(buf + len - pad, &data, pad);
138 }
139}
140
141static void amlogic_spifc_a1_fill_buffer(struct amlogic_spifc_a1 *spifc,
142 const char *buf, u32 len)
143{
144 u32 data;
145 const u32 count = len / sizeof(data);
146 const u32 pad = len % sizeof(data);
147
148 writel(SPIFC_A1_DBUF_DIR | SPIFC_A1_DBUF_AUTO_UPDATE_ADDR,
149 spifc->base + SPIFC_A1_DBUF_CTRL_REG);
Igor Prusov4d3a6952023-11-14 14:02:55 +0300150 iowrite32_rep(spifc->base + SPIFC_A1_DBUF_DATA_REG, buf, count);
Igor Prusov3f027752023-10-25 01:51:39 +0300151
152 if (pad) {
153 memcpy(&data, buf + len - pad, pad);
154 writel(data, spifc->base + SPIFC_A1_DBUF_DATA_REG);
155 }
156}
157
158static void amlogic_spifc_a1_user_init(struct amlogic_spifc_a1 *spifc)
159{
160 writel(0, spifc->base + SPIFC_A1_USER_CTRL0_REG);
161 writel(0, spifc->base + SPIFC_A1_USER_CTRL1_REG);
162 writel(0, spifc->base + SPIFC_A1_USER_CTRL2_REG);
163 writel(0, spifc->base + SPIFC_A1_USER_CTRL3_REG);
164}
165
166static void amlogic_spifc_a1_set_cmd(struct amlogic_spifc_a1 *spifc,
167 u32 cmd_cfg)
168{
169 u32 val;
170
171 val = readl(spifc->base + SPIFC_A1_USER_CTRL1_REG);
172 val &= ~(SPIFC_A1_USER_CMD_MODE | SPIFC_A1_USER_CMD_CODE);
173 val |= cmd_cfg;
174 writel(val, spifc->base + SPIFC_A1_USER_CTRL1_REG);
175}
176
177static void amlogic_spifc_a1_set_addr(struct amlogic_spifc_a1 *spifc, u32 addr,
178 u32 addr_cfg)
179{
180 u32 val;
181
182 writel(addr, spifc->base + SPIFC_A1_USER_ADDR_REG);
183
184 val = readl(spifc->base + SPIFC_A1_USER_CTRL1_REG);
185 val &= ~(SPIFC_A1_USER_ADDR_MODE | SPIFC_A1_USER_ADDR_BYTES);
186 val |= addr_cfg;
187 writel(val, spifc->base + SPIFC_A1_USER_CTRL1_REG);
188}
189
190static void amlogic_spifc_a1_set_dummy(struct amlogic_spifc_a1 *spifc,
191 u32 dummy_cfg)
192{
193 u32 val = readl(spifc->base + SPIFC_A1_USER_CTRL2_REG);
194
195 val &= ~(SPIFC_A1_USER_DUMMY_MODE | SPIFC_A1_USER_DUMMY_CLK_SYCLES);
196 val |= dummy_cfg;
197 writel(val, spifc->base + SPIFC_A1_USER_CTRL2_REG);
198}
199
200static int amlogic_spifc_a1_read(struct amlogic_spifc_a1 *spifc, void *buf,
201 u32 size, u32 mode)
202{
203 u32 val = readl(spifc->base + SPIFC_A1_USER_CTRL3_REG);
204 int ret;
205
206 val &= ~(SPIFC_A1_USER_DIN_MODE | SPIFC_A1_USER_DIN_BYTES);
207 val |= SPIFC_A1_USER_DIN_ENABLE;
208 val |= FIELD_PREP(SPIFC_A1_USER_DIN_MODE, mode);
209 val |= FIELD_PREP(SPIFC_A1_USER_DIN_BYTES, size);
210 writel(val, spifc->base + SPIFC_A1_USER_CTRL3_REG);
211
212 ret = amlogic_spifc_a1_request(spifc, true);
213 if (!ret)
214 amlogic_spifc_a1_drain_buffer(spifc, buf, size);
215
216 return ret;
217}
218
219static int amlogic_spifc_a1_write(struct amlogic_spifc_a1 *spifc,
220 const void *buf, u32 size, u32 mode)
221{
222 u32 val;
223
224 amlogic_spifc_a1_fill_buffer(spifc, buf, size);
225
226 val = readl(spifc->base + SPIFC_A1_USER_CTRL1_REG);
227 val &= ~(SPIFC_A1_USER_DOUT_MODE | SPIFC_A1_USER_DOUT_BYTES);
228 val |= FIELD_PREP(SPIFC_A1_USER_DOUT_MODE, mode);
229 val |= FIELD_PREP(SPIFC_A1_USER_DOUT_BYTES, size);
230 val |= SPIFC_A1_USER_DOUT_ENABLE;
231 writel(val, spifc->base + SPIFC_A1_USER_CTRL1_REG);
232
233 return amlogic_spifc_a1_request(spifc, false);
234}
235
236static int amlogic_spifc_a1_set_freq(struct amlogic_spifc_a1 *spifc, u32 freq)
237{
238 int ret;
239
240 if (freq == spifc->curr_speed_hz)
241 return 0;
242
243 ret = clk_set_rate(&spifc->clk, freq);
244 if (ret)
245 return ret;
246
247 spifc->curr_speed_hz = freq;
248 return 0;
249}
250
251static int amlogic_spifc_a1_exec_op(struct spi_slave *slave,
252 const struct spi_mem_op *op)
253{
254 struct amlogic_spifc_a1 *spifc = dev_get_priv(slave->dev->parent);
255 size_t data_size = op->data.nbytes;
256 int ret;
257
258 ret = amlogic_spifc_a1_set_freq(spifc, slave->max_hz);
259 if (ret)
260 return ret;
261
262 amlogic_spifc_a1_user_init(spifc);
263 amlogic_spifc_a1_set_cmd(spifc, SPIFC_A1_USER_CMD(op));
264
265 if (op->addr.nbytes)
266 amlogic_spifc_a1_set_addr(spifc, op->addr.val,
267 SPIFC_A1_USER_ADDR(op));
268
269 if (op->dummy.nbytes)
270 amlogic_spifc_a1_set_dummy(spifc, SPIFC_A1_USER_DUMMY(op));
271
272 if (data_size) {
273 u32 mode = ilog2(op->data.buswidth);
274
275 writel(0, spifc->base + SPIFC_A1_USER_DBUF_ADDR_REG);
276
277 if (op->data.dir == SPI_MEM_DATA_IN)
278 ret = amlogic_spifc_a1_read(spifc, op->data.buf.in,
279 data_size, mode);
280 else
281 ret = amlogic_spifc_a1_write(spifc, op->data.buf.out,
282 data_size, mode);
283 } else {
284 ret = amlogic_spifc_a1_request(spifc, false);
285 }
286
287 return ret;
288}
289
290static int amlogic_spifc_a1_adjust_op_size(struct spi_slave *slave,
291 struct spi_mem_op *op)
292{
293 op->data.nbytes = min(op->data.nbytes, SPIFC_A1_BUFFER_SIZE);
294 return 0;
295}
296
297static void amlogic_spifc_a1_hw_init(struct amlogic_spifc_a1 *spifc)
298{
299 u32 regv;
300
301 regv = readl(spifc->base + SPIFC_A1_AHB_REQ_CTRL_REG);
302 regv &= ~(SPIFC_A1_AHB_REQ_ENABLE);
303 writel(regv, spifc->base + SPIFC_A1_AHB_REQ_CTRL_REG);
304
305 regv = readl(spifc->base + SPIFC_A1_AHB_CTRL_REG);
306 regv &= ~(SPIFC_A1_AHB_BUS_EN);
307 writel(regv, spifc->base + SPIFC_A1_AHB_CTRL_REG);
308
309 writel(SPIFC_A1_ACTIMING0_VAL, spifc->base + SPIFC_A1_ACTIMING0_REG);
310
311 writel(0, spifc->base + SPIFC_A1_USER_DBUF_ADDR_REG);
312}
313
314static const struct spi_controller_mem_ops amlogic_spifc_a1_mem_ops = {
315 .exec_op = amlogic_spifc_a1_exec_op,
316 .adjust_op_size = amlogic_spifc_a1_adjust_op_size,
317};
318
319static int amlogic_spifc_a1_probe(struct udevice *dev)
320{
321 struct amlogic_spifc_a1 *spifc = dev_get_priv(dev);
322 int ret;
323 struct udevice *bus = dev;
324
325 spifc->base = dev_read_addr_ptr(dev);
326 if (!spifc->base)
327 return -EINVAL;
328
329 ret = clk_get_by_index(bus, 0, &spifc->clk);
330 if (ret) {
331 pr_err("can't get clk spifc_gate!\n");
332 return ret;
333 }
334
335 ret = clk_enable(&spifc->clk);
336 if (ret) {
337 pr_err("enable clk fail\n");
338 return ret;
339 }
340
341 amlogic_spifc_a1_hw_init(spifc);
342
343 return 0;
344}
345
Igor Prusov3f027752023-10-25 01:51:39 +0300346static const struct udevice_id meson_spifc_ids[] = {
347 { .compatible = "amlogic,a1-spifc", },
348 { }
349};
350
351int amlogic_spifc_a1_set_speed(struct udevice *bus, uint hz)
352{
353 return 0;
354}
355
356int amlogic_spifc_a1_set_mode(struct udevice *bus, uint mode)
357{
358 return 0;
359}
360
361static const struct dm_spi_ops amlogic_spifc_a1_ops = {
362 .mem_ops = &amlogic_spifc_a1_mem_ops,
363 .set_speed = amlogic_spifc_a1_set_speed,
364 .set_mode = amlogic_spifc_a1_set_mode,
365};
366
367U_BOOT_DRIVER(meson_spifc_a1) = {
368 .name = "meson_spifc_a1",
369 .id = UCLASS_SPI,
370 .of_match = meson_spifc_ids,
371 .ops = &amlogic_spifc_a1_ops,
372 .probe = amlogic_spifc_a1_probe,
Igor Prusov3f027752023-10-25 01:51:39 +0300373 .priv_auto = sizeof(struct amlogic_spifc_a1),
374};