blob: 0cb963f8decf2927deb4aad4400022e92f6a5e18 [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 Iwamatsubd3980c2008-11-21 12:04:18 +090070 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
71 ret = -EFAULT;
72 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090073 }
74
75 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090076 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090077 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
78 port_info->tx_desc_cur->td1 = len << 16;
79 /* Must preserve the end of descriptor list indication */
80 if (port_info->tx_desc_cur->td0 & TD_TDLE)
81 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
82 else
83 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
84
85 /* Restart the transmitter if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +000086 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
87 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090088
89 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090090 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090091 do {
92 invalidate_cache(port_info->tx_desc_cur,
93 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090094 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090095 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090096
97 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090098 printf(SHETHER_NAME ": transmit timeout\n");
99 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900100 goto err;
101 }
102
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900103 port_info->tx_desc_cur++;
104 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
105 port_info->tx_desc_cur = port_info->tx_desc_base;
106
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900107err:
108 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900109}
110
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900111int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900112{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900113 struct sh_eth_dev *eth = dev->priv;
114 int port = eth->port, len = 0;
115 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000116 uchar *packet;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900117
118 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900119 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900120 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
121 /* Check for errors */
122 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
123 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000124 packet = (uchar *)
125 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900126 invalidate_cache(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900127 NetReceive(packet, len);
128 }
129
130 /* Make current descriptor available again */
131 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
132 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
133 else
134 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900135 /* Point to the next descriptor */
136 port_info->rx_desc_cur++;
137 if (port_info->rx_desc_cur >=
138 port_info->rx_desc_base + NUM_RX_DESC)
139 port_info->rx_desc_cur = port_info->rx_desc_base;
140 }
141
142 /* Restart the receiver if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000143 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
144 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900145
146 return len;
147}
148
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900149static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900150{
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900151#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900152 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900153
154 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000155 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900156
157 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000158 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900159 for (i = 0; i < TIMEOUT_CNT ; i++) {
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000160 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900161 break;
162 udelay(1000);
163 }
164
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900165 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900166 printf(SHETHER_NAME ": Software reset timeout\n");
167 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900168 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900169
170 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900171#else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000172 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900173 udelay(3000);
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000174 sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900175
176 return 0;
177#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900178}
179
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900180static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900181{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900182 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900183 u32 tmp_addr;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900184 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900185 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900186
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900187 /*
188 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
189 */
190 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900191 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900192 TX_DESC_SIZE - 1);
193 if (!port_info->tx_desc_malloc) {
194 printf(SHETHER_NAME ": malloc failed\n");
195 ret = -ENOMEM;
196 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900197 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900198
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900199 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
200 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900201 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900202 /* Make sure we use a P2 address (non-cacheable) */
203 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900204 port_info->tx_desc_cur = port_info->tx_desc_base;
205
206 /* Initialize all descriptors */
207 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
208 cur_tx_desc++, i++) {
209 cur_tx_desc->td0 = 0x00;
210 cur_tx_desc->td1 = 0x00;
211 cur_tx_desc->td2 = 0x00;
212 }
213
214 /* Mark the end of the descriptors */
215 cur_tx_desc--;
216 cur_tx_desc->td0 |= TD_TDLE;
217
218 /* Point the controller to the tx descriptor list. Must use physical
219 addresses */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000220 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900221#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000222 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
223 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
224 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900225#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900226
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900227err:
228 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900229}
230
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900231static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900232{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900233 int port = eth->port, i , ret = 0;
234 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900235 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900236 u32 tmp_addr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900237 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900238
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900239 /*
240 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
241 */
242 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900243 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900244 RX_DESC_SIZE - 1);
245 if (!port_info->rx_desc_malloc) {
246 printf(SHETHER_NAME ": malloc failed\n");
247 ret = -ENOMEM;
248 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900249 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900250
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900251 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
252 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900253 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900254 /* Make sure we use a P2 address (non-cacheable) */
255 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
256
257 port_info->rx_desc_cur = port_info->rx_desc_base;
258
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900259 /*
260 * Allocate rx data buffers. They must be 32 bytes aligned and in
261 * P2 area
262 */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900263 port_info->rx_buf_malloc = malloc(
264 NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900265 if (!port_info->rx_buf_malloc) {
266 printf(SHETHER_NAME ": malloc failed\n");
267 ret = -ENOMEM;
268 goto err_buf_malloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900269 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900270
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900271 tmp_addr = (u32)(((int)port_info->rx_buf_malloc
272 + (RX_BUF_ALIGNE_SIZE - 1)) &
273 ~(RX_BUF_ALIGNE_SIZE - 1));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900274 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
275
276 /* Initialize all descriptors */
277 for (cur_rx_desc = port_info->rx_desc_base,
278 rx_buf = port_info->rx_buf_base, i = 0;
279 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
280 cur_rx_desc->rd0 = RD_RACT;
281 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
282 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
283 }
284
285 /* Mark the end of the descriptors */
286 cur_rx_desc--;
287 cur_rx_desc->rd0 |= RD_RDLE;
288
289 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000290 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900291#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000292 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
293 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
294 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900295#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900296
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900297 return ret;
298
299err_buf_malloc:
300 free(port_info->rx_desc_malloc);
301 port_info->rx_desc_malloc = NULL;
302
303err:
304 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900305}
306
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900307static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900308{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900309 int port = eth->port;
310 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900311
312 if (port_info->tx_desc_malloc) {
313 free(port_info->tx_desc_malloc);
314 port_info->tx_desc_malloc = NULL;
315 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900316}
317
318static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
319{
320 int port = eth->port;
321 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900322
323 if (port_info->rx_desc_malloc) {
324 free(port_info->rx_desc_malloc);
325 port_info->rx_desc_malloc = NULL;
326 }
327
328 if (port_info->rx_buf_malloc) {
329 free(port_info->rx_buf_malloc);
330 port_info->rx_buf_malloc = NULL;
331 }
332}
333
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900334static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900335{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900336 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900337
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900338 ret = sh_eth_tx_desc_init(eth);
339 if (ret)
340 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900341
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900342 ret = sh_eth_rx_desc_init(eth);
343 if (ret)
344 goto err_rx_init;
345
346 return ret;
347err_rx_init:
348 sh_eth_tx_desc_free(eth);
349
350err_tx_init:
351 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900352}
353
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900354static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900355{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900356 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900357 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900358 struct eth_device *dev = port_info->dev;
359 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900360
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000361 phydev = phy_connect(
362 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000363 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900364 port_info->phydev = phydev;
365 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900366
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900367 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900368}
369
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900370static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900371{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900372 int port = eth->port, ret = 0;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900373 u32 val;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900374 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingerc527ce92009-02-11 19:14:09 -0500375 struct eth_device *dev = port_info->dev;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900376 struct phy_device *phy;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900377
378 /* Configure e-dmac registers */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900379 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
380 (EMDR_DESC | EDMR_EL), EDMR);
381
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000382 sh_eth_write(eth, 0, EESIPR);
383 sh_eth_write(eth, 0, TRSCER);
384 sh_eth_write(eth, 0, TFTR);
385 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
386 sh_eth_write(eth, RMCR_RST, RMCR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900387#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000388 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900389#endif
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000390 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900391
392 /* Configure e-mac registers */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000393 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900394
395 /* Set Mac address */
Mike Frysingerc527ce92009-02-11 19:14:09 -0500396 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
397 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000398 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900399
Mike Frysingerc527ce92009-02-11 19:14:09 -0500400 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000401 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900402
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000403 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000404#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000405 sh_eth_write(eth, 0, PIPR);
Nobuhiro Iwamatsu62cbddc2014-01-23 07:52:18 +0900406#endif
407#if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000408 sh_eth_write(eth, APR_AP, APR);
409 sh_eth_write(eth, MPR_MP, MPR);
410 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900411#endif
412
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000413#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000414 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsu47ce8892013-09-24 15:38:33 +0900415#elif defined(CONFIG_R8A7790) || defined(CONFIG_R8A7791)
Nobuhiro Iwamatsu87076782013-08-22 13:22:04 +0900416 sh_eth_write(eth, sh_eth_read(eth, RMIIMR) | 0x1, RMIIMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000417#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900418 /* Configure phy */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900419 ret = sh_eth_phy_config(eth);
420 if (ret) {
Nobuhiro Iwamatsu88a4c2e2009-06-25 16:33:04 +0900421 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900422 goto err_phy_cfg;
423 }
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900424 phy = port_info->phydev;
Timur Tabi11af8d62012-07-09 08:52:43 +0000425 ret = phy_startup(phy);
426 if (ret) {
427 printf(SHETHER_NAME ": phy startup failure\n");
428 return ret;
429 }
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900430
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900431 val = 0;
432
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900433 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900434 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900435 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000436#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000437 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000438#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000439 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu47ce8892013-09-24 15:38:33 +0900440#elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_R8A7790) || \
441 defined(CONFIG_R8A7791)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900442 val = ECMR_RTM;
443#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900444 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900445 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000446#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000447 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000448#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000449 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900450#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900451 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000452#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000453 else if (phy->speed == 1000) {
454 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000455 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000456 }
457#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900458
459 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900460 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900461 printf("Full\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000462 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
463 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900464 } else {
465 printf("Half\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000466 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900467 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900468
469 return ret;
470
471err_phy_cfg:
472 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900473}
474
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900475static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900476{
477 /*
478 * Enable the e-dmac receiver only. The transmitter will be enabled when
479 * we have something to transmit
480 */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000481 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900482}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900483
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900484static void sh_eth_stop(struct sh_eth_dev *eth)
485{
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000486 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900487}
488
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900489int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900490{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900491 int ret = 0;
492 struct sh_eth_dev *eth = dev->priv;
493
494 ret = sh_eth_reset(eth);
495 if (ret)
496 goto err;
497
498 ret = sh_eth_desc_init(eth);
499 if (ret)
500 goto err;
501
502 ret = sh_eth_config(eth, bd);
503 if (ret)
504 goto err_config;
505
506 sh_eth_start(eth);
507
508 return ret;
509
510err_config:
511 sh_eth_tx_desc_free(eth);
512 sh_eth_rx_desc_free(eth);
513
514err:
515 return ret;
516}
517
518void sh_eth_halt(struct eth_device *dev)
519{
520 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900521 sh_eth_stop(eth);
522}
523
524int sh_eth_initialize(bd_t *bd)
525{
526 int ret = 0;
527 struct sh_eth_dev *eth = NULL;
528 struct eth_device *dev = NULL;
529
530 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
531 if (!eth) {
532 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
533 ret = -ENOMEM;
534 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900535 }
536
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900537 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
538 if (!dev) {
539 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
540 ret = -ENOMEM;
541 goto err;
542 }
543 memset(dev, 0, sizeof(struct eth_device));
544 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900545
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900546 eth->port = CONFIG_SH_ETHER_USE_PORT;
547 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
548
549 dev->priv = (void *)eth;
550 dev->iobase = 0;
551 dev->init = sh_eth_init;
552 dev->halt = sh_eth_halt;
553 dev->send = sh_eth_send;
554 dev->recv = sh_eth_recv;
555 eth->port_info[eth->port].dev = dev;
556
557 sprintf(dev->name, SHETHER_NAME);
558
559 /* Register Device to EtherNet subsystem */
560 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900561
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900562 bb_miiphy_buses[0].priv = eth;
563 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
564
Mike Frysingerc527ce92009-02-11 19:14:09 -0500565 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
566 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900567
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900568 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900569
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900570err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900571 if (dev)
572 free(dev);
573
574 if (eth)
575 free(eth);
576
577 printf(SHETHER_NAME ": Failed\n");
578 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900579}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900580
581/******* for bb_miiphy *******/
582static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
583{
584 return 0;
585}
586
587static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
588{
589 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900590
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000591 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900592
593 return 0;
594}
595
596static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
597{
598 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900599
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000600 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900601
602 return 0;
603}
604
605static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
606{
607 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900608
609 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000610 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900611 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000612 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900613
614 return 0;
615}
616
617static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
618{
619 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900620
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000621 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900622
623 return 0;
624}
625
626static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
627{
628 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900629
630 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000631 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900632 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000633 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900634
635 return 0;
636}
637
638static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
639{
640 udelay(10);
641
642 return 0;
643}
644
645struct bb_miiphy_bus bb_miiphy_buses[] = {
646 {
647 .name = "sh_eth",
648 .init = sh_eth_bb_init,
649 .mdio_active = sh_eth_bb_mdio_active,
650 .mdio_tristate = sh_eth_bb_mdio_tristate,
651 .set_mdio = sh_eth_bb_set_mdio,
652 .get_mdio = sh_eth_bb_get_mdio,
653 .set_mdc = sh_eth_bb_set_mdc,
654 .delay = sh_eth_bb_delay,
655 }
656};
657int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);