blob: 6edb51e12fa6da8f4849eaf3573e86487614c3c5 [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) \
Nobuhiro Iwamatsu425a3a52017-12-01 13:56:08 +090032 flush_dcache_range((u32)addr, \
33 (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090034#else
35#define flush_cache_wback(...)
36#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090037
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090038#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
39#define invalidate_cache(addr, len) \
40 { \
41 u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
42 u32 start, end; \
43 \
44 start = (u32)addr; \
45 end = start + len; \
46 start &= ~(line_size - 1); \
47 end = ((end + line_size - 1) & ~(line_size - 1)); \
48 \
49 invalidate_dcache_range(start, end); \
50 }
51#else
52#define invalidate_cache(...)
53#endif
54
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090055#define TIMEOUT_CNT 1000
56
Joe Hershbergere4e04882012-05-22 18:36:19 +000057int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090058{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090059 struct sh_eth_dev *eth = dev->priv;
60 int port = eth->port, ret = 0, timeout;
61 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090062
63 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090064 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
65 ret = -EINVAL;
66 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090067 }
68
69 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +000070 if ((int)packet & 3) {
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +090071 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +090072 , __func__);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090073 ret = -EFAULT;
74 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090075 }
76
77 /* Update tx descriptor */
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090078 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090079 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
80 port_info->tx_desc_cur->td1 = len << 16;
81 /* Must preserve the end of descriptor list indication */
82 if (port_info->tx_desc_cur->td0 & TD_TDLE)
83 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
84 else
85 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
86
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +090087 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
88
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090089 /* Restart the transmitter if disabled */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +090090 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
91 sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090092
93 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090094 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090095 do {
96 invalidate_cache(port_info->tx_desc_cur,
97 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090098 udelay(100);
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +090099 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900100
101 if (timeout < 0) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900102 printf(SHETHER_NAME ": transmit timeout\n");
103 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900104 goto err;
105 }
106
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900107 port_info->tx_desc_cur++;
108 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
109 port_info->tx_desc_cur = port_info->tx_desc_base;
110
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900111err:
112 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900113}
114
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900115int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900116{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900117 struct sh_eth_dev *eth = dev->priv;
118 int port = eth->port, len = 0;
119 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershbergere4e04882012-05-22 18:36:19 +0000120 uchar *packet;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900121
122 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900123 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900124 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
125 /* Check for errors */
126 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
127 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershbergere4e04882012-05-22 18:36:19 +0000128 packet = (uchar *)
129 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900130 invalidate_cache(packet, len);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500131 net_process_received_packet(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900132 }
133
134 /* Make current descriptor available again */
135 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
136 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
137 else
138 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +0900139
140 flush_cache_wback(port_info->rx_desc_cur,
141 sizeof(struct rx_desc_s));
142
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900143 /* Point to the next descriptor */
144 port_info->rx_desc_cur++;
145 if (port_info->rx_desc_cur >=
146 port_info->rx_desc_base + NUM_RX_DESC)
147 port_info->rx_desc_cur = port_info->rx_desc_base;
148 }
149
150 /* Restart the receiver if disabled */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900151 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
152 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900153
154 return len;
155}
156
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900157static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900158{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900159 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900160#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900161 int ret = 0, i;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900162
163 /* Start e-dmac transmitter and receiver */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900164 sh_eth_write(port_info, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900165
166 /* Perform a software reset and wait for it to complete */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900167 sh_eth_write(port_info, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900168 for (i = 0; i < TIMEOUT_CNT; i++) {
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900169 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900170 break;
171 udelay(1000);
172 }
173
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900174 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900175 printf(SHETHER_NAME ": Software reset timeout\n");
176 ret = -EIO;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900177 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900178
179 return ret;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900180#else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900181 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900182 udelay(3000);
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900183 sh_eth_write(port_info,
184 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900185
186 return 0;
187#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900188}
189
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900190static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900191{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900192 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900193 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900194 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900195 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900196
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900197 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900198 * Allocate rx descriptors. They must be aligned to size of struct
199 * tx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900200 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900201 port_info->tx_desc_alloc =
202 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
203 if (!port_info->tx_desc_alloc) {
204 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900205 ret = -ENOMEM;
206 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900207 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900208
Nobuhiro Iwamatsu425a3a52017-12-01 13:56:08 +0900209 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900210
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900211 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900212 port_info->tx_desc_base =
213 (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900214 port_info->tx_desc_cur = port_info->tx_desc_base;
215
216 /* Initialize all descriptors */
217 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
218 cur_tx_desc++, i++) {
219 cur_tx_desc->td0 = 0x00;
220 cur_tx_desc->td1 = 0x00;
221 cur_tx_desc->td2 = 0x00;
222 }
223
224 /* Mark the end of the descriptors */
225 cur_tx_desc--;
226 cur_tx_desc->td0 |= TD_TDLE;
227
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900228 /*
229 * Point the controller to the tx descriptor list. Must use physical
230 * addresses
231 */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900232 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900233#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900234 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
235 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
236 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900237#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900238
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900239err:
240 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900241}
242
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900243static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900244{
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900245 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900246 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900247 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900248 struct rx_desc_s *cur_rx_desc;
249 u8 *rx_buf;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900250
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900251 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900252 * Allocate rx descriptors. They must be aligned to size of struct
253 * rx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900254 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900255 port_info->rx_desc_alloc =
256 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
257 if (!port_info->rx_desc_alloc) {
258 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900259 ret = -ENOMEM;
260 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900261 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900262
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900263 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
264
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900265 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900266 port_info->rx_desc_base =
267 (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900268
269 port_info->rx_desc_cur = port_info->rx_desc_base;
270
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900271 /*
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900272 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
273 * aligned and in P2 area.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900274 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900275 port_info->rx_buf_alloc =
276 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
277 if (!port_info->rx_buf_alloc) {
278 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900279 ret = -ENOMEM;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900280 goto err_buf_alloc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900281 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900282
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900283 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900284
285 /* Initialize all descriptors */
286 for (cur_rx_desc = port_info->rx_desc_base,
287 rx_buf = port_info->rx_buf_base, i = 0;
288 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
289 cur_rx_desc->rd0 = RD_RACT;
290 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900291 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900292 }
293
294 /* Mark the end of the descriptors */
295 cur_rx_desc--;
296 cur_rx_desc->rd0 |= RD_RDLE;
297
298 /* Point the controller to the rx descriptor list */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900299 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900300#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900301 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
302 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
303 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900304#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900305
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900306 return ret;
307
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900308err_buf_alloc:
309 free(port_info->rx_desc_alloc);
310 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900311
312err:
313 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900314}
315
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900316static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900317{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900318 int port = eth->port;
319 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900320
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900321 if (port_info->tx_desc_alloc) {
322 free(port_info->tx_desc_alloc);
323 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900324 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900325}
326
327static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
328{
329 int port = eth->port;
330 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900331
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900332 if (port_info->rx_desc_alloc) {
333 free(port_info->rx_desc_alloc);
334 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900335 }
336
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900337 if (port_info->rx_buf_alloc) {
338 free(port_info->rx_buf_alloc);
339 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900340 }
341}
342
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900343static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900344{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900345 int ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900346
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900347 ret = sh_eth_tx_desc_init(eth);
348 if (ret)
349 goto err_tx_init;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900350
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900351 ret = sh_eth_rx_desc_init(eth);
352 if (ret)
353 goto err_rx_init;
354
355 return ret;
356err_rx_init:
357 sh_eth_tx_desc_free(eth);
358
359err_tx_init:
360 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900361}
362
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900363static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900364{
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900365 int port = eth->port, ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900366 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900367 struct eth_device *dev = port_info->dev;
368 struct phy_device *phydev;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900369
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +0000370 phydev = phy_connect(
371 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000372 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900373 port_info->phydev = phydev;
374 phy_config(phydev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900375
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900376 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900377}
378
Nobuhiro Iwamatsu65a81a42017-12-01 08:08:47 +0900379static int sh_eth_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900380{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900381 int port = eth->port, ret = 0;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900382 u32 val;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900383 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingera86bf132009-02-11 19:14:09 -0500384 struct eth_device *dev = port_info->dev;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900385 struct phy_device *phy;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900386
387 /* Configure e-dmac registers */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900388 sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
Nobuhiro Iwamatsu7a2142c2013-08-22 13:22:02 +0900389 (EMDR_DESC | EDMR_EL), EDMR);
390
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900391 sh_eth_write(port_info, 0, EESIPR);
392 sh_eth_write(port_info, 0, TRSCER);
393 sh_eth_write(port_info, 0, TFTR);
394 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
395 sh_eth_write(port_info, RMCR_RST, RMCR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900396#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900397 sh_eth_write(port_info, 0, RPADIR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900398#endif
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900399 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900400
401 /* Configure e-mac registers */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900402 sh_eth_write(port_info, 0, ECSIPR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900403
404 /* Set Mac address */
Mike Frysingera86bf132009-02-11 19:14:09 -0500405 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
406 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900407 sh_eth_write(port_info, val, MAHR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900408
Mike Frysingera86bf132009-02-11 19:14:09 -0500409 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900410 sh_eth_write(port_info, val, MALR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900411
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900412 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000413#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900414 sh_eth_write(port_info, 0, PIPR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900415#endif
416#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900417 sh_eth_write(port_info, APR_AP, APR);
418 sh_eth_write(port_info, MPR_MP, MPR);
419 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900420#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900421
Nobuhiro Iwamatsu4ad2c2a2012-08-02 22:08:40 +0000422#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900423 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsua2dd2a12014-06-24 17:01:08 +0900424#elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
Nobuhiro Iwamatsu290fdfd2014-11-04 09:13:40 +0900425 defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900426 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000427#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900428 /* Configure phy */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900429 ret = sh_eth_phy_config(eth);
430 if (ret) {
Nobuhiro Iwamatsufc4b0a22009-06-25 16:33:04 +0900431 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900432 goto err_phy_cfg;
433 }
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900434 phy = port_info->phydev;
Timur Tabi42387462012-07-09 08:52:43 +0000435 ret = phy_startup(phy);
436 if (ret) {
437 printf(SHETHER_NAME ": phy startup failure\n");
438 return ret;
439 }
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900440
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900441 val = 0;
442
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900443 /* Set the transfer speed */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900444 if (phy->speed == 100) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900445 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000446#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900447 sh_eth_write(port_info, GECMR_100B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000448#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900449 sh_eth_write(port_info, 1, RTRATE);
Nobuhiro Iwamatsu5e6cd1b2013-09-24 15:38:33 +0900450#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
Nobuhiro Iwamatsu290fdfd2014-11-04 09:13:40 +0900451 defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
452 defined(CONFIG_R8A7794)
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900453 val = ECMR_RTM;
454#endif
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900455 } else if (phy->speed == 10) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900456 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000457#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900458 sh_eth_write(port_info, GECMR_10B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000459#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900460 sh_eth_write(port_info, 0, RTRATE);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900461#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900462 }
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000463#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000464 else if (phy->speed == 1000) {
465 printf(SHETHER_NAME ": 1000Base/");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900466 sh_eth_write(port_info, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000467 }
468#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900469
470 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900471 if (phy->duplex) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900472 printf("Full\n");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900473 sh_eth_write(port_info,
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900474 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000475 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900476 } else {
477 printf("Half\n");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900478 sh_eth_write(port_info,
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900479 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
480 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900481 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900482
483 return ret;
484
485err_phy_cfg:
486 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900487}
488
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900489static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900490{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900491 struct sh_eth_info *port_info = &eth->port_info[eth->port];
492
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900493 /*
494 * Enable the e-dmac receiver only. The transmitter will be enabled when
495 * we have something to transmit
496 */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900497 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900498}
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900499
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900500static void sh_eth_stop(struct sh_eth_dev *eth)
501{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900502 struct sh_eth_info *port_info = &eth->port_info[eth->port];
503
504 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900505}
506
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900507int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900508{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900509 int ret = 0;
510 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900511
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900512 ret = sh_eth_reset(eth);
513 if (ret)
514 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900515
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900516 ret = sh_eth_desc_init(eth);
517 if (ret)
518 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900519
Nobuhiro Iwamatsu65a81a42017-12-01 08:08:47 +0900520 ret = sh_eth_config(eth);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900521 if (ret)
522 goto err_config;
523
524 sh_eth_start(eth);
525
526 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900527
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900528err_config:
529 sh_eth_tx_desc_free(eth);
530 sh_eth_rx_desc_free(eth);
531
532err:
533 return ret;
534}
535
536void sh_eth_halt(struct eth_device *dev)
537{
538 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900539
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900540 sh_eth_stop(eth);
541}
542
543int sh_eth_initialize(bd_t *bd)
544{
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900545 int ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900546 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900547 struct eth_device *dev = NULL;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900548 struct mii_dev *mdiodev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900549
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900550 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900551 if (!eth) {
552 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
553 ret = -ENOMEM;
554 goto err;
555 }
556
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900557 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900558 if (!dev) {
559 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
560 ret = -ENOMEM;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900561 goto err;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900562 }
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900563 memset(dev, 0, sizeof(struct eth_device));
564 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900565
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900566 eth->port = CONFIG_SH_ETHER_USE_PORT;
567 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900568 eth->port_info[eth->port].iobase =
569 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900570
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900571 dev->priv = (void *)eth;
572 dev->iobase = 0;
573 dev->init = sh_eth_init;
574 dev->halt = sh_eth_halt;
575 dev->send = sh_eth_send;
576 dev->recv = sh_eth_recv;
577 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900578
Ben Whitten34fd6c92015-12-30 13:05:58 +0000579 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900580
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900581 /* Register Device to EtherNet subsystem */
582 eth_register(dev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900583
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900584 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900585 mdiodev = mdio_alloc();
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500586 if (!mdiodev)
587 return -ENOMEM;
588 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
589 mdiodev->read = bb_miiphy_read;
590 mdiodev->write = bb_miiphy_write;
591
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900592 ret = mdio_register(mdiodev);
593 if (ret < 0)
594 return ret;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900595
Simon Glass399a9ce2017-08-03 12:22:14 -0600596 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingera86bf132009-02-11 19:14:09 -0500597 puts("Please set MAC address\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900598
599 return ret;
600
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900601err:
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900602 if (dev)
603 free(dev);
604
605 if (eth)
606 free(eth);
607
608 printf(SHETHER_NAME ": Failed\n");
609 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900610}
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900611
612/******* for bb_miiphy *******/
613static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
614{
615 return 0;
616}
617
618static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
619{
620 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900621 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900622
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900623 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900624
625 return 0;
626}
627
628static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
629{
630 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900631 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900632
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900633 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900634
635 return 0;
636}
637
638static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
639{
640 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900641 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900642
643 if (v)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900644 sh_eth_write(port_info,
645 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900646 else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900647 sh_eth_write(port_info,
648 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900649
650 return 0;
651}
652
653static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
654{
655 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900656 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900657
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900658 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900659
660 return 0;
661}
662
663static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
664{
665 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900666 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900667
668 if (v)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900669 sh_eth_write(port_info,
670 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900671 else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900672 sh_eth_write(port_info,
673 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900674
675 return 0;
676}
677
678static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
679{
680 udelay(10);
681
682 return 0;
683}
684
685struct bb_miiphy_bus bb_miiphy_buses[] = {
686 {
687 .name = "sh_eth",
688 .init = sh_eth_bb_init,
689 .mdio_active = sh_eth_bb_mdio_active,
690 .mdio_tristate = sh_eth_bb_mdio_tristate,
691 .set_mdio = sh_eth_bb_set_mdio,
692 .get_mdio = sh_eth_bb_get_mdio,
693 .set_mdc = sh_eth_bb_set_mdc,
694 .delay = sh_eth_bb_delay,
695 }
696};
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900697
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900698int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);