blob: fa8e1ef3ce593e91b0bdb5ea9abd155131b5faa6 [file] [log] [blame]
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -04001/*
2 * Ethernet driver for TI K2HK EVM.
3 *
4 * (C) Copyright 2012-2014
5 * Texas Instruments Incorporated, <www.ti.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9#include <common.h>
10#include <command.h>
11
12#include <net.h>
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +030013#include <phy.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040014#include <miiphy.h>
15#include <malloc.h>
Khoronzhuk, Ivan7954b862014-09-05 19:02:47 +030016#include <asm/ti-common/keystone_nav.h>
Khoronzhuk, Ivanf2c13ba2014-09-29 22:17:22 +030017#include <asm/ti-common/keystone_net.h>
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +030018#include <asm/ti-common/keystone_serdes.h>
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040019
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040020unsigned int emac_open;
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030021static struct mii_dev *mdio_bus;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040022static unsigned int sys_has_mdio = 1;
23
24#ifdef KEYSTONE2_EMAC_GIG_ENABLE
25#define emac_gigabit_enable(x) keystone2_eth_gigabit_enable(x)
26#else
27#define emac_gigabit_enable(x) /* no gigabit to enable */
28#endif
29
30#define RX_BUFF_NUMS 24
31#define RX_BUFF_LEN 1520
32#define MAX_SIZE_STREAM_BUFFER RX_BUFF_LEN
33
34static u8 rx_buffs[RX_BUFF_NUMS * RX_BUFF_LEN] __aligned(16);
35
36struct rx_buff_desc net_rx_buffs = {
37 .buff_ptr = rx_buffs,
38 .num_buffs = RX_BUFF_NUMS,
39 .buff_len = RX_BUFF_LEN,
40 .rx_flow = 22,
41};
42
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +030043static void keystone2_net_serdes_setup(void);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040044
45static int gen_get_link_speed(int phy_addr);
46
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040047int keystone2_eth_read_mac_addr(struct eth_device *dev)
48{
49 struct eth_priv_t *eth_priv;
50 u32 maca = 0;
51 u32 macb = 0;
52
53 eth_priv = (struct eth_priv_t *)dev->priv;
54
55 /* Read the e-fuse mac address */
56 if (eth_priv->slave_port == 1) {
57 maca = __raw_readl(MAC_ID_BASE_ADDR);
58 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
59 }
60
61 dev->enetaddr[0] = (macb >> 8) & 0xff;
62 dev->enetaddr[1] = (macb >> 0) & 0xff;
63 dev->enetaddr[2] = (maca >> 24) & 0xff;
64 dev->enetaddr[3] = (maca >> 16) & 0xff;
65 dev->enetaddr[4] = (maca >> 8) & 0xff;
66 dev->enetaddr[5] = (maca >> 0) & 0xff;
67
68 return 0;
69}
70
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030071/* MDIO */
72
73static int keystone2_mdio_reset(struct mii_dev *bus)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040074{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030075 u_int32_t clkdiv;
76 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040077
78 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
79
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030080 writel((clkdiv & 0xffff) | MDIO_CONTROL_ENABLE |
81 MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE,
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040082 &adap_mdio->control);
83
84 while (readl(&adap_mdio->control) & MDIO_CONTROL_IDLE)
85 ;
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030086
87 return 0;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040088}
89
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030090/**
91 * keystone2_mdio_read - read a PHY register via MDIO interface.
92 * Blocks until operation is complete.
93 */
94static int keystone2_mdio_read(struct mii_dev *bus,
95 int addr, int devad, int reg)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040096{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +030097 int tmp;
98 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -040099
100 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
101 ;
102
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300103 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ |
104 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16),
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400105 &adap_mdio->useraccess0);
106
107 /* Wait for command to complete */
108 while ((tmp = readl(&adap_mdio->useraccess0)) & MDIO_USERACCESS0_GO)
109 ;
110
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300111 if (tmp & MDIO_USERACCESS0_ACK)
112 return tmp & 0xffff;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400113
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400114 return -1;
115}
116
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300117/**
118 * keystone2_mdio_write - write to a PHY register via MDIO interface.
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400119 * Blocks until operation is complete.
120 */
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300121static int keystone2_mdio_write(struct mii_dev *bus,
122 int addr, int devad, int reg, u16 val)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400123{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300124 struct mdio_regs *adap_mdio = bus->priv;
125
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400126 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
127 ;
128
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300129 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE |
130 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16) |
131 (val & 0xffff), &adap_mdio->useraccess0);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400132
133 /* Wait for command to complete */
134 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
135 ;
136
137 return 0;
138}
139
140/* PHY functions for a generic PHY */
141static int gen_get_link_speed(int phy_addr)
142{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300143 u_int16_t tmp;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400144
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300145 tmp = mdio_bus->read(mdio_bus, phy_addr,
146 MDIO_DEVAD_NONE, MII_STATUS_REG);
147 if (tmp & 0x04)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400148 return 0;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400149
150 return -1;
151}
152
153static void __attribute__((unused))
154 keystone2_eth_gigabit_enable(struct eth_device *dev)
155{
156 u_int16_t data;
157 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
158
159 if (sys_has_mdio) {
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300160 data = keystone2_mdio_read(mdio_bus, eth_priv->phy_addr,
161 MDIO_DEVAD_NONE, 0);
162 /* speed selection MSB */
163 if (!(data & (1 << 6)))
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400164 return;
165 }
166
167 /*
168 * Check if link detected is giga-bit
169 * If Gigabit mode detected, enable gigbit in MAC
170 */
Hao Zhangc079f402014-09-29 22:17:20 +0300171 writel(readl(DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) +
172 CPGMACSL_REG_CTL) |
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400173 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
Hao Zhangc079f402014-09-29 22:17:20 +0300174 DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + CPGMACSL_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400175}
176
177int keystone_sgmii_link_status(int port)
178{
179 u32 status = 0;
180
181 status = __raw_readl(SGMII_STATUS_REG(port));
182
183 return status & SGMII_REG_STATUS_LINK;
184}
185
186
187int keystone_get_link_status(struct eth_device *dev)
188{
189 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
190 int sgmii_link;
191 int link_state = 0;
192#if CONFIG_GET_LINK_STATUS_ATTEMPTS > 1
193 int j;
194
195 for (j = 0; (j < CONFIG_GET_LINK_STATUS_ATTEMPTS) && (link_state == 0);
196 j++) {
197#endif
198 sgmii_link =
199 keystone_sgmii_link_status(eth_priv->slave_port - 1);
200
201 if (sgmii_link) {
202 link_state = 1;
203
204 if (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY)
205 if (gen_get_link_speed(eth_priv->phy_addr))
206 link_state = 0;
207 }
208#if CONFIG_GET_LINK_STATUS_ATTEMPTS > 1
209 }
210#endif
211 return link_state;
212}
213
214int keystone_sgmii_config(int port, int interface)
215{
216 unsigned int i, status, mask;
217 unsigned int mr_adv_ability, control;
218
219 switch (interface) {
220 case SGMII_LINK_MAC_MAC_AUTONEG:
221 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
222 SGMII_REG_MR_ADV_LINK |
223 SGMII_REG_MR_ADV_FULL_DUPLEX |
224 SGMII_REG_MR_ADV_GIG_MODE);
225 control = (SGMII_REG_CONTROL_MASTER |
226 SGMII_REG_CONTROL_AUTONEG);
227
228 break;
229 case SGMII_LINK_MAC_PHY:
230 case SGMII_LINK_MAC_PHY_FORCED:
231 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
232 control = SGMII_REG_CONTROL_AUTONEG;
233
234 break;
235 case SGMII_LINK_MAC_MAC_FORCED:
236 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
237 SGMII_REG_MR_ADV_LINK |
238 SGMII_REG_MR_ADV_FULL_DUPLEX |
239 SGMII_REG_MR_ADV_GIG_MODE);
240 control = SGMII_REG_CONTROL_MASTER;
241
242 break;
243 case SGMII_LINK_MAC_FIBER:
244 mr_adv_ability = 0x20;
245 control = SGMII_REG_CONTROL_AUTONEG;
246
247 break;
248 default:
249 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
250 control = SGMII_REG_CONTROL_AUTONEG;
251 }
252
253 __raw_writel(0, SGMII_CTL_REG(port));
254
255 /*
256 * Wait for the SerDes pll to lock,
257 * but don't trap if lock is never read
258 */
259 for (i = 0; i < 1000; i++) {
260 udelay(2000);
261 status = __raw_readl(SGMII_STATUS_REG(port));
262 if ((status & SGMII_REG_STATUS_LOCK) != 0)
263 break;
264 }
265
266 __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
267 __raw_writel(control, SGMII_CTL_REG(port));
268
269
270 mask = SGMII_REG_STATUS_LINK;
271
272 if (control & SGMII_REG_CONTROL_AUTONEG)
273 mask |= SGMII_REG_STATUS_AUTONEG;
274
275 for (i = 0; i < 1000; i++) {
276 status = __raw_readl(SGMII_STATUS_REG(port));
277 if ((status & mask) == mask)
278 break;
279 }
280
281 return 0;
282}
283
284int mac_sl_reset(u32 port)
285{
286 u32 i, v;
287
288 if (port >= DEVICE_N_GMACSL_PORTS)
289 return GMACSL_RET_INVALID_PORT;
290
291 /* Set the soft reset bit */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300292 writel(CPGMAC_REG_RESET_VAL_RESET,
293 DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400294
295 /* Wait for the bit to clear */
296 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300297 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400298 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
299 CPGMAC_REG_RESET_VAL_RESET)
300 return GMACSL_RET_OK;
301 }
302
303 /* Timeout on the reset */
304 return GMACSL_RET_WARN_RESET_INCOMPLETE;
305}
306
307int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
308{
309 u32 v, i;
310 int ret = GMACSL_RET_OK;
311
312 if (port >= DEVICE_N_GMACSL_PORTS)
313 return GMACSL_RET_INVALID_PORT;
314
315 if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
316 cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
317 ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
318 }
319
320 /* Must wait if the device is undergoing reset */
321 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300322 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400323 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
324 CPGMAC_REG_RESET_VAL_RESET)
325 break;
326 }
327
328 if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
329 return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
330
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300331 writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
332 writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400333
334 return ret;
335}
336
337int ethss_config(u32 ctl, u32 max_pkt_size)
338{
339 u32 i;
340
341 /* Max length register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300342 writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400343
344 /* Control register */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300345 writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400346
347 /* All statistics enabled by default */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300348 writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
349 DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400350
351 /* Reset and enable the ALE */
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300352 writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
353 CPSW_REG_VAL_ALE_CTL_BYPASS,
354 DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400355
356 /* All ports put into forward mode */
357 for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
Khoronzhuk, Ivan49d39a22014-08-28 16:07:45 +0300358 writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
359 DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400360
361 return 0;
362}
363
364int ethss_start(void)
365{
366 int i;
367 struct mac_sl_cfg cfg;
368
369 cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
370 cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
371
372 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
373 mac_sl_reset(i);
374 mac_sl_config(i, &cfg);
375 }
376
377 return 0;
378}
379
380int ethss_stop(void)
381{
382 int i;
383
384 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
385 mac_sl_reset(i);
386
387 return 0;
388}
389
390int32_t cpmac_drv_send(u32 *buffer, int num_bytes, int slave_port_num)
391{
392 if (num_bytes < EMAC_MIN_ETHERNET_PKT_SIZE)
393 num_bytes = EMAC_MIN_ETHERNET_PKT_SIZE;
394
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300395 return ksnav_send(&netcp_pktdma, buffer,
396 num_bytes, (slave_port_num) << 16);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400397}
398
399/* Eth device open */
400static int keystone2_eth_open(struct eth_device *dev, bd_t *bis)
401{
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400402 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300403 struct phy_device *phy_dev = eth_priv->phy_dev;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400404
405 debug("+ emac_open\n");
406
407 net_rx_buffs.rx_flow = eth_priv->rx_flow;
408
409 sys_has_mdio =
410 (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY) ? 1 : 0;
411
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +0300412 keystone2_net_serdes_setup();
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400413
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400414 keystone_sgmii_config(eth_priv->slave_port - 1,
415 eth_priv->sgmii_link_type);
416
417 udelay(10000);
418
419 /* On chip switch configuration */
420 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
421
422 /* TODO: add error handling code */
423 if (qm_init()) {
424 printf("ERROR: qm_init()\n");
425 return -1;
426 }
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300427 if (ksnav_init(&netcp_pktdma, &net_rx_buffs)) {
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400428 qm_close();
429 printf("ERROR: netcp_init()\n");
430 return -1;
431 }
432
433 /*
434 * Streaming switch configuration. If not present this
435 * statement is defined to void in target.h.
436 * If present this is usually defined to a series of register writes
437 */
438 hw_config_streaming_switch();
439
440 if (sys_has_mdio) {
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300441 keystone2_mdio_reset(mdio_bus);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400442
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300443 phy_startup(phy_dev);
444 if (phy_dev->link == 0) {
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300445 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400446 qm_close();
447 return -1;
448 }
449 }
450
451 emac_gigabit_enable(dev);
452
453 ethss_start();
454
455 debug("- emac_open\n");
456
457 emac_open = 1;
458
459 return 0;
460}
461
462/* Eth device close */
463void keystone2_eth_close(struct eth_device *dev)
464{
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300465 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
466 struct phy_device *phy_dev = eth_priv->phy_dev;
467
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400468 debug("+ emac_close\n");
469
470 if (!emac_open)
471 return;
472
473 ethss_stop();
474
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300475 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400476 qm_close();
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300477 phy_shutdown(phy_dev);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400478
479 emac_open = 0;
480
481 debug("- emac_close\n");
482}
483
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400484/*
485 * This function sends a single packet on the network and returns
486 * positive number (number of bytes transmitted) or negative for error
487 */
488static int keystone2_eth_send_packet(struct eth_device *dev,
489 void *packet, int length)
490{
491 int ret_status = -1;
492 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
493
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400494 if (keystone_get_link_status(dev) == 0)
495 return -1;
496
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400497 if (cpmac_drv_send((u32 *)packet, length, eth_priv->slave_port) != 0)
498 return ret_status;
499
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400500 return length;
501}
502
503/*
504 * This function handles receipt of a packet from the network
505 */
506static int keystone2_eth_rcv_packet(struct eth_device *dev)
507{
508 void *hd;
509 int pkt_size;
510 u32 *pkt;
511
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300512 hd = ksnav_recv(&netcp_pktdma, &pkt, &pkt_size);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400513 if (hd == NULL)
514 return 0;
515
516 NetReceive((uchar *)pkt, pkt_size);
517
Khoronzhuk, Ivanfa2105a2014-09-05 19:02:48 +0300518 ksnav_release_rxhd(&netcp_pktdma, hd);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400519
520 return pkt_size;
521}
522
523/*
524 * This function initializes the EMAC hardware.
525 */
526int keystone2_emac_initialize(struct eth_priv_t *eth_priv)
527{
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300528 int res;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400529 struct eth_device *dev;
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300530 struct phy_device *phy_dev;
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400531
532 dev = malloc(sizeof(struct eth_device));
533 if (dev == NULL)
534 return -1;
535
536 memset(dev, 0, sizeof(struct eth_device));
537
538 strcpy(dev->name, eth_priv->int_name);
539 dev->priv = eth_priv;
540
541 keystone2_eth_read_mac_addr(dev);
542
543 dev->iobase = 0;
544 dev->init = keystone2_eth_open;
545 dev->halt = keystone2_eth_close;
546 dev->send = keystone2_eth_send_packet;
547 dev->recv = keystone2_eth_rcv_packet;
548
549 eth_register(dev);
550
Khoronzhuk, Ivan47409fb2014-10-17 20:44:34 +0300551 /* Register MDIO bus if it's not registered yet */
552 if (!mdio_bus) {
553 mdio_bus = mdio_alloc();
554 mdio_bus->read = keystone2_mdio_read;
555 mdio_bus->write = keystone2_mdio_write;
556 mdio_bus->reset = keystone2_mdio_reset;
557 mdio_bus->priv = (void *)EMAC_MDIO_BASE_ADDR;
558 sprintf(mdio_bus->name, "ethernet-mdio");
559
560 res = mdio_register(mdio_bus);
561 if (res)
562 return res;
563 }
564
Khoronzhuk, Ivan39cd9f02014-10-17 20:44:35 +0300565 /* Create phy device and bind it with driver */
566#ifdef CONFIG_KSNET_MDIO_PHY_CONFIG_ENABLE
567 phy_dev = phy_connect(mdio_bus, eth_priv->phy_addr,
568 dev, PHY_INTERFACE_MODE_SGMII);
569 phy_config(phy_dev);
570#else
571 phy_dev = phy_find_by_mask(mdio_bus, 1 << eth_priv->phy_addr,
572 PHY_INTERFACE_MODE_SGMII);
573 phy_dev->dev = dev;
574#endif
575 eth_priv->phy_dev = phy_dev;
576
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400577 return 0;
578}
579
Hao Zhangd890dff2014-10-22 17:18:23 +0300580struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
581 .clk = SERDES_CLOCK_156P25M,
582 .rate = SERDES_RATE_5G,
583 .rate_mode = SERDES_QUARTER_RATE,
584 .intf = SERDES_PHY_SGMII,
585 .loopback = 0,
586};
587
Khoronzhuk, Ivandbfecb22014-10-22 17:18:21 +0300588static void keystone2_net_serdes_setup(void)
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400589{
Hao Zhangd890dff2014-10-22 17:18:23 +0300590 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE,
591 &ks2_serdes_sgmii_156p25mhz,
592 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
593
594 /* wait till setup */
595 udelay(5000);
Karicheri, Muralidharan657f6b52014-04-01 15:01:13 -0400596}