1606397d6SJoel Stanley // SPDX-License-Identifier: GPL-2.0-or-later
2606397d6SJoel Stanley // Copyright (C) IBM Corporation 2018
3606397d6SJoel Stanley // FSI master driver for AST2600
4606397d6SJoel Stanley 
5606397d6SJoel Stanley #include <linux/clk.h>
6606397d6SJoel Stanley #include <linux/delay.h>
7606397d6SJoel Stanley #include <linux/fsi.h>
8606397d6SJoel Stanley #include <linux/io.h>
9606397d6SJoel Stanley #include <linux/mfd/syscon.h>
10606397d6SJoel Stanley #include <linux/module.h>
11dfd7f2c1SEddie James #include <linux/mutex.h>
12606397d6SJoel Stanley #include <linux/of.h>
13606397d6SJoel Stanley #include <linux/platform_device.h>
14606397d6SJoel Stanley #include <linux/regmap.h>
15606397d6SJoel Stanley #include <linux/slab.h>
16606397d6SJoel Stanley #include <linux/iopoll.h>
17f369a29bSJoel Stanley #include <linux/gpio/consumer.h>
18606397d6SJoel Stanley 
19606397d6SJoel Stanley #include "fsi-master.h"
20606397d6SJoel Stanley 
21606397d6SJoel Stanley struct fsi_master_aspeed {
22606397d6SJoel Stanley 	struct fsi_master	master;
23dfd7f2c1SEddie James 	struct mutex		lock;	/* protect HW access */
24606397d6SJoel Stanley 	struct device		*dev;
25606397d6SJoel Stanley 	void __iomem		*base;
26606397d6SJoel Stanley 	struct clk		*clk;
274a851d71SJoel Stanley 	struct gpio_desc	*cfam_reset_gpio;
28606397d6SJoel Stanley };
29606397d6SJoel Stanley 
30606397d6SJoel Stanley #define to_fsi_master_aspeed(m) \
31606397d6SJoel Stanley 	container_of(m, struct fsi_master_aspeed, master)
32606397d6SJoel Stanley 
33606397d6SJoel Stanley /* Control register (size 0x400) */
34606397d6SJoel Stanley static const u32 ctrl_base = 0x80000000;
35606397d6SJoel Stanley 
36606397d6SJoel Stanley static const u32 fsi_base = 0xa0000000;
37606397d6SJoel Stanley 
38606397d6SJoel Stanley #define OPB_FSI_VER	0x00
39606397d6SJoel Stanley #define OPB_TRIGGER	0x04
40606397d6SJoel Stanley #define OPB_CTRL_BASE	0x08
41606397d6SJoel Stanley #define OPB_FSI_BASE	0x0c
42606397d6SJoel Stanley #define OPB_CLK_SYNC	0x3c
43606397d6SJoel Stanley #define OPB_IRQ_CLEAR	0x40
44606397d6SJoel Stanley #define OPB_IRQ_MASK	0x44
45606397d6SJoel Stanley #define OPB_IRQ_STATUS	0x48
46606397d6SJoel Stanley 
47606397d6SJoel Stanley #define OPB0_SELECT	0x10
48606397d6SJoel Stanley #define OPB0_RW		0x14
49606397d6SJoel Stanley #define OPB0_XFER_SIZE	0x18
50606397d6SJoel Stanley #define OPB0_FSI_ADDR	0x1c
51606397d6SJoel Stanley #define OPB0_FSI_DATA_W	0x20
52606397d6SJoel Stanley #define OPB0_STATUS	0x80
53606397d6SJoel Stanley #define OPB0_FSI_DATA_R	0x84
54606397d6SJoel Stanley 
55606397d6SJoel Stanley #define OPB0_WRITE_ORDER1	0x4c
56606397d6SJoel Stanley #define OPB0_WRITE_ORDER2	0x50
57606397d6SJoel Stanley #define OPB1_WRITE_ORDER1	0x54
58606397d6SJoel Stanley #define OPB1_WRITE_ORDER2	0x58
59606397d6SJoel Stanley #define OPB0_READ_ORDER1	0x5c
60606397d6SJoel Stanley #define OPB1_READ_ORDER2	0x60
61606397d6SJoel Stanley 
62606397d6SJoel Stanley #define OPB_RETRY_COUNTER	0x64
63606397d6SJoel Stanley 
64606397d6SJoel Stanley /* OPBn_STATUS */
65606397d6SJoel Stanley #define STATUS_HALFWORD_ACK	BIT(0)
66606397d6SJoel Stanley #define STATUS_FULLWORD_ACK	BIT(1)
67606397d6SJoel Stanley #define STATUS_ERR_ACK		BIT(2)
68606397d6SJoel Stanley #define STATUS_RETRY		BIT(3)
69606397d6SJoel Stanley #define STATUS_TIMEOUT		BIT(4)
70606397d6SJoel Stanley 
71606397d6SJoel Stanley /* OPB_IRQ_MASK */
72606397d6SJoel Stanley #define OPB1_XFER_ACK_EN BIT(17)
73606397d6SJoel Stanley #define OPB0_XFER_ACK_EN BIT(16)
74606397d6SJoel Stanley 
75606397d6SJoel Stanley /* OPB_RW */
76606397d6SJoel Stanley #define CMD_READ	BIT(0)
77606397d6SJoel Stanley #define CMD_WRITE	0
78606397d6SJoel Stanley 
79606397d6SJoel Stanley /* OPBx_XFER_SIZE */
80606397d6SJoel Stanley #define XFER_FULLWORD	(BIT(1) | BIT(0))
81606397d6SJoel Stanley #define XFER_HALFWORD	(BIT(0))
82606397d6SJoel Stanley #define XFER_BYTE	(0)
83606397d6SJoel Stanley 
84913b7373SJoel Stanley #define CREATE_TRACE_POINTS
85913b7373SJoel Stanley #include <trace/events/fsi_master_aspeed.h>
86913b7373SJoel Stanley 
87606397d6SJoel Stanley #define FSI_LINK_ENABLE_SETUP_TIME	10	/* in mS */
88606397d6SJoel Stanley 
894a80c201SJoel Stanley /* Run the bus at maximum speed by default */
904a80c201SJoel Stanley #define FSI_DIVISOR_DEFAULT            1
914a80c201SJoel Stanley #define FSI_DIVISOR_CABLED             2
924a80c201SJoel Stanley static u16 aspeed_fsi_divisor = FSI_DIVISOR_DEFAULT;
93add68951SJoel Stanley module_param_named(bus_div,aspeed_fsi_divisor, ushort, 0);
944a80c201SJoel Stanley 
951e2233d4SEddie James #define OPB_POLL_TIMEOUT		500
96606397d6SJoel Stanley 
__opb_write(struct fsi_master_aspeed * aspeed,u32 addr,u32 val,u32 transfer_size)97606397d6SJoel Stanley static int __opb_write(struct fsi_master_aspeed *aspeed, u32 addr,
98606397d6SJoel Stanley 		       u32 val, u32 transfer_size)
99606397d6SJoel Stanley {
100606397d6SJoel Stanley 	void __iomem *base = aspeed->base;
101606397d6SJoel Stanley 	u32 reg, status;
102606397d6SJoel Stanley 	int ret;
103606397d6SJoel Stanley 
1044134cb91SJoel Stanley 	/*
1054134cb91SJoel Stanley 	 * The ordering of these writes up until the trigger
1064134cb91SJoel Stanley 	 * write does not matter, so use writel_relaxed.
1074134cb91SJoel Stanley 	 */
1084134cb91SJoel Stanley 	writel_relaxed(CMD_WRITE, base + OPB0_RW);
1094134cb91SJoel Stanley 	writel_relaxed(transfer_size, base + OPB0_XFER_SIZE);
1104134cb91SJoel Stanley 	writel_relaxed(addr, base + OPB0_FSI_ADDR);
1114134cb91SJoel Stanley 	writel_relaxed(val, base + OPB0_FSI_DATA_W);
1124134cb91SJoel Stanley 	writel_relaxed(0x1, base + OPB_IRQ_CLEAR);
113606397d6SJoel Stanley 	writel(0x1, base + OPB_TRIGGER);
114606397d6SJoel Stanley 
115606397d6SJoel Stanley 	ret = readl_poll_timeout(base + OPB_IRQ_STATUS, reg,
116606397d6SJoel Stanley 				(reg & OPB0_XFER_ACK_EN) != 0,
117606397d6SJoel Stanley 				0, OPB_POLL_TIMEOUT);
118606397d6SJoel Stanley 
119606397d6SJoel Stanley 	status = readl(base + OPB0_STATUS);
120606397d6SJoel Stanley 
121913b7373SJoel Stanley 	trace_fsi_master_aspeed_opb_write(addr, val, transfer_size, status, reg);
122913b7373SJoel Stanley 
123606397d6SJoel Stanley 	/* Return error when poll timed out */
124606397d6SJoel Stanley 	if (ret)
125606397d6SJoel Stanley 		return ret;
126606397d6SJoel Stanley 
127606397d6SJoel Stanley 	/* Command failed, master will reset */
128606397d6SJoel Stanley 	if (status & STATUS_ERR_ACK)
129606397d6SJoel Stanley 		return -EIO;
130606397d6SJoel Stanley 
131606397d6SJoel Stanley 	return 0;
132606397d6SJoel Stanley }
133606397d6SJoel Stanley 
opb_writeb(struct fsi_master_aspeed * aspeed,u32 addr,u8 val)134606397d6SJoel Stanley static int opb_writeb(struct fsi_master_aspeed *aspeed, u32 addr, u8 val)
135606397d6SJoel Stanley {
136606397d6SJoel Stanley 	return __opb_write(aspeed, addr, val, XFER_BYTE);
137606397d6SJoel Stanley }
138606397d6SJoel Stanley 
opb_writew(struct fsi_master_aspeed * aspeed,u32 addr,__be16 val)139606397d6SJoel Stanley static int opb_writew(struct fsi_master_aspeed *aspeed, u32 addr, __be16 val)
140606397d6SJoel Stanley {
141606397d6SJoel Stanley 	return __opb_write(aspeed, addr, (__force u16)val, XFER_HALFWORD);
142606397d6SJoel Stanley }
143606397d6SJoel Stanley 
opb_writel(struct fsi_master_aspeed * aspeed,u32 addr,__be32 val)144606397d6SJoel Stanley static int opb_writel(struct fsi_master_aspeed *aspeed, u32 addr, __be32 val)
145606397d6SJoel Stanley {
146606397d6SJoel Stanley 	return __opb_write(aspeed, addr, (__force u32)val, XFER_FULLWORD);
147606397d6SJoel Stanley }
148606397d6SJoel Stanley 
__opb_read(struct fsi_master_aspeed * aspeed,uint32_t addr,u32 transfer_size,void * out)149606397d6SJoel Stanley static int __opb_read(struct fsi_master_aspeed *aspeed, uint32_t addr,
150606397d6SJoel Stanley 		      u32 transfer_size, void *out)
151606397d6SJoel Stanley {
152606397d6SJoel Stanley 	void __iomem *base = aspeed->base;
153606397d6SJoel Stanley 	u32 result, reg;
154606397d6SJoel Stanley 	int status, ret;
155606397d6SJoel Stanley 
1564134cb91SJoel Stanley 	/*
1574134cb91SJoel Stanley 	 * The ordering of these writes up until the trigger
1584134cb91SJoel Stanley 	 * write does not matter, so use writel_relaxed.
1594134cb91SJoel Stanley 	 */
1604134cb91SJoel Stanley 	writel_relaxed(CMD_READ, base + OPB0_RW);
1614134cb91SJoel Stanley 	writel_relaxed(transfer_size, base + OPB0_XFER_SIZE);
1624134cb91SJoel Stanley 	writel_relaxed(addr, base + OPB0_FSI_ADDR);
1634134cb91SJoel Stanley 	writel_relaxed(0x1, base + OPB_IRQ_CLEAR);
164606397d6SJoel Stanley 	writel(0x1, base + OPB_TRIGGER);
165606397d6SJoel Stanley 
166606397d6SJoel Stanley 	ret = readl_poll_timeout(base + OPB_IRQ_STATUS, reg,
167606397d6SJoel Stanley 			   (reg & OPB0_XFER_ACK_EN) != 0,
168606397d6SJoel Stanley 			   0, OPB_POLL_TIMEOUT);
169606397d6SJoel Stanley 
170606397d6SJoel Stanley 	status = readl(base + OPB0_STATUS);
171606397d6SJoel Stanley 
172606397d6SJoel Stanley 	result = readl(base + OPB0_FSI_DATA_R);
173606397d6SJoel Stanley 
174913b7373SJoel Stanley 	trace_fsi_master_aspeed_opb_read(addr, transfer_size, result,
175913b7373SJoel Stanley 			readl(base + OPB0_STATUS),
176913b7373SJoel Stanley 			reg);
177913b7373SJoel Stanley 
178606397d6SJoel Stanley 	/* Return error when poll timed out */
179606397d6SJoel Stanley 	if (ret)
180606397d6SJoel Stanley 		return ret;
181606397d6SJoel Stanley 
182606397d6SJoel Stanley 	/* Command failed, master will reset */
183606397d6SJoel Stanley 	if (status & STATUS_ERR_ACK)
184606397d6SJoel Stanley 		return -EIO;
185606397d6SJoel Stanley 
186606397d6SJoel Stanley 	if (out) {
187606397d6SJoel Stanley 		switch (transfer_size) {
188606397d6SJoel Stanley 		case XFER_BYTE:
189606397d6SJoel Stanley 			*(u8 *)out = result;
190606397d6SJoel Stanley 			break;
191606397d6SJoel Stanley 		case XFER_HALFWORD:
192606397d6SJoel Stanley 			*(u16 *)out = result;
193606397d6SJoel Stanley 			break;
194606397d6SJoel Stanley 		case XFER_FULLWORD:
195606397d6SJoel Stanley 			*(u32 *)out = result;
196606397d6SJoel Stanley 			break;
197606397d6SJoel Stanley 		default:
198606397d6SJoel Stanley 			return -EINVAL;
199606397d6SJoel Stanley 		}
200606397d6SJoel Stanley 
201606397d6SJoel Stanley 	}
202606397d6SJoel Stanley 
203606397d6SJoel Stanley 	return 0;
204606397d6SJoel Stanley }
205606397d6SJoel Stanley 
opb_readl(struct fsi_master_aspeed * aspeed,uint32_t addr,__be32 * out)206606397d6SJoel Stanley static int opb_readl(struct fsi_master_aspeed *aspeed, uint32_t addr, __be32 *out)
207606397d6SJoel Stanley {
208606397d6SJoel Stanley 	return __opb_read(aspeed, addr, XFER_FULLWORD, out);
209606397d6SJoel Stanley }
210606397d6SJoel Stanley 
opb_readw(struct fsi_master_aspeed * aspeed,uint32_t addr,__be16 * out)211606397d6SJoel Stanley static int opb_readw(struct fsi_master_aspeed *aspeed, uint32_t addr, __be16 *out)
212606397d6SJoel Stanley {
213606397d6SJoel Stanley 	return __opb_read(aspeed, addr, XFER_HALFWORD, (void *)out);
214606397d6SJoel Stanley }
215606397d6SJoel Stanley 
opb_readb(struct fsi_master_aspeed * aspeed,uint32_t addr,u8 * out)216606397d6SJoel Stanley static int opb_readb(struct fsi_master_aspeed *aspeed, uint32_t addr, u8 *out)
217606397d6SJoel Stanley {
218606397d6SJoel Stanley 	return __opb_read(aspeed, addr, XFER_BYTE, (void *)out);
219606397d6SJoel Stanley }
220606397d6SJoel Stanley 
check_errors(struct fsi_master_aspeed * aspeed,int err)221606397d6SJoel Stanley static int check_errors(struct fsi_master_aspeed *aspeed, int err)
222606397d6SJoel Stanley {
223606397d6SJoel Stanley 	int ret;
224606397d6SJoel Stanley 
225913b7373SJoel Stanley 	if (trace_fsi_master_aspeed_opb_error_enabled()) {
226913b7373SJoel Stanley 		__be32 mresp0, mstap0, mesrb0;
227913b7373SJoel Stanley 
228913b7373SJoel Stanley 		opb_readl(aspeed, ctrl_base + FSI_MRESP0, &mresp0);
229913b7373SJoel Stanley 		opb_readl(aspeed, ctrl_base + FSI_MSTAP0, &mstap0);
230913b7373SJoel Stanley 		opb_readl(aspeed, ctrl_base + FSI_MESRB0, &mesrb0);
231913b7373SJoel Stanley 
232913b7373SJoel Stanley 		trace_fsi_master_aspeed_opb_error(
233913b7373SJoel Stanley 				be32_to_cpu(mresp0),
234913b7373SJoel Stanley 				be32_to_cpu(mstap0),
235913b7373SJoel Stanley 				be32_to_cpu(mesrb0));
236913b7373SJoel Stanley 	}
237913b7373SJoel Stanley 
238606397d6SJoel Stanley 	if (err == -EIO) {
239606397d6SJoel Stanley 		/* Check MAEB (0x70) ? */
240606397d6SJoel Stanley 
241606397d6SJoel Stanley 		/* Then clear errors in master */
242606397d6SJoel Stanley 		ret = opb_writel(aspeed, ctrl_base + FSI_MRESP0,
243606397d6SJoel Stanley 				cpu_to_be32(FSI_MRESP_RST_ALL_MASTER));
244606397d6SJoel Stanley 		if (ret) {
245606397d6SJoel Stanley 			/* TODO: log? return different code? */
246606397d6SJoel Stanley 			return ret;
247606397d6SJoel Stanley 		}
248606397d6SJoel Stanley 		/* TODO: confirm that 0x70 was okay */
249606397d6SJoel Stanley 	}
250606397d6SJoel Stanley 
251606397d6SJoel Stanley 	/* This will pass through timeout errors */
252606397d6SJoel Stanley 	return err;
253606397d6SJoel Stanley }
254606397d6SJoel Stanley 
aspeed_master_read(struct fsi_master * master,int link,uint8_t id,uint32_t addr,void * val,size_t size)255606397d6SJoel Stanley static int aspeed_master_read(struct fsi_master *master, int link,
256606397d6SJoel Stanley 			uint8_t id, uint32_t addr, void *val, size_t size)
257606397d6SJoel Stanley {
258606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
259606397d6SJoel Stanley 	int ret;
260606397d6SJoel Stanley 
2616e0ef7d2SEddie James 	if (id > 0x3)
262606397d6SJoel Stanley 		return -EINVAL;
263606397d6SJoel Stanley 
2646e0ef7d2SEddie James 	addr |= id << 21;
265606397d6SJoel Stanley 	addr += link * FSI_HUB_LINK_SIZE;
266606397d6SJoel Stanley 
267dfd7f2c1SEddie James 	mutex_lock(&aspeed->lock);
268dfd7f2c1SEddie James 
269606397d6SJoel Stanley 	switch (size) {
270606397d6SJoel Stanley 	case 1:
271606397d6SJoel Stanley 		ret = opb_readb(aspeed, fsi_base + addr, val);
272606397d6SJoel Stanley 		break;
273606397d6SJoel Stanley 	case 2:
274606397d6SJoel Stanley 		ret = opb_readw(aspeed, fsi_base + addr, val);
275606397d6SJoel Stanley 		break;
276606397d6SJoel Stanley 	case 4:
277606397d6SJoel Stanley 		ret = opb_readl(aspeed, fsi_base + addr, val);
278606397d6SJoel Stanley 		break;
279606397d6SJoel Stanley 	default:
280dfd7f2c1SEddie James 		ret = -EINVAL;
281dfd7f2c1SEddie James 		goto done;
282606397d6SJoel Stanley 	}
283606397d6SJoel Stanley 
284606397d6SJoel Stanley 	ret = check_errors(aspeed, ret);
285dfd7f2c1SEddie James done:
286dfd7f2c1SEddie James 	mutex_unlock(&aspeed->lock);
287606397d6SJoel Stanley 	return ret;
288606397d6SJoel Stanley }
289606397d6SJoel Stanley 
aspeed_master_write(struct fsi_master * master,int link,uint8_t id,uint32_t addr,const void * val,size_t size)290606397d6SJoel Stanley static int aspeed_master_write(struct fsi_master *master, int link,
291606397d6SJoel Stanley 			uint8_t id, uint32_t addr, const void *val, size_t size)
292606397d6SJoel Stanley {
293606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
294606397d6SJoel Stanley 	int ret;
295606397d6SJoel Stanley 
2966e0ef7d2SEddie James 	if (id > 0x3)
297606397d6SJoel Stanley 		return -EINVAL;
298606397d6SJoel Stanley 
2996e0ef7d2SEddie James 	addr |= id << 21;
300606397d6SJoel Stanley 	addr += link * FSI_HUB_LINK_SIZE;
301606397d6SJoel Stanley 
302dfd7f2c1SEddie James 	mutex_lock(&aspeed->lock);
303dfd7f2c1SEddie James 
304606397d6SJoel Stanley 	switch (size) {
305606397d6SJoel Stanley 	case 1:
306606397d6SJoel Stanley 		ret = opb_writeb(aspeed, fsi_base + addr, *(u8 *)val);
307606397d6SJoel Stanley 		break;
308606397d6SJoel Stanley 	case 2:
309606397d6SJoel Stanley 		ret = opb_writew(aspeed, fsi_base + addr, *(__be16 *)val);
310606397d6SJoel Stanley 		break;
311606397d6SJoel Stanley 	case 4:
312606397d6SJoel Stanley 		ret = opb_writel(aspeed, fsi_base + addr, *(__be32 *)val);
313606397d6SJoel Stanley 		break;
314606397d6SJoel Stanley 	default:
315dfd7f2c1SEddie James 		ret = -EINVAL;
316dfd7f2c1SEddie James 		goto done;
317606397d6SJoel Stanley 	}
318606397d6SJoel Stanley 
319606397d6SJoel Stanley 	ret = check_errors(aspeed, ret);
320dfd7f2c1SEddie James done:
321dfd7f2c1SEddie James 	mutex_unlock(&aspeed->lock);
322606397d6SJoel Stanley 	return ret;
323606397d6SJoel Stanley }
324606397d6SJoel Stanley 
aspeed_master_link_enable(struct fsi_master * master,int link,bool enable)32504635a30SEddie James static int aspeed_master_link_enable(struct fsi_master *master, int link,
32604635a30SEddie James 				     bool enable)
327606397d6SJoel Stanley {
328606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed = to_fsi_master_aspeed(master);
329606397d6SJoel Stanley 	int idx, bit, ret;
330a1d5ce11SEddie James 	__be32 reg;
331606397d6SJoel Stanley 
332606397d6SJoel Stanley 	idx = link / 32;
333606397d6SJoel Stanley 	bit = link % 32;
334606397d6SJoel Stanley 
335606397d6SJoel Stanley 	reg = cpu_to_be32(0x80000000 >> bit);
336606397d6SJoel Stanley 
337dfd7f2c1SEddie James 	mutex_lock(&aspeed->lock);
338dfd7f2c1SEddie James 
339dfd7f2c1SEddie James 	if (!enable) {
340dfd7f2c1SEddie James 		ret = opb_writel(aspeed, ctrl_base + FSI_MCENP0 + (4 * idx), reg);
341dfd7f2c1SEddie James 		goto done;
342dfd7f2c1SEddie James 	}
34304635a30SEddie James 
344606397d6SJoel Stanley 	ret = opb_writel(aspeed, ctrl_base + FSI_MSENP0 + (4 * idx), reg);
345606397d6SJoel Stanley 	if (ret)
346dfd7f2c1SEddie James 		goto done;
347606397d6SJoel Stanley 
348606397d6SJoel Stanley 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
349dfd7f2c1SEddie James done:
350dfd7f2c1SEddie James 	mutex_unlock(&aspeed->lock);
351dfd7f2c1SEddie James 	return ret;
352606397d6SJoel Stanley }
353606397d6SJoel Stanley 
aspeed_master_term(struct fsi_master * master,int link,uint8_t id)354606397d6SJoel Stanley static int aspeed_master_term(struct fsi_master *master, int link, uint8_t id)
355606397d6SJoel Stanley {
356606397d6SJoel Stanley 	uint32_t addr;
357606397d6SJoel Stanley 	__be32 cmd;
358606397d6SJoel Stanley 
359606397d6SJoel Stanley 	addr = 0x4;
360606397d6SJoel Stanley 	cmd = cpu_to_be32(0xecc00000);
361606397d6SJoel Stanley 
362606397d6SJoel Stanley 	return aspeed_master_write(master, link, id, addr, &cmd, 4);
363606397d6SJoel Stanley }
364606397d6SJoel Stanley 
aspeed_master_break(struct fsi_master * master,int link)365606397d6SJoel Stanley static int aspeed_master_break(struct fsi_master *master, int link)
366606397d6SJoel Stanley {
367606397d6SJoel Stanley 	uint32_t addr;
368606397d6SJoel Stanley 	__be32 cmd;
369606397d6SJoel Stanley 
370606397d6SJoel Stanley 	addr = 0x0;
371606397d6SJoel Stanley 	cmd = cpu_to_be32(0xc0de0000);
372606397d6SJoel Stanley 
373606397d6SJoel Stanley 	return aspeed_master_write(master, link, 0, addr, &cmd, 4);
374606397d6SJoel Stanley }
375606397d6SJoel Stanley 
aspeed_master_release(struct device * dev)376606397d6SJoel Stanley static void aspeed_master_release(struct device *dev)
377606397d6SJoel Stanley {
378606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed =
379d5d8dfb0SEddie James 		to_fsi_master_aspeed(to_fsi_master(dev));
380606397d6SJoel Stanley 
381606397d6SJoel Stanley 	kfree(aspeed);
382606397d6SJoel Stanley }
383606397d6SJoel Stanley 
384606397d6SJoel Stanley /* mmode encoders */
fsi_mmode_crs0(u32 x)385606397d6SJoel Stanley static inline u32 fsi_mmode_crs0(u32 x)
386606397d6SJoel Stanley {
387606397d6SJoel Stanley 	return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
388606397d6SJoel Stanley }
389606397d6SJoel Stanley 
fsi_mmode_crs1(u32 x)390606397d6SJoel Stanley static inline u32 fsi_mmode_crs1(u32 x)
391606397d6SJoel Stanley {
392606397d6SJoel Stanley 	return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
393606397d6SJoel Stanley }
394606397d6SJoel Stanley 
aspeed_master_init(struct fsi_master_aspeed * aspeed)395606397d6SJoel Stanley static int aspeed_master_init(struct fsi_master_aspeed *aspeed)
396606397d6SJoel Stanley {
397606397d6SJoel Stanley 	__be32 reg;
398606397d6SJoel Stanley 
399606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
400606397d6SJoel Stanley 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
401606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
402606397d6SJoel Stanley 
403606397d6SJoel Stanley 	/* Initialize the MFSI (hub master) engine */
404606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
405606397d6SJoel Stanley 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
406606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
407606397d6SJoel Stanley 
408606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
409606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MECTRL, reg);
410606397d6SJoel Stanley 
411606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MMODE_ECRC | FSI_MMODE_EPC | FSI_MMODE_RELA
4124a80c201SJoel Stanley 			| fsi_mmode_crs0(aspeed_fsi_divisor)
4134a80c201SJoel Stanley 			| fsi_mmode_crs1(aspeed_fsi_divisor)
414606397d6SJoel Stanley 			| FSI_MMODE_P8_TO_LSB);
4154a80c201SJoel Stanley 	dev_info(aspeed->dev, "mmode set to %08x (divisor %d)\n",
4164a80c201SJoel Stanley 			be32_to_cpu(reg), aspeed_fsi_divisor);
417606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MMODE, reg);
418606397d6SJoel Stanley 
419606397d6SJoel Stanley 	reg = cpu_to_be32(0xffff0000);
420606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MDLYR, reg);
421606397d6SJoel Stanley 
422606397d6SJoel Stanley 	reg = cpu_to_be32(~0);
423606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MSENP0, reg);
424606397d6SJoel Stanley 
425606397d6SJoel Stanley 	/* Leave enabled long enough for master logic to set up */
426606397d6SJoel Stanley 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
427606397d6SJoel Stanley 
428606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MCENP0, reg);
429606397d6SJoel Stanley 
430606397d6SJoel Stanley 	opb_readl(aspeed, ctrl_base + FSI_MAEB, NULL);
431606397d6SJoel Stanley 
432606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
433606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MRESP0, reg);
434606397d6SJoel Stanley 
435606397d6SJoel Stanley 	opb_readl(aspeed, ctrl_base + FSI_MLEVP0, NULL);
436606397d6SJoel Stanley 
437606397d6SJoel Stanley 	/* Reset the master bridge */
438606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MRESB_RST_GEN);
439606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MRESB0, reg);
440606397d6SJoel Stanley 
441606397d6SJoel Stanley 	reg = cpu_to_be32(FSI_MRESB_RST_ERR);
442606397d6SJoel Stanley 	opb_writel(aspeed, ctrl_base + FSI_MRESB0, reg);
443606397d6SJoel Stanley 
444606397d6SJoel Stanley 	return 0;
445606397d6SJoel Stanley }
446606397d6SJoel Stanley 
cfam_reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4474a851d71SJoel Stanley static ssize_t cfam_reset_store(struct device *dev, struct device_attribute *attr,
4484a851d71SJoel Stanley 				const char *buf, size_t count)
4494a851d71SJoel Stanley {
4504a851d71SJoel Stanley 	struct fsi_master_aspeed *aspeed = dev_get_drvdata(dev);
4514a851d71SJoel Stanley 
452f2af60bbSEddie James 	trace_fsi_master_aspeed_cfam_reset(true);
453dfd7f2c1SEddie James 	mutex_lock(&aspeed->lock);
4544a851d71SJoel Stanley 	gpiod_set_value(aspeed->cfam_reset_gpio, 1);
4554a851d71SJoel Stanley 	usleep_range(900, 1000);
4564a851d71SJoel Stanley 	gpiod_set_value(aspeed->cfam_reset_gpio, 0);
457*52300909SEddie James 	usleep_range(900, 1000);
458*52300909SEddie James 	opb_writel(aspeed, ctrl_base + FSI_MRESP0, cpu_to_be32(FSI_MRESP_RST_ALL_MASTER));
459dfd7f2c1SEddie James 	mutex_unlock(&aspeed->lock);
460f2af60bbSEddie James 	trace_fsi_master_aspeed_cfam_reset(false);
4614a851d71SJoel Stanley 
4624a851d71SJoel Stanley 	return count;
4634a851d71SJoel Stanley }
4644a851d71SJoel Stanley 
4654a851d71SJoel Stanley static DEVICE_ATTR(cfam_reset, 0200, NULL, cfam_reset_store);
4664a851d71SJoel Stanley 
setup_cfam_reset(struct fsi_master_aspeed * aspeed)4674a851d71SJoel Stanley static int setup_cfam_reset(struct fsi_master_aspeed *aspeed)
4684a851d71SJoel Stanley {
4694a851d71SJoel Stanley 	struct device *dev = aspeed->dev;
4704a851d71SJoel Stanley 	struct gpio_desc *gpio;
4714a851d71SJoel Stanley 	int rc;
4724a851d71SJoel Stanley 
4734a851d71SJoel Stanley 	gpio = devm_gpiod_get_optional(dev, "cfam-reset", GPIOD_OUT_LOW);
4744a851d71SJoel Stanley 	if (IS_ERR(gpio))
4754a851d71SJoel Stanley 		return PTR_ERR(gpio);
4764a851d71SJoel Stanley 	if (!gpio)
4774a851d71SJoel Stanley 		return 0;
4784a851d71SJoel Stanley 
4794a851d71SJoel Stanley 	aspeed->cfam_reset_gpio = gpio;
4804a851d71SJoel Stanley 
4814a851d71SJoel Stanley 	rc = device_create_file(dev, &dev_attr_cfam_reset);
4824a851d71SJoel Stanley 	if (rc) {
4834a851d71SJoel Stanley 		devm_gpiod_put(dev, gpio);
4844a851d71SJoel Stanley 		return rc;
4854a851d71SJoel Stanley 	}
4864a851d71SJoel Stanley 
4874a851d71SJoel Stanley 	return 0;
4884a851d71SJoel Stanley }
4894a851d71SJoel Stanley 
tacoma_cabled_fsi_fixup(struct device * dev)490f369a29bSJoel Stanley static int tacoma_cabled_fsi_fixup(struct device *dev)
491f369a29bSJoel Stanley {
492f369a29bSJoel Stanley 	struct gpio_desc *routing_gpio, *mux_gpio;
493f369a29bSJoel Stanley 	int gpio;
494f369a29bSJoel Stanley 
495f369a29bSJoel Stanley 	/*
496f369a29bSJoel Stanley 	 * The routing GPIO is a jumper indicating we should mux for the
497f369a29bSJoel Stanley 	 * externally connected FSI cable.
498f369a29bSJoel Stanley 	 */
499f369a29bSJoel Stanley 	routing_gpio = devm_gpiod_get_optional(dev, "fsi-routing",
500f369a29bSJoel Stanley 			GPIOD_IN | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
501f369a29bSJoel Stanley 	if (IS_ERR(routing_gpio))
502f369a29bSJoel Stanley 		return PTR_ERR(routing_gpio);
503f369a29bSJoel Stanley 	if (!routing_gpio)
504f369a29bSJoel Stanley 		return 0;
505f369a29bSJoel Stanley 
506f369a29bSJoel Stanley 	mux_gpio = devm_gpiod_get_optional(dev, "fsi-mux", GPIOD_ASIS);
507f369a29bSJoel Stanley 	if (IS_ERR(mux_gpio))
508f369a29bSJoel Stanley 		return PTR_ERR(mux_gpio);
509f369a29bSJoel Stanley 	if (!mux_gpio)
510f369a29bSJoel Stanley 		return 0;
511f369a29bSJoel Stanley 
512f369a29bSJoel Stanley 	gpio = gpiod_get_value(routing_gpio);
513f369a29bSJoel Stanley 	if (gpio < 0)
514f369a29bSJoel Stanley 		return gpio;
515f369a29bSJoel Stanley 
516f369a29bSJoel Stanley 	/* If the routing GPIO is high we should set the mux to low. */
517f369a29bSJoel Stanley 	if (gpio) {
5184a80c201SJoel Stanley 		/*
5194a80c201SJoel Stanley 		 * Cable signal integrity means we should run the bus
520add68951SJoel Stanley 		 * slightly slower. Do not override if a kernel param
521add68951SJoel Stanley 		 * has already overridden.
5224a80c201SJoel Stanley 		 */
523add68951SJoel Stanley 		if (aspeed_fsi_divisor == FSI_DIVISOR_DEFAULT)
5244a80c201SJoel Stanley 			aspeed_fsi_divisor = FSI_DIVISOR_CABLED;
525add68951SJoel Stanley 
526f369a29bSJoel Stanley 		gpiod_direction_output(mux_gpio, 0);
527f369a29bSJoel Stanley 		dev_info(dev, "FSI configured for external cable\n");
528f369a29bSJoel Stanley 	} else {
529f369a29bSJoel Stanley 		gpiod_direction_output(mux_gpio, 1);
530f369a29bSJoel Stanley 	}
531f369a29bSJoel Stanley 
532f369a29bSJoel Stanley 	devm_gpiod_put(dev, routing_gpio);
533f369a29bSJoel Stanley 
534f369a29bSJoel Stanley 	return 0;
535f369a29bSJoel Stanley }
536f369a29bSJoel Stanley 
fsi_master_aspeed_probe(struct platform_device * pdev)537606397d6SJoel Stanley static int fsi_master_aspeed_probe(struct platform_device *pdev)
538606397d6SJoel Stanley {
539606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed;
540606397d6SJoel Stanley 	int rc, links, reg;
541606397d6SJoel Stanley 	__be32 raw;
542606397d6SJoel Stanley 
543f369a29bSJoel Stanley 	rc = tacoma_cabled_fsi_fixup(&pdev->dev);
544f369a29bSJoel Stanley 	if (rc) {
545f369a29bSJoel Stanley 		dev_err(&pdev->dev, "Tacoma FSI cable fixup failed\n");
546f369a29bSJoel Stanley 		return rc;
547f369a29bSJoel Stanley 	}
548f369a29bSJoel Stanley 
54983ba7e89SChristophe JAILLET 	aspeed = kzalloc(sizeof(*aspeed), GFP_KERNEL);
550606397d6SJoel Stanley 	if (!aspeed)
551606397d6SJoel Stanley 		return -ENOMEM;
552606397d6SJoel Stanley 
553606397d6SJoel Stanley 	aspeed->dev = &pdev->dev;
554606397d6SJoel Stanley 
555a3469912SYangtao Li 	aspeed->base = devm_platform_ioremap_resource(pdev, 0);
55683ba7e89SChristophe JAILLET 	if (IS_ERR(aspeed->base)) {
55783ba7e89SChristophe JAILLET 		rc = PTR_ERR(aspeed->base);
55883ba7e89SChristophe JAILLET 		goto err_free_aspeed;
55983ba7e89SChristophe JAILLET 	}
560606397d6SJoel Stanley 
561606397d6SJoel Stanley 	aspeed->clk = devm_clk_get(aspeed->dev, NULL);
562606397d6SJoel Stanley 	if (IS_ERR(aspeed->clk)) {
563606397d6SJoel Stanley 		dev_err(aspeed->dev, "couldn't get clock\n");
56483ba7e89SChristophe JAILLET 		rc = PTR_ERR(aspeed->clk);
56583ba7e89SChristophe JAILLET 		goto err_free_aspeed;
566606397d6SJoel Stanley 	}
567606397d6SJoel Stanley 	rc = clk_prepare_enable(aspeed->clk);
568606397d6SJoel Stanley 	if (rc) {
569606397d6SJoel Stanley 		dev_err(aspeed->dev, "couldn't enable clock\n");
57083ba7e89SChristophe JAILLET 		goto err_free_aspeed;
571606397d6SJoel Stanley 	}
572606397d6SJoel Stanley 
5734a851d71SJoel Stanley 	rc = setup_cfam_reset(aspeed);
5744a851d71SJoel Stanley 	if (rc) {
5754a851d71SJoel Stanley 		dev_err(&pdev->dev, "CFAM reset GPIO setup failed\n");
5764a851d71SJoel Stanley 	}
5774a851d71SJoel Stanley 
578606397d6SJoel Stanley 	writel(0x1, aspeed->base + OPB_CLK_SYNC);
579606397d6SJoel Stanley 	writel(OPB1_XFER_ACK_EN | OPB0_XFER_ACK_EN,
580606397d6SJoel Stanley 			aspeed->base + OPB_IRQ_MASK);
581606397d6SJoel Stanley 
582606397d6SJoel Stanley 	/* TODO: determine an appropriate value */
583606397d6SJoel Stanley 	writel(0x10, aspeed->base + OPB_RETRY_COUNTER);
584606397d6SJoel Stanley 
585606397d6SJoel Stanley 	writel(ctrl_base, aspeed->base + OPB_CTRL_BASE);
586606397d6SJoel Stanley 	writel(fsi_base, aspeed->base + OPB_FSI_BASE);
587606397d6SJoel Stanley 
588606397d6SJoel Stanley 	/* Set read data order */
5895e502299SAndrew Jeffery 	writel(0x00030b1b, aspeed->base + OPB0_READ_ORDER1);
590606397d6SJoel Stanley 
591606397d6SJoel Stanley 	/* Set write data order */
5925e502299SAndrew Jeffery 	writel(0x0011101b, aspeed->base + OPB0_WRITE_ORDER1);
5935e502299SAndrew Jeffery 	writel(0x0c330f3f, aspeed->base + OPB0_WRITE_ORDER2);
594606397d6SJoel Stanley 
595606397d6SJoel Stanley 	/*
596606397d6SJoel Stanley 	 * Select OPB0 for all operations.
597606397d6SJoel Stanley 	 * Will need to be reworked when enabling DMA or anything that uses
598606397d6SJoel Stanley 	 * OPB1.
599606397d6SJoel Stanley 	 */
600606397d6SJoel Stanley 	writel(0x1, aspeed->base + OPB0_SELECT);
601606397d6SJoel Stanley 
602606397d6SJoel Stanley 	rc = opb_readl(aspeed, ctrl_base + FSI_MVER, &raw);
603606397d6SJoel Stanley 	if (rc) {
604606397d6SJoel Stanley 		dev_err(&pdev->dev, "failed to read hub version\n");
60583ba7e89SChristophe JAILLET 		goto err_release;
606606397d6SJoel Stanley 	}
607606397d6SJoel Stanley 
608606397d6SJoel Stanley 	reg = be32_to_cpu(raw);
609606397d6SJoel Stanley 	links = (reg >> 8) & 0xff;
610606397d6SJoel Stanley 	dev_info(&pdev->dev, "hub version %08x (%d links)\n", reg, links);
611606397d6SJoel Stanley 
612606397d6SJoel Stanley 	aspeed->master.dev.parent = &pdev->dev;
613606397d6SJoel Stanley 	aspeed->master.dev.release = aspeed_master_release;
614606397d6SJoel Stanley 	aspeed->master.dev.of_node = of_node_get(dev_of_node(&pdev->dev));
615606397d6SJoel Stanley 
616606397d6SJoel Stanley 	aspeed->master.n_links = links;
617606397d6SJoel Stanley 	aspeed->master.read = aspeed_master_read;
618606397d6SJoel Stanley 	aspeed->master.write = aspeed_master_write;
619606397d6SJoel Stanley 	aspeed->master.send_break = aspeed_master_break;
620606397d6SJoel Stanley 	aspeed->master.term = aspeed_master_term;
621606397d6SJoel Stanley 	aspeed->master.link_enable = aspeed_master_link_enable;
622606397d6SJoel Stanley 
623606397d6SJoel Stanley 	dev_set_drvdata(&pdev->dev, aspeed);
624606397d6SJoel Stanley 
625dfd7f2c1SEddie James 	mutex_init(&aspeed->lock);
626606397d6SJoel Stanley 	aspeed_master_init(aspeed);
627606397d6SJoel Stanley 
628606397d6SJoel Stanley 	rc = fsi_master_register(&aspeed->master);
629606397d6SJoel Stanley 	if (rc)
630606397d6SJoel Stanley 		goto err_release;
631606397d6SJoel Stanley 
632606397d6SJoel Stanley 	/* At this point, fsi_master_register performs the device_initialize(),
633606397d6SJoel Stanley 	 * and holds the sole reference on master.dev. This means the device
634606397d6SJoel Stanley 	 * will be freed (via ->release) during any subsequent call to
635606397d6SJoel Stanley 	 * fsi_master_unregister.  We add our own reference to it here, so we
636606397d6SJoel Stanley 	 * can perform cleanup (in _remove()) without it being freed before
637606397d6SJoel Stanley 	 * we're ready.
638606397d6SJoel Stanley 	 */
639606397d6SJoel Stanley 	get_device(&aspeed->master.dev);
640606397d6SJoel Stanley 	return 0;
641606397d6SJoel Stanley 
642606397d6SJoel Stanley err_release:
643606397d6SJoel Stanley 	clk_disable_unprepare(aspeed->clk);
64483ba7e89SChristophe JAILLET err_free_aspeed:
64583ba7e89SChristophe JAILLET 	kfree(aspeed);
646606397d6SJoel Stanley 	return rc;
647606397d6SJoel Stanley }
648606397d6SJoel Stanley 
fsi_master_aspeed_remove(struct platform_device * pdev)649606397d6SJoel Stanley static int fsi_master_aspeed_remove(struct platform_device *pdev)
650606397d6SJoel Stanley {
651606397d6SJoel Stanley 	struct fsi_master_aspeed *aspeed = platform_get_drvdata(pdev);
652606397d6SJoel Stanley 
653606397d6SJoel Stanley 	fsi_master_unregister(&aspeed->master);
654606397d6SJoel Stanley 	clk_disable_unprepare(aspeed->clk);
655606397d6SJoel Stanley 
656606397d6SJoel Stanley 	return 0;
657606397d6SJoel Stanley }
658606397d6SJoel Stanley 
659606397d6SJoel Stanley static const struct of_device_id fsi_master_aspeed_match[] = {
660606397d6SJoel Stanley 	{ .compatible = "aspeed,ast2600-fsi-master" },
661606397d6SJoel Stanley 	{ },
662606397d6SJoel Stanley };
66319a52178SZou Wei MODULE_DEVICE_TABLE(of, fsi_master_aspeed_match);
664606397d6SJoel Stanley 
665606397d6SJoel Stanley static struct platform_driver fsi_master_aspeed_driver = {
666606397d6SJoel Stanley 	.driver = {
667606397d6SJoel Stanley 		.name		= "fsi-master-aspeed",
668606397d6SJoel Stanley 		.of_match_table	= fsi_master_aspeed_match,
669606397d6SJoel Stanley 	},
670606397d6SJoel Stanley 	.probe	= fsi_master_aspeed_probe,
671606397d6SJoel Stanley 	.remove = fsi_master_aspeed_remove,
672606397d6SJoel Stanley };
673606397d6SJoel Stanley 
674606397d6SJoel Stanley module_platform_driver(fsi_master_aspeed_driver);
675606397d6SJoel Stanley MODULE_LICENSE("GPL");
676