xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision f8b7507d41e9d2607e876b74f6ce79235f6bd618)
19751ee09SNobuhiro Iwamatsu /*
226235093SYoshihiro Shimoda  * sh_eth.c - Driver for Renesas ethernet controler.
39751ee09SNobuhiro Iwamatsu  *
43bb4cc31SNobuhiro Iwamatsu  * Copyright (C) 2008, 2011 Renesas Solutions Corp.
53bb4cc31SNobuhiro Iwamatsu  * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu
69751ee09SNobuhiro Iwamatsu  * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
79751ee09SNobuhiro Iwamatsu  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
99751ee09SNobuhiro Iwamatsu  */
109751ee09SNobuhiro Iwamatsu 
119751ee09SNobuhiro Iwamatsu #include <config.h>
129751ee09SNobuhiro Iwamatsu #include <common.h>
139751ee09SNobuhiro Iwamatsu #include <malloc.h>
149751ee09SNobuhiro Iwamatsu #include <net.h>
15bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
16bd1024b0SYoshihiro Shimoda #include <miiphy.h>
179751ee09SNobuhiro Iwamatsu #include <asm/errno.h>
189751ee09SNobuhiro Iwamatsu #include <asm/io.h>
199751ee09SNobuhiro Iwamatsu 
209751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
219751ee09SNobuhiro Iwamatsu 
229751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
239751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
249751ee09SNobuhiro Iwamatsu #endif
259751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
269751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
279751ee09SNobuhiro Iwamatsu #endif
28870cc23fSNobuhiro Iwamatsu 
2968260aabSYoshihiro Shimoda #ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
3068260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)	\
31870cc23fSNobuhiro Iwamatsu 			flush_dcache_range((u32)addr, (u32)(addr + len - 1))
3268260aabSYoshihiro Shimoda #else
3368260aabSYoshihiro Shimoda #define flush_cache_wback(...)
3468260aabSYoshihiro Shimoda #endif
359751ee09SNobuhiro Iwamatsu 
364ba62c72SNobuhiro Iwamatsu #define TIMEOUT_CNT 1000
374ba62c72SNobuhiro Iwamatsu 
3810cbe3b6SJoe Hershberger int sh_eth_send(struct eth_device *dev, void *packet, int len)
399751ee09SNobuhiro Iwamatsu {
40bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
41bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0, timeout;
42bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
439751ee09SNobuhiro Iwamatsu 
449751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
45bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
46bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
47bd3980ccSNobuhiro Iwamatsu 		goto err;
489751ee09SNobuhiro Iwamatsu 	}
499751ee09SNobuhiro Iwamatsu 
509751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
51ee6ec5d4SNobuhiro Iwamatsu 	if ((int)packet & 3) {
52bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
53bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
54bd3980ccSNobuhiro Iwamatsu 		goto err;
559751ee09SNobuhiro Iwamatsu 	}
569751ee09SNobuhiro Iwamatsu 
579751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
5868260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
599751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
609751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
619751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
629751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
639751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
649751ee09SNobuhiro Iwamatsu 	else
659751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
669751ee09SNobuhiro Iwamatsu 
679751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
6849afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
6949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
709751ee09SNobuhiro Iwamatsu 
719751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
724ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
739751ee09SNobuhiro Iwamatsu 	while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
749751ee09SNobuhiro Iwamatsu 		udelay(100);
759751ee09SNobuhiro Iwamatsu 
769751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
77bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
78bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
799751ee09SNobuhiro Iwamatsu 		goto err;
809751ee09SNobuhiro Iwamatsu 	}
819751ee09SNobuhiro Iwamatsu 
829751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
839751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
849751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
859751ee09SNobuhiro Iwamatsu 
86bd3980ccSNobuhiro Iwamatsu err:
87bd3980ccSNobuhiro Iwamatsu 	return ret;
889751ee09SNobuhiro Iwamatsu }
899751ee09SNobuhiro Iwamatsu 
90bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
919751ee09SNobuhiro Iwamatsu {
92bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
93bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
94bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
9510cbe3b6SJoe Hershberger 	uchar *packet;
969751ee09SNobuhiro Iwamatsu 
979751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
989751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
999751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1009751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1019751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
10210cbe3b6SJoe Hershberger 			packet = (uchar *)
1039751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
1049751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1059751ee09SNobuhiro Iwamatsu 		}
1069751ee09SNobuhiro Iwamatsu 
1079751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1089751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1099751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1109751ee09SNobuhiro Iwamatsu 		else
1119751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1129751ee09SNobuhiro Iwamatsu 
1139751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1149751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1159751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1169751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1179751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1189751ee09SNobuhiro Iwamatsu 	}
1199751ee09SNobuhiro Iwamatsu 
1209751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
12149afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
12249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1239751ee09SNobuhiro Iwamatsu 
1249751ee09SNobuhiro Iwamatsu 	return len;
1259751ee09SNobuhiro Iwamatsu }
1269751ee09SNobuhiro Iwamatsu 
127bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1289751ee09SNobuhiro Iwamatsu {
12926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
130bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1319751ee09SNobuhiro Iwamatsu 
1329751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
13349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1349751ee09SNobuhiro Iwamatsu 
1359751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
13649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1374ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT ; i++) {
13849afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1399751ee09SNobuhiro Iwamatsu 			break;
1409751ee09SNobuhiro Iwamatsu 		udelay(1000);
1419751ee09SNobuhiro Iwamatsu 	}
1429751ee09SNobuhiro Iwamatsu 
1434ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
144bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
145bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1469751ee09SNobuhiro Iwamatsu 	}
1479751ee09SNobuhiro Iwamatsu 
148bd3980ccSNobuhiro Iwamatsu 	return ret;
149903de461SYoshihiro Shimoda #else
15049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
151903de461SYoshihiro Shimoda 	udelay(3000);
15249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
153903de461SYoshihiro Shimoda 
154903de461SYoshihiro Shimoda 	return 0;
155903de461SYoshihiro Shimoda #endif
156bd3980ccSNobuhiro Iwamatsu }
157bd3980ccSNobuhiro Iwamatsu 
158bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1599751ee09SNobuhiro Iwamatsu {
160bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1619751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
162bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1639751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1649751ee09SNobuhiro Iwamatsu 
165bd3980ccSNobuhiro Iwamatsu 	/*
166bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
167bd3980ccSNobuhiro Iwamatsu 	 */
168bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1699751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
170bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
171bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
172bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
173bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
174bd3980ccSNobuhiro Iwamatsu 		goto err;
1759751ee09SNobuhiro Iwamatsu 	}
176bd3980ccSNobuhiro Iwamatsu 
1779751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
1789751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
17968260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
1809751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
1819751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
1829751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
1839751ee09SNobuhiro Iwamatsu 
1849751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
1859751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
1869751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
1879751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
1889751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
1899751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
1909751ee09SNobuhiro Iwamatsu 	}
1919751ee09SNobuhiro Iwamatsu 
1929751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
1939751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
1949751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
1959751ee09SNobuhiro Iwamatsu 
1969751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
1979751ee09SNobuhiro Iwamatsu 	   addresses */
19849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
19926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
20049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
20149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
20249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
203903de461SYoshihiro Shimoda #endif
2049751ee09SNobuhiro Iwamatsu 
205bd3980ccSNobuhiro Iwamatsu err:
206bd3980ccSNobuhiro Iwamatsu 	return ret;
2079751ee09SNobuhiro Iwamatsu }
2089751ee09SNobuhiro Iwamatsu 
209bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2109751ee09SNobuhiro Iwamatsu {
211bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
212bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2139751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
214bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2159751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2169751ee09SNobuhiro Iwamatsu 
217bd3980ccSNobuhiro Iwamatsu 	/*
218bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
219bd3980ccSNobuhiro Iwamatsu 	 */
220bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2219751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
222bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
223bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
224bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
225bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
226bd3980ccSNobuhiro Iwamatsu 		goto err;
2279751ee09SNobuhiro Iwamatsu 	}
228bd3980ccSNobuhiro Iwamatsu 
2299751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2309751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
23168260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2329751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2339751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2349751ee09SNobuhiro Iwamatsu 
2359751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2369751ee09SNobuhiro Iwamatsu 
237bd3980ccSNobuhiro Iwamatsu 	/*
238bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
239bd3980ccSNobuhiro Iwamatsu 	 * P2 area
240bd3980ccSNobuhiro Iwamatsu 	 */
241*f8b7507dSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(
242*f8b7507dSNobuhiro Iwamatsu 		NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
243bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
244bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
245bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
246bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2479751ee09SNobuhiro Iwamatsu 	}
248bd3980ccSNobuhiro Iwamatsu 
249*f8b7507dSNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc
250*f8b7507dSNobuhiro Iwamatsu 			  + (RX_BUF_ALIGNE_SIZE - 1)) &
251*f8b7507dSNobuhiro Iwamatsu 			  ~(RX_BUF_ALIGNE_SIZE - 1));
2529751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2539751ee09SNobuhiro Iwamatsu 
2549751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2559751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2569751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2579751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2589751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2599751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2609751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2619751ee09SNobuhiro Iwamatsu 	}
2629751ee09SNobuhiro Iwamatsu 
2639751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2649751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2659751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2669751ee09SNobuhiro Iwamatsu 
2679751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
26849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
26926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
27049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
27149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
27249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
273903de461SYoshihiro Shimoda #endif
2749751ee09SNobuhiro Iwamatsu 
275bd3980ccSNobuhiro Iwamatsu 	return ret;
276bd3980ccSNobuhiro Iwamatsu 
277bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
278bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
279bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
280bd3980ccSNobuhiro Iwamatsu 
281bd3980ccSNobuhiro Iwamatsu err:
282bd3980ccSNobuhiro Iwamatsu 	return ret;
2839751ee09SNobuhiro Iwamatsu }
2849751ee09SNobuhiro Iwamatsu 
285bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
2869751ee09SNobuhiro Iwamatsu {
287bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
288bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2899751ee09SNobuhiro Iwamatsu 
2909751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
2919751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
2929751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
2939751ee09SNobuhiro Iwamatsu 	}
294bd3980ccSNobuhiro Iwamatsu }
295bd3980ccSNobuhiro Iwamatsu 
296bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
297bd3980ccSNobuhiro Iwamatsu {
298bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
299bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3009751ee09SNobuhiro Iwamatsu 
3019751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3029751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3039751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3049751ee09SNobuhiro Iwamatsu 	}
3059751ee09SNobuhiro Iwamatsu 
3069751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3079751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3089751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3099751ee09SNobuhiro Iwamatsu 	}
3109751ee09SNobuhiro Iwamatsu }
3119751ee09SNobuhiro Iwamatsu 
312bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3139751ee09SNobuhiro Iwamatsu {
314bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3159751ee09SNobuhiro Iwamatsu 
316bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
317bd3980ccSNobuhiro Iwamatsu 	if (ret)
318bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
319bd3980ccSNobuhiro Iwamatsu 
320bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
321bd3980ccSNobuhiro Iwamatsu 	if (ret)
322bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
323bd3980ccSNobuhiro Iwamatsu 
324bd3980ccSNobuhiro Iwamatsu 	return ret;
325bd3980ccSNobuhiro Iwamatsu err_rx_init:
326bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
327bd3980ccSNobuhiro Iwamatsu 
328bd3980ccSNobuhiro Iwamatsu err_tx_init:
329bd3980ccSNobuhiro Iwamatsu 	return ret;
3309751ee09SNobuhiro Iwamatsu }
3319751ee09SNobuhiro Iwamatsu 
332bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3339751ee09SNobuhiro Iwamatsu {
334bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
335bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
336bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
337bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
338bd3980ccSNobuhiro Iwamatsu 
339ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
340ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3414398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
342bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
343bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
344bd3980ccSNobuhiro Iwamatsu 
345bd3980ccSNobuhiro Iwamatsu 	return ret;
3469751ee09SNobuhiro Iwamatsu }
3479751ee09SNobuhiro Iwamatsu 
348bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3499751ee09SNobuhiro Iwamatsu {
350bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
351bd1024b0SYoshihiro Shimoda 	u32 val;
352bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
353c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
354bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3559751ee09SNobuhiro Iwamatsu 
3569751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
357*f8b7507dSNobuhiro Iwamatsu 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
358*f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
359*f8b7507dSNobuhiro Iwamatsu 
36049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
36149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
36249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
36349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
36449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
36526235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
36649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
367903de461SYoshihiro Shimoda #endif
36849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3699751ee09SNobuhiro Iwamatsu 
3709751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
37149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3729751ee09SNobuhiro Iwamatsu 
3739751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
374c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
375c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
37649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
3779751ee09SNobuhiro Iwamatsu 
378c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
37949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
3809751ee09SNobuhiro Iwamatsu 
38149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
38226235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
38349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
38449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
38549afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
38649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
3873bb4cc31SNobuhiro Iwamatsu #endif
3883bb4cc31SNobuhiro Iwamatsu 
389dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
39049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
3914398d559SNobuhiro Iwamatsu #endif
3929751ee09SNobuhiro Iwamatsu 	/* Configure phy */
393bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
394bd3980ccSNobuhiro Iwamatsu 	if (ret) {
39588a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
396bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
397bd3980ccSNobuhiro Iwamatsu 	}
398bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
39911af8d65STimur Tabi 	ret = phy_startup(phy);
40011af8d65STimur Tabi 	if (ret) {
40111af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
40211af8d65STimur Tabi 		return ret;
40311af8d65STimur Tabi 	}
4049751ee09SNobuhiro Iwamatsu 
4053bb4cc31SNobuhiro Iwamatsu 	val = 0;
4063bb4cc31SNobuhiro Iwamatsu 
4079751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
408bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
409bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
41026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
41149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
412e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
41349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
4143bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724)
4153bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4163bb4cc31SNobuhiro Iwamatsu #endif
417bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
418bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
41926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
42049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
421e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
42249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
423903de461SYoshihiro Shimoda #endif
4243bb4cc31SNobuhiro Iwamatsu 	}
42526235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4264398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4274398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
42849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4294398d559SNobuhiro Iwamatsu 	}
4304398d559SNobuhiro Iwamatsu #endif
4319751ee09SNobuhiro Iwamatsu 
4329751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
433bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4349751ee09SNobuhiro Iwamatsu 		printf("Full\n");
43549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
43649afb8caSYoshihiro Shimoda 			     ECMR);
4379751ee09SNobuhiro Iwamatsu 	} else {
4389751ee09SNobuhiro Iwamatsu 		printf("Half\n");
43949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4409751ee09SNobuhiro Iwamatsu 	}
441bd3980ccSNobuhiro Iwamatsu 
442bd3980ccSNobuhiro Iwamatsu 	return ret;
443bd3980ccSNobuhiro Iwamatsu 
444bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
445bd3980ccSNobuhiro Iwamatsu 	return ret;
4469751ee09SNobuhiro Iwamatsu }
4479751ee09SNobuhiro Iwamatsu 
448bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4499751ee09SNobuhiro Iwamatsu {
4509751ee09SNobuhiro Iwamatsu 	/*
4519751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4529751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4539751ee09SNobuhiro Iwamatsu 	 */
45449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
455bd3980ccSNobuhiro Iwamatsu }
4569751ee09SNobuhiro Iwamatsu 
457bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
458bd3980ccSNobuhiro Iwamatsu {
45949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4609751ee09SNobuhiro Iwamatsu }
4619751ee09SNobuhiro Iwamatsu 
462bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4639751ee09SNobuhiro Iwamatsu {
464bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
465bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
466bd3980ccSNobuhiro Iwamatsu 
467bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
468bd3980ccSNobuhiro Iwamatsu 	if (ret)
469bd3980ccSNobuhiro Iwamatsu 		goto err;
470bd3980ccSNobuhiro Iwamatsu 
471bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
472bd3980ccSNobuhiro Iwamatsu 	if (ret)
473bd3980ccSNobuhiro Iwamatsu 		goto err;
474bd3980ccSNobuhiro Iwamatsu 
475bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
476bd3980ccSNobuhiro Iwamatsu 	if (ret)
477bd3980ccSNobuhiro Iwamatsu 		goto err_config;
478bd3980ccSNobuhiro Iwamatsu 
479bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
480bd3980ccSNobuhiro Iwamatsu 
481bd3980ccSNobuhiro Iwamatsu 	return ret;
482bd3980ccSNobuhiro Iwamatsu 
483bd3980ccSNobuhiro Iwamatsu err_config:
484bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
485bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
486bd3980ccSNobuhiro Iwamatsu 
487bd3980ccSNobuhiro Iwamatsu err:
488bd3980ccSNobuhiro Iwamatsu 	return ret;
4899751ee09SNobuhiro Iwamatsu }
4909751ee09SNobuhiro Iwamatsu 
491bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
492bd3980ccSNobuhiro Iwamatsu {
493bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
494bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
495bd3980ccSNobuhiro Iwamatsu }
496bd3980ccSNobuhiro Iwamatsu 
497bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
498bd3980ccSNobuhiro Iwamatsu {
499bd3980ccSNobuhiro Iwamatsu     int ret = 0;
500bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
501bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
502bd3980ccSNobuhiro Iwamatsu 
503bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
504bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
505bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
506bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
507bd3980ccSNobuhiro Iwamatsu 		goto err;
508bd3980ccSNobuhiro Iwamatsu 	}
509bd3980ccSNobuhiro Iwamatsu 
510bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
511bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
512bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
513bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
514bd3980ccSNobuhiro Iwamatsu 		goto err;
515bd3980ccSNobuhiro Iwamatsu 	}
516bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
517bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
518bd3980ccSNobuhiro Iwamatsu 
519bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
520bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
521bd3980ccSNobuhiro Iwamatsu 
522bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
523bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
524bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
525bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
526bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
527bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
528bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
529bd3980ccSNobuhiro Iwamatsu 
530bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
531bd3980ccSNobuhiro Iwamatsu 
532bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
533bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5349751ee09SNobuhiro Iwamatsu 
535bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
536bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
537bd1024b0SYoshihiro Shimoda 
538c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
539c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5409751ee09SNobuhiro Iwamatsu 
541bd3980ccSNobuhiro Iwamatsu 	return ret;
5429751ee09SNobuhiro Iwamatsu 
5439751ee09SNobuhiro Iwamatsu err:
544bd3980ccSNobuhiro Iwamatsu 	if (dev)
5459751ee09SNobuhiro Iwamatsu 		free(dev);
546bd3980ccSNobuhiro Iwamatsu 
547bd3980ccSNobuhiro Iwamatsu 	if (eth)
548bd3980ccSNobuhiro Iwamatsu 		free(eth);
549bd3980ccSNobuhiro Iwamatsu 
550bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
551bd3980ccSNobuhiro Iwamatsu 	return ret;
5529751ee09SNobuhiro Iwamatsu }
553bd1024b0SYoshihiro Shimoda 
554bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
555bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
556bd1024b0SYoshihiro Shimoda {
557bd1024b0SYoshihiro Shimoda 	return 0;
558bd1024b0SYoshihiro Shimoda }
559bd1024b0SYoshihiro Shimoda 
560bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
561bd1024b0SYoshihiro Shimoda {
562bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
563bd1024b0SYoshihiro Shimoda 
56449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
565bd1024b0SYoshihiro Shimoda 
566bd1024b0SYoshihiro Shimoda 	return 0;
567bd1024b0SYoshihiro Shimoda }
568bd1024b0SYoshihiro Shimoda 
569bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
570bd1024b0SYoshihiro Shimoda {
571bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
572bd1024b0SYoshihiro Shimoda 
57349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
574bd1024b0SYoshihiro Shimoda 
575bd1024b0SYoshihiro Shimoda 	return 0;
576bd1024b0SYoshihiro Shimoda }
577bd1024b0SYoshihiro Shimoda 
578bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
579bd1024b0SYoshihiro Shimoda {
580bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
581bd1024b0SYoshihiro Shimoda 
582bd1024b0SYoshihiro Shimoda 	if (v)
58349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
584bd1024b0SYoshihiro Shimoda 	else
58549afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
586bd1024b0SYoshihiro Shimoda 
587bd1024b0SYoshihiro Shimoda 	return 0;
588bd1024b0SYoshihiro Shimoda }
589bd1024b0SYoshihiro Shimoda 
590bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
591bd1024b0SYoshihiro Shimoda {
592bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
593bd1024b0SYoshihiro Shimoda 
59449afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
595bd1024b0SYoshihiro Shimoda 
596bd1024b0SYoshihiro Shimoda 	return 0;
597bd1024b0SYoshihiro Shimoda }
598bd1024b0SYoshihiro Shimoda 
599bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
600bd1024b0SYoshihiro Shimoda {
601bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
602bd1024b0SYoshihiro Shimoda 
603bd1024b0SYoshihiro Shimoda 	if (v)
60449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
605bd1024b0SYoshihiro Shimoda 	else
60649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
607bd1024b0SYoshihiro Shimoda 
608bd1024b0SYoshihiro Shimoda 	return 0;
609bd1024b0SYoshihiro Shimoda }
610bd1024b0SYoshihiro Shimoda 
611bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
612bd1024b0SYoshihiro Shimoda {
613bd1024b0SYoshihiro Shimoda 	udelay(10);
614bd1024b0SYoshihiro Shimoda 
615bd1024b0SYoshihiro Shimoda 	return 0;
616bd1024b0SYoshihiro Shimoda }
617bd1024b0SYoshihiro Shimoda 
618bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
619bd1024b0SYoshihiro Shimoda 	{
620bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
621bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
622bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
623bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
624bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
625bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
626bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
627bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
628bd1024b0SYoshihiro Shimoda 	}
629bd1024b0SYoshihiro Shimoda };
630bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
631