blob: 7b11a5a0d3064392a63fa73ea406b18c5cdec5fb [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
Marek Vasut044eb2d2018-01-21 14:27:51 +010057static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090058{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090059 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 */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +090089 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
90 sh_eth_write(port_info, 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
Marek Vasut044eb2d2018-01-21 14:27:51 +0100114static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900115{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900116 struct sh_eth_dev *eth = dev->priv;
Marek Vasut044eb2d2018-01-21 14:27:51 +0100117
118 return sh_eth_send_common(eth, packet, len);
119}
120
121static int sh_eth_recv_common(struct sh_eth_dev *eth)
122{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900123 int port = eth->port, len = 0;
124 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershbergere4e04882012-05-22 18:36:19 +0000125 uchar *packet;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900126
127 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900128 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900129 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
130 /* Check for errors */
131 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
132 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershbergere4e04882012-05-22 18:36:19 +0000133 packet = (uchar *)
134 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsuee74c702013-08-22 13:22:03 +0900135 invalidate_cache(packet, len);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500136 net_process_received_packet(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900137 }
138
139 /* Make current descriptor available again */
140 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
141 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
142 else
143 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu5ba66ad2014-11-04 09:15:48 +0900144
145 flush_cache_wback(port_info->rx_desc_cur,
146 sizeof(struct rx_desc_s));
147
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900148 /* Point to the next descriptor */
149 port_info->rx_desc_cur++;
150 if (port_info->rx_desc_cur >=
151 port_info->rx_desc_base + NUM_RX_DESC)
152 port_info->rx_desc_cur = port_info->rx_desc_base;
153 }
154
155 /* Restart the receiver if disabled */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900156 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
157 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900158
159 return len;
160}
161
Marek Vasut044eb2d2018-01-21 14:27:51 +0100162static int sh_eth_recv_legacy(struct eth_device *dev)
163{
164 struct sh_eth_dev *eth = dev->priv;
165
166 return sh_eth_recv_common(eth);
167}
168
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900169static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900170{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900171 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900172#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900173 int ret = 0, i;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900174
175 /* Start e-dmac transmitter and receiver */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900176 sh_eth_write(port_info, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900177
178 /* Perform a software reset and wait for it to complete */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900179 sh_eth_write(port_info, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900180 for (i = 0; i < TIMEOUT_CNT; i++) {
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900181 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900182 break;
183 udelay(1000);
184 }
185
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900186 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900187 printf(SHETHER_NAME ": Software reset timeout\n");
188 ret = -EIO;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900189 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900190
191 return ret;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900192#else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900193 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900194 udelay(3000);
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900195 sh_eth_write(port_info,
196 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900197
198 return 0;
199#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900200}
201
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900202static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900203{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900204 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900205 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900206 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900207 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900208
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900209 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900210 * Allocate rx descriptors. They must be aligned to size of struct
211 * tx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900212 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900213 port_info->tx_desc_alloc =
214 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
215 if (!port_info->tx_desc_alloc) {
216 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900217 ret = -ENOMEM;
218 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900219 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900220
Nobuhiro Iwamatsu425a3a52017-12-01 13:56:08 +0900221 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900222
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900223 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900224 port_info->tx_desc_base =
225 (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900226 port_info->tx_desc_cur = port_info->tx_desc_base;
227
228 /* Initialize all descriptors */
229 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
230 cur_tx_desc++, i++) {
231 cur_tx_desc->td0 = 0x00;
232 cur_tx_desc->td1 = 0x00;
233 cur_tx_desc->td2 = 0x00;
234 }
235
236 /* Mark the end of the descriptors */
237 cur_tx_desc--;
238 cur_tx_desc->td0 |= TD_TDLE;
239
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900240 /*
241 * Point the controller to the tx descriptor list. Must use physical
242 * addresses
243 */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900244 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900245#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900246 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
247 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
248 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900249#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900250
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900251err:
252 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900253}
254
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900255static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900256{
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900257 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900258 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900259 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900260 struct rx_desc_s *cur_rx_desc;
261 u8 *rx_buf;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900262
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900263 /*
Nobuhiro Iwamatsuc24b3eb2014-11-04 09:15:46 +0900264 * Allocate rx descriptors. They must be aligned to size of struct
265 * rx_desc_s.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900266 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900267 port_info->rx_desc_alloc =
268 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
269 if (!port_info->rx_desc_alloc) {
270 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900271 ret = -ENOMEM;
272 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900273 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900274
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900275 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
276
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900277 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900278 port_info->rx_desc_base =
279 (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900280
281 port_info->rx_desc_cur = port_info->rx_desc_base;
282
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900283 /*
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900284 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
285 * aligned and in P2 area.
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900286 */
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900287 port_info->rx_buf_alloc =
288 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
289 if (!port_info->rx_buf_alloc) {
290 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900291 ret = -ENOMEM;
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900292 goto err_buf_alloc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900293 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900294
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900295 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900296
297 /* Initialize all descriptors */
298 for (cur_rx_desc = port_info->rx_desc_base,
299 rx_buf = port_info->rx_buf_base, i = 0;
300 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
301 cur_rx_desc->rd0 = RD_RACT;
302 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900303 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900304 }
305
306 /* Mark the end of the descriptors */
307 cur_rx_desc--;
308 cur_rx_desc->rd0 |= RD_RDLE;
309
310 /* Point the controller to the rx descriptor list */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900311 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900312#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900313 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
314 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
315 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900316#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900317
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900318 return ret;
319
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900320err_buf_alloc:
321 free(port_info->rx_desc_alloc);
322 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900323
324err:
325 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900326}
327
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900328static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900329{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900330 int port = eth->port;
331 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900332
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900333 if (port_info->tx_desc_alloc) {
334 free(port_info->tx_desc_alloc);
335 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900336 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900337}
338
339static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
340{
341 int port = eth->port;
342 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900343
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900344 if (port_info->rx_desc_alloc) {
345 free(port_info->rx_desc_alloc);
346 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900347 }
348
Nobuhiro Iwamatsu1c822112014-11-04 09:15:47 +0900349 if (port_info->rx_buf_alloc) {
350 free(port_info->rx_buf_alloc);
351 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900352 }
353}
354
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900355static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900356{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900357 int ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900358
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900359 ret = sh_eth_tx_desc_init(eth);
360 if (ret)
361 goto err_tx_init;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900362
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900363 ret = sh_eth_rx_desc_init(eth);
364 if (ret)
365 goto err_rx_init;
366
367 return ret;
368err_rx_init:
369 sh_eth_tx_desc_free(eth);
370
371err_tx_init:
372 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900373}
374
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900375static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900376{
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900377 int port = eth->port, ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900378 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900379 struct eth_device *dev = port_info->dev;
380 struct phy_device *phydev;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900381
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +0000382 phydev = phy_connect(
383 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000384 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900385 port_info->phydev = phydev;
386 phy_config(phydev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900387
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900388 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900389}
390
Nobuhiro Iwamatsu65a81a42017-12-01 08:08:47 +0900391static int sh_eth_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900392{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900393 int port = eth->port, ret = 0;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900394 u32 val;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900395 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingera86bf132009-02-11 19:14:09 -0500396 struct eth_device *dev = port_info->dev;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900397 struct phy_device *phy;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900398
399 /* Configure e-dmac registers */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900400 sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
Nobuhiro Iwamatsu7a2142c2013-08-22 13:22:02 +0900401 (EMDR_DESC | EDMR_EL), EDMR);
402
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900403 sh_eth_write(port_info, 0, EESIPR);
404 sh_eth_write(port_info, 0, TRSCER);
405 sh_eth_write(port_info, 0, TFTR);
406 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
407 sh_eth_write(port_info, RMCR_RST, RMCR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900408#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900409 sh_eth_write(port_info, 0, RPADIR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900410#endif
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900411 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900412
413 /* Configure e-mac registers */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900414 sh_eth_write(port_info, 0, ECSIPR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900415
416 /* Set Mac address */
Mike Frysingera86bf132009-02-11 19:14:09 -0500417 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
418 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900419 sh_eth_write(port_info, val, MAHR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900420
Mike Frysingera86bf132009-02-11 19:14:09 -0500421 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900422 sh_eth_write(port_info, val, MALR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900423
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900424 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000425#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900426 sh_eth_write(port_info, 0, PIPR);
Nobuhiro Iwamatsu46288f42014-01-23 07:52:18 +0900427#endif
428#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900429 sh_eth_write(port_info, APR_AP, APR);
430 sh_eth_write(port_info, MPR_MP, MPR);
431 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900432#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900433
Nobuhiro Iwamatsu4ad2c2a2012-08-02 22:08:40 +0000434#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900435 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Marek Vasutee2f21b2018-01-22 01:42:32 +0100436#elif defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900437 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000438#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900439 /* Configure phy */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900440 ret = sh_eth_phy_config(eth);
441 if (ret) {
Nobuhiro Iwamatsufc4b0a22009-06-25 16:33:04 +0900442 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900443 goto err_phy_cfg;
444 }
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900445 phy = port_info->phydev;
Timur Tabi42387462012-07-09 08:52:43 +0000446 ret = phy_startup(phy);
447 if (ret) {
448 printf(SHETHER_NAME ": phy startup failure\n");
449 return ret;
450 }
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900451
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900452 val = 0;
453
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900454 /* Set the transfer speed */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900455 if (phy->speed == 100) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900456 printf(SHETHER_NAME ": 100Base/");
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_100B, 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, 1, RTRATE);
Marek Vasutee2f21b2018-01-22 01:42:32 +0100461#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900462 val = ECMR_RTM;
463#endif
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900464 } else if (phy->speed == 10) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900465 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000466#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900467 sh_eth_write(port_info, GECMR_10B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000468#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900469 sh_eth_write(port_info, 0, RTRATE);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900470#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900471 }
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000472#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000473 else if (phy->speed == 1000) {
474 printf(SHETHER_NAME ": 1000Base/");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900475 sh_eth_write(port_info, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000476 }
477#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900478
479 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900480 if (phy->duplex) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900481 printf("Full\n");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900482 sh_eth_write(port_info,
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900483 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000484 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900485 } else {
486 printf("Half\n");
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900487 sh_eth_write(port_info,
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900488 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
489 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900490 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900491
492 return ret;
493
494err_phy_cfg:
495 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900496}
497
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900498static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900499{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900500 struct sh_eth_info *port_info = &eth->port_info[eth->port];
501
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900502 /*
503 * Enable the e-dmac receiver only. The transmitter will be enabled when
504 * we have something to transmit
505 */
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900506 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900507}
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900508
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900509static void sh_eth_stop(struct sh_eth_dev *eth)
510{
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900511 struct sh_eth_info *port_info = &eth->port_info[eth->port];
512
513 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900514}
515
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900516int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900517{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900518 int ret = 0;
519 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900520
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900521 ret = sh_eth_reset(eth);
522 if (ret)
523 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900524
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900525 ret = sh_eth_desc_init(eth);
526 if (ret)
527 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900528
Nobuhiro Iwamatsu65a81a42017-12-01 08:08:47 +0900529 ret = sh_eth_config(eth);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900530 if (ret)
531 goto err_config;
532
533 sh_eth_start(eth);
534
535 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900536
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900537err_config:
538 sh_eth_tx_desc_free(eth);
539 sh_eth_rx_desc_free(eth);
540
541err:
542 return ret;
543}
544
545void sh_eth_halt(struct eth_device *dev)
546{
547 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900548
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900549 sh_eth_stop(eth);
550}
551
552int sh_eth_initialize(bd_t *bd)
553{
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900554 int ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900555 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900556 struct eth_device *dev = NULL;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900557 struct mii_dev *mdiodev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900558
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900559 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900560 if (!eth) {
561 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
562 ret = -ENOMEM;
563 goto err;
564 }
565
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900566 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900567 if (!dev) {
568 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
569 ret = -ENOMEM;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900570 goto err;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900571 }
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900572 memset(dev, 0, sizeof(struct eth_device));
573 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900574
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900575 eth->port = CONFIG_SH_ETHER_USE_PORT;
576 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900577 eth->port_info[eth->port].iobase =
578 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900579
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900580 dev->priv = (void *)eth;
581 dev->iobase = 0;
582 dev->init = sh_eth_init;
583 dev->halt = sh_eth_halt;
Marek Vasut044eb2d2018-01-21 14:27:51 +0100584 dev->send = sh_eth_send_legacy;
585 dev->recv = sh_eth_recv_legacy;
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900586 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900587
Ben Whitten34fd6c92015-12-30 13:05:58 +0000588 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900589
Nobuhiro Iwamatsu31e84df2014-01-23 07:52:19 +0900590 /* Register Device to EtherNet subsystem */
591 eth_register(dev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900592
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900593 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900594 mdiodev = mdio_alloc();
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500595 if (!mdiodev)
596 return -ENOMEM;
597 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
598 mdiodev->read = bb_miiphy_read;
599 mdiodev->write = bb_miiphy_write;
600
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900601 ret = mdio_register(mdiodev);
602 if (ret < 0)
603 return ret;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900604
Simon Glass399a9ce2017-08-03 12:22:14 -0600605 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingera86bf132009-02-11 19:14:09 -0500606 puts("Please set MAC address\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900607
608 return ret;
609
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900610err:
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900611 if (dev)
612 free(dev);
613
614 if (eth)
615 free(eth);
616
617 printf(SHETHER_NAME ": Failed\n");
618 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900619}
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900620
621/******* for bb_miiphy *******/
622static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
623{
624 return 0;
625}
626
627static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
628{
629 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900630 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900631
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900632 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900633
634 return 0;
635}
636
637static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
638{
639 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900640 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900641
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900642 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900643
644 return 0;
645}
646
647static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
648{
649 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900650 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900651
652 if (v)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900653 sh_eth_write(port_info,
654 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900655 else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900656 sh_eth_write(port_info,
657 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900658
659 return 0;
660}
661
662static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
663{
664 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900665 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900666
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900667 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900668
669 return 0;
670}
671
672static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
673{
674 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900675 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900676
677 if (v)
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900678 sh_eth_write(port_info,
679 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900680 else
Nobuhiro Iwamatsuec921f12017-12-01 08:10:32 +0900681 sh_eth_write(port_info,
682 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900683
684 return 0;
685}
686
687static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
688{
689 udelay(10);
690
691 return 0;
692}
693
694struct bb_miiphy_bus bb_miiphy_buses[] = {
695 {
696 .name = "sh_eth",
697 .init = sh_eth_bb_init,
698 .mdio_active = sh_eth_bb_mdio_active,
699 .mdio_tristate = sh_eth_bb_mdio_tristate,
700 .set_mdio = sh_eth_bb_set_mdio,
701 .get_mdio = sh_eth_bb_get_mdio,
702 .set_mdc = sh_eth_bb_set_mdc,
703 .delay = sh_eth_bb_delay,
704 }
705};
Nobuhiro Iwamatsuca36b0e2017-12-01 08:08:00 +0900706
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900707int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);