xref: /openbmc/u-boot/drivers/net/sh_eth.c (revision dc7df68f21093039b3ab1a1702f5b7cd3e621431)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
29751ee09SNobuhiro Iwamatsu /*
31cc0a9f4SRobert P. J. Day  * sh_eth.c - Driver for Renesas ethernet controller.
49751ee09SNobuhiro Iwamatsu  *
53bb4cc31SNobuhiro Iwamatsu  * Copyright (C) 2008, 2011 Renesas Solutions Corp.
6f7ca1f76SNobuhiro Iwamatsu  * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
79751ee09SNobuhiro Iwamatsu  * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
8f7ca1f76SNobuhiro Iwamatsu  * Copyright (C) 2013, 2014 Renesas Electronics Corporation
99751ee09SNobuhiro Iwamatsu  */
109751ee09SNobuhiro Iwamatsu 
119751ee09SNobuhiro Iwamatsu #include <config.h>
129751ee09SNobuhiro Iwamatsu #include <common.h>
139925f1dbSAlex Kiernan #include <environment.h>
149751ee09SNobuhiro Iwamatsu #include <malloc.h>
159751ee09SNobuhiro Iwamatsu #include <net.h>
16bd3980ccSNobuhiro Iwamatsu #include <netdev.h>
17bd1024b0SYoshihiro Shimoda #include <miiphy.h>
181221ce45SMasahiro Yamada #include <linux/errno.h>
199751ee09SNobuhiro Iwamatsu #include <asm/io.h>
209751ee09SNobuhiro Iwamatsu 
2131920264SMarek Vasut #ifdef CONFIG_DM_ETH
2231920264SMarek Vasut #include <clk.h>
2331920264SMarek Vasut #include <dm.h>
2431920264SMarek Vasut #include <linux/mii.h>
2531920264SMarek Vasut #include <asm/gpio.h>
2631920264SMarek Vasut #endif
2731920264SMarek Vasut 
289751ee09SNobuhiro Iwamatsu #include "sh_eth.h"
299751ee09SNobuhiro Iwamatsu 
309751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_USE_PORT
319751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_USE_PORT"
329751ee09SNobuhiro Iwamatsu #endif
339751ee09SNobuhiro Iwamatsu #ifndef CONFIG_SH_ETHER_PHY_ADDR
349751ee09SNobuhiro Iwamatsu # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
359751ee09SNobuhiro Iwamatsu #endif
36870cc23fSNobuhiro Iwamatsu 
3792f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
3868260aabSYoshihiro Shimoda #define flush_cache_wback(addr, len)    \
39aae5d237SNobuhiro Iwamatsu 		flush_dcache_range((u32)addr, \
40aae5d237SNobuhiro Iwamatsu 		(u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
4168260aabSYoshihiro Shimoda #else
4268260aabSYoshihiro Shimoda #define flush_cache_wback(...)
4368260aabSYoshihiro Shimoda #endif
449751ee09SNobuhiro Iwamatsu 
4592f07134SNobuhiro Iwamatsu #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
4692f07134SNobuhiro Iwamatsu #define invalidate_cache(addr, len)		\
4792f07134SNobuhiro Iwamatsu 	{	\
4892f07134SNobuhiro Iwamatsu 		u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE;	\
4992f07134SNobuhiro Iwamatsu 		u32 start, end;	\
5092f07134SNobuhiro Iwamatsu 		\
5192f07134SNobuhiro Iwamatsu 		start = (u32)addr;	\
5292f07134SNobuhiro Iwamatsu 		end = start + len;	\
5392f07134SNobuhiro Iwamatsu 		start &= ~(line_size - 1);	\
5492f07134SNobuhiro Iwamatsu 		end = ((end + line_size - 1) & ~(line_size - 1));	\
5592f07134SNobuhiro Iwamatsu 		\
5692f07134SNobuhiro Iwamatsu 		invalidate_dcache_range(start, end);	\
5792f07134SNobuhiro Iwamatsu 	}
5892f07134SNobuhiro Iwamatsu #else
5992f07134SNobuhiro Iwamatsu #define invalidate_cache(...)
6092f07134SNobuhiro Iwamatsu #endif
6192f07134SNobuhiro Iwamatsu 
624ba62c72SNobuhiro Iwamatsu #define TIMEOUT_CNT 1000
634ba62c72SNobuhiro Iwamatsu 
sh_eth_send_common(struct sh_eth_dev * eth,void * packet,int len)64dca221bdSMarek Vasut static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
659751ee09SNobuhiro Iwamatsu {
663c5a7b75SMarek Vasut 	int ret = 0, timeout;
673c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
689751ee09SNobuhiro Iwamatsu 
699751ee09SNobuhiro Iwamatsu 	if (!packet || len > 0xffff) {
70bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
71bd3980ccSNobuhiro Iwamatsu 		ret = -EINVAL;
72bd3980ccSNobuhiro Iwamatsu 		goto err;
739751ee09SNobuhiro Iwamatsu 	}
749751ee09SNobuhiro Iwamatsu 
759751ee09SNobuhiro Iwamatsu 	/* packet must be a 4 byte boundary */
76ee6ec5d4SNobuhiro Iwamatsu 	if ((int)packet & 3) {
77dc14867dSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
78e2752db0SNobuhiro Iwamatsu 				, __func__);
79bd3980ccSNobuhiro Iwamatsu 		ret = -EFAULT;
80bd3980ccSNobuhiro Iwamatsu 		goto err;
819751ee09SNobuhiro Iwamatsu 	}
829751ee09SNobuhiro Iwamatsu 
839751ee09SNobuhiro Iwamatsu 	/* Update tx descriptor */
8468260aabSYoshihiro Shimoda 	flush_cache_wback(packet, len);
859751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
869751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur->td1 = len << 16;
879751ee09SNobuhiro Iwamatsu 	/* Must preserve the end of descriptor list indication */
889751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur->td0 & TD_TDLE)
899751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
909751ee09SNobuhiro Iwamatsu 	else
919751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
929751ee09SNobuhiro Iwamatsu 
93f7ca1f76SNobuhiro Iwamatsu 	flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
94f7ca1f76SNobuhiro Iwamatsu 
959751ee09SNobuhiro Iwamatsu 	/* Restart the transmitter if disabled */
96fbfb5115SNobuhiro Iwamatsu 	if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
97fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
989751ee09SNobuhiro Iwamatsu 
999751ee09SNobuhiro Iwamatsu 	/* Wait until packet is transmitted */
1004ba62c72SNobuhiro Iwamatsu 	timeout = TIMEOUT_CNT;
10192f07134SNobuhiro Iwamatsu 	do {
10292f07134SNobuhiro Iwamatsu 		invalidate_cache(port_info->tx_desc_cur,
10392f07134SNobuhiro Iwamatsu 				 sizeof(struct tx_desc_s));
1049751ee09SNobuhiro Iwamatsu 		udelay(100);
10592f07134SNobuhiro Iwamatsu 	} while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
1069751ee09SNobuhiro Iwamatsu 
1079751ee09SNobuhiro Iwamatsu 	if (timeout < 0) {
108bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": transmit timeout\n");
109bd3980ccSNobuhiro Iwamatsu 		ret = -ETIMEDOUT;
1109751ee09SNobuhiro Iwamatsu 		goto err;
1119751ee09SNobuhiro Iwamatsu 	}
1129751ee09SNobuhiro Iwamatsu 
1139751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur++;
1149751ee09SNobuhiro Iwamatsu 	if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
1159751ee09SNobuhiro Iwamatsu 		port_info->tx_desc_cur = port_info->tx_desc_base;
1169751ee09SNobuhiro Iwamatsu 
117bd3980ccSNobuhiro Iwamatsu err:
118bd3980ccSNobuhiro Iwamatsu 	return ret;
1199751ee09SNobuhiro Iwamatsu }
1209751ee09SNobuhiro Iwamatsu 
sh_eth_recv_start(struct sh_eth_dev * eth)12152c15e22SMarek Vasut static int sh_eth_recv_start(struct sh_eth_dev *eth)
122dca221bdSMarek Vasut {
1233c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
1249751ee09SNobuhiro Iwamatsu 
1259751ee09SNobuhiro Iwamatsu 	/* Check if the rx descriptor is ready */
12692f07134SNobuhiro Iwamatsu 	invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
12752c15e22SMarek Vasut 	if (port_info->rx_desc_cur->rd0 & RD_RACT)
12852c15e22SMarek Vasut 		return -EINVAL;
12952c15e22SMarek Vasut 
1309751ee09SNobuhiro Iwamatsu 	/* Check for errors */
13152c15e22SMarek Vasut 	if (port_info->rx_desc_cur->rd0 & RD_RFE)
13252c15e22SMarek Vasut 		return -EINVAL;
13352c15e22SMarek Vasut 
13460279b57SMarek Vasut 	return port_info->rx_desc_cur->rd1 & 0xffff;
1359751ee09SNobuhiro Iwamatsu }
1369751ee09SNobuhiro Iwamatsu 
sh_eth_recv_finish(struct sh_eth_dev * eth)13752c15e22SMarek Vasut static void sh_eth_recv_finish(struct sh_eth_dev *eth)
13852c15e22SMarek Vasut {
13952c15e22SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
14052c15e22SMarek Vasut 
1419751ee09SNobuhiro Iwamatsu 	/* Make current descriptor available again */
1429751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_cur->rd0 & RD_RDLE)
1439751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
1449751ee09SNobuhiro Iwamatsu 	else
1459751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur->rd0 = RD_RACT;
146f7ca1f76SNobuhiro Iwamatsu 
147f7ca1f76SNobuhiro Iwamatsu 	flush_cache_wback(port_info->rx_desc_cur,
148f7ca1f76SNobuhiro Iwamatsu 			  sizeof(struct rx_desc_s));
149f7ca1f76SNobuhiro Iwamatsu 
1509751ee09SNobuhiro Iwamatsu 	/* Point to the next descriptor */
1519751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur++;
1529751ee09SNobuhiro Iwamatsu 	if (port_info->rx_desc_cur >=
1539751ee09SNobuhiro Iwamatsu 	    port_info->rx_desc_base + NUM_RX_DESC)
1549751ee09SNobuhiro Iwamatsu 		port_info->rx_desc_cur = port_info->rx_desc_base;
1559751ee09SNobuhiro Iwamatsu }
1569751ee09SNobuhiro Iwamatsu 
sh_eth_reset(struct sh_eth_dev * eth)157bd3980ccSNobuhiro Iwamatsu static int sh_eth_reset(struct sh_eth_dev *eth)
1589751ee09SNobuhiro Iwamatsu {
159fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
16062cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
161bd3980ccSNobuhiro Iwamatsu 	int ret = 0, i;
1629751ee09SNobuhiro Iwamatsu 
1639751ee09SNobuhiro Iwamatsu 	/* Start e-dmac transmitter and receiver */
164fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, EDSR_ENALL, EDSR);
1659751ee09SNobuhiro Iwamatsu 
1669751ee09SNobuhiro Iwamatsu 	/* Perform a software reset and wait for it to complete */
167fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, EDMR_SRST, EDMR);
1684ba62c72SNobuhiro Iwamatsu 	for (i = 0; i < TIMEOUT_CNT; i++) {
169fbfb5115SNobuhiro Iwamatsu 		if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
1709751ee09SNobuhiro Iwamatsu 			break;
1719751ee09SNobuhiro Iwamatsu 		udelay(1000);
1729751ee09SNobuhiro Iwamatsu 	}
1739751ee09SNobuhiro Iwamatsu 
1744ba62c72SNobuhiro Iwamatsu 	if (i == TIMEOUT_CNT) {
175bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME  ": Software reset timeout\n");
176bd3980ccSNobuhiro Iwamatsu 		ret = -EIO;
1779751ee09SNobuhiro Iwamatsu 	}
1789751ee09SNobuhiro Iwamatsu 
179bd3980ccSNobuhiro Iwamatsu 	return ret;
180903de461SYoshihiro Shimoda #else
181fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
1825262767dSMarek Vasut 	mdelay(3);
183fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info,
184fbfb5115SNobuhiro Iwamatsu 		     sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
185903de461SYoshihiro Shimoda 
186903de461SYoshihiro Shimoda 	return 0;
187903de461SYoshihiro Shimoda #endif
188bd3980ccSNobuhiro Iwamatsu }
189bd3980ccSNobuhiro Iwamatsu 
sh_eth_tx_desc_init(struct sh_eth_dev * eth)190bd3980ccSNobuhiro Iwamatsu static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
1919751ee09SNobuhiro Iwamatsu {
1923c5a7b75SMarek Vasut 	int i, ret = 0;
193000889cdSNobuhiro Iwamatsu 	u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
1943c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
1959751ee09SNobuhiro Iwamatsu 	struct tx_desc_s *cur_tx_desc;
1969751ee09SNobuhiro Iwamatsu 
197bd3980ccSNobuhiro Iwamatsu 	/*
198703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
199703949e4SNobuhiro Iwamatsu 	 * tx_desc_s.
200bd3980ccSNobuhiro Iwamatsu 	 */
201000889cdSNobuhiro Iwamatsu 	port_info->tx_desc_alloc =
202000889cdSNobuhiro Iwamatsu 		memalign(sizeof(struct tx_desc_s), alloc_desc_size);
203000889cdSNobuhiro Iwamatsu 	if (!port_info->tx_desc_alloc) {
204000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": memalign failed\n");
205bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
206bd3980ccSNobuhiro Iwamatsu 		goto err;
2079751ee09SNobuhiro Iwamatsu 	}
208bd3980ccSNobuhiro Iwamatsu 
209aae5d237SNobuhiro Iwamatsu 	flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
210000889cdSNobuhiro Iwamatsu 
2119751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
212000889cdSNobuhiro Iwamatsu 	port_info->tx_desc_base =
213000889cdSNobuhiro Iwamatsu 		(struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
2149751ee09SNobuhiro Iwamatsu 	port_info->tx_desc_cur = port_info->tx_desc_base;
2159751ee09SNobuhiro Iwamatsu 
2169751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2179751ee09SNobuhiro Iwamatsu 	for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
2189751ee09SNobuhiro Iwamatsu 	     cur_tx_desc++, i++) {
2199751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td0 = 0x00;
2209751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td1 = 0x00;
2219751ee09SNobuhiro Iwamatsu 		cur_tx_desc->td2 = 0x00;
2229751ee09SNobuhiro Iwamatsu 	}
2239751ee09SNobuhiro Iwamatsu 
2249751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2259751ee09SNobuhiro Iwamatsu 	cur_tx_desc--;
2269751ee09SNobuhiro Iwamatsu 	cur_tx_desc->td0 |= TD_TDLE;
2279751ee09SNobuhiro Iwamatsu 
228dc14867dSNobuhiro Iwamatsu 	/*
229dc14867dSNobuhiro Iwamatsu 	 * Point the controller to the tx descriptor list. Must use physical
230dc14867dSNobuhiro Iwamatsu 	 * addresses
231dc14867dSNobuhiro Iwamatsu 	 */
232fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
23362cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
234fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
235fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
236fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
237903de461SYoshihiro Shimoda #endif
2389751ee09SNobuhiro Iwamatsu 
239bd3980ccSNobuhiro Iwamatsu err:
240bd3980ccSNobuhiro Iwamatsu 	return ret;
2419751ee09SNobuhiro Iwamatsu }
2429751ee09SNobuhiro Iwamatsu 
sh_eth_rx_desc_init(struct sh_eth_dev * eth)243bd3980ccSNobuhiro Iwamatsu static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
2449751ee09SNobuhiro Iwamatsu {
2453c5a7b75SMarek Vasut 	int i, ret = 0;
246000889cdSNobuhiro Iwamatsu 	u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
2473c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
2489751ee09SNobuhiro Iwamatsu 	struct rx_desc_s *cur_rx_desc;
2499751ee09SNobuhiro Iwamatsu 	u8 *rx_buf;
2509751ee09SNobuhiro Iwamatsu 
251bd3980ccSNobuhiro Iwamatsu 	/*
252703949e4SNobuhiro Iwamatsu 	 * Allocate rx descriptors. They must be aligned to size of struct
253703949e4SNobuhiro Iwamatsu 	 * rx_desc_s.
254bd3980ccSNobuhiro Iwamatsu 	 */
255000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_alloc =
256000889cdSNobuhiro Iwamatsu 		memalign(sizeof(struct rx_desc_s), alloc_desc_size);
257000889cdSNobuhiro Iwamatsu 	if (!port_info->rx_desc_alloc) {
258000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": memalign failed\n");
259bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
260bd3980ccSNobuhiro Iwamatsu 		goto err;
2619751ee09SNobuhiro Iwamatsu 	}
262bd3980ccSNobuhiro Iwamatsu 
263000889cdSNobuhiro Iwamatsu 	flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
264000889cdSNobuhiro Iwamatsu 
2659751ee09SNobuhiro Iwamatsu 	/* Make sure we use a P2 address (non-cacheable) */
266000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_base =
267000889cdSNobuhiro Iwamatsu 		(struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
2689751ee09SNobuhiro Iwamatsu 
2699751ee09SNobuhiro Iwamatsu 	port_info->rx_desc_cur = port_info->rx_desc_base;
2709751ee09SNobuhiro Iwamatsu 
271bd3980ccSNobuhiro Iwamatsu 	/*
272000889cdSNobuhiro Iwamatsu 	 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
273000889cdSNobuhiro Iwamatsu 	 * aligned and in P2 area.
274bd3980ccSNobuhiro Iwamatsu 	 */
275000889cdSNobuhiro Iwamatsu 	port_info->rx_buf_alloc =
276000889cdSNobuhiro Iwamatsu 		memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
277000889cdSNobuhiro Iwamatsu 	if (!port_info->rx_buf_alloc) {
278000889cdSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": alloc failed\n");
279bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
280000889cdSNobuhiro Iwamatsu 		goto err_buf_alloc;
2819751ee09SNobuhiro Iwamatsu 	}
282bd3980ccSNobuhiro Iwamatsu 
283000889cdSNobuhiro Iwamatsu 	port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
2849751ee09SNobuhiro Iwamatsu 
2859751ee09SNobuhiro Iwamatsu 	/* Initialize all descriptors */
2869751ee09SNobuhiro Iwamatsu 	for (cur_rx_desc = port_info->rx_desc_base,
2879751ee09SNobuhiro Iwamatsu 	     rx_buf = port_info->rx_buf_base, i = 0;
2889751ee09SNobuhiro Iwamatsu 	     i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
2899751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd0 = RD_RACT;
2909751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
2919751ee09SNobuhiro Iwamatsu 		cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
2929751ee09SNobuhiro Iwamatsu 	}
2939751ee09SNobuhiro Iwamatsu 
2949751ee09SNobuhiro Iwamatsu 	/* Mark the end of the descriptors */
2959751ee09SNobuhiro Iwamatsu 	cur_rx_desc--;
2969751ee09SNobuhiro Iwamatsu 	cur_rx_desc->rd0 |= RD_RDLE;
2979751ee09SNobuhiro Iwamatsu 
2989751ee09SNobuhiro Iwamatsu 	/* Point the controller to the rx descriptor list */
299fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
30062cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
301fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
302fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
303fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
304903de461SYoshihiro Shimoda #endif
3059751ee09SNobuhiro Iwamatsu 
306bd3980ccSNobuhiro Iwamatsu 	return ret;
307bd3980ccSNobuhiro Iwamatsu 
308000889cdSNobuhiro Iwamatsu err_buf_alloc:
309000889cdSNobuhiro Iwamatsu 	free(port_info->rx_desc_alloc);
310000889cdSNobuhiro Iwamatsu 	port_info->rx_desc_alloc = NULL;
311bd3980ccSNobuhiro Iwamatsu 
312bd3980ccSNobuhiro Iwamatsu err:
313bd3980ccSNobuhiro Iwamatsu 	return ret;
3149751ee09SNobuhiro Iwamatsu }
3159751ee09SNobuhiro Iwamatsu 
sh_eth_tx_desc_free(struct sh_eth_dev * eth)316bd3980ccSNobuhiro Iwamatsu static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
3179751ee09SNobuhiro Iwamatsu {
3183c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
3199751ee09SNobuhiro Iwamatsu 
320000889cdSNobuhiro Iwamatsu 	if (port_info->tx_desc_alloc) {
321000889cdSNobuhiro Iwamatsu 		free(port_info->tx_desc_alloc);
322000889cdSNobuhiro Iwamatsu 		port_info->tx_desc_alloc = NULL;
3239751ee09SNobuhiro Iwamatsu 	}
324bd3980ccSNobuhiro Iwamatsu }
325bd3980ccSNobuhiro Iwamatsu 
sh_eth_rx_desc_free(struct sh_eth_dev * eth)326bd3980ccSNobuhiro Iwamatsu static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
327bd3980ccSNobuhiro Iwamatsu {
3283c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
3299751ee09SNobuhiro Iwamatsu 
330000889cdSNobuhiro Iwamatsu 	if (port_info->rx_desc_alloc) {
331000889cdSNobuhiro Iwamatsu 		free(port_info->rx_desc_alloc);
332000889cdSNobuhiro Iwamatsu 		port_info->rx_desc_alloc = NULL;
3339751ee09SNobuhiro Iwamatsu 	}
3349751ee09SNobuhiro Iwamatsu 
335000889cdSNobuhiro Iwamatsu 	if (port_info->rx_buf_alloc) {
336000889cdSNobuhiro Iwamatsu 		free(port_info->rx_buf_alloc);
337000889cdSNobuhiro Iwamatsu 		port_info->rx_buf_alloc = NULL;
3389751ee09SNobuhiro Iwamatsu 	}
3399751ee09SNobuhiro Iwamatsu }
3409751ee09SNobuhiro Iwamatsu 
sh_eth_desc_init(struct sh_eth_dev * eth)341bd3980ccSNobuhiro Iwamatsu static int sh_eth_desc_init(struct sh_eth_dev *eth)
3429751ee09SNobuhiro Iwamatsu {
343bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
3449751ee09SNobuhiro Iwamatsu 
345bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_tx_desc_init(eth);
346bd3980ccSNobuhiro Iwamatsu 	if (ret)
347bd3980ccSNobuhiro Iwamatsu 		goto err_tx_init;
348bd3980ccSNobuhiro Iwamatsu 
349bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_rx_desc_init(eth);
350bd3980ccSNobuhiro Iwamatsu 	if (ret)
351bd3980ccSNobuhiro Iwamatsu 		goto err_rx_init;
352bd3980ccSNobuhiro Iwamatsu 
353bd3980ccSNobuhiro Iwamatsu 	return ret;
354bd3980ccSNobuhiro Iwamatsu err_rx_init:
355bd3980ccSNobuhiro Iwamatsu 	sh_eth_tx_desc_free(eth);
356bd3980ccSNobuhiro Iwamatsu 
357bd3980ccSNobuhiro Iwamatsu err_tx_init:
358bd3980ccSNobuhiro Iwamatsu 	return ret;
3599751ee09SNobuhiro Iwamatsu }
3609751ee09SNobuhiro Iwamatsu 
sh_eth_write_hwaddr(struct sh_eth_info * port_info,unsigned char * mac)36168ac92e9SMarek Vasut static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
36268ac92e9SMarek Vasut 				unsigned char *mac)
36368ac92e9SMarek Vasut {
36468ac92e9SMarek Vasut 	u32 val;
36568ac92e9SMarek Vasut 
36668ac92e9SMarek Vasut 	val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
36768ac92e9SMarek Vasut 	sh_eth_write(port_info, val, MAHR);
36868ac92e9SMarek Vasut 
36968ac92e9SMarek Vasut 	val = (mac[4] << 8) | mac[5];
37068ac92e9SMarek Vasut 	sh_eth_write(port_info, val, MALR);
37168ac92e9SMarek Vasut }
37268ac92e9SMarek Vasut 
sh_eth_mac_regs_config(struct sh_eth_dev * eth,unsigned char * mac)373013af64fSMarek Vasut static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
3749751ee09SNobuhiro Iwamatsu {
375013af64fSMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
3769751ee09SNobuhiro Iwamatsu 
3779751ee09SNobuhiro Iwamatsu 	/* Configure e-dmac registers */
378fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
379f8b7507dSNobuhiro Iwamatsu 			(EMDR_DESC | EDMR_EL), EDMR);
380f8b7507dSNobuhiro Iwamatsu 
381fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, EESIPR);
382fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, TRSCER);
383fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, TFTR);
384fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
385fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, RMCR_RST, RMCR);
38662cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
387fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, RPADIR);
388903de461SYoshihiro Shimoda #endif
389fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
3909751ee09SNobuhiro Iwamatsu 
3919751ee09SNobuhiro Iwamatsu 	/* Configure e-mac registers */
392fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, ECSIPR);
3939751ee09SNobuhiro Iwamatsu 
3949751ee09SNobuhiro Iwamatsu 	/* Set Mac address */
395013af64fSMarek Vasut 	sh_eth_write_hwaddr(port_info, mac);
3969751ee09SNobuhiro Iwamatsu 
397fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
39826235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
399fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, 0, PIPR);
40062cbddc4SNobuhiro Iwamatsu #endif
40162cbddc4SNobuhiro Iwamatsu #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
402fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, APR_AP, APR);
403fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, MPR_MP, MPR);
404fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
4053bb4cc31SNobuhiro Iwamatsu #endif
4063bb4cc31SNobuhiro Iwamatsu 
407dcd5a593SNobuhiro Iwamatsu #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
408fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
409effb7902SMarek Vasut #elif defined(CONFIG_RCAR_GEN2)
410fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
4114398d559SNobuhiro Iwamatsu #endif
41211af8d65STimur Tabi }
4139751ee09SNobuhiro Iwamatsu 
sh_eth_phy_regs_config(struct sh_eth_dev * eth)414013af64fSMarek Vasut static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
415013af64fSMarek Vasut {
416013af64fSMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
417013af64fSMarek Vasut 	struct phy_device *phy = port_info->phydev;
418013af64fSMarek Vasut 	int ret = 0;
419013af64fSMarek Vasut 	u32 val = 0;
4203bb4cc31SNobuhiro Iwamatsu 
4219751ee09SNobuhiro Iwamatsu 	/* Set the transfer speed */
422bd1024b0SYoshihiro Shimoda 	if (phy->speed == 100) {
423bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 100Base/");
42426235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
425fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, GECMR_100B, GECMR);
426e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
427fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, 1, RTRATE);
428effb7902SMarek Vasut #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2)
4293bb4cc31SNobuhiro Iwamatsu 		val = ECMR_RTM;
4303bb4cc31SNobuhiro Iwamatsu #endif
431bd1024b0SYoshihiro Shimoda 	} else if (phy->speed == 10) {
432bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 10Base/");
43326235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
434fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, GECMR_10B, GECMR);
435e3bb3254SYoshihiro Shimoda #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
436fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, 0, RTRATE);
437903de461SYoshihiro Shimoda #endif
4383bb4cc31SNobuhiro Iwamatsu 	}
43926235093SYoshihiro Shimoda #if defined(SH_ETH_TYPE_GETHER)
4404398d559SNobuhiro Iwamatsu 	else if (phy->speed == 1000) {
4414398d559SNobuhiro Iwamatsu 		printf(SHETHER_NAME ": 1000Base/");
442fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info, GECMR_1000B, GECMR);
4434398d559SNobuhiro Iwamatsu 	}
4444398d559SNobuhiro Iwamatsu #endif
4459751ee09SNobuhiro Iwamatsu 
4469751ee09SNobuhiro Iwamatsu 	/* Check if full duplex mode is supported by the phy */
447bd1024b0SYoshihiro Shimoda 	if (phy->duplex) {
4489751ee09SNobuhiro Iwamatsu 		printf("Full\n");
449fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
450dc14867dSNobuhiro Iwamatsu 			     val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
45149afb8caSYoshihiro Shimoda 			     ECMR);
4529751ee09SNobuhiro Iwamatsu 	} else {
4539751ee09SNobuhiro Iwamatsu 		printf("Half\n");
454fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
455dc14867dSNobuhiro Iwamatsu 			     val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
456dc14867dSNobuhiro Iwamatsu 			     ECMR);
4579751ee09SNobuhiro Iwamatsu 	}
458bd3980ccSNobuhiro Iwamatsu 
459bd3980ccSNobuhiro Iwamatsu 	return ret;
4609751ee09SNobuhiro Iwamatsu }
4619751ee09SNobuhiro Iwamatsu 
sh_eth_start(struct sh_eth_dev * eth)462bd3980ccSNobuhiro Iwamatsu static void sh_eth_start(struct sh_eth_dev *eth)
4639751ee09SNobuhiro Iwamatsu {
464fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
465fbfb5115SNobuhiro Iwamatsu 
4669751ee09SNobuhiro Iwamatsu 	/*
4679751ee09SNobuhiro Iwamatsu 	 * Enable the e-dmac receiver only. The transmitter will be enabled when
4689751ee09SNobuhiro Iwamatsu 	 * we have something to transmit
4699751ee09SNobuhiro Iwamatsu 	 */
470fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, EDRRR_R, EDRRR);
471bd3980ccSNobuhiro Iwamatsu }
4729751ee09SNobuhiro Iwamatsu 
sh_eth_stop(struct sh_eth_dev * eth)473bd3980ccSNobuhiro Iwamatsu static void sh_eth_stop(struct sh_eth_dev *eth)
474bd3980ccSNobuhiro Iwamatsu {
475fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
476fbfb5115SNobuhiro Iwamatsu 
477fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, ~EDRRR_R, EDRRR);
4789751ee09SNobuhiro Iwamatsu }
4799751ee09SNobuhiro Iwamatsu 
sh_eth_init_common(struct sh_eth_dev * eth,unsigned char * mac)480013af64fSMarek Vasut static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
4819751ee09SNobuhiro Iwamatsu {
482bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
483bd3980ccSNobuhiro Iwamatsu 
484bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_reset(eth);
485bd3980ccSNobuhiro Iwamatsu 	if (ret)
486013af64fSMarek Vasut 		return ret;
487bd3980ccSNobuhiro Iwamatsu 
488bd3980ccSNobuhiro Iwamatsu 	ret = sh_eth_desc_init(eth);
489bd3980ccSNobuhiro Iwamatsu 	if (ret)
490bd3980ccSNobuhiro Iwamatsu 		return ret;
491bd3980ccSNobuhiro Iwamatsu 
492013af64fSMarek Vasut 	sh_eth_mac_regs_config(eth, mac);
493bd3980ccSNobuhiro Iwamatsu 
494013af64fSMarek Vasut 	return 0;
495013af64fSMarek Vasut }
496013af64fSMarek Vasut 
sh_eth_start_common(struct sh_eth_dev * eth)497013af64fSMarek Vasut static int sh_eth_start_common(struct sh_eth_dev *eth)
498013af64fSMarek Vasut {
499013af64fSMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
500013af64fSMarek Vasut 	int ret;
501013af64fSMarek Vasut 
502013af64fSMarek Vasut 	ret = phy_startup(port_info->phydev);
503013af64fSMarek Vasut 	if (ret) {
504013af64fSMarek Vasut 		printf(SHETHER_NAME ": phy startup failure\n");
505bd3980ccSNobuhiro Iwamatsu 		return ret;
5069751ee09SNobuhiro Iwamatsu 	}
5079751ee09SNobuhiro Iwamatsu 
508013af64fSMarek Vasut 	ret = sh_eth_phy_regs_config(eth);
509013af64fSMarek Vasut 	if (ret)
510013af64fSMarek Vasut 		return ret;
511013af64fSMarek Vasut 
512013af64fSMarek Vasut 	sh_eth_start(eth);
513013af64fSMarek Vasut 
514013af64fSMarek Vasut 	return 0;
515013af64fSMarek Vasut }
516013af64fSMarek Vasut 
51731920264SMarek Vasut #ifndef CONFIG_DM_ETH
sh_eth_phy_config_legacy(struct sh_eth_dev * eth)518a220784bSMarek Vasut static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
519a220784bSMarek Vasut {
5203c5a7b75SMarek Vasut 	int ret = 0;
5213c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
522a220784bSMarek Vasut 	struct eth_device *dev = port_info->dev;
523a220784bSMarek Vasut 	struct phy_device *phydev;
524a220784bSMarek Vasut 
525a220784bSMarek Vasut 	phydev = phy_connect(
526a220784bSMarek Vasut 			miiphy_get_dev_by_name(dev->name),
527a220784bSMarek Vasut 			port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
528a220784bSMarek Vasut 	port_info->phydev = phydev;
529a220784bSMarek Vasut 	phy_config(phydev);
530a220784bSMarek Vasut 
531a220784bSMarek Vasut 	return ret;
532a220784bSMarek Vasut }
533a220784bSMarek Vasut 
sh_eth_send_legacy(struct eth_device * dev,void * packet,int len)534a220784bSMarek Vasut static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
535a220784bSMarek Vasut {
536a220784bSMarek Vasut 	struct sh_eth_dev *eth = dev->priv;
537a220784bSMarek Vasut 
538a220784bSMarek Vasut 	return sh_eth_send_common(eth, packet, len);
539a220784bSMarek Vasut }
540a220784bSMarek Vasut 
sh_eth_recv_common(struct sh_eth_dev * eth)541a220784bSMarek Vasut static int sh_eth_recv_common(struct sh_eth_dev *eth)
542a220784bSMarek Vasut {
5433c5a7b75SMarek Vasut 	int len = 0;
5443c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
545a220784bSMarek Vasut 	uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
546a220784bSMarek Vasut 
547a220784bSMarek Vasut 	len = sh_eth_recv_start(eth);
548a220784bSMarek Vasut 	if (len > 0) {
549a220784bSMarek Vasut 		invalidate_cache(packet, len);
550a220784bSMarek Vasut 		net_process_received_packet(packet, len);
551a220784bSMarek Vasut 		sh_eth_recv_finish(eth);
552a220784bSMarek Vasut 	} else
553a220784bSMarek Vasut 		len = 0;
554a220784bSMarek Vasut 
555a220784bSMarek Vasut 	/* Restart the receiver if disabled */
556a220784bSMarek Vasut 	if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
557a220784bSMarek Vasut 		sh_eth_write(port_info, EDRRR_R, EDRRR);
558a220784bSMarek Vasut 
559a220784bSMarek Vasut 	return len;
560a220784bSMarek Vasut }
561a220784bSMarek Vasut 
sh_eth_recv_legacy(struct eth_device * dev)562a220784bSMarek Vasut static int sh_eth_recv_legacy(struct eth_device *dev)
563a220784bSMarek Vasut {
564a220784bSMarek Vasut 	struct sh_eth_dev *eth = dev->priv;
565a220784bSMarek Vasut 
566a220784bSMarek Vasut 	return sh_eth_recv_common(eth);
567a220784bSMarek Vasut }
568a220784bSMarek Vasut 
sh_eth_init_legacy(struct eth_device * dev,bd_t * bd)569013af64fSMarek Vasut static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd)
570013af64fSMarek Vasut {
571013af64fSMarek Vasut 	struct sh_eth_dev *eth = dev->priv;
572013af64fSMarek Vasut 	int ret;
573013af64fSMarek Vasut 
574013af64fSMarek Vasut 	ret = sh_eth_init_common(eth, dev->enetaddr);
575013af64fSMarek Vasut 	if (ret)
576013af64fSMarek Vasut 		return ret;
577013af64fSMarek Vasut 
578013af64fSMarek Vasut 	ret = sh_eth_phy_config_legacy(eth);
579013af64fSMarek Vasut 	if (ret) {
580013af64fSMarek Vasut 		printf(SHETHER_NAME ": phy config timeout\n");
581013af64fSMarek Vasut 		goto err_start;
582013af64fSMarek Vasut 	}
583013af64fSMarek Vasut 
584013af64fSMarek Vasut 	ret = sh_eth_start_common(eth);
585013af64fSMarek Vasut 	if (ret)
586013af64fSMarek Vasut 		goto err_start;
587013af64fSMarek Vasut 
588013af64fSMarek Vasut 	return 0;
589013af64fSMarek Vasut 
590013af64fSMarek Vasut err_start:
591013af64fSMarek Vasut 	sh_eth_tx_desc_free(eth);
592013af64fSMarek Vasut 	sh_eth_rx_desc_free(eth);
593013af64fSMarek Vasut 	return ret;
594013af64fSMarek Vasut }
595013af64fSMarek Vasut 
sh_eth_halt_legacy(struct eth_device * dev)596013af64fSMarek Vasut void sh_eth_halt_legacy(struct eth_device *dev)
597bd3980ccSNobuhiro Iwamatsu {
598bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = dev->priv;
599dc14867dSNobuhiro Iwamatsu 
600bd3980ccSNobuhiro Iwamatsu 	sh_eth_stop(eth);
601bd3980ccSNobuhiro Iwamatsu }
602bd3980ccSNobuhiro Iwamatsu 
sh_eth_initialize(bd_t * bd)603bd3980ccSNobuhiro Iwamatsu int sh_eth_initialize(bd_t *bd)
604bd3980ccSNobuhiro Iwamatsu {
605bd3980ccSNobuhiro Iwamatsu 	int ret = 0;
606bd3980ccSNobuhiro Iwamatsu 	struct sh_eth_dev *eth = NULL;
607bd3980ccSNobuhiro Iwamatsu 	struct eth_device *dev = NULL;
608dc14867dSNobuhiro Iwamatsu 	struct mii_dev *mdiodev;
609bd3980ccSNobuhiro Iwamatsu 
610bd3980ccSNobuhiro Iwamatsu 	eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
611bd3980ccSNobuhiro Iwamatsu 	if (!eth) {
612bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
613bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
614bd3980ccSNobuhiro Iwamatsu 		goto err;
615bd3980ccSNobuhiro Iwamatsu 	}
616bd3980ccSNobuhiro Iwamatsu 
617bd3980ccSNobuhiro Iwamatsu 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
618bd3980ccSNobuhiro Iwamatsu 	if (!dev) {
619bd3980ccSNobuhiro Iwamatsu 		printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
620bd3980ccSNobuhiro Iwamatsu 		ret = -ENOMEM;
621bd3980ccSNobuhiro Iwamatsu 		goto err;
622bd3980ccSNobuhiro Iwamatsu 	}
623bd3980ccSNobuhiro Iwamatsu 	memset(dev, 0, sizeof(struct eth_device));
624bd3980ccSNobuhiro Iwamatsu 	memset(eth, 0, sizeof(struct sh_eth_dev));
625bd3980ccSNobuhiro Iwamatsu 
626bd3980ccSNobuhiro Iwamatsu 	eth->port = CONFIG_SH_ETHER_USE_PORT;
627bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
628fbfb5115SNobuhiro Iwamatsu 	eth->port_info[eth->port].iobase =
629fbfb5115SNobuhiro Iwamatsu 		(void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
630bd3980ccSNobuhiro Iwamatsu 
631bd3980ccSNobuhiro Iwamatsu 	dev->priv = (void *)eth;
632bd3980ccSNobuhiro Iwamatsu 	dev->iobase = 0;
633013af64fSMarek Vasut 	dev->init = sh_eth_init_legacy;
634013af64fSMarek Vasut 	dev->halt = sh_eth_halt_legacy;
635dca221bdSMarek Vasut 	dev->send = sh_eth_send_legacy;
636dca221bdSMarek Vasut 	dev->recv = sh_eth_recv_legacy;
637bd3980ccSNobuhiro Iwamatsu 	eth->port_info[eth->port].dev = dev;
638bd3980ccSNobuhiro Iwamatsu 
639192bc694SBen Whitten 	strcpy(dev->name, SHETHER_NAME);
640bd3980ccSNobuhiro Iwamatsu 
641bd3980ccSNobuhiro Iwamatsu 	/* Register Device to EtherNet subsystem  */
642bd3980ccSNobuhiro Iwamatsu 	eth_register(dev);
6439751ee09SNobuhiro Iwamatsu 
644bd1024b0SYoshihiro Shimoda 	bb_miiphy_buses[0].priv = eth;
645dc14867dSNobuhiro Iwamatsu 	mdiodev = mdio_alloc();
6465a49f174SJoe Hershberger 	if (!mdiodev)
6475a49f174SJoe Hershberger 		return -ENOMEM;
6485a49f174SJoe Hershberger 	strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
6495a49f174SJoe Hershberger 	mdiodev->read = bb_miiphy_read;
6505a49f174SJoe Hershberger 	mdiodev->write = bb_miiphy_write;
6515a49f174SJoe Hershberger 
652dc14867dSNobuhiro Iwamatsu 	ret = mdio_register(mdiodev);
653dc14867dSNobuhiro Iwamatsu 	if (ret < 0)
654dc14867dSNobuhiro Iwamatsu 		return ret;
655bd1024b0SYoshihiro Shimoda 
65635affd7aSSimon Glass 	if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
657c527ce92SMike Frysinger 		puts("Please set MAC address\n");
6589751ee09SNobuhiro Iwamatsu 
659bd3980ccSNobuhiro Iwamatsu 	return ret;
6609751ee09SNobuhiro Iwamatsu 
6619751ee09SNobuhiro Iwamatsu err:
662bd3980ccSNobuhiro Iwamatsu 	if (dev)
6639751ee09SNobuhiro Iwamatsu 		free(dev);
664bd3980ccSNobuhiro Iwamatsu 
665bd3980ccSNobuhiro Iwamatsu 	if (eth)
666bd3980ccSNobuhiro Iwamatsu 		free(eth);
667bd3980ccSNobuhiro Iwamatsu 
668bd3980ccSNobuhiro Iwamatsu 	printf(SHETHER_NAME ": Failed\n");
669bd3980ccSNobuhiro Iwamatsu 	return ret;
6709751ee09SNobuhiro Iwamatsu }
671bd1024b0SYoshihiro Shimoda 
67231920264SMarek Vasut #else /* CONFIG_DM_ETH */
67331920264SMarek Vasut 
67431920264SMarek Vasut struct sh_ether_priv {
67531920264SMarek Vasut 	struct sh_eth_dev	shdev;
67631920264SMarek Vasut 
67731920264SMarek Vasut 	struct mii_dev		*bus;
6785abcbd78SMarek Vasut 	phys_addr_t		iobase;
67931920264SMarek Vasut 	struct clk		clk;
68031920264SMarek Vasut 	struct gpio_desc	reset_gpio;
68131920264SMarek Vasut };
68231920264SMarek Vasut 
sh_ether_send(struct udevice * dev,void * packet,int len)68331920264SMarek Vasut static int sh_ether_send(struct udevice *dev, void *packet, int len)
68431920264SMarek Vasut {
68531920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
68631920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
68731920264SMarek Vasut 
68831920264SMarek Vasut 	return sh_eth_send_common(eth, packet, len);
68931920264SMarek Vasut }
69031920264SMarek Vasut 
sh_ether_recv(struct udevice * dev,int flags,uchar ** packetp)69131920264SMarek Vasut static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp)
69231920264SMarek Vasut {
69331920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
69431920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
69531920264SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
69631920264SMarek Vasut 	uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
69731920264SMarek Vasut 	int len;
69831920264SMarek Vasut 
69931920264SMarek Vasut 	len = sh_eth_recv_start(eth);
70031920264SMarek Vasut 	if (len > 0) {
70131920264SMarek Vasut 		invalidate_cache(packet, len);
70231920264SMarek Vasut 		*packetp = packet;
70331920264SMarek Vasut 
70431920264SMarek Vasut 		return len;
70531920264SMarek Vasut 	} else {
70631920264SMarek Vasut 		len = 0;
70731920264SMarek Vasut 
70831920264SMarek Vasut 		/* Restart the receiver if disabled */
70931920264SMarek Vasut 		if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
71031920264SMarek Vasut 			sh_eth_write(port_info, EDRRR_R, EDRRR);
71131920264SMarek Vasut 
71231920264SMarek Vasut 		return -EAGAIN;
71331920264SMarek Vasut 	}
71431920264SMarek Vasut }
71531920264SMarek Vasut 
sh_ether_free_pkt(struct udevice * dev,uchar * packet,int length)71631920264SMarek Vasut static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length)
71731920264SMarek Vasut {
71831920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
71931920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
72031920264SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
72131920264SMarek Vasut 
72231920264SMarek Vasut 	sh_eth_recv_finish(eth);
72331920264SMarek Vasut 
72431920264SMarek Vasut 	/* Restart the receiver if disabled */
72531920264SMarek Vasut 	if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
72631920264SMarek Vasut 		sh_eth_write(port_info, EDRRR_R, EDRRR);
72731920264SMarek Vasut 
72831920264SMarek Vasut 	return 0;
72931920264SMarek Vasut }
73031920264SMarek Vasut 
sh_ether_write_hwaddr(struct udevice * dev)73131920264SMarek Vasut static int sh_ether_write_hwaddr(struct udevice *dev)
73231920264SMarek Vasut {
73331920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
73431920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
73531920264SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
73631920264SMarek Vasut 	struct eth_pdata *pdata = dev_get_platdata(dev);
73731920264SMarek Vasut 
73831920264SMarek Vasut 	sh_eth_write_hwaddr(port_info, pdata->enetaddr);
73931920264SMarek Vasut 
74031920264SMarek Vasut 	return 0;
74131920264SMarek Vasut }
74231920264SMarek Vasut 
sh_eth_phy_config(struct udevice * dev)74331920264SMarek Vasut static int sh_eth_phy_config(struct udevice *dev)
74431920264SMarek Vasut {
74531920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
74631920264SMarek Vasut 	struct eth_pdata *pdata = dev_get_platdata(dev);
74731920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
7483c5a7b75SMarek Vasut 	int ret = 0;
7493c5a7b75SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
75031920264SMarek Vasut 	struct phy_device *phydev;
75131920264SMarek Vasut 	int mask = 0xffffffff;
75231920264SMarek Vasut 
75331920264SMarek Vasut 	phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface);
75431920264SMarek Vasut 	if (!phydev)
75531920264SMarek Vasut 		return -ENODEV;
75631920264SMarek Vasut 
75731920264SMarek Vasut 	phy_connect_dev(phydev, dev);
75831920264SMarek Vasut 
75931920264SMarek Vasut 	port_info->phydev = phydev;
76031920264SMarek Vasut 	phy_config(phydev);
76131920264SMarek Vasut 
76231920264SMarek Vasut 	return ret;
76331920264SMarek Vasut }
76431920264SMarek Vasut 
sh_ether_start(struct udevice * dev)76531920264SMarek Vasut static int sh_ether_start(struct udevice *dev)
76631920264SMarek Vasut {
76731920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
76831920264SMarek Vasut 	struct eth_pdata *pdata = dev_get_platdata(dev);
76931920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
77031920264SMarek Vasut 	int ret;
77131920264SMarek Vasut 
77231920264SMarek Vasut 	ret = clk_enable(&priv->clk);
77331920264SMarek Vasut 	if (ret)
77431920264SMarek Vasut 		return ret;
77531920264SMarek Vasut 
77631920264SMarek Vasut 	ret = sh_eth_init_common(eth, pdata->enetaddr);
77731920264SMarek Vasut 	if (ret)
77831920264SMarek Vasut 		goto err_clk;
77931920264SMarek Vasut 
78031920264SMarek Vasut 	ret = sh_eth_phy_config(dev);
78131920264SMarek Vasut 	if (ret) {
78231920264SMarek Vasut 		printf(SHETHER_NAME ": phy config timeout\n");
78331920264SMarek Vasut 		goto err_start;
78431920264SMarek Vasut 	}
78531920264SMarek Vasut 
78631920264SMarek Vasut 	ret = sh_eth_start_common(eth);
78731920264SMarek Vasut 	if (ret)
78831920264SMarek Vasut 		goto err_start;
78931920264SMarek Vasut 
79031920264SMarek Vasut 	return 0;
79131920264SMarek Vasut 
79231920264SMarek Vasut err_start:
79331920264SMarek Vasut 	sh_eth_tx_desc_free(eth);
79431920264SMarek Vasut 	sh_eth_rx_desc_free(eth);
79531920264SMarek Vasut err_clk:
79631920264SMarek Vasut 	clk_disable(&priv->clk);
79731920264SMarek Vasut 	return ret;
79831920264SMarek Vasut }
79931920264SMarek Vasut 
sh_ether_stop(struct udevice * dev)80031920264SMarek Vasut static void sh_ether_stop(struct udevice *dev)
80131920264SMarek Vasut {
80231920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(dev);
80331920264SMarek Vasut 
80431920264SMarek Vasut 	sh_eth_stop(&priv->shdev);
80531920264SMarek Vasut 	clk_disable(&priv->clk);
80631920264SMarek Vasut }
80731920264SMarek Vasut 
sh_ether_probe(struct udevice * udev)80831920264SMarek Vasut static int sh_ether_probe(struct udevice *udev)
80931920264SMarek Vasut {
81031920264SMarek Vasut 	struct eth_pdata *pdata = dev_get_platdata(udev);
81131920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(udev);
81231920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
813*159b3292SMarek Vasut 	struct ofnode_phandle_args phandle_args;
81431920264SMarek Vasut 	struct mii_dev *mdiodev;
81531920264SMarek Vasut 	int ret;
81631920264SMarek Vasut 
8175abcbd78SMarek Vasut 	priv->iobase = pdata->iobase;
81831920264SMarek Vasut 
81931920264SMarek Vasut 	ret = clk_get_by_index(udev, 0, &priv->clk);
82031920264SMarek Vasut 	if (ret < 0)
8215abcbd78SMarek Vasut 		return ret;
82231920264SMarek Vasut 
823*159b3292SMarek Vasut 	ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args);
824*159b3292SMarek Vasut 	if (!ret) {
825*159b3292SMarek Vasut 		gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
826*159b3292SMarek Vasut 					   &priv->reset_gpio, GPIOD_IS_OUT);
827*159b3292SMarek Vasut 	}
828*159b3292SMarek Vasut 
829*159b3292SMarek Vasut 	if (!dm_gpio_is_valid(&priv->reset_gpio)) {
83031920264SMarek Vasut 		gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio,
83131920264SMarek Vasut 				     GPIOD_IS_OUT);
832*159b3292SMarek Vasut 	}
83331920264SMarek Vasut 
83431920264SMarek Vasut 	mdiodev = mdio_alloc();
83531920264SMarek Vasut 	if (!mdiodev) {
83631920264SMarek Vasut 		ret = -ENOMEM;
8375abcbd78SMarek Vasut 		return ret;
83831920264SMarek Vasut 	}
83931920264SMarek Vasut 
84031920264SMarek Vasut 	mdiodev->read = bb_miiphy_read;
84131920264SMarek Vasut 	mdiodev->write = bb_miiphy_write;
84231920264SMarek Vasut 	bb_miiphy_buses[0].priv = eth;
84331920264SMarek Vasut 	snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name);
84431920264SMarek Vasut 
84531920264SMarek Vasut 	ret = mdio_register(mdiodev);
84631920264SMarek Vasut 	if (ret < 0)
84731920264SMarek Vasut 		goto err_mdio_register;
84831920264SMarek Vasut 
84931920264SMarek Vasut 	priv->bus = miiphy_get_dev_by_name(udev->name);
85031920264SMarek Vasut 
85131920264SMarek Vasut 	eth->port = CONFIG_SH_ETHER_USE_PORT;
85231920264SMarek Vasut 	eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
85331920264SMarek Vasut 	eth->port_info[eth->port].iobase =
85431920264SMarek Vasut 		(void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
85531920264SMarek Vasut 
85631920264SMarek Vasut 	return 0;
85731920264SMarek Vasut 
85831920264SMarek Vasut err_mdio_register:
85931920264SMarek Vasut 	mdio_free(mdiodev);
86031920264SMarek Vasut 	return ret;
86131920264SMarek Vasut }
86231920264SMarek Vasut 
sh_ether_remove(struct udevice * udev)86331920264SMarek Vasut static int sh_ether_remove(struct udevice *udev)
86431920264SMarek Vasut {
86531920264SMarek Vasut 	struct sh_ether_priv *priv = dev_get_priv(udev);
86631920264SMarek Vasut 	struct sh_eth_dev *eth = &priv->shdev;
86731920264SMarek Vasut 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
86831920264SMarek Vasut 
86931920264SMarek Vasut 	free(port_info->phydev);
87031920264SMarek Vasut 	mdio_unregister(priv->bus);
87131920264SMarek Vasut 	mdio_free(priv->bus);
87231920264SMarek Vasut 
87331920264SMarek Vasut 	if (dm_gpio_is_valid(&priv->reset_gpio))
87431920264SMarek Vasut 		dm_gpio_free(udev, &priv->reset_gpio);
87531920264SMarek Vasut 
87631920264SMarek Vasut 	return 0;
87731920264SMarek Vasut }
87831920264SMarek Vasut 
87931920264SMarek Vasut static const struct eth_ops sh_ether_ops = {
88031920264SMarek Vasut 	.start			= sh_ether_start,
88131920264SMarek Vasut 	.send			= sh_ether_send,
88231920264SMarek Vasut 	.recv			= sh_ether_recv,
88331920264SMarek Vasut 	.free_pkt		= sh_ether_free_pkt,
88431920264SMarek Vasut 	.stop			= sh_ether_stop,
88531920264SMarek Vasut 	.write_hwaddr		= sh_ether_write_hwaddr,
88631920264SMarek Vasut };
88731920264SMarek Vasut 
sh_ether_ofdata_to_platdata(struct udevice * dev)88831920264SMarek Vasut int sh_ether_ofdata_to_platdata(struct udevice *dev)
88931920264SMarek Vasut {
89031920264SMarek Vasut 	struct eth_pdata *pdata = dev_get_platdata(dev);
89131920264SMarek Vasut 	const char *phy_mode;
89231920264SMarek Vasut 	const fdt32_t *cell;
89331920264SMarek Vasut 	int ret = 0;
89431920264SMarek Vasut 
89531920264SMarek Vasut 	pdata->iobase = devfdt_get_addr(dev);
89631920264SMarek Vasut 	pdata->phy_interface = -1;
89731920264SMarek Vasut 	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
89831920264SMarek Vasut 			       NULL);
89931920264SMarek Vasut 	if (phy_mode)
90031920264SMarek Vasut 		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
90131920264SMarek Vasut 	if (pdata->phy_interface == -1) {
90231920264SMarek Vasut 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
90331920264SMarek Vasut 		return -EINVAL;
90431920264SMarek Vasut 	}
90531920264SMarek Vasut 
90631920264SMarek Vasut 	pdata->max_speed = 1000;
90731920264SMarek Vasut 	cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
90831920264SMarek Vasut 	if (cell)
90931920264SMarek Vasut 		pdata->max_speed = fdt32_to_cpu(*cell);
91031920264SMarek Vasut 
91131920264SMarek Vasut 	sprintf(bb_miiphy_buses[0].name, dev->name);
91231920264SMarek Vasut 
91331920264SMarek Vasut 	return ret;
91431920264SMarek Vasut }
91531920264SMarek Vasut 
91631920264SMarek Vasut static const struct udevice_id sh_ether_ids[] = {
917d526801bSMarek Vasut 	{ .compatible = "renesas,ether-r8a7790" },
91831920264SMarek Vasut 	{ .compatible = "renesas,ether-r8a7791" },
919d526801bSMarek Vasut 	{ .compatible = "renesas,ether-r8a7793" },
920d526801bSMarek Vasut 	{ .compatible = "renesas,ether-r8a7794" },
92131920264SMarek Vasut 	{ }
92231920264SMarek Vasut };
92331920264SMarek Vasut 
92431920264SMarek Vasut U_BOOT_DRIVER(eth_sh_ether) = {
92531920264SMarek Vasut 	.name		= "sh_ether",
92631920264SMarek Vasut 	.id		= UCLASS_ETH,
92731920264SMarek Vasut 	.of_match	= sh_ether_ids,
92831920264SMarek Vasut 	.ofdata_to_platdata = sh_ether_ofdata_to_platdata,
92931920264SMarek Vasut 	.probe		= sh_ether_probe,
93031920264SMarek Vasut 	.remove		= sh_ether_remove,
93131920264SMarek Vasut 	.ops		= &sh_ether_ops,
93231920264SMarek Vasut 	.priv_auto_alloc_size = sizeof(struct sh_ether_priv),
93331920264SMarek Vasut 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
93431920264SMarek Vasut 	.flags		= DM_FLAG_ALLOC_PRIV_DMA,
93531920264SMarek Vasut };
93631920264SMarek Vasut #endif
93731920264SMarek Vasut 
938bd1024b0SYoshihiro Shimoda /******* for bb_miiphy *******/
sh_eth_bb_init(struct bb_miiphy_bus * bus)939bd1024b0SYoshihiro Shimoda static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
940bd1024b0SYoshihiro Shimoda {
941bd1024b0SYoshihiro Shimoda 	return 0;
942bd1024b0SYoshihiro Shimoda }
943bd1024b0SYoshihiro Shimoda 
sh_eth_bb_mdio_active(struct bb_miiphy_bus * bus)944bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
945bd1024b0SYoshihiro Shimoda {
946bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
947fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
948bd1024b0SYoshihiro Shimoda 
949fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
950bd1024b0SYoshihiro Shimoda 
951bd1024b0SYoshihiro Shimoda 	return 0;
952bd1024b0SYoshihiro Shimoda }
953bd1024b0SYoshihiro Shimoda 
sh_eth_bb_mdio_tristate(struct bb_miiphy_bus * bus)954bd1024b0SYoshihiro Shimoda static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
955bd1024b0SYoshihiro Shimoda {
956bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
957fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
958bd1024b0SYoshihiro Shimoda 
959fbfb5115SNobuhiro Iwamatsu 	sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
960bd1024b0SYoshihiro Shimoda 
961bd1024b0SYoshihiro Shimoda 	return 0;
962bd1024b0SYoshihiro Shimoda }
963bd1024b0SYoshihiro Shimoda 
sh_eth_bb_set_mdio(struct bb_miiphy_bus * bus,int v)964bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
965bd1024b0SYoshihiro Shimoda {
966bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
967fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
968bd1024b0SYoshihiro Shimoda 
969bd1024b0SYoshihiro Shimoda 	if (v)
970fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
971fbfb5115SNobuhiro Iwamatsu 			     sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
972bd1024b0SYoshihiro Shimoda 	else
973fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
974fbfb5115SNobuhiro Iwamatsu 			     sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
975bd1024b0SYoshihiro Shimoda 
976bd1024b0SYoshihiro Shimoda 	return 0;
977bd1024b0SYoshihiro Shimoda }
978bd1024b0SYoshihiro Shimoda 
sh_eth_bb_get_mdio(struct bb_miiphy_bus * bus,int * v)979bd1024b0SYoshihiro Shimoda static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
980bd1024b0SYoshihiro Shimoda {
981bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
982fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
983bd1024b0SYoshihiro Shimoda 
984fbfb5115SNobuhiro Iwamatsu 	*v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
985bd1024b0SYoshihiro Shimoda 
986bd1024b0SYoshihiro Shimoda 	return 0;
987bd1024b0SYoshihiro Shimoda }
988bd1024b0SYoshihiro Shimoda 
sh_eth_bb_set_mdc(struct bb_miiphy_bus * bus,int v)989bd1024b0SYoshihiro Shimoda static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
990bd1024b0SYoshihiro Shimoda {
991bd1024b0SYoshihiro Shimoda 	struct sh_eth_dev *eth = bus->priv;
992fbfb5115SNobuhiro Iwamatsu 	struct sh_eth_info *port_info = &eth->port_info[eth->port];
993bd1024b0SYoshihiro Shimoda 
994bd1024b0SYoshihiro Shimoda 	if (v)
995fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
996fbfb5115SNobuhiro Iwamatsu 			     sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
997bd1024b0SYoshihiro Shimoda 	else
998fbfb5115SNobuhiro Iwamatsu 		sh_eth_write(port_info,
999fbfb5115SNobuhiro Iwamatsu 			     sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
1000bd1024b0SYoshihiro Shimoda 
1001bd1024b0SYoshihiro Shimoda 	return 0;
1002bd1024b0SYoshihiro Shimoda }
1003bd1024b0SYoshihiro Shimoda 
sh_eth_bb_delay(struct bb_miiphy_bus * bus)1004bd1024b0SYoshihiro Shimoda static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
1005bd1024b0SYoshihiro Shimoda {
1006bd1024b0SYoshihiro Shimoda 	udelay(10);
1007bd1024b0SYoshihiro Shimoda 
1008bd1024b0SYoshihiro Shimoda 	return 0;
1009bd1024b0SYoshihiro Shimoda }
1010bd1024b0SYoshihiro Shimoda 
1011bd1024b0SYoshihiro Shimoda struct bb_miiphy_bus bb_miiphy_buses[] = {
1012bd1024b0SYoshihiro Shimoda 	{
1013bd1024b0SYoshihiro Shimoda 		.name		= "sh_eth",
1014bd1024b0SYoshihiro Shimoda 		.init		= sh_eth_bb_init,
1015bd1024b0SYoshihiro Shimoda 		.mdio_active	= sh_eth_bb_mdio_active,
1016bd1024b0SYoshihiro Shimoda 		.mdio_tristate	= sh_eth_bb_mdio_tristate,
1017bd1024b0SYoshihiro Shimoda 		.set_mdio	= sh_eth_bb_set_mdio,
1018bd1024b0SYoshihiro Shimoda 		.get_mdio	= sh_eth_bb_get_mdio,
1019bd1024b0SYoshihiro Shimoda 		.set_mdc	= sh_eth_bb_set_mdc,
1020bd1024b0SYoshihiro Shimoda 		.delay		= sh_eth_bb_delay,
1021bd1024b0SYoshihiro Shimoda 	}
1022bd1024b0SYoshihiro Shimoda };
1023dc14867dSNobuhiro Iwamatsu 
1024bd1024b0SYoshihiro Shimoda int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);
1025