xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 903de461e4519ae073b4c0e967838c220fa5e5b0)
19751ee09SNobuhiro Iwamatsu /*
29751ee09SNobuhiro Iwamatsu  * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
39751ee09SNobuhiro Iwamatsu  *
49751ee09SNobuhiro Iwamatsu  * Copyright (C) 2008 Renesas Solutions Corp.
59751ee09SNobuhiro Iwamatsu  * Copyright (c) 2008 Nobuhiro Iwamatsu
69751ee09SNobuhiro Iwamatsu  * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
79751ee09SNobuhiro Iwamatsu  *
89751ee09SNobuhiro Iwamatsu  * This program is free software; you can redistribute it and/or modify
99751ee09SNobuhiro Iwamatsu  * it under the terms of the GNU General Public License as published by
109751ee09SNobuhiro Iwamatsu  * the Free Software Foundation; either version 2 of the License, or
119751ee09SNobuhiro Iwamatsu  * (at your option) any later version.
129751ee09SNobuhiro Iwamatsu  *
139751ee09SNobuhiro Iwamatsu  * This program is distributed in the hope that it will be useful,
149751ee09SNobuhiro Iwamatsu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
159751ee09SNobuhiro Iwamatsu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
169751ee09SNobuhiro Iwamatsu  * GNU General Public License for more details.
179751ee09SNobuhiro Iwamatsu  *
189751ee09SNobuhiro Iwamatsu  * You should have received a copy of the GNU General Public License
199751ee09SNobuhiro Iwamatsu  * along with this program; if not, write to the Free Software
209751ee09SNobuhiro Iwamatsu  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
219751ee09SNobuhiro Iwamatsu  */
229751ee09SNobuhiro Iwamatsu 
239751ee09SNobuhiro Iwamatsu #include <config.h>
249751ee09SNobuhiro Iwamatsu #include <common.h>
259751ee09SNobuhiro Iwamatsu #include <malloc.h>
269751ee09SNobuhiro Iwamatsu #include <net.h>
27bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
289751ee09SNobuhiro Iwamatsu #include <asm/errno.h>
299751ee09SNobuhiro Iwamatsu #include <asm/io.h>
309751ee09SNobuhiro Iwamatsu 
319751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
329751ee09SNobuhiro Iwamatsu 
339751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
349751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
359751ee09SNobuhiro Iwamatsu #endif
369751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
379751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
389751ee09SNobuhiro Iwamatsu #endif
3968260aabSYoshihiro Shimoda #ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
4068260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)	\
4168260aabSYoshihiro Shimoda 			dcache_wback_range((u32)addr, (u32)(addr + len - 1))
4268260aabSYoshihiro Shimoda #else
4368260aabSYoshihiro Shimoda #define flush_cache_wback(...)
4468260aabSYoshihiro Shimoda #endif
459751ee09SNobuhiro Iwamatsu 
46bd3980ccSNobuhiro Iwamatsu #define SH_ETH_PHY_DELAY 50000
479751ee09SNobuhiro Iwamatsu 
489751ee09SNobuhiro Iwamatsu /*
499751ee09SNobuhiro Iwamatsu  * Bits are written to the PHY serially using the
509751ee09SNobuhiro Iwamatsu  * PIR register, just like a bit banger.
519751ee09SNobuhiro Iwamatsu  */
529751ee09SNobuhiro Iwamatsu static void sh_eth_mii_write_phy_bits(int port, u32 val, int len)
539751ee09SNobuhiro Iwamatsu {
549751ee09SNobuhiro Iwamatsu 	int i;
559751ee09SNobuhiro Iwamatsu 	u32 pir;
569751ee09SNobuhiro Iwamatsu 
579751ee09SNobuhiro Iwamatsu 	/* Bit positions is 1 less than the number of bits */
589751ee09SNobuhiro Iwamatsu 	for (i = len - 1; i >= 0; i--) {
599751ee09SNobuhiro Iwamatsu 		/* Write direction, bit to write, clock is low */
609751ee09SNobuhiro Iwamatsu 		pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
619751ee09SNobuhiro Iwamatsu 		outl(pir, PIR(port));
629751ee09SNobuhiro Iwamatsu 		udelay(1);
639751ee09SNobuhiro Iwamatsu 		/* Write direction, bit to write, clock is high */
649751ee09SNobuhiro Iwamatsu 		pir = 3 | ((val & 1 << i) ? 1 << 2 : 0);
659751ee09SNobuhiro Iwamatsu 		outl(pir, PIR(port));
669751ee09SNobuhiro Iwamatsu 		udelay(1);
679751ee09SNobuhiro Iwamatsu 		/* Write direction, bit to write, clock is low */
689751ee09SNobuhiro Iwamatsu 		pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
699751ee09SNobuhiro Iwamatsu 		outl(pir, PIR(port));
709751ee09SNobuhiro Iwamatsu 		udelay(1);
719751ee09SNobuhiro Iwamatsu 	}
729751ee09SNobuhiro Iwamatsu }
739751ee09SNobuhiro Iwamatsu 
749751ee09SNobuhiro Iwamatsu static void sh_eth_mii_bus_release(int port)
759751ee09SNobuhiro Iwamatsu {
769751ee09SNobuhiro Iwamatsu 	/* Read direction, clock is low */
779751ee09SNobuhiro Iwamatsu 	outl(0, PIR(port));
789751ee09SNobuhiro Iwamatsu 	udelay(1);
799751ee09SNobuhiro Iwamatsu 	/* Read direction, clock is high */
809751ee09SNobuhiro Iwamatsu 	outl(1, PIR(port));
819751ee09SNobuhiro Iwamatsu 	udelay(1);
829751ee09SNobuhiro Iwamatsu 	/* Read direction, clock is low */
839751ee09SNobuhiro Iwamatsu 	outl(0, PIR(port));
849751ee09SNobuhiro Iwamatsu 	udelay(1);
859751ee09SNobuhiro Iwamatsu }
869751ee09SNobuhiro Iwamatsu 
879751ee09SNobuhiro Iwamatsu static void sh_eth_mii_ind_bus_release(int port)
889751ee09SNobuhiro Iwamatsu {
899751ee09SNobuhiro Iwamatsu 	/* Read direction, clock is low */
909751ee09SNobuhiro Iwamatsu 	outl(0, PIR(port));
919751ee09SNobuhiro Iwamatsu 	udelay(1);
929751ee09SNobuhiro Iwamatsu }
939751ee09SNobuhiro Iwamatsu 
94bd3980ccSNobuhiro Iwamatsu static void sh_eth_mii_read_phy_bits(int port, u32 *val, int len)
959751ee09SNobuhiro Iwamatsu {
969751ee09SNobuhiro Iwamatsu 	int i;
979751ee09SNobuhiro Iwamatsu 	u32 pir;
989751ee09SNobuhiro Iwamatsu 
999751ee09SNobuhiro Iwamatsu 	*val = 0;
1009751ee09SNobuhiro Iwamatsu 	for (i = len - 1; i >= 0; i--) {
1019751ee09SNobuhiro Iwamatsu 		/* Read direction, clock is high */
1029751ee09SNobuhiro Iwamatsu 		outl(1, PIR(port));
1039751ee09SNobuhiro Iwamatsu 		udelay(1);
1049751ee09SNobuhiro Iwamatsu 		/* Read bit */
1059751ee09SNobuhiro Iwamatsu 		pir = inl(PIR(port));
1069751ee09SNobuhiro Iwamatsu 		*val |= (pir & 8) ? 1 << i : 0;
1079751ee09SNobuhiro Iwamatsu 		/* Read direction, clock is low */
1089751ee09SNobuhiro Iwamatsu 		outl(0, PIR(port));
1099751ee09SNobuhiro Iwamatsu 		udelay(1);
1109751ee09SNobuhiro Iwamatsu 	}
1119751ee09SNobuhiro Iwamatsu }
1129751ee09SNobuhiro Iwamatsu 
1139751ee09SNobuhiro Iwamatsu #define PHY_INIT	0xFFFFFFFF
1149751ee09SNobuhiro Iwamatsu #define PHY_READ	0x02
1159751ee09SNobuhiro Iwamatsu #define PHY_WRITE	0x01
1169751ee09SNobuhiro Iwamatsu /*
1179751ee09SNobuhiro Iwamatsu  * To read a phy register, mii managements frames are sent to the phy.
1189751ee09SNobuhiro Iwamatsu  * The frames look like this:
1199751ee09SNobuhiro Iwamatsu  * pre (32 bits):	0xffff ffff
1209751ee09SNobuhiro Iwamatsu  * st (2 bits):		01
1219751ee09SNobuhiro Iwamatsu  * op (2bits):		10: read 01: write
1229751ee09SNobuhiro Iwamatsu  * phyad (5 bits):	xxxxx
1239751ee09SNobuhiro Iwamatsu  * regad (5 bits):	xxxxx
1249751ee09SNobuhiro Iwamatsu  * ta (Bus release):
1259751ee09SNobuhiro Iwamatsu  * data (16 bits):	read data
1269751ee09SNobuhiro Iwamatsu  */
1279751ee09SNobuhiro Iwamatsu static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg)
1289751ee09SNobuhiro Iwamatsu {
1299751ee09SNobuhiro Iwamatsu 	u32 val;
1309751ee09SNobuhiro Iwamatsu 
1319751ee09SNobuhiro Iwamatsu 	/* Sent mii management frame */
1329751ee09SNobuhiro Iwamatsu 	/* pre */
1339751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
1349751ee09SNobuhiro Iwamatsu 	/* st (start of frame) */
1359751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, 0x1, 2);
1369751ee09SNobuhiro Iwamatsu 	/* op (code) */
1379751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
1389751ee09SNobuhiro Iwamatsu 	/* phy address */
1399751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, phy_addr, 5);
1409751ee09SNobuhiro Iwamatsu 	/* Register to read */
1419751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, reg, 5);
1429751ee09SNobuhiro Iwamatsu 
1439751ee09SNobuhiro Iwamatsu 	/* Bus release */
1449751ee09SNobuhiro Iwamatsu 	sh_eth_mii_bus_release(port);
1459751ee09SNobuhiro Iwamatsu 
1469751ee09SNobuhiro Iwamatsu 	/* Read register */
1479751ee09SNobuhiro Iwamatsu 	sh_eth_mii_read_phy_bits(port, &val, 16);
1489751ee09SNobuhiro Iwamatsu 
1499751ee09SNobuhiro Iwamatsu 	return val;
1509751ee09SNobuhiro Iwamatsu }
1519751ee09SNobuhiro Iwamatsu 
1529751ee09SNobuhiro Iwamatsu /*
1539751ee09SNobuhiro Iwamatsu  * To write a phy register, mii managements frames are sent to the phy.
1549751ee09SNobuhiro Iwamatsu  * The frames look like this:
1559751ee09SNobuhiro Iwamatsu  * pre (32 bits):	0xffff ffff
1569751ee09SNobuhiro Iwamatsu  * st (2 bits):		01
1579751ee09SNobuhiro Iwamatsu  * op (2bits):		10: read 01: write
1589751ee09SNobuhiro Iwamatsu  * phyad (5 bits):	xxxxx
1599751ee09SNobuhiro Iwamatsu  * regad (5 bits):	xxxxx
1609751ee09SNobuhiro Iwamatsu  * ta (2 bits):		10
1619751ee09SNobuhiro Iwamatsu  * data (16 bits):	write data
1629751ee09SNobuhiro Iwamatsu  * idle (Independent bus release)
1639751ee09SNobuhiro Iwamatsu  */
1649751ee09SNobuhiro Iwamatsu static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val)
1659751ee09SNobuhiro Iwamatsu {
1669751ee09SNobuhiro Iwamatsu 	/* Sent mii management frame */
1679751ee09SNobuhiro Iwamatsu 	/* pre */
1689751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
1699751ee09SNobuhiro Iwamatsu 	/* st (start of frame) */
1709751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, 0x1, 2);
1719751ee09SNobuhiro Iwamatsu 	/* op (code) */
1729751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2);
1739751ee09SNobuhiro Iwamatsu 	/* phy address */
1749751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, phy_addr, 5);
1759751ee09SNobuhiro Iwamatsu 	/* Register to read */
1769751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, reg, 5);
1779751ee09SNobuhiro Iwamatsu 	/* ta */
1789751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
1799751ee09SNobuhiro Iwamatsu 	/* Write register data */
1809751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_bits(port, val, 16);
1819751ee09SNobuhiro Iwamatsu 
1829751ee09SNobuhiro Iwamatsu 	/* Independent bus release */
1839751ee09SNobuhiro Iwamatsu 	sh_eth_mii_ind_bus_release(port);
1849751ee09SNobuhiro Iwamatsu }
1859751ee09SNobuhiro Iwamatsu 
186bd3980ccSNobuhiro Iwamatsu int sh_eth_send(struct eth_device *dev, volatile void *packet, int len)
1879751ee09SNobuhiro Iwamatsu {
188bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
189bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0, timeout;
190bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1919751ee09SNobuhiro Iwamatsu 
1929751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
193bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
194bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
195bd3980ccSNobuhiro Iwamatsu 		goto err;
1969751ee09SNobuhiro Iwamatsu 	}
1979751ee09SNobuhiro Iwamatsu 
1989751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
1999751ee09SNobuhiro Iwamatsu 	if ((int)packet & (4 - 1)) {
200bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
201bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
202bd3980ccSNobuhiro Iwamatsu 		goto err;
2039751ee09SNobuhiro Iwamatsu 	}
2049751ee09SNobuhiro Iwamatsu 
2059751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
20668260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
2079751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
2089751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
2099751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
2109751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
2119751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
2129751ee09SNobuhiro Iwamatsu 	else
2139751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
2149751ee09SNobuhiro Iwamatsu 
2159751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
2169751ee09SNobuhiro Iwamatsu 	if (!(inl(EDTRR(port)) & EDTRR_TRNS))
2179751ee09SNobuhiro Iwamatsu 		outl(EDTRR_TRNS, EDTRR(port));
2189751ee09SNobuhiro Iwamatsu 
2199751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
2209751ee09SNobuhiro Iwamatsu 	timeout = 1000;
2219751ee09SNobuhiro Iwamatsu 	while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
2229751ee09SNobuhiro Iwamatsu 		udelay(100);
2239751ee09SNobuhiro Iwamatsu 
2249751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
225bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
226bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
2279751ee09SNobuhiro Iwamatsu 		goto err;
2289751ee09SNobuhiro Iwamatsu 	}
2299751ee09SNobuhiro Iwamatsu 
2309751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
2319751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
2329751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
2339751ee09SNobuhiro Iwamatsu 
234bd3980ccSNobuhiro Iwamatsu 	return ret;
235bd3980ccSNobuhiro Iwamatsu err:
236bd3980ccSNobuhiro Iwamatsu 	return ret;
2379751ee09SNobuhiro Iwamatsu }
2389751ee09SNobuhiro Iwamatsu 
239bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
2409751ee09SNobuhiro Iwamatsu {
241bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
242bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
243bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2449751ee09SNobuhiro Iwamatsu 	volatile u8 *packet;
2459751ee09SNobuhiro Iwamatsu 
2469751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
2479751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
2489751ee09SNobuhiro Iwamatsu 		/* Check for errors */
2499751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
2509751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
2519751ee09SNobuhiro Iwamatsu 			packet = (volatile u8 *)
2529751ee09SNobuhiro Iwamatsu 			    ADDR_TO_P2(port_info->rx_desc_cur->rd2);
2539751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
2549751ee09SNobuhiro Iwamatsu 		}
2559751ee09SNobuhiro Iwamatsu 
2569751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
2579751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
2589751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
2599751ee09SNobuhiro Iwamatsu 		else
2609751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
2619751ee09SNobuhiro Iwamatsu 
2629751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
2639751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
2649751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
2659751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
2669751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
2679751ee09SNobuhiro Iwamatsu 	}
2689751ee09SNobuhiro Iwamatsu 
2699751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
2709751ee09SNobuhiro Iwamatsu 	if (!(inl(EDRRR(port)) & EDRRR_R))
2719751ee09SNobuhiro Iwamatsu 		outl(EDRRR_R, EDRRR(port));
2729751ee09SNobuhiro Iwamatsu 
2739751ee09SNobuhiro Iwamatsu 	return len;
2749751ee09SNobuhiro Iwamatsu }
2759751ee09SNobuhiro Iwamatsu 
2769751ee09SNobuhiro Iwamatsu #define EDMR_INIT_CNT 1000
277bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
2789751ee09SNobuhiro Iwamatsu {
279bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
280*903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
281bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
2829751ee09SNobuhiro Iwamatsu 
2839751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
2849751ee09SNobuhiro Iwamatsu 	outl(EDSR_ENALL, EDSR(port));
2859751ee09SNobuhiro Iwamatsu 
2869751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
2879751ee09SNobuhiro Iwamatsu 	outl(EDMR_SRST, EDMR(port));
2889751ee09SNobuhiro Iwamatsu 	for (i = 0; i < EDMR_INIT_CNT; i++) {
2899751ee09SNobuhiro Iwamatsu 		if (!(inl(EDMR(port)) & EDMR_SRST))
2909751ee09SNobuhiro Iwamatsu 			break;
2919751ee09SNobuhiro Iwamatsu 		udelay(1000);
2929751ee09SNobuhiro Iwamatsu 	}
2939751ee09SNobuhiro Iwamatsu 
2949751ee09SNobuhiro Iwamatsu 	if (i == EDMR_INIT_CNT) {
295bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
296bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
2979751ee09SNobuhiro Iwamatsu 	}
2989751ee09SNobuhiro Iwamatsu 
299bd3980ccSNobuhiro Iwamatsu 	return ret;
300*903de461SYoshihiro Shimoda #else
301*903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
302*903de461SYoshihiro Shimoda 	udelay(3000);
303*903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
304*903de461SYoshihiro Shimoda 
305*903de461SYoshihiro Shimoda 	return 0;
306*903de461SYoshihiro Shimoda #endif
307bd3980ccSNobuhiro Iwamatsu }
308bd3980ccSNobuhiro Iwamatsu 
309bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
3109751ee09SNobuhiro Iwamatsu {
311bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
3129751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
313bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3149751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
3159751ee09SNobuhiro Iwamatsu 
316bd3980ccSNobuhiro Iwamatsu 	/*
317bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
318bd3980ccSNobuhiro Iwamatsu 	 */
319bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
3209751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
321bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
322bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
323bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
324bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
325bd3980ccSNobuhiro Iwamatsu 		goto err;
3269751ee09SNobuhiro Iwamatsu 	}
327bd3980ccSNobuhiro Iwamatsu 
3289751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
3299751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
33068260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
3319751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
3329751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
3339751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
3349751ee09SNobuhiro Iwamatsu 
3359751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
3369751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
3379751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
3389751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
3399751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
3409751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
3419751ee09SNobuhiro Iwamatsu 	}
3429751ee09SNobuhiro Iwamatsu 
3439751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
3449751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
3459751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
3469751ee09SNobuhiro Iwamatsu 
3479751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
3489751ee09SNobuhiro Iwamatsu 	   addresses */
3499751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
350*903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
3519751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
3529751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
3539751ee09SNobuhiro Iwamatsu 	outl(0x01, TDFFR(port));/* Last discriptor bit */
354*903de461SYoshihiro Shimoda #endif
3559751ee09SNobuhiro Iwamatsu 
356bd3980ccSNobuhiro Iwamatsu err:
357bd3980ccSNobuhiro Iwamatsu 	return ret;
3589751ee09SNobuhiro Iwamatsu }
3599751ee09SNobuhiro Iwamatsu 
360bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
3619751ee09SNobuhiro Iwamatsu {
362bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
363bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3649751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
365bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
3669751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
3679751ee09SNobuhiro Iwamatsu 
368bd3980ccSNobuhiro Iwamatsu 	/*
369bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
370bd3980ccSNobuhiro Iwamatsu 	 */
371bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
3729751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
373bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
374bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
375bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
376bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
377bd3980ccSNobuhiro Iwamatsu 		goto err;
3789751ee09SNobuhiro Iwamatsu 	}
379bd3980ccSNobuhiro Iwamatsu 
3809751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
3819751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
38268260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
3839751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
3849751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
3859751ee09SNobuhiro Iwamatsu 
3869751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
3879751ee09SNobuhiro Iwamatsu 
388bd3980ccSNobuhiro Iwamatsu 	/*
389bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
390bd3980ccSNobuhiro Iwamatsu 	 * P2 area
391bd3980ccSNobuhiro Iwamatsu 	 */
392bd3980ccSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
393bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
394bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
395bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
396bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
3979751ee09SNobuhiro Iwamatsu 	}
398bd3980ccSNobuhiro Iwamatsu 
3999751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
4009751ee09SNobuhiro Iwamatsu 			  ~(32 - 1));
4019751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
4029751ee09SNobuhiro Iwamatsu 
4039751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
4049751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
4059751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
4069751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
4079751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
4089751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
4099751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
4109751ee09SNobuhiro Iwamatsu 	}
4119751ee09SNobuhiro Iwamatsu 
4129751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
4139751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
4149751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
4159751ee09SNobuhiro Iwamatsu 
4169751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
4179751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
418*903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
4199751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
4209751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
4219751ee09SNobuhiro Iwamatsu 	outl(RDFFR_RDLF, RDFFR(port));
422*903de461SYoshihiro Shimoda #endif
4239751ee09SNobuhiro Iwamatsu 
424bd3980ccSNobuhiro Iwamatsu 	return ret;
425bd3980ccSNobuhiro Iwamatsu 
426bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
427bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
428bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
429bd3980ccSNobuhiro Iwamatsu 
430bd3980ccSNobuhiro Iwamatsu err:
431bd3980ccSNobuhiro Iwamatsu 	return ret;
4329751ee09SNobuhiro Iwamatsu }
4339751ee09SNobuhiro Iwamatsu 
434bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
4359751ee09SNobuhiro Iwamatsu {
436bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
437bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
4389751ee09SNobuhiro Iwamatsu 
4399751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
4409751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
4419751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
4429751ee09SNobuhiro Iwamatsu 	}
443bd3980ccSNobuhiro Iwamatsu }
444bd3980ccSNobuhiro Iwamatsu 
445bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
446bd3980ccSNobuhiro Iwamatsu {
447bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
448bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
4499751ee09SNobuhiro Iwamatsu 
4509751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
4519751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
4529751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
4539751ee09SNobuhiro Iwamatsu 	}
4549751ee09SNobuhiro Iwamatsu 
4559751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
4569751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
4579751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
4589751ee09SNobuhiro Iwamatsu 	}
4599751ee09SNobuhiro Iwamatsu }
4609751ee09SNobuhiro Iwamatsu 
461bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
4629751ee09SNobuhiro Iwamatsu {
463bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
4649751ee09SNobuhiro Iwamatsu 
465bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
466bd3980ccSNobuhiro Iwamatsu 	if (ret)
467bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
468bd3980ccSNobuhiro Iwamatsu 
469bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
470bd3980ccSNobuhiro Iwamatsu 	if (ret)
471bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
472bd3980ccSNobuhiro Iwamatsu 
473bd3980ccSNobuhiro Iwamatsu 	return ret;
474bd3980ccSNobuhiro Iwamatsu err_rx_init:
475bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
476bd3980ccSNobuhiro Iwamatsu 
477bd3980ccSNobuhiro Iwamatsu err_tx_init:
478bd3980ccSNobuhiro Iwamatsu 	return ret;
4799751ee09SNobuhiro Iwamatsu }
4809751ee09SNobuhiro Iwamatsu 
481bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
4829751ee09SNobuhiro Iwamatsu {
483bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, timeout, ret = 0;
484bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
4859751ee09SNobuhiro Iwamatsu 	u32 val;
486bd3980ccSNobuhiro Iwamatsu 
4879751ee09SNobuhiro Iwamatsu 	/* Reset phy */
488bd3980ccSNobuhiro Iwamatsu 	sh_eth_mii_write_phy_reg
489bd3980ccSNobuhiro Iwamatsu 		(port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
4909751ee09SNobuhiro Iwamatsu 	timeout = 10;
4919751ee09SNobuhiro Iwamatsu 	while (timeout--) {
492bd3980ccSNobuhiro Iwamatsu 		val = sh_eth_mii_read_phy_reg(port,
493bd3980ccSNobuhiro Iwamatsu 				port_info->phy_addr, PHY_CTRL);
4949751ee09SNobuhiro Iwamatsu 		if (!(val & PHY_C_RESET))
4959751ee09SNobuhiro Iwamatsu 			break;
496bd3980ccSNobuhiro Iwamatsu 		udelay(SH_ETH_PHY_DELAY);
4979751ee09SNobuhiro Iwamatsu 	}
498bd3980ccSNobuhiro Iwamatsu 
4999751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
500bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy reset timeout\n");
501bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
502bd3980ccSNobuhiro Iwamatsu 		goto err_tout;
5039751ee09SNobuhiro Iwamatsu 	}
5049751ee09SNobuhiro Iwamatsu 
5059751ee09SNobuhiro Iwamatsu 	/* Advertise 100/10 baseT full/half duplex */
5069751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
5079751ee09SNobuhiro Iwamatsu 		(PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
5089751ee09SNobuhiro Iwamatsu 	/* Autonegotiation, normal operation, full duplex, enable tx */
5099751ee09SNobuhiro Iwamatsu 	sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
5109751ee09SNobuhiro Iwamatsu 		(PHY_C_ANEGEN|PHY_C_RANEG));
5119751ee09SNobuhiro Iwamatsu 	/* Wait for autonegotiation to complete */
5129751ee09SNobuhiro Iwamatsu 	timeout = 100;
5139751ee09SNobuhiro Iwamatsu 	while (timeout--) {
5149751ee09SNobuhiro Iwamatsu 		val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
5159751ee09SNobuhiro Iwamatsu 		if (val & PHY_S_ANEGC)
5169751ee09SNobuhiro Iwamatsu 			break;
517bd3980ccSNobuhiro Iwamatsu 
518bd3980ccSNobuhiro Iwamatsu 		udelay(SH_ETH_PHY_DELAY);
5199751ee09SNobuhiro Iwamatsu 	}
520bd3980ccSNobuhiro Iwamatsu 
5219751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
522bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy auto-negotiation failed\n");
523bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
524bd3980ccSNobuhiro Iwamatsu 		goto err_tout;
5259751ee09SNobuhiro Iwamatsu 	}
5269751ee09SNobuhiro Iwamatsu 
527bd3980ccSNobuhiro Iwamatsu 	return ret;
528bd3980ccSNobuhiro Iwamatsu 
529bd3980ccSNobuhiro Iwamatsu err_tout:
530bd3980ccSNobuhiro Iwamatsu 	return ret;
5319751ee09SNobuhiro Iwamatsu }
5329751ee09SNobuhiro Iwamatsu 
533bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
5349751ee09SNobuhiro Iwamatsu {
535bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
536bd3980ccSNobuhiro Iwamatsu 	u32 val,  phy_status;
537bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
538c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
5399751ee09SNobuhiro Iwamatsu 
5409751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
5419751ee09SNobuhiro Iwamatsu 	outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
5429751ee09SNobuhiro Iwamatsu 	outl(0, EESIPR(port));
5439751ee09SNobuhiro Iwamatsu 	outl(0, TRSCER(port));
5449751ee09SNobuhiro Iwamatsu 	outl(0, TFTR(port));
5459751ee09SNobuhiro Iwamatsu 	outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
5469751ee09SNobuhiro Iwamatsu 	outl(RMCR_RST, RMCR(port));
547*903de461SYoshihiro Shimoda #ifndef CONFIG_CPU_SH7757
5489751ee09SNobuhiro Iwamatsu 	outl(0, RPADIR(port));
549*903de461SYoshihiro Shimoda #endif
5509751ee09SNobuhiro Iwamatsu 	outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
5519751ee09SNobuhiro Iwamatsu 
5529751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
553*903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7757)
554*903de461SYoshihiro Shimoda 	outl(ECSIPR_BRCRXIP | ECSIPR_PSRTOIP | ECSIPR_LCHNGIP |
555*903de461SYoshihiro Shimoda 		ECSIPR_MPDIP | ECSIPR_ICDIP, ECSIPR(port));
556*903de461SYoshihiro Shimoda #else
5579751ee09SNobuhiro Iwamatsu 	outl(0, ECSIPR(port));
558*903de461SYoshihiro Shimoda #endif
5599751ee09SNobuhiro Iwamatsu 
5609751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
561c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
562c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
5639751ee09SNobuhiro Iwamatsu 	outl(val, MAHR(port));
5649751ee09SNobuhiro Iwamatsu 
565c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
5669751ee09SNobuhiro Iwamatsu 	outl(val, MALR(port));
5679751ee09SNobuhiro Iwamatsu 
5689751ee09SNobuhiro Iwamatsu 	outl(RFLR_RFL_MIN, RFLR(port));
569*903de461SYoshihiro Shimoda #ifndef CONFIG_CPU_SH7757
5709751ee09SNobuhiro Iwamatsu 	outl(0, PIPR(port));
571*903de461SYoshihiro Shimoda #endif
5729751ee09SNobuhiro Iwamatsu 	outl(APR_AP, APR(port));
5739751ee09SNobuhiro Iwamatsu 	outl(MPR_MP, MPR(port));
574*903de461SYoshihiro Shimoda #ifdef CONFIG_CPU_SH7757
575*903de461SYoshihiro Shimoda 	outl(TPAUSER_UNLIMITED, TPAUSER(port));
576*903de461SYoshihiro Shimoda #else
5779751ee09SNobuhiro Iwamatsu 	outl(TPAUSER_TPAUSE, TPAUSER(port));
578*903de461SYoshihiro Shimoda #endif
5799751ee09SNobuhiro Iwamatsu 	/* Configure phy */
580bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
581bd3980ccSNobuhiro Iwamatsu 	if (ret) {
58288a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
583bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
584bd3980ccSNobuhiro Iwamatsu 	}
5859751ee09SNobuhiro Iwamatsu 	/* Read phy status to finish configuring the e-mac */
586bd3980ccSNobuhiro Iwamatsu 	phy_status = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
5879751ee09SNobuhiro Iwamatsu 
5889751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
589*903de461SYoshihiro Shimoda #ifdef CONFIG_CPU_SH7763
5909751ee09SNobuhiro Iwamatsu 	if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
591bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
5929751ee09SNobuhiro Iwamatsu 		outl(GECMR_100B, GECMR(port));
5939751ee09SNobuhiro Iwamatsu 	} else {
594bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
5959751ee09SNobuhiro Iwamatsu 		outl(GECMR_10B, GECMR(port));
5969751ee09SNobuhiro Iwamatsu 	}
597*903de461SYoshihiro Shimoda #endif
598*903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7757)
599*903de461SYoshihiro Shimoda 	if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
600*903de461SYoshihiro Shimoda 		printf("100Base/");
601*903de461SYoshihiro Shimoda 		outl(1, RTRATE(port));
602*903de461SYoshihiro Shimoda 	} else {
603*903de461SYoshihiro Shimoda 		printf("10Base/");
604*903de461SYoshihiro Shimoda 		outl(0, RTRATE(port));
605*903de461SYoshihiro Shimoda 	}
606*903de461SYoshihiro Shimoda #endif
6079751ee09SNobuhiro Iwamatsu 
6089751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
6099751ee09SNobuhiro Iwamatsu 	if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
6109751ee09SNobuhiro Iwamatsu 		printf("Full\n");
6119751ee09SNobuhiro Iwamatsu 		outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
6129751ee09SNobuhiro Iwamatsu 	} else {
6139751ee09SNobuhiro Iwamatsu 		printf("Half\n");
6149751ee09SNobuhiro Iwamatsu 		outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE),  ECMR(port));
6159751ee09SNobuhiro Iwamatsu 	}
616bd3980ccSNobuhiro Iwamatsu 
617bd3980ccSNobuhiro Iwamatsu 	return ret;
618bd3980ccSNobuhiro Iwamatsu 
619bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
620bd3980ccSNobuhiro Iwamatsu 	return ret;
6219751ee09SNobuhiro Iwamatsu }
6229751ee09SNobuhiro Iwamatsu 
623bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
6249751ee09SNobuhiro Iwamatsu {
6259751ee09SNobuhiro Iwamatsu 	/*
6269751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
6279751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
6289751ee09SNobuhiro Iwamatsu 	 */
629bd3980ccSNobuhiro Iwamatsu 	outl(EDRRR_R, EDRRR(eth->port));
630bd3980ccSNobuhiro Iwamatsu }
6319751ee09SNobuhiro Iwamatsu 
632bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
633bd3980ccSNobuhiro Iwamatsu {
634bd3980ccSNobuhiro Iwamatsu 	outl(~EDRRR_R, EDRRR(eth->port));
6359751ee09SNobuhiro Iwamatsu }
6369751ee09SNobuhiro Iwamatsu 
637bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
6389751ee09SNobuhiro Iwamatsu {
639bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
640bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
641bd3980ccSNobuhiro Iwamatsu 
642bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
643bd3980ccSNobuhiro Iwamatsu 	if (ret)
644bd3980ccSNobuhiro Iwamatsu 		goto err;
645bd3980ccSNobuhiro Iwamatsu 
646bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
647bd3980ccSNobuhiro Iwamatsu 	if (ret)
648bd3980ccSNobuhiro Iwamatsu 		goto err;
649bd3980ccSNobuhiro Iwamatsu 
650bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
651bd3980ccSNobuhiro Iwamatsu 	if (ret)
652bd3980ccSNobuhiro Iwamatsu 		goto err_config;
653bd3980ccSNobuhiro Iwamatsu 
654bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
655bd3980ccSNobuhiro Iwamatsu 
656bd3980ccSNobuhiro Iwamatsu 	return ret;
657bd3980ccSNobuhiro Iwamatsu 
658bd3980ccSNobuhiro Iwamatsu err_config:
659bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
660bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
661bd3980ccSNobuhiro Iwamatsu 
662bd3980ccSNobuhiro Iwamatsu err:
663bd3980ccSNobuhiro Iwamatsu 	return ret;
6649751ee09SNobuhiro Iwamatsu }
6659751ee09SNobuhiro Iwamatsu 
666bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
667bd3980ccSNobuhiro Iwamatsu {
668bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
669bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
670bd3980ccSNobuhiro Iwamatsu }
671bd3980ccSNobuhiro Iwamatsu 
672bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
673bd3980ccSNobuhiro Iwamatsu {
674bd3980ccSNobuhiro Iwamatsu     int ret = 0;
675bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
676bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
677bd3980ccSNobuhiro Iwamatsu 
678bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
679bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
680bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
681bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
682bd3980ccSNobuhiro Iwamatsu 		goto err;
683bd3980ccSNobuhiro Iwamatsu 	}
684bd3980ccSNobuhiro Iwamatsu 
685bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
686bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
687bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
688bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
689bd3980ccSNobuhiro Iwamatsu 		goto err;
690bd3980ccSNobuhiro Iwamatsu 	}
691bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
692bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
693bd3980ccSNobuhiro Iwamatsu 
694bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
695bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
696bd3980ccSNobuhiro Iwamatsu 
697bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
698bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
699bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
700bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
701bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
702bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
703bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
704bd3980ccSNobuhiro Iwamatsu 
705bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
706bd3980ccSNobuhiro Iwamatsu 
707bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
708bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
7099751ee09SNobuhiro Iwamatsu 
710c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
711c527ce92SMike Frysinger 		puts("Please set MAC address\n");
7129751ee09SNobuhiro Iwamatsu 
713bd3980ccSNobuhiro Iwamatsu 	return ret;
7149751ee09SNobuhiro Iwamatsu 
7159751ee09SNobuhiro Iwamatsu err:
716bd3980ccSNobuhiro Iwamatsu 	if (dev)
7179751ee09SNobuhiro Iwamatsu 		free(dev);
718bd3980ccSNobuhiro Iwamatsu 
719bd3980ccSNobuhiro Iwamatsu 	if (eth)
720bd3980ccSNobuhiro Iwamatsu 		free(eth);
721bd3980ccSNobuhiro Iwamatsu 
722bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
723bd3980ccSNobuhiro Iwamatsu 	return ret;
7249751ee09SNobuhiro Iwamatsu }
725