blob: d4d909b983f07c98b16cf1c1d0e3a6d87169de20 [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 */
8#include <common.h>
9#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070010#include <console.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040011
Mugunthan V Nc8386732016-02-02 15:51:33 +053012#include <dm.h>
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +053013#include <dm/lists.h>
Mugunthan V Nc8386732016-02-02 15:51:33 +053014
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040015#include <net.h>
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +030016#include <phy.h>
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +030017#include <errno.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040018#include <miiphy.h>
19#include <malloc.h>
Khoronzhuk, Ivan7954b862014-09-05 19:02:47 +030020#include <asm/ti-common/keystone_nav.h>
Khoronzhuk, Ivanf2c13ba2014-09-29 22:17:22 +030021#include <asm/ti-common/keystone_net.h>
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +030022#include <asm/ti-common/keystone_serdes.h>
Mugunthan V Nc8386732016-02-02 15:51:33 +053023#include <asm/arch/psc_defs.h>
24
25DECLARE_GLOBAL_DATA_PTR;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040026
Mugunthan V Nc8386732016-02-02 15:51:33 +053027#ifndef CONFIG_DM_ETH
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040028unsigned int emac_open;
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030029static struct mii_dev *mdio_bus;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040030static unsigned int sys_has_mdio = 1;
Mugunthan V Nc8386732016-02-02 15:51:33 +053031#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040032
33#ifdef KEYSTONE2_EMAC_GIG_ENABLE
34#define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x)
35#else
36#define emac_gigabit_enable(x) /* no gigabit to enable */
37#endif
38
39#define RX_BUFF_NUMS 24
40#define RX_BUFF_LEN 1520
41#define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +030042#define SGMII_ANEG_TIMEOUT 4000
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040043
44static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16);
45
Mugunthan V Nc8386732016-02-02 15:51:33 +053046#ifndef CONFIG_DM_ETH
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040047struct rx_buff_desc net_rx_buffs = {
48 .buff_ptr = rx_buffs,
49 .num_buffs = RX_BUFF_NUMS,
50 .buff_len = RX_BUFF_LEN,
51 .rx_flow = 22,
52};
Vitaly Andrianov4cedef32015-09-19 16:26:50 +053053#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040054
Mugunthan V Nc8386732016-02-02 15:51:33 +053055#ifdef CONFIG_DM_ETH
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040056
Mugunthan V Nc8386732016-02-02 15:51:33 +053057enum link_type {
Mugunthan V N17819112016-08-11 20:04:03 +053058 LINK_TYPE_SGMII_MAC_TO_MAC_AUTO = 0,
59 LINK_TYPE_SGMII_MAC_TO_PHY_MODE = 1,
60 LINK_TYPE_SGMII_MAC_TO_MAC_FORCED_MODE = 2,
61 LINK_TYPE_SGMII_MAC_TO_FIBRE_MODE = 3,
62 LINK_TYPE_SGMII_MAC_TO_PHY_NO_MDIO_MODE = 4,
63 LINK_TYPE_RGMII_LINK_MAC_PHY = 5,
64 LINK_TYPE_RGMII_LINK_MAC_MAC_FORCED = 6,
65 LINK_TYPE_RGMII_LINK_MAC_PHY_NO_MDIO = 7,
66 LINK_TYPE_10G_MAC_TO_PHY_MODE = 10,
67 LINK_TYPE_10G_MAC_TO_MAC_FORCED_MODE = 11,
Mugunthan V Nc8386732016-02-02 15:51:33 +053068};
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040069
Mugunthan V Nc8386732016-02-02 15:51:33 +053070#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
71 ((mac)[2] << 16) | ((mac)[3] << 24))
72#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040073
Mugunthan V Nc8386732016-02-02 15:51:33 +053074#ifdef CONFIG_KSNET_NETCP_V1_0
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040075
Mugunthan V Nc8386732016-02-02 15:51:33 +053076#define EMAC_EMACSW_BASE_OFS 0x90800
77#define EMAC_EMACSW_PORT_BASE_OFS (EMAC_EMACSW_BASE_OFS + 0x60)
78
79/* CPSW Switch slave registers */
80#define CPGMACSL_REG_SA_LO 0x10
81#define CPGMACSL_REG_SA_HI 0x14
82
83#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
84 (x) * 0x30)
85
86#elif defined CONFIG_KSNET_NETCP_V1_5
87
88#define EMAC_EMACSW_PORT_BASE_OFS 0x222000
89
90/* CPSW Switch slave registers */
91#define CPGMACSL_REG_SA_LO 0x308
92#define CPGMACSL_REG_SA_HI 0x30c
93
94#define DEVICE_EMACSW_BASE(base, x) ((base) + EMAC_EMACSW_PORT_BASE_OFS + \
95 (x) * 0x1000)
96
97#endif
98
99
100struct ks2_eth_priv {
101 struct udevice *dev;
102 struct phy_device *phydev;
103 struct mii_dev *mdio_bus;
104 int phy_addr;
105 phy_interface_t phy_if;
106 int sgmii_link_type;
107 void *mdio_base;
108 struct rx_buff_desc net_rx_buffs;
109 struct pktdma_cfg *netcp_pktdma;
110 void *hd;
111 int slave_port;
112 enum link_type link_type;
113 bool emac_open;
114 bool has_mdio;
115};
116#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400117
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300118/* MDIO */
119
120static int keystone2_mdio_reset(struct mii_dev *bus)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400121{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300122 u_int32_t clkdiv;
123 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400124
125 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
126
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300127 writel((clkdiv & 0xffff) | MDIO_CONTROL_ENABLE |
128 MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE,
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400129 &adap_mdio->control);
130
131 while (readl(&adap_mdio->control) & MDIO_CONTROL_IDLE)
132 ;
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300133
134 return 0;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400135}
136
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300137/**
138 * keystone2_mdio_read - read a PHY register via MDIO interface.
139 * Blocks until operation is complete.
140 */
141static int keystone2_mdio_read(struct mii_dev *bus,
142 int addr, int devad, int reg)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400143{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300144 int tmp;
145 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400146
147 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
148 ;
149
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300150 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ |
151 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16),
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400152 &adap_mdio->useraccess0);
153
154 /* Wait for command to complete */
155 while ((tmp = readl(&adap_mdio->useraccess0)) & MDIO_USERACCESS0_GO)
156 ;
157
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300158 if (tmp & MDIO_USERACCESS0_ACK)
159 return tmp & 0xffff;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400160
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400161 return -1;
162}
163
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300164/**
165 * keystone2_mdio_write - write to a PHY register via MDIO interface.
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400166 * Blocks until operation is complete.
167 */
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300168static int keystone2_mdio_write(struct mii_dev *bus,
169 int addr, int devad, int reg, u16 val)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400170{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300171 struct mdio_regs *adap_mdio = bus->priv;
172
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400173 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
174 ;
175
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300176 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE |
177 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16) |
178 (val & 0xffff), &adap_mdio->useraccess0);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400179
180 /* Wait for command to complete */
181 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
182 ;
183
184 return 0;
185}
186
Mugunthan V Nc8386732016-02-02 15:51:33 +0530187#ifndef CONFIG_DM_ETH
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400188static void __attribute__((unused))
189 keystone2_eth_gigabit_enable(struct eth_device *dev)
190{
191 u_int16_t data;
192 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
193
194 if (sys_has_mdio) {
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300195 data = keystone2_mdio_read(mdio_bus, eth_priv->phy_addr,
196 MDIO_DEVAD_NONE, 0);
197 /* speed selection MSB */
198 if (!(data & (1 << 6)))
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400199 return;
200 }
201
202 /*
203 * Check if link detected is giga-bit
204 * If Gigabit mode detected, enable gigbit in MAC
205 */
Hao Zhangc079f402014-09-29 22:17:20 +0300206 writel(readl(DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) +
207 CPGMACSL_REG_CTL) |
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400208 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
Hao Zhangc079f402014-09-29 22:17:20 +0300209 DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + CPGMACSL_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400210}
Mugunthan V Nc8386732016-02-02 15:51:33 +0530211#else
212static void __attribute__((unused))
213 keystone2_eth_gigabit_enable(struct udevice *dev)
214{
215 struct ks2_eth_priv *priv = dev_get_priv(dev);
216 u_int16_t data;
217
218 if (priv->has_mdio) {
219 data = keystone2_mdio_read(priv->mdio_bus, priv->phy_addr,
220 MDIO_DEVAD_NONE, 0);
221 /* speed selection MSB */
222 if (!(data & (1 << 6)))
223 return;
224 }
225
226 /*
227 * Check if link detected is giga-bit
228 * If Gigabit mode detected, enable gigbit in MAC
229 */
230 writel(readl(DEVICE_EMACSL_BASE(priv->slave_port - 1) +
231 CPGMACSL_REG_CTL) |
232 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
233 DEVICE_EMACSL_BASE(priv->slave_port - 1) + CPGMACSL_REG_CTL);
234}
235#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400236
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530237#ifdef CONFIG_SOC_K2G
238int keystone_rgmii_config(struct phy_device *phy_dev)
239{
240 unsigned int i, status;
241
242 i = 0;
243 do {
244 if (i > SGMII_ANEG_TIMEOUT) {
245 puts(" TIMEOUT !\n");
246 phy_dev->link = 0;
247 return 0;
248 }
249
250 if (ctrlc()) {
251 puts("user interrupt!\n");
252 phy_dev->link = 0;
253 return -EINTR;
254 }
255
256 if ((i++ % 500) == 0)
257 printf(".");
258
259 udelay(1000); /* 1 ms */
260 status = readl(RGMII_STATUS_REG);
261 } while (!(status & RGMII_REG_STATUS_LINK));
262
263 puts(" done\n");
264
265 return 0;
266}
267#else
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300268int keystone_sgmii_config(struct phy_device *phy_dev, int port, int interface)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400269{
270 unsigned int i, status, mask;
271 unsigned int mr_adv_ability, control;
272
273 switch (interface) {
274 case SGMII_LINK_MAC_MAC_AUTONEG:
275 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
276 SGMII_REG_MR_ADV_LINK |
277 SGMII_REG_MR_ADV_FULL_DUPLEX |
278 SGMII_REG_MR_ADV_GIG_MODE);
279 control = (SGMII_REG_CONTROL_MASTER |
280 SGMII_REG_CONTROL_AUTONEG);
281
282 break;
283 case SGMII_LINK_MAC_PHY:
284 case SGMII_LINK_MAC_PHY_FORCED:
285 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
286 control = SGMII_REG_CONTROL_AUTONEG;
287
288 break;
289 case SGMII_LINK_MAC_MAC_FORCED:
290 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
291 SGMII_REG_MR_ADV_LINK |
292 SGMII_REG_MR_ADV_FULL_DUPLEX |
293 SGMII_REG_MR_ADV_GIG_MODE);
294 control = SGMII_REG_CONTROL_MASTER;
295
296 break;
297 case SGMII_LINK_MAC_FIBER:
298 mr_adv_ability = 0x20;
299 control = SGMII_REG_CONTROL_AUTONEG;
300
301 break;
302 default:
303 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
304 control = SGMII_REG_CONTROL_AUTONEG;
305 }
306
307 __raw_writel(0, SGMII_CTL_REG(port));
308
309 /*
310 * Wait for the SerDes pll to lock,
311 * but don't trap if lock is never read
312 */
313 for (i = 0; i < 1000; i++) {
314 udelay(2000);
315 status = __raw_readl(SGMII_STATUS_REG(port));
316 if ((status & SGMII_REG_STATUS_LOCK) != 0)
317 break;
318 }
319
320 __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
321 __raw_writel(control, SGMII_CTL_REG(port));
322
323
324 mask = SGMII_REG_STATUS_LINK;
325
326 if (control & SGMII_REG_CONTROL_AUTONEG)
327 mask |= SGMII_REG_STATUS_AUTONEG;
328
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300329 status = __raw_readl(SGMII_STATUS_REG(port));
330 if ((status & mask) == mask)
331 return 0;
332
333 printf("\n%s Waiting for SGMII auto negotiation to complete",
334 phy_dev->dev->name);
335 while ((status & mask) != mask) {
336 /*
337 * Timeout reached ?
338 */
339 if (i > SGMII_ANEG_TIMEOUT) {
340 puts(" TIMEOUT !\n");
341 phy_dev->link = 0;
342 return 0;
343 }
344
345 if (ctrlc()) {
346 puts("user interrupt!\n");
347 phy_dev->link = 0;
348 return -EINTR;
349 }
350
351 if ((i++ % 500) == 0)
352 printf(".");
353
354 udelay(1000); /* 1 ms */
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400355 status = __raw_readl(SGMII_STATUS_REG(port));
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400356 }
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300357 puts(" done\n");
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400358
359 return 0;
360}
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530361#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400362
363int mac_sl_reset(u32 port)
364{
365 u32 i, v;
366
367 if (port >= DEVICE_N_GMACSL_PORTS)
368 return GMACSL_RET_INVALID_PORT;
369
370 /* Set the soft reset bit */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300371 writel(CPGMAC_REG_RESET_VAL_RESET,
372 DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400373
374 /* Wait for the bit to clear */
375 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300376 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400377 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
378 CPGMAC_REG_RESET_VAL_RESET)
379 return GMACSL_RET_OK;
380 }
381
382 /* Timeout on the reset */
383 return GMACSL_RET_WARN_RESET_INCOMPLETE;
384}
385
386int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
387{
388 u32 v, i;
389 int ret = GMACSL_RET_OK;
390
391 if (port >= DEVICE_N_GMACSL_PORTS)
392 return GMACSL_RET_INVALID_PORT;
393
394 if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
395 cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
396 ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
397 }
398
399 /* Must wait if the device is undergoing reset */
400 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300401 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400402 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
403 CPGMAC_REG_RESET_VAL_RESET)
404 break;
405 }
406
407 if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
408 return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
409
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300410 writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
411 writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400412
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530413#ifndef CONFIG_SOC_K2HK
Khoronzhuk, Ivana7894b72014-10-17 21:01:14 +0300414 /* Map RX packet flow priority to 0 */
415 writel(0, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RX_PRI_MAP);
416#endif
417
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400418 return ret;
419}
420
421int ethss_config(u32 ctl, u32 max_pkt_size)
422{
423 u32 i;
424
425 /* Max length register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300426 writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400427
428 /* Control register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300429 writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400430
431 /* All statistics enabled by default */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300432 writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
433 DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400434
435 /* Reset and enable the ALE */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300436 writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
437 CPSW_REG_VAL_ALE_CTL_BYPASS,
438 DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400439
440 /* All ports put into forward mode */
441 for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300442 writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
443 DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400444
445 return 0;
446}
447
448int ethss_start(void)
449{
450 int i;
451 struct mac_sl_cfg cfg;
452
453 cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
454 cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
455
456 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
457 mac_sl_reset(i);
458 mac_sl_config(i, &cfg);
459 }
460
461 return 0;
462}
463
464int ethss_stop(void)
465{
466 int i;
467
468 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
469 mac_sl_reset(i);
470
471 return 0;
472}
473
Mugunthan V Nc8386732016-02-02 15:51:33 +0530474struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
475 .clk = SERDES_CLOCK_156P25M,
476 .rate = SERDES_RATE_5G,
477 .rate_mode = SERDES_QUARTER_RATE,
478 .intf = SERDES_PHY_SGMII,
479 .loopback = 0,
480};
481
482#ifndef CONFIG_SOC_K2G
483static void keystone2_net_serdes_setup(void)
484{
485 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE,
486 &ks2_serdes_sgmii_156p25mhz,
487 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
488
489#if defined(CONFIG_SOC_K2E) || defined(CONFIG_SOC_K2L)
490 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII2_BASE,
491 &ks2_serdes_sgmii_156p25mhz,
492 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
493#endif
494
495 /* wait till setup */
496 udelay(5000);
497}
498#endif
499
500#ifndef CONFIG_DM_ETH
501
502int keystone2_eth_read_mac_addr(struct eth_device *dev)
503{
504 struct eth_priv_t *eth_priv;
505 u32 maca = 0;
506 u32 macb = 0;
507
508 eth_priv = (struct eth_priv_t *)dev->priv;
509
510 /* Read the e-fuse mac address */
511 if (eth_priv->slave_port == 1) {
512 maca = __raw_readl(MAC_ID_BASE_ADDR);
513 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
514 }
515
516 dev->enetaddr[0] = (macb >> 8) & 0xff;
517 dev->enetaddr[1] = (macb >> 0) & 0xff;
518 dev->enetaddr[2] = (maca >> 24) & 0xff;
519 dev->enetaddr[3] = (maca >> 16) & 0xff;
520 dev->enetaddr[4] = (maca >> 8) & 0xff;
521 dev->enetaddr[5] = (maca >> 0) & 0xff;
522
523 return 0;
524}
525
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400526int32_t cpmac_drv_send(u32 *buffer, int num_bytes, int slave_port_num)
527{
528 if (num_bytes < EMAC_MIN_ETHERNET_PKT_SIZE)
529 num_bytes = EMAC_MIN_ETHERNET_PKT_SIZE;
530
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300531 return ksnav_send(&netcp_pktdma, buffer,
532 num_bytes, (slave_port_num) << 16);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400533}
534
535/* Eth device open */
536static int keystone2_eth_open(struct eth_device *dev, bd_t *bis)
537{
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400538 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300539 struct phy_device *phy_dev = eth_priv->phy_dev;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400540
541 debug("+ emac_open\n");
542
543 net_rx_buffs.rx_flow = eth_priv->rx_flow;
544
545 sys_has_mdio =
546 (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY) ? 1 : 0;
547
Khoronzhuk, Ivan689d95c2014-10-29 13:09:33 +0200548 if (sys_has_mdio)
549 keystone2_mdio_reset(mdio_bus);
550
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530551#ifdef CONFIG_SOC_K2G
552 keystone_rgmii_config(phy_dev);
553#else
Khoronzhuk, Ivan2b17d3a2014-10-17 21:01:15 +0300554 keystone_sgmii_config(phy_dev, eth_priv->slave_port - 1,
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400555 eth_priv->sgmii_link_type);
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530556#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400557
558 udelay(10000);
559
560 /* On chip switch configuration */
561 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
562
563 /* TODO: add error handling code */
564 if (qm_init()) {
565 printf("ERROR: qm_init()\n");
566 return -1;
567 }
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300568 if (ksnav_init(&netcp_pktdma, &net_rx_buffs)) {
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400569 qm_close();
570 printf("ERROR: netcp_init()\n");
571 return -1;
572 }
573
574 /*
575 * Streaming switch configuration. If not present this
576 * statement is defined to void in target.h.
577 * If present this is usually defined to a series of register writes
578 */
579 hw_config_streaming_switch();
580
581 if (sys_has_mdio) {
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300582 keystone2_mdio_reset(mdio_bus);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400583
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300584 phy_startup(phy_dev);
585 if (phy_dev->link == 0) {
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300586 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400587 qm_close();
588 return -1;
589 }
590 }
591
592 emac_gigabit_enable(dev);
593
594 ethss_start();
595
596 debug("- emac_open\n");
597
598 emac_open = 1;
599
600 return 0;
601}
602
603/* Eth device close */
604void keystone2_eth_close(struct eth_device *dev)
605{
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300606 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
607 struct phy_device *phy_dev = eth_priv->phy_dev;
608
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400609 debug("+ emac_close\n");
610
611 if (!emac_open)
612 return;
613
614 ethss_stop();
615
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300616 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400617 qm_close();
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300618 phy_shutdown(phy_dev);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400619
620 emac_open = 0;
621
622 debug("- emac_close\n");
623}
624
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400625/*
626 * This function sends a single packet on the network and returns
627 * positive number (number of bytes transmitted) or negative for error
628 */
629static int keystone2_eth_send_packet(struct eth_device *dev,
630 void *packet, int length)
631{
632 int ret_status = -1;
633 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
Khoronzhuk, Ivan12a5b372014-10-17 20:44:36 +0300634 struct phy_device *phy_dev = eth_priv->phy_dev;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400635
Khoronzhuk, Ivan12a5b372014-10-17 20:44:36 +0300636 genphy_update_link(phy_dev);
637 if (phy_dev->link == 0)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400638 return -1;
639
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400640 if (cpmac_drv_send((u32 *)packet, length, eth_priv->slave_port) != 0)
641 return ret_status;
642
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400643 return length;
644}
645
646/*
647 * This function handles receipt of a packet from the network
648 */
649static int keystone2_eth_rcv_packet(struct eth_device *dev)
650{
651 void *hd;
652 int pkt_size;
653 u32 *pkt;
654
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300655 hd = ksnav_recv(&netcp_pktdma, &pkt, &pkt_size);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400656 if (hd == NULL)
657 return 0;
658
Joe Hershberger9f09a362015-04-08 01:41:06 -0500659 net_process_received_packet((uchar *)pkt, pkt_size);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400660
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300661 ksnav_release_rxhd(&netcp_pktdma, hd);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400662
663 return pkt_size;
664}
665
Vitaly Andrianovc2d7e602015-07-08 11:56:01 -0400666#ifdef CONFIG_MCAST_TFTP
667static int keystone2_eth_bcast_addr(struct eth_device *dev, u32 ip, u8 set)
668{
669 return 0;
670}
671#endif
672
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400673/*
674 * This function initializes the EMAC hardware.
675 */
676int keystone2_emac_initialize(struct eth_priv_t *eth_priv)
677{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300678 int res;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400679 struct eth_device *dev;
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300680 struct phy_device *phy_dev;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530681 struct mdio_regs *adap_mdio = (struct mdio_regs *)EMAC_MDIO_BASE_ADDR;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400682
683 dev = malloc(sizeof(struct eth_device));
684 if (dev == NULL)
685 return -1;
686
687 memset(dev, 0, sizeof(struct eth_device));
688
689 strcpy(dev->name, eth_priv->int_name);
690 dev->priv = eth_priv;
691
692 keystone2_eth_read_mac_addr(dev);
693
694 dev->iobase = 0;
695 dev->init = keystone2_eth_open;
696 dev->halt = keystone2_eth_close;
697 dev->send = keystone2_eth_send_packet;
698 dev->recv = keystone2_eth_rcv_packet;
Vitaly Andrianovc2d7e602015-07-08 11:56:01 -0400699#ifdef CONFIG_MCAST_TFTP
700 dev->mcast = keystone2_eth_bcast_addr;
701#endif
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400702
703 eth_register(dev);
704
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300705 /* Register MDIO bus if it's not registered yet */
706 if (!mdio_bus) {
707 mdio_bus = mdio_alloc();
708 mdio_bus->read = keystone2_mdio_read;
709 mdio_bus->write = keystone2_mdio_write;
710 mdio_bus->reset = keystone2_mdio_reset;
711 mdio_bus->priv = (void *)EMAC_MDIO_BASE_ADDR;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000712 strcpy(mdio_bus->name, "ethernet-mdio");
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300713
714 res = mdio_register(mdio_bus);
715 if (res)
716 return res;
717 }
718
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530719#ifndef CONFIG_SOC_K2G
Vitaly Andrianov13cddec2015-02-11 14:05:41 -0500720 keystone2_net_serdes_setup();
Vitaly Andrianov4cedef32015-09-19 16:26:50 +0530721#endif
Vitaly Andrianov13cddec2015-02-11 14:05:41 -0500722
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300723 /* Create phy device and bind it with driver */
724#ifdef CONFIG_KSNET_MDIO_PHY_CONFIG_ENABLE
725 phy_dev = phy_connect(mdio_bus, eth_priv->phy_addr,
Mugunthan V Nd44bb342015-09-19 16:26:48 +0530726 dev, eth_priv->phy_if);
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300727 phy_config(phy_dev);
728#else
729 phy_dev = phy_find_by_mask(mdio_bus, 1 << eth_priv->phy_addr,
Mugunthan V Nd44bb342015-09-19 16:26:48 +0530730 eth_priv->phy_if);
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300731 phy_dev->dev = dev;
732#endif
733 eth_priv->phy_dev = phy_dev;
734
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400735 return 0;
736}
737
Mugunthan V Nc8386732016-02-02 15:51:33 +0530738#else
Hao Zhangd890dff2014-10-22 17:18:23 +0300739
Mugunthan V Nc8386732016-02-02 15:51:33 +0530740static int ks2_eth_start(struct udevice *dev)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400741{
Mugunthan V Nc8386732016-02-02 15:51:33 +0530742 struct ks2_eth_priv *priv = dev_get_priv(dev);
Hao Zhangd890dff2014-10-22 17:18:23 +0300743
Mugunthan V Nc8386732016-02-02 15:51:33 +0530744#ifdef CONFIG_SOC_K2G
745 keystone_rgmii_config(priv->phydev);
746#else
747 keystone_sgmii_config(priv->phydev, priv->slave_port - 1,
748 priv->sgmii_link_type);
Khoronzhuk, Ivan3df3e632014-10-17 21:01:13 +0300749#endif
750
Mugunthan V Nc8386732016-02-02 15:51:33 +0530751 udelay(10000);
752
753 /* On chip switch configuration */
754 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
755
756 qm_init();
757
758 if (ksnav_init(priv->netcp_pktdma, &priv->net_rx_buffs)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900759 pr_err("ksnav_init failed\n");
Mugunthan V Nc8386732016-02-02 15:51:33 +0530760 goto err_knav_init;
761 }
762
763 /*
764 * Streaming switch configuration. If not present this
765 * statement is defined to void in target.h.
766 * If present this is usually defined to a series of register writes
767 */
768 hw_config_streaming_switch();
769
770 if (priv->has_mdio) {
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530771 keystone2_mdio_reset(priv->mdio_bus);
772
Mugunthan V Nc8386732016-02-02 15:51:33 +0530773 phy_startup(priv->phydev);
774 if (priv->phydev->link == 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900775 pr_err("phy startup failed\n");
Mugunthan V Nc8386732016-02-02 15:51:33 +0530776 goto err_phy_start;
777 }
778 }
779
780 emac_gigabit_enable(dev);
781
782 ethss_start();
783
784 priv->emac_open = true;
785
786 return 0;
787
788err_phy_start:
789 ksnav_close(priv->netcp_pktdma);
790err_knav_init:
791 qm_close();
792
793 return -EFAULT;
794}
795
796static int ks2_eth_send(struct udevice *dev, void *packet, int length)
797{
798 struct ks2_eth_priv *priv = dev_get_priv(dev);
799
800 genphy_update_link(priv->phydev);
801 if (priv->phydev->link == 0)
802 return -1;
803
804 if (length < EMAC_MIN_ETHERNET_PKT_SIZE)
805 length = EMAC_MIN_ETHERNET_PKT_SIZE;
806
807 return ksnav_send(priv->netcp_pktdma, (u32 *)packet,
808 length, (priv->slave_port) << 16);
809}
810
811static int ks2_eth_recv(struct udevice *dev, int flags, uchar **packetp)
812{
813 struct ks2_eth_priv *priv = dev_get_priv(dev);
814 int pkt_size;
815 u32 *pkt = NULL;
816
817 priv->hd = ksnav_recv(priv->netcp_pktdma, &pkt, &pkt_size);
818 if (priv->hd == NULL)
819 return -EAGAIN;
820
821 *packetp = (uchar *)pkt;
822
823 return pkt_size;
824}
825
826static int ks2_eth_free_pkt(struct udevice *dev, uchar *packet,
827 int length)
828{
829 struct ks2_eth_priv *priv = dev_get_priv(dev);
830
831 ksnav_release_rxhd(priv->netcp_pktdma, priv->hd);
832
833 return 0;
834}
835
836static void ks2_eth_stop(struct udevice *dev)
837{
838 struct ks2_eth_priv *priv = dev_get_priv(dev);
839
840 if (!priv->emac_open)
841 return;
842 ethss_stop();
843
844 ksnav_close(priv->netcp_pktdma);
845 qm_close();
846 phy_shutdown(priv->phydev);
847 priv->emac_open = false;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400848}
Mugunthan V Nc8386732016-02-02 15:51:33 +0530849
850int ks2_eth_read_rom_hwaddr(struct udevice *dev)
851{
852 struct ks2_eth_priv *priv = dev_get_priv(dev);
853 struct eth_pdata *pdata = dev_get_platdata(dev);
854 u32 maca = 0;
855 u32 macb = 0;
856
857 /* Read the e-fuse mac address */
858 if (priv->slave_port == 1) {
859 maca = __raw_readl(MAC_ID_BASE_ADDR);
860 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
861 }
862
863 pdata->enetaddr[0] = (macb >> 8) & 0xff;
864 pdata->enetaddr[1] = (macb >> 0) & 0xff;
865 pdata->enetaddr[2] = (maca >> 24) & 0xff;
866 pdata->enetaddr[3] = (maca >> 16) & 0xff;
867 pdata->enetaddr[4] = (maca >> 8) & 0xff;
868 pdata->enetaddr[5] = (maca >> 0) & 0xff;
869
870 return 0;
871}
872
873int ks2_eth_write_hwaddr(struct udevice *dev)
874{
875 struct ks2_eth_priv *priv = dev_get_priv(dev);
876 struct eth_pdata *pdata = dev_get_platdata(dev);
877
878 writel(mac_hi(pdata->enetaddr),
879 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
880 CPGMACSL_REG_SA_HI);
881 writel(mac_lo(pdata->enetaddr),
882 DEVICE_EMACSW_BASE(pdata->iobase, priv->slave_port - 1) +
883 CPGMACSL_REG_SA_LO);
884
885 return 0;
886}
887
888static int ks2_eth_probe(struct udevice *dev)
889{
890 struct ks2_eth_priv *priv = dev_get_priv(dev);
891 struct mii_dev *mdio_bus;
892 int ret;
893
894 priv->dev = dev;
895
896 /* These clock enables has to be moved to common location */
897 if (cpu_is_k2g())
898 writel(KS2_ETHERNET_RGMII, KS2_ETHERNET_CFG);
899
900 /* By default, select PA PLL clock as PA clock source */
901#ifndef CONFIG_SOC_K2G
902 if (psc_enable_module(KS2_LPSC_PA))
903 return -EACCES;
904#endif
905 if (psc_enable_module(KS2_LPSC_CPGMAC))
906 return -EACCES;
907 if (psc_enable_module(KS2_LPSC_CRYPTO))
908 return -EACCES;
909
910 if (cpu_is_k2e() || cpu_is_k2l())
911 pll_pa_clk_sel();
912
913
Mugunthan V Nb499a3c2016-08-02 12:01:11 +0530914 priv->net_rx_buffs.buff_ptr = rx_buffs;
915 priv->net_rx_buffs.num_buffs = RX_BUFF_NUMS;
916 priv->net_rx_buffs.buff_len = RX_BUFF_LEN;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530917
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530918 if (priv->slave_port == 1) {
919 /*
920 * Register MDIO bus for slave 0 only, other slave have
921 * to re-use the same
922 */
923 mdio_bus = mdio_alloc();
924 if (!mdio_bus) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900925 pr_err("MDIO alloc failed\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530926 return -ENOMEM;
927 }
928 priv->mdio_bus = mdio_bus;
929 mdio_bus->read = keystone2_mdio_read;
930 mdio_bus->write = keystone2_mdio_write;
931 mdio_bus->reset = keystone2_mdio_reset;
932 mdio_bus->priv = priv->mdio_base;
933 sprintf(mdio_bus->name, "ethernet-mdio");
934
935 ret = mdio_register(mdio_bus);
936 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900937 pr_err("MDIO bus register failed\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530938 return ret;
939 }
940 } else {
941 /* Get the MDIO bus from slave 0 device */
942 struct ks2_eth_priv *parent_priv;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530943
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530944 parent_priv = dev_get_priv(dev->parent);
945 priv->mdio_bus = parent_priv->mdio_bus;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530946 }
947
948#ifndef CONFIG_SOC_K2G
949 keystone2_net_serdes_setup();
950#endif
951
952 priv->netcp_pktdma = &netcp_pktdma;
953
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530954 if (priv->has_mdio) {
955 priv->phydev = phy_connect(priv->mdio_bus, priv->phy_addr,
956 dev, priv->phy_if);
957 phy_config(priv->phydev);
958 }
Mugunthan V Nc8386732016-02-02 15:51:33 +0530959
960 return 0;
961}
962
963int ks2_eth_remove(struct udevice *dev)
964{
965 struct ks2_eth_priv *priv = dev_get_priv(dev);
966
967 free(priv->phydev);
968 mdio_unregister(priv->mdio_bus);
969 mdio_free(priv->mdio_bus);
970
971 return 0;
972}
973
974static const struct eth_ops ks2_eth_ops = {
975 .start = ks2_eth_start,
976 .send = ks2_eth_send,
977 .recv = ks2_eth_recv,
978 .free_pkt = ks2_eth_free_pkt,
979 .stop = ks2_eth_stop,
980 .read_rom_hwaddr = ks2_eth_read_rom_hwaddr,
981 .write_hwaddr = ks2_eth_write_hwaddr,
982};
983
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530984static int ks2_eth_bind_slaves(struct udevice *dev, int gbe, int *gbe_0)
Mugunthan V Nc8386732016-02-02 15:51:33 +0530985{
Mugunthan V Nc8386732016-02-02 15:51:33 +0530986 const void *fdt = gd->fdt_blob;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530987 struct udevice *sl_dev;
Mugunthan V Nc8386732016-02-02 15:51:33 +0530988 int interfaces;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530989 int sec_slave;
990 int slave;
991 int ret;
992 char *slave_name;
993
994 interfaces = fdt_subnode_offset(fdt, gbe, "interfaces");
Simon Glass499c29e2016-10-02 17:59:29 -0600995 fdt_for_each_subnode(slave, fdt, interfaces) {
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +0530996 int slave_no;
997
998 slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
999 if (slave_no == -ENOENT)
1000 continue;
1001
1002 if (slave_no == 0) {
1003 /* This is the current eth device */
1004 *gbe_0 = slave;
1005 } else {
1006 /* Slave devices to be registered */
1007 slave_name = malloc(20);
1008 snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
1009 ret = device_bind_driver_to_node(dev, "eth_ks2_sl",
Simon Glasse6dd8da2017-05-18 20:09:07 -06001010 slave_name, offset_to_ofnode(slave),
1011 &sl_dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301012 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001013 pr_err("ks2_net - not able to bind slave interfaces\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301014 return ret;
1015 }
1016 }
1017 }
1018
1019 sec_slave = fdt_subnode_offset(fdt, gbe, "secondary-slave-ports");
Simon Glass499c29e2016-10-02 17:59:29 -06001020 fdt_for_each_subnode(slave, fdt, sec_slave) {
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301021 int slave_no;
1022
1023 slave_no = fdtdec_get_int(fdt, slave, "slave-port", -ENOENT);
1024 if (slave_no == -ENOENT)
1025 continue;
1026
1027 /* Slave devices to be registered */
1028 slave_name = malloc(20);
1029 snprintf(slave_name, 20, "netcp@slave-%d", slave_no);
1030 ret = device_bind_driver_to_node(dev, "eth_ks2_sl", slave_name,
Simon Glasse6dd8da2017-05-18 20:09:07 -06001031 offset_to_ofnode(slave), &sl_dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301032 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001033 pr_err("ks2_net - not able to bind slave interfaces\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301034 return ret;
1035 }
1036 }
1037
1038 return 0;
1039}
1040
1041static int ks2_eth_parse_slave_interface(int netcp, int slave,
1042 struct ks2_eth_priv *priv,
1043 struct eth_pdata *pdata)
1044{
1045 const void *fdt = gd->fdt_blob;
Mugunthan V Nc8386732016-02-02 15:51:33 +05301046 int mdio;
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301047 int phy;
1048 int dma_count;
1049 u32 dma_channel[8];
Mugunthan V Nc8386732016-02-02 15:51:33 +05301050
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301051 priv->slave_port = fdtdec_get_int(fdt, slave, "slave-port", -1);
1052 priv->net_rx_buffs.rx_flow = priv->slave_port * 8;
Mugunthan V Nc8386732016-02-02 15:51:33 +05301053
Mugunthan V Nc8386732016-02-02 15:51:33 +05301054 /* U-Boot slave port number starts with 1 instead of 0 */
1055 priv->slave_port += 1;
1056
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301057 dma_count = fdtdec_get_int_array_count(fdt, netcp,
1058 "ti,navigator-dmas",
1059 dma_channel, 8);
Mugunthan V Nc8386732016-02-02 15:51:33 +05301060
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301061 if (dma_count > (2 * priv->slave_port)) {
1062 int dma_idx;
1063
1064 dma_idx = priv->slave_port * 2 - 1;
1065 priv->net_rx_buffs.rx_flow = dma_channel[dma_idx];
Mugunthan V Nc8386732016-02-02 15:51:33 +05301066 }
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301067
1068 priv->link_type = fdtdec_get_int(fdt, slave, "link-interface", -1);
1069
1070 phy = fdtdec_lookup_phandle(fdt, slave, "phy-handle");
1071 if (phy >= 0) {
1072 priv->phy_addr = fdtdec_get_int(fdt, phy, "reg", -1);
1073
1074 mdio = fdt_parent_offset(fdt, phy);
1075 if (mdio < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001076 pr_err("mdio dt not found\n");
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301077 return -ENODEV;
1078 }
1079 priv->mdio_base = (void *)fdtdec_get_addr(fdt, mdio, "reg");
1080 }
Mugunthan V Nc8386732016-02-02 15:51:33 +05301081
Mugunthan V N17819112016-08-11 20:04:03 +05301082 if (priv->link_type == LINK_TYPE_SGMII_MAC_TO_PHY_MODE) {
Mugunthan V Nc8386732016-02-02 15:51:33 +05301083 priv->phy_if = PHY_INTERFACE_MODE_SGMII;
1084 pdata->phy_interface = priv->phy_if;
1085 priv->sgmii_link_type = SGMII_LINK_MAC_PHY;
1086 priv->has_mdio = true;
Mugunthan V N17819112016-08-11 20:04:03 +05301087 } else if (priv->link_type == LINK_TYPE_RGMII_LINK_MAC_PHY) {
1088 priv->phy_if = PHY_INTERFACE_MODE_RGMII;
1089 pdata->phy_interface = priv->phy_if;
1090 priv->has_mdio = true;
Mugunthan V Nc8386732016-02-02 15:51:33 +05301091 }
Mugunthan V Nc8386732016-02-02 15:51:33 +05301092
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301093 return 0;
1094}
1095
1096static int ks2_sl_eth_ofdata_to_platdata(struct udevice *dev)
1097{
1098 struct ks2_eth_priv *priv = dev_get_priv(dev);
1099 struct eth_pdata *pdata = dev_get_platdata(dev);
1100 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001101 int slave = dev_of_offset(dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301102 int interfaces;
1103 int gbe;
1104 int netcp_devices;
1105 int netcp;
1106
1107 interfaces = fdt_parent_offset(fdt, slave);
1108 gbe = fdt_parent_offset(fdt, interfaces);
1109 netcp_devices = fdt_parent_offset(fdt, gbe);
1110 netcp = fdt_parent_offset(fdt, netcp_devices);
1111
1112 ks2_eth_parse_slave_interface(netcp, slave, priv, pdata);
1113
1114 pdata->iobase = fdtdec_get_addr(fdt, netcp, "reg");
Mugunthan V Nc8386732016-02-02 15:51:33 +05301115
1116 return 0;
1117}
1118
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301119static int ks2_eth_ofdata_to_platdata(struct udevice *dev)
1120{
1121 struct ks2_eth_priv *priv = dev_get_priv(dev);
1122 struct eth_pdata *pdata = dev_get_platdata(dev);
1123 const void *fdt = gd->fdt_blob;
1124 int gbe_0 = -ENODEV;
1125 int netcp_devices;
1126 int gbe;
1127
Simon Glassdd79d6e2017-01-17 16:52:55 -07001128 netcp_devices = fdt_subnode_offset(fdt, dev_of_offset(dev),
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301129 "netcp-devices");
1130 gbe = fdt_subnode_offset(fdt, netcp_devices, "gbe");
1131
1132 ks2_eth_bind_slaves(dev, gbe, &gbe_0);
1133
Simon Glassdd79d6e2017-01-17 16:52:55 -07001134 ks2_eth_parse_slave_interface(dev_of_offset(dev), gbe_0, priv, pdata);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301135
Simon Glassba1dea42017-05-17 17:18:05 -06001136 pdata->iobase = devfdt_get_addr(dev);
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301137
1138 return 0;
1139}
1140
Mugunthan V Nc8386732016-02-02 15:51:33 +05301141static const struct udevice_id ks2_eth_ids[] = {
1142 { .compatible = "ti,netcp-1.0" },
1143 { }
1144};
1145
Mugunthan V N1bf9c6f2016-08-02 12:01:12 +05301146U_BOOT_DRIVER(eth_ks2_slave) = {
1147 .name = "eth_ks2_sl",
1148 .id = UCLASS_ETH,
1149 .ofdata_to_platdata = ks2_sl_eth_ofdata_to_platdata,
1150 .probe = ks2_eth_probe,
1151 .remove = ks2_eth_remove,
1152 .ops = &ks2_eth_ops,
1153 .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
1154 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1155 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1156};
Mugunthan V Nc8386732016-02-02 15:51:33 +05301157
1158U_BOOT_DRIVER(eth_ks2) = {
1159 .name = "eth_ks2",
1160 .id = UCLASS_ETH,
1161 .of_match = ks2_eth_ids,
1162 .ofdata_to_platdata = ks2_eth_ofdata_to_platdata,
1163 .probe = ks2_eth_probe,
1164 .remove = ks2_eth_remove,
1165 .ops = &ks2_eth_ops,
1166 .priv_auto_alloc_size = sizeof(struct ks2_eth_priv),
1167 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1168 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1169};
Vitaly Andrianov4cedef32015-09-19 16:26:50 +05301170#endif