blob: b3ff4c50dbe7c17098ba7d65c76d6389eccc7082 [file] [log] [blame]
Kumar Galac916d7c2011-04-13 08:37:44 -05001/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kumar Galac916d7c2011-04-13 08:37:44 -05005 */
Igal Liberman97a8d012015-08-18 14:47:05 +03006#include <errno.h>
Kumar Galac916d7c2011-04-13 08:37:44 -05007#include <common.h>
8#include <asm/io.h>
9#include <asm/fsl_serdes.h>
Claudiu Manoil93f26f12014-09-05 13:52:36 +080010#include <fsl_mdio.h>
Kumar Galac916d7c2011-04-13 08:37:44 -050011
12#include "fm.h"
13
14struct fm_eth_info fm_info[] = {
15#if (CONFIG_SYS_NUM_FM1_DTSEC >= 1)
16 FM_DTSEC_INFO_INITIALIZER(1, 1),
17#endif
18#if (CONFIG_SYS_NUM_FM1_DTSEC >= 2)
19 FM_DTSEC_INFO_INITIALIZER(1, 2),
20#endif
21#if (CONFIG_SYS_NUM_FM1_DTSEC >= 3)
22 FM_DTSEC_INFO_INITIALIZER(1, 3),
23#endif
24#if (CONFIG_SYS_NUM_FM1_DTSEC >= 4)
25 FM_DTSEC_INFO_INITIALIZER(1, 4),
26#endif
27#if (CONFIG_SYS_NUM_FM1_DTSEC >= 5)
28 FM_DTSEC_INFO_INITIALIZER(1, 5),
29#endif
York Sun9e758752012-10-08 07:44:19 +000030#if (CONFIG_SYS_NUM_FM1_DTSEC >= 6)
31 FM_DTSEC_INFO_INITIALIZER(1, 6),
32#endif
33#if (CONFIG_SYS_NUM_FM1_DTSEC >= 7)
34 FM_DTSEC_INFO_INITIALIZER(1, 9),
35#endif
36#if (CONFIG_SYS_NUM_FM1_DTSEC >= 8)
37 FM_DTSEC_INFO_INITIALIZER(1, 10),
38#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050039#if (CONFIG_SYS_NUM_FM2_DTSEC >= 1)
40 FM_DTSEC_INFO_INITIALIZER(2, 1),
41#endif
42#if (CONFIG_SYS_NUM_FM2_DTSEC >= 2)
43 FM_DTSEC_INFO_INITIALIZER(2, 2),
44#endif
45#if (CONFIG_SYS_NUM_FM2_DTSEC >= 3)
46 FM_DTSEC_INFO_INITIALIZER(2, 3),
47#endif
48#if (CONFIG_SYS_NUM_FM2_DTSEC >= 4)
49 FM_DTSEC_INFO_INITIALIZER(2, 4),
50#endif
Timur Tabi99abf7d2012-08-14 06:47:21 +000051#if (CONFIG_SYS_NUM_FM2_DTSEC >= 5)
52 FM_DTSEC_INFO_INITIALIZER(2, 5),
53#endif
York Sun9e758752012-10-08 07:44:19 +000054#if (CONFIG_SYS_NUM_FM2_DTSEC >= 6)
55 FM_DTSEC_INFO_INITIALIZER(2, 6),
56#endif
57#if (CONFIG_SYS_NUM_FM2_DTSEC >= 7)
58 FM_DTSEC_INFO_INITIALIZER(2, 9),
59#endif
60#if (CONFIG_SYS_NUM_FM2_DTSEC >= 8)
61 FM_DTSEC_INFO_INITIALIZER(2, 10),
62#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050063#if (CONFIG_SYS_NUM_FM1_10GEC >= 1)
64 FM_TGEC_INFO_INITIALIZER(1, 1),
65#endif
Shaohui Xie944b6cc2013-03-25 07:33:17 +000066#if (CONFIG_SYS_NUM_FM1_10GEC >= 2)
67 FM_TGEC_INFO_INITIALIZER(1, 2),
68#endif
Shengzhou Liu82a55c12013-11-22 17:39:09 +080069#if (CONFIG_SYS_NUM_FM1_10GEC >= 3)
70 FM_TGEC_INFO_INITIALIZER2(1, 3),
71#endif
72#if (CONFIG_SYS_NUM_FM1_10GEC >= 4)
73 FM_TGEC_INFO_INITIALIZER2(1, 4),
74#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050075#if (CONFIG_SYS_NUM_FM2_10GEC >= 1)
76 FM_TGEC_INFO_INITIALIZER(2, 1),
77#endif
Shaohui Xie944b6cc2013-03-25 07:33:17 +000078#if (CONFIG_SYS_NUM_FM2_10GEC >= 2)
79 FM_TGEC_INFO_INITIALIZER(2, 2),
80#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050081};
82
83int fm_standard_init(bd_t *bis)
84{
85 int i;
86 struct ccsr_fman *reg;
87
88 reg = (void *)CONFIG_SYS_FSL_FM1_ADDR;
89 if (fm_init_common(0, reg))
90 return 0;
91
92 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
93 if ((fm_info[i].enabled) && (fm_info[i].index == 1))
94 fm_eth_initialize(reg, &fm_info[i]);
95 }
96
97#if (CONFIG_SYS_NUM_FMAN == 2)
98 reg = (void *)CONFIG_SYS_FSL_FM2_ADDR;
99 if (fm_init_common(1, reg))
100 return 0;
101
102 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
103 if ((fm_info[i].enabled) && (fm_info[i].index == 2))
104 fm_eth_initialize(reg, &fm_info[i]);
105 }
106#endif
107
108 return 1;
109}
110
111/* simple linear search to map from port to array index */
112static int fm_port_to_index(enum fm_port port)
113{
114 int i;
115
116 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
117 if (fm_info[i].port == port)
118 return i;
119 }
120
121 return -1;
122}
123
124/*
125 * Determine if an interface is actually active based on HW config
126 * we expect fman_port_enet_if() to report PHY_INTERFACE_MODE_NONE if
127 * the interface is not active based on HW cfg of the SoC
128 */
129void fman_enet_init(void)
130{
131 int i;
132
133 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
134 phy_interface_t enet_if;
135
136 enet_if = fman_port_enet_if(fm_info[i].port);
137 if (enet_if != PHY_INTERFACE_MODE_NONE) {
138 fm_info[i].enabled = 1;
139 fm_info[i].enet_if = enet_if;
140 } else {
141 fm_info[i].enabled = 0;
142 }
143 }
144
145 return ;
146}
147
Kumar Gala69a85242011-09-14 12:01:35 -0500148void fm_disable_port(enum fm_port port)
149{
150 int i = fm_port_to_index(port);
151
Rotariu Marian-Cristian1155d8d2014-05-19 10:59:52 +0300152 if (i == -1)
153 return;
154
Kumar Gala69a85242011-09-14 12:01:35 -0500155 fm_info[i].enabled = 0;
156 fman_disable_port(port);
157}
158
Valentin Longchampf51d3b72013-10-18 11:47:21 +0200159void fm_enable_port(enum fm_port port)
160{
161 int i = fm_port_to_index(port);
162
Rotariu Marian-Cristian1155d8d2014-05-19 10:59:52 +0300163 if (i == -1)
164 return;
165
Valentin Longchampf51d3b72013-10-18 11:47:21 +0200166 fm_info[i].enabled = 1;
167 fman_enable_port(port);
168}
169
Kumar Galac916d7c2011-04-13 08:37:44 -0500170void fm_info_set_mdio(enum fm_port port, struct mii_dev *bus)
171{
172 int i = fm_port_to_index(port);
173
174 if (i == -1)
175 return;
176
177 fm_info[i].bus = bus;
178}
179
180void fm_info_set_phy_address(enum fm_port port, int address)
181{
182 int i = fm_port_to_index(port);
183
184 if (i == -1)
185 return;
186
187 fm_info[i].phy_addr = address;
188}
189
190/*
Timur Tabiae2291f2012-08-14 06:47:22 +0000191 * Returns the PHY address for a given Fman port
192 *
193 * The port must be set via a prior call to fm_info_set_phy_address().
194 * A negative error code is returned if the port is invalid.
195 */
196int fm_info_get_phy_address(enum fm_port port)
197{
198 int i = fm_port_to_index(port);
199
200 if (i == -1)
201 return -1;
202
203 return fm_info[i].phy_addr;
204}
205
206/*
Kumar Galac916d7c2011-04-13 08:37:44 -0500207 * Returns the type of the data interface between the given MAC and its PHY.
208 * This is typically determined by the RCW.
209 */
210phy_interface_t fm_info_get_enet_if(enum fm_port port)
211{
212 int i = fm_port_to_index(port);
213
214 if (i == -1)
215 return PHY_INTERFACE_MODE_NONE;
216
217 if (fm_info[i].enabled)
218 return fm_info[i].enet_if;
219
220 return PHY_INTERFACE_MODE_NONE;
221}
222
223static void
224__def_board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
225 enum fm_port port, int offset)
226{
227 return ;
228}
229
230void board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
231 enum fm_port port, int offset)
232 __attribute__((weak, alias("__def_board_ft_fman_fixup_port")));
233
Igal Liberman97a8d012015-08-18 14:47:05 +0300234int ft_fixup_port(void *blob, struct fm_eth_info *info, char *prop)
Kumar Galac916d7c2011-04-13 08:37:44 -0500235{
Timur Tabi4376b4c2012-08-14 06:47:24 +0000236 int off;
237 uint32_t ph;
Kumar Galac916d7c2011-04-13 08:37:44 -0500238 phys_addr_t paddr = CONFIG_SYS_CCSRBAR_PHYS + info->compat_offset;
Kumar Galae81c0ab2011-10-13 14:55:59 -0500239 u64 dtsec1_addr = (u64)CONFIG_SYS_CCSRBAR_PHYS +
240 CONFIG_SYS_FSL_FM1_DTSEC1_OFFSET;
Kumar Galac916d7c2011-04-13 08:37:44 -0500241
242 off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
Igal Liberman97a8d012015-08-18 14:47:05 +0300243 if (off == -FDT_ERR_NOTFOUND)
244 return -EINVAL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500245
246 if (info->enabled) {
247 fdt_fixup_phy_connection(blob, off, info->enet_if);
248 board_ft_fman_fixup_port(blob, prop, paddr, info->port, off);
Igal Liberman97a8d012015-08-18 14:47:05 +0300249 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500250 }
251
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000252#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800253#ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000254 /*
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800255 * On T2/T4 SoCs, physically FM1_DTSEC9 and FM1_10GEC1 use the same
256 * dual-role MAC, when FM1_10GEC1 is enabled and FM1_DTSEC9
257 * is disabled, ensure that the dual-role MAC is not disabled,
258 * ditto for other dual-role MACs.
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000259 */
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800260 if (((info->port == FM1_DTSEC9) && (PORT_IS_ENABLED(FM1_10GEC1))) ||
261 ((info->port == FM1_DTSEC10) && (PORT_IS_ENABLED(FM1_10GEC2))) ||
262 ((info->port == FM1_DTSEC1) && (PORT_IS_ENABLED(FM1_10GEC3))) ||
263 ((info->port == FM1_DTSEC2) && (PORT_IS_ENABLED(FM1_10GEC4))) ||
264 ((info->port == FM1_10GEC1) && (PORT_IS_ENABLED(FM1_DTSEC9))) ||
265 ((info->port == FM1_10GEC2) && (PORT_IS_ENABLED(FM1_DTSEC10))) ||
266 ((info->port == FM1_10GEC3) && (PORT_IS_ENABLED(FM1_DTSEC1))) ||
267 ((info->port == FM1_10GEC4) && (PORT_IS_ENABLED(FM1_DTSEC2)))
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000268#if (CONFIG_SYS_NUM_FMAN == 2)
269 ||
270 ((info->port == FM2_DTSEC9) && (PORT_IS_ENABLED(FM2_10GEC1))) ||
271 ((info->port == FM2_DTSEC10) && (PORT_IS_ENABLED(FM2_10GEC2))) ||
272 ((info->port == FM2_10GEC1) && (PORT_IS_ENABLED(FM2_DTSEC9))) ||
273 ((info->port == FM2_10GEC2) && (PORT_IS_ENABLED(FM2_DTSEC10)))
274#endif
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800275#else
276 /* FM1_DTSECx and FM1_10GECx use the same dual-role MAC */
277 if (((info->port == FM1_DTSEC1) && (PORT_IS_ENABLED(FM1_10GEC1))) ||
278 ((info->port == FM1_DTSEC2) && (PORT_IS_ENABLED(FM1_10GEC2))) ||
279 ((info->port == FM1_DTSEC3) && (PORT_IS_ENABLED(FM1_10GEC3))) ||
280 ((info->port == FM1_DTSEC4) && (PORT_IS_ENABLED(FM1_10GEC4))) ||
281 ((info->port == FM1_10GEC1) && (PORT_IS_ENABLED(FM1_DTSEC1))) ||
282 ((info->port == FM1_10GEC2) && (PORT_IS_ENABLED(FM1_DTSEC2))) ||
283 ((info->port == FM1_10GEC3) && (PORT_IS_ENABLED(FM1_DTSEC3))) ||
284 ((info->port == FM1_10GEC4) && (PORT_IS_ENABLED(FM1_DTSEC4)))
285#endif
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000286 )
Igal Liberman97a8d012015-08-18 14:47:05 +0300287 return 0;
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000288#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500289 /* board code might have caused offset to change */
290 off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
291
Kumar Galae81c0ab2011-10-13 14:55:59 -0500292 /* Don't disable FM1-DTSEC1 MAC as its used for MDIO */
Timur Tabi4376b4c2012-08-14 06:47:24 +0000293 if (paddr != dtsec1_addr)
294 fdt_status_disabled(blob, off); /* disable the MAC node */
Kumar Galac916d7c2011-04-13 08:37:44 -0500295
Timur Tabi4376b4c2012-08-14 06:47:24 +0000296 /* disable the fsl,dpa-ethernet node that points to the MAC */
Kumar Galac916d7c2011-04-13 08:37:44 -0500297 ph = fdt_get_phandle(blob, off);
298 do_fixup_by_prop(blob, "fsl,fman-mac", &ph, sizeof(ph),
299 "status", "disabled", strlen("disabled") + 1, 1);
Igal Liberman97a8d012015-08-18 14:47:05 +0300300
301 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500302}
303
304void fdt_fixup_fman_ethernet(void *blob)
305{
306 int i;
307
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000308#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liu43994342014-01-13 15:32:24 +0800309 for (i = 0; i < ARRAY_SIZE(fm_info); i++)
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000310 ft_fixup_port(blob, &fm_info[i], "fsl,fman-memac");
311#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500312 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
Igal Liberman97a8d012015-08-18 14:47:05 +0300313 /* Try the new compatible first.
314 * If the node is missing, try the old.
315 */
316 if (fm_info[i].type == FM_ETH_1G_E) {
317 if (ft_fixup_port(blob, &fm_info[i], "fsl,fman-dtsec"))
318 ft_fixup_port(blob, &fm_info[i],
319 "fsl,fman-1g-mac");
320 } else {
321 if (ft_fixup_port(blob, &fm_info[i], "fsl,fman-tgec"))
322 ft_fixup_port(blob, &fm_info[i],
323 "fsl,fman-10g-mac");
324 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500325 }
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000326#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500327}
Zhao Qiangffee1dd2013-09-04 10:11:27 +0800328
329/*QSGMII Riser Card can work in SGMII mode, but the PHY address is different.
330 *This function scans which Riser Card being used(QSGMII or SGMII Riser Card),
331 *then set the correct PHY address
332 */
333void set_sgmii_phy(struct mii_dev *bus, enum fm_port base_port,
334 unsigned int port_num, int phy_base_addr)
335{
336 unsigned int regnum = 0;
337 int qsgmii;
338 int i;
339 int phy_real_addr;
340
341 qsgmii = is_qsgmii_riser_card(bus, phy_base_addr, port_num, regnum);
342
343 if (!qsgmii)
344 return;
345
346 for (i = base_port; i < base_port + port_num; i++) {
347 if (fm_info_get_enet_if(i) == PHY_INTERFACE_MODE_SGMII) {
348 phy_real_addr = phy_base_addr + i - base_port;
349 fm_info_set_phy_address(i, phy_real_addr);
350 }
351 }
352}
353
354/*to check whether qsgmii riser card is used*/
355int is_qsgmii_riser_card(struct mii_dev *bus, int phy_base_addr,
356 unsigned int port_num, unsigned regnum)
357{
358 int i;
359 int val;
360
361 if (!bus)
362 return 0;
363
364 for (i = phy_base_addr; i < phy_base_addr + port_num; i++) {
365 val = bus->read(bus, i, MDIO_DEVAD_NONE, regnum);
366 if (val != MIIM_TIMEOUT)
367 return 1;
368 }
369
370 return 0;
371}