blob: 2dfadbd82d5bcaf997e106817f590a3b6eb8b1f6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergey Kubushyne8f39122007-08-10 20:26:18 +02002/*
3 * Ethernet driver for TI TMS320DM644x (DaVinci) chips.
4 *
5 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
6 *
7 * Parts shamelessly stolen from TI's dm644x_emac.c. Original copyright
8 * follows:
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * dm644x_emac.c
13 *
14 * TI DaVinci (DM644X) EMAC peripheral driver source for DV-EVM
15 *
16 * Copyright (C) 2005 Texas Instruments.
17 *
18 * ----------------------------------------------------------------------------
19 *
Sergey Kubushyne8f39122007-08-10 20:26:18 +020020 * Modifications:
21 * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot.
22 * ver 1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors
Sergey Kubushyne8f39122007-08-10 20:26:18 +020023 */
24#include <common.h>
25#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070026#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060027#include <log.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020028#include <net.h>
29#include <miiphy.h>
Ben Warren5301bbf2009-05-26 00:34:07 -070030#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060031#include <asm/cache.h>
Ilya Yanokff672762011-11-28 06:37:33 +000032#include <linux/compiler.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020033#include <asm/arch/emac_defs.h>
Nick Thompsond5ee6f62009-12-18 13:33:07 +000034#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
Ilya Yanok5f732f72011-11-28 06:37:29 +000036#include "davinci_emac.h"
Sergey Kubushyne8f39122007-08-10 20:26:18 +020037
Sergey Kubushyne8f39122007-08-10 20:26:18 +020038unsigned int emac_dbg = 0;
39#define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)
40
Ilya Yanok518036e2011-11-28 06:37:30 +000041#ifdef EMAC_HW_RAM_ADDR
42static inline unsigned long BD_TO_HW(unsigned long x)
43{
44 if (x == 0)
45 return 0;
46
47 return x - EMAC_WRAPPER_RAM_ADDR + EMAC_HW_RAM_ADDR;
48}
49
50static inline unsigned long HW_TO_BD(unsigned long x)
51{
52 if (x == 0)
53 return 0;
54
55 return x - EMAC_HW_RAM_ADDR + EMAC_WRAPPER_RAM_ADDR;
56}
57#else
58#define BD_TO_HW(x) (x)
59#define HW_TO_BD(x) (x)
60#endif
61
Nick Thompsond5ee6f62009-12-18 13:33:07 +000062#ifdef DAVINCI_EMAC_GIG_ENABLE
Manjunath Hadli5b5260e2011-10-13 03:40:55 +000063#define emac_gigabit_enable(phy_addr) davinci_eth_gigabit_enable(phy_addr)
Nick Thompsond5ee6f62009-12-18 13:33:07 +000064#else
Manjunath Hadli5b5260e2011-10-13 03:40:55 +000065#define emac_gigabit_enable(phy_addr) /* no gigabit to enable */
Nick Thompsond5ee6f62009-12-18 13:33:07 +000066#endif
67
Heiko Schocher3e806132011-11-01 20:00:27 +000068#if !defined(CONFIG_SYS_EMAC_TI_CLKDIV)
69#define CONFIG_SYS_EMAC_TI_CLKDIV ((EMAC_MDIO_BUS_FREQ / \
70 EMAC_MDIO_CLOCK_FREQ) - 1)
71#endif
72
Sandeep Paulraj4b26f052008-08-31 00:39:46 +020073static void davinci_eth_mdio_enable(void);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020074
75static int gen_init_phy(int phy_addr);
76static int gen_is_phy_connected(int phy_addr);
77static int gen_get_link_speed(int phy_addr);
78static int gen_auto_negotiate(int phy_addr);
79
Sergey Kubushyne8f39122007-08-10 20:26:18 +020080void eth_mdio_enable(void)
81{
Sandeep Paulraj4b26f052008-08-31 00:39:46 +020082 davinci_eth_mdio_enable();
Sergey Kubushyne8f39122007-08-10 20:26:18 +020083}
Sergey Kubushyne8f39122007-08-10 20:26:18 +020084
Sergey Kubushyne8f39122007-08-10 20:26:18 +020085/* EMAC Addresses */
86static volatile emac_regs *adap_emac = (emac_regs *)EMAC_BASE_ADDR;
87static volatile ewrap_regs *adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;
88static volatile mdio_regs *adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;
89
90/* EMAC descriptors */
91static volatile emac_desc *emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);
92static volatile emac_desc *emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
93static volatile emac_desc *emac_rx_active_head = 0;
94static volatile emac_desc *emac_rx_active_tail = 0;
95static int emac_rx_queue_active = 0;
96
97/* Receive packet buffers */
Ilya Yanokff672762011-11-28 06:37:33 +000098static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * EMAC_RXBUF_SIZE]
99 __aligned(ARCH_DMA_MINALIGN);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200100
Heiko Schocher7d037f72011-11-15 10:00:04 -0500101#ifndef CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT
102#define CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT 3
103#endif
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000104
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200105/* PHY address for a discovered PHY (0xff - not found) */
Heiko Schocher7d037f72011-11-15 10:00:04 -0500106static u_int8_t active_phy_addr[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200107
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000108/* number of PHY found active */
109static u_int8_t num_phy;
110
Heiko Schocher7d037f72011-11-15 10:00:04 -0500111phy_t phy[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200112
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200113static int davinci_emac_write_hwaddr(struct udevice *dev)
Ben Gardiner1fb49e32010-09-23 09:58:43 -0400114{
Simon Glassfa20e932020-12-03 16:55:20 -0700115 struct eth_pdata *pdata = dev_get_plat(dev);
Ben Gardiner1fb49e32010-09-23 09:58:43 -0400116 unsigned long mac_hi;
117 unsigned long mac_lo;
118
119 /*
120 * Set MAC Addresses & Init multicast Hash to 0 (disable any multicast
121 * receive)
122 * Using channel 0 only - other channels are disabled
123 * */
124 writel(0, &adap_emac->MACINDEX);
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200125 mac_hi = (pdata->enetaddr[3] << 24) |
126 (pdata->enetaddr[2] << 16) |
127 (pdata->enetaddr[1] << 8) |
128 (pdata->enetaddr[0]);
129 mac_lo = (pdata->enetaddr[5] << 8) |
130 (pdata->enetaddr[4]);
Ben Gardiner1fb49e32010-09-23 09:58:43 -0400131
132 writel(mac_hi, &adap_emac->MACADDRHI);
133#if defined(DAVINCI_EMAC_VERSION2)
134 writel(mac_lo | EMAC_MAC_ADDR_IS_VALID | EMAC_MAC_ADDR_MATCH,
135 &adap_emac->MACADDRLO);
136#else
137 writel(mac_lo, &adap_emac->MACADDRLO);
138#endif
139
140 writel(0, &adap_emac->MACHASH1);
141 writel(0, &adap_emac->MACHASH2);
142
143 /* Set source MAC address - REQUIRED */
144 writel(mac_hi, &adap_emac->MACSRCADDRHI);
145 writel(mac_lo, &adap_emac->MACSRCADDRLO);
146
147
148 return 0;
149}
150
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200151static void davinci_eth_mdio_enable(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200152{
153 u_int32_t clkdiv;
154
Heiko Schocher3e806132011-11-01 20:00:27 +0000155 clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200156
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000157 writel((clkdiv & 0xff) |
158 MDIO_CONTROL_ENABLE |
159 MDIO_CONTROL_FAULT |
160 MDIO_CONTROL_FAULT_ENABLE,
161 &adap_mdio->CONTROL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200162
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000163 while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
164 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200165}
166
167/*
168 * Tries to find an active connected PHY. Returns 1 if address if found.
169 * If no active PHY (or more than one PHY) found returns 0.
170 * Sets active_phy_addr variable.
171 */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200172static int davinci_eth_phy_detect(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200173{
174 u_int32_t phy_act_state;
175 int i;
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000176 int j;
177 unsigned int count = 0;
178
Heiko Schocher7d037f72011-11-15 10:00:04 -0500179 for (i = 0; i < CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT; i++)
180 active_phy_addr[i] = 0xff;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200181
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000182 udelay(1000);
183 phy_act_state = readl(&adap_mdio->ALIVE);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200184
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000185 if (phy_act_state == 0)
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000186 return 0; /* No active PHYs */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200187
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200188 debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200189
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000190 for (i = 0, j = 0; i < 32; i++)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200191 if (phy_act_state & (1 << i)) {
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000192 count++;
Prabhakar Lad60289fe2011-11-17 02:53:23 +0000193 if (count <= CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT) {
Heiko Schocher7d037f72011-11-15 10:00:04 -0500194 active_phy_addr[j++] = i;
195 } else {
196 printf("%s: to many PHYs detected.\n",
197 __func__);
198 count = 0;
199 break;
200 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200201 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200202
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000203 num_phy = count;
204
205 return count;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200206}
207
208
209/* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200210int 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 +0200211{
212 int tmp;
213
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000214 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
215 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200216
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000217 writel(MDIO_USERACCESS0_GO |
218 MDIO_USERACCESS0_WRITE_READ |
219 ((reg_num & 0x1f) << 21) |
220 ((phy_addr & 0x1f) << 16),
221 &adap_mdio->USERACCESS0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200222
223 /* Wait for command to complete */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000224 while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
225 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200226
227 if (tmp & MDIO_USERACCESS0_ACK) {
228 *data = tmp & 0xffff;
karl beldan05d06982016-08-20 08:56:53 +0000229 return 1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200230 }
231
karl beldan05d06982016-08-20 08:56:53 +0000232 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200233}
234
235/* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200236int 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 +0200237{
238
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000239 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
240 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200241
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000242 writel(MDIO_USERACCESS0_GO |
243 MDIO_USERACCESS0_WRITE_WRITE |
244 ((reg_num & 0x1f) << 21) |
245 ((phy_addr & 0x1f) << 16) |
246 (data & 0xffff),
247 &adap_mdio->USERACCESS0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200248
249 /* Wait for command to complete */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000250 while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
251 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200252
karl beldan05d06982016-08-20 08:56:53 +0000253 return 1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200254}
255
256/* PHY functions for a generic PHY */
257static int gen_init_phy(int phy_addr)
258{
259 int ret = 1;
260
261 if (gen_get_link_speed(phy_addr)) {
262 /* Try another time */
263 ret = gen_get_link_speed(phy_addr);
264 }
265
266 return(ret);
267}
268
269static int gen_is_phy_connected(int phy_addr)
270{
271 u_int16_t dummy;
272
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000273 return davinci_eth_phy_read(phy_addr, MII_PHYSID1, &dummy);
274}
275
276static int get_active_phy(void)
277{
278 int i;
279
280 for (i = 0; i < num_phy; i++)
281 if (phy[i].get_link_speed(active_phy_addr[i]))
282 return i;
283
284 return -1; /* Return error if no link */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200285}
286
287static int gen_get_link_speed(int phy_addr)
288{
289 u_int16_t tmp;
290
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500291 if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) &&
292 (tmp & 0x04)) {
293#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
294 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
Ben Gardinerb936eaa2011-01-11 14:48:17 -0500295 davinci_eth_phy_read(phy_addr, MII_LPA, &tmp);
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500296
297 /* Speed doesn't matter, there is no setting for it in EMAC. */
Ben Gardinerb936eaa2011-01-11 14:48:17 -0500298 if (tmp & (LPA_100FULL | LPA_10FULL)) {
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500299 /* set EMAC for Full Duplex */
300 writel(EMAC_MACCONTROL_MIIEN_ENABLE |
301 EMAC_MACCONTROL_FULLDUPLEX_ENABLE,
302 &adap_emac->MACCONTROL);
303 } else {
304 /*set EMAC for Half Duplex */
305 writel(EMAC_MACCONTROL_MIIEN_ENABLE,
306 &adap_emac->MACCONTROL);
307 }
308
Ben Gardinerb936eaa2011-01-11 14:48:17 -0500309 if (tmp & (LPA_100FULL | LPA_100HALF))
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500310 writel(readl(&adap_emac->MACCONTROL) |
311 EMAC_MACCONTROL_RMIISPEED_100,
312 &adap_emac->MACCONTROL);
313 else
314 writel(readl(&adap_emac->MACCONTROL) &
315 ~EMAC_MACCONTROL_RMIISPEED_100,
316 &adap_emac->MACCONTROL);
317#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200318 return(1);
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500319 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200320
321 return(0);
322}
323
324static int gen_auto_negotiate(int phy_addr)
325{
326 u_int16_t tmp;
Manjunath Hadli4141ad42011-10-13 03:40:53 +0000327 u_int16_t val;
328 unsigned long cntr = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200329
Mike Frysingerd63ee712010-12-23 15:40:12 -0500330 if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
Manjunath Hadli4141ad42011-10-13 03:40:53 +0000331 return 0;
332
333 val = tmp | BMCR_FULLDPLX | BMCR_ANENABLE |
334 BMCR_SPEED100;
335 davinci_eth_phy_write(phy_addr, MII_BMCR, val);
336
337 if (!davinci_eth_phy_read(phy_addr, MII_ADVERTISE, &val))
338 return 0;
339
340 val |= (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL |
341 ADVERTISE_10HALF);
342 davinci_eth_phy_write(phy_addr, MII_ADVERTISE, val);
343
344 if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200345 return(0);
346
Tom Rinic3cf8992017-05-10 12:01:02 -0400347#ifdef DAVINCI_EMAC_GIG_ENABLE
348 davinci_eth_phy_read(phy_addr, MII_CTRL1000, &val);
349 val |= PHY_1000BTCR_1000FD;
350 val &= ~PHY_1000BTCR_1000HD;
351 davinci_eth_phy_write(phy_addr, MII_CTRL1000, val);
352 davinci_eth_phy_read(phy_addr, MII_CTRL1000, &val);
353#endif
354
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200355 /* Restart Auto_negotiation */
Manjunath Hadli4141ad42011-10-13 03:40:53 +0000356 tmp |= BMCR_ANRESTART;
Mike Frysingerd63ee712010-12-23 15:40:12 -0500357 davinci_eth_phy_write(phy_addr, MII_BMCR, tmp);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200358
359 /*check AutoNegotiate complete */
Manjunath Hadli4141ad42011-10-13 03:40:53 +0000360 do {
361 udelay(40000);
362 if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
363 return 0;
364
365 if (tmp & BMSR_ANEGCOMPLETE)
366 break;
367
368 cntr++;
369 } while (cntr < 200);
370
Mike Frysingerd63ee712010-12-23 15:40:12 -0500371 if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200372 return(0);
373
Mike Frysingerd63ee712010-12-23 15:40:12 -0500374 if (!(tmp & BMSR_ANEGCOMPLETE))
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200375 return(0);
376
377 return(gen_get_link_speed(phy_addr));
378}
379/* End of generic PHY functions */
380
381
Wolfgang Denk56cbd022007-08-12 14:27:39 +0200382#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500383static int davinci_mii_phy_read(struct mii_dev *bus, int addr, int devad,
384 int reg)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200385{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500386 unsigned short value = 0;
Joe Hershbergerece1aa62016-08-08 11:28:40 -0500387 int retval = davinci_eth_phy_read(addr, reg, &value);
karl beldan05d06982016-08-20 08:56:53 +0000388
389 return retval ? value : -EIO;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200390}
391
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500392static int davinci_mii_phy_write(struct mii_dev *bus, int addr, int devad,
393 int reg, u16 value)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200394{
karl beldan05d06982016-08-20 08:56:53 +0000395 return davinci_eth_phy_write(addr, reg, value) ? 0 : 1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200396}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200397#endif
398
Manjunath Hadli5b5260e2011-10-13 03:40:55 +0000399static void __attribute__((unused)) davinci_eth_gigabit_enable(int phy_addr)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000400{
401 u_int16_t data;
402
Manjunath Hadli5b5260e2011-10-13 03:40:55 +0000403 if (davinci_eth_phy_read(phy_addr, 0, &data)) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000404 if (data & (1 << 6)) { /* speed selection MSB */
405 /*
406 * Check if link detected is giga-bit
407 * If Gigabit mode detected, enable gigbit in MAC
408 */
Sandeep Paulraj9da994b2010-12-28 14:37:33 -0500409 writel(readl(&adap_emac->MACCONTROL) |
410 EMAC_MACCONTROL_GIGFORCE |
411 EMAC_MACCONTROL_GIGABIT_ENABLE,
412 &adap_emac->MACCONTROL);
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000413 }
414 }
415}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200416
417/* Eth device open */
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200418static int davinci_emac_start(struct udevice *dev)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200419{
420 dv_reg_p addr;
Tom Rinic3cf8992017-05-10 12:01:02 -0400421 u_int32_t clkdiv, cnt, mac_control;
422 uint16_t __maybe_unused lpa_val;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200423 volatile emac_desc *rx_desc;
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000424 int index;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200425
426 debug_emac("+ emac_open\n");
427
428 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000429 writel(1, &adap_emac->SOFTRESET);
430 while (readl(&adap_emac->SOFTRESET) != 0)
431 ;
432#if defined(DAVINCI_EMAC_VERSION2)
433 writel(1, &adap_ewrap->softrst);
434 while (readl(&adap_ewrap->softrst) != 0)
435 ;
436#else
437 writel(0, &adap_ewrap->EWCTL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200438 for (cnt = 0; cnt < 5; cnt++) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000439 clkdiv = readl(&adap_ewrap->EWCTL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200440 }
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000441#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200442
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500443#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
444 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
445 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
446 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
447 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
448#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200449 rx_desc = emac_rx_desc;
450
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000451 writel(1, &adap_emac->TXCONTROL);
452 writel(1, &adap_emac->RXCONTROL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200453
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200454 davinci_emac_write_hwaddr(dev);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200455
456 /* Set DMA 8 TX / 8 RX Head pointers to 0 */
457 addr = &adap_emac->TX0HDP;
Vishwas Srivastava8aaeb1f2016-01-25 21:28:17 +0530458 for (cnt = 0; cnt < 8; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000459 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200460
461 addr = &adap_emac->RX0HDP;
Vishwas Srivastava8aaeb1f2016-01-25 21:28:17 +0530462 for (cnt = 0; cnt < 8; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000463 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200464
465 /* Clear Statistics (do this before setting MacControl register) */
466 addr = &adap_emac->RXGOODFRAMES;
467 for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000468 writel(0, addr++);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200469
470 /* No multicast addressing */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000471 writel(0, &adap_emac->MACHASH1);
472 writel(0, &adap_emac->MACHASH2);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200473
474 /* Create RX queue and set receive process in place */
475 emac_rx_active_head = emac_rx_desc;
476 for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
Ilya Yanok518036e2011-11-28 06:37:30 +0000477 rx_desc->next = BD_TO_HW((u_int32_t)(rx_desc + 1));
Ilya Yanokff672762011-11-28 06:37:33 +0000478 rx_desc->buffer = &emac_rx_buffers[cnt * EMAC_RXBUF_SIZE];
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200479 rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
480 rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
481 rx_desc++;
482 }
483
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000484 /* Finalize the rx desc list */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200485 rx_desc--;
486 rx_desc->next = 0;
487 emac_rx_active_tail = rx_desc;
488 emac_rx_queue_active = 1;
489
490 /* Enable TX/RX */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000491 writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
492 writel(0, &adap_emac->RXBUFFEROFFSET);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200493
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000494 /*
495 * No fancy configs - Use this for promiscous debug
496 * - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
497 */
498 writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200499
500 /* Enable ch 0 only */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000501 writel(1, &adap_emac->RXUNICASTSET);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200502
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200503 /* Init MDIO & get link state */
Heiko Schocher3e806132011-11-01 20:00:27 +0000504 clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000505 writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
506 &adap_mdio->CONTROL);
507
508 /* We need to wait for MDIO to start */
509 udelay(1000);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200510
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000511 index = get_active_phy();
512 if (index == -1)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200513 return(0);
514
Tom Rinic3cf8992017-05-10 12:01:02 -0400515 /* Enable MII interface */
516 mac_control = EMAC_MACCONTROL_MIIEN_ENABLE;
517#ifdef DAVINCI_EMAC_GIG_ENABLE
518 davinci_eth_phy_read(active_phy_addr[index], MII_STAT1000, &lpa_val);
519 if (lpa_val & PHY_1000BTSR_1000FD) {
520 debug_emac("eth_open : gigabit negotiated\n");
521 mac_control |= EMAC_MACCONTROL_FULLDUPLEX_ENABLE;
522 mac_control |= EMAC_MACCONTROL_GIGABIT_ENABLE;
523 }
524#endif
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000525
Tom Rinic3cf8992017-05-10 12:01:02 -0400526 davinci_eth_phy_read(active_phy_addr[index], MII_LPA, &lpa_val);
527 if (lpa_val & (LPA_100FULL | LPA_10FULL))
528 /* set EMAC for Full Duplex */
529 mac_control |= EMAC_MACCONTROL_FULLDUPLEX_ENABLE;
530#if defined(CONFIG_SOC_DA8XX) || \
531 (defined(CONFIG_OMAP34XX) && defined(CONFIG_DRIVER_TI_EMAC_USE_RMII))
532 mac_control |= EMAC_MACCONTROL_RMIISPEED_100;
533#endif
534 writel(mac_control, &adap_emac->MACCONTROL);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200535 /* Start receive process */
Ilya Yanok518036e2011-11-28 06:37:30 +0000536 writel(BD_TO_HW((u_int32_t)emac_rx_desc), &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200537
538 debug_emac("- emac_open\n");
539
540 return(1);
541}
542
543/* EMAC Channel Teardown */
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200544static void davinci_eth_ch_teardown(int ch)
Sergey Kubushyne8f39122007-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 Netaguntea33bc4b2011-09-03 22:20:33 -0400553 writel(0, &adap_emac->TXTEARDOWN);
Nick Thompsond5ee6f62009-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 Denka1be4762008-05-20 16:00:29 +0200565 break;
Nick Thompsond5ee6f62009-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 Kubushyne8f39122007-08-10 20:26:18 +0200570 } else {
571 /* Init RX channel teardown */
Nagabhushana Netaguntea33bc4b2011-09-03 22:20:33 -0400572 writel(0, &adap_emac->RXTEARDOWN);
Nick Thompsond5ee6f62009-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 Denka1be4762008-05-20 16:00:29 +0200584 break;
Nick Thompsond5ee6f62009-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 Kubushyne8f39122007-08-10 20:26:18 +0200589 }
590
591 debug_emac("- emac_ch_teardown\n");
592}
593
594/* Eth device close */
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200595static void davinci_emac_stop(struct udevice *dev)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200596{
597 debug_emac("+ emac_close\n");
598
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200599 davinci_eth_ch_teardown(EMAC_CH_TX); /* TX Channel teardown */
Jeroen Hofstee8938b5b2015-06-07 17:30:38 +0200600 if (readl(&adap_emac->RXCONTROL) & 1)
601 davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200602
603 /* Reset EMAC module and disable interrupts in wrapper */
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000604 writel(1, &adap_emac->SOFTRESET);
605#if defined(DAVINCI_EMAC_VERSION2)
606 writel(1, &adap_ewrap->softrst);
607#else
608 writel(0, &adap_ewrap->EWCTL);
609#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200610
Sudhakar Rajashekhara5851e122010-11-18 09:59:37 -0500611#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
612 defined(CONFIG_MACH_DAVINCI_DA850_EVM)
613 adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
614 adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
615 adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
616#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200617 debug_emac("- emac_close\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200618}
619
620static int tx_send_loop = 0;
621
622/*
623 * This function sends a single packet on the network and returns
624 * positive number (number of bytes transmitted) or negative for error
625 */
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200626static int davinci_emac_send(struct udevice *dev,
627 void *packet, int length)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200628{
629 int ret_status = -1;
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000630 int index;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200631 tx_send_loop = 0;
632
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000633 index = get_active_phy();
634 if (index == -1) {
635 printf(" WARN: emac_send_packet: No link\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200636 return (ret_status);
637 }
638
639 /* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200640 if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200641 length = EMAC_MIN_ETHERNET_PKT_SIZE;
642 }
643
644 /* Populate the TX descriptor */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200645 emac_tx_desc->next = 0;
646 emac_tx_desc->buffer = (u_int8_t *) packet;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200647 emac_tx_desc->buff_off_len = (length & 0xffff);
648 emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
Wolfgang Denka1be4762008-05-20 16:00:29 +0200649 EMAC_CPPI_SOP_BIT |
650 EMAC_CPPI_OWNERSHIP_BIT |
651 EMAC_CPPI_EOP_BIT);
Ilya Yanokff672762011-11-28 06:37:33 +0000652
653 flush_dcache_range((unsigned long)packet,
karl beldane24ce8b2016-08-15 17:23:00 +0000654 (unsigned long)packet + ALIGN(length, PKTALIGN));
Ilya Yanokff672762011-11-28 06:37:33 +0000655
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200656 /* Send the packet */
Ilya Yanok518036e2011-11-28 06:37:30 +0000657 writel(BD_TO_HW((unsigned long)emac_tx_desc), &adap_emac->TX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200658
659 /* Wait for packet to complete or link down */
660 while (1) {
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000661 if (!phy[index].get_link_speed(active_phy_addr[index])) {
Sandeep Paulraj4b26f052008-08-31 00:39:46 +0200662 davinci_eth_ch_teardown (EMAC_CH_TX);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200663 return (ret_status);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200664 }
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000665
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000666 if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200667 ret_status = length;
668 break;
669 }
670 tx_send_loop++;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200671 }
672
Wolfgang Denka1be4762008-05-20 16:00:29 +0200673 return (ret_status);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200674}
675
676/*
677 * This function handles receipt of a packet from the network
678 */
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200679static int davinci_emac_recv(struct udevice *dev, int flags, uchar **packetp)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200680{
Wolfgang Denka1be4762008-05-20 16:00:29 +0200681 volatile emac_desc *rx_curr_desc;
682 volatile emac_desc *curr_desc;
683 volatile emac_desc *tail_desc;
684 int status, ret = -1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200685
686 rx_curr_desc = emac_rx_active_head;
Vishwas Srivastavac994c9c2016-01-26 12:46:42 +0530687 if (!rx_curr_desc)
688 return 0;
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200689 *packetp = rx_curr_desc->buffer;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200690 status = rx_curr_desc->pkt_flag_len;
Vishwas Srivastavac994c9c2016-01-26 12:46:42 +0530691 if ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200692 if (status & EMAC_CPPI_RX_ERROR_FRAME) {
693 /* Error in packet - discard it and requeue desc */
694 printf ("WARN: emac_rcv_pkt: Error in packet\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200695 } else {
Ilya Yanokff672762011-11-28 06:37:33 +0000696 unsigned long tmp = (unsigned long)rx_curr_desc->buffer;
karl beldan12e49872016-08-15 17:23:01 +0000697 unsigned short len =
698 rx_curr_desc->buff_off_len & 0xffff;
Ilya Yanokff672762011-11-28 06:37:33 +0000699
karl beldan12e49872016-08-15 17:23:01 +0000700 invalidate_dcache_range(tmp, tmp + ALIGN(len, PKTALIGN));
karl beldan12e49872016-08-15 17:23:01 +0000701 ret = len;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200702 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200703
Wolfgang Denka1be4762008-05-20 16:00:29 +0200704 /* Ack received packet descriptor */
Ilya Yanok518036e2011-11-28 06:37:30 +0000705 writel(BD_TO_HW((ulong)rx_curr_desc), &adap_emac->RX0CP);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200706 curr_desc = rx_curr_desc;
707 emac_rx_active_head =
Ilya Yanok518036e2011-11-28 06:37:30 +0000708 (volatile emac_desc *) (HW_TO_BD(rx_curr_desc->next));
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200709
Wolfgang Denka1be4762008-05-20 16:00:29 +0200710 if (status & EMAC_CPPI_EOQ_BIT) {
711 if (emac_rx_active_head) {
Ilya Yanok518036e2011-11-28 06:37:30 +0000712 writel(BD_TO_HW((ulong)emac_rx_active_head),
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000713 &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200714 } else {
715 emac_rx_queue_active = 0;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200716 printf ("INFO:emac_rcv_packet: RX Queue not active\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200717 }
718 }
719
720 /* Recycle RX descriptor */
721 rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
722 rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
723 rx_curr_desc->next = 0;
724
725 if (emac_rx_active_head == 0) {
Wolfgang Denka1be4762008-05-20 16:00:29 +0200726 printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200727 emac_rx_active_head = curr_desc;
728 emac_rx_active_tail = curr_desc;
729 if (emac_rx_queue_active != 0) {
Ilya Yanok518036e2011-11-28 06:37:30 +0000730 writel(BD_TO_HW((ulong)emac_rx_active_head),
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000731 &adap_emac->RX0HDP);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200732 printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200733 emac_rx_queue_active = 1;
734 }
735 } else {
736 tail_desc = emac_rx_active_tail;
737 emac_rx_active_tail = curr_desc;
Ilya Yanok518036e2011-11-28 06:37:30 +0000738 tail_desc->next = BD_TO_HW((ulong) curr_desc);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200739 status = tail_desc->pkt_flag_len;
740 if (status & EMAC_CPPI_EOQ_BIT) {
Ilya Yanok518036e2011-11-28 06:37:30 +0000741 writel(BD_TO_HW((ulong)curr_desc),
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000742 &adap_emac->RX0HDP);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200743 status &= ~EMAC_CPPI_EOQ_BIT;
744 tail_desc->pkt_flag_len = status;
745 }
746 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200747 return (ret);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200748 }
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200749
Wolfgang Denka1be4762008-05-20 16:00:29 +0200750 return (0);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200751}
752
Ben Warren4c28e272009-04-27 23:19:10 -0700753/*
754 * This function initializes the emac hardware. It does NOT initialize
755 * EMAC modules power or pin multiplexors, that is done by board_init()
756 * much earlier in bootup process. Returns 1 on success, 0 otherwise.
757 */
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200758static int davinci_emac_probe(struct udevice *dev)
Ben Warren4c28e272009-04-27 23:19:10 -0700759{
760 u_int32_t phy_id;
761 u_int16_t tmp;
762 int i;
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000763 int ret;
Ben Warren4c28e272009-04-27 23:19:10 -0700764
765 davinci_eth_mdio_enable();
766
Heiko Schocher70fa9662011-09-14 19:37:42 +0000767 /* let the EMAC detect the PHYs */
768 udelay(5000);
769
Ben Warren4c28e272009-04-27 23:19:10 -0700770 for (i = 0; i < 256; i++) {
Nick Thompsond5ee6f62009-12-18 13:33:07 +0000771 if (readl(&adap_mdio->ALIVE))
Ben Warren4c28e272009-04-27 23:19:10 -0700772 break;
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000773 udelay(1000);
Ben Warren4c28e272009-04-27 23:19:10 -0700774 }
775
776 if (i >= 256) {
777 printf("No ETH PHY detected!!!\n");
778 return(0);
779 }
780
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000781 /* Find if PHY(s) is/are connected */
782 ret = davinci_eth_phy_detect();
783 if (!ret)
Ben Warren4c28e272009-04-27 23:19:10 -0700784 return(0);
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000785 else
Heiko Schocher7d037f72011-11-15 10:00:04 -0500786 debug_emac(" %d ETH PHY detected\n", ret);
Ben Warren4c28e272009-04-27 23:19:10 -0700787
788 /* Get PHY ID and initialize phy_ops for a detected PHY */
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000789 for (i = 0; i < num_phy; i++) {
790 if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID1,
791 &tmp)) {
792 active_phy_addr[i] = 0xff;
793 continue;
794 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200795
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000796 phy_id = (tmp << 16) & 0xffff0000;
Ben Warren4c28e272009-04-27 23:19:10 -0700797
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000798 if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID2,
799 &tmp)) {
800 active_phy_addr[i] = 0xff;
801 continue;
802 }
Ben Warren4c28e272009-04-27 23:19:10 -0700803
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000804 phy_id |= tmp & 0x0000ffff;
Ben Warren4c28e272009-04-27 23:19:10 -0700805
Bartosz Golaszewski101c5ba2019-04-29 18:37:08 +0200806 sprintf(phy[i].name, "GENERIC @ 0x%02x",
807 active_phy_addr[i]);
808 phy[i].init = gen_init_phy;
809 phy[i].is_phy_connected = gen_is_phy_connected;
810 phy[i].get_link_speed = gen_get_link_speed;
811 phy[i].auto_negotiate = gen_auto_negotiate;
Ben Warren4c28e272009-04-27 23:19:10 -0700812
Ilya Yanok57c449d2011-11-01 13:15:55 +0000813 debug("Ethernet PHY: %s\n", phy[i].name);
Ben Warren4c28e272009-04-27 23:19:10 -0700814
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500815 int retval;
816 struct mii_dev *mdiodev = mdio_alloc();
817 if (!mdiodev)
818 return -ENOMEM;
Vladimir Olteanad2e1fc2021-09-27 14:21:58 +0300819 strlcpy(mdiodev->name, phy[i].name, MDIO_NAME_LEN);
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500820 mdiodev->read = davinci_mii_phy_read;
821 mdiodev->write = davinci_mii_phy_write;
822
823 retval = mdio_register(mdiodev);
824 if (retval < 0)
825 return retval;
Tom Rinic3cf8992017-05-10 12:01:02 -0400826#ifdef DAVINCI_EMAC_GIG_ENABLE
827#define PHY_CONF_REG 22
828 /* Enable PHY to clock out TX_CLK */
829 davinci_eth_phy_read(active_phy_addr[i], PHY_CONF_REG, &tmp);
830 tmp |= PHY_CONF_TXCLKEN;
831 davinci_eth_phy_write(active_phy_addr[i], PHY_CONF_REG, tmp);
832 davinci_eth_phy_read(active_phy_addr[i], PHY_CONF_REG, &tmp);
833#endif
Manjunath Hadli444d8c12011-10-13 03:40:54 +0000834 }
Rajashekhara, Sudhakarfe3a0d62012-06-07 00:27:44 +0000835
Tom Rinic3cf8992017-05-10 12:01:02 -0400836#if defined(CONFIG_TI816X) || (defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
Bastian Ruppertef2746a2012-09-13 22:29:03 +0000837 defined(CONFIG_MACH_DAVINCI_DA850_EVM) && \
Tom Rinic3cf8992017-05-10 12:01:02 -0400838 !defined(CONFIG_DRIVER_TI_EMAC_RMII_NO_NEGOTIATE))
Rajashekhara, Sudhakarfe3a0d62012-06-07 00:27:44 +0000839 for (i = 0; i < num_phy; i++) {
840 if (phy[i].is_phy_connected(i))
841 phy[i].auto_negotiate(i);
842 }
843#endif
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200844 return 0;
Ben Warren4c28e272009-04-27 23:19:10 -0700845}
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200846
847static const struct eth_ops davinci_emac_ops = {
848 .start = davinci_emac_start,
849 .send = davinci_emac_send,
850 .recv = davinci_emac_recv,
851 .stop = davinci_emac_stop,
852 .write_hwaddr = davinci_emac_write_hwaddr,
853};
854
855static const struct udevice_id davinci_emac_ids[] = {
856 { .compatible = "ti,davinci-dm6467-emac" },
857 { .compatible = "ti,am3517-emac", },
858 { .compatible = "ti,dm816-emac", },
859 { }
860};
861
862U_BOOT_DRIVER(davinci_emac) = {
863 .name = "davinci_emac",
864 .id = UCLASS_ETH,
865 .of_match = davinci_emac_ids,
866 .probe = davinci_emac_probe,
867 .ops = &davinci_emac_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700868 .plat_auto = sizeof(struct eth_pdata),
Bartosz Golaszewski2cedf752019-07-24 10:12:07 +0200869};