xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 92f0713408859952cf82a9a4f368b2459cd1619f)
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>
79751ee09SNobuhiro Iwamatsu  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
99751ee09SNobuhiro Iwamatsu  */
109751ee09SNobuhiro Iwamatsu 
119751ee09SNobuhiro Iwamatsu #include <config.h>
129751ee09SNobuhiro Iwamatsu #include <common.h>
139751ee09SNobuhiro Iwamatsu #include <malloc.h>
149751ee09SNobuhiro Iwamatsu #include <net.h>
15bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
16bd1024b0SYoshihiro Shimoda #include <miiphy.h>
179751ee09SNobuhiro Iwamatsu #include <asm/errno.h>
189751ee09SNobuhiro Iwamatsu #include <asm/io.h>
199751ee09SNobuhiro Iwamatsu 
209751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
219751ee09SNobuhiro Iwamatsu 
229751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
239751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
249751ee09SNobuhiro Iwamatsu #endif
259751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
269751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
279751ee09SNobuhiro Iwamatsu #endif
28870cc23fSNobuhiro Iwamatsu 
29*92f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
3068260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)    \
31870cc23fSNobuhiro Iwamatsu 		flush_dcache_range((u32)addr, (u32)(addr + len - 1))
3268260aabSYoshihiro Shimoda #else
3368260aabSYoshihiro Shimoda #define flush_cache_wback(...)
3468260aabSYoshihiro Shimoda #endif
359751ee09SNobuhiro Iwamatsu 
36*92f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
37*92f07134SNobuhiro Iwamatsu #define invalidate_cache(addr, len)		\
38*92f07134SNobuhiro Iwamatsu 	{	\
39*92f07134SNobuhiro Iwamatsu 		u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE;	\
40*92f07134SNobuhiro Iwamatsu 		u32 start, end;	\
41*92f07134SNobuhiro Iwamatsu 		\
42*92f07134SNobuhiro Iwamatsu 		start = (u32)addr;	\
43*92f07134SNobuhiro Iwamatsu 		end = start + len;	\
44*92f07134SNobuhiro Iwamatsu 		start &= ~(line_size - 1);	\
45*92f07134SNobuhiro Iwamatsu 		end = ((end + line_size - 1) & ~(line_size - 1));	\
46*92f07134SNobuhiro Iwamatsu 		\
47*92f07134SNobuhiro Iwamatsu 		invalidate_dcache_range(start, end);	\
48*92f07134SNobuhiro Iwamatsu 	}
49*92f07134SNobuhiro Iwamatsu #else
50*92f07134SNobuhiro Iwamatsu #define invalidate_cache(...)
51*92f07134SNobuhiro Iwamatsu #endif
52*92f07134SNobuhiro Iwamatsu 
534ba62c72SNobuhiro Iwamatsu #define TIMEOUT_CNT 1000
544ba62c72SNobuhiro Iwamatsu 
5510cbe3b6SJoe Hershberger int sh_eth_send(struct eth_device *dev, void *packet, int len)
569751ee09SNobuhiro Iwamatsu {
57bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
58bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0, timeout;
59bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
609751ee09SNobuhiro Iwamatsu 
619751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
62bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
63bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
64bd3980ccSNobuhiro Iwamatsu 		goto err;
659751ee09SNobuhiro Iwamatsu 	}
669751ee09SNobuhiro Iwamatsu 
679751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
68ee6ec5d4SNobuhiro Iwamatsu 	if ((int)packet & 3) {
69bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
70bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
71bd3980ccSNobuhiro Iwamatsu 		goto err;
729751ee09SNobuhiro Iwamatsu 	}
739751ee09SNobuhiro Iwamatsu 
749751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
7568260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
769751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
779751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
789751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
799751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
809751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
819751ee09SNobuhiro Iwamatsu 	else
829751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
839751ee09SNobuhiro Iwamatsu 
849751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
8549afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
8649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
879751ee09SNobuhiro Iwamatsu 
889751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
894ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
90*92f07134SNobuhiro Iwamatsu 	do {
91*92f07134SNobuhiro Iwamatsu 		invalidate_cache(port_info->tx_desc_cur,
92*92f07134SNobuhiro Iwamatsu 				 sizeof(struct tx_desc_s));
939751ee09SNobuhiro Iwamatsu 		udelay(100);
94*92f07134SNobuhiro Iwamatsu 	} while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
959751ee09SNobuhiro Iwamatsu 
969751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
97bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
98bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
999751ee09SNobuhiro Iwamatsu 		goto err;
1009751ee09SNobuhiro Iwamatsu 	}
1019751ee09SNobuhiro Iwamatsu 
1029751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
1039751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
1049751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
1059751ee09SNobuhiro Iwamatsu 
106bd3980ccSNobuhiro Iwamatsu err:
107bd3980ccSNobuhiro Iwamatsu 	return ret;
1089751ee09SNobuhiro Iwamatsu }
1099751ee09SNobuhiro Iwamatsu 
110bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1119751ee09SNobuhiro Iwamatsu {
112bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
113bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
114bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
11510cbe3b6SJoe Hershberger 	uchar *packet;
1169751ee09SNobuhiro Iwamatsu 
1179751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
118*92f07134SNobuhiro Iwamatsu 	invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
1199751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1209751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1219751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1229751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
12310cbe3b6SJoe Hershberger 			packet = (uchar *)
1249751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
125*92f07134SNobuhiro Iwamatsu 			invalidate_cache(packet, len);
1269751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1279751ee09SNobuhiro Iwamatsu 		}
1289751ee09SNobuhiro Iwamatsu 
1299751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1309751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1319751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1329751ee09SNobuhiro Iwamatsu 		else
1339751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1349751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1359751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1369751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1379751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1389751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1399751ee09SNobuhiro Iwamatsu 	}
1409751ee09SNobuhiro Iwamatsu 
1419751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
14249afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
14349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1449751ee09SNobuhiro Iwamatsu 
1459751ee09SNobuhiro Iwamatsu 	return len;
1469751ee09SNobuhiro Iwamatsu }
1479751ee09SNobuhiro Iwamatsu 
148bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1499751ee09SNobuhiro Iwamatsu {
15026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
151bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1529751ee09SNobuhiro Iwamatsu 
1539751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
15449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1559751ee09SNobuhiro Iwamatsu 
1569751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
15749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1584ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT ; i++) {
15949afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1609751ee09SNobuhiro Iwamatsu 			break;
1619751ee09SNobuhiro Iwamatsu 		udelay(1000);
1629751ee09SNobuhiro Iwamatsu 	}
1639751ee09SNobuhiro Iwamatsu 
1644ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
165bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
166bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1679751ee09SNobuhiro Iwamatsu 	}
1689751ee09SNobuhiro Iwamatsu 
169bd3980ccSNobuhiro Iwamatsu 	return ret;
170903de461SYoshihiro Shimoda #else
17149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
172903de461SYoshihiro Shimoda 	udelay(3000);
17349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
174903de461SYoshihiro Shimoda 
175903de461SYoshihiro Shimoda 	return 0;
176903de461SYoshihiro Shimoda #endif
177bd3980ccSNobuhiro Iwamatsu }
178bd3980ccSNobuhiro Iwamatsu 
179bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1809751ee09SNobuhiro Iwamatsu {
181bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1829751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
183bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1849751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1859751ee09SNobuhiro Iwamatsu 
186bd3980ccSNobuhiro Iwamatsu 	/*
187bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
188bd3980ccSNobuhiro Iwamatsu 	 */
189bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1909751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
191bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
192bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
193bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
194bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
195bd3980ccSNobuhiro Iwamatsu 		goto err;
1969751ee09SNobuhiro Iwamatsu 	}
197bd3980ccSNobuhiro Iwamatsu 
1989751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
1999751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
20068260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
2019751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2029751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
2039751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2049751ee09SNobuhiro Iwamatsu 
2059751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2069751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2079751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2089751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2099751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2109751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2119751ee09SNobuhiro Iwamatsu 	}
2129751ee09SNobuhiro Iwamatsu 
2139751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2149751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2159751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2169751ee09SNobuhiro Iwamatsu 
2179751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2189751ee09SNobuhiro Iwamatsu 	   addresses */
21949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
22026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
22149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
22249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
22349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
224903de461SYoshihiro Shimoda #endif
2259751ee09SNobuhiro Iwamatsu 
226bd3980ccSNobuhiro Iwamatsu err:
227bd3980ccSNobuhiro Iwamatsu 	return ret;
2289751ee09SNobuhiro Iwamatsu }
2299751ee09SNobuhiro Iwamatsu 
230bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2319751ee09SNobuhiro Iwamatsu {
232bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
233bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2349751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
235bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2369751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2379751ee09SNobuhiro Iwamatsu 
238bd3980ccSNobuhiro Iwamatsu 	/*
239bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
240bd3980ccSNobuhiro Iwamatsu 	 */
241bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2429751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
243bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
244bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
245bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
246bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
247bd3980ccSNobuhiro Iwamatsu 		goto err;
2489751ee09SNobuhiro Iwamatsu 	}
249bd3980ccSNobuhiro Iwamatsu 
2509751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2519751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
25268260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2539751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2549751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2559751ee09SNobuhiro Iwamatsu 
2569751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2579751ee09SNobuhiro Iwamatsu 
258bd3980ccSNobuhiro Iwamatsu 	/*
259bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
260bd3980ccSNobuhiro Iwamatsu 	 * P2 area
261bd3980ccSNobuhiro Iwamatsu 	 */
262f8b7507dSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(
263f8b7507dSNobuhiro Iwamatsu 		NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
264bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
265bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
266bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
267bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2689751ee09SNobuhiro Iwamatsu 	}
269bd3980ccSNobuhiro Iwamatsu 
270f8b7507dSNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc
271f8b7507dSNobuhiro Iwamatsu 			  + (RX_BUF_ALIGNE_SIZE - 1)) &
272f8b7507dSNobuhiro Iwamatsu 			  ~(RX_BUF_ALIGNE_SIZE - 1));
2739751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2749751ee09SNobuhiro Iwamatsu 
2759751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2769751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2779751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2789751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2799751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2809751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2819751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2829751ee09SNobuhiro Iwamatsu 	}
2839751ee09SNobuhiro Iwamatsu 
2849751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2859751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2869751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2879751ee09SNobuhiro Iwamatsu 
2889751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
28949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
29026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
29149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
29249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
29349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
294903de461SYoshihiro Shimoda #endif
2959751ee09SNobuhiro Iwamatsu 
296bd3980ccSNobuhiro Iwamatsu 	return ret;
297bd3980ccSNobuhiro Iwamatsu 
298bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
299bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
300bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
301bd3980ccSNobuhiro Iwamatsu 
302bd3980ccSNobuhiro Iwamatsu err:
303bd3980ccSNobuhiro Iwamatsu 	return ret;
3049751ee09SNobuhiro Iwamatsu }
3059751ee09SNobuhiro Iwamatsu 
306bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3079751ee09SNobuhiro Iwamatsu {
308bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
309bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3109751ee09SNobuhiro Iwamatsu 
3119751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3129751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3139751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3149751ee09SNobuhiro Iwamatsu 	}
315bd3980ccSNobuhiro Iwamatsu }
316bd3980ccSNobuhiro Iwamatsu 
317bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
318bd3980ccSNobuhiro Iwamatsu {
319bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
320bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3219751ee09SNobuhiro Iwamatsu 
3229751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3239751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3249751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3259751ee09SNobuhiro Iwamatsu 	}
3269751ee09SNobuhiro Iwamatsu 
3279751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3289751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3299751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3309751ee09SNobuhiro Iwamatsu 	}
3319751ee09SNobuhiro Iwamatsu }
3329751ee09SNobuhiro Iwamatsu 
333bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3349751ee09SNobuhiro Iwamatsu {
335bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3369751ee09SNobuhiro Iwamatsu 
337bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
338bd3980ccSNobuhiro Iwamatsu 	if (ret)
339bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
340bd3980ccSNobuhiro Iwamatsu 
341bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
342bd3980ccSNobuhiro Iwamatsu 	if (ret)
343bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
344bd3980ccSNobuhiro Iwamatsu 
345bd3980ccSNobuhiro Iwamatsu 	return ret;
346bd3980ccSNobuhiro Iwamatsu err_rx_init:
347bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
348bd3980ccSNobuhiro Iwamatsu 
349bd3980ccSNobuhiro Iwamatsu err_tx_init:
350bd3980ccSNobuhiro Iwamatsu 	return ret;
3519751ee09SNobuhiro Iwamatsu }
3529751ee09SNobuhiro Iwamatsu 
353bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3549751ee09SNobuhiro Iwamatsu {
355bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
356bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
357bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
358bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
359bd3980ccSNobuhiro Iwamatsu 
360ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
361ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3624398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
363bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
364bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
365bd3980ccSNobuhiro Iwamatsu 
366bd3980ccSNobuhiro Iwamatsu 	return ret;
3679751ee09SNobuhiro Iwamatsu }
3689751ee09SNobuhiro Iwamatsu 
369bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3709751ee09SNobuhiro Iwamatsu {
371bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
372bd1024b0SYoshihiro Shimoda 	u32 val;
373bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
374c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
375bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3769751ee09SNobuhiro Iwamatsu 
3779751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
378f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
379f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
380f8b7507dSNobuhiro Iwamatsu 
38149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
38249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
38349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
38449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
38549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
38626235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
38749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
388903de461SYoshihiro Shimoda #endif
38949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3909751ee09SNobuhiro Iwamatsu 
3919751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
39249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3939751ee09SNobuhiro Iwamatsu 
3949751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
395c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
396c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
39749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
3989751ee09SNobuhiro Iwamatsu 
399c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
40049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
4019751ee09SNobuhiro Iwamatsu 
40249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
40326235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
40449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
40549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
40649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
40749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
4083bb4cc31SNobuhiro Iwamatsu #endif
4093bb4cc31SNobuhiro Iwamatsu 
410dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
41149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
4124398d559SNobuhiro Iwamatsu #endif
4139751ee09SNobuhiro Iwamatsu 	/* Configure phy */
414bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
415bd3980ccSNobuhiro Iwamatsu 	if (ret) {
41688a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
417bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
418bd3980ccSNobuhiro Iwamatsu 	}
419bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
42011af8d65STimur Tabi 	ret = phy_startup(phy);
42111af8d65STimur Tabi 	if (ret) {
42211af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
42311af8d65STimur Tabi 		return ret;
42411af8d65STimur Tabi 	}
4259751ee09SNobuhiro Iwamatsu 
4263bb4cc31SNobuhiro Iwamatsu 	val = 0;
4273bb4cc31SNobuhiro Iwamatsu 
4289751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
429bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
430bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
43126235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
43249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
433e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
43449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
4353bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724)
4363bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4373bb4cc31SNobuhiro Iwamatsu #endif
438bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
439bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
44026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
44149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
442e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
44349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
444903de461SYoshihiro Shimoda #endif
4453bb4cc31SNobuhiro Iwamatsu 	}
44626235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4474398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4484398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
44949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4504398d559SNobuhiro Iwamatsu 	}
4514398d559SNobuhiro Iwamatsu #endif
4529751ee09SNobuhiro Iwamatsu 
4539751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
454bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4559751ee09SNobuhiro Iwamatsu 		printf("Full\n");
45649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
45749afb8caSYoshihiro Shimoda 			     ECMR);
4589751ee09SNobuhiro Iwamatsu 	} else {
4599751ee09SNobuhiro Iwamatsu 		printf("Half\n");
46049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4619751ee09SNobuhiro Iwamatsu 	}
462bd3980ccSNobuhiro Iwamatsu 
463bd3980ccSNobuhiro Iwamatsu 	return ret;
464bd3980ccSNobuhiro Iwamatsu 
465bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
466bd3980ccSNobuhiro Iwamatsu 	return ret;
4679751ee09SNobuhiro Iwamatsu }
4689751ee09SNobuhiro Iwamatsu 
469bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4709751ee09SNobuhiro Iwamatsu {
4719751ee09SNobuhiro Iwamatsu 	/*
4729751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4739751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4749751ee09SNobuhiro Iwamatsu 	 */
47549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
476bd3980ccSNobuhiro Iwamatsu }
4779751ee09SNobuhiro Iwamatsu 
478bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
479bd3980ccSNobuhiro Iwamatsu {
48049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4819751ee09SNobuhiro Iwamatsu }
4829751ee09SNobuhiro Iwamatsu 
483bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4849751ee09SNobuhiro Iwamatsu {
485bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
486bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
487bd3980ccSNobuhiro Iwamatsu 
488bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
489bd3980ccSNobuhiro Iwamatsu 	if (ret)
490bd3980ccSNobuhiro Iwamatsu 		goto err;
491bd3980ccSNobuhiro Iwamatsu 
492bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
493bd3980ccSNobuhiro Iwamatsu 	if (ret)
494bd3980ccSNobuhiro Iwamatsu 		goto err;
495bd3980ccSNobuhiro Iwamatsu 
496bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
497bd3980ccSNobuhiro Iwamatsu 	if (ret)
498bd3980ccSNobuhiro Iwamatsu 		goto err_config;
499bd3980ccSNobuhiro Iwamatsu 
500bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
501bd3980ccSNobuhiro Iwamatsu 
502bd3980ccSNobuhiro Iwamatsu 	return ret;
503bd3980ccSNobuhiro Iwamatsu 
504bd3980ccSNobuhiro Iwamatsu err_config:
505bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
506bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
507bd3980ccSNobuhiro Iwamatsu 
508bd3980ccSNobuhiro Iwamatsu err:
509bd3980ccSNobuhiro Iwamatsu 	return ret;
5109751ee09SNobuhiro Iwamatsu }
5119751ee09SNobuhiro Iwamatsu 
512bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
513bd3980ccSNobuhiro Iwamatsu {
514bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
515bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
516bd3980ccSNobuhiro Iwamatsu }
517bd3980ccSNobuhiro Iwamatsu 
518bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
519bd3980ccSNobuhiro Iwamatsu {
520bd3980ccSNobuhiro Iwamatsu     int ret = 0;
521bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
522bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
523bd3980ccSNobuhiro Iwamatsu 
524bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
525bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
526bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
527bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
528bd3980ccSNobuhiro Iwamatsu 		goto err;
529bd3980ccSNobuhiro Iwamatsu 	}
530bd3980ccSNobuhiro Iwamatsu 
531bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
532bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
533bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
534bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
535bd3980ccSNobuhiro Iwamatsu 		goto err;
536bd3980ccSNobuhiro Iwamatsu 	}
537bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
538bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
539bd3980ccSNobuhiro Iwamatsu 
540bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
541bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
542bd3980ccSNobuhiro Iwamatsu 
543bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
544bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
545bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
546bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
547bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
548bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
549bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
550bd3980ccSNobuhiro Iwamatsu 
551bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
552bd3980ccSNobuhiro Iwamatsu 
553bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
554bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5559751ee09SNobuhiro Iwamatsu 
556bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
557bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
558bd1024b0SYoshihiro Shimoda 
559c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
560c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5619751ee09SNobuhiro Iwamatsu 
562bd3980ccSNobuhiro Iwamatsu 	return ret;
5639751ee09SNobuhiro Iwamatsu 
5649751ee09SNobuhiro Iwamatsu err:
565bd3980ccSNobuhiro Iwamatsu 	if (dev)
5669751ee09SNobuhiro Iwamatsu 		free(dev);
567bd3980ccSNobuhiro Iwamatsu 
568bd3980ccSNobuhiro Iwamatsu 	if (eth)
569bd3980ccSNobuhiro Iwamatsu 		free(eth);
570bd3980ccSNobuhiro Iwamatsu 
571bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
572bd3980ccSNobuhiro Iwamatsu 	return ret;
5739751ee09SNobuhiro Iwamatsu }
574bd1024b0SYoshihiro Shimoda 
575bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
576bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
577bd1024b0SYoshihiro Shimoda {
578bd1024b0SYoshihiro Shimoda 	return 0;
579bd1024b0SYoshihiro Shimoda }
580bd1024b0SYoshihiro Shimoda 
581bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
582bd1024b0SYoshihiro Shimoda {
583bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
584bd1024b0SYoshihiro Shimoda 
58549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
586bd1024b0SYoshihiro Shimoda 
587bd1024b0SYoshihiro Shimoda 	return 0;
588bd1024b0SYoshihiro Shimoda }
589bd1024b0SYoshihiro Shimoda 
590bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
591bd1024b0SYoshihiro Shimoda {
592bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
593bd1024b0SYoshihiro Shimoda 
59449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
595bd1024b0SYoshihiro Shimoda 
596bd1024b0SYoshihiro Shimoda 	return 0;
597bd1024b0SYoshihiro Shimoda }
598bd1024b0SYoshihiro Shimoda 
599bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
600bd1024b0SYoshihiro Shimoda {
601bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
602bd1024b0SYoshihiro Shimoda 
603bd1024b0SYoshihiro Shimoda 	if (v)
60449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
605bd1024b0SYoshihiro Shimoda 	else
60649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
607bd1024b0SYoshihiro Shimoda 
608bd1024b0SYoshihiro Shimoda 	return 0;
609bd1024b0SYoshihiro Shimoda }
610bd1024b0SYoshihiro Shimoda 
611bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
612bd1024b0SYoshihiro Shimoda {
613bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
614bd1024b0SYoshihiro Shimoda 
61549afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
616bd1024b0SYoshihiro Shimoda 
617bd1024b0SYoshihiro Shimoda 	return 0;
618bd1024b0SYoshihiro Shimoda }
619bd1024b0SYoshihiro Shimoda 
620bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
621bd1024b0SYoshihiro Shimoda {
622bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
623bd1024b0SYoshihiro Shimoda 
624bd1024b0SYoshihiro Shimoda 	if (v)
62549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
626bd1024b0SYoshihiro Shimoda 	else
62749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
628bd1024b0SYoshihiro Shimoda 
629bd1024b0SYoshihiro Shimoda 	return 0;
630bd1024b0SYoshihiro Shimoda }
631bd1024b0SYoshihiro Shimoda 
632bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
633bd1024b0SYoshihiro Shimoda {
634bd1024b0SYoshihiro Shimoda 	udelay(10);
635bd1024b0SYoshihiro Shimoda 
636bd1024b0SYoshihiro Shimoda 	return 0;
637bd1024b0SYoshihiro Shimoda }
638bd1024b0SYoshihiro Shimoda 
639bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
640bd1024b0SYoshihiro Shimoda 	{
641bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
642bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
643bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
644bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
645bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
646bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
647bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
648bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
649bd1024b0SYoshihiro Shimoda 	}
650bd1024b0SYoshihiro Shimoda };
651bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
652