xref: /openbmc/linux/drivers/fsi/fsi-master-hub.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27f9e8f76SChristopher Bostic /*
37f9e8f76SChristopher Bostic  * FSI hub master driver
47f9e8f76SChristopher Bostic  *
57f9e8f76SChristopher Bostic  * Copyright (C) IBM Corporation 2016
67f9e8f76SChristopher Bostic  */
77f9e8f76SChristopher Bostic 
87f9e8f76SChristopher Bostic #include <linux/delay.h>
97f9e8f76SChristopher Bostic #include <linux/fsi.h>
107f9e8f76SChristopher Bostic #include <linux/module.h>
11f6a2f8ebSJeremy Kerr #include <linux/of.h>
127f9e8f76SChristopher Bostic #include <linux/slab.h>
137f9e8f76SChristopher Bostic 
147f9e8f76SChristopher Bostic #include "fsi-master.h"
157f9e8f76SChristopher Bostic 
167f9e8f76SChristopher Bostic #define FSI_ENGID_HUB_MASTER		0x1c
177f9e8f76SChristopher Bostic 
187f9e8f76SChristopher Bostic #define FSI_LINK_ENABLE_SETUP_TIME	10	/* in mS */
197f9e8f76SChristopher Bostic 
207f9e8f76SChristopher Bostic /*
217f9e8f76SChristopher Bostic  * FSI hub master support
227f9e8f76SChristopher Bostic  *
237f9e8f76SChristopher Bostic  * A hub master increases the number of potential target devices that the
247f9e8f76SChristopher Bostic  * primary FSI master can access. For each link a primary master supports,
257f9e8f76SChristopher Bostic  * each of those links can in turn be chained to a hub master with multiple
267f9e8f76SChristopher Bostic  * links of its own.
277f9e8f76SChristopher Bostic  *
287f9e8f76SChristopher Bostic  * The hub is controlled by a set of control registers exposed as a regular fsi
297f9e8f76SChristopher Bostic  * device (the hub->upstream device), and provides access to the downstream FSI
307f9e8f76SChristopher Bostic  * bus as through an address range on the slave itself (->addr and ->size).
317f9e8f76SChristopher Bostic  *
327f9e8f76SChristopher Bostic  * [This differs from "cascaded" masters, which expose the entire downstream
337f9e8f76SChristopher Bostic  * bus entirely through the fsi device address range, and so have a smaller
347f9e8f76SChristopher Bostic  * accessible address space.]
357f9e8f76SChristopher Bostic  */
367f9e8f76SChristopher Bostic struct fsi_master_hub {
377f9e8f76SChristopher Bostic 	struct fsi_master	master;
387f9e8f76SChristopher Bostic 	struct fsi_device	*upstream;
397f9e8f76SChristopher Bostic 	uint32_t		addr, size;	/* slave-relative addr of */
407f9e8f76SChristopher Bostic 						/* master address space */
417f9e8f76SChristopher Bostic };
427f9e8f76SChristopher Bostic 
437f9e8f76SChristopher Bostic #define to_fsi_master_hub(m) container_of(m, struct fsi_master_hub, master)
447f9e8f76SChristopher Bostic 
hub_master_read(struct fsi_master * master,int link,uint8_t id,uint32_t addr,void * val,size_t size)457f9e8f76SChristopher Bostic static int hub_master_read(struct fsi_master *master, int link,
467f9e8f76SChristopher Bostic 			uint8_t id, uint32_t addr, void *val, size_t size)
477f9e8f76SChristopher Bostic {
487f9e8f76SChristopher Bostic 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
497f9e8f76SChristopher Bostic 
507f9e8f76SChristopher Bostic 	if (id != 0)
517f9e8f76SChristopher Bostic 		return -EINVAL;
527f9e8f76SChristopher Bostic 
537f9e8f76SChristopher Bostic 	addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
547f9e8f76SChristopher Bostic 	return fsi_slave_read(hub->upstream->slave, addr, val, size);
557f9e8f76SChristopher Bostic }
567f9e8f76SChristopher Bostic 
hub_master_write(struct fsi_master * master,int link,uint8_t id,uint32_t addr,const void * val,size_t size)577f9e8f76SChristopher Bostic static int hub_master_write(struct fsi_master *master, int link,
587f9e8f76SChristopher Bostic 			uint8_t id, uint32_t addr, const void *val, size_t size)
597f9e8f76SChristopher Bostic {
607f9e8f76SChristopher Bostic 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
617f9e8f76SChristopher Bostic 
627f9e8f76SChristopher Bostic 	if (id != 0)
637f9e8f76SChristopher Bostic 		return -EINVAL;
647f9e8f76SChristopher Bostic 
657f9e8f76SChristopher Bostic 	addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
667f9e8f76SChristopher Bostic 	return fsi_slave_write(hub->upstream->slave, addr, val, size);
677f9e8f76SChristopher Bostic }
687f9e8f76SChristopher Bostic 
hub_master_break(struct fsi_master * master,int link)697f9e8f76SChristopher Bostic static int hub_master_break(struct fsi_master *master, int link)
707f9e8f76SChristopher Bostic {
71fbdb5eacSJoel Stanley 	uint32_t addr;
72fbdb5eacSJoel Stanley 	__be32 cmd;
737f9e8f76SChristopher Bostic 
747f9e8f76SChristopher Bostic 	addr = 0x4;
757f9e8f76SChristopher Bostic 	cmd = cpu_to_be32(0xc0de0000);
767f9e8f76SChristopher Bostic 
777f9e8f76SChristopher Bostic 	return hub_master_write(master, link, 0, addr, &cmd, sizeof(cmd));
787f9e8f76SChristopher Bostic }
797f9e8f76SChristopher Bostic 
hub_master_link_enable(struct fsi_master * master,int link,bool enable)8004635a30SEddie James static int hub_master_link_enable(struct fsi_master *master, int link,
8104635a30SEddie James 				  bool enable)
827f9e8f76SChristopher Bostic {
837f9e8f76SChristopher Bostic 	struct fsi_master_hub *hub = to_fsi_master_hub(master);
847f9e8f76SChristopher Bostic 	int idx, bit;
857f9e8f76SChristopher Bostic 	__be32 reg;
867f9e8f76SChristopher Bostic 	int rc;
877f9e8f76SChristopher Bostic 
887f9e8f76SChristopher Bostic 	idx = link / 32;
897f9e8f76SChristopher Bostic 	bit = link % 32;
907f9e8f76SChristopher Bostic 
917f9e8f76SChristopher Bostic 	reg = cpu_to_be32(0x80000000 >> bit);
927f9e8f76SChristopher Bostic 
9304635a30SEddie James 	if (!enable)
9404635a30SEddie James 		return fsi_device_write(hub->upstream, FSI_MCENP0 + (4 * idx),
9504635a30SEddie James 					&reg, 4);
9604635a30SEddie James 
977f9e8f76SChristopher Bostic 	rc = fsi_device_write(hub->upstream, FSI_MSENP0 + (4 * idx), &reg, 4);
98a1d5ce11SEddie James 	if (rc)
99a1d5ce11SEddie James 		return rc;
1007f9e8f76SChristopher Bostic 
1017f9e8f76SChristopher Bostic 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
1027f9e8f76SChristopher Bostic 
103a1d5ce11SEddie James 	return 0;
1047f9e8f76SChristopher Bostic }
1057f9e8f76SChristopher Bostic 
hub_master_release(struct device * dev)1067f9e8f76SChristopher Bostic static void hub_master_release(struct device *dev)
1077f9e8f76SChristopher Bostic {
108*d5d8dfb0SEddie James 	struct fsi_master_hub *hub = to_fsi_master_hub(to_fsi_master(dev));
1097f9e8f76SChristopher Bostic 
1107f9e8f76SChristopher Bostic 	kfree(hub);
1117f9e8f76SChristopher Bostic }
1127f9e8f76SChristopher Bostic 
1137f9e8f76SChristopher Bostic /* mmode encoders */
fsi_mmode_crs0(u32 x)1147f9e8f76SChristopher Bostic static inline u32 fsi_mmode_crs0(u32 x)
1157f9e8f76SChristopher Bostic {
1167f9e8f76SChristopher Bostic 	return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
1177f9e8f76SChristopher Bostic }
1187f9e8f76SChristopher Bostic 
fsi_mmode_crs1(u32 x)1197f9e8f76SChristopher Bostic static inline u32 fsi_mmode_crs1(u32 x)
1207f9e8f76SChristopher Bostic {
1217f9e8f76SChristopher Bostic 	return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
1227f9e8f76SChristopher Bostic }
1237f9e8f76SChristopher Bostic 
hub_master_init(struct fsi_master_hub * hub)1247f9e8f76SChristopher Bostic static int hub_master_init(struct fsi_master_hub *hub)
1257f9e8f76SChristopher Bostic {
1267f9e8f76SChristopher Bostic 	struct fsi_device *dev = hub->upstream;
1277f9e8f76SChristopher Bostic 	__be32 reg;
1287f9e8f76SChristopher Bostic 	int rc;
1297f9e8f76SChristopher Bostic 
1307f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
1317f9e8f76SChristopher Bostic 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
1327f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
1337f9e8f76SChristopher Bostic 	if (rc)
1347f9e8f76SChristopher Bostic 		return rc;
1357f9e8f76SChristopher Bostic 
1367f9e8f76SChristopher Bostic 	/* Initialize the MFSI (hub master) engine */
1377f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
1387f9e8f76SChristopher Bostic 			| FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
1397f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
1407f9e8f76SChristopher Bostic 	if (rc)
1417f9e8f76SChristopher Bostic 		return rc;
1427f9e8f76SChristopher Bostic 
1437f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
1447f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MECTRL, &reg, sizeof(reg));
1457f9e8f76SChristopher Bostic 	if (rc)
1467f9e8f76SChristopher Bostic 		return rc;
1477f9e8f76SChristopher Bostic 
1487f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
1497f9e8f76SChristopher Bostic 			| fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
1507f9e8f76SChristopher Bostic 			| FSI_MMODE_P8_TO_LSB);
1517f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MMODE, &reg, sizeof(reg));
1527f9e8f76SChristopher Bostic 	if (rc)
1537f9e8f76SChristopher Bostic 		return rc;
1547f9e8f76SChristopher Bostic 
1557f9e8f76SChristopher Bostic 	reg = cpu_to_be32(0xffff0000);
1567f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MDLYR, &reg, sizeof(reg));
1577f9e8f76SChristopher Bostic 	if (rc)
1587f9e8f76SChristopher Bostic 		return rc;
1597f9e8f76SChristopher Bostic 
160fbdb5eacSJoel Stanley 	reg = cpu_to_be32(~0);
1617f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MSENP0, &reg, sizeof(reg));
1627f9e8f76SChristopher Bostic 	if (rc)
1637f9e8f76SChristopher Bostic 		return rc;
1647f9e8f76SChristopher Bostic 
1657f9e8f76SChristopher Bostic 	/* Leave enabled long enough for master logic to set up */
1667f9e8f76SChristopher Bostic 	mdelay(FSI_LINK_ENABLE_SETUP_TIME);
1677f9e8f76SChristopher Bostic 
1687f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MCENP0, &reg, sizeof(reg));
1697f9e8f76SChristopher Bostic 	if (rc)
1707f9e8f76SChristopher Bostic 		return rc;
1717f9e8f76SChristopher Bostic 
1727f9e8f76SChristopher Bostic 	rc = fsi_device_read(dev, FSI_MAEB, &reg, sizeof(reg));
1737f9e8f76SChristopher Bostic 	if (rc)
1747f9e8f76SChristopher Bostic 		return rc;
1757f9e8f76SChristopher Bostic 
1767f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
1777f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
1787f9e8f76SChristopher Bostic 	if (rc)
1797f9e8f76SChristopher Bostic 		return rc;
1807f9e8f76SChristopher Bostic 
1817f9e8f76SChristopher Bostic 	rc = fsi_device_read(dev, FSI_MLEVP0, &reg, sizeof(reg));
1827f9e8f76SChristopher Bostic 	if (rc)
1837f9e8f76SChristopher Bostic 		return rc;
1847f9e8f76SChristopher Bostic 
1857f9e8f76SChristopher Bostic 	/* Reset the master bridge */
1867f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MRESB_RST_GEN);
1877f9e8f76SChristopher Bostic 	rc = fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
1887f9e8f76SChristopher Bostic 	if (rc)
1897f9e8f76SChristopher Bostic 		return rc;
1907f9e8f76SChristopher Bostic 
1917f9e8f76SChristopher Bostic 	reg = cpu_to_be32(FSI_MRESB_RST_ERR);
1927f9e8f76SChristopher Bostic 	return fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
1937f9e8f76SChristopher Bostic }
1947f9e8f76SChristopher Bostic 
hub_master_probe(struct device * dev)1957f9e8f76SChristopher Bostic static int hub_master_probe(struct device *dev)
1967f9e8f76SChristopher Bostic {
1977f9e8f76SChristopher Bostic 	struct fsi_device *fsi_dev = to_fsi_dev(dev);
1987f9e8f76SChristopher Bostic 	struct fsi_master_hub *hub;
1997f9e8f76SChristopher Bostic 	uint32_t reg, links;
2007f9e8f76SChristopher Bostic 	__be32 __reg;
2017f9e8f76SChristopher Bostic 	int rc;
2027f9e8f76SChristopher Bostic 
2037f9e8f76SChristopher Bostic 	rc = fsi_device_read(fsi_dev, FSI_MVER, &__reg, sizeof(__reg));
2047f9e8f76SChristopher Bostic 	if (rc)
2057f9e8f76SChristopher Bostic 		return rc;
2067f9e8f76SChristopher Bostic 
2077f9e8f76SChristopher Bostic 	reg = be32_to_cpu(__reg);
2087f9e8f76SChristopher Bostic 	links = (reg >> 8) & 0xff;
209638bd9acSChristopher Bostic 	dev_dbg(dev, "hub version %08x (%d links)\n", reg, links);
2107f9e8f76SChristopher Bostic 
2117f9e8f76SChristopher Bostic 	rc = fsi_slave_claim_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
2127f9e8f76SChristopher Bostic 			FSI_HUB_LINK_SIZE * links);
2137f9e8f76SChristopher Bostic 	if (rc) {
2147f9e8f76SChristopher Bostic 		dev_err(dev, "can't claim slave address range for links");
2157f9e8f76SChristopher Bostic 		return rc;
2167f9e8f76SChristopher Bostic 	}
2177f9e8f76SChristopher Bostic 
2187f9e8f76SChristopher Bostic 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
2197f9e8f76SChristopher Bostic 	if (!hub) {
2207f9e8f76SChristopher Bostic 		rc = -ENOMEM;
2217f9e8f76SChristopher Bostic 		goto err_release;
2227f9e8f76SChristopher Bostic 	}
2237f9e8f76SChristopher Bostic 
2247f9e8f76SChristopher Bostic 	hub->addr = FSI_HUB_LINK_OFFSET;
2257f9e8f76SChristopher Bostic 	hub->size = FSI_HUB_LINK_SIZE * links;
2267f9e8f76SChristopher Bostic 	hub->upstream = fsi_dev;
2277f9e8f76SChristopher Bostic 
2287f9e8f76SChristopher Bostic 	hub->master.dev.parent = dev;
2297f9e8f76SChristopher Bostic 	hub->master.dev.release = hub_master_release;
230f6a2f8ebSJeremy Kerr 	hub->master.dev.of_node = of_node_get(dev_of_node(dev));
2317f9e8f76SChristopher Bostic 
2327f9e8f76SChristopher Bostic 	hub->master.n_links = links;
2337f9e8f76SChristopher Bostic 	hub->master.read = hub_master_read;
2347f9e8f76SChristopher Bostic 	hub->master.write = hub_master_write;
2357f9e8f76SChristopher Bostic 	hub->master.send_break = hub_master_break;
2367f9e8f76SChristopher Bostic 	hub->master.link_enable = hub_master_link_enable;
2377f9e8f76SChristopher Bostic 
2387f9e8f76SChristopher Bostic 	dev_set_drvdata(dev, hub);
2397f9e8f76SChristopher Bostic 
2407f9e8f76SChristopher Bostic 	hub_master_init(hub);
2417f9e8f76SChristopher Bostic 
2427f9e8f76SChristopher Bostic 	rc = fsi_master_register(&hub->master);
243e0c24bddSJeremy Kerr 	if (rc)
244e0c24bddSJeremy Kerr 		goto err_release;
245e0c24bddSJeremy Kerr 
246e0c24bddSJeremy Kerr 	/* At this point, fsi_master_register performs the device_initialize(),
247e0c24bddSJeremy Kerr 	 * and holds the sole reference on master.dev. This means the device
248e0c24bddSJeremy Kerr 	 * will be freed (via ->release) during any subsequent call to
249e0c24bddSJeremy Kerr 	 * fsi_master_unregister.  We add our own reference to it here, so we
250e0c24bddSJeremy Kerr 	 * can perform cleanup (in _remove()) without it being freed before
251e0c24bddSJeremy Kerr 	 * we're ready.
252e0c24bddSJeremy Kerr 	 */
253e0c24bddSJeremy Kerr 	get_device(&hub->master.dev);
2547f9e8f76SChristopher Bostic 	return 0;
2557f9e8f76SChristopher Bostic 
2567f9e8f76SChristopher Bostic err_release:
2577f9e8f76SChristopher Bostic 	fsi_slave_release_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
2587f9e8f76SChristopher Bostic 			FSI_HUB_LINK_SIZE * links);
2597f9e8f76SChristopher Bostic 	return rc;
2607f9e8f76SChristopher Bostic }
2617f9e8f76SChristopher Bostic 
hub_master_remove(struct device * dev)2627f9e8f76SChristopher Bostic static int hub_master_remove(struct device *dev)
2637f9e8f76SChristopher Bostic {
2647f9e8f76SChristopher Bostic 	struct fsi_master_hub *hub = dev_get_drvdata(dev);
2657f9e8f76SChristopher Bostic 
2667f9e8f76SChristopher Bostic 	fsi_master_unregister(&hub->master);
2677f9e8f76SChristopher Bostic 	fsi_slave_release_range(hub->upstream->slave, hub->addr, hub->size);
268f6a2f8ebSJeremy Kerr 	of_node_put(hub->master.dev.of_node);
269f6a2f8ebSJeremy Kerr 
270e0c24bddSJeremy Kerr 	/*
271e0c24bddSJeremy Kerr 	 * master.dev will likely be ->release()ed after this, which free()s
272e0c24bddSJeremy Kerr 	 * the hub
273e0c24bddSJeremy Kerr 	 */
274e0c24bddSJeremy Kerr 	put_device(&hub->master.dev);
275e0c24bddSJeremy Kerr 
2767f9e8f76SChristopher Bostic 	return 0;
2777f9e8f76SChristopher Bostic }
2787f9e8f76SChristopher Bostic 
27931901bb7SRikard Falkeborn static const struct fsi_device_id hub_master_ids[] = {
2807f9e8f76SChristopher Bostic 	{
2817f9e8f76SChristopher Bostic 		.engine_type = FSI_ENGID_HUB_MASTER,
2827f9e8f76SChristopher Bostic 		.version = FSI_VERSION_ANY,
2837f9e8f76SChristopher Bostic 	},
2847f9e8f76SChristopher Bostic 	{ 0 }
2857f9e8f76SChristopher Bostic };
2867f9e8f76SChristopher Bostic 
2877f9e8f76SChristopher Bostic static struct fsi_driver hub_master_driver = {
2887f9e8f76SChristopher Bostic 	.id_table = hub_master_ids,
2897f9e8f76SChristopher Bostic 	.drv = {
2907f9e8f76SChristopher Bostic 		.name = "fsi-master-hub",
2917f9e8f76SChristopher Bostic 		.bus = &fsi_bus_type,
2927f9e8f76SChristopher Bostic 		.probe = hub_master_probe,
2937f9e8f76SChristopher Bostic 		.remove = hub_master_remove,
2947f9e8f76SChristopher Bostic 	}
2957f9e8f76SChristopher Bostic };
2967f9e8f76SChristopher Bostic 
2977f9e8f76SChristopher Bostic module_fsi_driver(hub_master_driver);
2987f9e8f76SChristopher Bostic MODULE_LICENSE("GPL");
299