blob: 47d25c550ee781965ad0223aab182239ba068472 [file] [log] [blame]
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09001/*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -04002 * sh_eth.c - Driver for Renesas ethernet controller.
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09003 *
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +09004 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
Nobuhiro Iwamatsuf7ca1f72014-11-04 09:15:48 +09005 * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09006 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
Nobuhiro Iwamatsuf7ca1f72014-11-04 09:15:48 +09007 * Copyright (C) 2013, 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09008 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090010 */
11
12#include <config.h>
13#include <common.h>
14#include <malloc.h>
15#include <net.h>
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090016#include <netdev.h>
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +090017#include <miiphy.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
Nobuhiro Iwamatsu9751ee02008-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 Iwamatsu870cc232013-08-22 13:22:01 +090029
Nobuhiro Iwamatsu92f07132013-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 Iwamatsuaae5d232017-12-01 13:56:08 +090032 flush_dcache_range((u32)addr, \
33 (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090034#else
35#define flush_cache_wback(...)
36#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090037
Nobuhiro Iwamatsu92f07132013-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 Iwamatsu4ba62c72012-01-11 10:23:51 +090055#define TIMEOUT_CNT 1000
56
Marek Vasutdca221b2018-01-21 14:27:51 +010057static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090058{
Nobuhiro Iwamatsubd3980c2008-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 Iwamatsu9751ee02008-06-11 21:05:00 +090061
62 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090063 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
64 ret = -EINVAL;
65 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090066 }
67
68 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +000069 if ((int)packet & 3) {
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +090070 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +090071 , __func__);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090072 ret = -EFAULT;
73 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090074 }
75
76 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090077 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-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 Iwamatsuf7ca1f72014-11-04 09:15:48 +090086 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
87
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090088 /* Restart the transmitter if disabled */
Nobuhiro Iwamatsufbfb5112017-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 Iwamatsu9751ee02008-06-11 21:05:00 +090091
92 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090093 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090094 do {
95 invalidate_cache(port_info->tx_desc_cur,
96 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090097 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090098 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090099
100 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900101 printf(SHETHER_NAME ": transmit timeout\n");
102 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900103 goto err;
104 }
105
Nobuhiro Iwamatsu9751ee02008-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 Iwamatsubd3980c2008-11-21 12:04:18 +0900110err:
111 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900112}
113
Marek Vasutdca221b2018-01-21 14:27:51 +0100114static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900115{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900116 struct sh_eth_dev *eth = dev->priv;
Marek Vasutdca221b2018-01-21 14:27:51 +0100117
118 return sh_eth_send_common(eth, packet, len);
119}
120
Marek Vasut52c15e22018-01-21 15:39:50 +0100121static int sh_eth_recv_start(struct sh_eth_dev *eth)
122{
123 int port = eth->port, len = 0;
124 struct sh_eth_info *port_info = &eth->port_info[port];
125
126 /* Check if the rx descriptor is ready */
127 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
128 if (port_info->rx_desc_cur->rd0 & RD_RACT)
129 return -EINVAL;
130
131 /* Check for errors */
132 if (port_info->rx_desc_cur->rd0 & RD_RFE)
133 return -EINVAL;
134
135 len = port_info->rx_desc_cur->rd1 & 0xffff;
136
137 return len;
138}
139
140static void sh_eth_recv_finish(struct sh_eth_dev *eth)
141{
142 struct sh_eth_info *port_info = &eth->port_info[eth->port];
143
144 /* Make current descriptor available again */
145 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
146 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
147 else
148 port_info->rx_desc_cur->rd0 = RD_RACT;
149
150 flush_cache_wback(port_info->rx_desc_cur,
151 sizeof(struct rx_desc_s));
152
153 /* Point to the next descriptor */
154 port_info->rx_desc_cur++;
155 if (port_info->rx_desc_cur >=
156 port_info->rx_desc_base + NUM_RX_DESC)
157 port_info->rx_desc_cur = port_info->rx_desc_base;
158}
159
Marek Vasutdca221b2018-01-21 14:27:51 +0100160static int sh_eth_recv_common(struct sh_eth_dev *eth)
161{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900162 int port = eth->port, len = 0;
163 struct sh_eth_info *port_info = &eth->port_info[port];
Marek Vasut52c15e22018-01-21 15:39:50 +0100164 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900165
Marek Vasut52c15e22018-01-21 15:39:50 +0100166 len = sh_eth_recv_start(eth);
167 if (len > 0) {
168 invalidate_cache(packet, len);
169 net_process_received_packet(packet, len);
170 sh_eth_recv_finish(eth);
171 } else
172 len = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900173
174 /* Restart the receiver if disabled */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900175 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
176 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900177
178 return len;
179}
180
Marek Vasutdca221b2018-01-21 14:27:51 +0100181static int sh_eth_recv_legacy(struct eth_device *dev)
182{
183 struct sh_eth_dev *eth = dev->priv;
184
185 return sh_eth_recv_common(eth);
186}
187
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900188static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900189{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900190 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900191#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900192 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900193
194 /* Start e-dmac transmitter and receiver */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900195 sh_eth_write(port_info, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900196
197 /* Perform a software reset and wait for it to complete */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900198 sh_eth_write(port_info, EDMR_SRST, EDMR);
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900199 for (i = 0; i < TIMEOUT_CNT; i++) {
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900200 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900201 break;
202 udelay(1000);
203 }
204
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900205 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900206 printf(SHETHER_NAME ": Software reset timeout\n");
207 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900208 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900209
210 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900211#else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900212 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900213 udelay(3000);
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900214 sh_eth_write(port_info,
215 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900216
217 return 0;
218#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900219}
220
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900221static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900222{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900223 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900224 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900225 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900226 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900227
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900228 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900229 * Allocate rx descriptors. They must be aligned to size of struct
230 * tx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900231 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900232 port_info->tx_desc_alloc =
233 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
234 if (!port_info->tx_desc_alloc) {
235 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900236 ret = -ENOMEM;
237 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900238 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900239
Nobuhiro Iwamatsuaae5d232017-12-01 13:56:08 +0900240 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900241
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900242 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900243 port_info->tx_desc_base =
244 (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900245 port_info->tx_desc_cur = port_info->tx_desc_base;
246
247 /* Initialize all descriptors */
248 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
249 cur_tx_desc++, i++) {
250 cur_tx_desc->td0 = 0x00;
251 cur_tx_desc->td1 = 0x00;
252 cur_tx_desc->td2 = 0x00;
253 }
254
255 /* Mark the end of the descriptors */
256 cur_tx_desc--;
257 cur_tx_desc->td0 |= TD_TDLE;
258
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900259 /*
260 * Point the controller to the tx descriptor list. Must use physical
261 * addresses
262 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900263 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900264#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900265 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
266 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
267 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900268#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900269
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900270err:
271 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900272}
273
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900274static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900275{
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900276 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900277 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900278 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900279 struct rx_desc_s *cur_rx_desc;
280 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900281
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900282 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900283 * Allocate rx descriptors. They must be aligned to size of struct
284 * rx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900285 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900286 port_info->rx_desc_alloc =
287 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
288 if (!port_info->rx_desc_alloc) {
289 printf(SHETHER_NAME ": memalign failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900290 ret = -ENOMEM;
291 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900292 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900293
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900294 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
295
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900296 /* Make sure we use a P2 address (non-cacheable) */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900297 port_info->rx_desc_base =
298 (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900299
300 port_info->rx_desc_cur = port_info->rx_desc_base;
301
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900302 /*
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900303 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
304 * aligned and in P2 area.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900305 */
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900306 port_info->rx_buf_alloc =
307 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
308 if (!port_info->rx_buf_alloc) {
309 printf(SHETHER_NAME ": alloc failed\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900310 ret = -ENOMEM;
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900311 goto err_buf_alloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900312 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900313
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900314 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900315
316 /* Initialize all descriptors */
317 for (cur_rx_desc = port_info->rx_desc_base,
318 rx_buf = port_info->rx_buf_base, i = 0;
319 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
320 cur_rx_desc->rd0 = RD_RACT;
321 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900322 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900323 }
324
325 /* Mark the end of the descriptors */
326 cur_rx_desc--;
327 cur_rx_desc->rd0 |= RD_RDLE;
328
329 /* Point the controller to the rx descriptor list */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900330 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900331#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900332 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
333 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
334 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900335#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900336
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900337 return ret;
338
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900339err_buf_alloc:
340 free(port_info->rx_desc_alloc);
341 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900342
343err:
344 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900345}
346
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900347static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900348{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900349 int port = eth->port;
350 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900351
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900352 if (port_info->tx_desc_alloc) {
353 free(port_info->tx_desc_alloc);
354 port_info->tx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900355 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900356}
357
358static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
359{
360 int port = eth->port;
361 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900362
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900363 if (port_info->rx_desc_alloc) {
364 free(port_info->rx_desc_alloc);
365 port_info->rx_desc_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900366 }
367
Nobuhiro Iwamatsu000889c2014-11-04 09:15:47 +0900368 if (port_info->rx_buf_alloc) {
369 free(port_info->rx_buf_alloc);
370 port_info->rx_buf_alloc = NULL;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900371 }
372}
373
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900374static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900375{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900376 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900377
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900378 ret = sh_eth_tx_desc_init(eth);
379 if (ret)
380 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900381
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900382 ret = sh_eth_rx_desc_init(eth);
383 if (ret)
384 goto err_rx_init;
385
386 return ret;
387err_rx_init:
388 sh_eth_tx_desc_free(eth);
389
390err_tx_init:
391 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900392}
393
Marek Vasut68ac92e2018-01-21 14:55:44 +0100394static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
395 unsigned char *mac)
396{
397 u32 val;
398
399 val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
400 sh_eth_write(port_info, val, MAHR);
401
402 val = (mac[4] << 8) | mac[5];
403 sh_eth_write(port_info, val, MALR);
404}
405
Marek Vasut013af642018-01-21 15:10:21 +0100406static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900407{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900408 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900409 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900410 struct eth_device *dev = port_info->dev;
411 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900412
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000413 phydev = phy_connect(
414 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000415 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900416 port_info->phydev = phydev;
417 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900418
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900419 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900420}
421
Marek Vasut013af642018-01-21 15:10:21 +0100422static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900423{
Marek Vasut013af642018-01-21 15:10:21 +0100424 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900425
426 /* Configure e-dmac registers */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900427 sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) |
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900428 (EMDR_DESC | EDMR_EL), EDMR);
429
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900430 sh_eth_write(port_info, 0, EESIPR);
431 sh_eth_write(port_info, 0, TRSCER);
432 sh_eth_write(port_info, 0, TFTR);
433 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
434 sh_eth_write(port_info, RMCR_RST, RMCR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900435#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900436 sh_eth_write(port_info, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900437#endif
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900438 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900439
440 /* Configure e-mac registers */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900441 sh_eth_write(port_info, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900442
443 /* Set Mac address */
Marek Vasut013af642018-01-21 15:10:21 +0100444 sh_eth_write_hwaddr(port_info, mac);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900445
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900446 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000447#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900448 sh_eth_write(port_info, 0, PIPR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900449#endif
450#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900451 sh_eth_write(port_info, APR_AP, APR);
452 sh_eth_write(port_info, MPR_MP, MPR);
453 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900454#endif
455
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000456#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900457 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Marek Vasuteffb7902018-01-22 01:42:32 +0100458#elif defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900459 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000460#endif
Marek Vasut013af642018-01-21 15:10:21 +0100461}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900462
Marek Vasut013af642018-01-21 15:10:21 +0100463static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
464{
465 struct sh_eth_info *port_info = &eth->port_info[eth->port];
466 struct phy_device *phy = port_info->phydev;
467 int ret = 0;
468 u32 val = 0;
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900469
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900470 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900471 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900472 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000473#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900474 sh_eth_write(port_info, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000475#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900476 sh_eth_write(port_info, 1, RTRATE);
Marek Vasuteffb7902018-01-22 01:42:32 +0100477#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900478 val = ECMR_RTM;
479#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900480 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900481 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000482#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900483 sh_eth_write(port_info, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000484#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900485 sh_eth_write(port_info, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900486#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900487 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000488#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000489 else if (phy->speed == 1000) {
490 printf(SHETHER_NAME ": 1000Base/");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900491 sh_eth_write(port_info, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000492 }
493#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900494
495 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900496 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900497 printf("Full\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900498 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900499 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000500 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900501 } else {
502 printf("Half\n");
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900503 sh_eth_write(port_info,
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900504 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
505 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900506 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900507
508 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900509}
510
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900511static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900512{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900513 struct sh_eth_info *port_info = &eth->port_info[eth->port];
514
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900515 /*
516 * Enable the e-dmac receiver only. The transmitter will be enabled when
517 * we have something to transmit
518 */
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900519 sh_eth_write(port_info, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900520}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900521
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900522static void sh_eth_stop(struct sh_eth_dev *eth)
523{
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900524 struct sh_eth_info *port_info = &eth->port_info[eth->port];
525
526 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900527}
528
Marek Vasut013af642018-01-21 15:10:21 +0100529static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900530{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900531 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900532
533 ret = sh_eth_reset(eth);
534 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100535 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900536
537 ret = sh_eth_desc_init(eth);
538 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100539 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900540
Marek Vasut013af642018-01-21 15:10:21 +0100541 sh_eth_mac_regs_config(eth, mac);
542
543 return 0;
544}
545
546static int sh_eth_start_common(struct sh_eth_dev *eth)
547{
548 struct sh_eth_info *port_info = &eth->port_info[eth->port];
549 int ret;
550
551 ret = phy_startup(port_info->phydev);
552 if (ret) {
553 printf(SHETHER_NAME ": phy startup failure\n");
554 return ret;
555 }
556
557 ret = sh_eth_phy_regs_config(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900558 if (ret)
Marek Vasut013af642018-01-21 15:10:21 +0100559 return ret;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900560
561 sh_eth_start(eth);
562
Marek Vasut013af642018-01-21 15:10:21 +0100563 return 0;
564}
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900565
Marek Vasut013af642018-01-21 15:10:21 +0100566static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd)
567{
568 struct sh_eth_dev *eth = dev->priv;
569 int ret;
570
571 ret = sh_eth_init_common(eth, dev->enetaddr);
572 if (ret)
573 return ret;
574
575 ret = sh_eth_phy_config_legacy(eth);
576 if (ret) {
577 printf(SHETHER_NAME ": phy config timeout\n");
578 goto err_start;
579 }
580
581 ret = sh_eth_start_common(eth);
582 if (ret)
583 goto err_start;
584
585 return 0;
586
587err_start:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900588 sh_eth_tx_desc_free(eth);
589 sh_eth_rx_desc_free(eth);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900590 return ret;
591}
592
Marek Vasut013af642018-01-21 15:10:21 +0100593void sh_eth_halt_legacy(struct eth_device *dev)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900594{
595 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900596
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900597 sh_eth_stop(eth);
598}
599
600int sh_eth_initialize(bd_t *bd)
601{
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900602 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900603 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900604 struct eth_device *dev = NULL;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900605 struct mii_dev *mdiodev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900606
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900607 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900608 if (!eth) {
609 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
610 ret = -ENOMEM;
611 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900612 }
613
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900614 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900615 if (!dev) {
616 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
617 ret = -ENOMEM;
618 goto err;
619 }
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900620 memset(dev, 0, sizeof(struct eth_device));
621 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900622
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900623 eth->port = CONFIG_SH_ETHER_USE_PORT;
624 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900625 eth->port_info[eth->port].iobase =
626 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900627
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900628 dev->priv = (void *)eth;
629 dev->iobase = 0;
Marek Vasut013af642018-01-21 15:10:21 +0100630 dev->init = sh_eth_init_legacy;
631 dev->halt = sh_eth_halt_legacy;
Marek Vasutdca221b2018-01-21 14:27:51 +0100632 dev->send = sh_eth_send_legacy;
633 dev->recv = sh_eth_recv_legacy;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900634 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900635
Ben Whitten192bc692015-12-30 13:05:58 +0000636 strcpy(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900637
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900638 /* Register Device to EtherNet subsystem */
639 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900640
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900641 bb_miiphy_buses[0].priv = eth;
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900642 mdiodev = mdio_alloc();
Joe Hershberger5a49f172016-08-08 11:28:38 -0500643 if (!mdiodev)
644 return -ENOMEM;
645 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
646 mdiodev->read = bb_miiphy_read;
647 mdiodev->write = bb_miiphy_write;
648
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900649 ret = mdio_register(mdiodev);
650 if (ret < 0)
651 return ret;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900652
Simon Glass35affd72017-08-03 12:22:14 -0600653 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
Mike Frysingerc527ce92009-02-11 19:14:09 -0500654 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900655
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900656 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900657
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900658err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900659 if (dev)
660 free(dev);
661
662 if (eth)
663 free(eth);
664
665 printf(SHETHER_NAME ": Failed\n");
666 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900667}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900668
669/******* for bb_miiphy *******/
670static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
671{
672 return 0;
673}
674
675static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
676{
677 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900678 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900679
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900680 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900681
682 return 0;
683}
684
685static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
686{
687 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900688 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900689
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900690 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900691
692 return 0;
693}
694
695static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
696{
697 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900698 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900699
700 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900701 sh_eth_write(port_info,
702 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900703 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900704 sh_eth_write(port_info,
705 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900706
707 return 0;
708}
709
710static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
711{
712 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900713 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900714
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900715 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900716
717 return 0;
718}
719
720static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
721{
722 struct sh_eth_dev *eth = bus->priv;
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900723 struct sh_eth_info *port_info = &eth->port_info[eth->port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900724
725 if (v)
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900726 sh_eth_write(port_info,
727 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900728 else
Nobuhiro Iwamatsufbfb5112017-12-01 08:10:32 +0900729 sh_eth_write(port_info,
730 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900731
732 return 0;
733}
734
735static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
736{
737 udelay(10);
738
739 return 0;
740}
741
742struct bb_miiphy_bus bb_miiphy_buses[] = {
743 {
744 .name = "sh_eth",
745 .init = sh_eth_bb_init,
746 .mdio_active = sh_eth_bb_mdio_active,
747 .mdio_tristate = sh_eth_bb_mdio_tristate,
748 .set_mdio = sh_eth_bb_set_mdio,
749 .get_mdio = sh_eth_bb_get_mdio,
750 .set_mdc = sh_eth_bb_set_mdc,
751 .delay = sh_eth_bb_delay,
752 }
753};
Nobuhiro Iwamatsudc148672017-12-01 08:08:00 +0900754
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900755int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);