xref: /rk3399_rockchip-uboot/drivers/net/sh_eth.c (revision bd1024b052be8c9604b6bc101d5e147f9f280f00)
19751ee09SNobuhiro Iwamatsu /*
29751ee09SNobuhiro Iwamatsu  * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
39751ee09SNobuhiro Iwamatsu  *
49751ee09SNobuhiro Iwamatsu  * Copyright (C) 2008 Renesas Solutions Corp.
59751ee09SNobuhiro Iwamatsu  * Copyright (c) 2008 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>
28*bd1024b0SYoshihiro 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 
47bd3980ccSNobuhiro Iwamatsu #define SH_ETH_PHY_DELAY 50000
489751ee09SNobuhiro Iwamatsu 
49bd3980ccSNobuhiro Iwamatsu int sh_eth_send(struct eth_device *dev, volatile 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 */
629751ee09SNobuhiro Iwamatsu 	if ((int)packet & (4 - 1)) {
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 */
839751ee09SNobuhiro Iwamatsu 	timeout = 1000;
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 	return ret;
98bd3980ccSNobuhiro Iwamatsu err:
99bd3980ccSNobuhiro Iwamatsu 	return ret;
1009751ee09SNobuhiro Iwamatsu }
1019751ee09SNobuhiro Iwamatsu 
102bd3980ccSNobuhiro Iwamatsu int sh_eth_recv(struct eth_device *dev)
1039751ee09SNobuhiro Iwamatsu {
104bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
105bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, len = 0;
106bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1079751ee09SNobuhiro Iwamatsu 	volatile u8 *packet;
1089751ee09SNobuhiro Iwamatsu 
1099751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
1109751ee09SNobuhiro Iwamatsu 	if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
1119751ee09SNobuhiro Iwamatsu 		/* Check for errors */
1129751ee09SNobuhiro Iwamatsu 		if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
1139751ee09SNobuhiro Iwamatsu 			len = port_info->rx_desc_cur->rd1 & 0xffff;
1149751ee09SNobuhiro Iwamatsu 			packet = (volatile u8 *)
1159751ee09SNobuhiro Iwamatsu 			    ADDR_TO_P2(port_info->rx_desc_cur->rd2);
1169751ee09SNobuhiro Iwamatsu 			NetReceive(packet, len);
1179751ee09SNobuhiro Iwamatsu 		}
1189751ee09SNobuhiro Iwamatsu 
1199751ee09SNobuhiro Iwamatsu 		/* Make current descriptor available again */
1209751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1219751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1229751ee09SNobuhiro Iwamatsu 		else
1239751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur->rd0 = RD_RACT;
1249751ee09SNobuhiro Iwamatsu 
1259751ee09SNobuhiro Iwamatsu 		/* Point to the next descriptor */
1269751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur++;
1279751ee09SNobuhiro Iwamatsu 		if (port_info->rx_desc_cur >=
1289751ee09SNobuhiro Iwamatsu 		    port_info->rx_desc_base + NUM_RX_DESC)
1299751ee09SNobuhiro Iwamatsu 			port_info->rx_desc_cur = port_info->rx_desc_base;
1309751ee09SNobuhiro Iwamatsu 	}
1319751ee09SNobuhiro Iwamatsu 
1329751ee09SNobuhiro Iwamatsu 	/* Restart the receiver if disabled */
1339751ee09SNobuhiro Iwamatsu 	if (!(inl(EDRRR(port)) & EDRRR_R))
1349751ee09SNobuhiro Iwamatsu 		outl(EDRRR_R, EDRRR(port));
1359751ee09SNobuhiro Iwamatsu 
1369751ee09SNobuhiro Iwamatsu 	return len;
1379751ee09SNobuhiro Iwamatsu }
1389751ee09SNobuhiro Iwamatsu 
1399751ee09SNobuhiro Iwamatsu #define EDMR_INIT_CNT 1000
140bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1419751ee09SNobuhiro Iwamatsu {
142bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
143903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
144bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1459751ee09SNobuhiro Iwamatsu 
1469751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
1479751ee09SNobuhiro Iwamatsu 	outl(EDSR_ENALL, EDSR(port));
1489751ee09SNobuhiro Iwamatsu 
1499751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
1509751ee09SNobuhiro Iwamatsu 	outl(EDMR_SRST, EDMR(port));
1519751ee09SNobuhiro Iwamatsu 	for (i = 0; i < EDMR_INIT_CNT; i++) {
1529751ee09SNobuhiro Iwamatsu 		if (!(inl(EDMR(port)) & EDMR_SRST))
1539751ee09SNobuhiro Iwamatsu 			break;
1549751ee09SNobuhiro Iwamatsu 		udelay(1000);
1559751ee09SNobuhiro Iwamatsu 	}
1569751ee09SNobuhiro Iwamatsu 
1579751ee09SNobuhiro Iwamatsu 	if (i == EDMR_INIT_CNT) {
158bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
159bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1609751ee09SNobuhiro Iwamatsu 	}
1619751ee09SNobuhiro Iwamatsu 
162bd3980ccSNobuhiro Iwamatsu 	return ret;
163903de461SYoshihiro Shimoda #else
164903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
165903de461SYoshihiro Shimoda 	udelay(3000);
166903de461SYoshihiro Shimoda 	outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
167903de461SYoshihiro Shimoda 
168903de461SYoshihiro Shimoda 	return 0;
169903de461SYoshihiro Shimoda #endif
170bd3980ccSNobuhiro Iwamatsu }
171bd3980ccSNobuhiro Iwamatsu 
172bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1739751ee09SNobuhiro Iwamatsu {
174bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i, ret = 0;
1759751ee09SNobuhiro Iwamatsu 	u32 tmp_addr;
176bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
1779751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1789751ee09SNobuhiro Iwamatsu 
179bd3980ccSNobuhiro Iwamatsu 	/*
180bd3980ccSNobuhiro Iwamatsu 	 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
181bd3980ccSNobuhiro Iwamatsu 	 */
182bd3980ccSNobuhiro Iwamatsu 	port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
1839751ee09SNobuhiro Iwamatsu 						 sizeof(struct tx_desc_s) +
184bd3980ccSNobuhiro Iwamatsu 						 TX_DESC_SIZE - 1);
185bd3980ccSNobuhiro Iwamatsu 	if (!port_info->tx_desc_malloc) {
186bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
187bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
188bd3980ccSNobuhiro Iwamatsu 		goto err;
1899751ee09SNobuhiro Iwamatsu 	}
190bd3980ccSNobuhiro Iwamatsu 
1919751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
1929751ee09SNobuhiro Iwamatsu 			  ~(TX_DESC_SIZE - 1));
19368260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
1949751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
1959751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
1969751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
1979751ee09SNobuhiro Iwamatsu 
1989751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
1999751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2009751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2019751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2029751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2039751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2049751ee09SNobuhiro Iwamatsu 	}
2059751ee09SNobuhiro Iwamatsu 
2069751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2079751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2089751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2099751ee09SNobuhiro Iwamatsu 
2109751ee09SNobuhiro Iwamatsu 	/* Point the controller to the tx descriptor list. Must use physical
2119751ee09SNobuhiro Iwamatsu 	   addresses */
2129751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
213903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
2149751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
2159751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
2169751ee09SNobuhiro Iwamatsu 	outl(0x01, TDFFR(port));/* Last discriptor bit */
217903de461SYoshihiro Shimoda #endif
2189751ee09SNobuhiro Iwamatsu 
219bd3980ccSNobuhiro Iwamatsu err:
220bd3980ccSNobuhiro Iwamatsu 	return ret;
2219751ee09SNobuhiro Iwamatsu }
2229751ee09SNobuhiro Iwamatsu 
223bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2249751ee09SNobuhiro Iwamatsu {
225bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, i , ret = 0;
226bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
2279751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
228bd3980ccSNobuhiro Iwamatsu 	u32 tmp_addr;
2299751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2309751ee09SNobuhiro Iwamatsu 
231bd3980ccSNobuhiro Iwamatsu 	/*
232bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
233bd3980ccSNobuhiro Iwamatsu 	 */
234bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
2359751ee09SNobuhiro Iwamatsu 						 sizeof(struct rx_desc_s) +
236bd3980ccSNobuhiro Iwamatsu 						 RX_DESC_SIZE - 1);
237bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_desc_malloc) {
238bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
239bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
240bd3980ccSNobuhiro Iwamatsu 		goto err;
2419751ee09SNobuhiro Iwamatsu 	}
242bd3980ccSNobuhiro Iwamatsu 
2439751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
2449751ee09SNobuhiro Iwamatsu 			  ~(RX_DESC_SIZE - 1));
24568260aabSYoshihiro Shimoda 	flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
2469751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
2479751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
2489751ee09SNobuhiro Iwamatsu 
2499751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2509751ee09SNobuhiro Iwamatsu 
251bd3980ccSNobuhiro Iwamatsu 	/*
252bd3980ccSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be 32 bytes aligned  and in
253bd3980ccSNobuhiro Iwamatsu 	 * P2 area
254bd3980ccSNobuhiro Iwamatsu 	 */
255bd3980ccSNobuhiro Iwamatsu 	port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
256bd3980ccSNobuhiro Iwamatsu 	if (!port_info->rx_buf_malloc) {
257bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": malloc failed\n");
258bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
259bd3980ccSNobuhiro Iwamatsu 		goto err_buf_malloc;
2609751ee09SNobuhiro Iwamatsu 	}
261bd3980ccSNobuhiro Iwamatsu 
2629751ee09SNobuhiro Iwamatsu 	tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
2639751ee09SNobuhiro Iwamatsu 			  ~(32 - 1));
2649751ee09SNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
2659751ee09SNobuhiro Iwamatsu 
2669751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2679751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2689751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2699751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2709751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2719751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2729751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
2739751ee09SNobuhiro Iwamatsu 	}
2749751ee09SNobuhiro Iwamatsu 
2759751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2769751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2779751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2789751ee09SNobuhiro Iwamatsu 
2799751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
2809751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
281903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7763)
2829751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
2839751ee09SNobuhiro Iwamatsu 	outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
2849751ee09SNobuhiro Iwamatsu 	outl(RDFFR_RDLF, RDFFR(port));
285903de461SYoshihiro Shimoda #endif
2869751ee09SNobuhiro Iwamatsu 
287bd3980ccSNobuhiro Iwamatsu 	return ret;
288bd3980ccSNobuhiro Iwamatsu 
289bd3980ccSNobuhiro Iwamatsu err_buf_malloc:
290bd3980ccSNobuhiro Iwamatsu 	free(port_info->rx_desc_malloc);
291bd3980ccSNobuhiro Iwamatsu 	port_info->rx_desc_malloc = NULL;
292bd3980ccSNobuhiro Iwamatsu 
293bd3980ccSNobuhiro Iwamatsu err:
294bd3980ccSNobuhiro Iwamatsu 	return ret;
2959751ee09SNobuhiro Iwamatsu }
2969751ee09SNobuhiro Iwamatsu 
297bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
2989751ee09SNobuhiro Iwamatsu {
299bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
300bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3019751ee09SNobuhiro Iwamatsu 
3029751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_malloc) {
3039751ee09SNobuhiro Iwamatsu 		free(port_info->tx_desc_malloc);
3049751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_malloc = NULL;
3059751ee09SNobuhiro Iwamatsu 	}
306bd3980ccSNobuhiro Iwamatsu }
307bd3980ccSNobuhiro Iwamatsu 
308bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
309bd3980ccSNobuhiro Iwamatsu {
310bd3980ccSNobuhiro Iwamatsu 	int port = eth->port;
311bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
3129751ee09SNobuhiro Iwamatsu 
3139751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_malloc) {
3149751ee09SNobuhiro Iwamatsu 		free(port_info->rx_desc_malloc);
3159751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_malloc = NULL;
3169751ee09SNobuhiro Iwamatsu 	}
3179751ee09SNobuhiro Iwamatsu 
3189751ee09SNobuhiro Iwamatsu 	if (port_info->rx_buf_malloc) {
3199751ee09SNobuhiro Iwamatsu 		free(port_info->rx_buf_malloc);
3209751ee09SNobuhiro Iwamatsu 		port_info->rx_buf_malloc = NULL;
3219751ee09SNobuhiro Iwamatsu 	}
3229751ee09SNobuhiro Iwamatsu }
3239751ee09SNobuhiro Iwamatsu 
324bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3259751ee09SNobuhiro Iwamatsu {
326bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3279751ee09SNobuhiro Iwamatsu 
328bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
329bd3980ccSNobuhiro Iwamatsu 	if (ret)
330bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
331bd3980ccSNobuhiro Iwamatsu 
332bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
333bd3980ccSNobuhiro Iwamatsu 	if (ret)
334bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
335bd3980ccSNobuhiro Iwamatsu 
336bd3980ccSNobuhiro Iwamatsu 	return ret;
337bd3980ccSNobuhiro Iwamatsu err_rx_init:
338bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
339bd3980ccSNobuhiro Iwamatsu 
340bd3980ccSNobuhiro Iwamatsu err_tx_init:
341bd3980ccSNobuhiro Iwamatsu 	return ret;
3429751ee09SNobuhiro Iwamatsu }
3439751ee09SNobuhiro Iwamatsu 
344bd3980ccSNobuhiro Iwamatsu static int sh_eth_phy_config(struct sh_eth_dev *eth)
3459751ee09SNobuhiro Iwamatsu {
346*bd1024b0SYoshihiro Shimoda 	int port = eth->port, ret = 0;
347bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
348*bd1024b0SYoshihiro Shimoda 	struct eth_device *dev = port_info->dev;
349*bd1024b0SYoshihiro Shimoda 	struct phy_device *phydev;
350bd3980ccSNobuhiro Iwamatsu 
351*bd1024b0SYoshihiro Shimoda 	phydev = phy_connect(miiphy_get_dev_by_name(dev->name),
352*bd1024b0SYoshihiro Shimoda 			port_info->phy_addr, dev, PHY_INTERFACE_MODE_MII);
353*bd1024b0SYoshihiro Shimoda 	port_info->phydev = phydev;
354*bd1024b0SYoshihiro Shimoda 	phy_config(phydev);
355bd3980ccSNobuhiro Iwamatsu 
356bd3980ccSNobuhiro Iwamatsu 	return ret;
3579751ee09SNobuhiro Iwamatsu }
3589751ee09SNobuhiro Iwamatsu 
359bd3980ccSNobuhiro Iwamatsu static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
3609751ee09SNobuhiro Iwamatsu {
361bd3980ccSNobuhiro Iwamatsu 	int port = eth->port, ret = 0;
362*bd1024b0SYoshihiro Shimoda 	u32 val;
363bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[port];
364c527ce92SMike Frysinger 	struct eth_device *dev = port_info->dev;
365*bd1024b0SYoshihiro Shimoda 	struct phy_device *phy;
3669751ee09SNobuhiro Iwamatsu 
3679751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
3689751ee09SNobuhiro Iwamatsu 	outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
3699751ee09SNobuhiro Iwamatsu 	outl(0, EESIPR(port));
3709751ee09SNobuhiro Iwamatsu 	outl(0, TRSCER(port));
3719751ee09SNobuhiro Iwamatsu 	outl(0, TFTR(port));
3729751ee09SNobuhiro Iwamatsu 	outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
3739751ee09SNobuhiro Iwamatsu 	outl(RMCR_RST, RMCR(port));
374903de461SYoshihiro Shimoda #ifndef CONFIG_CPU_SH7757
3759751ee09SNobuhiro Iwamatsu 	outl(0, RPADIR(port));
376903de461SYoshihiro Shimoda #endif
3779751ee09SNobuhiro Iwamatsu 	outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
3789751ee09SNobuhiro Iwamatsu 
3799751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
380903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7757)
381903de461SYoshihiro Shimoda 	outl(ECSIPR_BRCRXIP | ECSIPR_PSRTOIP | ECSIPR_LCHNGIP |
382903de461SYoshihiro Shimoda 		ECSIPR_MPDIP | ECSIPR_ICDIP, ECSIPR(port));
383903de461SYoshihiro Shimoda #else
3849751ee09SNobuhiro Iwamatsu 	outl(0, ECSIPR(port));
385903de461SYoshihiro Shimoda #endif
3869751ee09SNobuhiro Iwamatsu 
3879751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
388c527ce92SMike Frysinger 	val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
389c527ce92SMike Frysinger 	    dev->enetaddr[2] << 8 | dev->enetaddr[3];
3909751ee09SNobuhiro Iwamatsu 	outl(val, MAHR(port));
3919751ee09SNobuhiro Iwamatsu 
392c527ce92SMike Frysinger 	val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
3939751ee09SNobuhiro Iwamatsu 	outl(val, MALR(port));
3949751ee09SNobuhiro Iwamatsu 
3959751ee09SNobuhiro Iwamatsu 	outl(RFLR_RFL_MIN, RFLR(port));
396903de461SYoshihiro Shimoda #ifndef CONFIG_CPU_SH7757
3979751ee09SNobuhiro Iwamatsu 	outl(0, PIPR(port));
398903de461SYoshihiro Shimoda #endif
3999751ee09SNobuhiro Iwamatsu 	outl(APR_AP, APR(port));
4009751ee09SNobuhiro Iwamatsu 	outl(MPR_MP, MPR(port));
401903de461SYoshihiro Shimoda #ifdef CONFIG_CPU_SH7757
402903de461SYoshihiro Shimoda 	outl(TPAUSER_UNLIMITED, TPAUSER(port));
403903de461SYoshihiro Shimoda #else
4049751ee09SNobuhiro Iwamatsu 	outl(TPAUSER_TPAUSE, TPAUSER(port));
405903de461SYoshihiro Shimoda #endif
4069751ee09SNobuhiro Iwamatsu 	/* Configure phy */
407bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_phy_config(eth);
408bd3980ccSNobuhiro Iwamatsu 	if (ret) {
40988a4c2e7SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": phy config timeout\n");
410bd3980ccSNobuhiro Iwamatsu 		goto err_phy_cfg;
411bd3980ccSNobuhiro Iwamatsu 	}
412*bd1024b0SYoshihiro Shimoda 	phy = port_info->phydev;
413*bd1024b0SYoshihiro Shimoda 	phy_startup(phy);
4149751ee09SNobuhiro Iwamatsu 
4159751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
416903de461SYoshihiro Shimoda #ifdef CONFIG_CPU_SH7763
417*bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
418bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
4199751ee09SNobuhiro Iwamatsu 		outl(GECMR_100B, GECMR(port));
420*bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
421bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
4229751ee09SNobuhiro Iwamatsu 		outl(GECMR_10B, GECMR(port));
4239751ee09SNobuhiro Iwamatsu 	}
424903de461SYoshihiro Shimoda #endif
425903de461SYoshihiro Shimoda #if defined(CONFIG_CPU_SH7757)
426*bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
427903de461SYoshihiro Shimoda 		printf("100Base/");
428903de461SYoshihiro Shimoda 		outl(1, RTRATE(port));
429*bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
430903de461SYoshihiro Shimoda 		printf("10Base/");
431903de461SYoshihiro Shimoda 		outl(0, RTRATE(port));
432903de461SYoshihiro Shimoda 	}
433903de461SYoshihiro Shimoda #endif
4349751ee09SNobuhiro Iwamatsu 
4359751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
436*bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4379751ee09SNobuhiro Iwamatsu 		printf("Full\n");
4389751ee09SNobuhiro Iwamatsu 		outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
4399751ee09SNobuhiro Iwamatsu 	} else {
4409751ee09SNobuhiro Iwamatsu 		printf("Half\n");
4419751ee09SNobuhiro Iwamatsu 		outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE),  ECMR(port));
4429751ee09SNobuhiro Iwamatsu 	}
443bd3980ccSNobuhiro Iwamatsu 
444bd3980ccSNobuhiro Iwamatsu 	return ret;
445bd3980ccSNobuhiro Iwamatsu 
446bd3980ccSNobuhiro Iwamatsu err_phy_cfg:
447bd3980ccSNobuhiro Iwamatsu 	return ret;
4489751ee09SNobuhiro Iwamatsu }
4499751ee09SNobuhiro Iwamatsu 
450bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4519751ee09SNobuhiro Iwamatsu {
4529751ee09SNobuhiro Iwamatsu 	/*
4539751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4549751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4559751ee09SNobuhiro Iwamatsu 	 */
456bd3980ccSNobuhiro Iwamatsu 	outl(EDRRR_R, EDRRR(eth->port));
457bd3980ccSNobuhiro Iwamatsu }
4589751ee09SNobuhiro Iwamatsu 
459bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
460bd3980ccSNobuhiro Iwamatsu {
461bd3980ccSNobuhiro Iwamatsu 	outl(~EDRRR_R, EDRRR(eth->port));
4629751ee09SNobuhiro Iwamatsu }
4639751ee09SNobuhiro Iwamatsu 
464bd3980ccSNobuhiro Iwamatsu int sh_eth_init(struct eth_device *dev, bd_t *bd)
4659751ee09SNobuhiro Iwamatsu {
466bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
467bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
468bd3980ccSNobuhiro Iwamatsu 
469bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
470bd3980ccSNobuhiro Iwamatsu 	if (ret)
471bd3980ccSNobuhiro Iwamatsu 		goto err;
472bd3980ccSNobuhiro Iwamatsu 
473bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
474bd3980ccSNobuhiro Iwamatsu 	if (ret)
475bd3980ccSNobuhiro Iwamatsu 		goto err;
476bd3980ccSNobuhiro Iwamatsu 
477bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_config(eth, bd);
478bd3980ccSNobuhiro Iwamatsu 	if (ret)
479bd3980ccSNobuhiro Iwamatsu 		goto err_config;
480bd3980ccSNobuhiro Iwamatsu 
481bd3980ccSNobuhiro Iwamatsu 	sh_eth_start(eth);
482bd3980ccSNobuhiro Iwamatsu 
483bd3980ccSNobuhiro Iwamatsu 	return ret;
484bd3980ccSNobuhiro Iwamatsu 
485bd3980ccSNobuhiro Iwamatsu err_config:
486bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
487bd3980ccSNobuhiro Iwamatsu 	sh_eth_rx_desc_free(eth);
488bd3980ccSNobuhiro Iwamatsu 
489bd3980ccSNobuhiro Iwamatsu err:
490bd3980ccSNobuhiro Iwamatsu 	return ret;
4919751ee09SNobuhiro Iwamatsu }
4929751ee09SNobuhiro Iwamatsu 
493bd3980ccSNobuhiro Iwamatsu void sh_eth_halt(struct eth_device *dev)
494bd3980ccSNobuhiro Iwamatsu {
495bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
496bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
497bd3980ccSNobuhiro Iwamatsu }
498bd3980ccSNobuhiro Iwamatsu 
499bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
500bd3980ccSNobuhiro Iwamatsu {
501bd3980ccSNobuhiro Iwamatsu     int ret = 0;
502bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
503bd3980ccSNobuhiro Iwamatsu     struct eth_device *dev = NULL;
504bd3980ccSNobuhiro Iwamatsu 
505bd3980ccSNobuhiro Iwamatsu     eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
506bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
507bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
508bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
509bd3980ccSNobuhiro Iwamatsu 		goto err;
510bd3980ccSNobuhiro Iwamatsu 	}
511bd3980ccSNobuhiro Iwamatsu 
512bd3980ccSNobuhiro Iwamatsu     dev = (struct eth_device *)malloc(sizeof(struct eth_device));
513bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
514bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
515bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
516bd3980ccSNobuhiro Iwamatsu 		goto err;
517bd3980ccSNobuhiro Iwamatsu 	}
518bd3980ccSNobuhiro Iwamatsu     memset(dev, 0, sizeof(struct eth_device));
519bd3980ccSNobuhiro Iwamatsu     memset(eth, 0, sizeof(struct sh_eth_dev));
520bd3980ccSNobuhiro Iwamatsu 
521bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
522bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
523bd3980ccSNobuhiro Iwamatsu 
524bd3980ccSNobuhiro Iwamatsu     dev->priv = (void *)eth;
525bd3980ccSNobuhiro Iwamatsu     dev->iobase = 0;
526bd3980ccSNobuhiro Iwamatsu     dev->init = sh_eth_init;
527bd3980ccSNobuhiro Iwamatsu     dev->halt = sh_eth_halt;
528bd3980ccSNobuhiro Iwamatsu     dev->send = sh_eth_send;
529bd3980ccSNobuhiro Iwamatsu     dev->recv = sh_eth_recv;
530bd3980ccSNobuhiro Iwamatsu     eth->port_info[eth->port].dev = dev;
531bd3980ccSNobuhiro Iwamatsu 
532bd3980ccSNobuhiro Iwamatsu 	sprintf(dev->name, SHETHER_NAME);
533bd3980ccSNobuhiro Iwamatsu 
534bd3980ccSNobuhiro Iwamatsu     /* Register Device to EtherNet subsystem  */
535bd3980ccSNobuhiro Iwamatsu     eth_register(dev);
5369751ee09SNobuhiro Iwamatsu 
537*bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
538*bd1024b0SYoshihiro Shimoda 	miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
539*bd1024b0SYoshihiro Shimoda 
540c527ce92SMike Frysinger 	if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
541c527ce92SMike Frysinger 		puts("Please set MAC address\n");
5429751ee09SNobuhiro Iwamatsu 
543bd3980ccSNobuhiro Iwamatsu 	return ret;
5449751ee09SNobuhiro Iwamatsu 
5459751ee09SNobuhiro Iwamatsu err:
546bd3980ccSNobuhiro Iwamatsu 	if (dev)
5479751ee09SNobuhiro Iwamatsu 		free(dev);
548bd3980ccSNobuhiro Iwamatsu 
549bd3980ccSNobuhiro Iwamatsu 	if (eth)
550bd3980ccSNobuhiro Iwamatsu 		free(eth);
551bd3980ccSNobuhiro Iwamatsu 
552bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
553bd3980ccSNobuhiro Iwamatsu 	return ret;
5549751ee09SNobuhiro Iwamatsu }
555*bd1024b0SYoshihiro Shimoda 
556*bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
557*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
558*bd1024b0SYoshihiro Shimoda {
559*bd1024b0SYoshihiro Shimoda 	return 0;
560*bd1024b0SYoshihiro Shimoda }
561*bd1024b0SYoshihiro Shimoda 
562*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
563*bd1024b0SYoshihiro Shimoda {
564*bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
565*bd1024b0SYoshihiro Shimoda 	int port = eth->port;
566*bd1024b0SYoshihiro Shimoda 
567*bd1024b0SYoshihiro Shimoda 	outl(inl(PIR(port)) | PIR_MMD, PIR(port));
568*bd1024b0SYoshihiro Shimoda 
569*bd1024b0SYoshihiro Shimoda 	return 0;
570*bd1024b0SYoshihiro Shimoda }
571*bd1024b0SYoshihiro Shimoda 
572*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
573*bd1024b0SYoshihiro Shimoda {
574*bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
575*bd1024b0SYoshihiro Shimoda 	int port = eth->port;
576*bd1024b0SYoshihiro Shimoda 
577*bd1024b0SYoshihiro Shimoda 	outl(inl(PIR(port)) & ~PIR_MMD, PIR(port));
578*bd1024b0SYoshihiro Shimoda 
579*bd1024b0SYoshihiro Shimoda 	return 0;
580*bd1024b0SYoshihiro Shimoda }
581*bd1024b0SYoshihiro Shimoda 
582*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
583*bd1024b0SYoshihiro Shimoda {
584*bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
585*bd1024b0SYoshihiro Shimoda 	int port = eth->port;
586*bd1024b0SYoshihiro Shimoda 
587*bd1024b0SYoshihiro Shimoda 	if (v)
588*bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) | PIR_MDO, PIR(port));
589*bd1024b0SYoshihiro Shimoda 	else
590*bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) & ~PIR_MDO, PIR(port));
591*bd1024b0SYoshihiro Shimoda 
592*bd1024b0SYoshihiro Shimoda 	return 0;
593*bd1024b0SYoshihiro Shimoda }
594*bd1024b0SYoshihiro Shimoda 
595*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
596*bd1024b0SYoshihiro Shimoda {
597*bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
598*bd1024b0SYoshihiro Shimoda 	int port = eth->port;
599*bd1024b0SYoshihiro Shimoda 
600*bd1024b0SYoshihiro Shimoda 	*v = (inl(PIR(port)) & PIR_MDI) >> 3;
601*bd1024b0SYoshihiro Shimoda 
602*bd1024b0SYoshihiro Shimoda 	return 0;
603*bd1024b0SYoshihiro Shimoda }
604*bd1024b0SYoshihiro Shimoda 
605*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
606*bd1024b0SYoshihiro Shimoda {
607*bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
608*bd1024b0SYoshihiro Shimoda 	int port = eth->port;
609*bd1024b0SYoshihiro Shimoda 
610*bd1024b0SYoshihiro Shimoda 	if (v)
611*bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) | PIR_MDC, PIR(port));
612*bd1024b0SYoshihiro Shimoda 	else
613*bd1024b0SYoshihiro Shimoda 		outl(inl(PIR(port)) & ~PIR_MDC, PIR(port));
614*bd1024b0SYoshihiro Shimoda 
615*bd1024b0SYoshihiro Shimoda 	return 0;
616*bd1024b0SYoshihiro Shimoda }
617*bd1024b0SYoshihiro Shimoda 
618*bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
619*bd1024b0SYoshihiro Shimoda {
620*bd1024b0SYoshihiro Shimoda 	udelay(10);
621*bd1024b0SYoshihiro Shimoda 
622*bd1024b0SYoshihiro Shimoda 	return 0;
623*bd1024b0SYoshihiro Shimoda }
624*bd1024b0SYoshihiro Shimoda 
625*bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
626*bd1024b0SYoshihiro Shimoda 	{
627*bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
628*bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
629*bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
630*bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
631*bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
632*bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
633*bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
634*bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
635*bd1024b0SYoshihiro Shimoda 	}
636*bd1024b0SYoshihiro Shimoda };
637*bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
638