blob: 618c1bccbe3f628059701aaeed9943a6bf8ba367 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Galac916d7c2011-04-13 08:37:44 -05002/*
Shaohui Xiee8297342015-10-26 19:47:54 +08003 * Copyright 2011-2015 Freescale Semiconductor, Inc.
Kumar Galac916d7c2011-04-13 08:37:44 -05004 */
Igal Liberman97a8d012015-08-18 14:47:05 +03005#include <errno.h>
Kumar Galac916d7c2011-04-13 08:37:44 -05006#include <common.h>
Simon Glass90526e92020-05-10 11:39:56 -06007#include <net.h>
Kumar Galac916d7c2011-04-13 08:37:44 -05008#include <asm/io.h>
Simon Glass73223f02016-02-22 22:55:43 -07009#include <fdt_support.h>
Claudiu Manoil93f26f12014-09-05 13:52:36 +080010#include <fsl_mdio.h>
Shaohui Xiee8297342015-10-26 19:47:54 +080011#ifdef CONFIG_FSL_LAYERSCAPE
12#include <asm/arch/fsl_serdes.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060013#include <linux/libfdt.h>
Shaohui Xiee8297342015-10-26 19:47:54 +080014#else
15#include <asm/fsl_serdes.h>
16#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050017
18#include "fm.h"
19
Madalin Bucur6eb32a02020-04-23 16:25:19 +030020#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -050021struct fm_eth_info fm_info[] = {
Tom Rinicdc5ed82022-11-16 13:10:29 -050022#if (CFG_SYS_NUM_FM1_DTSEC >= 1)
Kumar Galac916d7c2011-04-13 08:37:44 -050023 FM_DTSEC_INFO_INITIALIZER(1, 1),
24#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050025#if (CFG_SYS_NUM_FM1_DTSEC >= 2)
Kumar Galac916d7c2011-04-13 08:37:44 -050026 FM_DTSEC_INFO_INITIALIZER(1, 2),
27#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050028#if (CFG_SYS_NUM_FM1_DTSEC >= 3)
Kumar Galac916d7c2011-04-13 08:37:44 -050029 FM_DTSEC_INFO_INITIALIZER(1, 3),
30#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050031#if (CFG_SYS_NUM_FM1_DTSEC >= 4)
Kumar Galac916d7c2011-04-13 08:37:44 -050032 FM_DTSEC_INFO_INITIALIZER(1, 4),
33#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050034#if (CFG_SYS_NUM_FM1_DTSEC >= 5)
Kumar Galac916d7c2011-04-13 08:37:44 -050035 FM_DTSEC_INFO_INITIALIZER(1, 5),
36#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050037#if (CFG_SYS_NUM_FM1_DTSEC >= 6)
York Sun9e758752012-10-08 07:44:19 +000038 FM_DTSEC_INFO_INITIALIZER(1, 6),
39#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050040#if (CFG_SYS_NUM_FM1_DTSEC >= 7)
York Sun9e758752012-10-08 07:44:19 +000041 FM_DTSEC_INFO_INITIALIZER(1, 9),
42#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050043#if (CFG_SYS_NUM_FM1_DTSEC >= 8)
York Sun9e758752012-10-08 07:44:19 +000044 FM_DTSEC_INFO_INITIALIZER(1, 10),
45#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050046#if (CFG_SYS_NUM_FM2_DTSEC >= 1)
Kumar Galac916d7c2011-04-13 08:37:44 -050047 FM_DTSEC_INFO_INITIALIZER(2, 1),
48#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050049#if (CFG_SYS_NUM_FM2_DTSEC >= 2)
Kumar Galac916d7c2011-04-13 08:37:44 -050050 FM_DTSEC_INFO_INITIALIZER(2, 2),
51#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050052#if (CFG_SYS_NUM_FM2_DTSEC >= 3)
Kumar Galac916d7c2011-04-13 08:37:44 -050053 FM_DTSEC_INFO_INITIALIZER(2, 3),
54#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050055#if (CFG_SYS_NUM_FM2_DTSEC >= 4)
Kumar Galac916d7c2011-04-13 08:37:44 -050056 FM_DTSEC_INFO_INITIALIZER(2, 4),
57#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050058#if (CFG_SYS_NUM_FM2_DTSEC >= 5)
Timur Tabi99abf7d2012-08-14 06:47:21 +000059 FM_DTSEC_INFO_INITIALIZER(2, 5),
60#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050061#if (CFG_SYS_NUM_FM2_DTSEC >= 6)
York Sun9e758752012-10-08 07:44:19 +000062 FM_DTSEC_INFO_INITIALIZER(2, 6),
63#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050064#if (CFG_SYS_NUM_FM2_DTSEC >= 7)
York Sun9e758752012-10-08 07:44:19 +000065 FM_DTSEC_INFO_INITIALIZER(2, 9),
66#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050067#if (CFG_SYS_NUM_FM2_DTSEC >= 8)
York Sun9e758752012-10-08 07:44:19 +000068 FM_DTSEC_INFO_INITIALIZER(2, 10),
69#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050070#if (CFG_SYS_NUM_FM1_10GEC >= 1)
Kumar Galac916d7c2011-04-13 08:37:44 -050071 FM_TGEC_INFO_INITIALIZER(1, 1),
72#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050073#if (CFG_SYS_NUM_FM1_10GEC >= 2)
Shaohui Xie944b6cc2013-03-25 07:33:17 +000074 FM_TGEC_INFO_INITIALIZER(1, 2),
75#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050076#if (CFG_SYS_NUM_FM1_10GEC >= 3)
Shengzhou Liu82a55c12013-11-22 17:39:09 +080077 FM_TGEC_INFO_INITIALIZER2(1, 3),
78#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050079#if (CFG_SYS_NUM_FM1_10GEC >= 4)
Shengzhou Liu82a55c12013-11-22 17:39:09 +080080 FM_TGEC_INFO_INITIALIZER2(1, 4),
81#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050082#if (CFG_SYS_NUM_FM2_10GEC >= 1)
Kumar Galac916d7c2011-04-13 08:37:44 -050083 FM_TGEC_INFO_INITIALIZER(2, 1),
84#endif
Tom Rinicdc5ed82022-11-16 13:10:29 -050085#if (CFG_SYS_NUM_FM2_10GEC >= 2)
Shaohui Xie944b6cc2013-03-25 07:33:17 +000086 FM_TGEC_INFO_INITIALIZER(2, 2),
87#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050088};
89
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090090int fm_standard_init(struct bd_info *bis)
Kumar Galac916d7c2011-04-13 08:37:44 -050091{
92 int i;
93 struct ccsr_fman *reg;
94
Tom Rini6cc04542022-10-28 20:27:13 -040095 reg = (void *)CFG_SYS_FSL_FM1_ADDR;
Kumar Galac916d7c2011-04-13 08:37:44 -050096 if (fm_init_common(0, reg))
97 return 0;
98
99 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
100 if ((fm_info[i].enabled) && (fm_info[i].index == 1))
101 fm_eth_initialize(reg, &fm_info[i]);
102 }
103
Tom Rinicdc5ed82022-11-16 13:10:29 -0500104#if (CFG_SYS_NUM_FMAN == 2)
Tom Rini6cc04542022-10-28 20:27:13 -0400105 reg = (void *)CFG_SYS_FSL_FM2_ADDR;
Kumar Galac916d7c2011-04-13 08:37:44 -0500106 if (fm_init_common(1, reg))
107 return 0;
108
109 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
110 if ((fm_info[i].enabled) && (fm_info[i].index == 2))
111 fm_eth_initialize(reg, &fm_info[i]);
112 }
113#endif
114
115 return 1;
116}
117
118/* simple linear search to map from port to array index */
119static int fm_port_to_index(enum fm_port port)
120{
121 int i;
122
123 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
124 if (fm_info[i].port == port)
125 return i;
126 }
127
128 return -1;
129}
130
131/*
132 * Determine if an interface is actually active based on HW config
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200133 * we expect fman_port_enet_if() to report PHY_INTERFACE_MODE_NA if
Kumar Galac916d7c2011-04-13 08:37:44 -0500134 * the interface is not active based on HW cfg of the SoC
135 */
136void fman_enet_init(void)
137{
138 int i;
139
140 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
141 phy_interface_t enet_if;
142
143 enet_if = fman_port_enet_if(fm_info[i].port);
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200144 if (enet_if != PHY_INTERFACE_MODE_NA) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500145 fm_info[i].enabled = 1;
146 fm_info[i].enet_if = enet_if;
147 } else {
148 fm_info[i].enabled = 0;
149 }
150 }
151
Bin Mengea253ad2022-10-26 12:40:07 +0800152 return;
Kumar Galac916d7c2011-04-13 08:37:44 -0500153}
154
Kumar Gala69a85242011-09-14 12:01:35 -0500155void fm_disable_port(enum fm_port port)
156{
157 int i = fm_port_to_index(port);
158
Rotariu Marian-Cristian1155d8d2014-05-19 10:59:52 +0300159 if (i == -1)
160 return;
161
Kumar Gala69a85242011-09-14 12:01:35 -0500162 fm_info[i].enabled = 0;
Shaohui Xiee8297342015-10-26 19:47:54 +0800163#ifndef CONFIG_SYS_FMAN_V3
Kumar Gala69a85242011-09-14 12:01:35 -0500164 fman_disable_port(port);
Shaohui Xiee8297342015-10-26 19:47:54 +0800165#endif
Kumar Gala69a85242011-09-14 12:01:35 -0500166}
167
Valentin Longchampf51d3b72013-10-18 11:47:21 +0200168void fm_enable_port(enum fm_port port)
169{
170 int i = fm_port_to_index(port);
171
Rotariu Marian-Cristian1155d8d2014-05-19 10:59:52 +0300172 if (i == -1)
173 return;
174
Valentin Longchampf51d3b72013-10-18 11:47:21 +0200175 fm_info[i].enabled = 1;
176 fman_enable_port(port);
177}
178
Kumar Galac916d7c2011-04-13 08:37:44 -0500179void fm_info_set_mdio(enum fm_port port, struct mii_dev *bus)
180{
181 int i = fm_port_to_index(port);
182
183 if (i == -1)
184 return;
185
186 fm_info[i].bus = bus;
187}
188
189void fm_info_set_phy_address(enum fm_port port, int address)
190{
191 int i = fm_port_to_index(port);
192
193 if (i == -1)
194 return;
195
196 fm_info[i].phy_addr = address;
197}
198
199/*
Timur Tabiae2291f2012-08-14 06:47:22 +0000200 * Returns the PHY address for a given Fman port
201 *
202 * The port must be set via a prior call to fm_info_set_phy_address().
203 * A negative error code is returned if the port is invalid.
204 */
205int fm_info_get_phy_address(enum fm_port port)
206{
207 int i = fm_port_to_index(port);
208
209 if (i == -1)
210 return -1;
211
212 return fm_info[i].phy_addr;
213}
214
215/*
Kumar Galac916d7c2011-04-13 08:37:44 -0500216 * Returns the type of the data interface between the given MAC and its PHY.
217 * This is typically determined by the RCW.
218 */
219phy_interface_t fm_info_get_enet_if(enum fm_port port)
220{
221 int i = fm_port_to_index(port);
222
223 if (i == -1)
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200224 return PHY_INTERFACE_MODE_NA;
Kumar Galac916d7c2011-04-13 08:37:44 -0500225
226 if (fm_info[i].enabled)
227 return fm_info[i].enet_if;
228
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200229 return PHY_INTERFACE_MODE_NA;
Kumar Galac916d7c2011-04-13 08:37:44 -0500230}
231
232static void
233__def_board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
234 enum fm_port port, int offset)
235{
Bin Mengea253ad2022-10-26 12:40:07 +0800236 return;
Kumar Galac916d7c2011-04-13 08:37:44 -0500237}
238
239void board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
240 enum fm_port port, int offset)
241 __attribute__((weak, alias("__def_board_ft_fman_fixup_port")));
242
Igal Liberman97a8d012015-08-18 14:47:05 +0300243int ft_fixup_port(void *blob, struct fm_eth_info *info, char *prop)
Kumar Galac916d7c2011-04-13 08:37:44 -0500244{
Timur Tabi4376b4c2012-08-14 06:47:24 +0000245 int off;
246 uint32_t ph;
Tom Rini65cc0e22022-11-16 13:10:41 -0500247 phys_addr_t paddr = CFG_SYS_CCSRBAR_PHYS + info->compat_offset;
Shaohui Xieea1332c2015-12-04 10:22:03 +0800248#ifndef CONFIG_SYS_FMAN_V3
Tom Rini65cc0e22022-11-16 13:10:41 -0500249 u64 dtsec1_addr = (u64)CFG_SYS_CCSRBAR_PHYS +
Tom Rini6cc04542022-10-28 20:27:13 -0400250 CFG_SYS_FSL_FM1_DTSEC1_OFFSET;
Shaohui Xieea1332c2015-12-04 10:22:03 +0800251#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500252
253 off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
Igal Liberman97a8d012015-08-18 14:47:05 +0300254 if (off == -FDT_ERR_NOTFOUND)
255 return -EINVAL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500256
257 if (info->enabled) {
258 fdt_fixup_phy_connection(blob, off, info->enet_if);
259 board_ft_fman_fixup_port(blob, prop, paddr, info->port, off);
Igal Liberman97a8d012015-08-18 14:47:05 +0300260 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500261 }
262
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000263#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800264#ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000265 /*
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800266 * On T2/T4 SoCs, physically FM1_DTSEC9 and FM1_10GEC1 use the same
267 * dual-role MAC, when FM1_10GEC1 is enabled and FM1_DTSEC9
268 * is disabled, ensure that the dual-role MAC is not disabled,
269 * ditto for other dual-role MACs.
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000270 */
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800271 if (((info->port == FM1_DTSEC9) && (PORT_IS_ENABLED(FM1_10GEC1))) ||
272 ((info->port == FM1_DTSEC10) && (PORT_IS_ENABLED(FM1_10GEC2))) ||
273 ((info->port == FM1_DTSEC1) && (PORT_IS_ENABLED(FM1_10GEC3))) ||
274 ((info->port == FM1_DTSEC2) && (PORT_IS_ENABLED(FM1_10GEC4))) ||
275 ((info->port == FM1_10GEC1) && (PORT_IS_ENABLED(FM1_DTSEC9))) ||
276 ((info->port == FM1_10GEC2) && (PORT_IS_ENABLED(FM1_DTSEC10))) ||
277 ((info->port == FM1_10GEC3) && (PORT_IS_ENABLED(FM1_DTSEC1))) ||
278 ((info->port == FM1_10GEC4) && (PORT_IS_ENABLED(FM1_DTSEC2)))
Tom Rinicdc5ed82022-11-16 13:10:29 -0500279#if (CFG_SYS_NUM_FMAN == 2)
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000280 ||
281 ((info->port == FM2_DTSEC9) && (PORT_IS_ENABLED(FM2_10GEC1))) ||
282 ((info->port == FM2_DTSEC10) && (PORT_IS_ENABLED(FM2_10GEC2))) ||
283 ((info->port == FM2_10GEC1) && (PORT_IS_ENABLED(FM2_DTSEC9))) ||
284 ((info->port == FM2_10GEC2) && (PORT_IS_ENABLED(FM2_DTSEC10)))
285#endif
Shengzhou Liu24d827f2014-12-03 15:27:03 +0800286#else
287 /* FM1_DTSECx and FM1_10GECx use the same dual-role MAC */
288 if (((info->port == FM1_DTSEC1) && (PORT_IS_ENABLED(FM1_10GEC1))) ||
289 ((info->port == FM1_DTSEC2) && (PORT_IS_ENABLED(FM1_10GEC2))) ||
290 ((info->port == FM1_DTSEC3) && (PORT_IS_ENABLED(FM1_10GEC3))) ||
291 ((info->port == FM1_DTSEC4) && (PORT_IS_ENABLED(FM1_10GEC4))) ||
292 ((info->port == FM1_10GEC1) && (PORT_IS_ENABLED(FM1_DTSEC1))) ||
293 ((info->port == FM1_10GEC2) && (PORT_IS_ENABLED(FM1_DTSEC2))) ||
294 ((info->port == FM1_10GEC3) && (PORT_IS_ENABLED(FM1_DTSEC3))) ||
295 ((info->port == FM1_10GEC4) && (PORT_IS_ENABLED(FM1_DTSEC4)))
296#endif
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000297 )
Igal Liberman97a8d012015-08-18 14:47:05 +0300298 return 0;
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000299#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500300 /* board code might have caused offset to change */
301 off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
302
Shaohui Xieea1332c2015-12-04 10:22:03 +0800303#ifndef CONFIG_SYS_FMAN_V3
Kumar Galae81c0ab2011-10-13 14:55:59 -0500304 /* Don't disable FM1-DTSEC1 MAC as its used for MDIO */
Timur Tabi4376b4c2012-08-14 06:47:24 +0000305 if (paddr != dtsec1_addr)
Shaohui Xieea1332c2015-12-04 10:22:03 +0800306#endif
Timur Tabi4376b4c2012-08-14 06:47:24 +0000307 fdt_status_disabled(blob, off); /* disable the MAC node */
Kumar Galac916d7c2011-04-13 08:37:44 -0500308
Timur Tabi4376b4c2012-08-14 06:47:24 +0000309 /* disable the fsl,dpa-ethernet node that points to the MAC */
Kumar Galac916d7c2011-04-13 08:37:44 -0500310 ph = fdt_get_phandle(blob, off);
311 do_fixup_by_prop(blob, "fsl,fman-mac", &ph, sizeof(ph),
312 "status", "disabled", strlen("disabled") + 1, 1);
Igal Liberman97a8d012015-08-18 14:47:05 +0300313
314 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500315}
316
317void fdt_fixup_fman_ethernet(void *blob)
318{
319 int i;
320
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000321#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liu43994342014-01-13 15:32:24 +0800322 for (i = 0; i < ARRAY_SIZE(fm_info); i++)
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000323 ft_fixup_port(blob, &fm_info[i], "fsl,fman-memac");
324#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500325 for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
Igal Liberman97a8d012015-08-18 14:47:05 +0300326 /* Try the new compatible first.
327 * If the node is missing, try the old.
328 */
329 if (fm_info[i].type == FM_ETH_1G_E) {
330 if (ft_fixup_port(blob, &fm_info[i], "fsl,fman-dtsec"))
331 ft_fixup_port(blob, &fm_info[i],
332 "fsl,fman-1g-mac");
333 } else {
Zhao Qiangdb947fa2018-08-29 11:46:34 +0800334 if (ft_fixup_port(blob, &fm_info[i], "fsl,fman-xgec") &&
335 ft_fixup_port(blob, &fm_info[i], "fsl,fman-tgec"))
Igal Liberman97a8d012015-08-18 14:47:05 +0300336 ft_fixup_port(blob, &fm_info[i],
337 "fsl,fman-10g-mac");
338 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500339 }
Shengzhou Liuae8a5d12013-03-25 07:39:29 +0000340#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500341}
Zhao Qiangffee1dd2013-09-04 10:11:27 +0800342
343/*QSGMII Riser Card can work in SGMII mode, but the PHY address is different.
344 *This function scans which Riser Card being used(QSGMII or SGMII Riser Card),
345 *then set the correct PHY address
346 */
347void set_sgmii_phy(struct mii_dev *bus, enum fm_port base_port,
348 unsigned int port_num, int phy_base_addr)
349{
350 unsigned int regnum = 0;
351 int qsgmii;
352 int i;
353 int phy_real_addr;
354
355 qsgmii = is_qsgmii_riser_card(bus, phy_base_addr, port_num, regnum);
356
357 if (!qsgmii)
358 return;
359
360 for (i = base_port; i < base_port + port_num; i++) {
361 if (fm_info_get_enet_if(i) == PHY_INTERFACE_MODE_SGMII) {
362 phy_real_addr = phy_base_addr + i - base_port;
363 fm_info_set_phy_address(i, phy_real_addr);
364 }
365 }
366}
367
368/*to check whether qsgmii riser card is used*/
369int is_qsgmii_riser_card(struct mii_dev *bus, int phy_base_addr,
370 unsigned int port_num, unsigned regnum)
371{
372 int i;
373 int val;
374
375 if (!bus)
376 return 0;
377
378 for (i = phy_base_addr; i < phy_base_addr + port_num; i++) {
379 val = bus->read(bus, i, MDIO_DEVAD_NONE, regnum);
380 if (val != MIIM_TIMEOUT)
381 return 1;
382 }
383
384 return 0;
385}
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300386#endif /* CONFIG_DM_ETH */