xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 35affd7a2ff9a77b9946bf93b616228fcf218d60)
19751ee09SNobuhiro Iwamatsu /*
21cc0a9f4SRobert P. J. Day  * sh_eth.c - Driver for Renesas ethernet controller.
39751ee09SNobuhiro Iwamatsu  *
43bb4cc31SNobuhiro Iwamatsu  * Copyright (C) 2008, 2011 Renesas Solutions Corp.
5f7ca1f76SNobuhiro Iwamatsu  * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
69751ee09SNobuhiro Iwamatsu  * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
7f7ca1f76SNobuhiro Iwamatsu  * Copyright (C) 2013, 2014 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>
181221ce45SMasahiro Yamada #include <linux/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 
sh_eth_send(struct eth_device * dev,void * packet,int len)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 
86f7ca1f76SNobuhiro Iwamatsu 	flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
87f7ca1f76SNobuhiro Iwamatsu 
889751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
8949afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
9049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
919751ee09SNobuhiro Iwamatsu 
929751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
934ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
9492f07134SNobuhiro Iwamatsu 	do {
9592f07134SNobuhiro Iwamatsu 		invalidate_cache(port_info->tx_desc_cur,
9692f07134SNobuhiro Iwamatsu 				 sizeof(struct tx_desc_s));
979751ee09SNobuhiro Iwamatsu 		udelay(100);
9892f07134SNobuhiro Iwamatsu 	} while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
999751ee09SNobuhiro Iwamatsu 
1009751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
101bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
102bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
1039751ee09SNobuhiro Iwamatsu 		goto err;
1049751ee09SNobuhiro Iwamatsu 	}
1059751ee09SNobuhiro Iwamatsu 
1069751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
1079751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
1089751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
1099751ee09SNobuhiro Iwamatsu 
110bd3980ccSNobuhiro Iwamatsu err:
111bd3980ccSNobuhiro Iwamatsu 	return ret;
1129751ee09SNobuhiro Iwamatsu }
1139751ee09SNobuhiro Iwamatsu 
sh_eth_recv(struct eth_device * dev)114bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1159751ee09SNobuhiro Iwamatsu {
116bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
117bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
118bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
11910cbe3b6SJoe Hershberger 	uchar *packet;
1209751ee09SNobuhiro Iwamatsu 
1219751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
12292f07134SNobuhiro Iwamatsu 	invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
1239751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1249751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1259751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1269751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
12710cbe3b6SJoe Hershberger 			packet = (uchar *)
1289751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
12992f07134SNobuhiro Iwamatsu 			invalidate_cache(packet, len);
1301fd92db8SJoe Hershberger 			net_process_received_packet(packet, len);
1319751ee09SNobuhiro Iwamatsu 		}
1329751ee09SNobuhiro Iwamatsu 
1339751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1349751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1359751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1369751ee09SNobuhiro Iwamatsu 		else
1379751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
138f7ca1f76SNobuhiro Iwamatsu 
139f7ca1f76SNobuhiro Iwamatsu 		flush_cache_wback(port_info->rx_desc_cur,
140f7ca1f76SNobuhiro Iwamatsu 				  sizeof(struct rx_desc_s));
141f7ca1f76SNobuhiro Iwamatsu 
1429751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1439751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1449751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1459751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1469751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1479751ee09SNobuhiro Iwamatsu 	}
1489751ee09SNobuhiro Iwamatsu 
1499751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
15049afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
15149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1529751ee09SNobuhiro Iwamatsu 
1539751ee09SNobuhiro Iwamatsu 	return len;
1549751ee09SNobuhiro Iwamatsu }
1559751ee09SNobuhiro Iwamatsu 
sh_eth_reset(struct sh_eth_dev * eth)156bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1579751ee09SNobuhiro Iwamatsu {
15862cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
159bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1609751ee09SNobuhiro Iwamatsu 
1619751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
16249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1639751ee09SNobuhiro Iwamatsu 
1649751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
16549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1664ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT; i++) {
16749afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1689751ee09SNobuhiro Iwamatsu 			break;
1699751ee09SNobuhiro Iwamatsu 		udelay(1000);
1709751ee09SNobuhiro Iwamatsu 	}
1719751ee09SNobuhiro Iwamatsu 
1724ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
173bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
174bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1759751ee09SNobuhiro Iwamatsu 	}
1769751ee09SNobuhiro Iwamatsu 
177bd3980ccSNobuhiro Iwamatsu 	return ret;
178903de461SYoshihiro Shimoda #else
17949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
180903de461SYoshihiro Shimoda 	udelay(3000);
18149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
182903de461SYoshihiro Shimoda 
183903de461SYoshihiro Shimoda 	return 0;
184903de461SYoshihiro Shimoda #endif
185bd3980ccSNobuhiro Iwamatsu }
186bd3980ccSNobuhiro Iwamatsu 
sh_eth_tx_desc_init(struct sh_eth_dev * eth)187bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1889751ee09SNobuhiro Iwamatsu {
189bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
190000889cdSNobuhiro Iwamatsu 	u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
191bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1929751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1939751ee09SNobuhiro Iwamatsu 
194bd3980ccSNobuhiro Iwamatsu 	/*
195703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
196703949e4SNobuhiro Iwamatsu 	 * tx_desc_s.
197bd3980ccSNobuhiro Iwamatsu 	 */
198000889cdSNobuhiro Iwamatsu 	port_info->tx_desc_alloc =
199000889cdSNobuhiro Iwamatsu 		memalign(sizeof(struct tx_desc_s), alloc_desc_size);
200000889cdSNobuhiro Iwamatsu 	if (!port_info->tx_desc_alloc) {
201000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": memalign failed\n");
202bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
203bd3980ccSNobuhiro Iwamatsu 		goto err;
2049751ee09SNobuhiro Iwamatsu 	}
205bd3980ccSNobuhiro Iwamatsu 
206000889cdSNobuhiro Iwamatsu 	flush_cache_wback((u32)port_info->tx_desc_alloc, alloc_desc_size);
207000889cdSNobuhiro Iwamatsu 
2089751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
209000889cdSNobuhiro Iwamatsu 	port_info->tx_desc_base =
210000889cdSNobuhiro Iwamatsu 		(struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
2119751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2129751ee09SNobuhiro Iwamatsu 
2139751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2149751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2159751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2169751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2179751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2189751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2199751ee09SNobuhiro Iwamatsu 	}
2209751ee09SNobuhiro Iwamatsu 
2219751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2229751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2239751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2249751ee09SNobuhiro Iwamatsu 
2259751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2269751ee09SNobuhiro Iwamatsu 	   addresses */
22749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
22862cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
22949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
23049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
23149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
232903de461SYoshihiro Shimoda #endif
2339751ee09SNobuhiro Iwamatsu 
234bd3980ccSNobuhiro Iwamatsu err:
235bd3980ccSNobuhiro Iwamatsu 	return ret;
2369751ee09SNobuhiro Iwamatsu }
2379751ee09SNobuhiro Iwamatsu 
sh_eth_rx_desc_init(struct sh_eth_dev * eth)238bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2399751ee09SNobuhiro Iwamatsu {
240bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
241000889cdSNobuhiro Iwamatsu 	u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
242bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2439751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
2449751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2459751ee09SNobuhiro Iwamatsu 
246bd3980ccSNobuhiro Iwamatsu 	/*
247703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
248703949e4SNobuhiro Iwamatsu 	 * rx_desc_s.
249bd3980ccSNobuhiro Iwamatsu 	 */
250000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_alloc =
251000889cdSNobuhiro Iwamatsu 		memalign(sizeof(struct rx_desc_s), alloc_desc_size);
252000889cdSNobuhiro Iwamatsu 	if (!port_info->rx_desc_alloc) {
253000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": memalign failed\n");
254bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
255bd3980ccSNobuhiro Iwamatsu 		goto err;
2569751ee09SNobuhiro Iwamatsu 	}
257bd3980ccSNobuhiro Iwamatsu 
258000889cdSNobuhiro Iwamatsu 	flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
259000889cdSNobuhiro Iwamatsu 
2609751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
261000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_base =
262000889cdSNobuhiro Iwamatsu 		(struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
2639751ee09SNobuhiro Iwamatsu 
2649751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2659751ee09SNobuhiro Iwamatsu 
266bd3980ccSNobuhiro Iwamatsu 	/*
267000889cdSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
268000889cdSNobuhiro Iwamatsu 	 * aligned and in P2 area.
269bd3980ccSNobuhiro Iwamatsu 	 */
270000889cdSNobuhiro Iwamatsu 	port_info->rx_buf_alloc =
271000889cdSNobuhiro Iwamatsu 		memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
272000889cdSNobuhiro Iwamatsu 	if (!port_info->rx_buf_alloc) {
273000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": alloc failed\n");
274bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
275000889cdSNobuhiro Iwamatsu 		goto err_buf_alloc;
2769751ee09SNobuhiro Iwamatsu 	}
277bd3980ccSNobuhiro Iwamatsu 
278000889cdSNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
2799751ee09SNobuhiro Iwamatsu 
2809751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2819751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2829751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2839751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2849751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2859751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2869751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2879751ee09SNobuhiro Iwamatsu 	}
2889751ee09SNobuhiro Iwamatsu 
2899751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2909751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2919751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2929751ee09SNobuhiro Iwamatsu 
2939751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
29449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
29562cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
29649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
29749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
29849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
299903de461SYoshihiro Shimoda #endif
3009751ee09SNobuhiro Iwamatsu 
301bd3980ccSNobuhiro Iwamatsu 	return ret;
302bd3980ccSNobuhiro Iwamatsu 
303000889cdSNobuhiro Iwamatsu err_buf_alloc:
304000889cdSNobuhiro Iwamatsu 	free(port_info->rx_desc_alloc);
305000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_alloc = NULL;
306bd3980ccSNobuhiro Iwamatsu 
307bd3980ccSNobuhiro Iwamatsu err:
308bd3980ccSNobuhiro Iwamatsu 	return ret;
3099751ee09SNobuhiro Iwamatsu }
3109751ee09SNobuhiro Iwamatsu 
sh_eth_tx_desc_free(struct sh_eth_dev * eth)311bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3129751ee09SNobuhiro Iwamatsu {
313bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
314bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3159751ee09SNobuhiro Iwamatsu 
316000889cdSNobuhiro Iwamatsu 	if (port_info->tx_desc_alloc) {
317000889cdSNobuhiro Iwamatsu 		free(port_info->tx_desc_alloc);
318000889cdSNobuhiro Iwamatsu 		port_info->tx_desc_alloc = NULL;
3199751ee09SNobuhiro Iwamatsu 	}
320bd3980ccSNobuhiro Iwamatsu }
321bd3980ccSNobuhiro Iwamatsu 
sh_eth_rx_desc_free(struct sh_eth_dev * eth)322bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
323bd3980ccSNobuhiro Iwamatsu {
324bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
325bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3269751ee09SNobuhiro Iwamatsu 
327000889cdSNobuhiro Iwamatsu 	if (port_info->rx_desc_alloc) {
328000889cdSNobuhiro Iwamatsu 		free(port_info->rx_desc_alloc);
329000889cdSNobuhiro Iwamatsu 		port_info->rx_desc_alloc = NULL;
3309751ee09SNobuhiro Iwamatsu 	}
3319751ee09SNobuhiro Iwamatsu 
332000889cdSNobuhiro Iwamatsu 	if (port_info->rx_buf_alloc) {
333000889cdSNobuhiro Iwamatsu 		free(port_info->rx_buf_alloc);
334000889cdSNobuhiro Iwamatsu 		port_info->rx_buf_alloc = NULL;
3359751ee09SNobuhiro Iwamatsu 	}
3369751ee09SNobuhiro Iwamatsu }
3379751ee09SNobuhiro Iwamatsu 
sh_eth_desc_init(struct sh_eth_dev * eth)338bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3399751ee09SNobuhiro Iwamatsu {
340bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3419751ee09SNobuhiro Iwamatsu 
342bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
343bd3980ccSNobuhiro Iwamatsu 	if (ret)
344bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
345bd3980ccSNobuhiro Iwamatsu 
346bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
347bd3980ccSNobuhiro Iwamatsu 	if (ret)
348bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
349bd3980ccSNobuhiro Iwamatsu 
350bd3980ccSNobuhiro Iwamatsu 	return ret;
351bd3980ccSNobuhiro Iwamatsu err_rx_init:
352bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
353bd3980ccSNobuhiro Iwamatsu 
354bd3980ccSNobuhiro Iwamatsu err_tx_init:
355bd3980ccSNobuhiro Iwamatsu 	return ret;
3569751ee09SNobuhiro Iwamatsu }
3579751ee09SNobuhiro Iwamatsu 
sh_eth_phy_config(struct sh_eth_dev * eth)358bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3599751ee09SNobuhiro Iwamatsu {
360bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
361bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
362bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
363bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
364bd3980ccSNobuhiro Iwamatsu 
365ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
366ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3674398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
368bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
369bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
370bd3980ccSNobuhiro Iwamatsu 
371bd3980ccSNobuhiro Iwamatsu 	return ret;
3729751ee09SNobuhiro Iwamatsu }
3739751ee09SNobuhiro Iwamatsu 
sh_eth_config(struct sh_eth_dev * eth,bd_t * bd)374bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3759751ee09SNobuhiro Iwamatsu {
376bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
377bd1024b0SYoshihiro Shimoda 	u32 val;
378bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
379c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
380bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3819751ee09SNobuhiro Iwamatsu 
3829751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
383f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
384f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
385f8b7507dSNobuhiro Iwamatsu 
38649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
38749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
38849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
38949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
39049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
39162cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
39249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
393903de461SYoshihiro Shimoda #endif
39449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3959751ee09SNobuhiro Iwamatsu 
3969751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
39749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3989751ee09SNobuhiro Iwamatsu 
3999751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
400c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
401c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
40249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
4039751ee09SNobuhiro Iwamatsu 
404c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
40549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
4069751ee09SNobuhiro Iwamatsu 
40749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
40826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
40949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
41062cbddc4SNobuhiro Iwamatsu #endif
41162cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
41249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
41349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
41449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
4153bb4cc31SNobuhiro Iwamatsu #endif
4163bb4cc31SNobuhiro Iwamatsu 
417dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
41849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
41917243747SNobuhiro Iwamatsu #elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
420a341b7e0SNobuhiro Iwamatsu 	defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
4218707678cSNobuhiro Iwamatsu 	sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
4224398d559SNobuhiro Iwamatsu #endif
4239751ee09SNobuhiro Iwamatsu 	/* Configure phy */
424bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
425bd3980ccSNobuhiro Iwamatsu 	if (ret) {
42688a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
427bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
428bd3980ccSNobuhiro Iwamatsu 	}
429bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
43011af8d65STimur Tabi 	ret = phy_startup(phy);
43111af8d65STimur Tabi 	if (ret) {
43211af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
43311af8d65STimur Tabi 		return ret;
43411af8d65STimur Tabi 	}
4359751ee09SNobuhiro Iwamatsu 
4363bb4cc31SNobuhiro Iwamatsu 	val = 0;
4373bb4cc31SNobuhiro Iwamatsu 
4389751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
439bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
440bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
44126235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
44249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
443e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
44449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
44547ce8890SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
446a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
447a341b7e0SNobuhiro Iwamatsu 		defined(CONFIG_R8A7794)
4483bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4493bb4cc31SNobuhiro Iwamatsu #endif
450bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
451bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
45226235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
45349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
454e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
45549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
456903de461SYoshihiro Shimoda #endif
4573bb4cc31SNobuhiro Iwamatsu 	}
45826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4594398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4604398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
46149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4624398d559SNobuhiro Iwamatsu 	}
4634398d559SNobuhiro Iwamatsu #endif
4649751ee09SNobuhiro Iwamatsu 
4659751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
466bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4679751ee09SNobuhiro Iwamatsu 		printf("Full\n");
46849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
46949afb8caSYoshihiro Shimoda 			     ECMR);
4709751ee09SNobuhiro Iwamatsu 	} else {
4719751ee09SNobuhiro Iwamatsu 		printf("Half\n");
47249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4739751ee09SNobuhiro Iwamatsu 	}
474bd3980ccSNobuhiro Iwamatsu 
475bd3980ccSNobuhiro Iwamatsu 	return ret;
476bd3980ccSNobuhiro Iwamatsu 
477bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
478bd3980ccSNobuhiro Iwamatsu 	return ret;
4799751ee09SNobuhiro Iwamatsu }
4809751ee09SNobuhiro Iwamatsu 
sh_eth_start(struct sh_eth_dev * eth)481bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4829751ee09SNobuhiro Iwamatsu {
4839751ee09SNobuhiro Iwamatsu 	/*
4849751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4859751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4869751ee09SNobuhiro Iwamatsu 	 */
48749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
488bd3980ccSNobuhiro Iwamatsu }
4899751ee09SNobuhiro Iwamatsu 
sh_eth_stop(struct sh_eth_dev * eth)490bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
491bd3980ccSNobuhiro Iwamatsu {
49249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4939751ee09SNobuhiro Iwamatsu }
4949751ee09SNobuhiro Iwamatsu 
sh_eth_init(struct eth_device * dev,bd_t * bd)495bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4969751ee09SNobuhiro Iwamatsu {
497bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
498bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
499bd3980ccSNobuhiro Iwamatsu 
500bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
501bd3980ccSNobuhiro Iwamatsu 	if (ret)
502bd3980ccSNobuhiro Iwamatsu 		goto err;
503bd3980ccSNobuhiro Iwamatsu 
504bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
505bd3980ccSNobuhiro Iwamatsu 	if (ret)
506bd3980ccSNobuhiro Iwamatsu 		goto err;
507bd3980ccSNobuhiro Iwamatsu 
508bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
509bd3980ccSNobuhiro Iwamatsu 	if (ret)
510bd3980ccSNobuhiro Iwamatsu 		goto err_config;
511bd3980ccSNobuhiro Iwamatsu 
512bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
513bd3980ccSNobuhiro Iwamatsu 
514bd3980ccSNobuhiro Iwamatsu 	return ret;
515bd3980ccSNobuhiro Iwamatsu 
516bd3980ccSNobuhiro Iwamatsu err_config:
517bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
518bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
519bd3980ccSNobuhiro Iwamatsu 
520bd3980ccSNobuhiro Iwamatsu err:
521bd3980ccSNobuhiro Iwamatsu 	return ret;
5229751ee09SNobuhiro Iwamatsu }
5239751ee09SNobuhiro Iwamatsu 
sh_eth_halt(struct eth_device * dev)524bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
525bd3980ccSNobuhiro Iwamatsu {
526bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
527bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
528bd3980ccSNobuhiro Iwamatsu }
529bd3980ccSNobuhiro Iwamatsu 
sh_eth_initialize(bd_t * bd)530bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
531bd3980ccSNobuhiro Iwamatsu {
532bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
533bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
534bd3980ccSNobuhiro Iwamatsu 	struct eth_device *dev = NULL;
535bd3980ccSNobuhiro Iwamatsu 
536bd3980ccSNobuhiro Iwamatsu 	eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
537bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
538bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
539bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
540bd3980ccSNobuhiro Iwamatsu 		goto err;
541bd3980ccSNobuhiro Iwamatsu 	}
542bd3980ccSNobuhiro Iwamatsu 
543bd3980ccSNobuhiro Iwamatsu 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
544bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
545bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
546bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
547bd3980ccSNobuhiro Iwamatsu 		goto err;
548bd3980ccSNobuhiro Iwamatsu 	}
549bd3980ccSNobuhiro Iwamatsu 	memset(dev, 0, sizeof(struct eth_device));
550bd3980ccSNobuhiro Iwamatsu 	memset(eth, 0, sizeof(struct sh_eth_dev));
551bd3980ccSNobuhiro Iwamatsu 
552bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
553bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
554bd3980ccSNobuhiro Iwamatsu 
555bd3980ccSNobuhiro Iwamatsu 	dev->priv = (void *)eth;
556bd3980ccSNobuhiro Iwamatsu 	dev->iobase = 0;
557bd3980ccSNobuhiro Iwamatsu 	dev->init = sh_eth_init;
558bd3980ccSNobuhiro Iwamatsu 	dev->halt = sh_eth_halt;
559bd3980ccSNobuhiro Iwamatsu 	dev->send = sh_eth_send;
560bd3980ccSNobuhiro Iwamatsu 	dev->recv = sh_eth_recv;
561bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].dev = dev;
562bd3980ccSNobuhiro Iwamatsu 
563192bc694SBen Whitten 	strcpy(dev->name, SHETHER_NAME);
564bd3980ccSNobuhiro Iwamatsu 
565bd3980ccSNobuhiro Iwamatsu 	/* Register Device to EtherNet subsystem  */
566bd3980ccSNobuhiro Iwamatsu 	eth_register(dev);
5679751ee09SNobuhiro Iwamatsu 
568bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
5695a49f174SJoe Hershberger 	int retval;
5705a49f174SJoe Hershberger 	struct mii_dev *mdiodev = mdio_alloc();
5715a49f174SJoe Hershberger 	if (!mdiodev)
5725a49f174SJoe Hershberger 		return -ENOMEM;
5735a49f174SJoe Hershberger 	strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
5745a49f174SJoe Hershberger 	mdiodev->read = bb_miiphy_read;
5755a49f174SJoe Hershberger 	mdiodev->write = bb_miiphy_write;
5765a49f174SJoe Hershberger 
5775a49f174SJoe Hershberger 	retval = mdio_register(mdiodev);
5785a49f174SJoe Hershberger 	if (retval < 0)
5795a49f174SJoe Hershberger 		return retval;
580bd1024b0SYoshihiro Shimoda 
581*35affd7aSSimon Glass 	if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
582c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5839751ee09SNobuhiro Iwamatsu 
584bd3980ccSNobuhiro Iwamatsu 	return ret;
5859751ee09SNobuhiro Iwamatsu 
5869751ee09SNobuhiro Iwamatsu err:
587bd3980ccSNobuhiro Iwamatsu 	if (dev)
5889751ee09SNobuhiro Iwamatsu 		free(dev);
589bd3980ccSNobuhiro Iwamatsu 
590bd3980ccSNobuhiro Iwamatsu 	if (eth)
591bd3980ccSNobuhiro Iwamatsu 		free(eth);
592bd3980ccSNobuhiro Iwamatsu 
593bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
594bd3980ccSNobuhiro Iwamatsu 	return ret;
5959751ee09SNobuhiro Iwamatsu }
596bd1024b0SYoshihiro Shimoda 
597bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
sh_eth_bb_init(struct bb_miiphy_bus * bus)598bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
599bd1024b0SYoshihiro Shimoda {
600bd1024b0SYoshihiro Shimoda 	return 0;
601bd1024b0SYoshihiro Shimoda }
602bd1024b0SYoshihiro Shimoda 
sh_eth_bb_mdio_active(struct bb_miiphy_bus * bus)603bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
604bd1024b0SYoshihiro Shimoda {
605bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
606bd1024b0SYoshihiro Shimoda 
60749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
608bd1024b0SYoshihiro Shimoda 
609bd1024b0SYoshihiro Shimoda 	return 0;
610bd1024b0SYoshihiro Shimoda }
611bd1024b0SYoshihiro Shimoda 
sh_eth_bb_mdio_tristate(struct bb_miiphy_bus * bus)612bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
613bd1024b0SYoshihiro Shimoda {
614bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
615bd1024b0SYoshihiro Shimoda 
61649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
617bd1024b0SYoshihiro Shimoda 
618bd1024b0SYoshihiro Shimoda 	return 0;
619bd1024b0SYoshihiro Shimoda }
620bd1024b0SYoshihiro Shimoda 
sh_eth_bb_set_mdio(struct bb_miiphy_bus * bus,int v)621bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
622bd1024b0SYoshihiro Shimoda {
623bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
624bd1024b0SYoshihiro Shimoda 
625bd1024b0SYoshihiro Shimoda 	if (v)
62649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
627bd1024b0SYoshihiro Shimoda 	else
62849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
629bd1024b0SYoshihiro Shimoda 
630bd1024b0SYoshihiro Shimoda 	return 0;
631bd1024b0SYoshihiro Shimoda }
632bd1024b0SYoshihiro Shimoda 
sh_eth_bb_get_mdio(struct bb_miiphy_bus * bus,int * v)633bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
634bd1024b0SYoshihiro Shimoda {
635bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
636bd1024b0SYoshihiro Shimoda 
63749afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
638bd1024b0SYoshihiro Shimoda 
639bd1024b0SYoshihiro Shimoda 	return 0;
640bd1024b0SYoshihiro Shimoda }
641bd1024b0SYoshihiro Shimoda 
sh_eth_bb_set_mdc(struct bb_miiphy_bus * bus,int v)642bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
643bd1024b0SYoshihiro Shimoda {
644bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
645bd1024b0SYoshihiro Shimoda 
646bd1024b0SYoshihiro Shimoda 	if (v)
64749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
648bd1024b0SYoshihiro Shimoda 	else
64949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
650bd1024b0SYoshihiro Shimoda 
651bd1024b0SYoshihiro Shimoda 	return 0;
652bd1024b0SYoshihiro Shimoda }
653bd1024b0SYoshihiro Shimoda 
sh_eth_bb_delay(struct bb_miiphy_bus * bus)654bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
655bd1024b0SYoshihiro Shimoda {
656bd1024b0SYoshihiro Shimoda 	udelay(10);
657bd1024b0SYoshihiro Shimoda 
658bd1024b0SYoshihiro Shimoda 	return 0;
659bd1024b0SYoshihiro Shimoda }
660bd1024b0SYoshihiro Shimoda 
661bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
662bd1024b0SYoshihiro Shimoda 	{
663bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
664bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
665bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
666bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
667bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
668bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
669bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
670bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
671bd1024b0SYoshihiro Shimoda 	}
672bd1024b0SYoshihiro Shimoda };
673bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
674