blob: 56cd2aaf492b63cfeeb6e9bd8631c9d3dc847862 [file] [log] [blame]
Sergey Kubushync74b2102007-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 Warren84535872009-05-26 00:34:07 -070043#include <malloc.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020044#include <asm/arch/emac_defs.h>
Nick Thompsond7e35432009-12-18 13:33:07 +000045#include <asm/io.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020046
Sergey Kubushync74b2102007-08-10 20:26:18 +020047unsigned int emac_dbg = 0;
48#define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)
49
Nick Thompsond7e35432009-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 Paulrajfcaac582008-08-31 00:39:46 +020056static void davinci_eth_mdio_enable(void);
Sergey Kubushync74b2102007-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 Kubushync74b2102007-08-10 20:26:18 +020063void eth_mdio_enable(void)
64{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +020065 davinci_eth_mdio_enable();
Sergey Kubushync74b2102007-08-10 20:26:18 +020066}
Sergey Kubushync74b2102007-08-10 20:26:18 +020067
Sergey Kubushync74b2102007-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 Gardiner7b37a272010-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 Paulrajfcaac582008-08-31 00:39:46 +0200125static void davinci_eth_mdio_enable(void)
Sergey Kubushync74b2102007-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 Thompsond7e35432009-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 Kubushync74b2102007-08-10 20:26:18 +0200136
Nick Thompsond7e35432009-12-18 13:33:07 +0000137 while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
138 ;
Sergey Kubushync74b2102007-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 Paulrajfcaac582008-08-31 00:39:46 +0200146static int davinci_eth_phy_detect(void)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200147{
148 u_int32_t phy_act_state;
149 int i;
150
151 active_phy_addr = 0xff;
152
Nick Thompsond7e35432009-12-18 13:33:07 +0000153 phy_act_state = readl(&adap_mdio->ALIVE) & EMAC_MDIO_PHY_MASK;
154 if (phy_act_state == 0)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200155 return(0); /* No active PHYs */
156
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200157 debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
Sergey Kubushync74b2102007-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 Paulrajfcaac582008-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 Kubushync74b2102007-08-10 20:26:18 +0200176{
177 int tmp;
178
Nick Thompsond7e35432009-12-18 13:33:07 +0000179 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
180 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200181
Nick Thompsond7e35432009-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 Kubushync74b2102007-08-10 20:26:18 +0200187
188 /* Wait for command to complete */
Nick Thompsond7e35432009-12-18 13:33:07 +0000189 while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
190 ;
Sergey Kubushync74b2102007-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 Paulrajfcaac582008-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 Kubushync74b2102007-08-10 20:26:18 +0200203{
204
Nick Thompsond7e35432009-12-18 13:33:07 +0000205 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
206 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200207
Nick Thompsond7e35432009-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 Kubushync74b2102007-08-10 20:26:18 +0200214
215 /* Wait for command to complete */
Nick Thompsond7e35432009-12-18 13:33:07 +0000216 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
217 ;
Sergey Kubushync74b2102007-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
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500239 return(davinci_eth_phy_read(phy_addr, MII_PHYSID1, &dummy));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200240}
241
242static int gen_get_link_speed(int phy_addr)
243{
244 u_int16_t tmp;
245
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500246 if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) &&
247 (tmp & 0x04)) {
248#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
249 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
250 davinci_eth_phy_read(phy_addr, PHY_ANLPAR, &tmp);
251
252 /* Speed doesn't matter, there is no setting for it in EMAC. */
253 if (tmp & (PHY_ANLPAR_TXFD | PHY_ANLPAR_10FD)) {
254 /* set EMAC for Full Duplex */
255 writel(EMAC_MACCONTROL_MIIEN_ENABLE |
256 EMAC_MACCONTROL_FULLDUPLEX_ENABLE,
257 &adap_emac->MACCONTROL);
258 } else {
259 /*set EMAC for Half Duplex */
260 writel(EMAC_MACCONTROL_MIIEN_ENABLE,
261 &adap_emac->MACCONTROL);
262 }
263
264 if (tmp & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
265 writel(readl(&adap_emac->MACCONTROL) |
266 EMAC_MACCONTROL_RMIISPEED_100,
267 &adap_emac->MACCONTROL);
268 else
269 writel(readl(&adap_emac->MACCONTROL) &
270 ~EMAC_MACCONTROL_RMIISPEED_100,
271 &adap_emac->MACCONTROL);
272#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200273 return(1);
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500274 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200275
276 return(0);
277}
278
279static int gen_auto_negotiate(int phy_addr)
280{
281 u_int16_t tmp;
282
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500283 if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200284 return(0);
285
286 /* Restart Auto_negotiation */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500287 tmp |= BMCR_ANENABLE;
288 davinci_eth_phy_write(phy_addr, MII_BMCR, tmp);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200289
290 /*check AutoNegotiate complete */
291 udelay (10000);
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500292 if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200293 return(0);
294
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500295 if (!(tmp & BMSR_ANEGCOMPLETE))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200296 return(0);
297
298 return(gen_get_link_speed(phy_addr));
299}
300/* End of generic PHY functions */
301
302
Wolfgang Denkafaac862007-08-12 14:27:39 +0200303#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Mike Frysinger5700bb62010-07-27 18:35:08 -0400304static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200305{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200306 return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200307}
308
Mike Frysinger5700bb62010-07-27 18:35:08 -0400309static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200310{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200311 return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200312}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200313#endif
314
Nick Thompsond7e35432009-12-18 13:33:07 +0000315static void __attribute__((unused)) davinci_eth_gigabit_enable(void)
316{
317 u_int16_t data;
318
319 if (davinci_eth_phy_read(EMAC_MDIO_PHY_NUM, 0, &data)) {
320 if (data & (1 << 6)) { /* speed selection MSB */
321 /*
322 * Check if link detected is giga-bit
323 * If Gigabit mode detected, enable gigbit in MAC
324 */
325 writel(EMAC_MACCONTROL_GIGFORCE |
326 EMAC_MACCONTROL_GIGABIT_ENABLE,
327 &adap_emac->MACCONTROL);
328 }
329 }
330}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200331
332/* Eth device open */
Ben Warren84535872009-05-26 00:34:07 -0700333static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200334{
335 dv_reg_p addr;
336 u_int32_t clkdiv, cnt;
337 volatile emac_desc *rx_desc;
338
339 debug_emac("+ emac_open\n");
340
341 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond7e35432009-12-18 13:33:07 +0000342 writel(1, &adap_emac->SOFTRESET);
343 while (readl(&adap_emac->SOFTRESET) != 0)
344 ;
345#if defined(DAVINCI_EMAC_VERSION2)
346 writel(1, &adap_ewrap->softrst);
347 while (readl(&adap_ewrap->softrst) != 0)
348 ;
349#else
350 writel(0, &adap_ewrap->EWCTL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200351 for (cnt = 0; cnt < 5; cnt++) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000352 clkdiv = readl(&adap_ewrap->EWCTL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200353 }
Nick Thompsond7e35432009-12-18 13:33:07 +0000354#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200355
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500356#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
357 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
358 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
359 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
360 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
361#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200362 rx_desc = emac_rx_desc;
363
Nick Thompsond7e35432009-12-18 13:33:07 +0000364 writel(1, &adap_emac->TXCONTROL);
365 writel(1, &adap_emac->RXCONTROL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200366
Ben Gardiner7b37a272010-09-23 09:58:43 -0400367 davinci_eth_set_mac_addr(dev);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200368
369 /* Set DMA 8 TX / 8 RX Head pointers to 0 */
370 addr = &adap_emac->TX0HDP;
371 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000372 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200373
374 addr = &adap_emac->RX0HDP;
375 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000376 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200377
378 /* Clear Statistics (do this before setting MacControl register) */
379 addr = &adap_emac->RXGOODFRAMES;
380 for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000381 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200382
383 /* No multicast addressing */
Nick Thompsond7e35432009-12-18 13:33:07 +0000384 writel(0, &adap_emac->MACHASH1);
385 writel(0, &adap_emac->MACHASH2);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200386
387 /* Create RX queue and set receive process in place */
388 emac_rx_active_head = emac_rx_desc;
389 for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
390 rx_desc->next = (u_int32_t)(rx_desc + 1);
391 rx_desc->buffer = &emac_rx_buffers[cnt * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];
392 rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
393 rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
394 rx_desc++;
395 }
396
Nick Thompsond7e35432009-12-18 13:33:07 +0000397 /* Finalize the rx desc list */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200398 rx_desc--;
399 rx_desc->next = 0;
400 emac_rx_active_tail = rx_desc;
401 emac_rx_queue_active = 1;
402
403 /* Enable TX/RX */
Nick Thompsond7e35432009-12-18 13:33:07 +0000404 writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
405 writel(0, &adap_emac->RXBUFFEROFFSET);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200406
Nick Thompsond7e35432009-12-18 13:33:07 +0000407 /*
408 * No fancy configs - Use this for promiscous debug
409 * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
410 */
411 writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200412
413 /* Enable ch 0 only */
Nick Thompsond7e35432009-12-18 13:33:07 +0000414 writel(1, &adap_emac->RXUNICASTSET);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200415
416 /* Enable MII interface and Full duplex mode */
Nick Thompsond7e35432009-12-18 13:33:07 +0000417#ifdef CONFIG_SOC_DA8XX
418 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
419 EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
420 EMAC_MACCONTROL_RMIISPEED_100),
421 &adap_emac->MACCONTROL);
422#else
423 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
424 EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
425 &adap_emac->MACCONTROL);
426#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200427
428 /* Init MDIO & get link state */
429 clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1;
Nick Thompsond7e35432009-12-18 13:33:07 +0000430 writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
431 &adap_mdio->CONTROL);
432
433 /* We need to wait for MDIO to start */
434 udelay(1000);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200435
436 if (!phy.get_link_speed(active_phy_addr))
437 return(0);
438
Nick Thompsond7e35432009-12-18 13:33:07 +0000439 emac_gigabit_enable();
440
Sergey Kubushync74b2102007-08-10 20:26:18 +0200441 /* Start receive process */
Nick Thompsond7e35432009-12-18 13:33:07 +0000442 writel((u_int32_t)emac_rx_desc, &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200443
444 debug_emac("- emac_open\n");
445
446 return(1);
447}
448
449/* EMAC Channel Teardown */
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200450static void davinci_eth_ch_teardown(int ch)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200451{
452 dv_reg dly = 0xff;
453 dv_reg cnt;
454
455 debug_emac("+ emac_ch_teardown\n");
456
457 if (ch == EMAC_CH_TX) {
458 /* Init TX channel teardown */
Nick Thompsond7e35432009-12-18 13:33:07 +0000459 writel(1, &adap_emac->TXTEARDOWN);
460 do {
461 /*
462 * Wait here for Tx teardown completion interrupt to
463 * occur. Note: A task delay can be called here to pend
464 * rather than occupying CPU cycles - anyway it has
465 * been found that teardown takes very few cpu cycles
466 * and does not affect functionality
467 */
468 dly--;
469 udelay(1);
470 if (dly == 0)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200471 break;
Nick Thompsond7e35432009-12-18 13:33:07 +0000472 cnt = readl(&adap_emac->TX0CP);
473 } while (cnt != 0xfffffffc);
474 writel(cnt, &adap_emac->TX0CP);
475 writel(0, &adap_emac->TX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200476 } else {
477 /* Init RX channel teardown */
Nick Thompsond7e35432009-12-18 13:33:07 +0000478 writel(1, &adap_emac->RXTEARDOWN);
479 do {
480 /*
481 * Wait here for Rx teardown completion interrupt to
482 * occur. Note: A task delay can be called here to pend
483 * rather than occupying CPU cycles - anyway it has
484 * been found that teardown takes very few cpu cycles
485 * and does not affect functionality
486 */
487 dly--;
488 udelay(1);
489 if (dly == 0)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200490 break;
Nick Thompsond7e35432009-12-18 13:33:07 +0000491 cnt = readl(&adap_emac->RX0CP);
492 } while (cnt != 0xfffffffc);
493 writel(cnt, &adap_emac->RX0CP);
494 writel(0, &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200495 }
496
497 debug_emac("- emac_ch_teardown\n");
498}
499
500/* Eth device close */
Ben Warren84535872009-05-26 00:34:07 -0700501static void davinci_eth_close(struct eth_device *dev)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200502{
503 debug_emac("+ emac_close\n");
504
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200505 davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
506 davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200507
508 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond7e35432009-12-18 13:33:07 +0000509 writel(1, &adap_emac->SOFTRESET);
510#if defined(DAVINCI_EMAC_VERSION2)
511 writel(1, &adap_ewrap->softrst);
512#else
513 writel(0, &adap_ewrap->EWCTL);
514#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200515
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500516#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
517 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
518 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
519 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
520 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
521#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200522 debug_emac("- emac_close\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200523}
524
525static int tx_send_loop = 0;
526
527/*
528 * This function sends a single packet on the network and returns
529 * positive number (number of bytes transmitted) or negative for error
530 */
Ben Warren84535872009-05-26 00:34:07 -0700531static int davinci_eth_send_packet (struct eth_device *dev,
532 volatile void *packet, int length)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200533{
534 int ret_status = -1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200535
Sergey Kubushync74b2102007-08-10 20:26:18 +0200536 tx_send_loop = 0;
537
538 /* Return error if no link */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200539 if (!phy.get_link_speed (active_phy_addr)) {
540 printf ("WARN: emac_send_packet: No link\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200541 return (ret_status);
542 }
543
Nick Thompsond7e35432009-12-18 13:33:07 +0000544 emac_gigabit_enable();
545
Sergey Kubushync74b2102007-08-10 20:26:18 +0200546 /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200547 if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
Sergey Kubushync74b2102007-08-10 20:26:18 +0200548 length = EMAC_MIN_ETHERNET_PKT_SIZE;
549 }
550
551 /* Populate the TX descriptor */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200552 emac_tx_desc->next = 0;
553 emac_tx_desc->buffer = (u_int8_t *) packet;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200554 emac_tx_desc->buff_off_len = (length & 0xffff);
555 emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200556 EMAC_CPPI_SOP_BIT |
557 EMAC_CPPI_OWNERSHIP_BIT |
558 EMAC_CPPI_EOP_BIT);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200559 /* Send the packet */
Nick Thompsond7e35432009-12-18 13:33:07 +0000560 writel((unsigned long)emac_tx_desc, &adap_emac->TX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200561
562 /* Wait for packet to complete or link down */
563 while (1) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200564 if (!phy.get_link_speed (active_phy_addr)) {
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200565 davinci_eth_ch_teardown (EMAC_CH_TX);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200566 return (ret_status);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200567 }
Nick Thompsond7e35432009-12-18 13:33:07 +0000568
569 emac_gigabit_enable();
570
571 if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200572 ret_status = length;
573 break;
574 }
575 tx_send_loop++;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200576 }
577
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200578 return (ret_status);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200579}
580
581/*
582 * This function handles receipt of a packet from the network
583 */
Ben Warren84535872009-05-26 00:34:07 -0700584static int davinci_eth_rcv_packet (struct eth_device *dev)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200585{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200586 volatile emac_desc *rx_curr_desc;
587 volatile emac_desc *curr_desc;
588 volatile emac_desc *tail_desc;
589 int status, ret = -1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200590
591 rx_curr_desc = emac_rx_active_head;
592 status = rx_curr_desc->pkt_flag_len;
593 if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200594 if (status & EMAC_CPPI_RX_ERROR_FRAME) {
595 /* Error in packet - discard it and requeue desc */
596 printf ("WARN: emac_rcv_pkt: Error in packet\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200597 } else {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200598 NetReceive (rx_curr_desc->buffer,
599 (rx_curr_desc->buff_off_len & 0xffff));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200600 ret = rx_curr_desc->buff_off_len & 0xffff;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200601 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200602
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200603 /* Ack received packet descriptor */
Nick Thompsond7e35432009-12-18 13:33:07 +0000604 writel((unsigned long)rx_curr_desc, &adap_emac->RX0CP);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200605 curr_desc = rx_curr_desc;
606 emac_rx_active_head =
607 (volatile emac_desc *) rx_curr_desc->next;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200608
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200609 if (status & EMAC_CPPI_EOQ_BIT) {
610 if (emac_rx_active_head) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000611 writel((unsigned long)emac_rx_active_head,
612 &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200613 } else {
614 emac_rx_queue_active = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200615 printf ("INFO:emac_rcv_packet: RX Queue not active\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200616 }
617 }
618
619 /* Recycle RX descriptor */
620 rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
621 rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
622 rx_curr_desc->next = 0;
623
624 if (emac_rx_active_head == 0) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200625 printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200626 emac_rx_active_head = curr_desc;
627 emac_rx_active_tail = curr_desc;
628 if (emac_rx_queue_active != 0) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000629 writel((unsigned long)emac_rx_active_head,
630 &adap_emac->RX0HDP);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200631 printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200632 emac_rx_queue_active = 1;
633 }
634 } else {
635 tail_desc = emac_rx_active_tail;
636 emac_rx_active_tail = curr_desc;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200637 tail_desc->next = (unsigned int) curr_desc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200638 status = tail_desc->pkt_flag_len;
639 if (status & EMAC_CPPI_EOQ_BIT) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000640 writel((unsigned long)curr_desc,
641 &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200642 status &= ~EMAC_CPPI_EOQ_BIT;
643 tail_desc->pkt_flag_len = status;
644 }
645 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200646 return (ret);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200647 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200648 return (0);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200649}
650
Ben Warren8cc13c12009-04-27 23:19:10 -0700651/*
652 * This function initializes the emac hardware. It does NOT initialize
653 * EMAC modules power or pin multiplexors, that is done by board_init()
654 * much earlier in bootup process. Returns 1 on success, 0 otherwise.
655 */
Ben Warren84535872009-05-26 00:34:07 -0700656int davinci_emac_initialize(void)
Ben Warren8cc13c12009-04-27 23:19:10 -0700657{
658 u_int32_t phy_id;
659 u_int16_t tmp;
660 int i;
Ben Warren84535872009-05-26 00:34:07 -0700661 struct eth_device *dev;
662
663 dev = malloc(sizeof *dev);
664
665 if (dev == NULL)
666 return -1;
667
668 memset(dev, 0, sizeof *dev);
669
670 dev->iobase = 0;
671 dev->init = davinci_eth_open;
672 dev->halt = davinci_eth_close;
673 dev->send = davinci_eth_send_packet;
674 dev->recv = davinci_eth_rcv_packet;
Ben Gardiner7b37a272010-09-23 09:58:43 -0400675 dev->write_hwaddr = davinci_eth_set_mac_addr;
Ben Warren84535872009-05-26 00:34:07 -0700676
677 eth_register(dev);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200678
Ben Warren8cc13c12009-04-27 23:19:10 -0700679 davinci_eth_mdio_enable();
680
681 for (i = 0; i < 256; i++) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000682 if (readl(&adap_mdio->ALIVE))
Ben Warren8cc13c12009-04-27 23:19:10 -0700683 break;
684 udelay(10);
685 }
686
687 if (i >= 256) {
688 printf("No ETH PHY detected!!!\n");
689 return(0);
690 }
691
692 /* Find if a PHY is connected and get it's address */
693 if (!davinci_eth_phy_detect())
694 return(0);
695
696 /* Get PHY ID and initialize phy_ops for a detected PHY */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500697 if (!davinci_eth_phy_read(active_phy_addr, MII_PHYSID1, &tmp)) {
Ben Warren8cc13c12009-04-27 23:19:10 -0700698 active_phy_addr = 0xff;
699 return(0);
700 }
701
702 phy_id = (tmp << 16) & 0xffff0000;
703
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500704 if (!davinci_eth_phy_read(active_phy_addr, MII_PHYSID2, &tmp)) {
Ben Warren8cc13c12009-04-27 23:19:10 -0700705 active_phy_addr = 0xff;
706 return(0);
707 }
708
709 phy_id |= tmp & 0x0000ffff;
710
711 switch (phy_id) {
712 case PHY_LXT972:
713 sprintf(phy.name, "LXT972 @ 0x%02x", active_phy_addr);
714 phy.init = lxt972_init_phy;
715 phy.is_phy_connected = lxt972_is_phy_connected;
716 phy.get_link_speed = lxt972_get_link_speed;
717 phy.auto_negotiate = lxt972_auto_negotiate;
718 break;
719 case PHY_DP83848:
720 sprintf(phy.name, "DP83848 @ 0x%02x", active_phy_addr);
721 phy.init = dp83848_init_phy;
722 phy.is_phy_connected = dp83848_is_phy_connected;
723 phy.get_link_speed = dp83848_get_link_speed;
724 phy.auto_negotiate = dp83848_auto_negotiate;
725 break;
726 default:
727 sprintf(phy.name, "GENERIC @ 0x%02x", active_phy_addr);
728 phy.init = gen_init_phy;
729 phy.is_phy_connected = gen_is_phy_connected;
730 phy.get_link_speed = gen_get_link_speed;
731 phy.auto_negotiate = gen_auto_negotiate;
732 }
733
734 printf("Ethernet PHY: %s\n", phy.name);
735
Ben Warren84535872009-05-26 00:34:07 -0700736 miiphy_register(phy.name, davinci_mii_phy_read, davinci_mii_phy_write);
Ben Warren8cc13c12009-04-27 23:19:10 -0700737 return(1);
738}