blob: 43c263e612e6dbaffbe3d94e49178a2bf2c7a438 [file] [log] [blame]
Karicheri, Muralidharanfc9a8e82014-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, Ivan3fe93622014-10-17 20:44:35 +030013#include <phy.h>
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040014#include <miiphy.h>
15#include <malloc.h>
Khoronzhuk, Ivanef454712014-09-05 19:02:47 +030016#include <asm/ti-common/keystone_nav.h>
Khoronzhuk, Ivan0935cac2014-09-29 22:17:22 +030017#include <asm/ti-common/keystone_net.h>
Khoronzhuk, Ivana43febd2014-10-22 17:18:21 +030018#include <asm/ti-common/keystone_serdes.h>
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040019
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040020unsigned int emac_open;
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030021static struct mii_dev *mdio_bus;
Karicheri, Muralidharanfc9a8e82014-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, Ivana43febd2014-10-22 17:18:21 +030043static void keystone2_net_serdes_setup(void);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040044
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040045int keystone2_eth_read_mac_addr(struct eth_device *dev)
46{
47 struct eth_priv_t *eth_priv;
48 u32 maca = 0;
49 u32 macb = 0;
50
51 eth_priv = (struct eth_priv_t *)dev->priv;
52
53 /* Read the e-fuse mac address */
54 if (eth_priv->slave_port == 1) {
55 maca = __raw_readl(MAC_ID_BASE_ADDR);
56 macb = __raw_readl(MAC_ID_BASE_ADDR + 4);
57 }
58
59 dev->enetaddr[0] = (macb >> 8) & 0xff;
60 dev->enetaddr[1] = (macb >> 0) & 0xff;
61 dev->enetaddr[2] = (maca >> 24) & 0xff;
62 dev->enetaddr[3] = (maca >> 16) & 0xff;
63 dev->enetaddr[4] = (maca >> 8) & 0xff;
64 dev->enetaddr[5] = (maca >> 0) & 0xff;
65
66 return 0;
67}
68
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030069/* MDIO */
70
71static int keystone2_mdio_reset(struct mii_dev *bus)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040072{
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030073 u_int32_t clkdiv;
74 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040075
76 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
77
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030078 writel((clkdiv & 0xffff) | MDIO_CONTROL_ENABLE |
79 MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE,
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040080 &adap_mdio->control);
81
82 while (readl(&adap_mdio->control) & MDIO_CONTROL_IDLE)
83 ;
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030084
85 return 0;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040086}
87
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030088/**
89 * keystone2_mdio_read - read a PHY register via MDIO interface.
90 * Blocks until operation is complete.
91 */
92static int keystone2_mdio_read(struct mii_dev *bus,
93 int addr, int devad, int reg)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040094{
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +030095 int tmp;
96 struct mdio_regs *adap_mdio = bus->priv;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -040097
98 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
99 ;
100
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300101 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ |
102 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16),
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400103 &adap_mdio->useraccess0);
104
105 /* Wait for command to complete */
106 while ((tmp = readl(&adap_mdio->useraccess0)) & MDIO_USERACCESS0_GO)
107 ;
108
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300109 if (tmp & MDIO_USERACCESS0_ACK)
110 return tmp & 0xffff;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400111
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400112 return -1;
113}
114
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300115/**
116 * keystone2_mdio_write - write to a PHY register via MDIO interface.
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400117 * Blocks until operation is complete.
118 */
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300119static int keystone2_mdio_write(struct mii_dev *bus,
120 int addr, int devad, int reg, u16 val)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400121{
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300122 struct mdio_regs *adap_mdio = bus->priv;
123
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400124 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
125 ;
126
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300127 writel(MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE |
128 ((reg & 0x1f) << 21) | ((addr & 0x1f) << 16) |
129 (val & 0xffff), &adap_mdio->useraccess0);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400130
131 /* Wait for command to complete */
132 while (readl(&adap_mdio->useraccess0) & MDIO_USERACCESS0_GO)
133 ;
134
135 return 0;
136}
137
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400138static void __attribute__((unused))
139 keystone2_eth_gigabit_enable(struct eth_device *dev)
140{
141 u_int16_t data;
142 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
143
144 if (sys_has_mdio) {
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300145 data = keystone2_mdio_read(mdio_bus, eth_priv->phy_addr,
146 MDIO_DEVAD_NONE, 0);
147 /* speed selection MSB */
148 if (!(data & (1 << 6)))
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400149 return;
150 }
151
152 /*
153 * Check if link detected is giga-bit
154 * If Gigabit mode detected, enable gigbit in MAC
155 */
Hao Zhangb2cfe322014-09-29 22:17:20 +0300156 writel(readl(DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) +
157 CPGMACSL_REG_CTL) |
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400158 EMAC_MACCONTROL_GIGFORCE | EMAC_MACCONTROL_GIGABIT_ENABLE,
Hao Zhangb2cfe322014-09-29 22:17:20 +0300159 DEVICE_EMACSL_BASE(eth_priv->slave_port - 1) + CPGMACSL_REG_CTL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400160}
161
162int keystone_sgmii_link_status(int port)
163{
164 u32 status = 0;
165
166 status = __raw_readl(SGMII_STATUS_REG(port));
167
Khoronzhuk, Ivana4d2ade2014-10-17 20:44:36 +0300168 return (status & SGMII_REG_STATUS_LOCK) &&
169 (status & SGMII_REG_STATUS_LINK);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400170}
171
172int keystone_sgmii_config(int port, int interface)
173{
174 unsigned int i, status, mask;
175 unsigned int mr_adv_ability, control;
176
177 switch (interface) {
178 case SGMII_LINK_MAC_MAC_AUTONEG:
179 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
180 SGMII_REG_MR_ADV_LINK |
181 SGMII_REG_MR_ADV_FULL_DUPLEX |
182 SGMII_REG_MR_ADV_GIG_MODE);
183 control = (SGMII_REG_CONTROL_MASTER |
184 SGMII_REG_CONTROL_AUTONEG);
185
186 break;
187 case SGMII_LINK_MAC_PHY:
188 case SGMII_LINK_MAC_PHY_FORCED:
189 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
190 control = SGMII_REG_CONTROL_AUTONEG;
191
192 break;
193 case SGMII_LINK_MAC_MAC_FORCED:
194 mr_adv_ability = (SGMII_REG_MR_ADV_ENABLE |
195 SGMII_REG_MR_ADV_LINK |
196 SGMII_REG_MR_ADV_FULL_DUPLEX |
197 SGMII_REG_MR_ADV_GIG_MODE);
198 control = SGMII_REG_CONTROL_MASTER;
199
200 break;
201 case SGMII_LINK_MAC_FIBER:
202 mr_adv_ability = 0x20;
203 control = SGMII_REG_CONTROL_AUTONEG;
204
205 break;
206 default:
207 mr_adv_ability = SGMII_REG_MR_ADV_ENABLE;
208 control = SGMII_REG_CONTROL_AUTONEG;
209 }
210
211 __raw_writel(0, SGMII_CTL_REG(port));
212
213 /*
214 * Wait for the SerDes pll to lock,
215 * but don't trap if lock is never read
216 */
217 for (i = 0; i < 1000; i++) {
218 udelay(2000);
219 status = __raw_readl(SGMII_STATUS_REG(port));
220 if ((status & SGMII_REG_STATUS_LOCK) != 0)
221 break;
222 }
223
224 __raw_writel(mr_adv_ability, SGMII_MRADV_REG(port));
225 __raw_writel(control, SGMII_CTL_REG(port));
226
227
228 mask = SGMII_REG_STATUS_LINK;
229
230 if (control & SGMII_REG_CONTROL_AUTONEG)
231 mask |= SGMII_REG_STATUS_AUTONEG;
232
233 for (i = 0; i < 1000; i++) {
234 status = __raw_readl(SGMII_STATUS_REG(port));
235 if ((status & mask) == mask)
236 break;
237 }
238
239 return 0;
240}
241
242int mac_sl_reset(u32 port)
243{
244 u32 i, v;
245
246 if (port >= DEVICE_N_GMACSL_PORTS)
247 return GMACSL_RET_INVALID_PORT;
248
249 /* Set the soft reset bit */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300250 writel(CPGMAC_REG_RESET_VAL_RESET,
251 DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400252
253 /* Wait for the bit to clear */
254 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300255 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400256 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
257 CPGMAC_REG_RESET_VAL_RESET)
258 return GMACSL_RET_OK;
259 }
260
261 /* Timeout on the reset */
262 return GMACSL_RET_WARN_RESET_INCOMPLETE;
263}
264
265int mac_sl_config(u_int16_t port, struct mac_sl_cfg *cfg)
266{
267 u32 v, i;
268 int ret = GMACSL_RET_OK;
269
270 if (port >= DEVICE_N_GMACSL_PORTS)
271 return GMACSL_RET_INVALID_PORT;
272
273 if (cfg->max_rx_len > CPGMAC_REG_MAXLEN_LEN) {
274 cfg->max_rx_len = CPGMAC_REG_MAXLEN_LEN;
275 ret = GMACSL_RET_WARN_MAXLEN_TOO_BIG;
276 }
277
278 /* Must wait if the device is undergoing reset */
279 for (i = 0; i < DEVICE_EMACSL_RESET_POLL_COUNT; i++) {
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300280 v = readl(DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_RESET);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400281 if ((v & CPGMAC_REG_RESET_VAL_RESET_MASK) !=
282 CPGMAC_REG_RESET_VAL_RESET)
283 break;
284 }
285
286 if (i == DEVICE_EMACSL_RESET_POLL_COUNT)
287 return GMACSL_RET_CONFIG_FAIL_RESET_ACTIVE;
288
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300289 writel(cfg->max_rx_len, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_MAXLEN);
290 writel(cfg->ctl, DEVICE_EMACSL_BASE(port) + CPGMACSL_REG_CTL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400291
292 return ret;
293}
294
295int ethss_config(u32 ctl, u32 max_pkt_size)
296{
297 u32 i;
298
299 /* Max length register */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300300 writel(max_pkt_size, DEVICE_CPSW_BASE + CPSW_REG_MAXLEN);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400301
302 /* Control register */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300303 writel(ctl, DEVICE_CPSW_BASE + CPSW_REG_CTL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400304
305 /* All statistics enabled by default */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300306 writel(CPSW_REG_VAL_STAT_ENABLE_ALL,
307 DEVICE_CPSW_BASE + CPSW_REG_STAT_PORT_EN);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400308
309 /* Reset and enable the ALE */
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300310 writel(CPSW_REG_VAL_ALE_CTL_RESET_AND_ENABLE |
311 CPSW_REG_VAL_ALE_CTL_BYPASS,
312 DEVICE_CPSW_BASE + CPSW_REG_ALE_CONTROL);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400313
314 /* All ports put into forward mode */
315 for (i = 0; i < DEVICE_CPSW_NUM_PORTS; i++)
Khoronzhuk, Ivane6c94282014-08-28 16:07:45 +0300316 writel(CPSW_REG_VAL_PORTCTL_FORWARD_MODE,
317 DEVICE_CPSW_BASE + CPSW_REG_ALE_PORTCTL(i));
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400318
319 return 0;
320}
321
322int ethss_start(void)
323{
324 int i;
325 struct mac_sl_cfg cfg;
326
327 cfg.max_rx_len = MAX_SIZE_STREAM_BUFFER;
328 cfg.ctl = GMACSL_ENABLE | GMACSL_RX_ENABLE_EXT_CTL;
329
330 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++) {
331 mac_sl_reset(i);
332 mac_sl_config(i, &cfg);
333 }
334
335 return 0;
336}
337
338int ethss_stop(void)
339{
340 int i;
341
342 for (i = 0; i < DEVICE_N_GMACSL_PORTS; i++)
343 mac_sl_reset(i);
344
345 return 0;
346}
347
348int32_t cpmac_drv_send(u32 *buffer, int num_bytes, int slave_port_num)
349{
350 if (num_bytes < EMAC_MIN_ETHERNET_PKT_SIZE)
351 num_bytes = EMAC_MIN_ETHERNET_PKT_SIZE;
352
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300353 return ksnav_send(&netcp_pktdma, buffer,
354 num_bytes, (slave_port_num) << 16);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400355}
356
357/* Eth device open */
358static int keystone2_eth_open(struct eth_device *dev, bd_t *bis)
359{
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400360 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300361 struct phy_device *phy_dev = eth_priv->phy_dev;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400362
363 debug("+ emac_open\n");
364
365 net_rx_buffs.rx_flow = eth_priv->rx_flow;
366
367 sys_has_mdio =
368 (eth_priv->sgmii_link_type == SGMII_LINK_MAC_PHY) ? 1 : 0;
369
Khoronzhuk, Ivana43febd2014-10-22 17:18:21 +0300370 keystone2_net_serdes_setup();
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400371
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400372 keystone_sgmii_config(eth_priv->slave_port - 1,
373 eth_priv->sgmii_link_type);
374
375 udelay(10000);
376
377 /* On chip switch configuration */
378 ethss_config(target_get_switch_ctl(), SWITCH_MAX_PKT_SIZE);
379
380 /* TODO: add error handling code */
381 if (qm_init()) {
382 printf("ERROR: qm_init()\n");
383 return -1;
384 }
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300385 if (ksnav_init(&netcp_pktdma, &net_rx_buffs)) {
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400386 qm_close();
387 printf("ERROR: netcp_init()\n");
388 return -1;
389 }
390
391 /*
392 * Streaming switch configuration. If not present this
393 * statement is defined to void in target.h.
394 * If present this is usually defined to a series of register writes
395 */
396 hw_config_streaming_switch();
397
398 if (sys_has_mdio) {
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300399 keystone2_mdio_reset(mdio_bus);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400400
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300401 phy_startup(phy_dev);
402 if (phy_dev->link == 0) {
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300403 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400404 qm_close();
405 return -1;
406 }
407 }
408
409 emac_gigabit_enable(dev);
410
411 ethss_start();
412
413 debug("- emac_open\n");
414
415 emac_open = 1;
416
417 return 0;
418}
419
420/* Eth device close */
421void keystone2_eth_close(struct eth_device *dev)
422{
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300423 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
424 struct phy_device *phy_dev = eth_priv->phy_dev;
425
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400426 debug("+ emac_close\n");
427
428 if (!emac_open)
429 return;
430
431 ethss_stop();
432
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300433 ksnav_close(&netcp_pktdma);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400434 qm_close();
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300435 phy_shutdown(phy_dev);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400436
437 emac_open = 0;
438
439 debug("- emac_close\n");
440}
441
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400442/*
443 * This function sends a single packet on the network and returns
444 * positive number (number of bytes transmitted) or negative for error
445 */
446static int keystone2_eth_send_packet(struct eth_device *dev,
447 void *packet, int length)
448{
449 int ret_status = -1;
450 struct eth_priv_t *eth_priv = (struct eth_priv_t *)dev->priv;
Khoronzhuk, Ivana4d2ade2014-10-17 20:44:36 +0300451 struct phy_device *phy_dev = eth_priv->phy_dev;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400452
Khoronzhuk, Ivana4d2ade2014-10-17 20:44:36 +0300453 genphy_update_link(phy_dev);
454 if (phy_dev->link == 0)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400455 return -1;
456
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400457 if (cpmac_drv_send((u32 *)packet, length, eth_priv->slave_port) != 0)
458 return ret_status;
459
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400460 return length;
461}
462
463/*
464 * This function handles receipt of a packet from the network
465 */
466static int keystone2_eth_rcv_packet(struct eth_device *dev)
467{
468 void *hd;
469 int pkt_size;
470 u32 *pkt;
471
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300472 hd = ksnav_recv(&netcp_pktdma, &pkt, &pkt_size);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400473 if (hd == NULL)
474 return 0;
475
476 NetReceive((uchar *)pkt, pkt_size);
477
Khoronzhuk, Ivan9ea90212014-09-05 19:02:48 +0300478 ksnav_release_rxhd(&netcp_pktdma, hd);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400479
480 return pkt_size;
481}
482
483/*
484 * This function initializes the EMAC hardware.
485 */
486int keystone2_emac_initialize(struct eth_priv_t *eth_priv)
487{
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300488 int res;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400489 struct eth_device *dev;
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300490 struct phy_device *phy_dev;
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400491
492 dev = malloc(sizeof(struct eth_device));
493 if (dev == NULL)
494 return -1;
495
496 memset(dev, 0, sizeof(struct eth_device));
497
498 strcpy(dev->name, eth_priv->int_name);
499 dev->priv = eth_priv;
500
501 keystone2_eth_read_mac_addr(dev);
502
503 dev->iobase = 0;
504 dev->init = keystone2_eth_open;
505 dev->halt = keystone2_eth_close;
506 dev->send = keystone2_eth_send_packet;
507 dev->recv = keystone2_eth_rcv_packet;
508
509 eth_register(dev);
510
Khoronzhuk, Ivan550c5ce2014-10-17 20:44:34 +0300511 /* Register MDIO bus if it's not registered yet */
512 if (!mdio_bus) {
513 mdio_bus = mdio_alloc();
514 mdio_bus->read = keystone2_mdio_read;
515 mdio_bus->write = keystone2_mdio_write;
516 mdio_bus->reset = keystone2_mdio_reset;
517 mdio_bus->priv = (void *)EMAC_MDIO_BASE_ADDR;
518 sprintf(mdio_bus->name, "ethernet-mdio");
519
520 res = mdio_register(mdio_bus);
521 if (res)
522 return res;
523 }
524
Khoronzhuk, Ivan3fe93622014-10-17 20:44:35 +0300525 /* Create phy device and bind it with driver */
526#ifdef CONFIG_KSNET_MDIO_PHY_CONFIG_ENABLE
527 phy_dev = phy_connect(mdio_bus, eth_priv->phy_addr,
528 dev, PHY_INTERFACE_MODE_SGMII);
529 phy_config(phy_dev);
530#else
531 phy_dev = phy_find_by_mask(mdio_bus, 1 << eth_priv->phy_addr,
532 PHY_INTERFACE_MODE_SGMII);
533 phy_dev->dev = dev;
534#endif
535 eth_priv->phy_dev = phy_dev;
536
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400537 return 0;
538}
539
Hao Zhang92a16c82014-10-22 17:18:23 +0300540struct ks2_serdes ks2_serdes_sgmii_156p25mhz = {
541 .clk = SERDES_CLOCK_156P25M,
542 .rate = SERDES_RATE_5G,
543 .rate_mode = SERDES_QUARTER_RATE,
544 .intf = SERDES_PHY_SGMII,
545 .loopback = 0,
546};
547
Khoronzhuk, Ivana43febd2014-10-22 17:18:21 +0300548static void keystone2_net_serdes_setup(void)
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400549{
Hao Zhang92a16c82014-10-22 17:18:23 +0300550 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII_BASE,
551 &ks2_serdes_sgmii_156p25mhz,
552 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
553
Khoronzhuk, Ivan3c615022014-10-17 21:01:13 +0300554#ifdef CONFIG_SOC_K2E
555 ks2_serdes_init(CONFIG_KSNET_SERDES_SGMII2_BASE,
556 &ks2_serdes_sgmii_156p25mhz,
557 CONFIG_KSNET_SERDES_LANES_PER_SGMII);
558#endif
559
Hao Zhang92a16c82014-10-22 17:18:23 +0300560 /* wait till setup */
561 udelay(5000);
Karicheri, Muralidharanfc9a8e82014-04-01 15:01:13 -0400562}