xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 703949e495cddefb1762746cf14e5669ea92a0ac)
19751ee09SNobuhiro Iwamatsu /*
226235093SYoshihiro Shimoda  * sh_eth.c - Driver for Renesas ethernet controler.
39751ee09SNobuhiro Iwamatsu  *
43bb4cc31SNobuhiro Iwamatsu  * Copyright (C) 2008, 2011 Renesas Solutions Corp.
53bb4cc31SNobuhiro Iwamatsu  * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu
69751ee09SNobuhiro Iwamatsu  * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
78707678cSNobuhiro Iwamatsu  * Copyright (C) 2013  Renesas Electronics Corporation
89751ee09SNobuhiro Iwamatsu  *
91a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
109751ee09SNobuhiro Iwamatsu  */
119751ee09SNobuhiro Iwamatsu 
129751ee09SNobuhiro Iwamatsu #include <config.h>
139751ee09SNobuhiro Iwamatsu #include <common.h>
149751ee09SNobuhiro Iwamatsu #include <malloc.h>
159751ee09SNobuhiro Iwamatsu #include <net.h>
16bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
17bd1024b0SYoshihiro Shimoda #include <miiphy.h>
189751ee09SNobuhiro Iwamatsu #include <asm/errno.h>
199751ee09SNobuhiro Iwamatsu #include <asm/io.h>
209751ee09SNobuhiro Iwamatsu 
219751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
229751ee09SNobuhiro Iwamatsu 
239751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
249751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
259751ee09SNobuhiro Iwamatsu #endif
269751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
279751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
289751ee09SNobuhiro Iwamatsu #endif
29870cc23fSNobuhiro Iwamatsu 
3092f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
3168260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)    \
32870cc23fSNobuhiro Iwamatsu 		flush_dcache_range((u32)addr, (u32)(addr + len - 1))
3368260aabSYoshihiro Shimoda #else
3468260aabSYoshihiro Shimoda #define flush_cache_wback(...)
3568260aabSYoshihiro Shimoda #endif
369751ee09SNobuhiro Iwamatsu 
3792f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
3892f07134SNobuhiro Iwamatsu #define invalidate_cache(addr, len)		\
3992f07134SNobuhiro Iwamatsu 	{	\
4092f07134SNobuhiro Iwamatsu 		u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE;	\
4192f07134SNobuhiro Iwamatsu 		u32 start, end;	\
4292f07134SNobuhiro Iwamatsu 		\
4392f07134SNobuhiro Iwamatsu 		start = (u32)addr;	\
4492f07134SNobuhiro Iwamatsu 		end = start + len;	\
4592f07134SNobuhiro Iwamatsu 		start &= ~(line_size - 1);	\
4692f07134SNobuhiro Iwamatsu 		end = ((end + line_size - 1) & ~(line_size - 1));	\
4792f07134SNobuhiro Iwamatsu 		\
4892f07134SNobuhiro Iwamatsu 		invalidate_dcache_range(start, end);	\
4992f07134SNobuhiro Iwamatsu 	}
5092f07134SNobuhiro Iwamatsu #else
5192f07134SNobuhiro Iwamatsu #define invalidate_cache(...)
5292f07134SNobuhiro Iwamatsu #endif
5392f07134SNobuhiro Iwamatsu 
544ba62c72SNobuhiro Iwamatsu #define TIMEOUT_CNT 1000
554ba62c72SNobuhiro Iwamatsu 
5610cbe3b6SJoe Hershberger int sh_eth_send(struct eth_device *dev, void *packet, int len)
579751ee09SNobuhiro Iwamatsu {
58bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
59bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0, timeout;
60bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
619751ee09SNobuhiro Iwamatsu 
629751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
63bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
64bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
65bd3980ccSNobuhiro Iwamatsu 		goto err;
669751ee09SNobuhiro Iwamatsu 	}
679751ee09SNobuhiro Iwamatsu 
689751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
69ee6ec5d4SNobuhiro Iwamatsu 	if ((int)packet & 3) {
70e2752db0SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n"
71e2752db0SNobuhiro Iwamatsu 				, __func__);
72bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
73bd3980ccSNobuhiro Iwamatsu 		goto err;
749751ee09SNobuhiro Iwamatsu 	}
759751ee09SNobuhiro Iwamatsu 
769751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
7768260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
789751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
799751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
809751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
819751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
829751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
839751ee09SNobuhiro Iwamatsu 	else
849751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
859751ee09SNobuhiro Iwamatsu 
869751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
8749afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
8849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
899751ee09SNobuhiro Iwamatsu 
909751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
914ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
9292f07134SNobuhiro Iwamatsu 	do {
9392f07134SNobuhiro Iwamatsu 		invalidate_cache(port_info->tx_desc_cur,
9492f07134SNobuhiro Iwamatsu 				 sizeof(struct tx_desc_s));
959751ee09SNobuhiro Iwamatsu 		udelay(100);
9692f07134SNobuhiro Iwamatsu 	} while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
979751ee09SNobuhiro Iwamatsu 
989751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
99bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
100bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
1019751ee09SNobuhiro Iwamatsu 		goto err;
1029751ee09SNobuhiro Iwamatsu 	}
1039751ee09SNobuhiro Iwamatsu 
1049751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
1059751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
1069751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
1079751ee09SNobuhiro Iwamatsu 
108bd3980ccSNobuhiro Iwamatsu err:
109bd3980ccSNobuhiro Iwamatsu 	return ret;
1109751ee09SNobuhiro Iwamatsu }
1119751ee09SNobuhiro Iwamatsu 
112bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1139751ee09SNobuhiro Iwamatsu {
114bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
115bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
116bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
11710cbe3b6SJoe Hershberger 	uchar *packet;
1189751ee09SNobuhiro Iwamatsu 
1199751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
12092f07134SNobuhiro Iwamatsu 	invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
1219751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1229751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1239751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1249751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
12510cbe3b6SJoe Hershberger 			packet = (uchar *)
1269751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
12792f07134SNobuhiro Iwamatsu 			invalidate_cache(packet, len);
1289751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1299751ee09SNobuhiro Iwamatsu 		}
1309751ee09SNobuhiro Iwamatsu 
1319751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1329751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1339751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1349751ee09SNobuhiro Iwamatsu 		else
1359751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1369751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1379751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1389751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1399751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1409751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1419751ee09SNobuhiro Iwamatsu 	}
1429751ee09SNobuhiro Iwamatsu 
1439751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
14449afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
14549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1469751ee09SNobuhiro Iwamatsu 
1479751ee09SNobuhiro Iwamatsu 	return len;
1489751ee09SNobuhiro Iwamatsu }
1499751ee09SNobuhiro Iwamatsu 
150bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1519751ee09SNobuhiro Iwamatsu {
15262cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
153bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1549751ee09SNobuhiro Iwamatsu 
1559751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
15649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1579751ee09SNobuhiro Iwamatsu 
1589751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
15949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1604ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT; i++) {
16149afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1629751ee09SNobuhiro Iwamatsu 			break;
1639751ee09SNobuhiro Iwamatsu 		udelay(1000);
1649751ee09SNobuhiro Iwamatsu 	}
1659751ee09SNobuhiro Iwamatsu 
1664ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
167bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
168bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1699751ee09SNobuhiro Iwamatsu 	}
1709751ee09SNobuhiro Iwamatsu 
171bd3980ccSNobuhiro Iwamatsu 	return ret;
172903de461SYoshihiro Shimoda #else
17349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
174903de461SYoshihiro Shimoda 	udelay(3000);
17549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
176903de461SYoshihiro Shimoda 
177903de461SYoshihiro Shimoda 	return 0;
178903de461SYoshihiro Shimoda #endif
179bd3980ccSNobuhiro Iwamatsu }
180bd3980ccSNobuhiro Iwamatsu 
181bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1829751ee09SNobuhiro Iwamatsu {
183bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1849751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
185bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1869751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1879751ee09SNobuhiro Iwamatsu 
188bd3980ccSNobuhiro Iwamatsu 	/*
189*703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
190*703949e4SNobuhiro Iwamatsu 	 * tx_desc_s.
191bd3980ccSNobuhiro Iwamatsu 	 */
192bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1939751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
194*703949e4SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) - 1);
195bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
196bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
197bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
198bd3980ccSNobuhiro Iwamatsu 		goto err;
1999751ee09SNobuhiro Iwamatsu 	}
200bd3980ccSNobuhiro Iwamatsu 
201*703949e4SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc +
202*703949e4SNobuhiro Iwamatsu 			  sizeof(struct tx_desc_s) - 1) &
203*703949e4SNobuhiro Iwamatsu 			  ~(sizeof(struct tx_desc_s) - 1));
20468260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
2059751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2069751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
2079751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2089751ee09SNobuhiro Iwamatsu 
2099751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2109751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2119751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2129751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2139751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2149751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2159751ee09SNobuhiro Iwamatsu 	}
2169751ee09SNobuhiro Iwamatsu 
2179751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2189751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2199751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2209751ee09SNobuhiro Iwamatsu 
2219751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2229751ee09SNobuhiro Iwamatsu 	   addresses */
22349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
22462cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
22549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
22649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
22749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
228903de461SYoshihiro Shimoda #endif
2299751ee09SNobuhiro Iwamatsu 
230bd3980ccSNobuhiro Iwamatsu err:
231bd3980ccSNobuhiro Iwamatsu 	return ret;
2329751ee09SNobuhiro Iwamatsu }
2339751ee09SNobuhiro Iwamatsu 
234bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2359751ee09SNobuhiro Iwamatsu {
236bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
237bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2389751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
239bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2409751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2419751ee09SNobuhiro Iwamatsu 
242bd3980ccSNobuhiro Iwamatsu 	/*
243*703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
244*703949e4SNobuhiro Iwamatsu 	 * rx_desc_s.
245bd3980ccSNobuhiro Iwamatsu 	 */
246bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2479751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
248*703949e4SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) - 1);
249bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
250bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
251bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
252bd3980ccSNobuhiro Iwamatsu 		goto err;
2539751ee09SNobuhiro Iwamatsu 	}
254bd3980ccSNobuhiro Iwamatsu 
255*703949e4SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc +
256*703949e4SNobuhiro Iwamatsu 			  sizeof(struct rx_desc_s) - 1) &
257*703949e4SNobuhiro Iwamatsu 			  ~(sizeof(struct rx_desc_s) - 1));
25868260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2599751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2609751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2619751ee09SNobuhiro Iwamatsu 
2629751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2639751ee09SNobuhiro Iwamatsu 
264bd3980ccSNobuhiro Iwamatsu 	/*
265bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
266bd3980ccSNobuhiro Iwamatsu 	 * P2 area
267bd3980ccSNobuhiro Iwamatsu 	 */
268f8b7507dSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(
269f8b7507dSNobuhiro Iwamatsu 		NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
270bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
271bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
272bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
273bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2749751ee09SNobuhiro Iwamatsu 	}
275bd3980ccSNobuhiro Iwamatsu 
276f8b7507dSNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc
277f8b7507dSNobuhiro Iwamatsu 			  + (RX_BUF_ALIGNE_SIZE - 1)) &
278f8b7507dSNobuhiro Iwamatsu 			  ~(RX_BUF_ALIGNE_SIZE - 1));
2799751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2809751ee09SNobuhiro Iwamatsu 
2819751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2829751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2839751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2849751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2859751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2869751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2879751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2889751ee09SNobuhiro Iwamatsu 	}
2899751ee09SNobuhiro Iwamatsu 
2909751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2919751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2929751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2939751ee09SNobuhiro Iwamatsu 
2949751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
29549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
29662cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
29749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
29849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
29949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
300903de461SYoshihiro Shimoda #endif
3019751ee09SNobuhiro Iwamatsu 
302bd3980ccSNobuhiro Iwamatsu 	return ret;
303bd3980ccSNobuhiro Iwamatsu 
304bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
305bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
306bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
307bd3980ccSNobuhiro Iwamatsu 
308bd3980ccSNobuhiro Iwamatsu err:
309bd3980ccSNobuhiro Iwamatsu 	return ret;
3109751ee09SNobuhiro Iwamatsu }
3119751ee09SNobuhiro Iwamatsu 
312bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3139751ee09SNobuhiro Iwamatsu {
314bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
315bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3169751ee09SNobuhiro Iwamatsu 
3179751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3189751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3199751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3209751ee09SNobuhiro Iwamatsu 	}
321bd3980ccSNobuhiro Iwamatsu }
322bd3980ccSNobuhiro Iwamatsu 
323bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
324bd3980ccSNobuhiro Iwamatsu {
325bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
326bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3279751ee09SNobuhiro Iwamatsu 
3289751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3299751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3309751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3319751ee09SNobuhiro Iwamatsu 	}
3329751ee09SNobuhiro Iwamatsu 
3339751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3349751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3359751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3369751ee09SNobuhiro Iwamatsu 	}
3379751ee09SNobuhiro Iwamatsu }
3389751ee09SNobuhiro Iwamatsu 
339bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3409751ee09SNobuhiro Iwamatsu {
341bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3429751ee09SNobuhiro Iwamatsu 
343bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
344bd3980ccSNobuhiro Iwamatsu 	if (ret)
345bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
346bd3980ccSNobuhiro Iwamatsu 
347bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
348bd3980ccSNobuhiro Iwamatsu 	if (ret)
349bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
350bd3980ccSNobuhiro Iwamatsu 
351bd3980ccSNobuhiro Iwamatsu 	return ret;
352bd3980ccSNobuhiro Iwamatsu err_rx_init:
353bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
354bd3980ccSNobuhiro Iwamatsu 
355bd3980ccSNobuhiro Iwamatsu err_tx_init:
356bd3980ccSNobuhiro Iwamatsu 	return ret;
3579751ee09SNobuhiro Iwamatsu }
3589751ee09SNobuhiro Iwamatsu 
359bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3609751ee09SNobuhiro Iwamatsu {
361bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
362bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
363bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
364bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
365bd3980ccSNobuhiro Iwamatsu 
366ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
367ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3684398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
369bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
370bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
371bd3980ccSNobuhiro Iwamatsu 
372bd3980ccSNobuhiro Iwamatsu 	return ret;
3739751ee09SNobuhiro Iwamatsu }
3749751ee09SNobuhiro Iwamatsu 
375bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3769751ee09SNobuhiro Iwamatsu {
377bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
378bd1024b0SYoshihiro Shimoda 	u32 val;
379bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
380c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
381bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3829751ee09SNobuhiro Iwamatsu 
3839751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
384f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
385f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
386f8b7507dSNobuhiro Iwamatsu 
38749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
38849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
38949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
39049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
39149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
39262cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
39349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
394903de461SYoshihiro Shimoda #endif
39549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3969751ee09SNobuhiro Iwamatsu 
3979751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
39849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3999751ee09SNobuhiro Iwamatsu 
4009751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
401c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
402c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
40349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
4049751ee09SNobuhiro Iwamatsu 
405c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
40649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
4079751ee09SNobuhiro Iwamatsu 
40849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
40926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
41049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
41162cbddc4SNobuhiro Iwamatsu #endif
41262cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
41349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
41449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
41549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
4163bb4cc31SNobuhiro Iwamatsu #endif
4173bb4cc31SNobuhiro Iwamatsu 
418dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
41949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
42017243747SNobuhiro Iwamatsu #elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
421a341b7e0SNobuhiro Iwamatsu 	defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
4228707678cSNobuhiro Iwamatsu 	sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
4234398d559SNobuhiro Iwamatsu #endif
4249751ee09SNobuhiro Iwamatsu 	/* Configure phy */
425bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
426bd3980ccSNobuhiro Iwamatsu 	if (ret) {
42788a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
428bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
429bd3980ccSNobuhiro Iwamatsu 	}
430bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
43111af8d65STimur Tabi 	ret = phy_startup(phy);
43211af8d65STimur Tabi 	if (ret) {
43311af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
43411af8d65STimur Tabi 		return ret;
43511af8d65STimur Tabi 	}
4369751ee09SNobuhiro Iwamatsu 
4373bb4cc31SNobuhiro Iwamatsu 	val = 0;
4383bb4cc31SNobuhiro Iwamatsu 
4399751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
440bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
441bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
44226235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
44349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
444e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
44549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
44647ce8890SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
447a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
448a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7794)
4493bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4503bb4cc31SNobuhiro Iwamatsu #endif
451bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
452bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
45326235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
45449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
455e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
45649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
457903de461SYoshihiro Shimoda #endif
4583bb4cc31SNobuhiro Iwamatsu 	}
45926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4604398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4614398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
46249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4634398d559SNobuhiro Iwamatsu 	}
4644398d559SNobuhiro Iwamatsu #endif
4659751ee09SNobuhiro Iwamatsu 
4669751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
467bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4689751ee09SNobuhiro Iwamatsu 		printf("Full\n");
46949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
47049afb8caSYoshihiro Shimoda 			     ECMR);
4719751ee09SNobuhiro Iwamatsu 	} else {
4729751ee09SNobuhiro Iwamatsu 		printf("Half\n");
47349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4749751ee09SNobuhiro Iwamatsu 	}
475bd3980ccSNobuhiro Iwamatsu 
476bd3980ccSNobuhiro Iwamatsu 	return ret;
477bd3980ccSNobuhiro Iwamatsu 
478bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
479bd3980ccSNobuhiro Iwamatsu 	return ret;
4809751ee09SNobuhiro Iwamatsu }
4819751ee09SNobuhiro Iwamatsu 
482bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4839751ee09SNobuhiro Iwamatsu {
4849751ee09SNobuhiro Iwamatsu 	/*
4859751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4869751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4879751ee09SNobuhiro Iwamatsu 	 */
48849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
489bd3980ccSNobuhiro Iwamatsu }
4909751ee09SNobuhiro Iwamatsu 
491bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
492bd3980ccSNobuhiro Iwamatsu {
49349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4949751ee09SNobuhiro Iwamatsu }
4959751ee09SNobuhiro Iwamatsu 
496bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4979751ee09SNobuhiro Iwamatsu {
498bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
499bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
500bd3980ccSNobuhiro Iwamatsu 
501bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
502bd3980ccSNobuhiro Iwamatsu 	if (ret)
503bd3980ccSNobuhiro Iwamatsu 		goto err;
504bd3980ccSNobuhiro Iwamatsu 
505bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
506bd3980ccSNobuhiro Iwamatsu 	if (ret)
507bd3980ccSNobuhiro Iwamatsu 		goto err;
508bd3980ccSNobuhiro Iwamatsu 
509bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
510bd3980ccSNobuhiro Iwamatsu 	if (ret)
511bd3980ccSNobuhiro Iwamatsu 		goto err_config;
512bd3980ccSNobuhiro Iwamatsu 
513bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
514bd3980ccSNobuhiro Iwamatsu 
515bd3980ccSNobuhiro Iwamatsu 	return ret;
516bd3980ccSNobuhiro Iwamatsu 
517bd3980ccSNobuhiro Iwamatsu err_config:
518bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
519bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
520bd3980ccSNobuhiro Iwamatsu 
521bd3980ccSNobuhiro Iwamatsu err:
522bd3980ccSNobuhiro Iwamatsu 	return ret;
5239751ee09SNobuhiro Iwamatsu }
5249751ee09SNobuhiro Iwamatsu 
525bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
526bd3980ccSNobuhiro Iwamatsu {
527bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
528bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
529bd3980ccSNobuhiro Iwamatsu }
530bd3980ccSNobuhiro Iwamatsu 
531bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
532bd3980ccSNobuhiro Iwamatsu {
533bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
534bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
535bd3980ccSNobuhiro Iwamatsu 	struct eth_device *dev = NULL;
536bd3980ccSNobuhiro Iwamatsu 
537bd3980ccSNobuhiro Iwamatsu 	eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
538bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
539bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
540bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
541bd3980ccSNobuhiro Iwamatsu 		goto err;
542bd3980ccSNobuhiro Iwamatsu 	}
543bd3980ccSNobuhiro Iwamatsu 
544bd3980ccSNobuhiro Iwamatsu 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
545bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
546bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
547bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
548bd3980ccSNobuhiro Iwamatsu 		goto err;
549bd3980ccSNobuhiro Iwamatsu 	}
550bd3980ccSNobuhiro Iwamatsu 	memset(dev, 0, sizeof(struct eth_device));
551bd3980ccSNobuhiro Iwamatsu 	memset(eth, 0, sizeof(struct sh_eth_dev));
552bd3980ccSNobuhiro Iwamatsu 
553bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
554bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
555bd3980ccSNobuhiro Iwamatsu 
556bd3980ccSNobuhiro Iwamatsu 	dev->priv = (void *)eth;
557bd3980ccSNobuhiro Iwamatsu 	dev->iobase = 0;
558bd3980ccSNobuhiro Iwamatsu 	dev->init = sh_eth_init;
559bd3980ccSNobuhiro Iwamatsu 	dev->halt = sh_eth_halt;
560bd3980ccSNobuhiro Iwamatsu 	dev->send = sh_eth_send;
561bd3980ccSNobuhiro Iwamatsu 	dev->recv = sh_eth_recv;
562bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].dev = dev;
563bd3980ccSNobuhiro Iwamatsu 
564bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
565bd3980ccSNobuhiro Iwamatsu 
566bd3980ccSNobuhiro Iwamatsu 	/* Register Device to EtherNet subsystem  */
567bd3980ccSNobuhiro Iwamatsu 	eth_register(dev);
5689751ee09SNobuhiro Iwamatsu 
569bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
570bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
571bd1024b0SYoshihiro Shimoda 
572c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
573c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5749751ee09SNobuhiro Iwamatsu 
575bd3980ccSNobuhiro Iwamatsu 	return ret;
5769751ee09SNobuhiro Iwamatsu 
5779751ee09SNobuhiro Iwamatsu err:
578bd3980ccSNobuhiro Iwamatsu 	if (dev)
5799751ee09SNobuhiro Iwamatsu 		free(dev);
580bd3980ccSNobuhiro Iwamatsu 
581bd3980ccSNobuhiro Iwamatsu 	if (eth)
582bd3980ccSNobuhiro Iwamatsu 		free(eth);
583bd3980ccSNobuhiro Iwamatsu 
584bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
585bd3980ccSNobuhiro Iwamatsu 	return ret;
5869751ee09SNobuhiro Iwamatsu }
587bd1024b0SYoshihiro Shimoda 
588bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
589bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
590bd1024b0SYoshihiro Shimoda {
591bd1024b0SYoshihiro Shimoda 	return 0;
592bd1024b0SYoshihiro Shimoda }
593bd1024b0SYoshihiro Shimoda 
594bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
595bd1024b0SYoshihiro Shimoda {
596bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
597bd1024b0SYoshihiro Shimoda 
59849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
599bd1024b0SYoshihiro Shimoda 
600bd1024b0SYoshihiro Shimoda 	return 0;
601bd1024b0SYoshihiro Shimoda }
602bd1024b0SYoshihiro Shimoda 
603bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
604bd1024b0SYoshihiro Shimoda {
605bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
606bd1024b0SYoshihiro Shimoda 
60749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
608bd1024b0SYoshihiro Shimoda 
609bd1024b0SYoshihiro Shimoda 	return 0;
610bd1024b0SYoshihiro Shimoda }
611bd1024b0SYoshihiro Shimoda 
612bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
613bd1024b0SYoshihiro Shimoda {
614bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
615bd1024b0SYoshihiro Shimoda 
616bd1024b0SYoshihiro Shimoda 	if (v)
61749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
618bd1024b0SYoshihiro Shimoda 	else
61949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
620bd1024b0SYoshihiro Shimoda 
621bd1024b0SYoshihiro Shimoda 	return 0;
622bd1024b0SYoshihiro Shimoda }
623bd1024b0SYoshihiro Shimoda 
624bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
625bd1024b0SYoshihiro Shimoda {
626bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
627bd1024b0SYoshihiro Shimoda 
62849afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
629bd1024b0SYoshihiro Shimoda 
630bd1024b0SYoshihiro Shimoda 	return 0;
631bd1024b0SYoshihiro Shimoda }
632bd1024b0SYoshihiro Shimoda 
633bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
634bd1024b0SYoshihiro Shimoda {
635bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
636bd1024b0SYoshihiro Shimoda 
637bd1024b0SYoshihiro Shimoda 	if (v)
63849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
639bd1024b0SYoshihiro Shimoda 	else
64049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
641bd1024b0SYoshihiro Shimoda 
642bd1024b0SYoshihiro Shimoda 	return 0;
643bd1024b0SYoshihiro Shimoda }
644bd1024b0SYoshihiro Shimoda 
645bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
646bd1024b0SYoshihiro Shimoda {
647bd1024b0SYoshihiro Shimoda 	udelay(10);
648bd1024b0SYoshihiro Shimoda 
649bd1024b0SYoshihiro Shimoda 	return 0;
650bd1024b0SYoshihiro Shimoda }
651bd1024b0SYoshihiro Shimoda 
652bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
653bd1024b0SYoshihiro Shimoda 	{
654bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
655bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
656bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
657bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
658bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
659bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
660bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
661bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
662bd1024b0SYoshihiro Shimoda 	}
663bd1024b0SYoshihiro Shimoda };
664bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
665