blob: 6a78df021944bbca878e4bd1146f20f602941492 [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>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09009 */
10
11#include <config.h>
12#include <common.h>
13#include <malloc.h>
14#include <net.h>
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090015#include <netdev.h>
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +090016#include <miiphy.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090017#include <asm/errno.h>
18#include <asm/io.h>
19
20#include "sh_eth.h"
21
22#ifndef CONFIG_SH_ETHER_USE_PORT
23# error "Please define CONFIG_SH_ETHER_USE_PORT"
24#endif
25#ifndef CONFIG_SH_ETHER_PHY_ADDR
26# error "Please define CONFIG_SH_ETHER_PHY_ADDR"
27#endif
Nobuhiro Iwamatsu870cc232013-08-22 13:22:01 +090028
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090029#if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF)
30#define flush_cache_wback(addr, len) \
31 flush_dcache_range((u32)addr, (u32)(addr + len - 1))
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090032#else
33#define flush_cache_wback(...)
34#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090035
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090036#if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
37#define invalidate_cache(addr, len) \
38 { \
39 u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
40 u32 start, end; \
41 \
42 start = (u32)addr; \
43 end = start + len; \
44 start &= ~(line_size - 1); \
45 end = ((end + line_size - 1) & ~(line_size - 1)); \
46 \
47 invalidate_dcache_range(start, end); \
48 }
49#else
50#define invalidate_cache(...)
51#endif
52
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090053#define TIMEOUT_CNT 1000
54
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000055int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090056{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090057 struct sh_eth_dev *eth = dev->priv;
58 int port = eth->port, ret = 0, timeout;
59 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090060
61 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090062 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
63 ret = -EINVAL;
64 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090065 }
66
67 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +000068 if ((int)packet & 3) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090069 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
70 ret = -EFAULT;
71 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090072 }
73
74 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090075 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090076 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
77 port_info->tx_desc_cur->td1 = len << 16;
78 /* Must preserve the end of descriptor list indication */
79 if (port_info->tx_desc_cur->td0 & TD_TDLE)
80 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
81 else
82 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
83
84 /* Restart the transmitter if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +000085 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
86 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090087
88 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090089 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090090 do {
91 invalidate_cache(port_info->tx_desc_cur,
92 sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090093 udelay(100);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +090094 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090095
96 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090097 printf(SHETHER_NAME ": transmit timeout\n");
98 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090099 goto err;
100 }
101
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900102 port_info->tx_desc_cur++;
103 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
104 port_info->tx_desc_cur = port_info->tx_desc_base;
105
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900106err:
107 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900108}
109
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900110int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900111{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900112 struct sh_eth_dev *eth = dev->priv;
113 int port = eth->port, len = 0;
114 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000115 uchar *packet;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900116
117 /* Check if the rx descriptor is ready */
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900118 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900119 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
120 /* Check for errors */
121 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
122 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000123 packet = (uchar *)
124 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu92f07132013-08-22 13:22:03 +0900125 invalidate_cache(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900126 NetReceive(packet, len);
127 }
128
129 /* Make current descriptor available again */
130 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
131 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
132 else
133 port_info->rx_desc_cur->rd0 = RD_RACT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900134 /* Point to the next descriptor */
135 port_info->rx_desc_cur++;
136 if (port_info->rx_desc_cur >=
137 port_info->rx_desc_base + NUM_RX_DESC)
138 port_info->rx_desc_cur = port_info->rx_desc_base;
139 }
140
141 /* Restart the receiver if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000142 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
143 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900144
145 return len;
146}
147
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900148static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900149{
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000150#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900151 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900152
153 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000154 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900155
156 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000157 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900158 for (i = 0; i < TIMEOUT_CNT ; i++) {
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000159 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900160 break;
161 udelay(1000);
162 }
163
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900164 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900165 printf(SHETHER_NAME ": Software reset timeout\n");
166 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900167 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900168
169 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900170#else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000171 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900172 udelay(3000);
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
175 return 0;
176#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900177}
178
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900179static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900180{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900181 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900182 u32 tmp_addr;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900183 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900184 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900185
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900186 /*
187 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
188 */
189 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900190 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900191 TX_DESC_SIZE - 1);
192 if (!port_info->tx_desc_malloc) {
193 printf(SHETHER_NAME ": malloc failed\n");
194 ret = -ENOMEM;
195 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900196 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900197
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900198 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
199 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900200 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900201 /* Make sure we use a P2 address (non-cacheable) */
202 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900203 port_info->tx_desc_cur = port_info->tx_desc_base;
204
205 /* Initialize all descriptors */
206 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
207 cur_tx_desc++, i++) {
208 cur_tx_desc->td0 = 0x00;
209 cur_tx_desc->td1 = 0x00;
210 cur_tx_desc->td2 = 0x00;
211 }
212
213 /* Mark the end of the descriptors */
214 cur_tx_desc--;
215 cur_tx_desc->td0 |= TD_TDLE;
216
217 /* Point the controller to the tx descriptor list. Must use physical
218 addresses */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000219 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000220#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000221 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
222 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
223 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900224#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900225
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900226err:
227 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900228}
229
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900230static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900231{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900232 int port = eth->port, i , ret = 0;
233 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900234 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900235 u32 tmp_addr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900236 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900237
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900238 /*
239 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
240 */
241 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900242 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900243 RX_DESC_SIZE - 1);
244 if (!port_info->rx_desc_malloc) {
245 printf(SHETHER_NAME ": malloc failed\n");
246 ret = -ENOMEM;
247 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900248 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900249
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900250 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
251 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900252 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900253 /* Make sure we use a P2 address (non-cacheable) */
254 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
255
256 port_info->rx_desc_cur = port_info->rx_desc_base;
257
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900258 /*
259 * Allocate rx data buffers. They must be 32 bytes aligned and in
260 * P2 area
261 */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900262 port_info->rx_buf_malloc = malloc(
263 NUM_RX_DESC * MAX_BUF_SIZE + RX_BUF_ALIGNE_SIZE - 1);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900264 if (!port_info->rx_buf_malloc) {
265 printf(SHETHER_NAME ": malloc failed\n");
266 ret = -ENOMEM;
267 goto err_buf_malloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900268 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900269
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900270 tmp_addr = (u32)(((int)port_info->rx_buf_malloc
271 + (RX_BUF_ALIGNE_SIZE - 1)) &
272 ~(RX_BUF_ALIGNE_SIZE - 1));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900273 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
274
275 /* Initialize all descriptors */
276 for (cur_rx_desc = port_info->rx_desc_base,
277 rx_buf = port_info->rx_buf_base, i = 0;
278 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
279 cur_rx_desc->rd0 = RD_RACT;
280 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
281 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
282 }
283
284 /* Mark the end of the descriptors */
285 cur_rx_desc--;
286 cur_rx_desc->rd0 |= RD_RDLE;
287
288 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000289 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000290#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000291 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
292 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
293 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900294#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900295
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900296 return ret;
297
298err_buf_malloc:
299 free(port_info->rx_desc_malloc);
300 port_info->rx_desc_malloc = NULL;
301
302err:
303 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900304}
305
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900306static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900307{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900308 int port = eth->port;
309 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900310
311 if (port_info->tx_desc_malloc) {
312 free(port_info->tx_desc_malloc);
313 port_info->tx_desc_malloc = NULL;
314 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900315}
316
317static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
318{
319 int port = eth->port;
320 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900321
322 if (port_info->rx_desc_malloc) {
323 free(port_info->rx_desc_malloc);
324 port_info->rx_desc_malloc = NULL;
325 }
326
327 if (port_info->rx_buf_malloc) {
328 free(port_info->rx_buf_malloc);
329 port_info->rx_buf_malloc = NULL;
330 }
331}
332
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900333static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900334{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900335 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900336
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900337 ret = sh_eth_tx_desc_init(eth);
338 if (ret)
339 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900340
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900341 ret = sh_eth_rx_desc_init(eth);
342 if (ret)
343 goto err_rx_init;
344
345 return ret;
346err_rx_init:
347 sh_eth_tx_desc_free(eth);
348
349err_tx_init:
350 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900351}
352
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900353static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900354{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900355 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900356 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900357 struct eth_device *dev = port_info->dev;
358 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900359
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000360 phydev = phy_connect(
361 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000362 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900363 port_info->phydev = phydev;
364 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900365
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900366 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900367}
368
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900369static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900370{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900371 int port = eth->port, ret = 0;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900372 u32 val;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900373 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingerc527ce92009-02-11 19:14:09 -0500374 struct eth_device *dev = port_info->dev;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900375 struct phy_device *phy;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900376
377 /* Configure e-dmac registers */
Nobuhiro Iwamatsuf8b75072013-08-22 13:22:02 +0900378 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) |
379 (EMDR_DESC | EDMR_EL), EDMR);
380
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000381 sh_eth_write(eth, 0, EESIPR);
382 sh_eth_write(eth, 0, TRSCER);
383 sh_eth_write(eth, 0, TFTR);
384 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
385 sh_eth_write(eth, RMCR_RST, RMCR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000386#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000387 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900388#endif
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000389 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900390
391 /* Configure e-mac registers */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000392 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900393
394 /* Set Mac address */
Mike Frysingerc527ce92009-02-11 19:14:09 -0500395 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
396 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000397 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900398
Mike Frysingerc527ce92009-02-11 19:14:09 -0500399 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000400 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900401
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000402 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000403#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000404 sh_eth_write(eth, 0, PIPR);
405 sh_eth_write(eth, APR_AP, APR);
406 sh_eth_write(eth, MPR_MP, MPR);
407 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900408#endif
409
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000410#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000411 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000412#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900413 /* Configure phy */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900414 ret = sh_eth_phy_config(eth);
415 if (ret) {
Nobuhiro Iwamatsu88a4c2e2009-06-25 16:33:04 +0900416 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900417 goto err_phy_cfg;
418 }
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900419 phy = port_info->phydev;
Timur Tabi11af8d62012-07-09 08:52:43 +0000420 ret = phy_startup(phy);
421 if (ret) {
422 printf(SHETHER_NAME ": phy startup failure\n");
423 return ret;
424 }
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900425
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900426 val = 0;
427
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900428 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900429 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900430 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000431#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000432 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000433#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000434 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900435#elif defined(CONFIG_CPU_SH7724)
436 val = ECMR_RTM;
437#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900438 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900439 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000440#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000441 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000442#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000443 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900444#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900445 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000446#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000447 else if (phy->speed == 1000) {
448 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000449 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000450 }
451#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900452
453 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900454 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900455 printf("Full\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000456 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
457 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900458 } else {
459 printf("Half\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000460 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900461 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900462
463 return ret;
464
465err_phy_cfg:
466 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900467}
468
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900469static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900470{
471 /*
472 * Enable the e-dmac receiver only. The transmitter will be enabled when
473 * we have something to transmit
474 */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000475 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900476}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900477
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900478static void sh_eth_stop(struct sh_eth_dev *eth)
479{
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000480 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900481}
482
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900483int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900484{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900485 int ret = 0;
486 struct sh_eth_dev *eth = dev->priv;
487
488 ret = sh_eth_reset(eth);
489 if (ret)
490 goto err;
491
492 ret = sh_eth_desc_init(eth);
493 if (ret)
494 goto err;
495
496 ret = sh_eth_config(eth, bd);
497 if (ret)
498 goto err_config;
499
500 sh_eth_start(eth);
501
502 return ret;
503
504err_config:
505 sh_eth_tx_desc_free(eth);
506 sh_eth_rx_desc_free(eth);
507
508err:
509 return ret;
510}
511
512void sh_eth_halt(struct eth_device *dev)
513{
514 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900515 sh_eth_stop(eth);
516}
517
518int sh_eth_initialize(bd_t *bd)
519{
520 int ret = 0;
521 struct sh_eth_dev *eth = NULL;
522 struct eth_device *dev = NULL;
523
524 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
525 if (!eth) {
526 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
527 ret = -ENOMEM;
528 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900529 }
530
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900531 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
532 if (!dev) {
533 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
534 ret = -ENOMEM;
535 goto err;
536 }
537 memset(dev, 0, sizeof(struct eth_device));
538 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900539
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900540 eth->port = CONFIG_SH_ETHER_USE_PORT;
541 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
542
543 dev->priv = (void *)eth;
544 dev->iobase = 0;
545 dev->init = sh_eth_init;
546 dev->halt = sh_eth_halt;
547 dev->send = sh_eth_send;
548 dev->recv = sh_eth_recv;
549 eth->port_info[eth->port].dev = dev;
550
551 sprintf(dev->name, SHETHER_NAME);
552
553 /* Register Device to EtherNet subsystem */
554 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900555
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900556 bb_miiphy_buses[0].priv = eth;
557 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
558
Mike Frysingerc527ce92009-02-11 19:14:09 -0500559 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
560 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900561
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900562 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900563
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900564err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900565 if (dev)
566 free(dev);
567
568 if (eth)
569 free(eth);
570
571 printf(SHETHER_NAME ": Failed\n");
572 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900573}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900574
575/******* for bb_miiphy *******/
576static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
577{
578 return 0;
579}
580
581static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
582{
583 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900584
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000585 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900586
587 return 0;
588}
589
590static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
591{
592 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900593
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000594 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900595
596 return 0;
597}
598
599static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
600{
601 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900602
603 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000604 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900605 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000606 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900607
608 return 0;
609}
610
611static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
612{
613 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900614
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000615 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900616
617 return 0;
618}
619
620static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
621{
622 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900623
624 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000625 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900626 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000627 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900628
629 return 0;
630}
631
632static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
633{
634 udelay(10);
635
636 return 0;
637}
638
639struct bb_miiphy_bus bb_miiphy_buses[] = {
640 {
641 .name = "sh_eth",
642 .init = sh_eth_bb_init,
643 .mdio_active = sh_eth_bb_mdio_active,
644 .mdio_tristate = sh_eth_bb_mdio_tristate,
645 .set_mdio = sh_eth_bb_set_mdio,
646 .get_mdio = sh_eth_bb_get_mdio,
647 .set_mdc = sh_eth_bb_set_mdc,
648 .delay = sh_eth_bb_delay,
649 }
650};
651int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);