blob: a3ba91cc3f5600a8bc928908ffae4b4d33f62ac0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Karicheri, Muralidharanfc9a8e82014-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, Muralidharanfc9a8e82014-04-01 15:01:13 -04007 */
8#include <common.h>
9#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040011
Mugunthan V N6599f362016-02-02 15:51:33 +053012#include <dm.h>
Mugunthan V Na61f6a52016-08-02 12:01:12 +053013#include <dm/lists.h>
Mugunthan V N6599f362016-02-02 15:51:33 +053014
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040015#include <net.h>
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +030016#include <phy.h>
Khoronzhuk, Ivanc05d05e2014-10-17 21:01:15 +030017#include <errno.h>
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040018#include <miiphy.h>
19#include <malloc.h>
Khoronzhuk, Ivanef454712014-09-05 19:02:47 +030020#include <asm/ti-common/keystone_nav.h>
Khoronzhuk, Ivan0935cac2014-09-29 22:17:22 +030021#include <asm/ti-common/keystone_net.h>
Khoronzhuk, Ivana43febd2014-10-22 17:18:21 +030022#include <asm/ti-common/keystone_serdes.h>
Mugunthan V N6599f362016-02-02 15:51:33 +053023#include <asm/arch/psc_defs.h>
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040024
Grygorii Strashko79d81272018-10-31 16:21:45 -050025#include "cpsw_mdio.h"
26
Mugunthan V N6599f362016-02-02 15:51:33 +053027DECLARE_GLOBAL_DATA_PTR;
28
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040029#ifdef KEYSTONE2_EMAC_GIG_ENABLE
30#define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x)
31#else
32#define emac_gigabit_enable(x) /* no gigabit to enable */
33#endif
34
35#define RX_BUFF_NUMS 24
36#define RX_BUFF_LEN 1520
37#define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN
Khoronzhuk, Ivanc05d05e2014-10-17 21:01:15 +030038#define SGMII_ANEG_TIMEOUT 4000
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040039
40static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16);
41
Mugunthan V N6599f362016-02-02 15:51:33 +053042enum link_type {
Mugunthan V N1de40662016-08-11 20:04:03 +053043 LINK_TYPE_SGMII_MAC_TO_MAC_AUTO = 0,
44 LINK_TYPE_SGMII_MAC_TO_PHY_MODE = 1,
45 LINK_TYPE_SGMII_MAC_TO_MAC_FORCED_MODE = 2,
46 LINK_TYPE_SGMII_MAC_TO_FIBRE_MODE = 3,
47 LINK_TYPE_SGMII_MAC_TO_PHY_NO_MDIO_MODE = 4,
48 LINK_TYPE_RGMII_LINK_MAC_PHY = 5,
49 LINK_TYPE_RGMII_LINK_MAC_MAC_FORCED = 6,
50 LINK_TYPE_RGMII_LINK_MAC_PHY_NO_MDIO = 7,
51 LINK_TYPE_10G_MAC_TO_PHY_MODE = 10,
52 LINK_TYPE_10G_MAC_TO_MAC_FORCED_MODE = 11,
Mugunthan V N6599f362016-02-02 15:51:33 +053053};
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040054
Mugunthan V N6599f362016-02-02 15:51:33 +053055#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
56 ((mac)[2] << 16) | ((mac)[3] << 24))
57#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040058
Mugunthan V N6599f362016-02-02 15:51:33 +053059#ifdef CONFIG_KSNET_NETCP_V1_0
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040060
Mugunthan V N6599f362016-02-02 15:51:33 +053061#define EMAC_EMACSW_BASE_OFS 0x90800
62#define EMAC_EMACSW_PORT_BASE_OFS (EMAC_EMACSW_BASE_OFS + 0x60)
63
64/* CPSW Switch slave registers */
65#define CPGMACSL_REG_SA_LO 0x10
66#define CPGMACSL_REG_SA_HI 0x14
67
68#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
69 (x) * 0x30)
70
Grygorii Strashkoaf0cf212018-10-31 16:21:41 -050071#elif defined(CONFIG_KSNET_NETCP_V1_5)
Mugunthan V N6599f362016-02-02 15:51:33 +053072
73#define EMAC_EMACSW_PORT_BASE_OFS 0x222000
74
75/* CPSW Switch slave registers */
76#define CPGMACSL_REG_SA_LO 0x308
77#define CPGMACSL_REG_SA_HI 0x30c
78
79#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
80 (x) * 0x1000)
81
82#endif
83
84
85struct ks2_eth_priv {
86 struct udevice *dev;
87 struct phy_device *phydev;
88 struct mii_dev *mdio_bus;
89 int phy_addr;
90 phy_interface_t phy_if;
91 int sgmii_link_type;
92 void *mdio_base;
93 struct rx_buff_desc net_rx_buffs;
94 struct pktdma_cfg *netcp_pktdma;
95 void *hd;
96 int slave_port;
97 enum link_type link_type;
98 bool emac_open;
99 bool has_mdio;
100};
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400101
Mugunthan V N6599f362016-02-02 15:51:33 +0530102static void __attribute__((unused))
103 keystone2_eth_gigabit_enable(struct udevice *dev)
104{
105 struct ks2_eth_priv *priv = dev_get_priv(dev);
Mugunthan V N6599f362016-02-02 15:51:33 +0530106
107 /*
108 * Check if link detected is giga-bit
109 * If Gigabit mode detected, enable gigbit in MAC
110 */
Grygorii Strashko79d81272018-10-31 16:21:45 -0500111 if (priv->has_mdio) {
112 if (priv->phydev->speed != 1000)
113 return;
114 }
115
Mugunthan V N6599f362016-02-02 15:51:33 +0530116 writel(readl(DEVICE_EMACSL_BASE(priv->slave_port - 1) +
117 CPGMACSL_REG_CTL) |
118 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
119 DEVICE_EMACSL_BASE(priv->slave_port - 1) + CPGMACSL_REG_CTL);
120}
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400121
Vitaly Andrianov4657a2d2015-09-19 16:26:50 +0530122#ifdef CONFIG_SOC_K2G
123int keystone_rgmii_config(struct phy_device *phy_dev)
124{
125 unsigned int i, status;
126
127 i = 0;
128 do {
129 if (i > SGMII_ANEG_TIMEOUT) {
130 puts(" TIMEOUT !\n");
131 phy_dev->link = 0;
132 return 0;
133 }
134
135 if (ctrlc()) {
136 puts("user interrupt!\n");
137 phy_dev->link = 0;
138 return -EINTR;
139 }
140
141 if ((i++ % 500) == 0)
142 printf(".");
143
144 udelay(1000); /* 1 ms */
145 status = readl(RGMII_STATUS_REG);
146 } while (!(status & RGMII_REG_STATUS_LINK));
147
148 puts(" done\n");
149
150 return 0;
151}
152#else
Khoronzhuk, Ivanc05d05e2014-10-17 21:01:15 +0300153int keystone_sgmii_config(struct phy_device *phy_dev, int port, int interface)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400154{
155 unsigned int i, status, mask;
156 unsigned int mr_adv_ability, control;
157
158 switch (interface) {
159 case SGMII_LINK_MAC_MAC_AUTONEG:
160 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
161 SGMII_REG_MR_ADV_LINK |
162 SGMII_REG_MR_ADV_FULL_DUPLEX |
163 SGMII_REG_MR_ADV_GIG_MODE);
164 control = (SGMII_REG_CONTROL_MASTER |
165 SGMII_REG_CONTROL_AUTONEG);
166
167 break;
168 case SGMII_LINK_MAC_PHY:
169 case SGMII_LINK_MAC_PHY_FORCED:
170 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
171 control = SGMII_REG_CONTROL_AUTONEG;
172
173 break;
174 case SGMII_LINK_MAC_MAC_FORCED:
175 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
176 SGMII_REG_MR_ADV_LINK |
177 SGMII_REG_MR_ADV_FULL_DUPLEX |
178 SGMII_REG_MR_ADV_GIG_MODE);
179 control = SGMII_REG_CONTROL_MASTER;
180
181 break;
182 case SGMII_LINK_MAC_FIBER:
183 mr_adv_ability = 0x20;
184 control = SGMII_REG_CONTROL_AUTONEG;
185
186 break;
187 default:
188 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
189 control = SGMII_REG_CONTROL_AUTONEG;
190 }
191
192 __raw_writel(0, SGMII_CTL_REG(port));
193
194 /*
195 * Wait for the SerDes pll to lock,
196 * but don't trap if lock is never read
197 */
198 for (i = 0; i < 1000; i++) {
199 udelay(2000);
200 status = __raw_readl(SGMII_STATUS_REG(port));
201 if ((status & SGMII_REG_STATUS_LOCK) != 0)
202 break;
203 }
204
205 __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
206 __raw_writel(control, SGMII_CTL_REG(port));
207
208
209 mask = SGMII_REG_STATUS_LINK;
210
211 if (control & SGMII_REG_CONTROL_AUTONEG)
212 mask |= SGMII_REG_STATUS_AUTONEG;
213
Khoronzhuk, Ivanc05d05e2014-10-17 21:01:15 +0300214 status = __raw_readl(SGMII_STATUS_REG(port));
215 if ((status & mask) == mask)
216 return 0;
217
218 printf("\n%s Waiting for SGMII auto negotiation to complete",
219 phy_dev->dev->name);
220 while ((status & mask) != mask) {
221 /*
222 * Timeout reached ?
223 */
224 if (i > SGMII_ANEG_TIMEOUT) {
225 puts(" TIMEOUT !\n");
226 phy_dev->link = 0;
227 return 0;
228 }
229
230 if (ctrlc()) {
231 puts("user interrupt!\n");
232 phy_dev->link = 0;
233 return -EINTR;
234 }
235
236 if ((i++ % 500) == 0)
237 printf(".");
238
239 udelay(1000); /* 1 ms */
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400240 status = __raw_readl(SGMII_STATUS_REG(port));
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400241 }
Khoronzhuk, Ivanc05d05e2014-10-17 21:01:15 +0300242 puts(" done\n");
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400243
244 return 0;
245}
Vitaly Andrianov4657a2d2015-09-19 16:26:50 +0530246#endif
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400247
248int mac_sl_reset(u32 port)
249{
250 u32 i, v;
251
252 if (port >= DEVICE_N_GMACSL_PORTS)
253 return GMACSL_RET_INVALID_PORT;
254
255 /* Set the soft reset bit */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300256 writel(CPGMAC_REG_RESET_VAL_RESET,
257 DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400258
259 /* Wait for the bit to clear */
260 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300261 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400262 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
263 CPGMAC_REG_RESET_VAL_RESET)
264 return GMACSL_RET_OK;
265 }
266
267 /* Timeout on the reset */
268 return GMACSL_RET_WARN_RESET_INCOMPLETE;
269}
270
271int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
272{
273 u32 v, i;
274 int ret = GMACSL_RET_OK;
275
276 if (port >= DEVICE_N_GMACSL_PORTS)
277 return GMACSL_RET_INVALID_PORT;
278
279 if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
280 cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
281 ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
282 }
283
284 /* Must wait if the device is undergoing reset */
285 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300286 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400287 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
288 CPGMAC_REG_RESET_VAL_RESET)
289 break;
290 }
291
292 if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
293 return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
294
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300295 writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
296 writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400297
Vitaly Andrianov4657a2d2015-09-19 16:26:50 +0530298#ifndef CONFIG_SOC_K2HK
Khoronzhuk, Ivanff11c762014-10-17 21:01:14 +0300299 /* Map RX packet flow priority to 0 */
300 writel(0, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RX_PRI_MAP);
301#endif
302
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400303 return ret;
304}
305
306int ethss_config(u32 ctl, u32 max_pkt_size)
307{
308 u32 i;
309
310 /* Max length register */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300311 writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400312
313 /* Control register */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300314 writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400315
316 /* All statistics enabled by default */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300317 writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
318 DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400319
320 /* Reset and enable the ALE */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300321 writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
322 CPSW_REG_VAL_ALE_CTL_BYPASS,
323 DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400324
325 /* All ports put into forward mode */
326 for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300327 writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
328 DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400329
330 return 0;
331}
332
333int ethss_start(void)
334{
335 int i;
336 struct mac_sl_cfg cfg;
337
338 cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
339 cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
340
341 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
342 mac_sl_reset(i);
343 mac_sl_config(i, &cfg);
344 }
345
346 return 0;
347}
348
349int ethss_stop(void)
350{
351 int i;
352
353 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
354 mac_sl_reset(i);
355
356 return 0;
357}
358
Mugunthan V N6599f362016-02-02 15:51:33 +0530359struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
360 .clk = SERDES_CLOCK_156P25M,
361 .rate = SERDES_RATE_5G,
362 .rate_mode = SERDES_QUARTER_RATE,
363 .intf = SERDES_PHY_SGMII,
364 .loopback = 0,
365};
366
367#ifndef CONFIG_SOC_K2G
368static void keystone2_net_serdes_setup(void)
369{
370 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE,
371 &ks2_serdes_sgmii_156p25mhz,
372 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
373
374#if defined(CONFIG_SOC_K2E) || defined(CONFIG_SOC_K2L)
375 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII2_BASE,
376 &ks2_serdes_sgmii_156p25mhz,
377 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
378#endif
379
380 /* wait till setup */
381 udelay(5000);
382}
383#endif
384
Mugunthan V N6599f362016-02-02 15:51:33 +0530385static int ks2_eth_start(struct udevice *dev)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400386{
Mugunthan V N6599f362016-02-02 15:51:33 +0530387 struct ks2_eth_priv *priv = dev_get_priv(dev);
Hao Zhang92a16c82014-10-22 17:18:23 +0300388
Mugunthan V N6599f362016-02-02 15:51:33 +0530389#ifdef CONFIG_SOC_K2G
390 keystone_rgmii_config(priv->phydev);
391#else
392 keystone_sgmii_config(priv->phydev, priv->slave_port - 1,
393 priv->sgmii_link_type);
Khoronzhuk, Ivan3c615022014-10-17 21:01:13 +0300394#endif
395
Mugunthan V N6599f362016-02-02 15:51:33 +0530396 udelay(10000);
397
398 /* On chip switch configuration */
399 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
400
401 qm_init();
402
403 if (ksnav_init(priv->netcp_pktdma, &priv->net_rx_buffs)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900404 pr_err("ksnav_init failed\n");
Mugunthan V N6599f362016-02-02 15:51:33 +0530405 goto err_knav_init;
406 }
407
408 /*
409 * Streaming switch configuration. If not present this
410 * statement is defined to void in target.h.
411 * If present this is usually defined to a series of register writes
412 */
413 hw_config_streaming_switch();
414
415 if (priv->has_mdio) {
416 phy_startup(priv->phydev);
417 if (priv->phydev->link == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900418 pr_err("phy startup failed\n");
Mugunthan V N6599f362016-02-02 15:51:33 +0530419 goto err_phy_start;
420 }
421 }
422
423 emac_gigabit_enable(dev);
424
425 ethss_start();
426
427 priv->emac_open = true;
428
429 return 0;
430
431err_phy_start:
432 ksnav_close(priv->netcp_pktdma);
433err_knav_init:
434 qm_close();
435
436 return -EFAULT;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400437}
Mugunthan V N6599f362016-02-02 15:51:33 +0530438
439static int ks2_eth_send(struct udevice *dev, void *packet, int length)
440{
441 struct ks2_eth_priv *priv = dev_get_priv(dev);
442
443 genphy_update_link(priv->phydev);
444 if (priv->phydev->link == 0)
445 return -1;
446
447 if (length < EMAC_MIN_ETHERNET_PKT_SIZE)
448 length = EMAC_MIN_ETHERNET_PKT_SIZE;
449
450 return ksnav_send(priv->netcp_pktdma, (u32 *)packet,
451 length, (priv->slave_port) << 16);
452}
453
454static int ks2_eth_recv(struct udevice *dev, int flags, uchar **packetp)
455{
456 struct ks2_eth_priv *priv = dev_get_priv(dev);
457 int pkt_size;
458 u32 *pkt = NULL;
459
460 priv->hd = ksnav_recv(priv->netcp_pktdma, &pkt, &pkt_size);
461 if (priv->hd == NULL)
462 return -EAGAIN;
463
464 *packetp = (uchar *)pkt;
465
466 return pkt_size;
467}
468
469static int ks2_eth_free_pkt(struct udevice *dev, uchar *packet,
470 int length)
471{
472 struct ks2_eth_priv *priv = dev_get_priv(dev);
473
474 ksnav_release_rxhd(priv->netcp_pktdma, priv->hd);
475
476 return 0;
477}
478
479static void ks2_eth_stop(struct udevice *dev)
480{
481 struct ks2_eth_priv *priv = dev_get_priv(dev);
482
483 if (!priv->emac_open)
484 return;
485 ethss_stop();
486
487 ksnav_close(priv->netcp_pktdma);
488 qm_close();
489 phy_shutdown(priv->phydev);
490 priv->emac_open = false;
491}
492
493int ks2_eth_read_rom_hwaddr(struct udevice *dev)
494{
495 struct ks2_eth_priv *priv = dev_get_priv(dev);
496 struct eth_pdata *pdata = dev_get_platdata(dev);
497 u32 maca = 0;
498 u32 macb = 0;
499
500 /* Read the e-fuse mac address */
501 if (priv->slave_port == 1) {
502 maca = __raw_readl(MAC_ID_BASE_ADDR);
503 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
504 }
505
506 pdata->enetaddr[0] = (macb >> 8) & 0xff;
507 pdata->enetaddr[1] = (macb >> 0) & 0xff;
508 pdata->enetaddr[2] = (maca >> 24) & 0xff;
509 pdata->enetaddr[3] = (maca >> 16) & 0xff;
510 pdata->enetaddr[4] = (maca >> 8) & 0xff;
511 pdata->enetaddr[5] = (maca >> 0) & 0xff;
512
513 return 0;
514}
515
516int ks2_eth_write_hwaddr(struct udevice *dev)
517{
518 struct ks2_eth_priv *priv = dev_get_priv(dev);
519 struct eth_pdata *pdata = dev_get_platdata(dev);
520
521 writel(mac_hi(pdata->enetaddr),
522 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
523 CPGMACSL_REG_SA_HI);
524 writel(mac_lo(pdata->enetaddr),
525 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
526 CPGMACSL_REG_SA_LO);
527
528 return 0;
529}
530
531static int ks2_eth_probe(struct udevice *dev)
532{
533 struct ks2_eth_priv *priv = dev_get_priv(dev);
534 struct mii_dev *mdio_bus;
Mugunthan V N6599f362016-02-02 15:51:33 +0530535
536 priv->dev = dev;
Grygorii Strashko79d81272018-10-31 16:21:45 -0500537 priv->emac_open = false;
Mugunthan V N6599f362016-02-02 15:51:33 +0530538
539 /* These clock enables has to be moved to common location */
540 if (cpu_is_k2g())
541 writel(KS2_ETHERNET_RGMII, KS2_ETHERNET_CFG);
542
543 /* By default, select PA PLL clock as PA clock source */
544#ifndef CONFIG_SOC_K2G
545 if (psc_enable_module(KS2_LPSC_PA))
546 return -EACCES;
547#endif
548 if (psc_enable_module(KS2_LPSC_CPGMAC))
549 return -EACCES;
550 if (psc_enable_module(KS2_LPSC_CRYPTO))
551 return -EACCES;
552
553 if (cpu_is_k2e() || cpu_is_k2l())
554 pll_pa_clk_sel();
555
Mugunthan V N1610a922016-08-02 12:01:11 +0530556 priv->net_rx_buffs.buff_ptr = rx_buffs;
557 priv->net_rx_buffs.num_buffs = RX_BUFF_NUMS;
558 priv->net_rx_buffs.buff_len = RX_BUFF_LEN;
Mugunthan V N6599f362016-02-02 15:51:33 +0530559
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530560 if (priv->slave_port == 1) {
Grygorii Strashko79d81272018-10-31 16:21:45 -0500561#ifndef CONFIG_SOC_K2G
562 keystone2_net_serdes_setup();
563#endif
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530564 /*
565 * Register MDIO bus for slave 0 only, other slave have
566 * to re-use the same
567 */
Grygorii Strashko79d81272018-10-31 16:21:45 -0500568 mdio_bus = cpsw_mdio_init("ethernet-mdio",
569 (u32)priv->mdio_base,
570 EMAC_MDIO_CLOCK_FREQ,
571 EMAC_MDIO_BUS_FREQ);
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530572 if (!mdio_bus) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900573 pr_err("MDIO alloc failed\n");
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530574 return -ENOMEM;
575 }
576 priv->mdio_bus = mdio_bus;
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530577 } else {
578 /* Get the MDIO bus from slave 0 device */
579 struct ks2_eth_priv *parent_priv;
580
581 parent_priv = dev_get_priv(dev->parent);
582 priv->mdio_bus = parent_priv->mdio_bus;
Grygorii Strashko79d81272018-10-31 16:21:45 -0500583 priv->mdio_base = parent_priv->mdio_base;
Mugunthan V N6599f362016-02-02 15:51:33 +0530584 }
585
Mugunthan V N6599f362016-02-02 15:51:33 +0530586 priv->netcp_pktdma = &netcp_pktdma;
587
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530588 if (priv->has_mdio) {
589 priv->phydev = phy_connect(priv->mdio_bus, priv->phy_addr,
590 dev, priv->phy_if);
591 phy_config(priv->phydev);
592 }
Mugunthan V N6599f362016-02-02 15:51:33 +0530593
594 return 0;
595}
596
597int ks2_eth_remove(struct udevice *dev)
598{
599 struct ks2_eth_priv *priv = dev_get_priv(dev);
600
Grygorii Strashko79d81272018-10-31 16:21:45 -0500601 cpsw_mdio_free(priv->mdio_bus);
Mugunthan V N6599f362016-02-02 15:51:33 +0530602
603 return 0;
604}
605
606static const struct eth_ops ks2_eth_ops = {
607 .start = ks2_eth_start,
608 .send = ks2_eth_send,
609 .recv = ks2_eth_recv,
610 .free_pkt = ks2_eth_free_pkt,
611 .stop = ks2_eth_stop,
612 .read_rom_hwaddr = ks2_eth_read_rom_hwaddr,
613 .write_hwaddr = ks2_eth_write_hwaddr,
614};
615
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530616static int ks2_eth_bind_slaves(struct udevice *dev, int gbe, int *gbe_0)
Mugunthan V N6599f362016-02-02 15:51:33 +0530617{
Mugunthan V N6599f362016-02-02 15:51:33 +0530618 const void *fdt = gd->fdt_blob;
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530619 struct udevice *sl_dev;
Mugunthan V N6599f362016-02-02 15:51:33 +0530620 int interfaces;
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530621 int sec_slave;
622 int slave;
623 int ret;
624 char *slave_name;
625
626 interfaces = fdt_subnode_offset(fdt, gbe, "interfaces");
Simon Glassdf87e6b2016-10-02 17:59:29 -0600627 fdt_for_each_subnode(slave, fdt, interfaces) {
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530628 int slave_no;
629
630 slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
631 if (slave_no == -ENOENT)
632 continue;
633
634 if (slave_no == 0) {
635 /* This is the current eth device */
636 *gbe_0 = slave;
637 } else {
638 /* Slave devices to be registered */
639 slave_name = malloc(20);
640 snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
641 ret = device_bind_driver_to_node(dev, "eth_ks2_sl",
Simon Glass45a26862017-05-18 20:09:07 -0600642 slave_name, offset_to_ofnode(slave),
643 &sl_dev);
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530644 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900645 pr_err("ks2_net - not able to bind slave interfaces\n");
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530646 return ret;
647 }
648 }
649 }
650
651 sec_slave = fdt_subnode_offset(fdt, gbe, "secondary-slave-ports");
Simon Glassdf87e6b2016-10-02 17:59:29 -0600652 fdt_for_each_subnode(slave, fdt, sec_slave) {
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530653 int slave_no;
654
655 slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
656 if (slave_no == -ENOENT)
657 continue;
658
659 /* Slave devices to be registered */
660 slave_name = malloc(20);
661 snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
662 ret = device_bind_driver_to_node(dev, "eth_ks2_sl", slave_name,
Simon Glass45a26862017-05-18 20:09:07 -0600663 offset_to_ofnode(slave), &sl_dev);
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530664 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900665 pr_err("ks2_net - not able to bind slave interfaces\n");
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530666 return ret;
667 }
668 }
669
670 return 0;
671}
672
673static int ks2_eth_parse_slave_interface(int netcp, int slave,
674 struct ks2_eth_priv *priv,
675 struct eth_pdata *pdata)
676{
677 const void *fdt = gd->fdt_blob;
Mugunthan V N6599f362016-02-02 15:51:33 +0530678 int mdio;
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530679 int phy;
680 int dma_count;
681 u32 dma_channel[8];
Mugunthan V N6599f362016-02-02 15:51:33 +0530682
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530683 priv->slave_port = fdtdec_get_int(fdt, slave, "slave-port", -1);
684 priv->net_rx_buffs.rx_flow = priv->slave_port * 8;
Mugunthan V N6599f362016-02-02 15:51:33 +0530685
Mugunthan V N6599f362016-02-02 15:51:33 +0530686 /* U-Boot slave port number starts with 1 instead of 0 */
687 priv->slave_port += 1;
688
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530689 dma_count = fdtdec_get_int_array_count(fdt, netcp,
690 "ti,navigator-dmas",
691 dma_channel, 8);
Mugunthan V N6599f362016-02-02 15:51:33 +0530692
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530693 if (dma_count > (2 * priv->slave_port)) {
694 int dma_idx;
695
696 dma_idx = priv->slave_port * 2 - 1;
697 priv->net_rx_buffs.rx_flow = dma_channel[dma_idx];
Mugunthan V N6599f362016-02-02 15:51:33 +0530698 }
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530699
700 priv->link_type = fdtdec_get_int(fdt, slave, "link-interface", -1);
701
702 phy = fdtdec_lookup_phandle(fdt, slave, "phy-handle");
703 if (phy >= 0) {
704 priv->phy_addr = fdtdec_get_int(fdt, phy, "reg", -1);
705
706 mdio = fdt_parent_offset(fdt, phy);
707 if (mdio < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900708 pr_err("mdio dt not found\n");
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530709 return -ENODEV;
710 }
711 priv->mdio_base = (void *)fdtdec_get_addr(fdt, mdio, "reg");
712 }
Mugunthan V N6599f362016-02-02 15:51:33 +0530713
Mugunthan V N1de40662016-08-11 20:04:03 +0530714 if (priv->link_type == LINK_TYPE_SGMII_MAC_TO_PHY_MODE) {
Mugunthan V N6599f362016-02-02 15:51:33 +0530715 priv->phy_if = PHY_INTERFACE_MODE_SGMII;
716 pdata->phy_interface = priv->phy_if;
717 priv->sgmii_link_type = SGMII_LINK_MAC_PHY;
718 priv->has_mdio = true;
Mugunthan V N1de40662016-08-11 20:04:03 +0530719 } else if (priv->link_type == LINK_TYPE_RGMII_LINK_MAC_PHY) {
720 priv->phy_if = PHY_INTERFACE_MODE_RGMII;
721 pdata->phy_interface = priv->phy_if;
722 priv->has_mdio = true;
Mugunthan V N6599f362016-02-02 15:51:33 +0530723 }
Mugunthan V N6599f362016-02-02 15:51:33 +0530724
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530725 return 0;
726}
727
728static int ks2_sl_eth_ofdata_to_platdata(struct udevice *dev)
729{
730 struct ks2_eth_priv *priv = dev_get_priv(dev);
731 struct eth_pdata *pdata = dev_get_platdata(dev);
732 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700733 int slave = dev_of_offset(dev);
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530734 int interfaces;
735 int gbe;
736 int netcp_devices;
737 int netcp;
738
739 interfaces = fdt_parent_offset(fdt, slave);
740 gbe = fdt_parent_offset(fdt, interfaces);
741 netcp_devices = fdt_parent_offset(fdt, gbe);
742 netcp = fdt_parent_offset(fdt, netcp_devices);
743
744 ks2_eth_parse_slave_interface(netcp, slave, priv, pdata);
745
746 pdata->iobase = fdtdec_get_addr(fdt, netcp, "reg");
747
748 return 0;
749}
750
751static int ks2_eth_ofdata_to_platdata(struct udevice *dev)
752{
753 struct ks2_eth_priv *priv = dev_get_priv(dev);
754 struct eth_pdata *pdata = dev_get_platdata(dev);
755 const void *fdt = gd->fdt_blob;
756 int gbe_0 = -ENODEV;
757 int netcp_devices;
758 int gbe;
759
Simon Glasse160f7d2017-01-17 16:52:55 -0700760 netcp_devices = fdt_subnode_offset(fdt, dev_of_offset(dev),
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530761 "netcp-devices");
762 gbe = fdt_subnode_offset(fdt, netcp_devices, "gbe");
763
764 ks2_eth_bind_slaves(dev, gbe, &gbe_0);
765
Simon Glasse160f7d2017-01-17 16:52:55 -0700766 ks2_eth_parse_slave_interface(dev_of_offset(dev), gbe_0, priv, pdata);
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530767
Simon Glassa821c4a2017-05-17 17:18:05 -0600768 pdata->iobase = devfdt_get_addr(dev);
Mugunthan V N6599f362016-02-02 15:51:33 +0530769
770 return 0;
771}
772
773static const struct udevice_id ks2_eth_ids[] = {
774 { .compatible = "ti,netcp-1.0" },
775 { }
776};
777
Mugunthan V Na61f6a52016-08-02 12:01:12 +0530778U_BOOT_DRIVER(eth_ks2_slave) = {
779 .name = "eth_ks2_sl",
780 .id = UCLASS_ETH,
781 .ofdata_to_platdata = ks2_sl_eth_ofdata_to_platdata,
782 .probe = ks2_eth_probe,
783 .remove = ks2_eth_remove,
784 .ops = &ks2_eth_ops,
785 .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
786 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
787 .flags = DM_FLAG_ALLOC_PRIV_DMA,
788};
Mugunthan V N6599f362016-02-02 15:51:33 +0530789
790U_BOOT_DRIVER(eth_ks2) = {
791 .name = "eth_ks2",
792 .id = UCLASS_ETH,
793 .of_match = ks2_eth_ids,
794 .ofdata_to_platdata = ks2_eth_ofdata_to_platdata,
795 .probe = ks2_eth_probe,
796 .remove = ks2_eth_remove,
797 .ops = &ks2_eth_ops,
798 .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
799 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
800 .flags = DM_FLAG_ALLOC_PRIV_DMA,
801};