blob: d5a83e0bf5b32b3dc89336cffd49a434839960ed [file] [log] [blame]
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09001/*
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +00002 * sh_eth.c - Driver for Renesas ethernet controler.
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09003 *
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +09004 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
5 * Copyright (c) 2008, 2011 Nobuhiro Iwamatsu
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09006 * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
7 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09009 */
10
11#include <config.h>
12#include <common.h>
13#include <malloc.h>
14#include <net.h>
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090015#include <netdev.h>
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +090016#include <miiphy.h>
Nobuhiro Iwamatsu240b7232008-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
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090028#ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
29#define flush_cache_wback(addr, len) \
30 dcache_wback_range((u32)addr, (u32)(addr + len - 1))
31#else
32#define flush_cache_wback(...)
33#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090034
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090035#define TIMEOUT_CNT 1000
36
Joe Hershbergere4e04882012-05-22 18:36:19 +000037int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090038{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090039 struct sh_eth_dev *eth = dev->priv;
40 int port = eth->port, ret = 0, timeout;
41 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090042
43 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090044 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
45 ret = -EINVAL;
46 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090047 }
48
49 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +000050 if ((int)packet & 3) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090051 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
52 ret = -EFAULT;
53 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090054 }
55
56 /* Update tx descriptor */
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090057 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090058 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
59 port_info->tx_desc_cur->td1 = len << 16;
60 /* Must preserve the end of descriptor list indication */
61 if (port_info->tx_desc_cur->td0 & TD_TDLE)
62 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
63 else
64 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
65
66 /* Restart the transmitter if disabled */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +000067 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
68 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090069
70 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090071 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090072 while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
73 udelay(100);
74
75 if (timeout < 0) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090076 printf(SHETHER_NAME ": transmit timeout\n");
77 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090078 goto err;
79 }
80
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090081 port_info->tx_desc_cur++;
82 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
83 port_info->tx_desc_cur = port_info->tx_desc_base;
84
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090085err:
86 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090087}
88
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090089int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090090{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090091 struct sh_eth_dev *eth = dev->priv;
92 int port = eth->port, len = 0;
93 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershbergere4e04882012-05-22 18:36:19 +000094 uchar *packet;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090095
96 /* Check if the rx descriptor is ready */
97 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
98 /* Check for errors */
99 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
100 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershbergere4e04882012-05-22 18:36:19 +0000101 packet = (uchar *)
102 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900103 NetReceive(packet, len);
104 }
105
106 /* Make current descriptor available again */
107 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
108 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
109 else
110 port_info->rx_desc_cur->rd0 = RD_RACT;
111
112 /* Point to the next descriptor */
113 port_info->rx_desc_cur++;
114 if (port_info->rx_desc_cur >=
115 port_info->rx_desc_base + NUM_RX_DESC)
116 port_info->rx_desc_cur = port_info->rx_desc_base;
117 }
118
119 /* Restart the receiver if disabled */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000120 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
121 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900122
123 return len;
124}
125
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900126static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900127{
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000128#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900129 int ret = 0, i;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900130
131 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000132 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900133
134 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000135 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900136 for (i = 0; i < TIMEOUT_CNT ; i++) {
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000137 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900138 break;
139 udelay(1000);
140 }
141
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900142 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900143 printf(SHETHER_NAME ": Software reset timeout\n");
144 ret = -EIO;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900145 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900146
147 return ret;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900148#else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000149 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900150 udelay(3000);
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000151 sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900152
153 return 0;
154#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900155}
156
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900157static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900158{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900159 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900160 u32 tmp_addr;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900161 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900162 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900163
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900164 /*
165 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
166 */
167 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900168 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900169 TX_DESC_SIZE - 1);
170 if (!port_info->tx_desc_malloc) {
171 printf(SHETHER_NAME ": malloc failed\n");
172 ret = -ENOMEM;
173 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900174 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900175
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900176 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
177 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +0900178 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900179 /* Make sure we use a P2 address (non-cacheable) */
180 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900181 port_info->tx_desc_cur = port_info->tx_desc_base;
182
183 /* Initialize all descriptors */
184 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
185 cur_tx_desc++, i++) {
186 cur_tx_desc->td0 = 0x00;
187 cur_tx_desc->td1 = 0x00;
188 cur_tx_desc->td2 = 0x00;
189 }
190
191 /* Mark the end of the descriptors */
192 cur_tx_desc--;
193 cur_tx_desc->td0 |= TD_TDLE;
194
195 /* Point the controller to the tx descriptor list. Must use physical
196 addresses */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000197 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000198#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000199 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
200 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
201 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900202#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900203
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900204err:
205 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900206}
207
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900208static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900209{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900210 int port = eth->port, i , ret = 0;
211 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900212 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900213 u32 tmp_addr;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900214 u8 *rx_buf;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900215
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900216 /*
217 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
218 */
219 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900220 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900221 RX_DESC_SIZE - 1);
222 if (!port_info->rx_desc_malloc) {
223 printf(SHETHER_NAME ": malloc failed\n");
224 ret = -ENOMEM;
225 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900226 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900227
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900228 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
229 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +0900230 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900231 /* Make sure we use a P2 address (non-cacheable) */
232 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
233
234 port_info->rx_desc_cur = port_info->rx_desc_base;
235
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900236 /*
237 * Allocate rx data buffers. They must be 32 bytes aligned and in
238 * P2 area
239 */
240 port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
241 if (!port_info->rx_buf_malloc) {
242 printf(SHETHER_NAME ": malloc failed\n");
243 ret = -ENOMEM;
244 goto err_buf_malloc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900245 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900246
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900247 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
248 ~(32 - 1));
249 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
250
251 /* Initialize all descriptors */
252 for (cur_rx_desc = port_info->rx_desc_base,
253 rx_buf = port_info->rx_buf_base, i = 0;
254 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
255 cur_rx_desc->rd0 = RD_RACT;
256 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
257 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
258 }
259
260 /* Mark the end of the descriptors */
261 cur_rx_desc--;
262 cur_rx_desc->rd0 |= RD_RDLE;
263
264 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000265 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000266#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000267 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
268 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
269 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900270#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900271
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900272 return ret;
273
274err_buf_malloc:
275 free(port_info->rx_desc_malloc);
276 port_info->rx_desc_malloc = NULL;
277
278err:
279 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900280}
281
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900282static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900283{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900284 int port = eth->port;
285 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900286
287 if (port_info->tx_desc_malloc) {
288 free(port_info->tx_desc_malloc);
289 port_info->tx_desc_malloc = NULL;
290 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900291}
292
293static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
294{
295 int port = eth->port;
296 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900297
298 if (port_info->rx_desc_malloc) {
299 free(port_info->rx_desc_malloc);
300 port_info->rx_desc_malloc = NULL;
301 }
302
303 if (port_info->rx_buf_malloc) {
304 free(port_info->rx_buf_malloc);
305 port_info->rx_buf_malloc = NULL;
306 }
307}
308
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900309static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900310{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900311 int ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900312
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900313 ret = sh_eth_tx_desc_init(eth);
314 if (ret)
315 goto err_tx_init;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900316
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900317 ret = sh_eth_rx_desc_init(eth);
318 if (ret)
319 goto err_rx_init;
320
321 return ret;
322err_rx_init:
323 sh_eth_tx_desc_free(eth);
324
325err_tx_init:
326 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900327}
328
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900329static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900330{
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900331 int port = eth->port, ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900332 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900333 struct eth_device *dev = port_info->dev;
334 struct phy_device *phydev;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900335
Nobuhiro Iwamatsu58802902012-02-02 21:28:49 +0000336 phydev = phy_connect(
337 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000338 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900339 port_info->phydev = phydev;
340 phy_config(phydev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900341
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900342 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900343}
344
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900345static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900346{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900347 int port = eth->port, ret = 0;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900348 u32 val;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900349 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingera86bf132009-02-11 19:14:09 -0500350 struct eth_device *dev = port_info->dev;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900351 struct phy_device *phy;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900352
353 /* Configure e-dmac registers */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000354 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) | EDMR_EL,
355 EDMR);
356 sh_eth_write(eth, 0, EESIPR);
357 sh_eth_write(eth, 0, TRSCER);
358 sh_eth_write(eth, 0, TFTR);
359 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
360 sh_eth_write(eth, RMCR_RST, RMCR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000361#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000362 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900363#endif
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000364 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900365
366 /* Configure e-mac registers */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000367 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900368
369 /* Set Mac address */
Mike Frysingera86bf132009-02-11 19:14:09 -0500370 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
371 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000372 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900373
Mike Frysingera86bf132009-02-11 19:14:09 -0500374 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000375 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900376
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000377 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000378#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000379 sh_eth_write(eth, 0, PIPR);
380 sh_eth_write(eth, APR_AP, APR);
381 sh_eth_write(eth, MPR_MP, MPR);
382 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900383#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900384
Nobuhiro Iwamatsu4ad2c2a2012-08-02 22:08:40 +0000385#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000386 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000387#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900388 /* Configure phy */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900389 ret = sh_eth_phy_config(eth);
390 if (ret) {
Nobuhiro Iwamatsufc4b0a22009-06-25 16:33:04 +0900391 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900392 goto err_phy_cfg;
393 }
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900394 phy = port_info->phydev;
Timur Tabi42387462012-07-09 08:52:43 +0000395 ret = phy_startup(phy);
396 if (ret) {
397 printf(SHETHER_NAME ": phy startup failure\n");
398 return ret;
399 }
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900400
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900401 val = 0;
402
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900403 /* Set the transfer speed */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900404 if (phy->speed == 100) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900405 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000406#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000407 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000408#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000409 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900410#elif defined(CONFIG_CPU_SH7724)
411 val = ECMR_RTM;
412#endif
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900413 } else if (phy->speed == 10) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900414 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000415#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000416 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodad27e8c92012-11-04 15:54:30 +0000417#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000418 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900419#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900420 }
Yoshihiro Shimoda9d553032012-06-26 16:38:06 +0000421#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000422 else if (phy->speed == 1000) {
423 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000424 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu475f40d2012-05-15 15:49:39 +0000425 }
426#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900427
428 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900429 if (phy->duplex) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900430 printf("Full\n");
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000431 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
432 ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900433 } else {
434 printf("Half\n");
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000435 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900436 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900437
438 return ret;
439
440err_phy_cfg:
441 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900442}
443
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900444static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900445{
446 /*
447 * Enable the e-dmac receiver only. The transmitter will be enabled when
448 * we have something to transmit
449 */
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000450 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900451}
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900452
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900453static void sh_eth_stop(struct sh_eth_dev *eth)
454{
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000455 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900456}
457
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900458int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900459{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900460 int ret = 0;
461 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900462
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900463 ret = sh_eth_reset(eth);
464 if (ret)
465 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900466
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900467 ret = sh_eth_desc_init(eth);
468 if (ret)
469 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900470
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900471 ret = sh_eth_config(eth, bd);
472 if (ret)
473 goto err_config;
474
475 sh_eth_start(eth);
476
477 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900478
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900479err_config:
480 sh_eth_tx_desc_free(eth);
481 sh_eth_rx_desc_free(eth);
482
483err:
484 return ret;
485}
486
487void sh_eth_halt(struct eth_device *dev)
488{
489 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900490 sh_eth_stop(eth);
491}
492
493int sh_eth_initialize(bd_t *bd)
494{
495 int ret = 0;
496 struct sh_eth_dev *eth = NULL;
497 struct eth_device *dev = NULL;
498
499 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
500 if (!eth) {
501 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
502 ret = -ENOMEM;
503 goto err;
504 }
505
506 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
507 if (!dev) {
508 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
509 ret = -ENOMEM;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900510 goto err;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900511 }
512 memset(dev, 0, sizeof(struct eth_device));
513 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900514
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900515 eth->port = CONFIG_SH_ETHER_USE_PORT;
516 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900517
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900518 dev->priv = (void *)eth;
519 dev->iobase = 0;
520 dev->init = sh_eth_init;
521 dev->halt = sh_eth_halt;
522 dev->send = sh_eth_send;
523 dev->recv = sh_eth_recv;
524 eth->port_info[eth->port].dev = dev;
525
526 sprintf(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900527
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900528 /* Register Device to EtherNet subsystem */
529 eth_register(dev);
530
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900531 bb_miiphy_buses[0].priv = eth;
532 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
533
Mike Frysingera86bf132009-02-11 19:14:09 -0500534 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
535 puts("Please set MAC address\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900536
537 return ret;
538
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900539err:
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900540 if (dev)
541 free(dev);
542
543 if (eth)
544 free(eth);
545
546 printf(SHETHER_NAME ": Failed\n");
547 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900548}
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900549
550/******* for bb_miiphy *******/
551static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
552{
553 return 0;
554}
555
556static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
557{
558 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900559
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000560 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900561
562 return 0;
563}
564
565static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
566{
567 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900568
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000569 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900570
571 return 0;
572}
573
574static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
575{
576 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900577
578 if (v)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000579 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900580 else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000581 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900582
583 return 0;
584}
585
586static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
587{
588 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900589
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000590 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900591
592 return 0;
593}
594
595static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
596{
597 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900598
599 if (v)
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000600 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900601 else
Yoshihiro Shimoda4c4aa6c2012-06-26 16:38:09 +0000602 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900603
604 return 0;
605}
606
607static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
608{
609 udelay(10);
610
611 return 0;
612}
613
614struct bb_miiphy_bus bb_miiphy_buses[] = {
615 {
616 .name = "sh_eth",
617 .init = sh_eth_bb_init,
618 .mdio_active = sh_eth_bb_mdio_active,
619 .mdio_tristate = sh_eth_bb_mdio_tristate,
620 .set_mdio = sh_eth_bb_set_mdio,
621 .get_mdio = sh_eth_bb_get_mdio,
622 .set_mdc = sh_eth_bb_set_mdc,
623 .delay = sh_eth_bb_delay,
624 }
625};
626int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);