blob: 25b490bcf4ae5c77998f69bf9df848a2c494e213 [file] [log] [blame]
Chin-Ting Kuo4daa6bb2022-08-19 17:01:04 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ASPEED FMC/SPI Controller driver
4 *
5 * Copyright (c) 2022 ASPEED Corporation.
6 * Copyright (c) 2022 IBM Corporation.
7 *
8 * Author:
9 * Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>
10 * Cedric Le Goater <clg@kaod.org>
11 */
12
13#include <asm/io.h>
14#include <clk.h>
15#include <common.h>
16#include <dm.h>
17#include <dm/device_compat.h>
18#include <linux/bitops.h>
19#include <linux/bug.h>
20#include <linux/err.h>
21#include <linux/iopoll.h>
22#include <linux/kernel.h>
23#include <linux/mtd/spi-nor.h>
24#include <linux/sizes.h>
25#include <malloc.h>
26#include <spi.h>
27#include <spi-mem.h>
28
29#define ASPEED_SPI_MAX_CS 3
30
31#define CTRL_IO_SINGLE_DATA 0
32#define CTRL_IO_QUAD_DATA BIT(30)
33#define CTRL_IO_DUAL_DATA BIT(29)
34
35#define CTRL_IO_MODE_USER GENMASK(1, 0)
36#define CTRL_IO_MODE_CMD_READ BIT(0)
37#define CTRL_IO_MODE_CMD_WRITE BIT(1)
38#define CTRL_STOP_ACTIVE BIT(2)
39
40struct aspeed_spi_regs {
41 u32 conf; /* 0x00 CE Type Setting */
42 u32 ctrl; /* 0x04 CE Control */
43 u32 intr_ctrl; /* 0x08 Interrupt Control and Status */
44 u32 cmd_ctrl; /* 0x0c Command Control */
45 u32 ce_ctrl[ASPEED_SPI_MAX_CS]; /* 0x10 .. 0x18 CEx Control */
46 u32 _reserved0[5]; /* .. */
47 u32 segment_addr[ASPEED_SPI_MAX_CS]; /* 0x30 .. 0x38 Segment Address */
48 u32 _reserved1[5]; /* .. */
49 u32 soft_rst_cmd_ctrl; /* 0x50 Auto Soft-Reset Command Control */
50 u32 _reserved2[11]; /* .. */
51 u32 dma_ctrl; /* 0x80 DMA Control/Status */
52 u32 dma_flash_addr; /* 0x84 DMA Flash Side Address */
53 u32 dma_dram_addr; /* 0x88 DMA DRAM Side Address */
54 u32 dma_len; /* 0x8c DMA Length Register */
55 u32 dma_checksum; /* 0x90 Checksum Calculation Result */
56 u32 timings[ASPEED_SPI_MAX_CS]; /* 0x94 Read Timing Compensation */
57};
58
59struct aspeed_spi_plat {
60 u8 max_cs;
61 void __iomem *ahb_base; /* AHB address base for all flash devices. */
62 fdt_size_t ahb_sz; /* Overall AHB window size for all flash device. */
63};
64
65struct aspeed_spi_flash {
66 void __iomem *ahb_base;
67 u32 ahb_decoded_sz;
68 u32 ce_ctrl_user;
69 u32 ce_ctrl_read;
70};
71
72struct aspeed_spi_priv {
73 u32 num_cs;
74 struct aspeed_spi_regs *regs;
75 struct aspeed_spi_info *info;
76 struct aspeed_spi_flash flashes[ASPEED_SPI_MAX_CS];
77};
78
79struct aspeed_spi_info {
80 u32 io_mode_mask;
81 u32 max_bus_width;
82 u32 min_decoded_sz;
83 void (*set_4byte)(struct udevice *bus, u32 cs);
84 u32 (*segment_start)(struct udevice *bus, u32 reg);
85 u32 (*segment_end)(struct udevice *bus, u32 reg);
86 u32 (*segment_reg)(u32 start, u32 end);
87};
88
89static u32 aspeed_spi_get_io_mode(u32 bus_width)
90{
91 switch (bus_width) {
92 case 1:
93 return CTRL_IO_SINGLE_DATA;
94 case 2:
95 return CTRL_IO_DUAL_DATA;
96 case 4:
97 return CTRL_IO_QUAD_DATA;
98 default:
99 /* keep in default value */
100 return CTRL_IO_SINGLE_DATA;
101 }
102}
103
104static u32 ast2500_spi_segment_start(struct udevice *bus, u32 reg)
105{
106 struct aspeed_spi_plat *plat = dev_get_plat(bus);
107 u32 start_offset = ((reg >> 16) & 0xff) << 23;
108
109 if (start_offset == 0)
110 return (u32)plat->ahb_base;
111
112 return (u32)plat->ahb_base + start_offset;
113}
114
115static u32 ast2500_spi_segment_end(struct udevice *bus, u32 reg)
116{
117 struct aspeed_spi_plat *plat = dev_get_plat(bus);
118 u32 end_offset = ((reg >> 24) & 0xff) << 23;
119
120 /* Meaningless end_offset, set to physical ahb base. */
121 if (end_offset == 0)
122 return (u32)plat->ahb_base;
123
124 return (u32)plat->ahb_base + end_offset;
125}
126
127static u32 ast2500_spi_segment_reg(u32 start, u32 end)
128{
129 if (start == end)
130 return 0;
131
132 return ((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24);
133}
134
135static void ast2500_spi_chip_set_4byte(struct udevice *bus, u32 cs)
136{
137 struct aspeed_spi_priv *priv = dev_get_priv(bus);
138 u32 reg_val;
139
140 reg_val = readl(&priv->regs->ctrl);
141 reg_val |= 0x1 << cs;
142 writel(reg_val, &priv->regs->ctrl);
143}
144
145static u32 ast2600_spi_segment_start(struct udevice *bus, u32 reg)
146{
147 struct aspeed_spi_plat *plat = dev_get_plat(bus);
148 u32 start_offset = (reg << 16) & 0x0ff00000;
149
150 if (start_offset == 0)
151 return (u32)plat->ahb_base;
152
153 return (u32)plat->ahb_base + start_offset;
154}
155
156static u32 ast2600_spi_segment_end(struct udevice *bus, u32 reg)
157{
158 struct aspeed_spi_plat *plat = dev_get_plat(bus);
159 u32 end_offset = reg & 0x0ff00000;
160
161 /* Meaningless end_offset, set to physical ahb base. */
162 if (end_offset == 0)
163 return (u32)plat->ahb_base;
164
165 return (u32)plat->ahb_base + end_offset + 0x100000;
166}
167
168static u32 ast2600_spi_segment_reg(u32 start, u32 end)
169{
170 if (start == end)
171 return 0;
172
173 return ((start & 0x0ff00000) >> 16) | ((end - 0x100000) & 0x0ff00000);
174}
175
176static void ast2600_spi_chip_set_4byte(struct udevice *bus, u32 cs)
177{
178 struct aspeed_spi_priv *priv = dev_get_priv(bus);
179 u32 reg_val;
180
181 reg_val = readl(&priv->regs->ctrl);
182 reg_val |= 0x11 << cs;
183 writel(reg_val, &priv->regs->ctrl);
184}
185
186static int aspeed_spi_read_from_ahb(void __iomem *ahb_base, void *buf,
187 size_t len)
188{
189 size_t offset = 0;
190
191 if (IS_ALIGNED((uintptr_t)ahb_base, sizeof(uintptr_t)) &&
192 IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
193 readsl(ahb_base, buf, len >> 2);
194 offset = len & ~0x3;
195 len -= offset;
196 }
197
198 readsb(ahb_base, (u8 *)buf + offset, len);
199
200 return 0;
201}
202
203static int aspeed_spi_write_to_ahb(void __iomem *ahb_base, const void *buf,
204 size_t len)
205{
206 size_t offset = 0;
207
208 if (IS_ALIGNED((uintptr_t)ahb_base, sizeof(uintptr_t)) &&
209 IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
210 writesl(ahb_base, buf, len >> 2);
211 offset = len & ~0x3;
212 len -= offset;
213 }
214
215 writesb(ahb_base, (u8 *)buf + offset, len);
216
217 return 0;
218}
219
220/*
221 * Currently, only support 1-1-1, 1-1-2 or 1-1-4
222 * SPI NOR flash operation format.
223 */
224static bool aspeed_spi_supports_op(struct spi_slave *slave,
225 const struct spi_mem_op *op)
226{
227 struct udevice *bus = slave->dev->parent;
228 struct aspeed_spi_priv *priv = dev_get_priv(bus);
229
230 if (op->cmd.buswidth > 1)
231 return false;
232
233 if (op->addr.nbytes != 0) {
234 if (op->addr.buswidth > 1)
235 return false;
236 if (op->addr.nbytes < 3 || op->addr.nbytes > 4)
237 return false;
238 }
239
240 if (op->dummy.nbytes != 0) {
241 if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7)
242 return false;
243 }
244
245 if (op->data.nbytes != 0 &&
246 op->data.buswidth > priv->info->max_bus_width)
247 return false;
248
249 if (!spi_mem_default_supports_op(slave, op))
250 return false;
251
252 return true;
253}
254
255static int aspeed_spi_exec_op_user_mode(struct spi_slave *slave,
256 const struct spi_mem_op *op)
257{
258 struct udevice *dev = slave->dev;
259 struct udevice *bus = dev->parent;
260 struct aspeed_spi_priv *priv = dev_get_priv(bus);
261 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(slave->dev);
262 u32 cs = slave_plat->cs;
263 u32 ce_ctrl_reg = (u32)&priv->regs->ce_ctrl[cs];
264 u32 ce_ctrl_val;
265 struct aspeed_spi_flash *flash = &priv->flashes[cs];
266 u8 dummy_data[16] = {0};
267 u8 addr[4] = {0};
268 int i;
269
270 dev_dbg(dev, "cmd:%x(%d),addr:%llx(%d),dummy:%d(%d),data_len:0x%x(%d)\n",
271 op->cmd.opcode, op->cmd.buswidth, op->addr.val,
272 op->addr.buswidth, op->dummy.nbytes, op->dummy.buswidth,
273 op->data.nbytes, op->data.buswidth);
274
275 /*
276 * Set controller to 4-byte address mode
277 * if flash is in 4-byte address mode.
278 */
279 if (op->cmd.opcode == SPINOR_OP_EN4B)
280 priv->info->set_4byte(bus, cs);
281
282 /* Start user mode */
283 ce_ctrl_val = flash->ce_ctrl_user;
284 writel(ce_ctrl_val, ce_ctrl_reg);
285 ce_ctrl_val &= (~CTRL_STOP_ACTIVE);
286 writel(ce_ctrl_val, ce_ctrl_reg);
287
288 /* Send command */
289 aspeed_spi_write_to_ahb(flash->ahb_base, &op->cmd.opcode, 1);
290
291 /* Send address */
292 for (i = op->addr.nbytes; i > 0; i--) {
293 addr[op->addr.nbytes - i] =
294 ((u32)op->addr.val >> ((i - 1) * 8)) & 0xff;
295 }
296
297 /* Change io_mode */
298 ce_ctrl_val &= ~priv->info->io_mode_mask;
299 ce_ctrl_val |= aspeed_spi_get_io_mode(op->addr.buswidth);
300 writel(ce_ctrl_val, ce_ctrl_reg);
301 aspeed_spi_write_to_ahb(flash->ahb_base, addr, op->addr.nbytes);
302
303 /* Send dummy cycles */
304 aspeed_spi_write_to_ahb(flash->ahb_base, dummy_data, op->dummy.nbytes);
305
306 /* Change io_mode */
307 ce_ctrl_val &= ~priv->info->io_mode_mask;
308 ce_ctrl_val |= aspeed_spi_get_io_mode(op->data.buswidth);
309 writel(ce_ctrl_val, ce_ctrl_reg);
310
311 /* Send data */
312 if (op->data.dir == SPI_MEM_DATA_OUT) {
313 aspeed_spi_write_to_ahb(flash->ahb_base, op->data.buf.out,
314 op->data.nbytes);
315 } else {
316 aspeed_spi_read_from_ahb(flash->ahb_base, op->data.buf.in,
317 op->data.nbytes);
318 }
319
320 ce_ctrl_val |= CTRL_STOP_ACTIVE;
321 writel(ce_ctrl_val, ce_ctrl_reg);
322
323 /* Restore controller setting. */
324 writel(flash->ce_ctrl_read, ce_ctrl_reg);
325
326 return 0;
327}
328
329static struct aspeed_spi_flash *aspeed_spi_get_flash(struct udevice *dev)
330{
331 struct udevice *bus = dev->parent;
332 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
333 struct aspeed_spi_plat *plat = dev_get_plat(bus);
334 struct aspeed_spi_priv *priv = dev_get_priv(bus);
335 u32 cs = slave_plat->cs;
336
337 if (cs >= plat->max_cs) {
338 dev_err(dev, "invalid CS %u\n", cs);
339 return NULL;
340 }
341
342 return &priv->flashes[cs];
343}
344
345static void aspeed_spi_decoded_base_calculate(struct udevice *bus)
346{
347 struct aspeed_spi_plat *plat = dev_get_plat(bus);
348 struct aspeed_spi_priv *priv = dev_get_priv(bus);
349 u32 cs;
350
351 priv->flashes[0].ahb_base = plat->ahb_base;
352
353 for (cs = 1; cs < plat->max_cs; cs++) {
354 priv->flashes[cs].ahb_base =
355 priv->flashes[cs - 1].ahb_base +
356 priv->flashes[cs - 1].ahb_decoded_sz;
357 }
358}
359
360static void aspeed_spi_decoded_range_set(struct udevice *bus)
361{
362 struct aspeed_spi_plat *plat = dev_get_plat(bus);
363 struct aspeed_spi_priv *priv = dev_get_priv(bus);
364 u32 decoded_reg_val;
365 u32 start_addr, end_addr;
366 u32 cs;
367
368 for (cs = 0; cs < plat->max_cs; cs++) {
369 start_addr = (u32)priv->flashes[cs].ahb_base;
370 end_addr = (u32)priv->flashes[cs].ahb_base +
371 priv->flashes[cs].ahb_decoded_sz;
372
373 decoded_reg_val = priv->info->segment_reg(start_addr, end_addr);
374
375 writel(decoded_reg_val, &priv->regs->segment_addr[cs]);
376
377 dev_dbg(bus, "cs: %d, decoded_reg: 0x%x, start: 0x%x, end: 0x%x\n",
378 cs, decoded_reg_val, start_addr, end_addr);
379 }
380}
381
382static int aspeed_spi_decoded_range_config(struct udevice *bus)
383{
384 aspeed_spi_decoded_base_calculate(bus);
385 aspeed_spi_decoded_range_set(bus);
386
387 return 0;
388}
389
390/*
391 * Initialize SPI controller for each chip select.
392 * Here, only the minimum decode range is configured
393 * in order to get device (SPI NOR flash) information
394 * at the early stage.
395 */
396static int aspeed_spi_ctrl_init(struct udevice *bus)
397{
398 int ret;
399 struct aspeed_spi_plat *plat = dev_get_plat(bus);
400 struct aspeed_spi_priv *priv = dev_get_priv(bus);
401 u32 cs;
402 u32 reg_val;
403 u32 decoded_sz;
404
405 /* Enable write capability for all CS. */
406 reg_val = readl(&priv->regs->conf);
407 writel(reg_val | (GENMASK(plat->max_cs - 1, 0) << 16),
408 &priv->regs->conf);
409
410 memset(priv->flashes, 0x0,
411 sizeof(struct aspeed_spi_flash) * ASPEED_SPI_MAX_CS);
412
413 /* Initial user mode. */
414 for (cs = 0; cs < priv->num_cs; cs++) {
415 priv->flashes[cs].ce_ctrl_user =
416 (CTRL_STOP_ACTIVE | CTRL_IO_MODE_USER);
417 }
418
419 /* Assign basic AHB decoded size for each CS. */
420 for (cs = 0; cs < plat->max_cs; cs++) {
421 reg_val = readl(&priv->regs->segment_addr[cs]);
422 decoded_sz = priv->info->segment_end(bus, reg_val) -
423 priv->info->segment_start(bus, reg_val);
424
425 if (decoded_sz < priv->info->min_decoded_sz)
426 decoded_sz = priv->info->min_decoded_sz;
427
428 priv->flashes[cs].ahb_decoded_sz = decoded_sz;
429 }
430
431 ret = aspeed_spi_decoded_range_config(bus);
432
433 return ret;
434}
435
436static const struct aspeed_spi_info ast2500_fmc_info = {
437 .io_mode_mask = 0x70000000,
438 .max_bus_width = 2,
439 .min_decoded_sz = 0x800000,
440 .set_4byte = ast2500_spi_chip_set_4byte,
441 .segment_start = ast2500_spi_segment_start,
442 .segment_end = ast2500_spi_segment_end,
443 .segment_reg = ast2500_spi_segment_reg,
444};
445
446/*
447 * There are some different between FMC and SPI controllers.
448 * For example, DMA operation, but this isn't implemented currently.
449 */
450static const struct aspeed_spi_info ast2500_spi_info = {
451 .io_mode_mask = 0x70000000,
452 .max_bus_width = 2,
453 .min_decoded_sz = 0x800000,
454 .set_4byte = ast2500_spi_chip_set_4byte,
455 .segment_start = ast2500_spi_segment_start,
456 .segment_end = ast2500_spi_segment_end,
457 .segment_reg = ast2500_spi_segment_reg,
458};
459
460static const struct aspeed_spi_info ast2600_fmc_info = {
461 .io_mode_mask = 0xf0000000,
462 .max_bus_width = 4,
463 .min_decoded_sz = 0x200000,
464 .set_4byte = ast2600_spi_chip_set_4byte,
465 .segment_start = ast2600_spi_segment_start,
466 .segment_end = ast2600_spi_segment_end,
467 .segment_reg = ast2600_spi_segment_reg,
468};
469
470static const struct aspeed_spi_info ast2600_spi_info = {
471 .io_mode_mask = 0xf0000000,
472 .max_bus_width = 4,
473 .min_decoded_sz = 0x200000,
474 .set_4byte = ast2600_spi_chip_set_4byte,
475 .segment_start = ast2600_spi_segment_start,
476 .segment_end = ast2600_spi_segment_end,
477 .segment_reg = ast2600_spi_segment_reg,
478};
479
480static int aspeed_spi_claim_bus(struct udevice *dev)
481{
482 struct udevice *bus = dev->parent;
483 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
484
485 dev_dbg(bus, "%s: claim bus CS%u\n", bus->name, slave_plat->cs);
486
487 return 0;
488}
489
490static int aspeed_spi_release_bus(struct udevice *dev)
491{
492 struct udevice *bus = dev->parent;
493 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
494
495 dev_dbg(bus, "%s: release bus CS%u\n", bus->name, slave_plat->cs);
496
497 if (!aspeed_spi_get_flash(dev))
498 return -ENODEV;
499
500 return 0;
501}
502
503static int aspeed_spi_set_mode(struct udevice *bus, uint mode)
504{
505 dev_dbg(bus, "%s: setting mode to %x\n", bus->name, mode);
506
507 return 0;
508}
509
510static int aspeed_spi_set_speed(struct udevice *bus, uint hz)
511{
512 dev_dbg(bus, "%s: setting speed to %u\n", bus->name, hz);
513 /*
514 * ASPEED SPI controller supports multiple CS with different
515 * clock frequency. We cannot distinguish which CS here.
516 * Thus, the related implementation is postponed to claim_bus.
517 */
518
519 return 0;
520}
521
522static int apseed_spi_of_to_plat(struct udevice *bus)
523{
524 struct aspeed_spi_plat *plat = dev_get_plat(bus);
525 struct aspeed_spi_priv *priv = dev_get_priv(bus);
526
527 priv->regs = (void __iomem *)devfdt_get_addr_index(bus, 0);
528 if ((u32)priv->regs == FDT_ADDR_T_NONE) {
529 dev_err(bus, "wrong ctrl base\n");
530 return -ENODEV;
531 }
532
533 plat->ahb_base =
534 (void __iomem *)devfdt_get_addr_size_index(bus, 1, &plat->ahb_sz);
535 if ((u32)plat->ahb_base == FDT_ADDR_T_NONE) {
536 dev_err(bus, "wrong AHB base\n");
537 return -ENODEV;
538 }
539
540 plat->max_cs = dev_read_u32_default(bus, "num-cs", ASPEED_SPI_MAX_CS);
541 if (plat->max_cs > ASPEED_SPI_MAX_CS)
542 return -EINVAL;
543
544 dev_dbg(bus, "ctrl_base = 0x%x, ahb_base = 0x%p, size = 0x%lx\n",
545 (u32)priv->regs, plat->ahb_base, plat->ahb_sz);
546 dev_dbg(bus, "max_cs = %d\n", plat->max_cs);
547
548 return 0;
549}
550
551static int aspeed_spi_probe(struct udevice *bus)
552{
553 int ret;
554 struct aspeed_spi_priv *priv = dev_get_priv(bus);
555 struct udevice *dev;
556
557 priv->info = (struct aspeed_spi_info *)dev_get_driver_data(bus);
558
559 priv->num_cs = 0;
560 for (device_find_first_child(bus, &dev); dev;
561 device_find_next_child(&dev)) {
562 priv->num_cs++;
563 }
564
565 if (priv->num_cs > ASPEED_SPI_MAX_CS)
566 return -EINVAL;
567
568 ret = aspeed_spi_ctrl_init(bus);
569
570 return ret;
571}
572
573static const struct spi_controller_mem_ops aspeed_spi_mem_ops = {
574 .supports_op = aspeed_spi_supports_op,
575 .exec_op = aspeed_spi_exec_op_user_mode,
576};
577
578static const struct dm_spi_ops aspeed_spi_ops = {
579 .claim_bus = aspeed_spi_claim_bus,
580 .release_bus = aspeed_spi_release_bus,
581 .set_speed = aspeed_spi_set_speed,
582 .set_mode = aspeed_spi_set_mode,
583 .mem_ops = &aspeed_spi_mem_ops,
584};
585
586static const struct udevice_id aspeed_spi_ids[] = {
587 { .compatible = "aspeed,ast2500-fmc", .data = (ulong)&ast2500_fmc_info, },
588 { .compatible = "aspeed,ast2500-spi", .data = (ulong)&ast2500_spi_info, },
589 { .compatible = "aspeed,ast2600-fmc", .data = (ulong)&ast2600_fmc_info, },
590 { .compatible = "aspeed,ast2600-spi", .data = (ulong)&ast2600_spi_info, },
591 { }
592};
593
594U_BOOT_DRIVER(aspeed_spi) = {
595 .name = "aspeed_spi_smc",
596 .id = UCLASS_SPI,
597 .of_match = aspeed_spi_ids,
598 .ops = &aspeed_spi_ops,
599 .of_to_plat = apseed_spi_of_to_plat,
600 .plat_auto = sizeof(struct aspeed_spi_plat),
601 .priv_auto = sizeof(struct aspeed_spi_priv),
602 .probe = aspeed_spi_probe,
603};