xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision a341b7e0dd74826d366390fabd6f8309902cf2ae)
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 	/*
189bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
190bd3980ccSNobuhiro Iwamatsu 	 */
191bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1929751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
193bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
194bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
195bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
196bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
197bd3980ccSNobuhiro Iwamatsu 		goto err;
1989751ee09SNobuhiro Iwamatsu 	}
199bd3980ccSNobuhiro Iwamatsu 
2009751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
2019751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
20268260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
2039751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2049751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
2059751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2069751ee09SNobuhiro Iwamatsu 
2079751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2089751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2099751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2109751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2119751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2129751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2139751ee09SNobuhiro Iwamatsu 	}
2149751ee09SNobuhiro Iwamatsu 
2159751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2169751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2179751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2189751ee09SNobuhiro Iwamatsu 
2199751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2209751ee09SNobuhiro Iwamatsu 	   addresses */
22149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
22262cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
22349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
22449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
22549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
226903de461SYoshihiro Shimoda #endif
2279751ee09SNobuhiro Iwamatsu 
228bd3980ccSNobuhiro Iwamatsu err:
229bd3980ccSNobuhiro Iwamatsu 	return ret;
2309751ee09SNobuhiro Iwamatsu }
2319751ee09SNobuhiro Iwamatsu 
232bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2339751ee09SNobuhiro Iwamatsu {
234bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
235bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2369751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
237bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2389751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2399751ee09SNobuhiro Iwamatsu 
240bd3980ccSNobuhiro Iwamatsu 	/*
241bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
242bd3980ccSNobuhiro Iwamatsu 	 */
243bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2449751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
245bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
246bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
247bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
248bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
249bd3980ccSNobuhiro Iwamatsu 		goto err;
2509751ee09SNobuhiro Iwamatsu 	}
251bd3980ccSNobuhiro Iwamatsu 
2529751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2539751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
25468260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2559751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2569751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2579751ee09SNobuhiro Iwamatsu 
2589751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2599751ee09SNobuhiro Iwamatsu 
260bd3980ccSNobuhiro Iwamatsu 	/*
261bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
262bd3980ccSNobuhiro Iwamatsu 	 * P2 area
263bd3980ccSNobuhiro Iwamatsu 	 */
264f8b7507dSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(
265f8b7507dSNobuhiro Iwamatsu 		NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
266bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
267bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
268bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
269bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2709751ee09SNobuhiro Iwamatsu 	}
271bd3980ccSNobuhiro Iwamatsu 
272f8b7507dSNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc
273f8b7507dSNobuhiro Iwamatsu 			  + (RX_BUF_ALIGNE_SIZE - 1)) &
274f8b7507dSNobuhiro Iwamatsu 			  ~(RX_BUF_ALIGNE_SIZE - 1));
2759751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2769751ee09SNobuhiro Iwamatsu 
2779751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2789751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2799751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2809751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2819751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2829751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2839751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2849751ee09SNobuhiro Iwamatsu 	}
2859751ee09SNobuhiro Iwamatsu 
2869751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2879751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2889751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2899751ee09SNobuhiro Iwamatsu 
2909751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
29149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
29262cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
29349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
29449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
29549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
296903de461SYoshihiro Shimoda #endif
2979751ee09SNobuhiro Iwamatsu 
298bd3980ccSNobuhiro Iwamatsu 	return ret;
299bd3980ccSNobuhiro Iwamatsu 
300bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
301bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
302bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
303bd3980ccSNobuhiro Iwamatsu 
304bd3980ccSNobuhiro Iwamatsu err:
305bd3980ccSNobuhiro Iwamatsu 	return ret;
3069751ee09SNobuhiro Iwamatsu }
3079751ee09SNobuhiro Iwamatsu 
308bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3099751ee09SNobuhiro Iwamatsu {
310bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
311bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3129751ee09SNobuhiro Iwamatsu 
3139751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3149751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3159751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3169751ee09SNobuhiro Iwamatsu 	}
317bd3980ccSNobuhiro Iwamatsu }
318bd3980ccSNobuhiro Iwamatsu 
319bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
320bd3980ccSNobuhiro Iwamatsu {
321bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
322bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3239751ee09SNobuhiro Iwamatsu 
3249751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3259751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3269751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3279751ee09SNobuhiro Iwamatsu 	}
3289751ee09SNobuhiro Iwamatsu 
3299751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3309751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3319751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3329751ee09SNobuhiro Iwamatsu 	}
3339751ee09SNobuhiro Iwamatsu }
3349751ee09SNobuhiro Iwamatsu 
335bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3369751ee09SNobuhiro Iwamatsu {
337bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3389751ee09SNobuhiro Iwamatsu 
339bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
340bd3980ccSNobuhiro Iwamatsu 	if (ret)
341bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
342bd3980ccSNobuhiro Iwamatsu 
343bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
344bd3980ccSNobuhiro Iwamatsu 	if (ret)
345bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
346bd3980ccSNobuhiro Iwamatsu 
347bd3980ccSNobuhiro Iwamatsu 	return ret;
348bd3980ccSNobuhiro Iwamatsu err_rx_init:
349bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
350bd3980ccSNobuhiro Iwamatsu 
351bd3980ccSNobuhiro Iwamatsu err_tx_init:
352bd3980ccSNobuhiro Iwamatsu 	return ret;
3539751ee09SNobuhiro Iwamatsu }
3549751ee09SNobuhiro Iwamatsu 
355bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3569751ee09SNobuhiro Iwamatsu {
357bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
358bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
359bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
360bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
361bd3980ccSNobuhiro Iwamatsu 
362ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
363ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3644398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
365bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
366bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
367bd3980ccSNobuhiro Iwamatsu 
368bd3980ccSNobuhiro Iwamatsu 	return ret;
3699751ee09SNobuhiro Iwamatsu }
3709751ee09SNobuhiro Iwamatsu 
371bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3729751ee09SNobuhiro Iwamatsu {
373bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
374bd1024b0SYoshihiro Shimoda 	u32 val;
375bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
376c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
377bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3789751ee09SNobuhiro Iwamatsu 
3799751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
380f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
381f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
382f8b7507dSNobuhiro Iwamatsu 
38349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
38449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
38549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
38649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
38749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
38862cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
38949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
390903de461SYoshihiro Shimoda #endif
39149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3929751ee09SNobuhiro Iwamatsu 
3939751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
39449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3959751ee09SNobuhiro Iwamatsu 
3969751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
397c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
398c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
39949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
4009751ee09SNobuhiro Iwamatsu 
401c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
40249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
4039751ee09SNobuhiro Iwamatsu 
40449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
40526235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
40649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
40762cbddc4SNobuhiro Iwamatsu #endif
40862cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
40949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
41049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
41149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
4123bb4cc31SNobuhiro Iwamatsu #endif
4133bb4cc31SNobuhiro Iwamatsu 
414dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
41549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
41617243747SNobuhiro Iwamatsu #elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
417*a341b7e0SNobuhiro Iwamatsu 	defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
4188707678cSNobuhiro Iwamatsu 	sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
4194398d559SNobuhiro Iwamatsu #endif
4209751ee09SNobuhiro Iwamatsu 	/* Configure phy */
421bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
422bd3980ccSNobuhiro Iwamatsu 	if (ret) {
42388a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
424bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
425bd3980ccSNobuhiro Iwamatsu 	}
426bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
42711af8d65STimur Tabi 	ret = phy_startup(phy);
42811af8d65STimur Tabi 	if (ret) {
42911af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
43011af8d65STimur Tabi 		return ret;
43111af8d65STimur Tabi 	}
4329751ee09SNobuhiro Iwamatsu 
4333bb4cc31SNobuhiro Iwamatsu 	val = 0;
4343bb4cc31SNobuhiro Iwamatsu 
4359751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
436bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
437bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
43826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
43949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
440e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
44149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
44247ce8890SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
443*a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
444*a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7794)
4453bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4463bb4cc31SNobuhiro Iwamatsu #endif
447bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
448bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
44926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
45049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
451e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
45249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
453903de461SYoshihiro Shimoda #endif
4543bb4cc31SNobuhiro Iwamatsu 	}
45526235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4564398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4574398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
45849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4594398d559SNobuhiro Iwamatsu 	}
4604398d559SNobuhiro Iwamatsu #endif
4619751ee09SNobuhiro Iwamatsu 
4629751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
463bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4649751ee09SNobuhiro Iwamatsu 		printf("Full\n");
46549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
46649afb8caSYoshihiro Shimoda 			     ECMR);
4679751ee09SNobuhiro Iwamatsu 	} else {
4689751ee09SNobuhiro Iwamatsu 		printf("Half\n");
46949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4709751ee09SNobuhiro Iwamatsu 	}
471bd3980ccSNobuhiro Iwamatsu 
472bd3980ccSNobuhiro Iwamatsu 	return ret;
473bd3980ccSNobuhiro Iwamatsu 
474bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
475bd3980ccSNobuhiro Iwamatsu 	return ret;
4769751ee09SNobuhiro Iwamatsu }
4779751ee09SNobuhiro Iwamatsu 
478bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4799751ee09SNobuhiro Iwamatsu {
4809751ee09SNobuhiro Iwamatsu 	/*
4819751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4829751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4839751ee09SNobuhiro Iwamatsu 	 */
48449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
485bd3980ccSNobuhiro Iwamatsu }
4869751ee09SNobuhiro Iwamatsu 
487bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
488bd3980ccSNobuhiro Iwamatsu {
48949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4909751ee09SNobuhiro Iwamatsu }
4919751ee09SNobuhiro Iwamatsu 
492bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4939751ee09SNobuhiro Iwamatsu {
494bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
495bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
496bd3980ccSNobuhiro Iwamatsu 
497bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
498bd3980ccSNobuhiro Iwamatsu 	if (ret)
499bd3980ccSNobuhiro Iwamatsu 		goto err;
500bd3980ccSNobuhiro Iwamatsu 
501bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
502bd3980ccSNobuhiro Iwamatsu 	if (ret)
503bd3980ccSNobuhiro Iwamatsu 		goto err;
504bd3980ccSNobuhiro Iwamatsu 
505bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
506bd3980ccSNobuhiro Iwamatsu 	if (ret)
507bd3980ccSNobuhiro Iwamatsu 		goto err_config;
508bd3980ccSNobuhiro Iwamatsu 
509bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
510bd3980ccSNobuhiro Iwamatsu 
511bd3980ccSNobuhiro Iwamatsu 	return ret;
512bd3980ccSNobuhiro Iwamatsu 
513bd3980ccSNobuhiro Iwamatsu err_config:
514bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
515bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
516bd3980ccSNobuhiro Iwamatsu 
517bd3980ccSNobuhiro Iwamatsu err:
518bd3980ccSNobuhiro Iwamatsu 	return ret;
5199751ee09SNobuhiro Iwamatsu }
5209751ee09SNobuhiro Iwamatsu 
521bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
522bd3980ccSNobuhiro Iwamatsu {
523bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
524bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
525bd3980ccSNobuhiro Iwamatsu }
526bd3980ccSNobuhiro Iwamatsu 
527bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
528bd3980ccSNobuhiro Iwamatsu {
529bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
530bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
531bd3980ccSNobuhiro Iwamatsu 	struct eth_device *dev = NULL;
532bd3980ccSNobuhiro Iwamatsu 
533bd3980ccSNobuhiro Iwamatsu 	eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
534bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
535bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
536bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
537bd3980ccSNobuhiro Iwamatsu 		goto err;
538bd3980ccSNobuhiro Iwamatsu 	}
539bd3980ccSNobuhiro Iwamatsu 
540bd3980ccSNobuhiro Iwamatsu 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
541bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
542bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
543bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
544bd3980ccSNobuhiro Iwamatsu 		goto err;
545bd3980ccSNobuhiro Iwamatsu 	}
546bd3980ccSNobuhiro Iwamatsu 	memset(dev, 0, sizeof(struct eth_device));
547bd3980ccSNobuhiro Iwamatsu 	memset(eth, 0, sizeof(struct sh_eth_dev));
548bd3980ccSNobuhiro Iwamatsu 
549bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
550bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
551bd3980ccSNobuhiro Iwamatsu 
552bd3980ccSNobuhiro Iwamatsu 	dev->priv = (void *)eth;
553bd3980ccSNobuhiro Iwamatsu 	dev->iobase = 0;
554bd3980ccSNobuhiro Iwamatsu 	dev->init = sh_eth_init;
555bd3980ccSNobuhiro Iwamatsu 	dev->halt = sh_eth_halt;
556bd3980ccSNobuhiro Iwamatsu 	dev->send = sh_eth_send;
557bd3980ccSNobuhiro Iwamatsu 	dev->recv = sh_eth_recv;
558bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].dev = dev;
559bd3980ccSNobuhiro Iwamatsu 
560bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
561bd3980ccSNobuhiro Iwamatsu 
562bd3980ccSNobuhiro Iwamatsu 	/* Register Device to EtherNet subsystem  */
563bd3980ccSNobuhiro Iwamatsu 	eth_register(dev);
5649751ee09SNobuhiro Iwamatsu 
565bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
566bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
567bd1024b0SYoshihiro Shimoda 
568c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
569c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5709751ee09SNobuhiro Iwamatsu 
571bd3980ccSNobuhiro Iwamatsu 	return ret;
5729751ee09SNobuhiro Iwamatsu 
5739751ee09SNobuhiro Iwamatsu err:
574bd3980ccSNobuhiro Iwamatsu 	if (dev)
5759751ee09SNobuhiro Iwamatsu 		free(dev);
576bd3980ccSNobuhiro Iwamatsu 
577bd3980ccSNobuhiro Iwamatsu 	if (eth)
578bd3980ccSNobuhiro Iwamatsu 		free(eth);
579bd3980ccSNobuhiro Iwamatsu 
580bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
581bd3980ccSNobuhiro Iwamatsu 	return ret;
5829751ee09SNobuhiro Iwamatsu }
583bd1024b0SYoshihiro Shimoda 
584bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
585bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
586bd1024b0SYoshihiro Shimoda {
587bd1024b0SYoshihiro Shimoda 	return 0;
588bd1024b0SYoshihiro Shimoda }
589bd1024b0SYoshihiro Shimoda 
590bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(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_mdio_tristate(struct bb_miiphy_bus *bus)
600bd1024b0SYoshihiro Shimoda {
601bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
602bd1024b0SYoshihiro Shimoda 
60349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
604bd1024b0SYoshihiro Shimoda 
605bd1024b0SYoshihiro Shimoda 	return 0;
606bd1024b0SYoshihiro Shimoda }
607bd1024b0SYoshihiro Shimoda 
608bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
609bd1024b0SYoshihiro Shimoda {
610bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
611bd1024b0SYoshihiro Shimoda 
612bd1024b0SYoshihiro Shimoda 	if (v)
61349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
614bd1024b0SYoshihiro Shimoda 	else
61549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
616bd1024b0SYoshihiro Shimoda 
617bd1024b0SYoshihiro Shimoda 	return 0;
618bd1024b0SYoshihiro Shimoda }
619bd1024b0SYoshihiro Shimoda 
620bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
621bd1024b0SYoshihiro Shimoda {
622bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
623bd1024b0SYoshihiro Shimoda 
62449afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
625bd1024b0SYoshihiro Shimoda 
626bd1024b0SYoshihiro Shimoda 	return 0;
627bd1024b0SYoshihiro Shimoda }
628bd1024b0SYoshihiro Shimoda 
629bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
630bd1024b0SYoshihiro Shimoda {
631bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
632bd1024b0SYoshihiro Shimoda 
633bd1024b0SYoshihiro Shimoda 	if (v)
63449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
635bd1024b0SYoshihiro Shimoda 	else
63649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
637bd1024b0SYoshihiro Shimoda 
638bd1024b0SYoshihiro Shimoda 	return 0;
639bd1024b0SYoshihiro Shimoda }
640bd1024b0SYoshihiro Shimoda 
641bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
642bd1024b0SYoshihiro Shimoda {
643bd1024b0SYoshihiro Shimoda 	udelay(10);
644bd1024b0SYoshihiro Shimoda 
645bd1024b0SYoshihiro Shimoda 	return 0;
646bd1024b0SYoshihiro Shimoda }
647bd1024b0SYoshihiro Shimoda 
648bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
649bd1024b0SYoshihiro Shimoda 	{
650bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
651bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
652bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
653bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
654bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
655bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
656bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
657bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
658bd1024b0SYoshihiro Shimoda 	}
659bd1024b0SYoshihiro Shimoda };
660bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
661