xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision dcd5a593f59b58da4875e3e78d7c74501bc319c4)
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  *
89751ee09SNobuhiro Iwamatsu  * This program is free software; you can redistribute it and/or modify
99751ee09SNobuhiro Iwamatsu  * it under the terms of the GNU General Public License as published by
109751ee09SNobuhiro Iwamatsu  * the Free Software Foundation; either version 2 of the License, or
119751ee09SNobuhiro Iwamatsu  * (at your option) any later version.
129751ee09SNobuhiro Iwamatsu  *
139751ee09SNobuhiro Iwamatsu  * This program is distributed in the hope that it will be useful,
149751ee09SNobuhiro Iwamatsu  * but WITHOUT ANY WARRANTY; without even the implied warranty of
159751ee09SNobuhiro Iwamatsu  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
169751ee09SNobuhiro Iwamatsu  * GNU General Public License for more details.
179751ee09SNobuhiro Iwamatsu  *
189751ee09SNobuhiro Iwamatsu  * You should have received a copy of the GNU General Public License
199751ee09SNobuhiro Iwamatsu  * along with this program; if not, write to the Free Software
209751ee09SNobuhiro Iwamatsu  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
219751ee09SNobuhiro Iwamatsu  */
229751ee09SNobuhiro Iwamatsu 
239751ee09SNobuhiro Iwamatsu #include <config.h>
249751ee09SNobuhiro Iwamatsu #include <common.h>
259751ee09SNobuhiro Iwamatsu #include <malloc.h>
269751ee09SNobuhiro Iwamatsu #include <net.h>
27bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
28bd1024b0SYoshihiro Shimoda #include <miiphy.h>
299751ee09SNobuhiro Iwamatsu #include <asm/errno.h>
309751ee09SNobuhiro Iwamatsu #include <asm/io.h>
319751ee09SNobuhiro Iwamatsu 
329751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
339751ee09SNobuhiro Iwamatsu 
349751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
359751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
369751ee09SNobuhiro Iwamatsu #endif
379751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
389751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
399751ee09SNobuhiro Iwamatsu #endif
4068260aabSYoshihiro Shimoda #ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
4168260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)	\
4268260aabSYoshihiro Shimoda 			dcache_wback_range((u32)addr, (u32)(addr + len - 1))
4368260aabSYoshihiro Shimoda #else
4468260aabSYoshihiro Shimoda #define flush_cache_wback(...)
4568260aabSYoshihiro Shimoda #endif
469751ee09SNobuhiro Iwamatsu 
474ba62c72SNobuhiro Iwamatsu #define TIMEOUT_CNT 1000
484ba62c72SNobuhiro Iwamatsu 
4910cbe3b6SJoe Hershberger int sh_eth_send(struct eth_device *dev, void *packet, int len)
509751ee09SNobuhiro Iwamatsu {
51bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
52bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0, timeout;
53bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
549751ee09SNobuhiro Iwamatsu 
559751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
56bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
57bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
58bd3980ccSNobuhiro Iwamatsu 		goto err;
599751ee09SNobuhiro Iwamatsu 	}
609751ee09SNobuhiro Iwamatsu 
619751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
62ee6ec5d4SNobuhiro Iwamatsu 	if ((int)packet & 3) {
63bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
64bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
65bd3980ccSNobuhiro Iwamatsu 		goto err;
669751ee09SNobuhiro Iwamatsu 	}
679751ee09SNobuhiro Iwamatsu 
689751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
6968260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
709751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
719751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
729751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
739751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
749751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
759751ee09SNobuhiro Iwamatsu 	else
769751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
779751ee09SNobuhiro Iwamatsu 
789751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
7949afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
8049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDTRR_TRNS, EDTRR);
819751ee09SNobuhiro Iwamatsu 
829751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
834ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
849751ee09SNobuhiro Iwamatsu 	while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
859751ee09SNobuhiro Iwamatsu 		udelay(100);
869751ee09SNobuhiro Iwamatsu 
879751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
88bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
89bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
909751ee09SNobuhiro Iwamatsu 		goto err;
919751ee09SNobuhiro Iwamatsu 	}
929751ee09SNobuhiro Iwamatsu 
939751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
949751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
959751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
969751ee09SNobuhiro Iwamatsu 
97bd3980ccSNobuhiro Iwamatsu err:
98bd3980ccSNobuhiro Iwamatsu 	return ret;
999751ee09SNobuhiro Iwamatsu }
1009751ee09SNobuhiro Iwamatsu 
101bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1029751ee09SNobuhiro Iwamatsu {
103bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
104bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
105bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
10610cbe3b6SJoe Hershberger 	uchar *packet;
1079751ee09SNobuhiro Iwamatsu 
1089751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
1099751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1109751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1119751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1129751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
11310cbe3b6SJoe Hershberger 			packet = (uchar *)
1149751ee09SNobuhiro Iwamatsu 				ADDR_TO_P2(port_info->rx_desc_cur->rd2);
1159751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1169751ee09SNobuhiro Iwamatsu 		}
1179751ee09SNobuhiro Iwamatsu 
1189751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1199751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1209751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1219751ee09SNobuhiro Iwamatsu 		else
1229751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1239751ee09SNobuhiro Iwamatsu 
1249751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1259751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1269751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1279751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1289751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1299751ee09SNobuhiro Iwamatsu 	}
1309751ee09SNobuhiro Iwamatsu 
1319751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
13249afb8caSYoshihiro Shimoda 	if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
13349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, EDRRR_R, EDRRR);
1349751ee09SNobuhiro Iwamatsu 
1359751ee09SNobuhiro Iwamatsu 	return len;
1369751ee09SNobuhiro Iwamatsu }
1379751ee09SNobuhiro Iwamatsu 
138bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1399751ee09SNobuhiro Iwamatsu {
14026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
141bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1429751ee09SNobuhiro Iwamatsu 
1439751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
14449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDSR_ENALL, EDSR);
1459751ee09SNobuhiro Iwamatsu 
1469751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
14749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDMR_SRST, EDMR);
1484ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT ; i++) {
14949afb8caSYoshihiro Shimoda 		if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
1509751ee09SNobuhiro Iwamatsu 			break;
1519751ee09SNobuhiro Iwamatsu 		udelay(1000);
1529751ee09SNobuhiro Iwamatsu 	}
1539751ee09SNobuhiro Iwamatsu 
1544ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
155bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
156bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1579751ee09SNobuhiro Iwamatsu 	}
1589751ee09SNobuhiro Iwamatsu 
159bd3980ccSNobuhiro Iwamatsu 	return ret;
160903de461SYoshihiro Shimoda #else
16149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
162903de461SYoshihiro Shimoda 	udelay(3000);
16349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
164903de461SYoshihiro Shimoda 
165903de461SYoshihiro Shimoda 	return 0;
166903de461SYoshihiro Shimoda #endif
167bd3980ccSNobuhiro Iwamatsu }
168bd3980ccSNobuhiro Iwamatsu 
169bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1709751ee09SNobuhiro Iwamatsu {
171bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1729751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
173bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1749751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1759751ee09SNobuhiro Iwamatsu 
176bd3980ccSNobuhiro Iwamatsu 	/*
177bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
178bd3980ccSNobuhiro Iwamatsu 	 */
179bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1809751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
181bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
182bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
183bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
184bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
185bd3980ccSNobuhiro Iwamatsu 		goto err;
1869751ee09SNobuhiro Iwamatsu 	}
187bd3980ccSNobuhiro Iwamatsu 
1889751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
1899751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
19068260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
1919751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
1929751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
1939751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
1949751ee09SNobuhiro Iwamatsu 
1959751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
1969751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
1979751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
1989751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
1999751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2009751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2019751ee09SNobuhiro Iwamatsu 	}
2029751ee09SNobuhiro Iwamatsu 
2039751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2049751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2059751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2069751ee09SNobuhiro Iwamatsu 
2079751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2089751ee09SNobuhiro Iwamatsu 	   addresses */
20949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
21026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
21149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
21249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
21349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
214903de461SYoshihiro Shimoda #endif
2159751ee09SNobuhiro Iwamatsu 
216bd3980ccSNobuhiro Iwamatsu err:
217bd3980ccSNobuhiro Iwamatsu 	return ret;
2189751ee09SNobuhiro Iwamatsu }
2199751ee09SNobuhiro Iwamatsu 
220bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2219751ee09SNobuhiro Iwamatsu {
222bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
223bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2249751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
225bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2269751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2279751ee09SNobuhiro Iwamatsu 
228bd3980ccSNobuhiro Iwamatsu 	/*
229bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
230bd3980ccSNobuhiro Iwamatsu 	 */
231bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2329751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
233bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
234bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
235bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
236bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
237bd3980ccSNobuhiro Iwamatsu 		goto err;
2389751ee09SNobuhiro Iwamatsu 	}
239bd3980ccSNobuhiro Iwamatsu 
2409751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2419751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
24268260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2439751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2449751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2459751ee09SNobuhiro Iwamatsu 
2469751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2479751ee09SNobuhiro Iwamatsu 
248bd3980ccSNobuhiro Iwamatsu 	/*
249bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
250bd3980ccSNobuhiro Iwamatsu 	 * P2 area
251bd3980ccSNobuhiro Iwamatsu 	 */
252bd3980ccSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
253bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
254bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
255bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
256bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2579751ee09SNobuhiro Iwamatsu 	}
258bd3980ccSNobuhiro Iwamatsu 
2599751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
2609751ee09SNobuhiro Iwamatsu 			  ~(32 - 1));
2619751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2629751ee09SNobuhiro Iwamatsu 
2639751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2649751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2659751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2669751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2679751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2689751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2699751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2709751ee09SNobuhiro Iwamatsu 	}
2719751ee09SNobuhiro Iwamatsu 
2729751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2739751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2749751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2759751ee09SNobuhiro Iwamatsu 
2769751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
27749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
27826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
27949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
28049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
28149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RDFFR_RDLF, RDFFR);
282903de461SYoshihiro Shimoda #endif
2839751ee09SNobuhiro Iwamatsu 
284bd3980ccSNobuhiro Iwamatsu 	return ret;
285bd3980ccSNobuhiro Iwamatsu 
286bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
287bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
288bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
289bd3980ccSNobuhiro Iwamatsu 
290bd3980ccSNobuhiro Iwamatsu err:
291bd3980ccSNobuhiro Iwamatsu 	return ret;
2929751ee09SNobuhiro Iwamatsu }
2939751ee09SNobuhiro Iwamatsu 
294bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
2959751ee09SNobuhiro Iwamatsu {
296bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
297bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2989751ee09SNobuhiro Iwamatsu 
2999751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3009751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3019751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3029751ee09SNobuhiro Iwamatsu 	}
303bd3980ccSNobuhiro Iwamatsu }
304bd3980ccSNobuhiro Iwamatsu 
305bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
306bd3980ccSNobuhiro Iwamatsu {
307bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
308bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3099751ee09SNobuhiro Iwamatsu 
3109751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3119751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3129751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3139751ee09SNobuhiro Iwamatsu 	}
3149751ee09SNobuhiro Iwamatsu 
3159751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3169751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3179751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3189751ee09SNobuhiro Iwamatsu 	}
3199751ee09SNobuhiro Iwamatsu }
3209751ee09SNobuhiro Iwamatsu 
321bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3229751ee09SNobuhiro Iwamatsu {
323bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3249751ee09SNobuhiro Iwamatsu 
325bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
326bd3980ccSNobuhiro Iwamatsu 	if (ret)
327bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
328bd3980ccSNobuhiro Iwamatsu 
329bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
330bd3980ccSNobuhiro Iwamatsu 	if (ret)
331bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
332bd3980ccSNobuhiro Iwamatsu 
333bd3980ccSNobuhiro Iwamatsu 	return ret;
334bd3980ccSNobuhiro Iwamatsu err_rx_init:
335bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
336bd3980ccSNobuhiro Iwamatsu 
337bd3980ccSNobuhiro Iwamatsu err_tx_init:
338bd3980ccSNobuhiro Iwamatsu 	return ret;
3399751ee09SNobuhiro Iwamatsu }
3409751ee09SNobuhiro Iwamatsu 
341bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3429751ee09SNobuhiro Iwamatsu {
343bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
344bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
345bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
346bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
347bd3980ccSNobuhiro Iwamatsu 
348ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
349ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3504398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
351bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
352bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
353bd3980ccSNobuhiro Iwamatsu 
354bd3980ccSNobuhiro Iwamatsu 	return ret;
3559751ee09SNobuhiro Iwamatsu }
3569751ee09SNobuhiro Iwamatsu 
357bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3589751ee09SNobuhiro Iwamatsu {
359bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
360bd1024b0SYoshihiro Shimoda 	u32 val;
361bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
362c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
363bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3649751ee09SNobuhiro Iwamatsu 
3659751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
36649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) | EDMR_EL,
36749afb8caSYoshihiro Shimoda 		     EDMR);
36849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, EESIPR);
36949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TRSCER);
37049afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, TFTR);
37149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
37249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RMCR_RST, RMCR);
37326235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
37449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, RPADIR);
375903de461SYoshihiro Shimoda #endif
37649afb8caSYoshihiro Shimoda 	sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3779751ee09SNobuhiro Iwamatsu 
3789751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
37949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, ECSIPR);
3809751ee09SNobuhiro Iwamatsu 
3819751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
382c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
383c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
38449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MAHR);
3859751ee09SNobuhiro Iwamatsu 
386c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
38749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, val, MALR);
3889751ee09SNobuhiro Iwamatsu 
38949afb8caSYoshihiro Shimoda 	sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
39026235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
39149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, 0, PIPR);
39249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, APR_AP, APR);
39349afb8caSYoshihiro Shimoda 	sh_eth_write(eth, MPR_MP, MPR);
39449afb8caSYoshihiro Shimoda 	sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
3953bb4cc31SNobuhiro Iwamatsu #endif
3963bb4cc31SNobuhiro Iwamatsu 
397*dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
39849afb8caSYoshihiro Shimoda 	sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
3994398d559SNobuhiro Iwamatsu #endif
4009751ee09SNobuhiro Iwamatsu 	/* Configure phy */
401bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
402bd3980ccSNobuhiro Iwamatsu 	if (ret) {
40388a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
404bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
405bd3980ccSNobuhiro Iwamatsu 	}
406bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
40711af8d65STimur Tabi 	ret = phy_startup(phy);
40811af8d65STimur Tabi 	if (ret) {
40911af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
41011af8d65STimur Tabi 		return ret;
41111af8d65STimur Tabi 	}
4129751ee09SNobuhiro Iwamatsu 
4133bb4cc31SNobuhiro Iwamatsu 	val = 0;
4143bb4cc31SNobuhiro Iwamatsu 
4159751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
416bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
417bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
41826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
41949afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_100B, GECMR);
4203bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7757)
42149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 1, RTRATE);
4223bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724)
4233bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4243bb4cc31SNobuhiro Iwamatsu #endif
425bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
426bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
42726235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
42849afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_10B, GECMR);
4293bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7757)
43049afb8caSYoshihiro Shimoda 		sh_eth_write(eth, 0, RTRATE);
431903de461SYoshihiro Shimoda #endif
4323bb4cc31SNobuhiro Iwamatsu 	}
43326235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4344398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4354398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
43649afb8caSYoshihiro Shimoda 		sh_eth_write(eth, GECMR_1000B, GECMR);
4374398d559SNobuhiro Iwamatsu 	}
4384398d559SNobuhiro Iwamatsu #endif
4399751ee09SNobuhiro Iwamatsu 
4409751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
441bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4429751ee09SNobuhiro Iwamatsu 		printf("Full\n");
44349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
44449afb8caSYoshihiro Shimoda 			     ECMR);
4459751ee09SNobuhiro Iwamatsu 	} else {
4469751ee09SNobuhiro Iwamatsu 		printf("Half\n");
44749afb8caSYoshihiro Shimoda 		sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
4489751ee09SNobuhiro Iwamatsu 	}
449bd3980ccSNobuhiro Iwamatsu 
450bd3980ccSNobuhiro Iwamatsu 	return ret;
451bd3980ccSNobuhiro Iwamatsu 
452bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
453bd3980ccSNobuhiro Iwamatsu 	return ret;
4549751ee09SNobuhiro Iwamatsu }
4559751ee09SNobuhiro Iwamatsu 
456bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4579751ee09SNobuhiro Iwamatsu {
4589751ee09SNobuhiro Iwamatsu 	/*
4599751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4609751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4619751ee09SNobuhiro Iwamatsu 	 */
46249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, EDRRR_R, EDRRR);
463bd3980ccSNobuhiro Iwamatsu }
4649751ee09SNobuhiro Iwamatsu 
465bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
466bd3980ccSNobuhiro Iwamatsu {
46749afb8caSYoshihiro Shimoda 	sh_eth_write(eth, ~EDRRR_R, EDRRR);
4689751ee09SNobuhiro Iwamatsu }
4699751ee09SNobuhiro Iwamatsu 
470bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4719751ee09SNobuhiro Iwamatsu {
472bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
473bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
474bd3980ccSNobuhiro Iwamatsu 
475bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
476bd3980ccSNobuhiro Iwamatsu 	if (ret)
477bd3980ccSNobuhiro Iwamatsu 		goto err;
478bd3980ccSNobuhiro Iwamatsu 
479bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
480bd3980ccSNobuhiro Iwamatsu 	if (ret)
481bd3980ccSNobuhiro Iwamatsu 		goto err;
482bd3980ccSNobuhiro Iwamatsu 
483bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
484bd3980ccSNobuhiro Iwamatsu 	if (ret)
485bd3980ccSNobuhiro Iwamatsu 		goto err_config;
486bd3980ccSNobuhiro Iwamatsu 
487bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
488bd3980ccSNobuhiro Iwamatsu 
489bd3980ccSNobuhiro Iwamatsu 	return ret;
490bd3980ccSNobuhiro Iwamatsu 
491bd3980ccSNobuhiro Iwamatsu err_config:
492bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
493bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
494bd3980ccSNobuhiro Iwamatsu 
495bd3980ccSNobuhiro Iwamatsu err:
496bd3980ccSNobuhiro Iwamatsu 	return ret;
4979751ee09SNobuhiro Iwamatsu }
4989751ee09SNobuhiro Iwamatsu 
499bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
500bd3980ccSNobuhiro Iwamatsu {
501bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
502bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
503bd3980ccSNobuhiro Iwamatsu }
504bd3980ccSNobuhiro Iwamatsu 
505bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
506bd3980ccSNobuhiro Iwamatsu {
507bd3980ccSNobuhiro Iwamatsu     int ret = 0;
508bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
509bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
510bd3980ccSNobuhiro Iwamatsu 
511bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
512bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
513bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
514bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
515bd3980ccSNobuhiro Iwamatsu 		goto err;
516bd3980ccSNobuhiro Iwamatsu 	}
517bd3980ccSNobuhiro Iwamatsu 
518bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
519bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
520bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
521bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
522bd3980ccSNobuhiro Iwamatsu 		goto err;
523bd3980ccSNobuhiro Iwamatsu 	}
524bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
525bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
526bd3980ccSNobuhiro Iwamatsu 
527bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
528bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
529bd3980ccSNobuhiro Iwamatsu 
530bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
531bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
532bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
533bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
534bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
535bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
536bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
537bd3980ccSNobuhiro Iwamatsu 
538bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
539bd3980ccSNobuhiro Iwamatsu 
540bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
541bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5429751ee09SNobuhiro Iwamatsu 
543bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
544bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
545bd1024b0SYoshihiro Shimoda 
546c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
547c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5489751ee09SNobuhiro Iwamatsu 
549bd3980ccSNobuhiro Iwamatsu 	return ret;
5509751ee09SNobuhiro Iwamatsu 
5519751ee09SNobuhiro Iwamatsu err:
552bd3980ccSNobuhiro Iwamatsu 	if (dev)
5539751ee09SNobuhiro Iwamatsu 		free(dev);
554bd3980ccSNobuhiro Iwamatsu 
555bd3980ccSNobuhiro Iwamatsu 	if (eth)
556bd3980ccSNobuhiro Iwamatsu 		free(eth);
557bd3980ccSNobuhiro Iwamatsu 
558bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
559bd3980ccSNobuhiro Iwamatsu 	return ret;
5609751ee09SNobuhiro Iwamatsu }
561bd1024b0SYoshihiro Shimoda 
562bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
563bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
564bd1024b0SYoshihiro Shimoda {
565bd1024b0SYoshihiro Shimoda 	return 0;
566bd1024b0SYoshihiro Shimoda }
567bd1024b0SYoshihiro Shimoda 
568bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
569bd1024b0SYoshihiro Shimoda {
570bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
571bd1024b0SYoshihiro Shimoda 
57249afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
573bd1024b0SYoshihiro Shimoda 
574bd1024b0SYoshihiro Shimoda 	return 0;
575bd1024b0SYoshihiro Shimoda }
576bd1024b0SYoshihiro Shimoda 
577bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
578bd1024b0SYoshihiro Shimoda {
579bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
580bd1024b0SYoshihiro Shimoda 
58149afb8caSYoshihiro Shimoda 	sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
582bd1024b0SYoshihiro Shimoda 
583bd1024b0SYoshihiro Shimoda 	return 0;
584bd1024b0SYoshihiro Shimoda }
585bd1024b0SYoshihiro Shimoda 
586bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
587bd1024b0SYoshihiro Shimoda {
588bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
589bd1024b0SYoshihiro Shimoda 
590bd1024b0SYoshihiro Shimoda 	if (v)
59149afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
592bd1024b0SYoshihiro Shimoda 	else
59349afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
594bd1024b0SYoshihiro Shimoda 
595bd1024b0SYoshihiro Shimoda 	return 0;
596bd1024b0SYoshihiro Shimoda }
597bd1024b0SYoshihiro Shimoda 
598bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
599bd1024b0SYoshihiro Shimoda {
600bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
601bd1024b0SYoshihiro Shimoda 
60249afb8caSYoshihiro Shimoda 	*v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
603bd1024b0SYoshihiro Shimoda 
604bd1024b0SYoshihiro Shimoda 	return 0;
605bd1024b0SYoshihiro Shimoda }
606bd1024b0SYoshihiro Shimoda 
607bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
608bd1024b0SYoshihiro Shimoda {
609bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
610bd1024b0SYoshihiro Shimoda 
611bd1024b0SYoshihiro Shimoda 	if (v)
61249afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
613bd1024b0SYoshihiro Shimoda 	else
61449afb8caSYoshihiro Shimoda 		sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
615bd1024b0SYoshihiro Shimoda 
616bd1024b0SYoshihiro Shimoda 	return 0;
617bd1024b0SYoshihiro Shimoda }
618bd1024b0SYoshihiro Shimoda 
619bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
620bd1024b0SYoshihiro Shimoda {
621bd1024b0SYoshihiro Shimoda 	udelay(10);
622bd1024b0SYoshihiro Shimoda 
623bd1024b0SYoshihiro Shimoda 	return 0;
624bd1024b0SYoshihiro Shimoda }
625bd1024b0SYoshihiro Shimoda 
626bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
627bd1024b0SYoshihiro Shimoda 	{
628bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
629bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
630bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
631bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
632bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
633bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
634bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
635bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
636bd1024b0SYoshihiro Shimoda 	}
637bd1024b0SYoshihiro Shimoda };
638bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
639