1 // SPDX-License-Identifier: GPL-2.0+
3 * ASPEED FMC/SPI Controller driver
5 * Copyright (c) 2022 ASPEED Corporation.
6 * Copyright (c) 2022 IBM Corporation.
9 * Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>
10 * Cedric Le Goater <clg@kaod.org>
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>
29 #define ASPEED_SPI_MAX_CS 3
31 #define CTRL_IO_SINGLE_DATA 0
32 #define CTRL_IO_QUAD_DATA BIT(30)
33 #define CTRL_IO_DUAL_DATA BIT(29)
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)
40 struct 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 */
59 struct aspeed_spi_plat {
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. */
65 struct aspeed_spi_flash {
66 void __iomem *ahb_base;
72 struct aspeed_spi_priv {
74 struct aspeed_spi_regs *regs;
75 struct aspeed_spi_info *info;
76 struct aspeed_spi_flash flashes[ASPEED_SPI_MAX_CS];
79 struct aspeed_spi_info {
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);
89 static u32 aspeed_spi_get_io_mode(u32 bus_width)
93 return CTRL_IO_SINGLE_DATA;
95 return CTRL_IO_DUAL_DATA;
97 return CTRL_IO_QUAD_DATA;
99 /* keep in default value */
100 return CTRL_IO_SINGLE_DATA;
104 static u32 ast2500_spi_segment_start(struct udevice *bus, u32 reg)
106 struct aspeed_spi_plat *plat = dev_get_plat(bus);
107 u32 start_offset = ((reg >> 16) & 0xff) << 23;
109 if (start_offset == 0)
110 return (u32)plat->ahb_base;
112 return (u32)plat->ahb_base + start_offset;
115 static u32 ast2500_spi_segment_end(struct udevice *bus, u32 reg)
117 struct aspeed_spi_plat *plat = dev_get_plat(bus);
118 u32 end_offset = ((reg >> 24) & 0xff) << 23;
120 /* Meaningless end_offset, set to physical ahb base. */
122 return (u32)plat->ahb_base;
124 return (u32)plat->ahb_base + end_offset;
127 static u32 ast2500_spi_segment_reg(u32 start, u32 end)
132 return ((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24);
135 static void ast2500_spi_chip_set_4byte(struct udevice *bus, u32 cs)
137 struct aspeed_spi_priv *priv = dev_get_priv(bus);
140 reg_val = readl(&priv->regs->ctrl);
141 reg_val |= 0x1 << cs;
142 writel(reg_val, &priv->regs->ctrl);
145 static u32 ast2600_spi_segment_start(struct udevice *bus, u32 reg)
147 struct aspeed_spi_plat *plat = dev_get_plat(bus);
148 u32 start_offset = (reg << 16) & 0x0ff00000;
150 if (start_offset == 0)
151 return (u32)plat->ahb_base;
153 return (u32)plat->ahb_base + start_offset;
156 static u32 ast2600_spi_segment_end(struct udevice *bus, u32 reg)
158 struct aspeed_spi_plat *plat = dev_get_plat(bus);
159 u32 end_offset = reg & 0x0ff00000;
161 /* Meaningless end_offset, set to physical ahb base. */
163 return (u32)plat->ahb_base;
165 return (u32)plat->ahb_base + end_offset + 0x100000;
168 static u32 ast2600_spi_segment_reg(u32 start, u32 end)
173 return ((start & 0x0ff00000) >> 16) | ((end - 0x100000) & 0x0ff00000);
176 static void ast2600_spi_chip_set_4byte(struct udevice *bus, u32 cs)
178 struct aspeed_spi_priv *priv = dev_get_priv(bus);
181 reg_val = readl(&priv->regs->ctrl);
182 reg_val |= 0x11 << cs;
183 writel(reg_val, &priv->regs->ctrl);
186 static int aspeed_spi_read_from_ahb(void __iomem *ahb_base, void *buf,
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);
198 readsb(ahb_base, (u8 *)buf + offset, len);
203 static int aspeed_spi_write_to_ahb(void __iomem *ahb_base, const void *buf,
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);
215 writesb(ahb_base, (u8 *)buf + offset, len);
221 * Currently, only support 1-1-1, 1-1-2 or 1-1-4
222 * SPI NOR flash operation format.
224 static bool aspeed_spi_supports_op(struct spi_slave *slave,
225 const struct spi_mem_op *op)
227 struct udevice *bus = slave->dev->parent;
228 struct aspeed_spi_priv *priv = dev_get_priv(bus);
230 if (op->cmd.buswidth > 1)
233 if (op->addr.nbytes != 0) {
234 if (op->addr.buswidth > 1)
236 if (op->addr.nbytes < 3 || op->addr.nbytes > 4)
240 if (op->dummy.nbytes != 0) {
241 if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7)
245 if (op->data.nbytes != 0 &&
246 op->data.buswidth > priv->info->max_bus_width)
249 if (!spi_mem_default_supports_op(slave, op))
255 static int aspeed_spi_exec_op_user_mode(struct spi_slave *slave,
256 const struct spi_mem_op *op)
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];
265 struct aspeed_spi_flash *flash = &priv->flashes[cs];
266 u8 dummy_data[16] = {0};
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);
276 * Set controller to 4-byte address mode
277 * if flash is in 4-byte address mode.
279 if (op->cmd.opcode == SPINOR_OP_EN4B)
280 priv->info->set_4byte(bus, cs);
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);
289 aspeed_spi_write_to_ahb(flash->ahb_base, &op->cmd.opcode, 1);
292 for (i = op->addr.nbytes; i > 0; i--) {
293 addr[op->addr.nbytes - i] =
294 ((u32)op->addr.val >> ((i - 1) * 8)) & 0xff;
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);
303 /* Send dummy cycles */
304 aspeed_spi_write_to_ahb(flash->ahb_base, dummy_data, op->dummy.nbytes);
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);
312 if (op->data.dir == SPI_MEM_DATA_OUT) {
313 aspeed_spi_write_to_ahb(flash->ahb_base, op->data.buf.out,
316 aspeed_spi_read_from_ahb(flash->ahb_base, op->data.buf.in,
320 ce_ctrl_val |= CTRL_STOP_ACTIVE;
321 writel(ce_ctrl_val, ce_ctrl_reg);
323 /* Restore controller setting. */
324 writel(flash->ce_ctrl_read, ce_ctrl_reg);
329 static struct aspeed_spi_flash *aspeed_spi_get_flash(struct udevice *dev)
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;
337 if (cs >= plat->max_cs) {
338 dev_err(dev, "invalid CS %u\n", cs);
342 return &priv->flashes[cs];
345 static void aspeed_spi_decoded_base_calculate(struct udevice *bus)
347 struct aspeed_spi_plat *plat = dev_get_plat(bus);
348 struct aspeed_spi_priv *priv = dev_get_priv(bus);
351 priv->flashes[0].ahb_base = plat->ahb_base;
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;
360 static void aspeed_spi_decoded_range_set(struct udevice *bus)
362 struct aspeed_spi_plat *plat = dev_get_plat(bus);
363 struct aspeed_spi_priv *priv = dev_get_priv(bus);
365 u32 start_addr, end_addr;
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;
373 decoded_reg_val = priv->info->segment_reg(start_addr, end_addr);
375 writel(decoded_reg_val, &priv->regs->segment_addr[cs]);
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);
382 static int aspeed_spi_decoded_range_config(struct udevice *bus)
384 aspeed_spi_decoded_base_calculate(bus);
385 aspeed_spi_decoded_range_set(bus);
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.
396 static int aspeed_spi_ctrl_init(struct udevice *bus)
399 struct aspeed_spi_plat *plat = dev_get_plat(bus);
400 struct aspeed_spi_priv *priv = dev_get_priv(bus);
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),
410 memset(priv->flashes, 0x0,
411 sizeof(struct aspeed_spi_flash) * ASPEED_SPI_MAX_CS);
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);
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);
425 if (decoded_sz < priv->info->min_decoded_sz)
426 decoded_sz = priv->info->min_decoded_sz;
428 priv->flashes[cs].ahb_decoded_sz = decoded_sz;
431 ret = aspeed_spi_decoded_range_config(bus);
436 static const struct aspeed_spi_info ast2500_fmc_info = {
437 .io_mode_mask = 0x70000000,
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,
447 * There are some different between FMC and SPI controllers.
448 * For example, DMA operation, but this isn't implemented currently.
450 static const struct aspeed_spi_info ast2500_spi_info = {
451 .io_mode_mask = 0x70000000,
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,
460 static const struct aspeed_spi_info ast2600_fmc_info = {
461 .io_mode_mask = 0xf0000000,
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,
470 static const struct aspeed_spi_info ast2600_spi_info = {
471 .io_mode_mask = 0xf0000000,
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,
480 static int aspeed_spi_claim_bus(struct udevice *dev)
482 struct udevice *bus = dev->parent;
483 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
485 dev_dbg(bus, "%s: claim bus CS%u\n", bus->name, slave_plat->cs);
490 static int aspeed_spi_release_bus(struct udevice *dev)
492 struct udevice *bus = dev->parent;
493 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
495 dev_dbg(bus, "%s: release bus CS%u\n", bus->name, slave_plat->cs);
497 if (!aspeed_spi_get_flash(dev))
503 static int aspeed_spi_set_mode(struct udevice *bus, uint mode)
505 dev_dbg(bus, "%s: setting mode to %x\n", bus->name, mode);
510 static int aspeed_spi_set_speed(struct udevice *bus, uint hz)
512 dev_dbg(bus, "%s: setting speed to %u\n", bus->name, hz);
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.
522 static int apseed_spi_of_to_plat(struct udevice *bus)
524 struct aspeed_spi_plat *plat = dev_get_plat(bus);
525 struct aspeed_spi_priv *priv = dev_get_priv(bus);
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");
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");
540 plat->max_cs = dev_read_u32_default(bus, "num-cs", ASPEED_SPI_MAX_CS);
541 if (plat->max_cs > ASPEED_SPI_MAX_CS)
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);
551 static int aspeed_spi_probe(struct udevice *bus)
554 struct aspeed_spi_priv *priv = dev_get_priv(bus);
557 priv->info = (struct aspeed_spi_info *)dev_get_driver_data(bus);
560 for (device_find_first_child(bus, &dev); dev;
561 device_find_next_child(&dev)) {
565 if (priv->num_cs > ASPEED_SPI_MAX_CS)
568 ret = aspeed_spi_ctrl_init(bus);
573 static 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,
578 static 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,
586 static 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, },
594 U_BOOT_DRIVER(aspeed_spi) = {
595 .name = "aspeed_spi_smc",
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,