xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 62cbddc493e5f4c6c1e1ba62bdf36f3df4708a16)
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) {
70bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
71bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
72bd3980ccSNobuhiro Iwamatsu 		goto err;
739751ee09SNobuhiro Iwamatsu 	}
749751ee09SNobuhiro Iwamatsu 
759751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
7668260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
779751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
789751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
799751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
809751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
819751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
829751ee09SNobuhiro Iwamatsu 	else
839751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
849751ee09SNobuhiro Iwamatsu 
859751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
8649afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
8749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
889751ee09SNobuhiro Iwamatsu 
899751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
904ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
9192f07134SNobuhiro Iwamatsu 	do {
9292f07134SNobuhiro Iwamatsu 		invalidate_cache(port_info->tx_desc_cur,
9392f07134SNobuhiro Iwamatsu 				 sizeof(struct tx_desc_s));
949751ee09SNobuhiro Iwamatsu 		udelay(100);
9592f07134SNobuhiro Iwamatsu 	} while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
969751ee09SNobuhiro Iwamatsu 
979751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
98bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
99bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
1009751ee09SNobuhiro Iwamatsu 		goto err;
1019751ee09SNobuhiro Iwamatsu 	}
1029751ee09SNobuhiro Iwamatsu 
1039751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
1049751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
1059751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
1069751ee09SNobuhiro Iwamatsu 
107bd3980ccSNobuhiro Iwamatsu err:
108bd3980ccSNobuhiro Iwamatsu 	return ret;
1099751ee09SNobuhiro Iwamatsu }
1109751ee09SNobuhiro Iwamatsu 
111bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1129751ee09SNobuhiro Iwamatsu {
113bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
114bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
115bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
11610cbe3b6SJoe Hershberger 	uchar *packet;
1179751ee09SNobuhiro Iwamatsu 
1189751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
11992f07134SNobuhiro Iwamatsu 	invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
1209751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1219751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1229751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1239751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
12410cbe3b6SJoe Hershberger 			packet = (uchar *)
1259751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
12692f07134SNobuhiro Iwamatsu 			invalidate_cache(packet, len);
1279751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1289751ee09SNobuhiro Iwamatsu 		}
1299751ee09SNobuhiro Iwamatsu 
1309751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1319751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1329751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1339751ee09SNobuhiro Iwamatsu 		else
1349751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1359751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1369751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1379751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1389751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1399751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1409751ee09SNobuhiro Iwamatsu 	}
1419751ee09SNobuhiro Iwamatsu 
1429751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
14349afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
14449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1459751ee09SNobuhiro Iwamatsu 
1469751ee09SNobuhiro Iwamatsu 	return len;
1479751ee09SNobuhiro Iwamatsu }
1489751ee09SNobuhiro Iwamatsu 
149bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1509751ee09SNobuhiro Iwamatsu {
151*62cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
152bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1539751ee09SNobuhiro Iwamatsu 
1549751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
15549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1569751ee09SNobuhiro Iwamatsu 
1579751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
15849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1594ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT ; i++) {
16049afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1619751ee09SNobuhiro Iwamatsu 			break;
1629751ee09SNobuhiro Iwamatsu 		udelay(1000);
1639751ee09SNobuhiro Iwamatsu 	}
1649751ee09SNobuhiro Iwamatsu 
1654ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
166bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
167bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1689751ee09SNobuhiro Iwamatsu 	}
1699751ee09SNobuhiro Iwamatsu 
170bd3980ccSNobuhiro Iwamatsu 	return ret;
171903de461SYoshihiro Shimoda #else
17249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
173903de461SYoshihiro Shimoda 	udelay(3000);
17449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
175903de461SYoshihiro Shimoda 
176903de461SYoshihiro Shimoda 	return 0;
177903de461SYoshihiro Shimoda #endif
178bd3980ccSNobuhiro Iwamatsu }
179bd3980ccSNobuhiro Iwamatsu 
180bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1819751ee09SNobuhiro Iwamatsu {
182bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1839751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
184bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1859751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1869751ee09SNobuhiro Iwamatsu 
187bd3980ccSNobuhiro Iwamatsu 	/*
188bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
189bd3980ccSNobuhiro Iwamatsu 	 */
190bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1919751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
192bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
193bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
194bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
195bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
196bd3980ccSNobuhiro Iwamatsu 		goto err;
1979751ee09SNobuhiro Iwamatsu 	}
198bd3980ccSNobuhiro Iwamatsu 
1999751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
2009751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
20168260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
2029751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2039751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
2049751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2059751ee09SNobuhiro Iwamatsu 
2069751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2079751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2089751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2099751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2109751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2119751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2129751ee09SNobuhiro Iwamatsu 	}
2139751ee09SNobuhiro Iwamatsu 
2149751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2159751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2169751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2179751ee09SNobuhiro Iwamatsu 
2189751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2199751ee09SNobuhiro Iwamatsu 	   addresses */
22049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
221*62cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
22249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
22349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
22449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
225903de461SYoshihiro Shimoda #endif
2269751ee09SNobuhiro Iwamatsu 
227bd3980ccSNobuhiro Iwamatsu err:
228bd3980ccSNobuhiro Iwamatsu 	return ret;
2299751ee09SNobuhiro Iwamatsu }
2309751ee09SNobuhiro Iwamatsu 
231bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2329751ee09SNobuhiro Iwamatsu {
233bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
234bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2359751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
236bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2379751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2389751ee09SNobuhiro Iwamatsu 
239bd3980ccSNobuhiro Iwamatsu 	/*
240bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
241bd3980ccSNobuhiro Iwamatsu 	 */
242bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2439751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
244bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
245bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
246bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
247bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
248bd3980ccSNobuhiro Iwamatsu 		goto err;
2499751ee09SNobuhiro Iwamatsu 	}
250bd3980ccSNobuhiro Iwamatsu 
2519751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2529751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
25368260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2549751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2559751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2569751ee09SNobuhiro Iwamatsu 
2579751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2589751ee09SNobuhiro Iwamatsu 
259bd3980ccSNobuhiro Iwamatsu 	/*
260bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
261bd3980ccSNobuhiro Iwamatsu 	 * P2 area
262bd3980ccSNobuhiro Iwamatsu 	 */
263f8b7507dSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(
264f8b7507dSNobuhiro Iwamatsu 		NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
265bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
266bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
267bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
268bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2699751ee09SNobuhiro Iwamatsu 	}
270bd3980ccSNobuhiro Iwamatsu 
271f8b7507dSNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc
272f8b7507dSNobuhiro Iwamatsu 			  + (RX_BUF_ALIGNE_SIZE - 1)) &
273f8b7507dSNobuhiro Iwamatsu 			  ~(RX_BUF_ALIGNE_SIZE - 1));
2749751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2759751ee09SNobuhiro Iwamatsu 
2769751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2779751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2789751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2799751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2809751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2819751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2829751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2839751ee09SNobuhiro Iwamatsu 	}
2849751ee09SNobuhiro Iwamatsu 
2859751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2869751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2879751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2889751ee09SNobuhiro Iwamatsu 
2899751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
29049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
291*62cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
29249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
29349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
29449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
295903de461SYoshihiro Shimoda #endif
2969751ee09SNobuhiro Iwamatsu 
297bd3980ccSNobuhiro Iwamatsu 	return ret;
298bd3980ccSNobuhiro Iwamatsu 
299bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
300bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
301bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
302bd3980ccSNobuhiro Iwamatsu 
303bd3980ccSNobuhiro Iwamatsu err:
304bd3980ccSNobuhiro Iwamatsu 	return ret;
3059751ee09SNobuhiro Iwamatsu }
3069751ee09SNobuhiro Iwamatsu 
307bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3089751ee09SNobuhiro Iwamatsu {
309bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
310bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3119751ee09SNobuhiro Iwamatsu 
3129751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3139751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3149751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3159751ee09SNobuhiro Iwamatsu 	}
316bd3980ccSNobuhiro Iwamatsu }
317bd3980ccSNobuhiro Iwamatsu 
318bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
319bd3980ccSNobuhiro Iwamatsu {
320bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
321bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3229751ee09SNobuhiro Iwamatsu 
3239751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3249751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3259751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3269751ee09SNobuhiro Iwamatsu 	}
3279751ee09SNobuhiro Iwamatsu 
3289751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3299751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3309751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3319751ee09SNobuhiro Iwamatsu 	}
3329751ee09SNobuhiro Iwamatsu }
3339751ee09SNobuhiro Iwamatsu 
334bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3359751ee09SNobuhiro Iwamatsu {
336bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3379751ee09SNobuhiro Iwamatsu 
338bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
339bd3980ccSNobuhiro Iwamatsu 	if (ret)
340bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
341bd3980ccSNobuhiro Iwamatsu 
342bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
343bd3980ccSNobuhiro Iwamatsu 	if (ret)
344bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
345bd3980ccSNobuhiro Iwamatsu 
346bd3980ccSNobuhiro Iwamatsu 	return ret;
347bd3980ccSNobuhiro Iwamatsu err_rx_init:
348bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
349bd3980ccSNobuhiro Iwamatsu 
350bd3980ccSNobuhiro Iwamatsu err_tx_init:
351bd3980ccSNobuhiro Iwamatsu 	return ret;
3529751ee09SNobuhiro Iwamatsu }
3539751ee09SNobuhiro Iwamatsu 
354bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3559751ee09SNobuhiro Iwamatsu {
356bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
357bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
358bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
359bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
360bd3980ccSNobuhiro Iwamatsu 
361ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
362ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3634398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
364bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
365bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
366bd3980ccSNobuhiro Iwamatsu 
367bd3980ccSNobuhiro Iwamatsu 	return ret;
3689751ee09SNobuhiro Iwamatsu }
3699751ee09SNobuhiro Iwamatsu 
370bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3719751ee09SNobuhiro Iwamatsu {
372bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
373bd1024b0SYoshihiro Shimoda 	u32 val;
374bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
375c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
376bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3779751ee09SNobuhiro Iwamatsu 
3789751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
379f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
380f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
381f8b7507dSNobuhiro Iwamatsu 
38249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
38349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
38449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
38549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
38649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
387*62cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
38849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
389903de461SYoshihiro Shimoda #endif
39049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3919751ee09SNobuhiro Iwamatsu 
3929751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
39349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3949751ee09SNobuhiro Iwamatsu 
3959751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
396c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
397c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
39849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
3999751ee09SNobuhiro Iwamatsu 
400c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
40149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
4029751ee09SNobuhiro Iwamatsu 
40349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
40426235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
40549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
406*62cbddc4SNobuhiro Iwamatsu #endif
407*62cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
40849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
40949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
41049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
4113bb4cc31SNobuhiro Iwamatsu #endif
4123bb4cc31SNobuhiro Iwamatsu 
413dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
41449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
41547ce8890SNobuhiro Iwamatsu #elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791)
4168707678cSNobuhiro Iwamatsu 	sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
4174398d559SNobuhiro Iwamatsu #endif
4189751ee09SNobuhiro Iwamatsu 	/* Configure phy */
419bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
420bd3980ccSNobuhiro Iwamatsu 	if (ret) {
42188a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
422bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
423bd3980ccSNobuhiro Iwamatsu 	}
424bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
42511af8d65STimur Tabi 	ret = phy_startup(phy);
42611af8d65STimur Tabi 	if (ret) {
42711af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
42811af8d65STimur Tabi 		return ret;
42911af8d65STimur Tabi 	}
4309751ee09SNobuhiro Iwamatsu 
4313bb4cc31SNobuhiro Iwamatsu 	val = 0;
4323bb4cc31SNobuhiro Iwamatsu 
4339751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
434bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
435bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
43626235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
43749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
438e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
43949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
44047ce8890SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
44147ce8890SNobuhiro Iwamatsu 		defined(CONFIG_R8A7791)
4423bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4433bb4cc31SNobuhiro Iwamatsu #endif
444bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
445bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
44626235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
44749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
448e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
44949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
450903de461SYoshihiro Shimoda #endif
4513bb4cc31SNobuhiro Iwamatsu 	}
45226235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4534398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4544398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
45549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4564398d559SNobuhiro Iwamatsu 	}
4574398d559SNobuhiro Iwamatsu #endif
4589751ee09SNobuhiro Iwamatsu 
4599751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
460bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4619751ee09SNobuhiro Iwamatsu 		printf("Full\n");
46249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
46349afb8caSYoshihiro Shimoda 			     ECMR);
4649751ee09SNobuhiro Iwamatsu 	} else {
4659751ee09SNobuhiro Iwamatsu 		printf("Half\n");
46649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4679751ee09SNobuhiro Iwamatsu 	}
468bd3980ccSNobuhiro Iwamatsu 
469bd3980ccSNobuhiro Iwamatsu 	return ret;
470bd3980ccSNobuhiro Iwamatsu 
471bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
472bd3980ccSNobuhiro Iwamatsu 	return ret;
4739751ee09SNobuhiro Iwamatsu }
4749751ee09SNobuhiro Iwamatsu 
475bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4769751ee09SNobuhiro Iwamatsu {
4779751ee09SNobuhiro Iwamatsu 	/*
4789751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4799751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4809751ee09SNobuhiro Iwamatsu 	 */
48149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
482bd3980ccSNobuhiro Iwamatsu }
4839751ee09SNobuhiro Iwamatsu 
484bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
485bd3980ccSNobuhiro Iwamatsu {
48649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4879751ee09SNobuhiro Iwamatsu }
4889751ee09SNobuhiro Iwamatsu 
489bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4909751ee09SNobuhiro Iwamatsu {
491bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
492bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
493bd3980ccSNobuhiro Iwamatsu 
494bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
495bd3980ccSNobuhiro Iwamatsu 	if (ret)
496bd3980ccSNobuhiro Iwamatsu 		goto err;
497bd3980ccSNobuhiro Iwamatsu 
498bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
499bd3980ccSNobuhiro Iwamatsu 	if (ret)
500bd3980ccSNobuhiro Iwamatsu 		goto err;
501bd3980ccSNobuhiro Iwamatsu 
502bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
503bd3980ccSNobuhiro Iwamatsu 	if (ret)
504bd3980ccSNobuhiro Iwamatsu 		goto err_config;
505bd3980ccSNobuhiro Iwamatsu 
506bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
507bd3980ccSNobuhiro Iwamatsu 
508bd3980ccSNobuhiro Iwamatsu 	return ret;
509bd3980ccSNobuhiro Iwamatsu 
510bd3980ccSNobuhiro Iwamatsu err_config:
511bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
512bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
513bd3980ccSNobuhiro Iwamatsu 
514bd3980ccSNobuhiro Iwamatsu err:
515bd3980ccSNobuhiro Iwamatsu 	return ret;
5169751ee09SNobuhiro Iwamatsu }
5179751ee09SNobuhiro Iwamatsu 
518bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
519bd3980ccSNobuhiro Iwamatsu {
520bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
521bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
522bd3980ccSNobuhiro Iwamatsu }
523bd3980ccSNobuhiro Iwamatsu 
524bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
525bd3980ccSNobuhiro Iwamatsu {
526bd3980ccSNobuhiro Iwamatsu     int ret = 0;
527bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
528bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
529bd3980ccSNobuhiro Iwamatsu 
530bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
531bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
532bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
533bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
534bd3980ccSNobuhiro Iwamatsu 		goto err;
535bd3980ccSNobuhiro Iwamatsu 	}
536bd3980ccSNobuhiro Iwamatsu 
537bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
538bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
539bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
540bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
541bd3980ccSNobuhiro Iwamatsu 		goto err;
542bd3980ccSNobuhiro Iwamatsu 	}
543bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
544bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
545bd3980ccSNobuhiro Iwamatsu 
546bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
547bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
548bd3980ccSNobuhiro Iwamatsu 
549bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
550bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
551bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
552bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
553bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
554bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
555bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
556bd3980ccSNobuhiro Iwamatsu 
557bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
558bd3980ccSNobuhiro Iwamatsu 
559bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
560bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5619751ee09SNobuhiro Iwamatsu 
562bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
563bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
564bd1024b0SYoshihiro Shimoda 
565c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
566c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5679751ee09SNobuhiro Iwamatsu 
568bd3980ccSNobuhiro Iwamatsu 	return ret;
5699751ee09SNobuhiro Iwamatsu 
5709751ee09SNobuhiro Iwamatsu err:
571bd3980ccSNobuhiro Iwamatsu 	if (dev)
5729751ee09SNobuhiro Iwamatsu 		free(dev);
573bd3980ccSNobuhiro Iwamatsu 
574bd3980ccSNobuhiro Iwamatsu 	if (eth)
575bd3980ccSNobuhiro Iwamatsu 		free(eth);
576bd3980ccSNobuhiro Iwamatsu 
577bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
578bd3980ccSNobuhiro Iwamatsu 	return ret;
5799751ee09SNobuhiro Iwamatsu }
580bd1024b0SYoshihiro Shimoda 
581bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
582bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
583bd1024b0SYoshihiro Shimoda {
584bd1024b0SYoshihiro Shimoda 	return 0;
585bd1024b0SYoshihiro Shimoda }
586bd1024b0SYoshihiro Shimoda 
587bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
588bd1024b0SYoshihiro Shimoda {
589bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
590bd1024b0SYoshihiro Shimoda 
59149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
592bd1024b0SYoshihiro Shimoda 
593bd1024b0SYoshihiro Shimoda 	return 0;
594bd1024b0SYoshihiro Shimoda }
595bd1024b0SYoshihiro Shimoda 
596bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
597bd1024b0SYoshihiro Shimoda {
598bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
599bd1024b0SYoshihiro Shimoda 
60049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
601bd1024b0SYoshihiro Shimoda 
602bd1024b0SYoshihiro Shimoda 	return 0;
603bd1024b0SYoshihiro Shimoda }
604bd1024b0SYoshihiro Shimoda 
605bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
606bd1024b0SYoshihiro Shimoda {
607bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
608bd1024b0SYoshihiro Shimoda 
609bd1024b0SYoshihiro Shimoda 	if (v)
61049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
611bd1024b0SYoshihiro Shimoda 	else
61249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
613bd1024b0SYoshihiro Shimoda 
614bd1024b0SYoshihiro Shimoda 	return 0;
615bd1024b0SYoshihiro Shimoda }
616bd1024b0SYoshihiro Shimoda 
617bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
618bd1024b0SYoshihiro Shimoda {
619bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
620bd1024b0SYoshihiro Shimoda 
62149afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
622bd1024b0SYoshihiro Shimoda 
623bd1024b0SYoshihiro Shimoda 	return 0;
624bd1024b0SYoshihiro Shimoda }
625bd1024b0SYoshihiro Shimoda 
626bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
627bd1024b0SYoshihiro Shimoda {
628bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
629bd1024b0SYoshihiro Shimoda 
630bd1024b0SYoshihiro Shimoda 	if (v)
63149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
632bd1024b0SYoshihiro Shimoda 	else
63349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
634bd1024b0SYoshihiro Shimoda 
635bd1024b0SYoshihiro Shimoda 	return 0;
636bd1024b0SYoshihiro Shimoda }
637bd1024b0SYoshihiro Shimoda 
638bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
639bd1024b0SYoshihiro Shimoda {
640bd1024b0SYoshihiro Shimoda 	udelay(10);
641bd1024b0SYoshihiro Shimoda 
642bd1024b0SYoshihiro Shimoda 	return 0;
643bd1024b0SYoshihiro Shimoda }
644bd1024b0SYoshihiro Shimoda 
645bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
646bd1024b0SYoshihiro Shimoda 	{
647bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
648bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
649bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
650bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
651bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
652bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
653bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
654bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
655bd1024b0SYoshihiro Shimoda 	}
656bd1024b0SYoshihiro Shimoda };
657bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
658