xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision 11af8d65274df736deeb651d12e0763eec527ea5)
19751ee09SNobuhiro Iwamatsu /*
29751ee09SNobuhiro Iwamatsu  * sh_eth.c - Driver for Renesas SH7763's 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 */
799751ee09SNobuhiro Iwamatsu 	if (!(inl(EDTRR(port)) & EDTRR_TRNS))
809751ee09SNobuhiro Iwamatsu 		outl(EDTRR_TRNS, EDTRR(port));
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 */
1329751ee09SNobuhiro Iwamatsu 	if (!(inl(EDRRR(port)) & EDRRR_R))
1339751ee09SNobuhiro Iwamatsu 		outl(EDRRR_R, EDRRR(port));
1349751ee09SNobuhiro Iwamatsu 
1359751ee09SNobuhiro Iwamatsu 	return len;
1369751ee09SNobuhiro Iwamatsu }
1379751ee09SNobuhiro Iwamatsu 
138bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1399751ee09SNobuhiro Iwamatsu {
140bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
141ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
142bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1439751ee09SNobuhiro Iwamatsu 
1449751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
1459751ee09SNobuhiro Iwamatsu 	outl(EDSR_ENALL, EDSR(port));
1469751ee09SNobuhiro Iwamatsu 
1479751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
1489751ee09SNobuhiro Iwamatsu 	outl(EDMR_SRST, EDMR(port));
1494ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT ; i++) {
1509751ee09SNobuhiro Iwamatsu 		if (!(inl(EDMR(port)) & EDMR_SRST))
1519751ee09SNobuhiro Iwamatsu 			break;
1529751ee09SNobuhiro Iwamatsu 		udelay(1000);
1539751ee09SNobuhiro Iwamatsu 	}
1549751ee09SNobuhiro Iwamatsu 
1554ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
156bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
157bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1589751ee09SNobuhiro Iwamatsu 	}
1599751ee09SNobuhiro Iwamatsu 
160bd3980ccSNobuhiro Iwamatsu 	return ret;
161903de461SYoshihiro Shimoda #else
162903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
163903de461SYoshihiro Shimoda 	udelay(3000);
164903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
165903de461SYoshihiro Shimoda 
166903de461SYoshihiro Shimoda 	return 0;
167903de461SYoshihiro Shimoda #endif
168bd3980ccSNobuhiro Iwamatsu }
169bd3980ccSNobuhiro Iwamatsu 
170bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1719751ee09SNobuhiro Iwamatsu {
172bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1739751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
174bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1759751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1769751ee09SNobuhiro Iwamatsu 
177bd3980ccSNobuhiro Iwamatsu 	/*
178bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
179bd3980ccSNobuhiro Iwamatsu 	 */
180bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1819751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
182bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
183bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
184bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
185bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
186bd3980ccSNobuhiro Iwamatsu 		goto err;
1879751ee09SNobuhiro Iwamatsu 	}
188bd3980ccSNobuhiro Iwamatsu 
1899751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
1909751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
19168260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
1929751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
1939751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
1949751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
1959751ee09SNobuhiro Iwamatsu 
1969751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
1979751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
1989751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
1999751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2009751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2019751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2029751ee09SNobuhiro Iwamatsu 	}
2039751ee09SNobuhiro Iwamatsu 
2049751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2059751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2069751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2079751ee09SNobuhiro Iwamatsu 
2089751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2099751ee09SNobuhiro Iwamatsu 	   addresses */
2109751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
211ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
2129751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
2139751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
2149751ee09SNobuhiro Iwamatsu 	outl(0x01, TDFFR(port));/* Last discriptor bit */
215903de461SYoshihiro Shimoda #endif
2169751ee09SNobuhiro Iwamatsu 
217bd3980ccSNobuhiro Iwamatsu err:
218bd3980ccSNobuhiro Iwamatsu 	return ret;
2199751ee09SNobuhiro Iwamatsu }
2209751ee09SNobuhiro Iwamatsu 
221bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2229751ee09SNobuhiro Iwamatsu {
223bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
224bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2259751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
226bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2279751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2289751ee09SNobuhiro Iwamatsu 
229bd3980ccSNobuhiro Iwamatsu 	/*
230bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
231bd3980ccSNobuhiro Iwamatsu 	 */
232bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2339751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
234bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
235bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
236bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
237bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
238bd3980ccSNobuhiro Iwamatsu 		goto err;
2399751ee09SNobuhiro Iwamatsu 	}
240bd3980ccSNobuhiro Iwamatsu 
2419751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2429751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
24368260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2449751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2459751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2469751ee09SNobuhiro Iwamatsu 
2479751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2489751ee09SNobuhiro Iwamatsu 
249bd3980ccSNobuhiro Iwamatsu 	/*
250bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
251bd3980ccSNobuhiro Iwamatsu 	 * P2 area
252bd3980ccSNobuhiro Iwamatsu 	 */
253bd3980ccSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
254bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
255bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
256bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
257bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2589751ee09SNobuhiro Iwamatsu 	}
259bd3980ccSNobuhiro Iwamatsu 
2609751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
2619751ee09SNobuhiro Iwamatsu 			  ~(32 - 1));
2629751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2639751ee09SNobuhiro Iwamatsu 
2649751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2659751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2669751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2679751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2689751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2699751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2709751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2719751ee09SNobuhiro Iwamatsu 	}
2729751ee09SNobuhiro Iwamatsu 
2739751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2749751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2759751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2769751ee09SNobuhiro Iwamatsu 
2779751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
2789751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
279ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
2809751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
2819751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
2829751ee09SNobuhiro Iwamatsu 	outl(RDFFR_RDLF, RDFFR(port));
283903de461SYoshihiro Shimoda #endif
2849751ee09SNobuhiro Iwamatsu 
285bd3980ccSNobuhiro Iwamatsu 	return ret;
286bd3980ccSNobuhiro Iwamatsu 
287bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
288bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
289bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
290bd3980ccSNobuhiro Iwamatsu 
291bd3980ccSNobuhiro Iwamatsu err:
292bd3980ccSNobuhiro Iwamatsu 	return ret;
2939751ee09SNobuhiro Iwamatsu }
2949751ee09SNobuhiro Iwamatsu 
295bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
2969751ee09SNobuhiro Iwamatsu {
297bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
298bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2999751ee09SNobuhiro Iwamatsu 
3009751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3019751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3029751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3039751ee09SNobuhiro Iwamatsu 	}
304bd3980ccSNobuhiro Iwamatsu }
305bd3980ccSNobuhiro Iwamatsu 
306bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
307bd3980ccSNobuhiro Iwamatsu {
308bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
309bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3109751ee09SNobuhiro Iwamatsu 
3119751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3129751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3139751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3149751ee09SNobuhiro Iwamatsu 	}
3159751ee09SNobuhiro Iwamatsu 
3169751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3179751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3189751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3199751ee09SNobuhiro Iwamatsu 	}
3209751ee09SNobuhiro Iwamatsu }
3219751ee09SNobuhiro Iwamatsu 
322bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3239751ee09SNobuhiro Iwamatsu {
324bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3259751ee09SNobuhiro Iwamatsu 
326bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
327bd3980ccSNobuhiro Iwamatsu 	if (ret)
328bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
329bd3980ccSNobuhiro Iwamatsu 
330bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
331bd3980ccSNobuhiro Iwamatsu 	if (ret)
332bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
333bd3980ccSNobuhiro Iwamatsu 
334bd3980ccSNobuhiro Iwamatsu 	return ret;
335bd3980ccSNobuhiro Iwamatsu err_rx_init:
336bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
337bd3980ccSNobuhiro Iwamatsu 
338bd3980ccSNobuhiro Iwamatsu err_tx_init:
339bd3980ccSNobuhiro Iwamatsu 	return ret;
3409751ee09SNobuhiro Iwamatsu }
3419751ee09SNobuhiro Iwamatsu 
342bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3439751ee09SNobuhiro Iwamatsu {
344bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
345bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
346bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
347bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
348bd3980ccSNobuhiro Iwamatsu 
349ee6ec5d4SNobuhiro Iwamatsu 	phydev = phy_connect(
350ee6ec5d4SNobuhiro Iwamatsu 			miiphy_get_dev_by_name(dev->name),
3514398d559SNobuhiro Iwamatsu 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
352bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
353bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
354bd3980ccSNobuhiro Iwamatsu 
355bd3980ccSNobuhiro Iwamatsu 	return ret;
3569751ee09SNobuhiro Iwamatsu }
3579751ee09SNobuhiro Iwamatsu 
358bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3599751ee09SNobuhiro Iwamatsu {
360bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
361bd1024b0SYoshihiro Shimoda 	u32 val;
362bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
363c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
364bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3659751ee09SNobuhiro Iwamatsu 
3669751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
3679751ee09SNobuhiro Iwamatsu 	outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
3689751ee09SNobuhiro Iwamatsu 	outl(0, EESIPR(port));
3699751ee09SNobuhiro Iwamatsu 	outl(0, TRSCER(port));
3709751ee09SNobuhiro Iwamatsu 	outl(0, TFTR(port));
3719751ee09SNobuhiro Iwamatsu 	outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
3729751ee09SNobuhiro Iwamatsu 	outl(RMCR_RST, RMCR(port));
3733bb4cc31SNobuhiro Iwamatsu #if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
3749751ee09SNobuhiro Iwamatsu 	outl(0, RPADIR(port));
375903de461SYoshihiro Shimoda #endif
3769751ee09SNobuhiro Iwamatsu 	outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
3779751ee09SNobuhiro Iwamatsu 
3789751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
379903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7757)
380903de461SYoshihiro Shimoda 	outl(ECSIPR_BRCRXIP | ECSIPR_PSRTOIP | ECSIPR_LCHNGIP |
381903de461SYoshihiro Shimoda 		ECSIPR_MPDIP | ECSIPR_ICDIP, ECSIPR(port));
382903de461SYoshihiro Shimoda #else
3839751ee09SNobuhiro Iwamatsu 	outl(0, ECSIPR(port));
384903de461SYoshihiro Shimoda #endif
3859751ee09SNobuhiro Iwamatsu 
3869751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
387c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
388c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
3899751ee09SNobuhiro Iwamatsu 	outl(val, MAHR(port));
3909751ee09SNobuhiro Iwamatsu 
391c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
3929751ee09SNobuhiro Iwamatsu 	outl(val, MALR(port));
3939751ee09SNobuhiro Iwamatsu 
3949751ee09SNobuhiro Iwamatsu 	outl(RFLR_RFL_MIN, RFLR(port));
3953bb4cc31SNobuhiro Iwamatsu #if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
3969751ee09SNobuhiro Iwamatsu 	outl(0, PIPR(port));
397903de461SYoshihiro Shimoda #endif
3983bb4cc31SNobuhiro Iwamatsu #if !defined(CONFIG_CPU_SH7724)
3999751ee09SNobuhiro Iwamatsu 	outl(APR_AP, APR(port));
4009751ee09SNobuhiro Iwamatsu 	outl(MPR_MP, MPR(port));
401903de461SYoshihiro Shimoda #endif
402ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
4033bb4cc31SNobuhiro Iwamatsu 	outl(TPAUSER_TPAUSE, TPAUSER(port));
4043bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7757)
4053bb4cc31SNobuhiro Iwamatsu 	outl(TPAUSER_UNLIMITED, TPAUSER(port));
4063bb4cc31SNobuhiro Iwamatsu #endif
4073bb4cc31SNobuhiro Iwamatsu 
4084398d559SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734)
4094398d559SNobuhiro Iwamatsu 	outl(CONFIG_SH_ETHER_SH7734_MII, RMII_MII(port));
4104398d559SNobuhiro Iwamatsu #endif
4119751ee09SNobuhiro Iwamatsu 	/* Configure phy */
412bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
413bd3980ccSNobuhiro Iwamatsu 	if (ret) {
41488a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
415bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
416bd3980ccSNobuhiro Iwamatsu 	}
417bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
418*11af8d65STimur Tabi 	ret = phy_startup(phy);
419*11af8d65STimur Tabi 	if (ret) {
420*11af8d65STimur Tabi 		printf(SHETHER_NAME ": phy startup failure\n");
421*11af8d65STimur Tabi 		return ret;
422*11af8d65STimur Tabi 	}
4239751ee09SNobuhiro Iwamatsu 
4243bb4cc31SNobuhiro Iwamatsu 	val = 0;
4253bb4cc31SNobuhiro Iwamatsu 
4269751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
427bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
428bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
429ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
4309751ee09SNobuhiro Iwamatsu 		outl(GECMR_100B, GECMR(port));
4313bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7757)
4323bb4cc31SNobuhiro Iwamatsu 		outl(1, RTRATE(port));
4333bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7724)
4343bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4353bb4cc31SNobuhiro Iwamatsu #endif
436bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
437bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
438ee6ec5d4SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
4399751ee09SNobuhiro Iwamatsu 		outl(GECMR_10B, GECMR(port));
4403bb4cc31SNobuhiro Iwamatsu #elif defined(CONFIG_CPU_SH7757)
441903de461SYoshihiro Shimoda 		outl(0, RTRATE(port));
442903de461SYoshihiro Shimoda #endif
4433bb4cc31SNobuhiro Iwamatsu 	}
4444398d559SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
4454398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4464398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
4474398d559SNobuhiro Iwamatsu 		outl(GECMR_1000B, GECMR(port));
4484398d559SNobuhiro Iwamatsu 	}
4494398d559SNobuhiro Iwamatsu #endif
4509751ee09SNobuhiro Iwamatsu 
4519751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
452bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4539751ee09SNobuhiro Iwamatsu 		printf("Full\n");
4543bb4cc31SNobuhiro Iwamatsu 		outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
4559751ee09SNobuhiro Iwamatsu 	} else {
4569751ee09SNobuhiro Iwamatsu 		printf("Half\n");
4573bb4cc31SNobuhiro Iwamatsu 		outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE),  ECMR(port));
4589751ee09SNobuhiro Iwamatsu 	}
459bd3980ccSNobuhiro Iwamatsu 
460bd3980ccSNobuhiro Iwamatsu 	return ret;
461bd3980ccSNobuhiro Iwamatsu 
462bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
463bd3980ccSNobuhiro Iwamatsu 	return ret;
4649751ee09SNobuhiro Iwamatsu }
4659751ee09SNobuhiro Iwamatsu 
466bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4679751ee09SNobuhiro Iwamatsu {
4689751ee09SNobuhiro Iwamatsu 	/*
4699751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4709751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4719751ee09SNobuhiro Iwamatsu 	 */
472bd3980ccSNobuhiro Iwamatsu 	outl(EDRRR_R, EDRRR(eth->port));
473bd3980ccSNobuhiro Iwamatsu }
4749751ee09SNobuhiro Iwamatsu 
475bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
476bd3980ccSNobuhiro Iwamatsu {
477bd3980ccSNobuhiro Iwamatsu 	outl(~EDRRR_R, EDRRR(eth->port));
4789751ee09SNobuhiro Iwamatsu }
4799751ee09SNobuhiro Iwamatsu 
480bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4819751ee09SNobuhiro Iwamatsu {
482bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
483bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
484bd3980ccSNobuhiro Iwamatsu 
485bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
486bd3980ccSNobuhiro Iwamatsu 	if (ret)
487bd3980ccSNobuhiro Iwamatsu 		goto err;
488bd3980ccSNobuhiro Iwamatsu 
489bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
490bd3980ccSNobuhiro Iwamatsu 	if (ret)
491bd3980ccSNobuhiro Iwamatsu 		goto err;
492bd3980ccSNobuhiro Iwamatsu 
493bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
494bd3980ccSNobuhiro Iwamatsu 	if (ret)
495bd3980ccSNobuhiro Iwamatsu 		goto err_config;
496bd3980ccSNobuhiro Iwamatsu 
497bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
498bd3980ccSNobuhiro Iwamatsu 
499bd3980ccSNobuhiro Iwamatsu 	return ret;
500bd3980ccSNobuhiro Iwamatsu 
501bd3980ccSNobuhiro Iwamatsu err_config:
502bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
503bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
504bd3980ccSNobuhiro Iwamatsu 
505bd3980ccSNobuhiro Iwamatsu err:
506bd3980ccSNobuhiro Iwamatsu 	return ret;
5079751ee09SNobuhiro Iwamatsu }
5089751ee09SNobuhiro Iwamatsu 
509bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
510bd3980ccSNobuhiro Iwamatsu {
511bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
512bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
513bd3980ccSNobuhiro Iwamatsu }
514bd3980ccSNobuhiro Iwamatsu 
515bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
516bd3980ccSNobuhiro Iwamatsu {
517bd3980ccSNobuhiro Iwamatsu     int ret = 0;
518bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
519bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
520bd3980ccSNobuhiro Iwamatsu 
521bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
522bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
523bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
524bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
525bd3980ccSNobuhiro Iwamatsu 		goto err;
526bd3980ccSNobuhiro Iwamatsu 	}
527bd3980ccSNobuhiro Iwamatsu 
528bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
529bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
530bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
531bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
532bd3980ccSNobuhiro Iwamatsu 		goto err;
533bd3980ccSNobuhiro Iwamatsu 	}
534bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
535bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
536bd3980ccSNobuhiro Iwamatsu 
537bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
538bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
539bd3980ccSNobuhiro Iwamatsu 
540bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
541bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
542bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
543bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
544bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
545bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
546bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
547bd3980ccSNobuhiro Iwamatsu 
548bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
549bd3980ccSNobuhiro Iwamatsu 
550bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
551bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5529751ee09SNobuhiro Iwamatsu 
553bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
554bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
555bd1024b0SYoshihiro Shimoda 
556c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
557c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5589751ee09SNobuhiro Iwamatsu 
559bd3980ccSNobuhiro Iwamatsu 	return ret;
5609751ee09SNobuhiro Iwamatsu 
5619751ee09SNobuhiro Iwamatsu err:
562bd3980ccSNobuhiro Iwamatsu 	if (dev)
5639751ee09SNobuhiro Iwamatsu 		free(dev);
564bd3980ccSNobuhiro Iwamatsu 
565bd3980ccSNobuhiro Iwamatsu 	if (eth)
566bd3980ccSNobuhiro Iwamatsu 		free(eth);
567bd3980ccSNobuhiro Iwamatsu 
568bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
569bd3980ccSNobuhiro Iwamatsu 	return ret;
5709751ee09SNobuhiro Iwamatsu }
571bd1024b0SYoshihiro Shimoda 
572bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
573bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
574bd1024b0SYoshihiro Shimoda {
575bd1024b0SYoshihiro Shimoda 	return 0;
576bd1024b0SYoshihiro Shimoda }
577bd1024b0SYoshihiro Shimoda 
578bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
579bd1024b0SYoshihiro Shimoda {
580bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
581bd1024b0SYoshihiro Shimoda 	int port = eth->port;
582bd1024b0SYoshihiro Shimoda 
583bd1024b0SYoshihiro Shimoda 	outl(inl(PIR(port)) | PIR_MMD, PIR(port));
584bd1024b0SYoshihiro Shimoda 
585bd1024b0SYoshihiro Shimoda 	return 0;
586bd1024b0SYoshihiro Shimoda }
587bd1024b0SYoshihiro Shimoda 
588bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
589bd1024b0SYoshihiro Shimoda {
590bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
591bd1024b0SYoshihiro Shimoda 	int port = eth->port;
592bd1024b0SYoshihiro Shimoda 
593bd1024b0SYoshihiro Shimoda 	outl(inl(PIR(port)) & ~PIR_MMD, PIR(port));
594bd1024b0SYoshihiro Shimoda 
595bd1024b0SYoshihiro Shimoda 	return 0;
596bd1024b0SYoshihiro Shimoda }
597bd1024b0SYoshihiro Shimoda 
598bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
599bd1024b0SYoshihiro Shimoda {
600bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
601bd1024b0SYoshihiro Shimoda 	int port = eth->port;
602bd1024b0SYoshihiro Shimoda 
603bd1024b0SYoshihiro Shimoda 	if (v)
604bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) | PIR_MDO, PIR(port));
605bd1024b0SYoshihiro Shimoda 	else
606bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) & ~PIR_MDO, PIR(port));
607bd1024b0SYoshihiro Shimoda 
608bd1024b0SYoshihiro Shimoda 	return 0;
609bd1024b0SYoshihiro Shimoda }
610bd1024b0SYoshihiro Shimoda 
611bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
612bd1024b0SYoshihiro Shimoda {
613bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
614bd1024b0SYoshihiro Shimoda 	int port = eth->port;
615bd1024b0SYoshihiro Shimoda 
616bd1024b0SYoshihiro Shimoda 	*v = (inl(PIR(port)) & PIR_MDI) >> 3;
617bd1024b0SYoshihiro Shimoda 
618bd1024b0SYoshihiro Shimoda 	return 0;
619bd1024b0SYoshihiro Shimoda }
620bd1024b0SYoshihiro Shimoda 
621bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
622bd1024b0SYoshihiro Shimoda {
623bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
624bd1024b0SYoshihiro Shimoda 	int port = eth->port;
625bd1024b0SYoshihiro Shimoda 
626bd1024b0SYoshihiro Shimoda 	if (v)
627bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) | PIR_MDC, PIR(port));
628bd1024b0SYoshihiro Shimoda 	else
629bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) & ~PIR_MDC, PIR(port));
630bd1024b0SYoshihiro Shimoda 
631bd1024b0SYoshihiro Shimoda 	return 0;
632bd1024b0SYoshihiro Shimoda }
633bd1024b0SYoshihiro Shimoda 
634bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
635bd1024b0SYoshihiro Shimoda {
636bd1024b0SYoshihiro Shimoda 	udelay(10);
637bd1024b0SYoshihiro Shimoda 
638bd1024b0SYoshihiro Shimoda 	return 0;
639bd1024b0SYoshihiro Shimoda }
640bd1024b0SYoshihiro Shimoda 
641bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
642bd1024b0SYoshihiro Shimoda 	{
643bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
644bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
645bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
646bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
647bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
648bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
649bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
650bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
651bd1024b0SYoshihiro Shimoda 	}
652bd1024b0SYoshihiro Shimoda };
653bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
654