xref: /openbmc/linux/arch/powerpc/sysdev/tsi108_dev.c (revision 75016ca3)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tsi108/109 device setup code
4  *
5  * Maintained by Roy Zang < tie-fei.zang@freescale.com >
6  */
7 
8 #include <linux/stddef.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/major.h>
13 #include <linux/delay.h>
14 #include <linux/irq.h>
15 #include <linux/export.h>
16 #include <linux/device.h>
17 #include <linux/etherdevice.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_net.h>
20 #include <asm/tsi108.h>
21 
22 #include <linux/atomic.h>
23 #include <asm/io.h>
24 #include <asm/irq.h>
25 #include <asm/prom.h>
26 #include <mm/mmu_decl.h>
27 
28 #undef DEBUG
29 
30 #ifdef DEBUG
31 #define DBG(fmt...) do { printk(fmt); } while(0)
32 #else
33 #define DBG(fmt...) do { } while(0)
34 #endif
35 
36 static phys_addr_t tsi108_csr_base = -1;
37 
38 phys_addr_t get_csrbase(void)
39 {
40 	struct device_node *tsi;
41 
42 	if (tsi108_csr_base != -1)
43 		return tsi108_csr_base;
44 
45 	tsi = of_find_node_by_type(NULL, "tsi-bridge");
46 	if (tsi) {
47 		unsigned int size;
48 		const void *prop = of_get_property(tsi, "reg", &size);
49 		tsi108_csr_base = of_translate_address(tsi, prop);
50 		of_node_put(tsi);
51 	}
52 	return tsi108_csr_base;
53 }
54 EXPORT_SYMBOL(get_csrbase);
55 
56 u32 get_vir_csrbase(void)
57 {
58 	return (u32) (ioremap(get_csrbase(), 0x10000));
59 }
60 EXPORT_SYMBOL(get_vir_csrbase);
61 
62 static int __init tsi108_eth_of_init(void)
63 {
64 	struct device_node *np;
65 	unsigned int i = 0;
66 	struct platform_device *tsi_eth_dev;
67 	struct resource res;
68 	int ret;
69 
70 	for_each_compatible_node(np, "network", "tsi108-ethernet") {
71 		struct resource r[2];
72 		struct device_node *phy, *mdio;
73 		hw_info tsi_eth_data;
74 		const unsigned int *phy_id;
75 		const phandle *ph;
76 
77 		memset(r, 0, sizeof(r));
78 		memset(&tsi_eth_data, 0, sizeof(tsi_eth_data));
79 
80 		ret = of_address_to_resource(np, 0, &r[0]);
81 		DBG("%s: name:start->end = %s:%pR\n",
82 		    __func__, r[0].name, &r[0]);
83 		if (ret)
84 			goto err;
85 
86 		r[1].name = "tx";
87 		r[1].start = irq_of_parse_and_map(np, 0);
88 		r[1].end = irq_of_parse_and_map(np, 0);
89 		r[1].flags = IORESOURCE_IRQ;
90 		DBG("%s: name:start->end = %s:%pR\n",
91 			__func__, r[1].name, &r[1]);
92 
93 		tsi_eth_dev =
94 		    platform_device_register_simple("tsi-ethernet", i++, &r[0],
95 						    1);
96 
97 		if (IS_ERR(tsi_eth_dev)) {
98 			ret = PTR_ERR(tsi_eth_dev);
99 			goto err;
100 		}
101 
102 		of_get_mac_address(np, tsi_eth_data.mac_addr);
103 
104 		ph = of_get_property(np, "mdio-handle", NULL);
105 		mdio = of_find_node_by_phandle(*ph);
106 		ret = of_address_to_resource(mdio, 0, &res);
107 		of_node_put(mdio);
108 		if (ret)
109 			goto unreg;
110 
111 		ph = of_get_property(np, "phy-handle", NULL);
112 		phy = of_find_node_by_phandle(*ph);
113 
114 		if (phy == NULL) {
115 			ret = -ENODEV;
116 			goto unreg;
117 		}
118 
119 		phy_id = of_get_property(phy, "reg", NULL);
120 
121 		tsi_eth_data.regs = r[0].start;
122 		tsi_eth_data.phyregs = res.start;
123 		tsi_eth_data.phy = *phy_id;
124 		tsi_eth_data.irq_num = irq_of_parse_and_map(np, 0);
125 
126 		/* Some boards with the TSI108 bridge (e.g. Holly)
127 		 * have a miswiring of the ethernet PHYs which
128 		 * requires a workaround.  The special
129 		 * "txc-rxc-delay-disable" property enables this
130 		 * workaround.  FIXME: Need to port the tsi108_eth
131 		 * driver itself to phylib and use a non-misleading
132 		 * name for the workaround flag - it's not actually to
133 		 * do with the model of PHY in use */
134 		if (of_get_property(phy, "txc-rxc-delay-disable", NULL))
135 			tsi_eth_data.phy_type = TSI108_PHY_BCM54XX;
136 		of_node_put(phy);
137 
138 		ret =
139 		    platform_device_add_data(tsi_eth_dev, &tsi_eth_data,
140 					     sizeof(hw_info));
141 		if (ret)
142 			goto unreg;
143 	}
144 	return 0;
145 unreg:
146 	platform_device_unregister(tsi_eth_dev);
147 err:
148 	of_node_put(np);
149 	return ret;
150 }
151 
152 arch_initcall(tsi108_eth_of_init);
153