1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // Copyright (C) IBM Corporation 2018
3 // FSI master driver for AST2600
6 #include <linux/delay.h>
9 #include <linux/mfd/syscon.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/iopoll.h>
17 #include <linux/gpio/consumer.h>
19 #include "fsi-master.h"
21 struct fsi_master_aspeed {
22 struct fsi_master master;
23 struct mutex lock; /* protect HW access */
27 struct gpio_desc *cfam_reset_gpio;
30 #define to_fsi_master_aspeed(m) \
31 container_of(m, struct fsi_master_aspeed, master)
33 /* Control register (size 0x400) */
34 static const u32 ctrl_base = 0x80000000;
36 static const u32 fsi_base = 0xa0000000;
38 #define OPB_FSI_VER 0x00
39 #define OPB_TRIGGER 0x04
40 #define OPB_CTRL_BASE 0x08
41 #define OPB_FSI_BASE 0x0c
42 #define OPB_CLK_SYNC 0x3c
43 #define OPB_IRQ_CLEAR 0x40
44 #define OPB_IRQ_MASK 0x44
45 #define OPB_IRQ_STATUS 0x48
47 #define OPB0_SELECT 0x10
49 #define OPB0_XFER_SIZE 0x18
50 #define OPB0_FSI_ADDR 0x1c
51 #define OPB0_FSI_DATA_W 0x20
52 #define OPB0_STATUS 0x80
53 #define OPB0_FSI_DATA_R 0x84
55 #define OPB0_WRITE_ORDER1 0x4c
56 #define OPB0_WRITE_ORDER2 0x50
57 #define OPB1_WRITE_ORDER1 0x54
58 #define OPB1_WRITE_ORDER2 0x58
59 #define OPB0_READ_ORDER1 0x5c
60 #define OPB1_READ_ORDER2 0x60
62 #define OPB_RETRY_COUNTER 0x64
65 #define STATUS_HALFWORD_ACK BIT(0)
66 #define STATUS_FULLWORD_ACK BIT(1)
67 #define STATUS_ERR_ACK BIT(2)
68 #define STATUS_RETRY BIT(3)
69 #define STATUS_TIMEOUT BIT(4)
72 #define OPB1_XFER_ACK_EN BIT(17)
73 #define OPB0_XFER_ACK_EN BIT(16)
76 #define CMD_READ BIT(0)
80 #define XFER_FULLWORD (BIT(1) | BIT(0))
81 #define XFER_HALFWORD (BIT(0))
84 #define CREATE_TRACE_POINTS
85 #include <trace/events/fsi_master_aspeed.h>
87 #define FSI_LINK_ENABLE_SETUP_TIME 10 /* in mS */
89 /* Run the bus at maximum speed by default */
90 #define FSI_DIVISOR_DEFAULT 1
91 #define FSI_DIVISOR_CABLED 2
92 static u16 aspeed_fsi_divisor = FSI_DIVISOR_DEFAULT;
93 module_param_named(bus_div,aspeed_fsi_divisor, ushort, 0);
95 #define OPB_POLL_TIMEOUT 500
97 static int __opb_write(struct fsi_master_aspeed *aspeed, u32 addr,
98 u32 val, u32 transfer_size)
100 void __iomem *base = aspeed->base;
105 * The ordering of these writes up until the trigger
106 * write does not matter, so use writel_relaxed.
108 writel_relaxed(CMD_WRITE, base + OPB0_RW);
109 writel_relaxed(transfer_size, base + OPB0_XFER_SIZE);
110 writel_relaxed(addr, base + OPB0_FSI_ADDR);
111 writel_relaxed(val, base + OPB0_FSI_DATA_W);
112 writel_relaxed(0x1, base + OPB_IRQ_CLEAR);
113 writel(0x1, base + OPB_TRIGGER);
115 ret = readl_poll_timeout(base + OPB_IRQ_STATUS, reg,
116 (reg & OPB0_XFER_ACK_EN) != 0,
117 0, OPB_POLL_TIMEOUT);
119 status = readl(base + OPB0_STATUS);
121 trace_fsi_master_aspeed_opb_write(addr, val, transfer_size, status, reg);
123 /* Return error when poll timed out */
127 /* Command failed, master will reset */
128 if (status & STATUS_ERR_ACK)
134 static int opb_writeb(struct fsi_master_aspeed *aspeed, u32 addr, u8 val)
136 return __opb_write(aspeed, addr, val, XFER_BYTE);
139 static int opb_writew(struct fsi_master_aspeed *aspeed, u32 addr, __be16 val)
141 return __opb_write(aspeed, addr, (__force u16)val, XFER_HALFWORD);
144 static int opb_writel(struct fsi_master_aspeed *aspeed, u32 addr, __be32 val)
146 return __opb_write(aspeed, addr, (__force u32)val, XFER_FULLWORD);
149 static int __opb_read(struct fsi_master_aspeed *aspeed, uint32_t addr,
150 u32 transfer_size, void *out)
152 void __iomem *base = aspeed->base;
157 * The ordering of these writes up until the trigger
158 * write does not matter, so use writel_relaxed.
160 writel_relaxed(CMD_READ, base + OPB0_RW);
161 writel_relaxed(transfer_size, base + OPB0_XFER_SIZE);
162 writel_relaxed(addr, base + OPB0_FSI_ADDR);
163 writel_relaxed(0x1, base + OPB_IRQ_CLEAR);
164 writel(0x1, base + OPB_TRIGGER);
166 ret = readl_poll_timeout(base + OPB_IRQ_STATUS, reg,
167 (reg & OPB0_XFER_ACK_EN) != 0,
168 0, OPB_POLL_TIMEOUT);
170 status = readl(base + OPB0_STATUS);
172 result = readl(base + OPB0_FSI_DATA_R);
174 trace_fsi_master_aspeed_opb_read(addr, transfer_size, result,
175 readl(base + OPB0_STATUS),
178 /* Return error when poll timed out */
182 /* Command failed, master will reset */
183 if (status & STATUS_ERR_ACK)
187 switch (transfer_size) {
192 *(u16 *)out = result;
195 *(u32 *)out = result;
206 static int opb_readl(struct fsi_master_aspeed *aspeed, uint32_t addr, __be32 *out)
208 return __opb_read(aspeed, addr, XFER_FULLWORD, out);
211 static int opb_readw(struct fsi_master_aspeed *aspeed, uint32_t addr, __be16 *out)
213 return __opb_read(aspeed, addr, XFER_HALFWORD, (void *)out);
216 static int opb_readb(struct fsi_master_aspeed *aspeed, uint32_t addr, u8 *out)
218 return __opb_read(aspeed, addr, XFER_BYTE, (void *)out);
221 static int check_errors(struct fsi_master_aspeed *aspeed, int err)
225 if (trace_fsi_master_aspeed_opb_error_enabled()) {
226 __be32 mresp0, mstap0, mesrb0;
228 opb_readl(aspeed, ctrl_base + FSI_MRESP0, &mresp0);
229 opb_readl(aspeed, ctrl_base + FSI_MSTAP0, &mstap0);
230 opb_readl(aspeed, ctrl_base + FSI_MESRB0, &mesrb0);
232 trace_fsi_master_aspeed_opb_error(
235 be32_to_cpu(mesrb0));
239 /* Check MAEB (0x70) ? */
241 /* Then clear errors in master */
242 ret = opb_writel(aspeed, ctrl_base + FSI_MRESP0,
243 cpu_to_be32(FSI_MRESP_RST_ALL_MASTER));
245 /* TODO: log? return different code? */
248 /* TODO: confirm that 0x70 was okay */
251 /* This will pass through timeout errors */
255 static int aspeed_master_read(struct fsi_master *master, int link,
256 uint8_t id, uint32_t addr, void *val, size_t size)
258 struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
265 addr += link * FSI_HUB_LINK_SIZE;
267 mutex_lock(&aspeed->lock);
271 ret = opb_readb(aspeed, fsi_base + addr, val);
274 ret = opb_readw(aspeed, fsi_base + addr, val);
277 ret = opb_readl(aspeed, fsi_base + addr, val);
284 ret = check_errors(aspeed, ret);
286 mutex_unlock(&aspeed->lock);
290 static int aspeed_master_write(struct fsi_master *master, int link,
291 uint8_t id, uint32_t addr, const void *val, size_t size)
293 struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
300 addr += link * FSI_HUB_LINK_SIZE;
302 mutex_lock(&aspeed->lock);
306 ret = opb_writeb(aspeed, fsi_base + addr, *(u8 *)val);
309 ret = opb_writew(aspeed, fsi_base + addr, *(__be16 *)val);
312 ret = opb_writel(aspeed, fsi_base + addr, *(__be32 *)val);
319 ret = check_errors(aspeed, ret);
321 mutex_unlock(&aspeed->lock);
325 static int aspeed_master_link_enable(struct fsi_master *master, int link,
328 struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
335 reg = cpu_to_be32(0x80000000 >> bit);
337 mutex_lock(&aspeed->lock);
340 ret = opb_writel(aspeed, ctrl_base + FSI_MCENP0 + (4 * idx), reg);
344 ret = opb_writel(aspeed, ctrl_base + FSI_MSENP0 + (4 * idx), reg);
348 mdelay(FSI_LINK_ENABLE_SETUP_TIME);
350 mutex_unlock(&aspeed->lock);
354 static int aspeed_master_term(struct fsi_master *master, int link, uint8_t id)
360 cmd = cpu_to_be32(0xecc00000);
362 return aspeed_master_write(master, link, id, addr, &cmd, 4);
365 static int aspeed_master_break(struct fsi_master *master, int link)
371 cmd = cpu_to_be32(0xc0de0000);
373 return aspeed_master_write(master, link, 0, addr, &cmd, 4);
376 static void aspeed_master_release(struct device *dev)
378 struct fsi_master_aspeed *aspeed =
379 to_fsi_master_aspeed(dev_to_fsi_master(dev));
385 static inline u32 fsi_mmode_crs0(u32 x)
387 return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
390 static inline u32 fsi_mmode_crs1(u32 x)
392 return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
395 static int aspeed_master_init(struct fsi_master_aspeed *aspeed)
399 reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
400 | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
401 opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
403 /* Initialize the MFSI (hub master) engine */
404 reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
405 | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
406 opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
408 reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
409 opb_writel(aspeed, ctrl_base + FSI_MECTRL, reg);
411 reg = cpu_to_be32(FSI_MMODE_ECRC | FSI_MMODE_EPC | FSI_MMODE_RELA
412 | fsi_mmode_crs0(aspeed_fsi_divisor)
413 | fsi_mmode_crs1(aspeed_fsi_divisor)
414 | FSI_MMODE_P8_TO_LSB);
415 dev_info(aspeed->dev, "mmode set to %08x (divisor %d)\n",
416 be32_to_cpu(reg), aspeed_fsi_divisor);
417 opb_writel(aspeed, ctrl_base + FSI_MMODE, reg);
419 reg = cpu_to_be32(0xffff0000);
420 opb_writel(aspeed, ctrl_base + FSI_MDLYR, reg);
422 reg = cpu_to_be32(~0);
423 opb_writel(aspeed, ctrl_base + FSI_MSENP0, reg);
425 /* Leave enabled long enough for master logic to set up */
426 mdelay(FSI_LINK_ENABLE_SETUP_TIME);
428 opb_writel(aspeed, ctrl_base + FSI_MCENP0, reg);
430 opb_readl(aspeed, ctrl_base + FSI_MAEB, NULL);
432 reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
433 opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
435 opb_readl(aspeed, ctrl_base + FSI_MLEVP0, NULL);
437 /* Reset the master bridge */
438 reg = cpu_to_be32(FSI_MRESB_RST_GEN);
439 opb_writel(aspeed, ctrl_base + FSI_MRESB0, reg);
441 reg = cpu_to_be32(FSI_MRESB_RST_ERR);
442 opb_writel(aspeed, ctrl_base + FSI_MRESB0, reg);
447 static ssize_t cfam_reset_store(struct device *dev, struct device_attribute *attr,
448 const char *buf, size_t count)
450 struct fsi_master_aspeed *aspeed = dev_get_drvdata(dev);
452 mutex_lock(&aspeed->lock);
453 gpiod_set_value(aspeed->cfam_reset_gpio, 1);
454 usleep_range(900, 1000);
455 gpiod_set_value(aspeed->cfam_reset_gpio, 0);
456 mutex_unlock(&aspeed->lock);
461 static DEVICE_ATTR(cfam_reset, 0200, NULL, cfam_reset_store);
463 static int setup_cfam_reset(struct fsi_master_aspeed *aspeed)
465 struct device *dev = aspeed->dev;
466 struct gpio_desc *gpio;
469 gpio = devm_gpiod_get_optional(dev, "cfam-reset", GPIOD_OUT_LOW);
471 return PTR_ERR(gpio);
475 aspeed->cfam_reset_gpio = gpio;
477 rc = device_create_file(dev, &dev_attr_cfam_reset);
479 devm_gpiod_put(dev, gpio);
486 static int tacoma_cabled_fsi_fixup(struct device *dev)
488 struct gpio_desc *routing_gpio, *mux_gpio;
492 * The routing GPIO is a jumper indicating we should mux for the
493 * externally connected FSI cable.
495 routing_gpio = devm_gpiod_get_optional(dev, "fsi-routing",
496 GPIOD_IN | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
497 if (IS_ERR(routing_gpio))
498 return PTR_ERR(routing_gpio);
502 mux_gpio = devm_gpiod_get_optional(dev, "fsi-mux", GPIOD_ASIS);
503 if (IS_ERR(mux_gpio))
504 return PTR_ERR(mux_gpio);
508 gpio = gpiod_get_value(routing_gpio);
512 /* If the routing GPIO is high we should set the mux to low. */
515 * Cable signal integrity means we should run the bus
516 * slightly slower. Do not override if a kernel param
517 * has already overridden.
519 if (aspeed_fsi_divisor == FSI_DIVISOR_DEFAULT)
520 aspeed_fsi_divisor = FSI_DIVISOR_CABLED;
522 gpiod_direction_output(mux_gpio, 0);
523 dev_info(dev, "FSI configured for external cable\n");
525 gpiod_direction_output(mux_gpio, 1);
528 devm_gpiod_put(dev, routing_gpio);
533 static int fsi_master_aspeed_probe(struct platform_device *pdev)
535 struct fsi_master_aspeed *aspeed;
539 rc = tacoma_cabled_fsi_fixup(&pdev->dev);
541 dev_err(&pdev->dev, "Tacoma FSI cable fixup failed\n");
545 aspeed = kzalloc(sizeof(*aspeed), GFP_KERNEL);
549 aspeed->dev = &pdev->dev;
551 aspeed->base = devm_platform_ioremap_resource(pdev, 0);
552 if (IS_ERR(aspeed->base)) {
553 rc = PTR_ERR(aspeed->base);
554 goto err_free_aspeed;
557 aspeed->clk = devm_clk_get(aspeed->dev, NULL);
558 if (IS_ERR(aspeed->clk)) {
559 dev_err(aspeed->dev, "couldn't get clock\n");
560 rc = PTR_ERR(aspeed->clk);
561 goto err_free_aspeed;
563 rc = clk_prepare_enable(aspeed->clk);
565 dev_err(aspeed->dev, "couldn't enable clock\n");
566 goto err_free_aspeed;
569 rc = setup_cfam_reset(aspeed);
571 dev_err(&pdev->dev, "CFAM reset GPIO setup failed\n");
574 writel(0x1, aspeed->base + OPB_CLK_SYNC);
575 writel(OPB1_XFER_ACK_EN | OPB0_XFER_ACK_EN,
576 aspeed->base + OPB_IRQ_MASK);
578 /* TODO: determine an appropriate value */
579 writel(0x10, aspeed->base + OPB_RETRY_COUNTER);
581 writel(ctrl_base, aspeed->base + OPB_CTRL_BASE);
582 writel(fsi_base, aspeed->base + OPB_FSI_BASE);
584 /* Set read data order */
585 writel(0x00030b1b, aspeed->base + OPB0_READ_ORDER1);
587 /* Set write data order */
588 writel(0x0011101b, aspeed->base + OPB0_WRITE_ORDER1);
589 writel(0x0c330f3f, aspeed->base + OPB0_WRITE_ORDER2);
592 * Select OPB0 for all operations.
593 * Will need to be reworked when enabling DMA or anything that uses
596 writel(0x1, aspeed->base + OPB0_SELECT);
598 rc = opb_readl(aspeed, ctrl_base + FSI_MVER, &raw);
600 dev_err(&pdev->dev, "failed to read hub version\n");
604 reg = be32_to_cpu(raw);
605 links = (reg >> 8) & 0xff;
606 dev_info(&pdev->dev, "hub version %08x (%d links)\n", reg, links);
608 aspeed->master.dev.parent = &pdev->dev;
609 aspeed->master.dev.release = aspeed_master_release;
610 aspeed->master.dev.of_node = of_node_get(dev_of_node(&pdev->dev));
612 aspeed->master.n_links = links;
613 aspeed->master.read = aspeed_master_read;
614 aspeed->master.write = aspeed_master_write;
615 aspeed->master.send_break = aspeed_master_break;
616 aspeed->master.term = aspeed_master_term;
617 aspeed->master.link_enable = aspeed_master_link_enable;
619 dev_set_drvdata(&pdev->dev, aspeed);
621 mutex_init(&aspeed->lock);
622 aspeed_master_init(aspeed);
624 rc = fsi_master_register(&aspeed->master);
628 /* At this point, fsi_master_register performs the device_initialize(),
629 * and holds the sole reference on master.dev. This means the device
630 * will be freed (via ->release) during any subsequent call to
631 * fsi_master_unregister. We add our own reference to it here, so we
632 * can perform cleanup (in _remove()) without it being freed before
635 get_device(&aspeed->master.dev);
639 clk_disable_unprepare(aspeed->clk);
645 static int fsi_master_aspeed_remove(struct platform_device *pdev)
647 struct fsi_master_aspeed *aspeed = platform_get_drvdata(pdev);
649 fsi_master_unregister(&aspeed->master);
650 clk_disable_unprepare(aspeed->clk);
655 static const struct of_device_id fsi_master_aspeed_match[] = {
656 { .compatible = "aspeed,ast2600-fsi-master" },
659 MODULE_DEVICE_TABLE(of, fsi_master_aspeed_match);
661 static struct platform_driver fsi_master_aspeed_driver = {
663 .name = "fsi-master-aspeed",
664 .of_match_table = fsi_master_aspeed_match,
666 .probe = fsi_master_aspeed_probe,
667 .remove = fsi_master_aspeed_remove,
670 module_platform_driver(fsi_master_aspeed_driver);
671 MODULE_LICENSE("GPL");