blob: eafc4d3f27a293659dbc5b9e6bc9ea60c57e9a41 [file] [log] [blame]
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09001/*
Robert P. J. Day8c60f922016-05-04 04:47:31 -04002 * sh_eth.c - Driver for Renesas ethernet controller.
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09003 *
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +09004 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +09005 * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09006 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +09007 * Copyright (C) 2013, 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09008 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090010 */
11
12#include <config.h>
13#include <common.h>
14#include <malloc.h>
15#include <net.h>
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090016#include <netdev.h>
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +090017#include <miiphy.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090018#include <linux/errno.h>
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090019#include <asm/io.h>
20
21#include "sh_eth.h"
22
23#ifndef CONFIG_SH_ETHER_USE_PORT
24# error "Please define CONFIG_SH_ETHER_USE_PORT"
25#endif
26#ifndef CONFIG_SH_ETHER_PHY_ADDR
27# error "Please define CONFIG_SH_ETHER_PHY_ADDR"
28#endif
Nobuhiro Iwamatsu6bff09d2013-08-22 13:22:01 +090029
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090030#if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
31#define flush_cache_wback(addr, len) \
32 flush_dcache_range((u32)addr, (u32)(addr + len - 1))
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090033#else
34#define flush_cache_wback(...)
35#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090036
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090037#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
38#define invalidate_cache(addr, len) \
39 { \
40 u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
41 u32 start, end; \
42 \
43 start = (u32)addr; \
44 end = start + len; \
45 start &= ~(line_size - 1); \
46 end = ((end + line_size - 1) & ~(line_size - 1)); \
47 \
48 invalidate_dcache_range(start, end); \
49 }
50#else
51#define invalidate_cache(...)
52#endif
53
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090054#define TIMEOUT_CNT 1000
55
Joe Hershbergere4e04882012-05-22 18:36:19 +000056int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090057{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090058 struct sh_eth_dev *eth = dev->priv;
59 int port = eth->port, ret = 0, timeout;
60 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090061
62 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090063 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
64 ret = -EINVAL;
65 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090066 }
67
68 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +000069 if ((int)packet & 3) {
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +090070 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +090071 , __func__);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090072 ret = -EFAULT;
73 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090074 }
75
76 /* Update tx descriptor */
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090077 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090078 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
79 port_info->tx_desc_cur->td1 = len << 16;
80 /* Must preserve the end of descriptor list indication */
81 if (port_info->tx_desc_cur->td0 & TD_TDLE)
82 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
83 else
84 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
85
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +090086 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
87
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090088 /* Restart the transmitter if disabled */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +000089 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
90 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090091
92 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090093 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090094 do {
95 invalidate_cache(port_info->tx_desc_cur,
96 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090097 udelay(100);
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090098 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090099
100 if (timeout < 0) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900101 printf(SHETHER_NAME ": transmit timeout\n");
102 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900103 goto err;
104 }
105
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900106 port_info->tx_desc_cur++;
107 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
108 port_info->tx_desc_cur = port_info->tx_desc_base;
109
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900110err:
111 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900112}
113
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900114int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900115{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900116 struct sh_eth_dev *eth = dev->priv;
117 int port = eth->port, len = 0;
118 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershbergere4e04882012-05-22 18:36:19 +0000119 uchar *packet;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900120
121 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900122 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900123 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
124 /* Check for errors */
125 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
126 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershbergere4e04882012-05-22 18:36:19 +0000127 packet = (uchar *)
128 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900129 invalidate_cache(packet, len);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500130 net_process_received_packet(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900131 }
132
133 /* Make current descriptor available again */
134 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
135 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
136 else
137 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +0900138
139 flush_cache_wback(port_info->rx_desc_cur,
140 sizeof(struct rx_desc_s));
141
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900142 /* Point to the next descriptor */
143 port_info->rx_desc_cur++;
144 if (port_info->rx_desc_cur >=
145 port_info->rx_desc_base + NUM_RX_DESC)
146 port_info->rx_desc_cur = port_info->rx_desc_base;
147 }
148
149 /* Restart the receiver if disabled */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000150 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
151 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900152
153 return len;
154}
155
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900156static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900157{
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900158#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900159 int ret = 0, i;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900160
161 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000162 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900163
164 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000165 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900166 for (i = 0; i < TIMEOUT_CNT; i++) {
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000167 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900168 break;
169 udelay(1000);
170 }
171
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900172 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900173 printf(SHETHER_NAME ": Software reset timeout\n");
174 ret = -EIO;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900175 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900176
177 return ret;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900178#else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000179 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900180 udelay(3000);
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000181 sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900182
183 return 0;
184#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900185}
186
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900187static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900188{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900189 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900190 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900191 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900192 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900193
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900194 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900195 * Allocate rx descriptors. They must be aligned to size of struct
196 * tx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900197 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900198 port_info->tx_desc_alloc =
199 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
200 if (!port_info->tx_desc_alloc) {
201 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900202 ret = -ENOMEM;
203 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900204 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900205
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900206 flush_cache_wback((u32)port_info->tx_desc_alloc, alloc_desc_size);
207
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900208 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900209 port_info->tx_desc_base =
210 (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900211 port_info->tx_desc_cur = port_info->tx_desc_base;
212
213 /* Initialize all descriptors */
214 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
215 cur_tx_desc++, i++) {
216 cur_tx_desc->td0 = 0x00;
217 cur_tx_desc->td1 = 0x00;
218 cur_tx_desc->td2 = 0x00;
219 }
220
221 /* Mark the end of the descriptors */
222 cur_tx_desc--;
223 cur_tx_desc->td0 |= TD_TDLE;
224
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900225 /*
226 * Point the controller to the tx descriptor list. Must use physical
227 * addresses
228 */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000229 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900230#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000231 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
232 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
233 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900234#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900235
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900236err:
237 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900238}
239
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900240static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900241{
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900242 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900243 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900244 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900245 struct rx_desc_s *cur_rx_desc;
246 u8 *rx_buf;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900247
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900248 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900249 * Allocate rx descriptors. They must be aligned to size of struct
250 * rx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900251 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900252 port_info->rx_desc_alloc =
253 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
254 if (!port_info->rx_desc_alloc) {
255 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900256 ret = -ENOMEM;
257 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900258 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900259
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900260 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
261
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900262 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900263 port_info->rx_desc_base =
264 (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900265
266 port_info->rx_desc_cur = port_info->rx_desc_base;
267
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900268 /*
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900269 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
270 * aligned and in P2 area.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900271 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900272 port_info->rx_buf_alloc =
273 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
274 if (!port_info->rx_buf_alloc) {
275 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900276 ret = -ENOMEM;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900277 goto err_buf_alloc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900278 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900279
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900280 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900281
282 /* Initialize all descriptors */
283 for (cur_rx_desc = port_info->rx_desc_base,
284 rx_buf = port_info->rx_buf_base, i = 0;
285 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
286 cur_rx_desc->rd0 = RD_RACT;
287 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900288 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900289 }
290
291 /* Mark the end of the descriptors */
292 cur_rx_desc--;
293 cur_rx_desc->rd0 |= RD_RDLE;
294
295 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000296 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900297#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000298 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
299 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
300 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900301#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900302
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900303 return ret;
304
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900305err_buf_alloc:
306 free(port_info->rx_desc_alloc);
307 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900308
309err:
310 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900311}
312
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900313static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900314{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900315 int port = eth->port;
316 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900317
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900318 if (port_info->tx_desc_alloc) {
319 free(port_info->tx_desc_alloc);
320 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900321 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900322}
323
324static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
325{
326 int port = eth->port;
327 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900328
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900329 if (port_info->rx_desc_alloc) {
330 free(port_info->rx_desc_alloc);
331 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900332 }
333
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900334 if (port_info->rx_buf_alloc) {
335 free(port_info->rx_buf_alloc);
336 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900337 }
338}
339
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900340static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900341{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900342 int ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900343
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900344 ret = sh_eth_tx_desc_init(eth);
345 if (ret)
346 goto err_tx_init;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900347
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900348 ret = sh_eth_rx_desc_init(eth);
349 if (ret)
350 goto err_rx_init;
351
352 return ret;
353err_rx_init:
354 sh_eth_tx_desc_free(eth);
355
356err_tx_init:
357 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900358}
359
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900360static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900361{
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900362 int port = eth->port, ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900363 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900364 struct eth_device *dev = port_info->dev;
365 struct phy_device *phydev;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900366
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +0000367 phydev = phy_connect(
368 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000369 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900370 port_info->phydev = phydev;
371 phy_config(phydev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900372
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900373 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900374}
375
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900376static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900377{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900378 int port = eth->port, ret = 0;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900379 u32 val;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900380 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingera86bf132009-02-11 19:14:09 -0500381 struct eth_device *dev = port_info->dev;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900382 struct phy_device *phy;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900383
384 /* Configure e-dmac registers */
Nobuhiro Iwamatsu7a2142c2013-08-22 13:22:02 +0900385 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
386 (EMDR_DESC | EDMR_EL), EDMR);
387
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000388 sh_eth_write(eth, 0, EESIPR);
389 sh_eth_write(eth, 0, TRSCER);
390 sh_eth_write(eth, 0, TFTR);
391 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
392 sh_eth_write(eth, RMCR_RST, RMCR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900393#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000394 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900395#endif
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000396 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900397
398 /* Configure e-mac registers */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000399 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900400
401 /* Set Mac address */
Mike Frysingera86bf132009-02-11 19:14:09 -0500402 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
403 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000404 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900405
Mike Frysingera86bf132009-02-11 19:14:09 -0500406 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000407 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900408
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000409 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000410#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000411 sh_eth_write(eth, 0, PIPR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900412#endif
413#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000414 sh_eth_write(eth, APR_AP, APR);
415 sh_eth_write(eth, MPR_MP, MPR);
416 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900417#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900418
Nobuhiro Iwamatsu4ad2c2a2012-08-02 22:08:40 +0000419#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000420 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsua2dd2a12014-06-24 17:01:08 +0900421#elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
Nobuhiro Iwamatsu290fdfd2014-11-04 09:13:40 +0900422 defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
Nobuhiro Iwamatsu72befd32013-08-22 13:22:04 +0900423 sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000424#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900425 /* Configure phy */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900426 ret = sh_eth_phy_config(eth);
427 if (ret) {
Nobuhiro Iwamatsufc4b0a22009-06-25 16:33:04 +0900428 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900429 goto err_phy_cfg;
430 }
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900431 phy = port_info->phydev;
Timur Tabi42387462012-07-09 08:52:43 +0000432 ret = phy_startup(phy);
433 if (ret) {
434 printf(SHETHER_NAME ": phy startup failure\n");
435 return ret;
436 }
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900437
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900438 val = 0;
439
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900440 /* Set the transfer speed */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900441 if (phy->speed == 100) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900442 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000443#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000444 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000445#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000446 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu5e6cd1b2013-09-24 15:38:33 +0900447#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
Nobuhiro Iwamatsu290fdfd2014-11-04 09:13:40 +0900448 defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
449 defined(CONFIG_R8A7794)
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900450 val = ECMR_RTM;
451#endif
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900452 } else if (phy->speed == 10) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900453 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000454#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000455 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000456#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000457 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900458#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900459 }
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000460#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000461 else if (phy->speed == 1000) {
462 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000463 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000464 }
465#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900466
467 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900468 if (phy->duplex) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900469 printf("Full\n");
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900470 sh_eth_write(eth,
471 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000472 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900473 } else {
474 printf("Half\n");
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900475 sh_eth_write(eth,
476 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
477 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900478 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900479
480 return ret;
481
482err_phy_cfg:
483 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900484}
485
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900486static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900487{
488 /*
489 * Enable the e-dmac receiver only. The transmitter will be enabled when
490 * we have something to transmit
491 */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000492 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900493}
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900494
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900495static void sh_eth_stop(struct sh_eth_dev *eth)
496{
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000497 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900498}
499
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900500int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900501{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900502 int ret = 0;
503 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900504
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900505 ret = sh_eth_reset(eth);
506 if (ret)
507 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900508
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900509 ret = sh_eth_desc_init(eth);
510 if (ret)
511 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900512
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900513 ret = sh_eth_config(eth, bd);
514 if (ret)
515 goto err_config;
516
517 sh_eth_start(eth);
518
519 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900520
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900521err_config:
522 sh_eth_tx_desc_free(eth);
523 sh_eth_rx_desc_free(eth);
524
525err:
526 return ret;
527}
528
529void sh_eth_halt(struct eth_device *dev)
530{
531 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900532
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900533 sh_eth_stop(eth);
534}
535
536int sh_eth_initialize(bd_t *bd)
537{
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900538 int ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900539 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900540 struct eth_device *dev = NULL;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900541 struct mii_dev *mdiodev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900542
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900543 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900544 if (!eth) {
545 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
546 ret = -ENOMEM;
547 goto err;
548 }
549
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900550 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900551 if (!dev) {
552 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
553 ret = -ENOMEM;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900554 goto err;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900555 }
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900556 memset(dev, 0, sizeof(struct eth_device));
557 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900558
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900559 eth->port = CONFIG_SH_ETHER_USE_PORT;
560 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900561
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900562 dev->priv = (void *)eth;
563 dev->iobase = 0;
564 dev->init = sh_eth_init;
565 dev->halt = sh_eth_halt;
566 dev->send = sh_eth_send;
567 dev->recv = sh_eth_recv;
568 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900569
Ben Whitten34fd6c92015-12-30 13:05:58 +0000570 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900571
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900572 /* Register Device to EtherNet subsystem */
573 eth_register(dev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900574
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900575 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900576 mdiodev = mdio_alloc();
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500577 if (!mdiodev)
578 return -ENOMEM;
579 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
580 mdiodev->read = bb_miiphy_read;
581 mdiodev->write = bb_miiphy_write;
582
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900583 ret = mdio_register(mdiodev);
584 if (ret < 0)
585 return ret;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900586
Simon Glass399a9ce2017-08-03 12:22:14 -0600587 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingera86bf132009-02-11 19:14:09 -0500588 puts("Please set MAC address\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900589
590 return ret;
591
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900592err:
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900593 if (dev)
594 free(dev);
595
596 if (eth)
597 free(eth);
598
599 printf(SHETHER_NAME ": Failed\n");
600 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900601}
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900602
603/******* for bb_miiphy *******/
604static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
605{
606 return 0;
607}
608
609static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
610{
611 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900612
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000613 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900614
615 return 0;
616}
617
618static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
619{
620 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900621
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000622 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900623
624 return 0;
625}
626
627static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
628{
629 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900630
631 if (v)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000632 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900633 else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000634 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900635
636 return 0;
637}
638
639static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
640{
641 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900642
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000643 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900644
645 return 0;
646}
647
648static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
649{
650 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900651
652 if (v)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000653 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900654 else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000655 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900656
657 return 0;
658}
659
660static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
661{
662 udelay(10);
663
664 return 0;
665}
666
667struct bb_miiphy_bus bb_miiphy_buses[] = {
668 {
669 .name = "sh_eth",
670 .init = sh_eth_bb_init,
671 .mdio_active = sh_eth_bb_mdio_active,
672 .mdio_tristate = sh_eth_bb_mdio_tristate,
673 .set_mdio = sh_eth_bb_set_mdio,
674 .get_mdio = sh_eth_bb_get_mdio,
675 .set_mdc = sh_eth_bb_set_mdc,
676 .delay = sh_eth_bb_delay,
677 }
678};
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900679
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900680int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);