1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
2 /*
3  * Copyright (c) 2003-2012 Broadcom Corporation
4  * All Rights Reserved
5  */
6 
7 #include <linux/device.h>
8 #include <linux/platform_device.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/delay.h>
13 #include <linux/ioport.h>
14 #include <linux/resource.h>
15 #include <linux/phy.h>
16 
17 #include <asm/netlogic/haldefs.h>
18 #include <asm/netlogic/common.h>
19 #include <asm/netlogic/xlr/fmn.h>
20 #include <asm/netlogic/xlr/xlr.h>
21 #include <asm/netlogic/psb-bootinfo.h>
22 #include <asm/netlogic/xlr/pic.h>
23 #include <asm/netlogic/xlr/iomap.h>
24 
25 #include "platform_net.h"
26 
27 /* Linux Net */
28 #define MAX_NUM_GMAC		8
29 #define MAX_NUM_XLS_GMAC	8
30 #define MAX_NUM_XLR_GMAC	4
31 
32 static u32 xlr_gmac_offsets[] = {
33 	NETLOGIC_IO_GMAC_0_OFFSET, NETLOGIC_IO_GMAC_1_OFFSET,
34 	NETLOGIC_IO_GMAC_2_OFFSET, NETLOGIC_IO_GMAC_3_OFFSET,
35 	NETLOGIC_IO_GMAC_4_OFFSET, NETLOGIC_IO_GMAC_5_OFFSET,
36 	NETLOGIC_IO_GMAC_6_OFFSET, NETLOGIC_IO_GMAC_7_OFFSET
37 };
38 
39 static u32 xlr_gmac_irqs[] = { PIC_GMAC_0_IRQ, PIC_GMAC_1_IRQ,
40 	PIC_GMAC_2_IRQ, PIC_GMAC_3_IRQ,
41 	PIC_GMAC_4_IRQ, PIC_GMAC_5_IRQ,
42 	PIC_GMAC_6_IRQ, PIC_GMAC_7_IRQ
43 };
44 
45 static struct resource xlr_net0_res[8];
46 static struct resource xlr_net1_res[8];
47 static u32 __iomem *gmac4_addr;
48 static u32 __iomem *gpio_addr;
49 
xlr_resource_init(struct resource * res,int offset,int irq)50 static void xlr_resource_init(struct resource *res, int offset, int irq)
51 {
52 	res->name = "gmac";
53 
54 	res->start = CPHYSADDR(nlm_mmio_base(offset));
55 	res->end = res->start + 0xfff;
56 	res->flags = IORESOURCE_MEM;
57 
58 	res++;
59 	res->name = "gmac";
60 	res->start = irq;
61 	res->end = irq;
62 	res->flags = IORESOURCE_IRQ;
63 }
64 
gmac_controller2_init(void * gmac0_addr)65 static struct platform_device *gmac_controller2_init(void *gmac0_addr)
66 {
67 	int mac;
68 	static struct xlr_net_data ndata1 = {
69 		.phy_interface	= PHY_INTERFACE_MODE_SGMII,
70 		.rfr_station	= FMN_STNID_GMAC1_FR_0,
71 		.bucket_size	= xlr_board_fmn_config.bucket_size,
72 		.gmac_fmn_info	= &xlr_board_fmn_config.gmac[1],
73 	};
74 
75 	static struct platform_device xlr_net_dev1 = {
76 		.name		= "xlr-net",
77 		.id		= 1,
78 		.dev.platform_data = &ndata1,
79 	};
80 
81 	gmac4_addr =
82 		ioremap(CPHYSADDR(nlm_mmio_base(NETLOGIC_IO_GMAC_4_OFFSET)),
83 			0xfff);
84 	ndata1.serdes_addr = gmac4_addr;
85 	ndata1.pcs_addr	= gmac4_addr;
86 	ndata1.mii_addr	= gmac0_addr;
87 	ndata1.gpio_addr = gpio_addr;
88 	ndata1.cpu_mask = nlm_current_node()->coremask;
89 
90 	xlr_net_dev1.resource = xlr_net1_res;
91 
92 	for (mac = 0; mac < 4; mac++) {
93 		ndata1.tx_stnid[mac] = FMN_STNID_GMAC1_TX0 + mac;
94 		ndata1.phy_addr[mac] = mac + 4 + 0x10;
95 
96 		xlr_resource_init(&xlr_net1_res[mac * 2],
97 				  xlr_gmac_offsets[mac + 4],
98 				  xlr_gmac_irqs[mac + 4]);
99 	}
100 	xlr_net_dev1.num_resources = 8;
101 
102 	return &xlr_net_dev1;
103 }
104 
xls_gmac_init(void)105 static void xls_gmac_init(void)
106 {
107 	int mac;
108 	struct platform_device *xlr_net_dev1;
109 	void __iomem *gmac0_addr =
110 		ioremap(CPHYSADDR(nlm_mmio_base(NETLOGIC_IO_GMAC_0_OFFSET)),
111 			0xfff);
112 
113 	static struct xlr_net_data ndata0 = {
114 		.rfr_station	= FMN_STNID_GMACRFR_0,
115 		.bucket_size	= xlr_board_fmn_config.bucket_size,
116 		.gmac_fmn_info	= &xlr_board_fmn_config.gmac[0],
117 	};
118 
119 	static struct platform_device xlr_net_dev0 = {
120 		.name		= "xlr-net",
121 		.id		= 0,
122 	};
123 	xlr_net_dev0.dev.platform_data = &ndata0;
124 	ndata0.serdes_addr = gmac0_addr;
125 	ndata0.pcs_addr	= gmac0_addr;
126 	ndata0.mii_addr	= gmac0_addr;
127 
128 	/* Passing GPIO base for serdes init. Only needed on sgmii ports */
129 	gpio_addr =
130 		ioremap(CPHYSADDR(nlm_mmio_base(NETLOGIC_IO_GPIO_OFFSET)),
131 			0xfff);
132 	ndata0.gpio_addr = gpio_addr;
133 	ndata0.cpu_mask = nlm_current_node()->coremask;
134 
135 	xlr_net_dev0.resource = xlr_net0_res;
136 
137 	switch (nlm_prom_info.board_major_version) {
138 	case 12:
139 		/* first block RGMII or XAUI, use RGMII */
140 		ndata0.phy_interface = PHY_INTERFACE_MODE_RGMII;
141 		ndata0.tx_stnid[0] = FMN_STNID_GMAC0_TX0;
142 		ndata0.phy_addr[0] = 0;
143 
144 		xlr_net_dev0.num_resources = 2;
145 
146 		xlr_resource_init(&xlr_net0_res[0], xlr_gmac_offsets[0],
147 				  xlr_gmac_irqs[0]);
148 		platform_device_register(&xlr_net_dev0);
149 
150 		/* second block is XAUI, not supported yet */
151 		break;
152 	default:
153 		/* default XLS config, all ports SGMII */
154 		ndata0.phy_interface = PHY_INTERFACE_MODE_SGMII;
155 		for (mac = 0; mac < 4; mac++) {
156 			ndata0.tx_stnid[mac] = FMN_STNID_GMAC0_TX0 + mac;
157 			ndata0.phy_addr[mac] = mac + 0x10;
158 
159 			xlr_resource_init(&xlr_net0_res[mac * 2],
160 					  xlr_gmac_offsets[mac],
161 					xlr_gmac_irqs[mac]);
162 		}
163 		xlr_net_dev0.num_resources = 8;
164 		platform_device_register(&xlr_net_dev0);
165 
166 		xlr_net_dev1 = gmac_controller2_init(gmac0_addr);
167 		platform_device_register(xlr_net_dev1);
168 	}
169 }
170 
xlr_gmac_init(void)171 static void xlr_gmac_init(void)
172 {
173 	int mac;
174 
175 	/* assume all GMACs for now */
176 	static struct xlr_net_data ndata0 = {
177 		.phy_interface	= PHY_INTERFACE_MODE_RGMII,
178 		.serdes_addr	= NULL,
179 		.pcs_addr	= NULL,
180 		.rfr_station	= FMN_STNID_GMACRFR_0,
181 		.bucket_size	= xlr_board_fmn_config.bucket_size,
182 		.gmac_fmn_info	= &xlr_board_fmn_config.gmac[0],
183 		.gpio_addr	= NULL,
184 	};
185 
186 	static struct platform_device xlr_net_dev0 = {
187 		.name		= "xlr-net",
188 		.id		= 0,
189 		.dev.platform_data = &ndata0,
190 	};
191 	ndata0.mii_addr =
192 		ioremap(CPHYSADDR(nlm_mmio_base(NETLOGIC_IO_GMAC_0_OFFSET)),
193 			0xfff);
194 
195 	ndata0.cpu_mask = nlm_current_node()->coremask;
196 
197 	for (mac = 0; mac < MAX_NUM_XLR_GMAC; mac++) {
198 		ndata0.tx_stnid[mac] = FMN_STNID_GMAC0_TX0 + mac;
199 		ndata0.phy_addr[mac] = mac;
200 		xlr_resource_init(&xlr_net0_res[mac * 2], xlr_gmac_offsets[mac],
201 				  xlr_gmac_irqs[mac]);
202 	}
203 	xlr_net_dev0.num_resources = 8;
204 	xlr_net_dev0.resource = xlr_net0_res;
205 
206 	platform_device_register(&xlr_net_dev0);
207 }
208 
xlr_net_init(void)209 static int __init xlr_net_init(void)
210 {
211 	if (nlm_chip_is_xls())
212 		xls_gmac_init();
213 	else
214 		xlr_gmac_init();
215 
216 	return 0;
217 }
218 
219 arch_initcall(xlr_net_init);
220