blob: c6e5bf21cf05a1799b3339cb3ee4ad6636d3f141 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -04002/*
3 * Ethernet driver for TI K2HK EVM.
4 *
5 * (C) Copyright 2012-2014
6 * Texas Instruments Incorporated, <www.ti.com>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -04007 */
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -04008#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040013
Mugunthan V Nc8386732016-02-02 15:51:33 +053014#include <dm.h>
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +053015#include <dm/lists.h>
Mugunthan V Nc8386732016-02-02 15:51:33 +053016
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040017#include <net.h>
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +030018#include <phy.h>
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +030019#include <errno.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040020#include <miiphy.h>
21#include <malloc.h>
Khoronzhuk, Ivan7954b862014-09-05 19:02:47 +030022#include <asm/ti-common/keystone_nav.h>
Khoronzhuk, Ivanf2c13ba2014-09-29 22:17:22 +030023#include <asm/ti-common/keystone_net.h>
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +030024#include <asm/ti-common/keystone_serdes.h>
Mugunthan V Nc8386732016-02-02 15:51:33 +053025#include <asm/arch/psc_defs.h>
26
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -050027#include "cpsw_mdio.h"
28
Mugunthan V Nc8386732016-02-02 15:51:33 +053029DECLARE_GLOBAL_DATA_PTR;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040030
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040031#ifdef KEYSTONE2_EMAC_GIG_ENABLE
32#define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x)
33#else
34#define emac_gigabit_enable(x) /* no gigabit to enable */
35#endif
36
37#define RX_BUFF_NUMS 24
38#define RX_BUFF_LEN 1520
39#define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +030040#define SGMII_ANEG_TIMEOUT 4000
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040041
42static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16);
43
Mugunthan V Nc8386732016-02-02 15:51:33 +053044enum link_type {
Mugunthan V N17819112016-08-11 20:04:03 +053045 LINK_TYPE_SGMII_MAC_TO_MAC_AUTO = 0,
46 LINK_TYPE_SGMII_MAC_TO_PHY_MODE = 1,
47 LINK_TYPE_SGMII_MAC_TO_MAC_FORCED_MODE = 2,
48 LINK_TYPE_SGMII_MAC_TO_FIBRE_MODE = 3,
49 LINK_TYPE_SGMII_MAC_TO_PHY_NO_MDIO_MODE = 4,
50 LINK_TYPE_RGMII_LINK_MAC_PHY = 5,
51 LINK_TYPE_RGMII_LINK_MAC_MAC_FORCED = 6,
52 LINK_TYPE_RGMII_LINK_MAC_PHY_NO_MDIO = 7,
53 LINK_TYPE_10G_MAC_TO_PHY_MODE = 10,
54 LINK_TYPE_10G_MAC_TO_MAC_FORCED_MODE = 11,
Mugunthan V Nc8386732016-02-02 15:51:33 +053055};
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040056
Mugunthan V Nc8386732016-02-02 15:51:33 +053057#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
58 ((mac)[2] << 16) | ((mac)[3] << 24))
59#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040060
Mugunthan V Nc8386732016-02-02 15:51:33 +053061#ifdef CONFIG_KSNET_NETCP_V1_0
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040062
Mugunthan V Nc8386732016-02-02 15:51:33 +053063#define EMAC_EMACSW_BASE_OFS 0x90800
64#define EMAC_EMACSW_PORT_BASE_OFS (EMAC_EMACSW_BASE_OFS + 0x60)
65
66/* CPSW Switch slave registers */
67#define CPGMACSL_REG_SA_LO 0x10
68#define CPGMACSL_REG_SA_HI 0x14
69
70#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
71 (x) * 0x30)
72
Grygorii Strashko6519ea62018-10-31 16:21:41 -050073#elif defined(CONFIG_KSNET_NETCP_V1_5)
Mugunthan V Nc8386732016-02-02 15:51:33 +053074
75#define EMAC_EMACSW_PORT_BASE_OFS 0x222000
76
77/* CPSW Switch slave registers */
78#define CPGMACSL_REG_SA_LO 0x308
79#define CPGMACSL_REG_SA_HI 0x30c
80
81#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
82 (x) * 0x1000)
83
84#endif
85
86
87struct ks2_eth_priv {
88 struct udevice *dev;
89 struct phy_device *phydev;
90 struct mii_dev *mdio_bus;
91 int phy_addr;
92 phy_interface_t phy_if;
Marek Behún1d371f32022-04-11 21:20:54 +020093 ofnode phy_ofnode;
Mugunthan V Nc8386732016-02-02 15:51:33 +053094 int sgmii_link_type;
Marek Behún6f897ec2022-04-11 21:20:55 +020095 phys_addr_t mdio_base;
Mugunthan V Nc8386732016-02-02 15:51:33 +053096 struct rx_buff_desc net_rx_buffs;
97 struct pktdma_cfg *netcp_pktdma;
98 void *hd;
99 int slave_port;
100 enum link_type link_type;
101 bool emac_open;
102 bool has_mdio;
103};
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400104
Mugunthan V Nc8386732016-02-02 15:51:33 +0530105static void __attribute__((unused))
106 keystone2_eth_gigabit_enable(struct udevice *dev)
107{
108 struct ks2_eth_priv *priv = dev_get_priv(dev);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530109
110 /*
111 * Check if link detected is giga-bit
112 * If Gigabit mode detected, enable gigbit in MAC
113 */
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500114 if (priv->has_mdio) {
115 if (priv->phydev->speed != 1000)
116 return;
117 }
118
Mugunthan V Nc8386732016-02-02 15:51:33 +0530119 writel(readl(DEVICE_EMACSL_BASE(priv->slave_port - 1) +
120 CPGMACSL_REG_CTL) |
121 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
122 DEVICE_EMACSL_BASE(priv->slave_port - 1) + CPGMACSL_REG_CTL);
123}
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400124
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530125#ifdef CONFIG_SOC_K2G
126int keystone_rgmii_config(struct phy_device *phy_dev)
127{
128 unsigned int i, status;
129
130 i = 0;
131 do {
132 if (i > SGMII_ANEG_TIMEOUT) {
133 puts(" TIMEOUT !\n");
134 phy_dev->link = 0;
135 return 0;
136 }
137
138 if (ctrlc()) {
139 puts("user interrupt!\n");
140 phy_dev->link = 0;
141 return -EINTR;
142 }
143
144 if ((i++ % 500) == 0)
145 printf(".");
146
147 udelay(1000); /* 1 ms */
148 status = readl(RGMII_STATUS_REG);
149 } while (!(status & RGMII_REG_STATUS_LINK));
150
151 puts(" done\n");
152
153 return 0;
154}
155#else
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300156int keystone_sgmii_config(struct phy_device *phy_dev, int port, int interface)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400157{
158 unsigned int i, status, mask;
159 unsigned int mr_adv_ability, control;
160
161 switch (interface) {
162 case SGMII_LINK_MAC_MAC_AUTONEG:
163 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
164 SGMII_REG_MR_ADV_LINK |
165 SGMII_REG_MR_ADV_FULL_DUPLEX |
166 SGMII_REG_MR_ADV_GIG_MODE);
167 control = (SGMII_REG_CONTROL_MASTER |
168 SGMII_REG_CONTROL_AUTONEG);
169
170 break;
171 case SGMII_LINK_MAC_PHY:
172 case SGMII_LINK_MAC_PHY_FORCED:
173 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
174 control = SGMII_REG_CONTROL_AUTONEG;
175
176 break;
177 case SGMII_LINK_MAC_MAC_FORCED:
178 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
179 SGMII_REG_MR_ADV_LINK |
180 SGMII_REG_MR_ADV_FULL_DUPLEX |
181 SGMII_REG_MR_ADV_GIG_MODE);
182 control = SGMII_REG_CONTROL_MASTER;
183
184 break;
185 case SGMII_LINK_MAC_FIBER:
186 mr_adv_ability = 0x20;
187 control = SGMII_REG_CONTROL_AUTONEG;
188
189 break;
190 default:
191 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
192 control = SGMII_REG_CONTROL_AUTONEG;
193 }
194
195 __raw_writel(0, SGMII_CTL_REG(port));
196
197 /*
198 * Wait for the SerDes pll to lock,
199 * but don't trap if lock is never read
200 */
201 for (i = 0; i < 1000; i++) {
202 udelay(2000);
203 status = __raw_readl(SGMII_STATUS_REG(port));
204 if ((status & SGMII_REG_STATUS_LOCK) != 0)
205 break;
206 }
207
208 __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
209 __raw_writel(control, SGMII_CTL_REG(port));
210
211
212 mask = SGMII_REG_STATUS_LINK;
213
214 if (control & SGMII_REG_CONTROL_AUTONEG)
215 mask |= SGMII_REG_STATUS_AUTONEG;
216
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300217 status = __raw_readl(SGMII_STATUS_REG(port));
218 if ((status & mask) == mask)
219 return 0;
220
221 printf("\n%s Waiting for SGMII auto negotiation to complete",
222 phy_dev->dev->name);
223 while ((status & mask) != mask) {
224 /*
225 * Timeout reached ?
226 */
227 if (i > SGMII_ANEG_TIMEOUT) {
228 puts(" TIMEOUT !\n");
229 phy_dev->link = 0;
230 return 0;
231 }
232
233 if (ctrlc()) {
234 puts("user interrupt!\n");
235 phy_dev->link = 0;
236 return -EINTR;
237 }
238
239 if ((i++ % 500) == 0)
240 printf(".");
241
242 udelay(1000); /* 1 ms */
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400243 status = __raw_readl(SGMII_STATUS_REG(port));
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400244 }
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300245 puts(" done\n");
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400246
247 return 0;
248}
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530249#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400250
251int mac_sl_reset(u32 port)
252{
253 u32 i, v;
254
255 if (port >= DEVICE_N_GMACSL_PORTS)
256 return GMACSL_RET_INVALID_PORT;
257
258 /* Set the soft reset bit */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300259 writel(CPGMAC_REG_RESET_VAL_RESET,
260 DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400261
262 /* Wait for the bit to clear */
263 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300264 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400265 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
266 CPGMAC_REG_RESET_VAL_RESET)
267 return GMACSL_RET_OK;
268 }
269
270 /* Timeout on the reset */
271 return GMACSL_RET_WARN_RESET_INCOMPLETE;
272}
273
274int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
275{
276 u32 v, i;
277 int ret = GMACSL_RET_OK;
278
279 if (port >= DEVICE_N_GMACSL_PORTS)
280 return GMACSL_RET_INVALID_PORT;
281
282 if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
283 cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
284 ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
285 }
286
287 /* Must wait if the device is undergoing reset */
288 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300289 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400290 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
291 CPGMAC_REG_RESET_VAL_RESET)
292 break;
293 }
294
295 if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
296 return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
297
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300298 writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
299 writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400300
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530301#ifndef CONFIG_SOC_K2HK
Khoronzhuk, Ivana7894b72014-10-17 21:01:14 +0300302 /* Map RX packet flow priority to 0 */
303 writel(0, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RX_PRI_MAP);
304#endif
305
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400306 return ret;
307}
308
309int ethss_config(u32 ctl, u32 max_pkt_size)
310{
311 u32 i;
312
313 /* Max length register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300314 writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400315
316 /* Control register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300317 writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400318
319 /* All statistics enabled by default */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300320 writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
321 DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400322
323 /* Reset and enable the ALE */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300324 writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
325 CPSW_REG_VAL_ALE_CTL_BYPASS,
326 DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400327
328 /* All ports put into forward mode */
329 for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300330 writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
331 DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400332
333 return 0;
334}
335
336int ethss_start(void)
337{
338 int i;
339 struct mac_sl_cfg cfg;
340
341 cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
342 cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
343
344 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
345 mac_sl_reset(i);
346 mac_sl_config(i, &cfg);
347 }
348
349 return 0;
350}
351
352int ethss_stop(void)
353{
354 int i;
355
356 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
357 mac_sl_reset(i);
358
359 return 0;
360}
361
Mugunthan V Nc8386732016-02-02 15:51:33 +0530362struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
363 .clk = SERDES_CLOCK_156P25M,
364 .rate = SERDES_RATE_5G,
365 .rate_mode = SERDES_QUARTER_RATE,
366 .intf = SERDES_PHY_SGMII,
367 .loopback = 0,
368};
369
370#ifndef CONFIG_SOC_K2G
371static void keystone2_net_serdes_setup(void)
372{
Tom Rinic71cb122022-12-04 10:04:30 -0500373 ks2_serdes_init(CFG_KSNET_SERDES_SGMII_BASE,
Mugunthan V Nc8386732016-02-02 15:51:33 +0530374 &ks2_serdes_sgmii_156p25mhz,
Tom Rinifd130b82022-12-04 10:04:28 -0500375 CFG_KSNET_SERDES_LANES_PER_SGMII);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530376
377#if defined(CONFIG_SOC_K2E) || defined(CONFIG_SOC_K2L)
Tom Rinic2e481e2022-12-04 10:04:29 -0500378 ks2_serdes_init(CFG_KSNET_SERDES_SGMII2_BASE,
Mugunthan V Nc8386732016-02-02 15:51:33 +0530379 &ks2_serdes_sgmii_156p25mhz,
Tom Rinifd130b82022-12-04 10:04:28 -0500380 CFG_KSNET_SERDES_LANES_PER_SGMII);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530381#endif
382
383 /* wait till setup */
384 udelay(5000);
385}
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530386#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400387
Mugunthan V Nc8386732016-02-02 15:51:33 +0530388static int ks2_eth_start(struct udevice *dev)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400389{
Mugunthan V Nc8386732016-02-02 15:51:33 +0530390 struct ks2_eth_priv *priv = dev_get_priv(dev);
Hao Zhangd890dff2014-10-22 17:18:23 +0300391
Mugunthan V Nc8386732016-02-02 15:51:33 +0530392#ifdef CONFIG_SOC_K2G
393 keystone_rgmii_config(priv->phydev);
394#else
395 keystone_sgmii_config(priv->phydev, priv->slave_port - 1,
396 priv->sgmii_link_type);
Khoronzhuk, Ivan3df3e632014-10-17 21:01:13 +0300397#endif
398
Mugunthan V Nc8386732016-02-02 15:51:33 +0530399 udelay(10000);
400
401 /* On chip switch configuration */
402 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
403
404 qm_init();
405
406 if (ksnav_init(priv->netcp_pktdma, &priv->net_rx_buffs)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900407 pr_err("ksnav_init failed\n");
Mugunthan V Nc8386732016-02-02 15:51:33 +0530408 goto err_knav_init;
409 }
410
411 /*
412 * Streaming switch configuration. If not present this
413 * statement is defined to void in target.h.
414 * If present this is usually defined to a series of register writes
415 */
416 hw_config_streaming_switch();
417
418 if (priv->has_mdio) {
419 phy_startup(priv->phydev);
420 if (priv->phydev->link == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900421 pr_err("phy startup failed\n");
Mugunthan V Nc8386732016-02-02 15:51:33 +0530422 goto err_phy_start;
423 }
424 }
425
426 emac_gigabit_enable(dev);
427
428 ethss_start();
429
430 priv->emac_open = true;
431
432 return 0;
433
434err_phy_start:
435 ksnav_close(priv->netcp_pktdma);
436err_knav_init:
437 qm_close();
438
439 return -EFAULT;
440}
441
442static int ks2_eth_send(struct udevice *dev, void *packet, int length)
443{
444 struct ks2_eth_priv *priv = dev_get_priv(dev);
445
446 genphy_update_link(priv->phydev);
447 if (priv->phydev->link == 0)
448 return -1;
449
450 if (length < EMAC_MIN_ETHERNET_PKT_SIZE)
451 length = EMAC_MIN_ETHERNET_PKT_SIZE;
452
453 return ksnav_send(priv->netcp_pktdma, (u32 *)packet,
454 length, (priv->slave_port) << 16);
455}
456
457static int ks2_eth_recv(struct udevice *dev, int flags, uchar **packetp)
458{
459 struct ks2_eth_priv *priv = dev_get_priv(dev);
460 int pkt_size;
461 u32 *pkt = NULL;
462
463 priv->hd = ksnav_recv(priv->netcp_pktdma, &pkt, &pkt_size);
464 if (priv->hd == NULL)
465 return -EAGAIN;
466
467 *packetp = (uchar *)pkt;
468
469 return pkt_size;
470}
471
472static int ks2_eth_free_pkt(struct udevice *dev, uchar *packet,
473 int length)
474{
475 struct ks2_eth_priv *priv = dev_get_priv(dev);
476
477 ksnav_release_rxhd(priv->netcp_pktdma, priv->hd);
478
479 return 0;
480}
481
482static void ks2_eth_stop(struct udevice *dev)
483{
484 struct ks2_eth_priv *priv = dev_get_priv(dev);
485
486 if (!priv->emac_open)
487 return;
488 ethss_stop();
489
490 ksnav_close(priv->netcp_pktdma);
491 qm_close();
492 phy_shutdown(priv->phydev);
493 priv->emac_open = false;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400494}
Mugunthan V Nc8386732016-02-02 15:51:33 +0530495
496int ks2_eth_read_rom_hwaddr(struct udevice *dev)
497{
498 struct ks2_eth_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700499 struct eth_pdata *pdata = dev_get_plat(dev);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530500 u32 maca = 0;
501 u32 macb = 0;
502
503 /* Read the e-fuse mac address */
504 if (priv->slave_port == 1) {
505 maca = __raw_readl(MAC_ID_BASE_ADDR);
506 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
507 }
508
509 pdata->enetaddr[0] = (macb >> 8) & 0xff;
510 pdata->enetaddr[1] = (macb >> 0) & 0xff;
511 pdata->enetaddr[2] = (maca >> 24) & 0xff;
512 pdata->enetaddr[3] = (maca >> 16) & 0xff;
513 pdata->enetaddr[4] = (maca >> 8) & 0xff;
514 pdata->enetaddr[5] = (maca >> 0) & 0xff;
515
516 return 0;
517}
518
519int ks2_eth_write_hwaddr(struct udevice *dev)
520{
521 struct ks2_eth_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700522 struct eth_pdata *pdata = dev_get_plat(dev);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530523
524 writel(mac_hi(pdata->enetaddr),
525 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
526 CPGMACSL_REG_SA_HI);
527 writel(mac_lo(pdata->enetaddr),
528 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
529 CPGMACSL_REG_SA_LO);
530
531 return 0;
532}
533
534static int ks2_eth_probe(struct udevice *dev)
535{
536 struct ks2_eth_priv *priv = dev_get_priv(dev);
537 struct mii_dev *mdio_bus;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530538
539 priv->dev = dev;
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500540 priv->emac_open = false;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530541
542 /* These clock enables has to be moved to common location */
543 if (cpu_is_k2g())
544 writel(KS2_ETHERNET_RGMII, KS2_ETHERNET_CFG);
545
546 /* By default, select PA PLL clock as PA clock source */
547#ifndef CONFIG_SOC_K2G
548 if (psc_enable_module(KS2_LPSC_PA))
549 return -EACCES;
550#endif
551 if (psc_enable_module(KS2_LPSC_CPGMAC))
552 return -EACCES;
553 if (psc_enable_module(KS2_LPSC_CRYPTO))
554 return -EACCES;
555
556 if (cpu_is_k2e() || cpu_is_k2l())
557 pll_pa_clk_sel();
558
Mugunthan V Nb499a3c2016-08-02 12:01:11 +0530559 priv->net_rx_buffs.buff_ptr = rx_buffs;
560 priv->net_rx_buffs.num_buffs = RX_BUFF_NUMS;
561 priv->net_rx_buffs.buff_len = RX_BUFF_LEN;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530562
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530563 if (priv->slave_port == 1) {
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500564#ifndef CONFIG_SOC_K2G
565 keystone2_net_serdes_setup();
566#endif
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530567 /*
568 * Register MDIO bus for slave 0 only, other slave have
569 * to re-use the same
570 */
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500571 mdio_bus = cpsw_mdio_init("ethernet-mdio",
Marek Behún6f897ec2022-04-11 21:20:55 +0200572 priv->mdio_base,
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500573 EMAC_MDIO_CLOCK_FREQ,
Ravi Gunasekaran40cea492022-09-22 15:21:23 +0530574 EMAC_MDIO_BUS_FREQ,
575 false);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530576 if (!mdio_bus) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900577 pr_err("MDIO alloc failed\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530578 return -ENOMEM;
579 }
580 priv->mdio_bus = mdio_bus;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530581 } else {
582 /* Get the MDIO bus from slave 0 device */
583 struct ks2_eth_priv *parent_priv;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530584
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530585 parent_priv = dev_get_priv(dev->parent);
586 priv->mdio_bus = parent_priv->mdio_bus;
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500587 priv->mdio_base = parent_priv->mdio_base;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530588 }
589
Mugunthan V Nc8386732016-02-02 15:51:33 +0530590 priv->netcp_pktdma = &netcp_pktdma;
591
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530592 if (priv->has_mdio) {
593 priv->phydev = phy_connect(priv->mdio_bus, priv->phy_addr,
594 dev, priv->phy_if);
Tom Rinica15d0d2022-11-27 10:25:27 -0500595 if (ofnode_valid(priv->phy_ofnode))
596 priv->phydev->node = priv->phy_ofnode;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530597 phy_config(priv->phydev);
598 }
Mugunthan V Nc8386732016-02-02 15:51:33 +0530599
600 return 0;
601}
602
603int ks2_eth_remove(struct udevice *dev)
604{
605 struct ks2_eth_priv *priv = dev_get_priv(dev);
606
Grygorii Strashkoa2c2e4a2018-10-31 16:21:45 -0500607 cpsw_mdio_free(priv->mdio_bus);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530608
609 return 0;
610}
611
612static const struct eth_ops ks2_eth_ops = {
613 .start = ks2_eth_start,
614 .send = ks2_eth_send,
615 .recv = ks2_eth_recv,
616 .free_pkt = ks2_eth_free_pkt,
617 .stop = ks2_eth_stop,
618 .read_rom_hwaddr = ks2_eth_read_rom_hwaddr,
619 .write_hwaddr = ks2_eth_write_hwaddr,
620};
621
Marek Behún1d371f32022-04-11 21:20:54 +0200622static int ks2_bind_one_slave(struct udevice *dev, ofnode slave, ofnode *gbe_0)
Mugunthan V Nc8386732016-02-02 15:51:33 +0530623{
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530624 char *slave_name;
Marek Behún1d371f32022-04-11 21:20:54 +0200625 u32 slave_no;
Marek Behún92336d32022-04-11 21:20:53 +0200626 int ret;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530627
Marek Behún1d371f32022-04-11 21:20:54 +0200628 if (ofnode_read_u32(slave, "slave-port", &slave_no))
Marek Behún92336d32022-04-11 21:20:53 +0200629 return 0;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530630
Marek Behún92336d32022-04-11 21:20:53 +0200631 if (gbe_0 && slave_no == 0) {
632 /* This is the current eth device */
633 *gbe_0 = slave;
634 return 0;
635 }
636
637 /* Slave devices to be registered */
638 slave_name = malloc(20);
639 snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
Marek Behún1d371f32022-04-11 21:20:54 +0200640 ret = device_bind_driver_to_node(dev, "eth_ks2_sl", slave_name, slave,
641 NULL);
Marek Behún92336d32022-04-11 21:20:53 +0200642 if (ret)
643 pr_err("ks2_net - not able to bind slave interfaces\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530644
Marek Behún92336d32022-04-11 21:20:53 +0200645 return ret;
646}
647
Marek Behún1d371f32022-04-11 21:20:54 +0200648static int ks2_eth_bind_slaves(struct udevice *dev, ofnode gbe, ofnode *gbe_0)
Marek Behún92336d32022-04-11 21:20:53 +0200649{
Marek Behún1d371f32022-04-11 21:20:54 +0200650 ofnode interfaces, sec_slave, slave;
651 int ret;
Marek Behún92336d32022-04-11 21:20:53 +0200652
Marek Behún1d371f32022-04-11 21:20:54 +0200653 interfaces = ofnode_find_subnode(gbe, "interfaces");
654 ofnode_for_each_subnode(slave, interfaces) {
Marek Behún92336d32022-04-11 21:20:53 +0200655 ret = ks2_bind_one_slave(dev, slave, gbe_0);
656 if (ret)
657 return ret;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530658 }
659
Marek Behún1d371f32022-04-11 21:20:54 +0200660 sec_slave = ofnode_find_subnode(gbe, "secondary-slave-ports");
661 ofnode_for_each_subnode(slave, sec_slave) {
Marek Behún92336d32022-04-11 21:20:53 +0200662 ret = ks2_bind_one_slave(dev, slave, NULL);
663 if (ret)
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530664 return ret;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530665 }
666
667 return 0;
668}
669
Marek Behún1d371f32022-04-11 21:20:54 +0200670static int ks2_eth_parse_slave_interface(ofnode netcp, ofnode slave,
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530671 struct ks2_eth_priv *priv,
672 struct eth_pdata *pdata)
673{
Marek Behún1d371f32022-04-11 21:20:54 +0200674 struct ofnode_phandle_args dma_args;
675 ofnode phy, mdio;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530676 int dma_count;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530677
Marek Behún1d371f32022-04-11 21:20:54 +0200678 priv->slave_port = ofnode_read_s32_default(slave, "slave-port", -1);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530679 priv->net_rx_buffs.rx_flow = priv->slave_port * 8;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530680
Mugunthan V Nc8386732016-02-02 15:51:33 +0530681 /* U-Boot slave port number starts with 1 instead of 0 */
682 priv->slave_port += 1;
683
Marek Behún1d371f32022-04-11 21:20:54 +0200684 dma_count = ofnode_count_phandle_with_args(netcp, "ti,navigator-dmas",
685 NULL, 1);
686 if (priv->slave_port < dma_count &&
687 !ofnode_parse_phandle_with_args(netcp, "ti,navigator-dmas", NULL, 1,
688 priv->slave_port - 1, &dma_args))
689 priv->net_rx_buffs.rx_flow = dma_args.args[0];
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530690
Marek Behún1d371f32022-04-11 21:20:54 +0200691 priv->link_type = ofnode_read_s32_default(slave, "link-interface", -1);
Murali Karicheri3c796482019-02-21 12:02:03 -0500692
Marek Behún1d371f32022-04-11 21:20:54 +0200693 phy = ofnode_get_phy_node(slave);
694 priv->phy_ofnode = phy;
695 if (ofnode_valid(phy)) {
696 priv->phy_addr = ofnode_read_s32_default(phy, "reg", -1);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530697
Marek Behún1d371f32022-04-11 21:20:54 +0200698 mdio = ofnode_get_parent(phy);
699 if (!ofnode_valid(mdio)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900700 pr_err("mdio dt not found\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530701 return -ENODEV;
702 }
Marek Behún6f897ec2022-04-11 21:20:55 +0200703 priv->mdio_base = ofnode_get_addr(mdio);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530704 }
Mugunthan V Nc8386732016-02-02 15:51:33 +0530705
Mugunthan V N17819112016-08-11 20:04:03 +0530706 if (priv->link_type == LINK_TYPE_SGMII_MAC_TO_PHY_MODE) {
Mugunthan V Nc8386732016-02-02 15:51:33 +0530707 priv->phy_if = PHY_INTERFACE_MODE_SGMII;
708 pdata->phy_interface = priv->phy_if;
709 priv->sgmii_link_type = SGMII_LINK_MAC_PHY;
710 priv->has_mdio = true;
Mugunthan V N17819112016-08-11 20:04:03 +0530711 } else if (priv->link_type == LINK_TYPE_RGMII_LINK_MAC_PHY) {
Marek Behún1d371f32022-04-11 21:20:54 +0200712 priv->phy_if = ofnode_read_phy_mode(slave);
Marek Behún48631e42022-04-07 00:33:03 +0200713 if (priv->phy_if == PHY_INTERFACE_MODE_NA)
Murali Karicheri3c796482019-02-21 12:02:03 -0500714 priv->phy_if = PHY_INTERFACE_MODE_RGMII;
Mugunthan V N17819112016-08-11 20:04:03 +0530715 pdata->phy_interface = priv->phy_if;
Marek Behúnbc194772022-04-07 00:33:01 +0200716
717 if (priv->phy_if != PHY_INTERFACE_MODE_RGMII &&
718 priv->phy_if != PHY_INTERFACE_MODE_RGMII_ID &&
719 priv->phy_if != PHY_INTERFACE_MODE_RGMII_RXID &&
720 priv->phy_if != PHY_INTERFACE_MODE_RGMII_TXID) {
721 pr_err("invalid phy-mode\n");
722 return -EINVAL;
723 }
724
Mugunthan V N17819112016-08-11 20:04:03 +0530725 priv->has_mdio = true;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530726 }
Mugunthan V Nc8386732016-02-02 15:51:33 +0530727
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530728 return 0;
729}
730
Simon Glassaad29ae2020-12-03 16:55:21 -0700731static int ks2_sl_eth_of_to_plat(struct udevice *dev)
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530732{
Marek Behún1d371f32022-04-11 21:20:54 +0200733 ofnode slave, interfaces, gbe, netcp_devices, netcp;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530734 struct ks2_eth_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700735 struct eth_pdata *pdata = dev_get_plat(dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530736
Marek Behún1d371f32022-04-11 21:20:54 +0200737 slave = dev_ofnode(dev);
738 interfaces = ofnode_get_parent(slave);
739 gbe = ofnode_get_parent(interfaces);
740 netcp_devices = ofnode_get_parent(gbe);
741 netcp = ofnode_get_parent(netcp_devices);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530742
743 ks2_eth_parse_slave_interface(netcp, slave, priv, pdata);
744
Marek Behún1d371f32022-04-11 21:20:54 +0200745 pdata->iobase = ofnode_get_addr(netcp);
Mugunthan V Nc8386732016-02-02 15:51:33 +0530746
747 return 0;
748}
749
Simon Glassaad29ae2020-12-03 16:55:21 -0700750static int ks2_eth_of_to_plat(struct udevice *dev)
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530751{
752 struct ks2_eth_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700753 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Behún1d371f32022-04-11 21:20:54 +0200754 ofnode netcp_devices, gbe, gbe_0;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530755
Marek Behún1d371f32022-04-11 21:20:54 +0200756 netcp_devices = dev_read_subnode(dev, "netcp-devices");
757 gbe = ofnode_find_subnode(netcp_devices, "gbe");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530758
Marek Behún1d371f32022-04-11 21:20:54 +0200759 gbe_0 = ofnode_null();
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530760 ks2_eth_bind_slaves(dev, gbe, &gbe_0);
761
Marek Behún1d371f32022-04-11 21:20:54 +0200762 ks2_eth_parse_slave_interface(dev_ofnode(dev), gbe_0, priv, pdata);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530763
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900764 pdata->iobase = dev_read_addr(dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530765
766 return 0;
767}
768
Mugunthan V Nc8386732016-02-02 15:51:33 +0530769static const struct udevice_id ks2_eth_ids[] = {
770 { .compatible = "ti,netcp-1.0" },
771 { }
772};
773
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530774U_BOOT_DRIVER(eth_ks2_slave) = {
775 .name = "eth_ks2_sl",
776 .id = UCLASS_ETH,
Simon Glassaad29ae2020-12-03 16:55:21 -0700777 .of_to_plat = ks2_sl_eth_of_to_plat,
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530778 .probe = ks2_eth_probe,
779 .remove = ks2_eth_remove,
780 .ops = &ks2_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700781 .priv_auto = sizeof(struct ks2_eth_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700782 .plat_auto = sizeof(struct eth_pdata),
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530783 .flags = DM_FLAG_ALLOC_PRIV_DMA,
784};
Mugunthan V Nc8386732016-02-02 15:51:33 +0530785
786U_BOOT_DRIVER(eth_ks2) = {
787 .name = "eth_ks2",
788 .id = UCLASS_ETH,
789 .of_match = ks2_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700790 .of_to_plat = ks2_eth_of_to_plat,
Mugunthan V Nc8386732016-02-02 15:51:33 +0530791 .probe = ks2_eth_probe,
792 .remove = ks2_eth_remove,
793 .ops = &ks2_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700794 .priv_auto = sizeof(struct ks2_eth_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700795 .plat_auto = sizeof(struct eth_pdata),
Mugunthan V Nc8386732016-02-02 15:51:33 +0530796 .flags = DM_FLAG_ALLOC_PRIV_DMA,
797};