blob: c5bc97c865a28d729d3e4fab1e7fbabee5da7f1a [file] [log] [blame]
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09001/*
Yoshihiro Shimoda26235092012-06-26 16:38:06 +00002 * sh_eth.c - Driver for Renesas ethernet controler.
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09003 *
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +09004 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
5 * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09006 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
Nobuhiro Iwamatsu87076782013-08-22 13:22:04 +09007 * Copyright (C) 2013 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>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090018#include <asm/errno.h>
19#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) \
32 flush_dcache_range((u32)addr, (u32)(addr + len - 1))
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090033#else
34#define flush_cache_wback(...)
35#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090036
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090037#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
38#define invalidate_cache(addr, len) \
39 { \
40 u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
41 u32 start, end; \
42 \
43 start = (u32)addr; \
44 end = start + len; \
45 start &= ~(line_size - 1); \
46 end = ((end + line_size - 1) & ~(line_size - 1)); \
47 \
48 invalidate_dcache_range(start, end); \
49 }
50#else
51#define invalidate_cache(...)
52#endif
53
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090054#define TIMEOUT_CNT 1000
55
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000056int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090057{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090058 struct sh_eth_dev *eth = dev->priv;
59 int port = eth->port, ret = 0, timeout;
60 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro 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 Iwamatsue2752db2014-01-23 07:52:19 +090070 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n"
71 , __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
86 /* Restart the transmitter if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +000087 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
88 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090089
90 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090091 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090092 do {
93 invalidate_cache(port_info->tx_desc_cur,
94 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090095 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090096 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090097
98 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090099 printf(SHETHER_NAME ": transmit timeout\n");
100 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900101 goto err;
102 }
103
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900104 port_info->tx_desc_cur++;
105 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
106 port_info->tx_desc_cur = port_info->tx_desc_base;
107
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900108err:
109 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900110}
111
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900112int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900113{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900114 struct sh_eth_dev *eth = dev->priv;
115 int port = eth->port, len = 0;
116 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000117 uchar *packet;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900118
119 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900120 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900121 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
122 /* Check for errors */
123 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
124 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000125 packet = (uchar *)
126 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900127 invalidate_cache(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900128 NetReceive(packet, len);
129 }
130
131 /* Make current descriptor available again */
132 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
133 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
134 else
135 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900136 /* Point to the next descriptor */
137 port_info->rx_desc_cur++;
138 if (port_info->rx_desc_cur >=
139 port_info->rx_desc_base + NUM_RX_DESC)
140 port_info->rx_desc_cur = port_info->rx_desc_base;
141 }
142
143 /* Restart the receiver if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000144 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
145 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900146
147 return len;
148}
149
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900150static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900151{
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900152#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900153 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900154
155 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000156 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900157
158 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000159 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900160 for (i = 0; i < TIMEOUT_CNT; i++) {
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000161 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900162 break;
163 udelay(1000);
164 }
165
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900166 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900167 printf(SHETHER_NAME ": Software reset timeout\n");
168 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900169 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900170
171 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900172#else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000173 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900174 udelay(3000);
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000175 sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900176
177 return 0;
178#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900179}
180
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900181static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900182{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900183 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900184 u32 tmp_addr;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900185 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900186 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900187
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900188 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900189 * Allocate rx descriptors. They must be aligned to size of struct
190 * tx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900191 */
192 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900193 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900194 sizeof(struct tx_desc_s) - 1);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900195 if (!port_info->tx_desc_malloc) {
196 printf(SHETHER_NAME ": malloc failed\n");
197 ret = -ENOMEM;
198 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900199 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900200
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900201 tmp_addr = (u32) (((int)port_info->tx_desc_malloc +
202 sizeof(struct tx_desc_s) - 1) &
203 ~(sizeof(struct tx_desc_s) - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900204 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900205 /* Make sure we use a P2 address (non-cacheable) */
206 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900207 port_info->tx_desc_cur = port_info->tx_desc_base;
208
209 /* Initialize all descriptors */
210 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
211 cur_tx_desc++, i++) {
212 cur_tx_desc->td0 = 0x00;
213 cur_tx_desc->td1 = 0x00;
214 cur_tx_desc->td2 = 0x00;
215 }
216
217 /* Mark the end of the descriptors */
218 cur_tx_desc--;
219 cur_tx_desc->td0 |= TD_TDLE;
220
221 /* Point the controller to the tx descriptor list. Must use physical
222 addresses */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000223 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900224#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000225 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
226 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
227 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900228#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900229
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900230err:
231 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900232}
233
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900234static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900235{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900236 int port = eth->port, i , ret = 0;
237 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900238 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900239 u32 tmp_addr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900240 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900241
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900242 /*
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900243 * Allocate rx descriptors. They must be aligned to size of struct
244 * rx_desc_s.
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900245 */
246 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900247 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900248 sizeof(struct rx_desc_s) - 1);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900249 if (!port_info->rx_desc_malloc) {
250 printf(SHETHER_NAME ": malloc failed\n");
251 ret = -ENOMEM;
252 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900253 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900254
Nobuhiro Iwamatsu703949e2014-11-04 09:15:46 +0900255 tmp_addr = (u32) (((int)port_info->rx_desc_malloc +
256 sizeof(struct rx_desc_s) - 1) &
257 ~(sizeof(struct rx_desc_s) - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900258 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900259 /* Make sure we use a P2 address (non-cacheable) */
260 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
261
262 port_info->rx_desc_cur = port_info->rx_desc_base;
263
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900264 /*
265 * Allocate rx data buffers. They must be 32 bytes aligned and in
266 * P2 area
267 */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900268 port_info->rx_buf_malloc = malloc(
269 NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900270 if (!port_info->rx_buf_malloc) {
271 printf(SHETHER_NAME ": malloc failed\n");
272 ret = -ENOMEM;
273 goto err_buf_malloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900274 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900275
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900276 tmp_addr = (u32)(((int)port_info->rx_buf_malloc
277 + (RX_BUF_ALIGNE_SIZE - 1)) &
278 ~(RX_BUF_ALIGNE_SIZE - 1));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900279 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
280
281 /* Initialize all descriptors */
282 for (cur_rx_desc = port_info->rx_desc_base,
283 rx_buf = port_info->rx_buf_base, i = 0;
284 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
285 cur_rx_desc->rd0 = RD_RACT;
286 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
287 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
288 }
289
290 /* Mark the end of the descriptors */
291 cur_rx_desc--;
292 cur_rx_desc->rd0 |= RD_RDLE;
293
294 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000295 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900296#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000297 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
298 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
299 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900300#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900301
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900302 return ret;
303
304err_buf_malloc:
305 free(port_info->rx_desc_malloc);
306 port_info->rx_desc_malloc = NULL;
307
308err:
309 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900310}
311
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900312static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900313{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900314 int port = eth->port;
315 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900316
317 if (port_info->tx_desc_malloc) {
318 free(port_info->tx_desc_malloc);
319 port_info->tx_desc_malloc = NULL;
320 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900321}
322
323static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
324{
325 int port = eth->port;
326 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900327
328 if (port_info->rx_desc_malloc) {
329 free(port_info->rx_desc_malloc);
330 port_info->rx_desc_malloc = NULL;
331 }
332
333 if (port_info->rx_buf_malloc) {
334 free(port_info->rx_buf_malloc);
335 port_info->rx_buf_malloc = NULL;
336 }
337}
338
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900339static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900340{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900341 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900342
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900343 ret = sh_eth_tx_desc_init(eth);
344 if (ret)
345 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900346
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900347 ret = sh_eth_rx_desc_init(eth);
348 if (ret)
349 goto err_rx_init;
350
351 return ret;
352err_rx_init:
353 sh_eth_tx_desc_free(eth);
354
355err_tx_init:
356 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900357}
358
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900359static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900360{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900361 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900362 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900363 struct eth_device *dev = port_info->dev;
364 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900365
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000366 phydev = phy_connect(
367 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000368 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900369 port_info->phydev = phydev;
370 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900371
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900372 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900373}
374
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900375static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900376{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900377 int port = eth->port, ret = 0;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900378 u32 val;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900379 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingerc527ce92009-02-11 19:14:09 -0500380 struct eth_device *dev = port_info->dev;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900381 struct phy_device *phy;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900382
383 /* Configure e-dmac registers */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900384 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
385 (EMDR_DESC | EDMR_EL), EDMR);
386
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000387 sh_eth_write(eth, 0, EESIPR);
388 sh_eth_write(eth, 0, TRSCER);
389 sh_eth_write(eth, 0, TFTR);
390 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
391 sh_eth_write(eth, RMCR_RST, RMCR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900392#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000393 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900394#endif
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000395 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900396
397 /* Configure e-mac registers */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000398 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900399
400 /* Set Mac address */
Mike Frysingerc527ce92009-02-11 19:14:09 -0500401 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
402 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000403 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900404
Mike Frysingerc527ce92009-02-11 19:14:09 -0500405 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000406 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900407
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000408 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000409#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000410 sh_eth_write(eth, 0, PIPR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900411#endif
412#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000413 sh_eth_write(eth, APR_AP, APR);
414 sh_eth_write(eth, MPR_MP, MPR);
415 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900416#endif
417
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000418#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000419 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsu17243742014-06-24 17:01:08 +0900420#elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791) || \
Nobuhiro Iwamatsua341b7e2014-11-04 09:13:40 +0900421 defined(CONFIG_R8A7793) || defined(CONFIG_R8A7794)
Nobuhiro Iwamatsu87076782013-08-22 13:22:04 +0900422 sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000423#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900424 /* Configure phy */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900425 ret = sh_eth_phy_config(eth);
426 if (ret) {
Nobuhiro Iwamatsu88a4c2e2009-06-25 16:33:04 +0900427 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900428 goto err_phy_cfg;
429 }
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900430 phy = port_info->phydev;
Timur Tabi11af8d62012-07-09 08:52:43 +0000431 ret = phy_startup(phy);
432 if (ret) {
433 printf(SHETHER_NAME ": phy startup failure\n");
434 return ret;
435 }
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900436
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900437 val = 0;
438
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900439 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900440 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900441 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000442#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000443 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000444#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000445 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu47ce8892013-09-24 15:38:33 +0900446#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
Nobuhiro Iwamatsua341b7e2014-11-04 09:13:40 +0900447 defined(CONFIG_R8A7791) || defined(CONFIG_R8A7793) || \
448 defined(CONFIG_R8A7794)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900449 val = ECMR_RTM;
450#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900451 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900452 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000453#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000454 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000455#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000456 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900457#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900458 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000459#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000460 else if (phy->speed == 1000) {
461 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000462 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000463 }
464#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900465
466 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900467 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900468 printf("Full\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000469 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
470 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900471 } else {
472 printf("Half\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000473 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900474 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900475
476 return ret;
477
478err_phy_cfg:
479 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900480}
481
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900482static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900483{
484 /*
485 * Enable the e-dmac receiver only. The transmitter will be enabled when
486 * we have something to transmit
487 */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000488 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900489}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900490
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900491static void sh_eth_stop(struct sh_eth_dev *eth)
492{
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000493 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900494}
495
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900496int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900497{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900498 int ret = 0;
499 struct sh_eth_dev *eth = dev->priv;
500
501 ret = sh_eth_reset(eth);
502 if (ret)
503 goto err;
504
505 ret = sh_eth_desc_init(eth);
506 if (ret)
507 goto err;
508
509 ret = sh_eth_config(eth, bd);
510 if (ret)
511 goto err_config;
512
513 sh_eth_start(eth);
514
515 return ret;
516
517err_config:
518 sh_eth_tx_desc_free(eth);
519 sh_eth_rx_desc_free(eth);
520
521err:
522 return ret;
523}
524
525void sh_eth_halt(struct eth_device *dev)
526{
527 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900528 sh_eth_stop(eth);
529}
530
531int sh_eth_initialize(bd_t *bd)
532{
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900533 int ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900534 struct sh_eth_dev *eth = NULL;
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900535 struct eth_device *dev = NULL;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900536
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900537 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900538 if (!eth) {
539 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
540 ret = -ENOMEM;
541 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900542 }
543
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900544 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900545 if (!dev) {
546 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
547 ret = -ENOMEM;
548 goto err;
549 }
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900550 memset(dev, 0, sizeof(struct eth_device));
551 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900552
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900553 eth->port = CONFIG_SH_ETHER_USE_PORT;
554 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
555
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900556 dev->priv = (void *)eth;
557 dev->iobase = 0;
558 dev->init = sh_eth_init;
559 dev->halt = sh_eth_halt;
560 dev->send = sh_eth_send;
561 dev->recv = sh_eth_recv;
562 eth->port_info[eth->port].dev = dev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900563
564 sprintf(dev->name, SHETHER_NAME);
565
Nobuhiro Iwamatsue2752db2014-01-23 07:52:19 +0900566 /* Register Device to EtherNet subsystem */
567 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900568
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900569 bb_miiphy_buses[0].priv = eth;
570 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
571
Mike Frysingerc527ce92009-02-11 19:14:09 -0500572 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
573 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900574
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900575 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900576
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900577err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900578 if (dev)
579 free(dev);
580
581 if (eth)
582 free(eth);
583
584 printf(SHETHER_NAME ": Failed\n");
585 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900586}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900587
588/******* for bb_miiphy *******/
589static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
590{
591 return 0;
592}
593
594static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
595{
596 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900597
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000598 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900599
600 return 0;
601}
602
603static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
604{
605 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900606
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000607 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900608
609 return 0;
610}
611
612static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
613{
614 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900615
616 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000617 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900618 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000619 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900620
621 return 0;
622}
623
624static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
625{
626 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900627
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000628 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900629
630 return 0;
631}
632
633static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
634{
635 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900636
637 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000638 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900639 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000640 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900641
642 return 0;
643}
644
645static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
646{
647 udelay(10);
648
649 return 0;
650}
651
652struct bb_miiphy_bus bb_miiphy_buses[] = {
653 {
654 .name = "sh_eth",
655 .init = sh_eth_bb_init,
656 .mdio_active = sh_eth_bb_mdio_active,
657 .mdio_tristate = sh_eth_bb_mdio_tristate,
658 .set_mdio = sh_eth_bb_set_mdio,
659 .get_mdio = sh_eth_bb_get_mdio,
660 .set_mdc = sh_eth_bb_set_mdc,
661 .delay = sh_eth_bb_delay,
662 }
663};
664int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);