blob: e06896fea710fa8141a03f74adb8d03bbd89ef06 [file] [log] [blame]
Sergey Kubushyne8f39122007-08-10 20:26:18 +02001/*
2 * Ethernet driver for TI TMS320DM644x (DaVinci) chips.
3 *
4 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
5 *
6 * Parts shamelessly stolen from TI's dm644x_emac.c. Original copyright
7 * follows:
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * dm644x_emac.c
12 *
13 * TI DaVinci (DM644X) EMAC peripheral driver source for DV-EVM
14 *
15 * Copyright (C) 2005 Texas Instruments.
16 *
17 * ----------------------------------------------------------------------------
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 * ----------------------------------------------------------------------------
33
34 * Modifications:
35 * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot.
36 * ver 1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors
37 *
38 */
39#include <common.h>
40#include <command.h>
41#include <net.h>
42#include <miiphy.h>
Ben Warren5301bbf2009-05-26 00:34:07 -070043#include <malloc.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020044#include <asm/arch/emac_defs.h>
Nick Thompsond5ee6f62009-12-18 13:33:07 +000045#include <asm/io.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020046
Sergey Kubushyne8f39122007-08-10 20:26:18 +020047unsigned int emac_dbg = 0;
48#define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)
49
Nick Thompsond5ee6f62009-12-18 13:33:07 +000050#ifdef DAVINCI_EMAC_GIG_ENABLE
51#define emac_gigabit_enable() davinci_eth_gigabit_enable()
52#else
53#define emac_gigabit_enable() /* no gigabit to enable */
54#endif
55
Sandeep Paulraj4b26f052008-08-31 00:39:46 +020056static void davinci_eth_mdio_enable(void);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020057
58static int gen_init_phy(int phy_addr);
59static int gen_is_phy_connected(int phy_addr);
60static int gen_get_link_speed(int phy_addr);
61static int gen_auto_negotiate(int phy_addr);
62
Sergey Kubushyne8f39122007-08-10 20:26:18 +020063void eth_mdio_enable(void)
64{
Sandeep Paulraj4b26f052008-08-31 00:39:46 +020065 davinci_eth_mdio_enable();
Sergey Kubushyne8f39122007-08-10 20:26:18 +020066}
Sergey Kubushyne8f39122007-08-10 20:26:18 +020067
Sergey Kubushyne8f39122007-08-10 20:26:18 +020068/* EMAC Addresses */
69static volatile emac_regs *adap_emac = (emac_regs *)EMAC_BASE_ADDR;
70static volatile ewrap_regs *adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;
71static volatile mdio_regs *adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;
72
73/* EMAC descriptors */
74static volatile emac_desc *emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);
75static volatile emac_desc *emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
76static volatile emac_desc *emac_rx_active_head = 0;
77static volatile emac_desc *emac_rx_active_tail = 0;
78static int emac_rx_queue_active = 0;
79
80/* Receive packet buffers */
81static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
82
83/* PHY address for a discovered PHY (0xff - not found) */
84static volatile u_int8_t active_phy_addr = 0xff;
85
86phy_t phy;
87
Ben Gardiner1fb49e32010-09-23 09:58:43 -040088static int davinci_eth_set_mac_addr(struct eth_device *dev)
89{
90 unsigned long mac_hi;
91 unsigned long mac_lo;
92
93 /*
94 * Set MAC Addresses & Init multicast Hash to 0 (disable any multicast
95 * receive)
96 * Using channel 0 only - other channels are disabled
97 * */
98 writel(0, &adap_emac->MACINDEX);
99 mac_hi = (dev->enetaddr[3] << 24) |
100 (dev->enetaddr[2] << 16) |
101 (dev->enetaddr[1] << 8) |
102 (dev->enetaddr[0]);
103 mac_lo = (dev->enetaddr[5] << 8) |
104 (dev->enetaddr[4]);
105
106 writel(mac_hi, &adap_emac->MACADDRHI);
107#if defined(DAVINCI_EMAC_VERSION2)
108 writel(mac_lo | EMAC_MAC_ADDR_IS_VALID | EMAC_MAC_ADDR_MATCH,
109 &adap_emac->MACADDRLO);
110#else
111 writel(mac_lo, &adap_emac->MACADDRLO);
112#endif
113
114 writel(0, &adap_emac->MACHASH1);
115 writel(0, &adap_emac->MACHASH2);
116
117 /* Set source MAC address - REQUIRED */
118 writel(mac_hi, &adap_emac->MACSRCADDRHI);
119 writel(mac_lo, &adap_emac->MACSRCADDRLO);
120
121
122 return 0;
123}
124
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200125static void davinci_eth_mdio_enable(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200126{
127 u_int32_t clkdiv;
128
129 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
130
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000131 writel((clkdiv & 0xff) |
132 MDIO_CONTROL_ENABLE |
133 MDIO_CONTROL_FAULT |
134 MDIO_CONTROL_FAULT_ENABLE,
135 &adap_mdio->CONTROL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200136
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000137 while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
138 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200139}
140
141/*
142 * Tries to find an active connected PHY. Returns 1 if address if found.
143 * If no active PHY (or more than one PHY) found returns 0.
144 * Sets active_phy_addr variable.
145 */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200146static int davinci_eth_phy_detect(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200147{
148 u_int32_t phy_act_state;
149 int i;
150
151 active_phy_addr = 0xff;
152
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000153 phy_act_state = readl(&adap_mdio->ALIVE) & EMAC_MDIO_PHY_MASK;
154 if (phy_act_state == 0)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200155 return(0); /* No active PHYs */
156
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200157 debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200158
159 for (i = 0; i < 32; i++) {
160 if (phy_act_state & (1 << i)) {
161 if (phy_act_state & ~(1 << i))
162 return(0); /* More than one PHY */
163 else {
164 active_phy_addr = i;
165 return(1);
166 }
167 }
168 }
169
170 return(0); /* Just to make GCC happy */
171}
172
173
174/* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200175int davinci_eth_phy_read(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t *data)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200176{
177 int tmp;
178
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000179 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
180 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200181
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000182 writel(MDIO_USERACCESS0_GO |
183 MDIO_USERACCESS0_WRITE_READ |
184 ((reg_num & 0x1f) << 21) |
185 ((phy_addr & 0x1f) << 16),
186 &adap_mdio->USERACCESS0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200187
188 /* Wait for command to complete */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000189 while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
190 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200191
192 if (tmp & MDIO_USERACCESS0_ACK) {
193 *data = tmp & 0xffff;
194 return(1);
195 }
196
197 *data = -1;
198 return(0);
199}
200
201/* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200202int davinci_eth_phy_write(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t data)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200203{
204
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000205 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
206 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200207
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000208 writel(MDIO_USERACCESS0_GO |
209 MDIO_USERACCESS0_WRITE_WRITE |
210 ((reg_num & 0x1f) << 21) |
211 ((phy_addr & 0x1f) << 16) |
212 (data & 0xffff),
213 &adap_mdio->USERACCESS0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200214
215 /* Wait for command to complete */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000216 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
217 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200218
219 return(1);
220}
221
222/* PHY functions for a generic PHY */
223static int gen_init_phy(int phy_addr)
224{
225 int ret = 1;
226
227 if (gen_get_link_speed(phy_addr)) {
228 /* Try another time */
229 ret = gen_get_link_speed(phy_addr);
230 }
231
232 return(ret);
233}
234
235static int gen_is_phy_connected(int phy_addr)
236{
237 u_int16_t dummy;
238
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200239 return(davinci_eth_phy_read(phy_addr, PHY_PHYIDR1, &dummy));
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200240}
241
242static int gen_get_link_speed(int phy_addr)
243{
244 u_int16_t tmp;
245
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200246 if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) && (tmp & 0x04))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200247 return(1);
248
249 return(0);
250}
251
252static int gen_auto_negotiate(int phy_addr)
253{
254 u_int16_t tmp;
255
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200256 if (!davinci_eth_phy_read(phy_addr, PHY_BMCR, &tmp))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200257 return(0);
258
259 /* Restart Auto_negotiation */
260 tmp |= PHY_BMCR_AUTON;
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200261 davinci_eth_phy_write(phy_addr, PHY_BMCR, tmp);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200262
263 /*check AutoNegotiate complete */
264 udelay (10000);
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200265 if (!davinci_eth_phy_read(phy_addr, PHY_BMSR, &tmp))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200266 return(0);
267
268 if (!(tmp & PHY_BMSR_AUTN_COMP))
269 return(0);
270
271 return(gen_get_link_speed(phy_addr));
272}
273/* End of generic PHY functions */
274
275
Wolfgang Denk56cbd022007-08-12 14:27:39 +0200276#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400277static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200278{
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200279 return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200280}
281
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400282static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200283{
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200284 return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200285}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200286#endif
287
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000288static void __attribute__((unused)) davinci_eth_gigabit_enable(void)
289{
290 u_int16_t data;
291
292 if (davinci_eth_phy_read(EMAC_MDIO_PHY_NUM, 0, &data)) {
293 if (data & (1 << 6)) { /* speed selection MSB */
294 /*
295 * Check if link detected is giga-bit
296 * If Gigabit mode detected, enable gigbit in MAC
297 */
298 writel(EMAC_MACCONTROL_GIGFORCE |
299 EMAC_MACCONTROL_GIGABIT_ENABLE,
300 &adap_emac->MACCONTROL);
301 }
302 }
303}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200304
305/* Eth device open */
Ben Warren5301bbf2009-05-26 00:34:07 -0700306static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200307{
308 dv_reg_p addr;
309 u_int32_t clkdiv, cnt;
310 volatile emac_desc *rx_desc;
311
312 debug_emac("+ emac_open\n");
313
314 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000315 writel(1, &adap_emac->SOFTRESET);
316 while (readl(&adap_emac->SOFTRESET) != 0)
317 ;
318#if defined(DAVINCI_EMAC_VERSION2)
319 writel(1, &adap_ewrap->softrst);
320 while (readl(&adap_ewrap->softrst) != 0)
321 ;
322#else
323 writel(0, &adap_ewrap->EWCTL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200324 for (cnt = 0; cnt < 5; cnt++) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000325 clkdiv = readl(&adap_ewrap->EWCTL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200326 }
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000327#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200328
329 rx_desc = emac_rx_desc;
330
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000331 writel(1, &adap_emac->TXCONTROL);
332 writel(1, &adap_emac->RXCONTROL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200333
Ben Gardiner1fb49e32010-09-23 09:58:43 -0400334 davinci_eth_set_mac_addr(dev);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200335
336 /* Set DMA 8 TX / 8 RX Head pointers to 0 */
337 addr = &adap_emac->TX0HDP;
338 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000339 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200340
341 addr = &adap_emac->RX0HDP;
342 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000343 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200344
345 /* Clear Statistics (do this before setting MacControl register) */
346 addr = &adap_emac->RXGOODFRAMES;
347 for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000348 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200349
350 /* No multicast addressing */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000351 writel(0, &adap_emac->MACHASH1);
352 writel(0, &adap_emac->MACHASH2);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200353
354 /* Create RX queue and set receive process in place */
355 emac_rx_active_head = emac_rx_desc;
356 for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
357 rx_desc->next = (u_int32_t)(rx_desc + 1);
358 rx_desc->buffer = &emac_rx_buffers[cnt * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
359 rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
360 rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
361 rx_desc++;
362 }
363
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000364 /* Finalize the rx desc list */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200365 rx_desc--;
366 rx_desc->next = 0;
367 emac_rx_active_tail = rx_desc;
368 emac_rx_queue_active = 1;
369
370 /* Enable TX/RX */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000371 writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
372 writel(0, &adap_emac->RXBUFFEROFFSET);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200373
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000374 /*
375 * No fancy configs - Use this for promiscous debug
376 * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
377 */
378 writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200379
380 /* Enable ch 0 only */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000381 writel(1, &adap_emac->RXUNICASTSET);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200382
383 /* Enable MII interface and Full duplex mode */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000384#ifdef CONFIG_SOC_DA8XX
385 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
386 EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
387 EMAC_MACCONTROL_RMIISPEED_100),
388 &adap_emac->MACCONTROL);
389#else
390 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
391 EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
392 &adap_emac->MACCONTROL);
393#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200394
395 /* Init MDIO & get link state */
396 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000397 writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
398 &adap_mdio->CONTROL);
399
400 /* We need to wait for MDIO to start */
401 udelay(1000);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200402
403 if (!phy.get_link_speed(active_phy_addr))
404 return(0);
405
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000406 emac_gigabit_enable();
407
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200408 /* Start receive process */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000409 writel((u_int32_t)emac_rx_desc, &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200410
411 debug_emac("- emac_open\n");
412
413 return(1);
414}
415
416/* EMAC Channel Teardown */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200417static void davinci_eth_ch_teardown(int ch)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200418{
419 dv_reg dly = 0xff;
420 dv_reg cnt;
421
422 debug_emac("+ emac_ch_teardown\n");
423
424 if (ch == EMAC_CH_TX) {
425 /* Init TX channel teardown */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000426 writel(1, &adap_emac->TXTEARDOWN);
427 do {
428 /*
429 * Wait here for Tx teardown completion interrupt to
430 * occur. Note: A task delay can be called here to pend
431 * rather than occupying CPU cycles - anyway it has
432 * been found that teardown takes very few cpu cycles
433 * and does not affect functionality
434 */
435 dly--;
436 udelay(1);
437 if (dly == 0)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200438 break;
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000439 cnt = readl(&adap_emac->TX0CP);
440 } while (cnt != 0xfffffffc);
441 writel(cnt, &adap_emac->TX0CP);
442 writel(0, &adap_emac->TX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200443 } else {
444 /* Init RX channel teardown */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000445 writel(1, &adap_emac->RXTEARDOWN);
446 do {
447 /*
448 * Wait here for Rx teardown completion interrupt to
449 * occur. Note: A task delay can be called here to pend
450 * rather than occupying CPU cycles - anyway it has
451 * been found that teardown takes very few cpu cycles
452 * and does not affect functionality
453 */
454 dly--;
455 udelay(1);
456 if (dly == 0)
Wolfgang Denka1be4762008-05-20 16:00:29 +0200457 break;
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000458 cnt = readl(&adap_emac->RX0CP);
459 } while (cnt != 0xfffffffc);
460 writel(cnt, &adap_emac->RX0CP);
461 writel(0, &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200462 }
463
464 debug_emac("- emac_ch_teardown\n");
465}
466
467/* Eth device close */
Ben Warren5301bbf2009-05-26 00:34:07 -0700468static void davinci_eth_close(struct eth_device *dev)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200469{
470 debug_emac("+ emac_close\n");
471
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200472 davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
473 davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200474
475 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000476 writel(1, &adap_emac->SOFTRESET);
477#if defined(DAVINCI_EMAC_VERSION2)
478 writel(1, &adap_ewrap->softrst);
479#else
480 writel(0, &adap_ewrap->EWCTL);
481#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200482
483 debug_emac("- emac_close\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200484}
485
486static int tx_send_loop = 0;
487
488/*
489 * This function sends a single packet on the network and returns
490 * positive number (number of bytes transmitted) or negative for error
491 */
Ben Warren5301bbf2009-05-26 00:34:07 -0700492static int davinci_eth_send_packet (struct eth_device *dev,
493 volatile void *packet, int length)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200494{
495 int ret_status = -1;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200496
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200497 tx_send_loop = 0;
498
499 /* Return error if no link */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200500 if (!phy.get_link_speed (active_phy_addr)) {
501 printf ("WARN: emac_send_packet: No link\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200502 return (ret_status);
503 }
504
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000505 emac_gigabit_enable();
506
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200507 /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200508 if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200509 length = EMAC_MIN_ETHERNET_PKT_SIZE;
510 }
511
512 /* Populate the TX descriptor */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200513 emac_tx_desc->next = 0;
514 emac_tx_desc->buffer = (u_int8_t *) packet;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200515 emac_tx_desc->buff_off_len = (length & 0xffff);
516 emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
Wolfgang Denka1be4762008-05-20 16:00:29 +0200517 EMAC_CPPI_SOP_BIT |
518 EMAC_CPPI_OWNERSHIP_BIT |
519 EMAC_CPPI_EOP_BIT);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200520 /* Send the packet */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000521 writel((unsigned long)emac_tx_desc, &adap_emac->TX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200522
523 /* Wait for packet to complete or link down */
524 while (1) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200525 if (!phy.get_link_speed (active_phy_addr)) {
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200526 davinci_eth_ch_teardown (EMAC_CH_TX);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200527 return (ret_status);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200528 }
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000529
530 emac_gigabit_enable();
531
532 if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200533 ret_status = length;
534 break;
535 }
536 tx_send_loop++;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200537 }
538
Wolfgang Denka1be4762008-05-20 16:00:29 +0200539 return (ret_status);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200540}
541
542/*
543 * This function handles receipt of a packet from the network
544 */
Ben Warren5301bbf2009-05-26 00:34:07 -0700545static int davinci_eth_rcv_packet (struct eth_device *dev)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200546{
Wolfgang Denka1be4762008-05-20 16:00:29 +0200547 volatile emac_desc *rx_curr_desc;
548 volatile emac_desc *curr_desc;
549 volatile emac_desc *tail_desc;
550 int status, ret = -1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200551
552 rx_curr_desc = emac_rx_active_head;
553 status = rx_curr_desc->pkt_flag_len;
554 if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200555 if (status & EMAC_CPPI_RX_ERROR_FRAME) {
556 /* Error in packet - discard it and requeue desc */
557 printf ("WARN: emac_rcv_pkt: Error in packet\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200558 } else {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200559 NetReceive (rx_curr_desc->buffer,
560 (rx_curr_desc->buff_off_len & 0xffff));
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200561 ret = rx_curr_desc->buff_off_len & 0xffff;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200562 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200563
Wolfgang Denka1be4762008-05-20 16:00:29 +0200564 /* Ack received packet descriptor */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000565 writel((unsigned long)rx_curr_desc, &adap_emac->RX0CP);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200566 curr_desc = rx_curr_desc;
567 emac_rx_active_head =
568 (volatile emac_desc *) rx_curr_desc->next;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200569
Wolfgang Denka1be4762008-05-20 16:00:29 +0200570 if (status & EMAC_CPPI_EOQ_BIT) {
571 if (emac_rx_active_head) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000572 writel((unsigned long)emac_rx_active_head,
573 &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200574 } else {
575 emac_rx_queue_active = 0;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200576 printf ("INFO:emac_rcv_packet: RX Queue not active\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200577 }
578 }
579
580 /* Recycle RX descriptor */
581 rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
582 rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
583 rx_curr_desc->next = 0;
584
585 if (emac_rx_active_head == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200586 printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200587 emac_rx_active_head = curr_desc;
588 emac_rx_active_tail = curr_desc;
589 if (emac_rx_queue_active != 0) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000590 writel((unsigned long)emac_rx_active_head,
591 &adap_emac->RX0HDP);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200592 printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200593 emac_rx_queue_active = 1;
594 }
595 } else {
596 tail_desc = emac_rx_active_tail;
597 emac_rx_active_tail = curr_desc;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200598 tail_desc->next = (unsigned int) curr_desc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200599 status = tail_desc->pkt_flag_len;
600 if (status & EMAC_CPPI_EOQ_BIT) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000601 writel((unsigned long)curr_desc,
602 &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200603 status &= ~EMAC_CPPI_EOQ_BIT;
604 tail_desc->pkt_flag_len = status;
605 }
606 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200607 return (ret);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200608 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200609 return (0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200610}
611
Ben Warren4c28e272009-04-27 23:19:10 -0700612/*
613 * This function initializes the emac hardware. It does NOT initialize
614 * EMAC modules power or pin multiplexors, that is done by board_init()
615 * much earlier in bootup process. Returns 1 on success, 0 otherwise.
616 */
Ben Warren5301bbf2009-05-26 00:34:07 -0700617int davinci_emac_initialize(void)
Ben Warren4c28e272009-04-27 23:19:10 -0700618{
619 u_int32_t phy_id;
620 u_int16_t tmp;
621 int i;
Ben Warren5301bbf2009-05-26 00:34:07 -0700622 struct eth_device *dev;
623
624 dev = malloc(sizeof *dev);
625
626 if (dev == NULL)
627 return -1;
628
629 memset(dev, 0, sizeof *dev);
630
631 dev->iobase = 0;
632 dev->init = davinci_eth_open;
633 dev->halt = davinci_eth_close;
634 dev->send = davinci_eth_send_packet;
635 dev->recv = davinci_eth_rcv_packet;
Ben Gardiner1fb49e32010-09-23 09:58:43 -0400636 dev->write_hwaddr = davinci_eth_set_mac_addr;
Ben Warren5301bbf2009-05-26 00:34:07 -0700637
638 eth_register(dev);
Ben Warren4c28e272009-04-27 23:19:10 -0700639
640 davinci_eth_mdio_enable();
641
642 for (i = 0; i < 256; i++) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000643 if (readl(&adap_mdio->ALIVE))
Ben Warren4c28e272009-04-27 23:19:10 -0700644 break;
645 udelay(10);
646 }
647
648 if (i >= 256) {
649 printf("No ETH PHY detected!!!\n");
650 return(0);
651 }
652
653 /* Find if a PHY is connected and get it's address */
654 if (!davinci_eth_phy_detect())
655 return(0);
656
657 /* Get PHY ID and initialize phy_ops for a detected PHY */
658 if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR1, &tmp)) {
659 active_phy_addr = 0xff;
660 return(0);
661 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200662
Ben Warren4c28e272009-04-27 23:19:10 -0700663 phy_id = (tmp << 16) & 0xffff0000;
664
665 if (!davinci_eth_phy_read(active_phy_addr, PHY_PHYIDR2, &tmp)) {
666 active_phy_addr = 0xff;
667 return(0);
668 }
669
670 phy_id |= tmp & 0x0000ffff;
671
672 switch (phy_id) {
673 case PHY_LXT972:
674 sprintf(phy.name, "LXT972 @ 0x%02x", active_phy_addr);
675 phy.init = lxt972_init_phy;
676 phy.is_phy_connected = lxt972_is_phy_connected;
677 phy.get_link_speed = lxt972_get_link_speed;
678 phy.auto_negotiate = lxt972_auto_negotiate;
679 break;
680 case PHY_DP83848:
681 sprintf(phy.name, "DP83848 @ 0x%02x", active_phy_addr);
682 phy.init = dp83848_init_phy;
683 phy.is_phy_connected = dp83848_is_phy_connected;
684 phy.get_link_speed = dp83848_get_link_speed;
685 phy.auto_negotiate = dp83848_auto_negotiate;
686 break;
687 default:
688 sprintf(phy.name, "GENERIC @ 0x%02x", active_phy_addr);
689 phy.init = gen_init_phy;
690 phy.is_phy_connected = gen_is_phy_connected;
691 phy.get_link_speed = gen_get_link_speed;
692 phy.auto_negotiate = gen_auto_negotiate;
693 }
694
695 printf("Ethernet PHY: %s\n", phy.name);
696
Ben Warren5301bbf2009-05-26 00:34:07 -0700697 miiphy_register(phy.name, davinci_mii_phy_read, davinci_mii_phy_write);
Ben Warren4c28e272009-04-27 23:19:10 -0700698 return(1);
699}