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>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <linux/iopoll.h>
17 #include "fsi-master.h"
19 struct fsi_master_aspeed
{
20 struct fsi_master master
;
26 #define to_fsi_master_aspeed(m) \
27 container_of(m, struct fsi_master_aspeed, master)
29 /* Control register (size 0x400) */
30 static const u32 ctrl_base
= 0x80000000;
32 static const u32 fsi_base
= 0xa0000000;
34 #define OPB_FSI_VER 0x00
35 #define OPB_TRIGGER 0x04
36 #define OPB_CTRL_BASE 0x08
37 #define OPB_FSI_BASE 0x0c
38 #define OPB_CLK_SYNC 0x3c
39 #define OPB_IRQ_CLEAR 0x40
40 #define OPB_IRQ_MASK 0x44
41 #define OPB_IRQ_STATUS 0x48
43 #define OPB0_SELECT 0x10
45 #define OPB0_XFER_SIZE 0x18
46 #define OPB0_FSI_ADDR 0x1c
47 #define OPB0_FSI_DATA_W 0x20
48 #define OPB0_STATUS 0x80
49 #define OPB0_FSI_DATA_R 0x84
51 #define OPB0_WRITE_ORDER1 0x4c
52 #define OPB0_WRITE_ORDER2 0x50
53 #define OPB1_WRITE_ORDER1 0x54
54 #define OPB1_WRITE_ORDER2 0x58
55 #define OPB0_READ_ORDER1 0x5c
56 #define OPB1_READ_ORDER2 0x60
58 #define OPB_RETRY_COUNTER 0x64
61 #define STATUS_HALFWORD_ACK BIT(0)
62 #define STATUS_FULLWORD_ACK BIT(1)
63 #define STATUS_ERR_ACK BIT(2)
64 #define STATUS_RETRY BIT(3)
65 #define STATUS_TIMEOUT BIT(4)
68 #define OPB1_XFER_ACK_EN BIT(17)
69 #define OPB0_XFER_ACK_EN BIT(16)
72 #define CMD_READ BIT(0)
76 #define XFER_FULLWORD (BIT(1) | BIT(0))
77 #define XFER_HALFWORD (BIT(0))
80 #define CREATE_TRACE_POINTS
81 #include <trace/events/fsi_master_aspeed.h>
83 #define FSI_LINK_ENABLE_SETUP_TIME 10 /* in mS */
85 #define DEFAULT_DIVISOR 14
86 #define OPB_POLL_TIMEOUT 10000
88 static int __opb_write(struct fsi_master_aspeed
*aspeed
, u32 addr
,
89 u32 val
, u32 transfer_size
)
91 void __iomem
*base
= aspeed
->base
;
95 writel(CMD_WRITE
, base
+ OPB0_RW
);
96 writel(transfer_size
, base
+ OPB0_XFER_SIZE
);
97 writel(addr
, base
+ OPB0_FSI_ADDR
);
98 writel(val
, base
+ OPB0_FSI_DATA_W
);
99 writel(0x1, base
+ OPB_IRQ_CLEAR
);
100 writel(0x1, base
+ OPB_TRIGGER
);
102 ret
= readl_poll_timeout(base
+ OPB_IRQ_STATUS
, reg
,
103 (reg
& OPB0_XFER_ACK_EN
) != 0,
104 0, OPB_POLL_TIMEOUT
);
106 status
= readl(base
+ OPB0_STATUS
);
108 trace_fsi_master_aspeed_opb_write(addr
, val
, transfer_size
, status
, reg
);
110 /* Return error when poll timed out */
114 /* Command failed, master will reset */
115 if (status
& STATUS_ERR_ACK
)
121 static int opb_writeb(struct fsi_master_aspeed
*aspeed
, u32 addr
, u8 val
)
123 return __opb_write(aspeed
, addr
, val
, XFER_BYTE
);
126 static int opb_writew(struct fsi_master_aspeed
*aspeed
, u32 addr
, __be16 val
)
128 return __opb_write(aspeed
, addr
, (__force u16
)val
, XFER_HALFWORD
);
131 static int opb_writel(struct fsi_master_aspeed
*aspeed
, u32 addr
, __be32 val
)
133 return __opb_write(aspeed
, addr
, (__force u32
)val
, XFER_FULLWORD
);
136 static int __opb_read(struct fsi_master_aspeed
*aspeed
, uint32_t addr
,
137 u32 transfer_size
, void *out
)
139 void __iomem
*base
= aspeed
->base
;
143 writel(CMD_READ
, base
+ OPB0_RW
);
144 writel(transfer_size
, base
+ OPB0_XFER_SIZE
);
145 writel(addr
, base
+ OPB0_FSI_ADDR
);
146 writel(0x1, base
+ OPB_IRQ_CLEAR
);
147 writel(0x1, base
+ OPB_TRIGGER
);
149 ret
= readl_poll_timeout(base
+ OPB_IRQ_STATUS
, reg
,
150 (reg
& OPB0_XFER_ACK_EN
) != 0,
151 0, OPB_POLL_TIMEOUT
);
153 status
= readl(base
+ OPB0_STATUS
);
155 result
= readl(base
+ OPB0_FSI_DATA_R
);
157 trace_fsi_master_aspeed_opb_read(addr
, transfer_size
, result
,
158 readl(base
+ OPB0_STATUS
),
161 /* Return error when poll timed out */
165 /* Command failed, master will reset */
166 if (status
& STATUS_ERR_ACK
)
170 switch (transfer_size
) {
175 *(u16
*)out
= result
;
178 *(u32
*)out
= result
;
189 static int opb_readl(struct fsi_master_aspeed
*aspeed
, uint32_t addr
, __be32
*out
)
191 return __opb_read(aspeed
, addr
, XFER_FULLWORD
, out
);
194 static int opb_readw(struct fsi_master_aspeed
*aspeed
, uint32_t addr
, __be16
*out
)
196 return __opb_read(aspeed
, addr
, XFER_HALFWORD
, (void *)out
);
199 static int opb_readb(struct fsi_master_aspeed
*aspeed
, uint32_t addr
, u8
*out
)
201 return __opb_read(aspeed
, addr
, XFER_BYTE
, (void *)out
);
204 static int check_errors(struct fsi_master_aspeed
*aspeed
, int err
)
208 if (trace_fsi_master_aspeed_opb_error_enabled()) {
209 __be32 mresp0
, mstap0
, mesrb0
;
211 opb_readl(aspeed
, ctrl_base
+ FSI_MRESP0
, &mresp0
);
212 opb_readl(aspeed
, ctrl_base
+ FSI_MSTAP0
, &mstap0
);
213 opb_readl(aspeed
, ctrl_base
+ FSI_MESRB0
, &mesrb0
);
215 trace_fsi_master_aspeed_opb_error(
218 be32_to_cpu(mesrb0
));
222 /* Check MAEB (0x70) ? */
224 /* Then clear errors in master */
225 ret
= opb_writel(aspeed
, ctrl_base
+ FSI_MRESP0
,
226 cpu_to_be32(FSI_MRESP_RST_ALL_MASTER
));
228 /* TODO: log? return different code? */
231 /* TODO: confirm that 0x70 was okay */
234 /* This will pass through timeout errors */
238 static int aspeed_master_read(struct fsi_master
*master
, int link
,
239 uint8_t id
, uint32_t addr
, void *val
, size_t size
)
241 struct fsi_master_aspeed
*aspeed
= to_fsi_master_aspeed(master
);
247 addr
+= link
* FSI_HUB_LINK_SIZE
;
251 ret
= opb_readb(aspeed
, fsi_base
+ addr
, val
);
254 ret
= opb_readw(aspeed
, fsi_base
+ addr
, val
);
257 ret
= opb_readl(aspeed
, fsi_base
+ addr
, val
);
263 ret
= check_errors(aspeed
, ret
);
270 static int aspeed_master_write(struct fsi_master
*master
, int link
,
271 uint8_t id
, uint32_t addr
, const void *val
, size_t size
)
273 struct fsi_master_aspeed
*aspeed
= to_fsi_master_aspeed(master
);
279 addr
+= link
* FSI_HUB_LINK_SIZE
;
283 ret
= opb_writeb(aspeed
, fsi_base
+ addr
, *(u8
*)val
);
286 ret
= opb_writew(aspeed
, fsi_base
+ addr
, *(__be16
*)val
);
289 ret
= opb_writel(aspeed
, fsi_base
+ addr
, *(__be32
*)val
);
295 ret
= check_errors(aspeed
, ret
);
302 static int aspeed_master_link_enable(struct fsi_master
*master
, int link
)
304 struct fsi_master_aspeed
*aspeed
= to_fsi_master_aspeed(master
);
311 reg
= cpu_to_be32(0x80000000 >> bit
);
313 ret
= opb_writel(aspeed
, ctrl_base
+ FSI_MSENP0
+ (4 * idx
), reg
);
317 mdelay(FSI_LINK_ENABLE_SETUP_TIME
);
319 ret
= opb_readl(aspeed
, ctrl_base
+ FSI_MENP0
+ (4 * idx
), &result
);
324 dev_err(aspeed
->dev
, "%s failed: %08x\n", __func__
, result
);
331 static int aspeed_master_term(struct fsi_master
*master
, int link
, uint8_t id
)
337 cmd
= cpu_to_be32(0xecc00000);
339 return aspeed_master_write(master
, link
, id
, addr
, &cmd
, 4);
342 static int aspeed_master_break(struct fsi_master
*master
, int link
)
348 cmd
= cpu_to_be32(0xc0de0000);
350 return aspeed_master_write(master
, link
, 0, addr
, &cmd
, 4);
353 static void aspeed_master_release(struct device
*dev
)
355 struct fsi_master_aspeed
*aspeed
=
356 to_fsi_master_aspeed(dev_to_fsi_master(dev
));
362 static inline u32
fsi_mmode_crs0(u32 x
)
364 return (x
& FSI_MMODE_CRS0MASK
) << FSI_MMODE_CRS0SHFT
;
367 static inline u32
fsi_mmode_crs1(u32 x
)
369 return (x
& FSI_MMODE_CRS1MASK
) << FSI_MMODE_CRS1SHFT
;
372 static int aspeed_master_init(struct fsi_master_aspeed
*aspeed
)
376 reg
= cpu_to_be32(FSI_MRESP_RST_ALL_MASTER
| FSI_MRESP_RST_ALL_LINK
377 | FSI_MRESP_RST_MCR
| FSI_MRESP_RST_PYE
);
378 opb_writel(aspeed
, ctrl_base
+ FSI_MRESP0
, reg
);
380 /* Initialize the MFSI (hub master) engine */
381 reg
= cpu_to_be32(FSI_MRESP_RST_ALL_MASTER
| FSI_MRESP_RST_ALL_LINK
382 | FSI_MRESP_RST_MCR
| FSI_MRESP_RST_PYE
);
383 opb_writel(aspeed
, ctrl_base
+ FSI_MRESP0
, reg
);
385 reg
= cpu_to_be32(FSI_MECTRL_EOAE
| FSI_MECTRL_P8_AUTO_TERM
);
386 opb_writel(aspeed
, ctrl_base
+ FSI_MECTRL
, reg
);
388 reg
= cpu_to_be32(FSI_MMODE_ECRC
| FSI_MMODE_EPC
| FSI_MMODE_RELA
389 | fsi_mmode_crs0(DEFAULT_DIVISOR
)
390 | fsi_mmode_crs1(DEFAULT_DIVISOR
)
391 | FSI_MMODE_P8_TO_LSB
);
392 opb_writel(aspeed
, ctrl_base
+ FSI_MMODE
, reg
);
394 reg
= cpu_to_be32(0xffff0000);
395 opb_writel(aspeed
, ctrl_base
+ FSI_MDLYR
, reg
);
397 reg
= cpu_to_be32(~0);
398 opb_writel(aspeed
, ctrl_base
+ FSI_MSENP0
, reg
);
400 /* Leave enabled long enough for master logic to set up */
401 mdelay(FSI_LINK_ENABLE_SETUP_TIME
);
403 opb_writel(aspeed
, ctrl_base
+ FSI_MCENP0
, reg
);
405 opb_readl(aspeed
, ctrl_base
+ FSI_MAEB
, NULL
);
407 reg
= cpu_to_be32(FSI_MRESP_RST_ALL_MASTER
| FSI_MRESP_RST_ALL_LINK
);
408 opb_writel(aspeed
, ctrl_base
+ FSI_MRESP0
, reg
);
410 opb_readl(aspeed
, ctrl_base
+ FSI_MLEVP0
, NULL
);
412 /* Reset the master bridge */
413 reg
= cpu_to_be32(FSI_MRESB_RST_GEN
);
414 opb_writel(aspeed
, ctrl_base
+ FSI_MRESB0
, reg
);
416 reg
= cpu_to_be32(FSI_MRESB_RST_ERR
);
417 opb_writel(aspeed
, ctrl_base
+ FSI_MRESB0
, reg
);
422 static int fsi_master_aspeed_probe(struct platform_device
*pdev
)
424 struct fsi_master_aspeed
*aspeed
;
425 struct resource
*res
;
429 aspeed
= devm_kzalloc(&pdev
->dev
, sizeof(*aspeed
), GFP_KERNEL
);
433 aspeed
->dev
= &pdev
->dev
;
435 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
436 aspeed
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
437 if (IS_ERR(aspeed
->base
))
438 return PTR_ERR(aspeed
->base
);
440 aspeed
->clk
= devm_clk_get(aspeed
->dev
, NULL
);
441 if (IS_ERR(aspeed
->clk
)) {
442 dev_err(aspeed
->dev
, "couldn't get clock\n");
443 return PTR_ERR(aspeed
->clk
);
445 rc
= clk_prepare_enable(aspeed
->clk
);
447 dev_err(aspeed
->dev
, "couldn't enable clock\n");
451 writel(0x1, aspeed
->base
+ OPB_CLK_SYNC
);
452 writel(OPB1_XFER_ACK_EN
| OPB0_XFER_ACK_EN
,
453 aspeed
->base
+ OPB_IRQ_MASK
);
455 /* TODO: determine an appropriate value */
456 writel(0x10, aspeed
->base
+ OPB_RETRY_COUNTER
);
458 writel(ctrl_base
, aspeed
->base
+ OPB_CTRL_BASE
);
459 writel(fsi_base
, aspeed
->base
+ OPB_FSI_BASE
);
461 /* Set read data order */
462 writel(0x00030b1b, aspeed
->base
+ OPB0_READ_ORDER1
);
464 /* Set write data order */
465 writel(0x0011101b, aspeed
->base
+ OPB0_WRITE_ORDER1
);
466 writel(0x0c330f3f, aspeed
->base
+ OPB0_WRITE_ORDER2
);
469 * Select OPB0 for all operations.
470 * Will need to be reworked when enabling DMA or anything that uses
473 writel(0x1, aspeed
->base
+ OPB0_SELECT
);
475 rc
= opb_readl(aspeed
, ctrl_base
+ FSI_MVER
, &raw
);
477 dev_err(&pdev
->dev
, "failed to read hub version\n");
481 reg
= be32_to_cpu(raw
);
482 links
= (reg
>> 8) & 0xff;
483 dev_info(&pdev
->dev
, "hub version %08x (%d links)\n", reg
, links
);
485 aspeed
->master
.dev
.parent
= &pdev
->dev
;
486 aspeed
->master
.dev
.release
= aspeed_master_release
;
487 aspeed
->master
.dev
.of_node
= of_node_get(dev_of_node(&pdev
->dev
));
489 aspeed
->master
.n_links
= links
;
490 aspeed
->master
.read
= aspeed_master_read
;
491 aspeed
->master
.write
= aspeed_master_write
;
492 aspeed
->master
.send_break
= aspeed_master_break
;
493 aspeed
->master
.term
= aspeed_master_term
;
494 aspeed
->master
.link_enable
= aspeed_master_link_enable
;
496 dev_set_drvdata(&pdev
->dev
, aspeed
);
498 aspeed_master_init(aspeed
);
500 rc
= fsi_master_register(&aspeed
->master
);
504 /* At this point, fsi_master_register performs the device_initialize(),
505 * and holds the sole reference on master.dev. This means the device
506 * will be freed (via ->release) during any subsequent call to
507 * fsi_master_unregister. We add our own reference to it here, so we
508 * can perform cleanup (in _remove()) without it being freed before
511 get_device(&aspeed
->master
.dev
);
515 clk_disable_unprepare(aspeed
->clk
);
519 static int fsi_master_aspeed_remove(struct platform_device
*pdev
)
521 struct fsi_master_aspeed
*aspeed
= platform_get_drvdata(pdev
);
523 fsi_master_unregister(&aspeed
->master
);
524 clk_disable_unprepare(aspeed
->clk
);
529 static const struct of_device_id fsi_master_aspeed_match
[] = {
530 { .compatible
= "aspeed,ast2600-fsi-master" },
534 static struct platform_driver fsi_master_aspeed_driver
= {
536 .name
= "fsi-master-aspeed",
537 .of_match_table
= fsi_master_aspeed_match
,
539 .probe
= fsi_master_aspeed_probe
,
540 .remove
= fsi_master_aspeed_remove
,
543 module_platform_driver(fsi_master_aspeed_driver
);
544 MODULE_LICENSE("GPL");