blob: 439f8ae99e364f2082ceaa2ad5f0630c21e0fe9f [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020019 * SPDX-License-Identifier: GPL-2.0+
Sergey Kubushync74b2102007-08-10 20:26:18 +020020 *
Sergey Kubushync74b2102007-08-10 20:26:18 +020021 * Modifications:
22 * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot.
23 * ver 1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors
Sergey Kubushync74b2102007-08-10 20:26:18 +020024 */
25#include <common.h>
26#include <command.h>
27#include <net.h>
28#include <miiphy.h>
Ben Warren84535872009-05-26 00:34:07 -070029#include <malloc.h>
Ilya Yanok2aa87202011-11-28 06:37:33 +000030#include <linux/compiler.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020031#include <asm/arch/emac_defs.h>
Nick Thompsond7e35432009-12-18 13:33:07 +000032#include <asm/io.h>
Ilya Yanok7c587d32011-11-28 06:37:29 +000033#include "davinci_emac.h"
Sergey Kubushync74b2102007-08-10 20:26:18 +020034
Sergey Kubushync74b2102007-08-10 20:26:18 +020035unsigned int emac_dbg = 0;
36#define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)
37
Ilya Yanok82b77212011-11-28 06:37:30 +000038#ifdef EMAC_HW_RAM_ADDR
39static inline unsigned long BD_TO_HW(unsigned long x)
40{
41 if (x == 0)
42 return 0;
43
44 return x - EMAC_WRAPPER_RAM_ADDR + EMAC_HW_RAM_ADDR;
45}
46
47static inline unsigned long HW_TO_BD(unsigned long x)
48{
49 if (x == 0)
50 return 0;
51
52 return x - EMAC_HW_RAM_ADDR + EMAC_WRAPPER_RAM_ADDR;
53}
54#else
55#define BD_TO_HW(x) (x)
56#define HW_TO_BD(x) (x)
57#endif
58
Nick Thompsond7e35432009-12-18 13:33:07 +000059#ifdef DAVINCI_EMAC_GIG_ENABLE
Manjunath Hadlifb1d6332011-10-13 03:40:55 +000060#define emac_gigabit_enable(phy_addr) davinci_eth_gigabit_enable(phy_addr)
Nick Thompsond7e35432009-12-18 13:33:07 +000061#else
Manjunath Hadlifb1d6332011-10-13 03:40:55 +000062#define emac_gigabit_enable(phy_addr) /* no gigabit to enable */
Nick Thompsond7e35432009-12-18 13:33:07 +000063#endif
64
Heiko Schocher882ecfa2011-11-01 20:00:27 +000065#if !defined(CONFIG_SYS_EMAC_TI_CLKDIV)
66#define CONFIG_SYS_EMAC_TI_CLKDIV ((EMAC_MDIO_BUS_FREQ / \
67 EMAC_MDIO_CLOCK_FREQ) - 1)
68#endif
69
Sandeep Paulrajfcaac582008-08-31 00:39:46 +020070static void davinci_eth_mdio_enable(void);
Sergey Kubushync74b2102007-08-10 20:26:18 +020071
72static int gen_init_phy(int phy_addr);
73static int gen_is_phy_connected(int phy_addr);
74static int gen_get_link_speed(int phy_addr);
75static int gen_auto_negotiate(int phy_addr);
76
Sergey Kubushync74b2102007-08-10 20:26:18 +020077void eth_mdio_enable(void)
78{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +020079 davinci_eth_mdio_enable();
Sergey Kubushync74b2102007-08-10 20:26:18 +020080}
Sergey Kubushync74b2102007-08-10 20:26:18 +020081
Sergey Kubushync74b2102007-08-10 20:26:18 +020082/* EMAC Addresses */
83static volatile emac_regs *adap_emac = (emac_regs *)EMAC_BASE_ADDR;
84static volatile ewrap_regs *adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;
85static volatile mdio_regs *adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;
86
87/* EMAC descriptors */
88static volatile emac_desc *emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);
89static volatile emac_desc *emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
90static volatile emac_desc *emac_rx_active_head = 0;
91static volatile emac_desc *emac_rx_active_tail = 0;
92static int emac_rx_queue_active = 0;
93
94/* Receive packet buffers */
Ilya Yanok2aa87202011-11-28 06:37:33 +000095static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * EMAC_RXBUF_SIZE]
96 __aligned(ARCH_DMA_MINALIGN);
Sergey Kubushync74b2102007-08-10 20:26:18 +020097
Heiko Schocherdc02bad2011-11-15 10:00:04 -050098#ifndef CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT
99#define CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT 3
100#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200101
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000102/* PHY address for a discovered PHY (0xff - not found) */
Heiko Schocherdc02bad2011-11-15 10:00:04 -0500103static u_int8_t active_phy_addr[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000104
105/* number of PHY found active */
106static u_int8_t num_phy;
107
Heiko Schocherdc02bad2011-11-15 10:00:04 -0500108phy_t phy[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
Sergey Kubushync74b2102007-08-10 20:26:18 +0200109
Ilya Yanok2aa87202011-11-28 06:37:33 +0000110static inline void davinci_flush_rx_descs(void)
111{
112 /* flush the whole RX descs area */
113 flush_dcache_range(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE,
114 EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
115}
116
117static inline void davinci_invalidate_rx_descs(void)
118{
119 /* invalidate the whole RX descs area */
120 invalidate_dcache_range(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE,
121 EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
122}
123
124static inline void davinci_flush_desc(emac_desc *desc)
125{
126 flush_dcache_range((unsigned long)desc,
127 (unsigned long)desc + sizeof(*desc));
128}
129
Ben Gardiner7b37a272010-09-23 09:58:43 -0400130static int davinci_eth_set_mac_addr(struct eth_device *dev)
131{
132 unsigned long mac_hi;
133 unsigned long mac_lo;
134
135 /*
136 * Set MAC Addresses & Init multicast Hash to 0 (disable any multicast
137 * receive)
138 * Using channel 0 only - other channels are disabled
139 * */
140 writel(0, &adap_emac->MACINDEX);
141 mac_hi = (dev->enetaddr[3] << 24) |
142 (dev->enetaddr[2] << 16) |
143 (dev->enetaddr[1] << 8) |
144 (dev->enetaddr[0]);
145 mac_lo = (dev->enetaddr[5] << 8) |
146 (dev->enetaddr[4]);
147
148 writel(mac_hi, &adap_emac->MACADDRHI);
149#if defined(DAVINCI_EMAC_VERSION2)
150 writel(mac_lo | EMAC_MAC_ADDR_IS_VALID | EMAC_MAC_ADDR_MATCH,
151 &adap_emac->MACADDRLO);
152#else
153 writel(mac_lo, &adap_emac->MACADDRLO);
154#endif
155
156 writel(0, &adap_emac->MACHASH1);
157 writel(0, &adap_emac->MACHASH2);
158
159 /* Set source MAC address - REQUIRED */
160 writel(mac_hi, &adap_emac->MACSRCADDRHI);
161 writel(mac_lo, &adap_emac->MACSRCADDRLO);
162
163
164 return 0;
165}
166
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200167static void davinci_eth_mdio_enable(void)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200168{
169 u_int32_t clkdiv;
170
Heiko Schocher882ecfa2011-11-01 20:00:27 +0000171 clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200172
Nick Thompsond7e35432009-12-18 13:33:07 +0000173 writel((clkdiv & 0xff) |
174 MDIO_CONTROL_ENABLE |
175 MDIO_CONTROL_FAULT |
176 MDIO_CONTROL_FAULT_ENABLE,
177 &adap_mdio->CONTROL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200178
Nick Thompsond7e35432009-12-18 13:33:07 +0000179 while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
180 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200181}
182
183/*
184 * Tries to find an active connected PHY. Returns 1 if address if found.
185 * If no active PHY (or more than one PHY) found returns 0.
186 * Sets active_phy_addr variable.
187 */
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200188static int davinci_eth_phy_detect(void)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200189{
190 u_int32_t phy_act_state;
191 int i;
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000192 int j;
193 unsigned int count = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200194
Heiko Schocherdc02bad2011-11-15 10:00:04 -0500195 for (i = 0; i < CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT; i++)
196 active_phy_addr[i] = 0xff;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200197
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000198 udelay(1000);
199 phy_act_state = readl(&adap_mdio->ALIVE);
200
Nick Thompsond7e35432009-12-18 13:33:07 +0000201 if (phy_act_state == 0)
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000202 return 0; /* No active PHYs */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200203
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200204 debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200205
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000206 for (i = 0, j = 0; i < 32; i++)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200207 if (phy_act_state & (1 << i)) {
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000208 count++;
Prabhakar Ladb6090092011-11-17 02:53:23 +0000209 if (count <= CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT) {
Heiko Schocherdc02bad2011-11-15 10:00:04 -0500210 active_phy_addr[j++] = i;
211 } else {
212 printf("%s: to many PHYs detected.\n",
213 __func__);
214 count = 0;
215 break;
216 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200217 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200218
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000219 num_phy = count;
220
221 return count;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200222}
223
224
225/* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200226int 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 +0200227{
228 int tmp;
229
Nick Thompsond7e35432009-12-18 13:33:07 +0000230 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
231 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200232
Nick Thompsond7e35432009-12-18 13:33:07 +0000233 writel(MDIO_USERACCESS0_GO |
234 MDIO_USERACCESS0_WRITE_READ |
235 ((reg_num & 0x1f) << 21) |
236 ((phy_addr & 0x1f) << 16),
237 &adap_mdio->USERACCESS0);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200238
239 /* Wait for command to complete */
Nick Thompsond7e35432009-12-18 13:33:07 +0000240 while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
241 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200242
243 if (tmp & MDIO_USERACCESS0_ACK) {
244 *data = tmp & 0xffff;
245 return(1);
246 }
247
248 *data = -1;
249 return(0);
250}
251
252/* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200253int 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 +0200254{
255
Nick Thompsond7e35432009-12-18 13:33:07 +0000256 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
257 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200258
Nick Thompsond7e35432009-12-18 13:33:07 +0000259 writel(MDIO_USERACCESS0_GO |
260 MDIO_USERACCESS0_WRITE_WRITE |
261 ((reg_num & 0x1f) << 21) |
262 ((phy_addr & 0x1f) << 16) |
263 (data & 0xffff),
264 &adap_mdio->USERACCESS0);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200265
266 /* Wait for command to complete */
Nick Thompsond7e35432009-12-18 13:33:07 +0000267 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
268 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200269
270 return(1);
271}
272
273/* PHY functions for a generic PHY */
274static int gen_init_phy(int phy_addr)
275{
276 int ret = 1;
277
278 if (gen_get_link_speed(phy_addr)) {
279 /* Try another time */
280 ret = gen_get_link_speed(phy_addr);
281 }
282
283 return(ret);
284}
285
286static int gen_is_phy_connected(int phy_addr)
287{
288 u_int16_t dummy;
289
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000290 return davinci_eth_phy_read(phy_addr, MII_PHYSID1, &dummy);
291}
292
293static int get_active_phy(void)
294{
295 int i;
296
297 for (i = 0; i < num_phy; i++)
298 if (phy[i].get_link_speed(active_phy_addr[i]))
299 return i;
300
301 return -1; /* Return error if no link */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200302}
303
304static int gen_get_link_speed(int phy_addr)
305{
306 u_int16_t tmp;
307
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500308 if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) &&
309 (tmp & 0x04)) {
310#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
311 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
Ben Gardiner7d2fade2011-01-11 14:48:17 -0500312 davinci_eth_phy_read(phy_addr, MII_LPA, &tmp);
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500313
314 /* Speed doesn't matter, there is no setting for it in EMAC. */
Ben Gardiner7d2fade2011-01-11 14:48:17 -0500315 if (tmp & (LPA_100FULL | LPA_10FULL)) {
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500316 /* set EMAC for Full Duplex */
317 writel(EMAC_MACCONTROL_MIIEN_ENABLE |
318 EMAC_MACCONTROL_FULLDUPLEX_ENABLE,
319 &adap_emac->MACCONTROL);
320 } else {
321 /*set EMAC for Half Duplex */
322 writel(EMAC_MACCONTROL_MIIEN_ENABLE,
323 &adap_emac->MACCONTROL);
324 }
325
Ben Gardiner7d2fade2011-01-11 14:48:17 -0500326 if (tmp & (LPA_100FULL | LPA_100HALF))
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500327 writel(readl(&adap_emac->MACCONTROL) |
328 EMAC_MACCONTROL_RMIISPEED_100,
329 &adap_emac->MACCONTROL);
330 else
331 writel(readl(&adap_emac->MACCONTROL) &
332 ~EMAC_MACCONTROL_RMIISPEED_100,
333 &adap_emac->MACCONTROL);
334#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200335 return(1);
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500336 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200337
338 return(0);
339}
340
341static int gen_auto_negotiate(int phy_addr)
342{
343 u_int16_t tmp;
Manjunath Hadlicc4bd472011-10-13 03:40:53 +0000344 u_int16_t val;
345 unsigned long cntr = 0;
346
347 if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
348 return 0;
349
350 val = tmp | BMCR_FULLDPLX | BMCR_ANENABLE |
351 BMCR_SPEED100;
352 davinci_eth_phy_write(phy_addr, MII_BMCR, val);
353
354 if (!davinci_eth_phy_read(phy_addr, MII_ADVERTISE, &val))
355 return 0;
356
357 val |= (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL |
358 ADVERTISE_10HALF);
359 davinci_eth_phy_write(phy_addr, MII_ADVERTISE, val);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200360
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500361 if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200362 return(0);
363
364 /* Restart Auto_negotiation */
Manjunath Hadlicc4bd472011-10-13 03:40:53 +0000365 tmp |= BMCR_ANRESTART;
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500366 davinci_eth_phy_write(phy_addr, MII_BMCR, tmp);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200367
368 /*check AutoNegotiate complete */
Manjunath Hadlicc4bd472011-10-13 03:40:53 +0000369 do {
370 udelay(40000);
371 if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
372 return 0;
373
374 if (tmp & BMSR_ANEGCOMPLETE)
375 break;
376
377 cntr++;
378 } while (cntr < 200);
379
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500380 if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200381 return(0);
382
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500383 if (!(tmp & BMSR_ANEGCOMPLETE))
Sergey Kubushync74b2102007-08-10 20:26:18 +0200384 return(0);
385
386 return(gen_get_link_speed(phy_addr));
387}
388/* End of generic PHY functions */
389
390
Wolfgang Denkafaac862007-08-12 14:27:39 +0200391#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Mike Frysinger5700bb62010-07-27 18:35:08 -0400392static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200393{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200394 return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200395}
396
Mike Frysinger5700bb62010-07-27 18:35:08 -0400397static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200398{
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200399 return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200400}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200401#endif
402
Manjunath Hadlifb1d6332011-10-13 03:40:55 +0000403static void __attribute__((unused)) davinci_eth_gigabit_enable(int phy_addr)
Nick Thompsond7e35432009-12-18 13:33:07 +0000404{
405 u_int16_t data;
406
Manjunath Hadlifb1d6332011-10-13 03:40:55 +0000407 if (davinci_eth_phy_read(phy_addr, 0, &data)) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000408 if (data & (1 << 6)) { /* speed selection MSB */
409 /*
410 * Check if link detected is giga-bit
411 * If Gigabit mode detected, enable gigbit in MAC
412 */
Sandeep Paulraj4b9b9e72010-12-28 14:37:33 -0500413 writel(readl(&adap_emac->MACCONTROL) |
414 EMAC_MACCONTROL_GIGFORCE |
415 EMAC_MACCONTROL_GIGABIT_ENABLE,
416 &adap_emac->MACCONTROL);
Nick Thompsond7e35432009-12-18 13:33:07 +0000417 }
418 }
419}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200420
421/* Eth device open */
Ben Warren84535872009-05-26 00:34:07 -0700422static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200423{
424 dv_reg_p addr;
425 u_int32_t clkdiv, cnt;
426 volatile emac_desc *rx_desc;
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000427 int index;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200428
429 debug_emac("+ emac_open\n");
430
431 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond7e35432009-12-18 13:33:07 +0000432 writel(1, &adap_emac->SOFTRESET);
433 while (readl(&adap_emac->SOFTRESET) != 0)
434 ;
435#if defined(DAVINCI_EMAC_VERSION2)
436 writel(1, &adap_ewrap->softrst);
437 while (readl(&adap_ewrap->softrst) != 0)
438 ;
439#else
440 writel(0, &adap_ewrap->EWCTL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200441 for (cnt = 0; cnt < 5; cnt++) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000442 clkdiv = readl(&adap_ewrap->EWCTL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200443 }
Nick Thompsond7e35432009-12-18 13:33:07 +0000444#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200445
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500446#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
447 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
448 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
449 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
450 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
451#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200452 rx_desc = emac_rx_desc;
453
Nick Thompsond7e35432009-12-18 13:33:07 +0000454 writel(1, &adap_emac->TXCONTROL);
455 writel(1, &adap_emac->RXCONTROL);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200456
Ben Gardiner7b37a272010-09-23 09:58:43 -0400457 davinci_eth_set_mac_addr(dev);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200458
459 /* Set DMA 8 TX / 8 RX Head pointers to 0 */
460 addr = &adap_emac->TX0HDP;
461 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000462 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200463
464 addr = &adap_emac->RX0HDP;
465 for(cnt = 0; cnt < 16; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000466 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200467
468 /* Clear Statistics (do this before setting MacControl register) */
469 addr = &adap_emac->RXGOODFRAMES;
470 for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
Nick Thompsond7e35432009-12-18 13:33:07 +0000471 writel(0, addr++);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200472
473 /* No multicast addressing */
Nick Thompsond7e35432009-12-18 13:33:07 +0000474 writel(0, &adap_emac->MACHASH1);
475 writel(0, &adap_emac->MACHASH2);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200476
477 /* Create RX queue and set receive process in place */
478 emac_rx_active_head = emac_rx_desc;
479 for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
Ilya Yanok82b77212011-11-28 06:37:30 +0000480 rx_desc->next = BD_TO_HW((u_int32_t)(rx_desc + 1));
Ilya Yanok2aa87202011-11-28 06:37:33 +0000481 rx_desc->buffer = &emac_rx_buffers[cnt * EMAC_RXBUF_SIZE];
Sergey Kubushync74b2102007-08-10 20:26:18 +0200482 rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
483 rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
484 rx_desc++;
485 }
486
Nick Thompsond7e35432009-12-18 13:33:07 +0000487 /* Finalize the rx desc list */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200488 rx_desc--;
489 rx_desc->next = 0;
490 emac_rx_active_tail = rx_desc;
491 emac_rx_queue_active = 1;
492
Ilya Yanok2aa87202011-11-28 06:37:33 +0000493 davinci_flush_rx_descs();
494
Sergey Kubushync74b2102007-08-10 20:26:18 +0200495 /* Enable TX/RX */
Nick Thompsond7e35432009-12-18 13:33:07 +0000496 writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
497 writel(0, &adap_emac->RXBUFFEROFFSET);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200498
Nick Thompsond7e35432009-12-18 13:33:07 +0000499 /*
500 * No fancy configs - Use this for promiscous debug
501 * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
502 */
503 writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200504
505 /* Enable ch 0 only */
Nick Thompsond7e35432009-12-18 13:33:07 +0000506 writel(1, &adap_emac->RXUNICASTSET);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200507
508 /* Enable MII interface and Full duplex mode */
Ilya Yanok80deda52011-11-28 06:37:34 +0000509#if defined(CONFIG_SOC_DA8XX) || \
510 (defined(CONFIG_OMAP34XX) && defined(CONFIG_DRIVER_TI_EMAC_USE_RMII))
Nick Thompsond7e35432009-12-18 13:33:07 +0000511 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
512 EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
513 EMAC_MACCONTROL_RMIISPEED_100),
514 &adap_emac->MACCONTROL);
515#else
516 writel((EMAC_MACCONTROL_MIIEN_ENABLE |
517 EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
518 &adap_emac->MACCONTROL);
519#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200520
521 /* Init MDIO & get link state */
Heiko Schocher882ecfa2011-11-01 20:00:27 +0000522 clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
Nick Thompsond7e35432009-12-18 13:33:07 +0000523 writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
524 &adap_mdio->CONTROL);
525
526 /* We need to wait for MDIO to start */
527 udelay(1000);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200528
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000529 index = get_active_phy();
530 if (index == -1)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200531 return(0);
532
Manjunath Hadlifb1d6332011-10-13 03:40:55 +0000533 emac_gigabit_enable(active_phy_addr[index]);
Nick Thompsond7e35432009-12-18 13:33:07 +0000534
Sergey Kubushync74b2102007-08-10 20:26:18 +0200535 /* Start receive process */
Ilya Yanok82b77212011-11-28 06:37:30 +0000536 writel(BD_TO_HW((u_int32_t)emac_rx_desc), &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200537
538 debug_emac("- emac_open\n");
539
540 return(1);
541}
542
543/* EMAC Channel Teardown */
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200544static void davinci_eth_ch_teardown(int ch)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200545{
546 dv_reg dly = 0xff;
547 dv_reg cnt;
548
549 debug_emac("+ emac_ch_teardown\n");
550
551 if (ch == EMAC_CH_TX) {
552 /* Init TX channel teardown */
Nagabhushana Netagunteba511f72011-09-03 22:20:33 -0400553 writel(0, &adap_emac->TXTEARDOWN);
Nick Thompsond7e35432009-12-18 13:33:07 +0000554 do {
555 /*
556 * Wait here for Tx teardown completion interrupt to
557 * occur. Note: A task delay can be called here to pend
558 * rather than occupying CPU cycles - anyway it has
559 * been found that teardown takes very few cpu cycles
560 * and does not affect functionality
561 */
562 dly--;
563 udelay(1);
564 if (dly == 0)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200565 break;
Nick Thompsond7e35432009-12-18 13:33:07 +0000566 cnt = readl(&adap_emac->TX0CP);
567 } while (cnt != 0xfffffffc);
568 writel(cnt, &adap_emac->TX0CP);
569 writel(0, &adap_emac->TX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200570 } else {
571 /* Init RX channel teardown */
Nagabhushana Netagunteba511f72011-09-03 22:20:33 -0400572 writel(0, &adap_emac->RXTEARDOWN);
Nick Thompsond7e35432009-12-18 13:33:07 +0000573 do {
574 /*
575 * Wait here for Rx teardown completion interrupt to
576 * occur. Note: A task delay can be called here to pend
577 * rather than occupying CPU cycles - anyway it has
578 * been found that teardown takes very few cpu cycles
579 * and does not affect functionality
580 */
581 dly--;
582 udelay(1);
583 if (dly == 0)
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200584 break;
Nick Thompsond7e35432009-12-18 13:33:07 +0000585 cnt = readl(&adap_emac->RX0CP);
586 } while (cnt != 0xfffffffc);
587 writel(cnt, &adap_emac->RX0CP);
588 writel(0, &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200589 }
590
591 debug_emac("- emac_ch_teardown\n");
592}
593
594/* Eth device close */
Ben Warren84535872009-05-26 00:34:07 -0700595static void davinci_eth_close(struct eth_device *dev)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200596{
597 debug_emac("+ emac_close\n");
598
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200599 davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
600 davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200601
602 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond7e35432009-12-18 13:33:07 +0000603 writel(1, &adap_emac->SOFTRESET);
604#if defined(DAVINCI_EMAC_VERSION2)
605 writel(1, &adap_ewrap->softrst);
606#else
607 writel(0, &adap_ewrap->EWCTL);
608#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200609
Sudhakar Rajashekharad2607402010-11-18 09:59:37 -0500610#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
611 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
612 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
613 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
614 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
615#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200616 debug_emac("- emac_close\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200617}
618
619static int tx_send_loop = 0;
620
621/*
622 * This function sends a single packet on the network and returns
623 * positive number (number of bytes transmitted) or negative for error
624 */
Ben Warren84535872009-05-26 00:34:07 -0700625static int davinci_eth_send_packet (struct eth_device *dev,
Joe Hershbergerbbcdefb2012-05-21 05:54:01 +0000626 void *packet, int length)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200627{
628 int ret_status = -1;
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000629 int index;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200630 tx_send_loop = 0;
631
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000632 index = get_active_phy();
633 if (index == -1) {
634 printf(" WARN: emac_send_packet: No link\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200635 return (ret_status);
636 }
637
Manjunath Hadlifb1d6332011-10-13 03:40:55 +0000638 emac_gigabit_enable(active_phy_addr[index]);
Nick Thompsond7e35432009-12-18 13:33:07 +0000639
Sergey Kubushync74b2102007-08-10 20:26:18 +0200640 /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200641 if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
Sergey Kubushync74b2102007-08-10 20:26:18 +0200642 length = EMAC_MIN_ETHERNET_PKT_SIZE;
643 }
644
645 /* Populate the TX descriptor */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200646 emac_tx_desc->next = 0;
647 emac_tx_desc->buffer = (u_int8_t *) packet;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200648 emac_tx_desc->buff_off_len = (length & 0xffff);
649 emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200650 EMAC_CPPI_SOP_BIT |
651 EMAC_CPPI_OWNERSHIP_BIT |
652 EMAC_CPPI_EOP_BIT);
Ilya Yanok2aa87202011-11-28 06:37:33 +0000653
654 flush_dcache_range((unsigned long)packet,
655 (unsigned long)packet + length);
656 davinci_flush_desc(emac_tx_desc);
657
Sergey Kubushync74b2102007-08-10 20:26:18 +0200658 /* Send the packet */
Ilya Yanok82b77212011-11-28 06:37:30 +0000659 writel(BD_TO_HW((unsigned long)emac_tx_desc), &adap_emac->TX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200660
661 /* Wait for packet to complete or link down */
662 while (1) {
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000663 if (!phy[index].get_link_speed(active_phy_addr[index])) {
Sandeep Paulrajfcaac582008-08-31 00:39:46 +0200664 davinci_eth_ch_teardown (EMAC_CH_TX);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200665 return (ret_status);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200666 }
Nick Thompsond7e35432009-12-18 13:33:07 +0000667
Manjunath Hadlifb1d6332011-10-13 03:40:55 +0000668 emac_gigabit_enable(active_phy_addr[index]);
Nick Thompsond7e35432009-12-18 13:33:07 +0000669
670 if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200671 ret_status = length;
672 break;
673 }
674 tx_send_loop++;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200675 }
676
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200677 return (ret_status);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200678}
679
680/*
681 * This function handles receipt of a packet from the network
682 */
Ben Warren84535872009-05-26 00:34:07 -0700683static int davinci_eth_rcv_packet (struct eth_device *dev)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200684{
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200685 volatile emac_desc *rx_curr_desc;
686 volatile emac_desc *curr_desc;
687 volatile emac_desc *tail_desc;
688 int status, ret = -1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200689
Ilya Yanok2aa87202011-11-28 06:37:33 +0000690 davinci_invalidate_rx_descs();
691
Sergey Kubushync74b2102007-08-10 20:26:18 +0200692 rx_curr_desc = emac_rx_active_head;
693 status = rx_curr_desc->pkt_flag_len;
694 if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200695 if (status & EMAC_CPPI_RX_ERROR_FRAME) {
696 /* Error in packet - discard it and requeue desc */
697 printf ("WARN: emac_rcv_pkt: Error in packet\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200698 } else {
Ilya Yanok2aa87202011-11-28 06:37:33 +0000699 unsigned long tmp = (unsigned long)rx_curr_desc->buffer;
700
701 invalidate_dcache_range(tmp, tmp + EMAC_RXBUF_SIZE);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200702 NetReceive (rx_curr_desc->buffer,
703 (rx_curr_desc->buff_off_len & 0xffff));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200704 ret = rx_curr_desc->buff_off_len & 0xffff;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200705 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200706
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200707 /* Ack received packet descriptor */
Ilya Yanok82b77212011-11-28 06:37:30 +0000708 writel(BD_TO_HW((ulong)rx_curr_desc), &adap_emac->RX0CP);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200709 curr_desc = rx_curr_desc;
710 emac_rx_active_head =
Ilya Yanok82b77212011-11-28 06:37:30 +0000711 (volatile emac_desc *) (HW_TO_BD(rx_curr_desc->next));
Sergey Kubushync74b2102007-08-10 20:26:18 +0200712
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200713 if (status & EMAC_CPPI_EOQ_BIT) {
714 if (emac_rx_active_head) {
Ilya Yanok82b77212011-11-28 06:37:30 +0000715 writel(BD_TO_HW((ulong)emac_rx_active_head),
Nick Thompsond7e35432009-12-18 13:33:07 +0000716 &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200717 } else {
718 emac_rx_queue_active = 0;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200719 printf ("INFO:emac_rcv_packet: RX Queue not active\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200720 }
721 }
722
723 /* Recycle RX descriptor */
724 rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
725 rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
726 rx_curr_desc->next = 0;
Ilya Yanok2aa87202011-11-28 06:37:33 +0000727 davinci_flush_desc(rx_curr_desc);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200728
729 if (emac_rx_active_head == 0) {
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200730 printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200731 emac_rx_active_head = curr_desc;
732 emac_rx_active_tail = curr_desc;
733 if (emac_rx_queue_active != 0) {
Ilya Yanok82b77212011-11-28 06:37:30 +0000734 writel(BD_TO_HW((ulong)emac_rx_active_head),
Nick Thompsond7e35432009-12-18 13:33:07 +0000735 &adap_emac->RX0HDP);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200736 printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
Sergey Kubushync74b2102007-08-10 20:26:18 +0200737 emac_rx_queue_active = 1;
738 }
739 } else {
740 tail_desc = emac_rx_active_tail;
741 emac_rx_active_tail = curr_desc;
Ilya Yanok82b77212011-11-28 06:37:30 +0000742 tail_desc->next = BD_TO_HW((ulong) curr_desc);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200743 status = tail_desc->pkt_flag_len;
744 if (status & EMAC_CPPI_EOQ_BIT) {
Ilya Yanok2aa87202011-11-28 06:37:33 +0000745 davinci_flush_desc(tail_desc);
Ilya Yanok82b77212011-11-28 06:37:30 +0000746 writel(BD_TO_HW((ulong)curr_desc),
Nick Thompsond7e35432009-12-18 13:33:07 +0000747 &adap_emac->RX0HDP);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200748 status &= ~EMAC_CPPI_EOQ_BIT;
749 tail_desc->pkt_flag_len = status;
750 }
Ilya Yanok2aa87202011-11-28 06:37:33 +0000751 davinci_flush_desc(tail_desc);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200752 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200753 return (ret);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200754 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200755 return (0);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200756}
757
Ben Warren8cc13c12009-04-27 23:19:10 -0700758/*
759 * This function initializes the emac hardware. It does NOT initialize
760 * EMAC modules power or pin multiplexors, that is done by board_init()
761 * much earlier in bootup process. Returns 1 on success, 0 otherwise.
762 */
Ben Warren84535872009-05-26 00:34:07 -0700763int davinci_emac_initialize(void)
Ben Warren8cc13c12009-04-27 23:19:10 -0700764{
765 u_int32_t phy_id;
766 u_int16_t tmp;
767 int i;
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000768 int ret;
Ben Warren84535872009-05-26 00:34:07 -0700769 struct eth_device *dev;
770
771 dev = malloc(sizeof *dev);
772
773 if (dev == NULL)
774 return -1;
775
776 memset(dev, 0, sizeof *dev);
Sandeep Paulraj2a7d6032010-12-28 14:42:27 -0500777 sprintf(dev->name, "DaVinci-EMAC");
Ben Warren84535872009-05-26 00:34:07 -0700778
779 dev->iobase = 0;
780 dev->init = davinci_eth_open;
781 dev->halt = davinci_eth_close;
782 dev->send = davinci_eth_send_packet;
783 dev->recv = davinci_eth_rcv_packet;
Ben Gardiner7b37a272010-09-23 09:58:43 -0400784 dev->write_hwaddr = davinci_eth_set_mac_addr;
Ben Warren84535872009-05-26 00:34:07 -0700785
786 eth_register(dev);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200787
Ben Warren8cc13c12009-04-27 23:19:10 -0700788 davinci_eth_mdio_enable();
789
Heiko Schocher19fdf9a2011-09-14 19:37:42 +0000790 /* let the EMAC detect the PHYs */
791 udelay(5000);
792
Ben Warren8cc13c12009-04-27 23:19:10 -0700793 for (i = 0; i < 256; i++) {
Nick Thompsond7e35432009-12-18 13:33:07 +0000794 if (readl(&adap_mdio->ALIVE))
Ben Warren8cc13c12009-04-27 23:19:10 -0700795 break;
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000796 udelay(1000);
Ben Warren8cc13c12009-04-27 23:19:10 -0700797 }
798
799 if (i >= 256) {
800 printf("No ETH PHY detected!!!\n");
801 return(0);
802 }
803
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000804 /* Find if PHY(s) is/are connected */
805 ret = davinci_eth_phy_detect();
806 if (!ret)
Ben Warren8cc13c12009-04-27 23:19:10 -0700807 return(0);
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000808 else
Heiko Schocherdc02bad2011-11-15 10:00:04 -0500809 debug_emac(" %d ETH PHY detected\n", ret);
Ben Warren8cc13c12009-04-27 23:19:10 -0700810
811 /* Get PHY ID and initialize phy_ops for a detected PHY */
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000812 for (i = 0; i < num_phy; i++) {
813 if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID1,
814 &tmp)) {
815 active_phy_addr[i] = 0xff;
816 continue;
817 }
Ben Warren8cc13c12009-04-27 23:19:10 -0700818
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000819 phy_id = (tmp << 16) & 0xffff0000;
Ben Warren8cc13c12009-04-27 23:19:10 -0700820
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000821 if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID2,
822 &tmp)) {
823 active_phy_addr[i] = 0xff;
824 continue;
825 }
Ben Warren8cc13c12009-04-27 23:19:10 -0700826
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000827 phy_id |= tmp & 0x0000ffff;
Ben Warren8cc13c12009-04-27 23:19:10 -0700828
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000829 switch (phy_id) {
Ilya Yanok918588c2011-11-28 06:37:31 +0000830#ifdef PHY_KSZ8873
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000831 case PHY_KSZ8873:
832 sprintf(phy[i].name, "KSZ8873 @ 0x%02x",
833 active_phy_addr[i]);
834 phy[i].init = ksz8873_init_phy;
835 phy[i].is_phy_connected = ksz8873_is_phy_connected;
836 phy[i].get_link_speed = ksz8873_get_link_speed;
837 phy[i].auto_negotiate = ksz8873_auto_negotiate;
838 break;
Ilya Yanok918588c2011-11-28 06:37:31 +0000839#endif
840#ifdef PHY_LXT972
Ben Warren8cc13c12009-04-27 23:19:10 -0700841 case PHY_LXT972:
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000842 sprintf(phy[i].name, "LXT972 @ 0x%02x",
843 active_phy_addr[i]);
844 phy[i].init = lxt972_init_phy;
845 phy[i].is_phy_connected = lxt972_is_phy_connected;
846 phy[i].get_link_speed = lxt972_get_link_speed;
847 phy[i].auto_negotiate = lxt972_auto_negotiate;
Ben Warren8cc13c12009-04-27 23:19:10 -0700848 break;
Ilya Yanok918588c2011-11-28 06:37:31 +0000849#endif
850#ifdef PHY_DP83848
Ben Warren8cc13c12009-04-27 23:19:10 -0700851 case PHY_DP83848:
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000852 sprintf(phy[i].name, "DP83848 @ 0x%02x",
853 active_phy_addr[i]);
854 phy[i].init = dp83848_init_phy;
855 phy[i].is_phy_connected = dp83848_is_phy_connected;
856 phy[i].get_link_speed = dp83848_get_link_speed;
857 phy[i].auto_negotiate = dp83848_auto_negotiate;
Ben Warren8cc13c12009-04-27 23:19:10 -0700858 break;
Ilya Yanok918588c2011-11-28 06:37:31 +0000859#endif
860#ifdef PHY_ET1011C
Sandeep Paulraj840f8922010-12-28 15:43:16 -0500861 case PHY_ET1011C:
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000862 sprintf(phy[i].name, "ET1011C @ 0x%02x",
863 active_phy_addr[i]);
864 phy[i].init = gen_init_phy;
865 phy[i].is_phy_connected = gen_is_phy_connected;
866 phy[i].get_link_speed = et1011c_get_link_speed;
867 phy[i].auto_negotiate = gen_auto_negotiate;
Sandeep Paulraj840f8922010-12-28 15:43:16 -0500868 break;
Ilya Yanok918588c2011-11-28 06:37:31 +0000869#endif
Ben Warren8cc13c12009-04-27 23:19:10 -0700870 default:
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000871 sprintf(phy[i].name, "GENERIC @ 0x%02x",
872 active_phy_addr[i]);
873 phy[i].init = gen_init_phy;
874 phy[i].is_phy_connected = gen_is_phy_connected;
875 phy[i].get_link_speed = gen_get_link_speed;
876 phy[i].auto_negotiate = gen_auto_negotiate;
877 }
878
Ilya Yanoke0297a52011-11-01 13:15:55 +0000879 debug("Ethernet PHY: %s\n", phy[i].name);
Manjunath Hadli062fe7d2011-10-13 03:40:54 +0000880
881 miiphy_register(phy[i].name, davinci_mii_phy_read,
882 davinci_mii_phy_write);
Ben Warren8cc13c12009-04-27 23:19:10 -0700883 }
Rajashekhara, Sudhakarb78375a2012-06-07 00:27:44 +0000884
885#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
Bastian Ruppertde575502012-09-13 22:29:03 +0000886 defined(CONFIG_MACH_DAVINCI_DA850_EVM) && \
887 !defined(CONFIG_DRIVER_TI_EMAC_RMII_NO_NEGOTIATE)
Rajashekhara, Sudhakarb78375a2012-06-07 00:27:44 +0000888 for (i = 0; i < num_phy; i++) {
889 if (phy[i].is_phy_connected(i))
890 phy[i].auto_negotiate(i);
891 }
892#endif
Ben Warren8cc13c12009-04-27 23:19:10 -0700893 return(1);
894}