xref: /OK3568_Linux_fs/kernel/drivers/fsi/fsi-master-hub.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * FSI hub master driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) IBM Corporation 2016
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/fsi.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "fsi-master.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define FSI_ENGID_HUB_MASTER		0x1c
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define FSI_LINK_ENABLE_SETUP_TIME	10	/* in mS */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun  * FSI hub master support
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * A hub master increases the number of potential target devices that the
24*4882a593Smuzhiyun  * primary FSI master can access. For each link a primary master supports,
25*4882a593Smuzhiyun  * each of those links can in turn be chained to a hub master with multiple
26*4882a593Smuzhiyun  * links of its own.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * The hub is controlled by a set of control registers exposed as a regular fsi
29*4882a593Smuzhiyun  * device (the hub->upstream device), and provides access to the downstream FSI
30*4882a593Smuzhiyun  * bus as through an address range on the slave itself (->addr and ->size).
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * [This differs from "cascaded" masters, which expose the entire downstream
33*4882a593Smuzhiyun  * bus entirely through the fsi device address range, and so have a smaller
34*4882a593Smuzhiyun  * accessible address space.]
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun struct fsi_master_hub {
37*4882a593Smuzhiyun 	struct fsi_master	master;
38*4882a593Smuzhiyun 	struct fsi_device	*upstream;
39*4882a593Smuzhiyun 	uint32_t		addr, size;	/* slave-relative addr of */
40*4882a593Smuzhiyun 						/* master address space */
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define to_fsi_master_hub(m) container_of(m, struct fsi_master_hub, master)
44*4882a593Smuzhiyun 
hub_master_read(struct fsi_master * master,int link,uint8_t id,uint32_t addr,void * val,size_t size)45*4882a593Smuzhiyun static int hub_master_read(struct fsi_master *master, int link,
46*4882a593Smuzhiyun 			uint8_t id, uint32_t addr, void *val, size_t size)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (id != 0)
51*4882a593Smuzhiyun 		return -EINVAL;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
54*4882a593Smuzhiyun 	return fsi_slave_read(hub->upstream->slave, addr, val, size);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
hub_master_write(struct fsi_master * master,int link,uint8_t id,uint32_t addr,const void * val,size_t size)57*4882a593Smuzhiyun static int hub_master_write(struct fsi_master *master, int link,
58*4882a593Smuzhiyun 			uint8_t id, uint32_t addr, const void *val, size_t size)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (id != 0)
63*4882a593Smuzhiyun 		return -EINVAL;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
66*4882a593Smuzhiyun 	return fsi_slave_write(hub->upstream->slave, addr, val, size);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
hub_master_break(struct fsi_master * master,int link)69*4882a593Smuzhiyun static int hub_master_break(struct fsi_master *master, int link)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	uint32_t addr;
72*4882a593Smuzhiyun 	__be32 cmd;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	addr = 0x4;
75*4882a593Smuzhiyun 	cmd = cpu_to_be32(0xc0de0000);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return hub_master_write(master, link, 0, addr, &cmd, sizeof(cmd));
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
hub_master_link_enable(struct fsi_master * master,int link,bool enable)80*4882a593Smuzhiyun static int hub_master_link_enable(struct fsi_master *master, int link,
81*4882a593Smuzhiyun 				  bool enable)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
84*4882a593Smuzhiyun 	int idx, bit;
85*4882a593Smuzhiyun 	__be32 reg;
86*4882a593Smuzhiyun 	int rc;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	idx = link / 32;
89*4882a593Smuzhiyun 	bit = link % 32;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	reg = cpu_to_be32(0x80000000 >> bit);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (!enable)
94*4882a593Smuzhiyun 		return fsi_device_write(hub->upstream, FSI_MCENP0 + (4 * idx),
95*4882a593Smuzhiyun 					&reg, 4);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	rc = fsi_device_write(hub->upstream, FSI_MSENP0 + (4 * idx), &reg, 4);
98*4882a593Smuzhiyun 	if (rc)
99*4882a593Smuzhiyun 		return rc;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	return 0;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
hub_master_release(struct device * dev)106*4882a593Smuzhiyun static void hub_master_release(struct device *dev)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct fsi_master_hub *hub = to_fsi_master_hub(dev_to_fsi_master(dev));
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	kfree(hub);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* mmode encoders */
fsi_mmode_crs0(u32 x)114*4882a593Smuzhiyun static inline u32 fsi_mmode_crs0(u32 x)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
fsi_mmode_crs1(u32 x)119*4882a593Smuzhiyun static inline u32 fsi_mmode_crs1(u32 x)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
hub_master_init(struct fsi_master_hub * hub)124*4882a593Smuzhiyun static int hub_master_init(struct fsi_master_hub *hub)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct fsi_device *dev = hub->upstream;
127*4882a593Smuzhiyun 	__be32 reg;
128*4882a593Smuzhiyun 	int rc;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
131*4882a593Smuzhiyun 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
132*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
133*4882a593Smuzhiyun 	if (rc)
134*4882a593Smuzhiyun 		return rc;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* Initialize the MFSI (hub master) engine */
137*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
138*4882a593Smuzhiyun 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
139*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
140*4882a593Smuzhiyun 	if (rc)
141*4882a593Smuzhiyun 		return rc;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
144*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MECTRL, &reg, sizeof(reg));
145*4882a593Smuzhiyun 	if (rc)
146*4882a593Smuzhiyun 		return rc;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
149*4882a593Smuzhiyun 			| fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
150*4882a593Smuzhiyun 			| FSI_MMODE_P8_TO_LSB);
151*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MMODE, &reg, sizeof(reg));
152*4882a593Smuzhiyun 	if (rc)
153*4882a593Smuzhiyun 		return rc;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	reg = cpu_to_be32(0xffff0000);
156*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MDLYR, &reg, sizeof(reg));
157*4882a593Smuzhiyun 	if (rc)
158*4882a593Smuzhiyun 		return rc;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	reg = cpu_to_be32(~0);
161*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MSENP0, &reg, sizeof(reg));
162*4882a593Smuzhiyun 	if (rc)
163*4882a593Smuzhiyun 		return rc;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Leave enabled long enough for master logic to set up */
166*4882a593Smuzhiyun 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MCENP0, &reg, sizeof(reg));
169*4882a593Smuzhiyun 	if (rc)
170*4882a593Smuzhiyun 		return rc;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	rc = fsi_device_read(dev, FSI_MAEB, &reg, sizeof(reg));
173*4882a593Smuzhiyun 	if (rc)
174*4882a593Smuzhiyun 		return rc;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
177*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
178*4882a593Smuzhiyun 	if (rc)
179*4882a593Smuzhiyun 		return rc;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	rc = fsi_device_read(dev, FSI_MLEVP0, &reg, sizeof(reg));
182*4882a593Smuzhiyun 	if (rc)
183*4882a593Smuzhiyun 		return rc;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	/* Reset the master bridge */
186*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MRESB_RST_GEN);
187*4882a593Smuzhiyun 	rc = fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
188*4882a593Smuzhiyun 	if (rc)
189*4882a593Smuzhiyun 		return rc;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	reg = cpu_to_be32(FSI_MRESB_RST_ERR);
192*4882a593Smuzhiyun 	return fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
hub_master_probe(struct device * dev)195*4882a593Smuzhiyun static int hub_master_probe(struct device *dev)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct fsi_device *fsi_dev = to_fsi_dev(dev);
198*4882a593Smuzhiyun 	struct fsi_master_hub *hub;
199*4882a593Smuzhiyun 	uint32_t reg, links;
200*4882a593Smuzhiyun 	__be32 __reg;
201*4882a593Smuzhiyun 	int rc;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	rc = fsi_device_read(fsi_dev, FSI_MVER, &__reg, sizeof(__reg));
204*4882a593Smuzhiyun 	if (rc)
205*4882a593Smuzhiyun 		return rc;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	reg = be32_to_cpu(__reg);
208*4882a593Smuzhiyun 	links = (reg >> 8) & 0xff;
209*4882a593Smuzhiyun 	dev_dbg(dev, "hub version %08x (%d links)\n", reg, links);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	rc = fsi_slave_claim_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
212*4882a593Smuzhiyun 			FSI_HUB_LINK_SIZE * links);
213*4882a593Smuzhiyun 	if (rc) {
214*4882a593Smuzhiyun 		dev_err(dev, "can't claim slave address range for links");
215*4882a593Smuzhiyun 		return rc;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
219*4882a593Smuzhiyun 	if (!hub) {
220*4882a593Smuzhiyun 		rc = -ENOMEM;
221*4882a593Smuzhiyun 		goto err_release;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	hub->addr = FSI_HUB_LINK_OFFSET;
225*4882a593Smuzhiyun 	hub->size = FSI_HUB_LINK_SIZE * links;
226*4882a593Smuzhiyun 	hub->upstream = fsi_dev;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	hub->master.dev.parent = dev;
229*4882a593Smuzhiyun 	hub->master.dev.release = hub_master_release;
230*4882a593Smuzhiyun 	hub->master.dev.of_node = of_node_get(dev_of_node(dev));
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	hub->master.n_links = links;
233*4882a593Smuzhiyun 	hub->master.read = hub_master_read;
234*4882a593Smuzhiyun 	hub->master.write = hub_master_write;
235*4882a593Smuzhiyun 	hub->master.send_break = hub_master_break;
236*4882a593Smuzhiyun 	hub->master.link_enable = hub_master_link_enable;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	dev_set_drvdata(dev, hub);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	hub_master_init(hub);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	rc = fsi_master_register(&hub->master);
243*4882a593Smuzhiyun 	if (rc)
244*4882a593Smuzhiyun 		goto err_release;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	/* At this point, fsi_master_register performs the device_initialize(),
247*4882a593Smuzhiyun 	 * and holds the sole reference on master.dev. This means the device
248*4882a593Smuzhiyun 	 * will be freed (via ->release) during any subsequent call to
249*4882a593Smuzhiyun 	 * fsi_master_unregister.  We add our own reference to it here, so we
250*4882a593Smuzhiyun 	 * can perform cleanup (in _remove()) without it being freed before
251*4882a593Smuzhiyun 	 * we're ready.
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 	get_device(&hub->master.dev);
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun err_release:
257*4882a593Smuzhiyun 	fsi_slave_release_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
258*4882a593Smuzhiyun 			FSI_HUB_LINK_SIZE * links);
259*4882a593Smuzhiyun 	return rc;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
hub_master_remove(struct device * dev)262*4882a593Smuzhiyun static int hub_master_remove(struct device *dev)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct fsi_master_hub *hub = dev_get_drvdata(dev);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	fsi_master_unregister(&hub->master);
267*4882a593Smuzhiyun 	fsi_slave_release_range(hub->upstream->slave, hub->addr, hub->size);
268*4882a593Smuzhiyun 	of_node_put(hub->master.dev.of_node);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/*
271*4882a593Smuzhiyun 	 * master.dev will likely be ->release()ed after this, which free()s
272*4882a593Smuzhiyun 	 * the hub
273*4882a593Smuzhiyun 	 */
274*4882a593Smuzhiyun 	put_device(&hub->master.dev);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	return 0;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun static const struct fsi_device_id hub_master_ids[] = {
280*4882a593Smuzhiyun 	{
281*4882a593Smuzhiyun 		.engine_type = FSI_ENGID_HUB_MASTER,
282*4882a593Smuzhiyun 		.version = FSI_VERSION_ANY,
283*4882a593Smuzhiyun 	},
284*4882a593Smuzhiyun 	{ 0 }
285*4882a593Smuzhiyun };
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun static struct fsi_driver hub_master_driver = {
288*4882a593Smuzhiyun 	.id_table = hub_master_ids,
289*4882a593Smuzhiyun 	.drv = {
290*4882a593Smuzhiyun 		.name = "fsi-master-hub",
291*4882a593Smuzhiyun 		.bus = &fsi_bus_type,
292*4882a593Smuzhiyun 		.probe = hub_master_probe,
293*4882a593Smuzhiyun 		.remove = hub_master_remove,
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun };
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun module_fsi_driver(hub_master_driver);
298*4882a593Smuzhiyun MODULE_LICENSE("GPL");
299