blob: bb57e4d53a03a2c82f326479bdd6893d475456a3 [file] [log] [blame]
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +09001/*
2 * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
3 *
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <config.h>
24#include <common.h>
25#include <malloc.h>
26#include <net.h>
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090027#include <netdev.h>
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +090028#include <miiphy.h>
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090029#include <asm/errno.h>
30#include <asm/io.h>
31
32#include "sh_eth.h"
33
34#ifndef CONFIG_SH_ETHER_USE_PORT
35# error "Please define CONFIG_SH_ETHER_USE_PORT"
36#endif
37#ifndef CONFIG_SH_ETHER_PHY_ADDR
38# error "Please define CONFIG_SH_ETHER_PHY_ADDR"
39#endif
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090040#ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
41#define flush_cache_wback(addr, len) \
42 dcache_wback_range((u32)addr, (u32)(addr + len - 1))
43#else
44#define flush_cache_wback(...)
45#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090046
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090047#define TIMEOUT_CNT 1000
48
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000049int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090050{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090051 struct sh_eth_dev *eth = dev->priv;
52 int port = eth->port, ret = 0, timeout;
53 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090054
55 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090056 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
57 ret = -EINVAL;
58 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090059 }
60
61 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +000062 if ((int)packet & 3) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090063 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
64 ret = -EFAULT;
65 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090066 }
67
68 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090069 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090070 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
71 port_info->tx_desc_cur->td1 = len << 16;
72 /* Must preserve the end of descriptor list indication */
73 if (port_info->tx_desc_cur->td0 & TD_TDLE)
74 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
75 else
76 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
77
78 /* Restart the transmitter if disabled */
79 if (!(inl(EDTRR(port)) & EDTRR_TRNS))
80 outl(EDTRR_TRNS, EDTRR(port));
81
82 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090083 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090084 while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
85 udelay(100);
86
87 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090088 printf(SHETHER_NAME ": transmit timeout\n");
89 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090090 goto err;
91 }
92
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090093 port_info->tx_desc_cur++;
94 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
95 port_info->tx_desc_cur = port_info->tx_desc_base;
96
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090097err:
98 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090099}
100
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900101int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900102{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900103 struct sh_eth_dev *eth = dev->priv;
104 int port = eth->port, len = 0;
105 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000106 uchar *packet;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900107
108 /* Check if the rx descriptor is ready */
109 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
110 /* Check for errors */
111 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
112 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000113 packet = (uchar *)
114 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900115 NetReceive(packet, len);
116 }
117
118 /* Make current descriptor available again */
119 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
120 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
121 else
122 port_info->rx_desc_cur->rd0 = RD_RACT;
123
124 /* Point to the next descriptor */
125 port_info->rx_desc_cur++;
126 if (port_info->rx_desc_cur >=
127 port_info->rx_desc_base + NUM_RX_DESC)
128 port_info->rx_desc_cur = port_info->rx_desc_base;
129 }
130
131 /* Restart the receiver if disabled */
132 if (!(inl(EDRRR(port)) & EDRRR_R))
133 outl(EDRRR_R, EDRRR(port));
134
135 return len;
136}
137
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900138static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900139{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900140 int port = eth->port;
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000141#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900142 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900143
144 /* Start e-dmac transmitter and receiver */
145 outl(EDSR_ENALL, EDSR(port));
146
147 /* Perform a software reset and wait for it to complete */
148 outl(EDMR_SRST, EDMR(port));
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900149 for (i = 0; i < TIMEOUT_CNT ; i++) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900150 if (!(inl(EDMR(port)) & EDMR_SRST))
151 break;
152 udelay(1000);
153 }
154
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900155 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900156 printf(SHETHER_NAME ": Software reset timeout\n");
157 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900158 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900159
160 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900161#else
162 outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
163 udelay(3000);
164 outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
165
166 return 0;
167#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900168}
169
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900170static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900171{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900172 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900173 u32 tmp_addr;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900174 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900175 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900176
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900177 /*
178 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
179 */
180 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900181 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900182 TX_DESC_SIZE - 1);
183 if (!port_info->tx_desc_malloc) {
184 printf(SHETHER_NAME ": malloc failed\n");
185 ret = -ENOMEM;
186 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900187 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900188
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900189 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
190 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900191 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900192 /* Make sure we use a P2 address (non-cacheable) */
193 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900194 port_info->tx_desc_cur = port_info->tx_desc_base;
195
196 /* Initialize all descriptors */
197 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
198 cur_tx_desc++, i++) {
199 cur_tx_desc->td0 = 0x00;
200 cur_tx_desc->td1 = 0x00;
201 cur_tx_desc->td2 = 0x00;
202 }
203
204 /* Mark the end of the descriptors */
205 cur_tx_desc--;
206 cur_tx_desc->td0 |= TD_TDLE;
207
208 /* Point the controller to the tx descriptor list. Must use physical
209 addresses */
210 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000211#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900212 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
213 outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
214 outl(0x01, TDFFR(port));/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900215#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900216
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900217err:
218 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900219}
220
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900221static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900222{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900223 int port = eth->port, i , ret = 0;
224 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900225 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900226 u32 tmp_addr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900227 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900228
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900229 /*
230 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
231 */
232 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900233 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900234 RX_DESC_SIZE - 1);
235 if (!port_info->rx_desc_malloc) {
236 printf(SHETHER_NAME ": malloc failed\n");
237 ret = -ENOMEM;
238 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900239 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900240
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900241 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
242 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900243 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900244 /* Make sure we use a P2 address (non-cacheable) */
245 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
246
247 port_info->rx_desc_cur = port_info->rx_desc_base;
248
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900249 /*
250 * Allocate rx data buffers. They must be 32 bytes aligned and in
251 * P2 area
252 */
253 port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
254 if (!port_info->rx_buf_malloc) {
255 printf(SHETHER_NAME ": malloc failed\n");
256 ret = -ENOMEM;
257 goto err_buf_malloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900258 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900259
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900260 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
261 ~(32 - 1));
262 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
263
264 /* Initialize all descriptors */
265 for (cur_rx_desc = port_info->rx_desc_base,
266 rx_buf = port_info->rx_buf_base, i = 0;
267 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
268 cur_rx_desc->rd0 = RD_RACT;
269 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
270 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
271 }
272
273 /* Mark the end of the descriptors */
274 cur_rx_desc--;
275 cur_rx_desc->rd0 |= RD_RDLE;
276
277 /* Point the controller to the rx descriptor list */
278 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000279#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900280 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
281 outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
282 outl(RDFFR_RDLF, RDFFR(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900283#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900284
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900285 return ret;
286
287err_buf_malloc:
288 free(port_info->rx_desc_malloc);
289 port_info->rx_desc_malloc = NULL;
290
291err:
292 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900293}
294
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900295static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900296{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900297 int port = eth->port;
298 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900299
300 if (port_info->tx_desc_malloc) {
301 free(port_info->tx_desc_malloc);
302 port_info->tx_desc_malloc = NULL;
303 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900304}
305
306static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
307{
308 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->rx_desc_malloc) {
312 free(port_info->rx_desc_malloc);
313 port_info->rx_desc_malloc = NULL;
314 }
315
316 if (port_info->rx_buf_malloc) {
317 free(port_info->rx_buf_malloc);
318 port_info->rx_buf_malloc = NULL;
319 }
320}
321
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900322static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900323{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900324 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900325
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900326 ret = sh_eth_tx_desc_init(eth);
327 if (ret)
328 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900329
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900330 ret = sh_eth_rx_desc_init(eth);
331 if (ret)
332 goto err_rx_init;
333
334 return ret;
335err_rx_init:
336 sh_eth_tx_desc_free(eth);
337
338err_tx_init:
339 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900340}
341
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900342static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900343{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900344 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900345 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900346 struct eth_device *dev = port_info->dev;
347 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900348
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000349 phydev = phy_connect(
350 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000351 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900352 port_info->phydev = phydev;
353 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900354
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900355 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900356}
357
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900358static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900359{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900360 int port = eth->port, ret = 0;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900361 u32 val;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900362 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingerc527ce92009-02-11 19:14:09 -0500363 struct eth_device *dev = port_info->dev;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900364 struct phy_device *phy;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900365
366 /* Configure e-dmac registers */
367 outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
368 outl(0, EESIPR(port));
369 outl(0, TRSCER(port));
370 outl(0, TFTR(port));
371 outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
372 outl(RMCR_RST, RMCR(port));
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900373#if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900374 outl(0, RPADIR(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900375#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900376 outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
377
378 /* Configure e-mac registers */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900379#if defined(CONFIG_CPU_SH7757)
380 outl(ECSIPR_BRCRXIP | ECSIPR_PSRTOIP | ECSIPR_LCHNGIP |
381 ECSIPR_MPDIP | ECSIPR_ICDIP, ECSIPR(port));
382#else
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900383 outl(0, ECSIPR(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900384#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900385
386 /* Set Mac address */
Mike Frysingerc527ce92009-02-11 19:14:09 -0500387 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
388 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900389 outl(val, MAHR(port));
390
Mike Frysingerc527ce92009-02-11 19:14:09 -0500391 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900392 outl(val, MALR(port));
393
394 outl(RFLR_RFL_MIN, RFLR(port));
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900395#if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900396 outl(0, PIPR(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900397#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900398#if !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900399 outl(APR_AP, APR(port));
400 outl(MPR_MP, MPR(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900401#endif
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000402#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900403 outl(TPAUSER_TPAUSE, TPAUSER(port));
404#elif defined(CONFIG_CPU_SH7757)
405 outl(TPAUSER_UNLIMITED, TPAUSER(port));
406#endif
407
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000408#if defined(CONFIG_CPU_SH7734)
409 outl(CONFIG_SH_ETHER_SH7734_MII, RMII_MII(port));
410#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900411 /* Configure phy */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900412 ret = sh_eth_phy_config(eth);
413 if (ret) {
Nobuhiro Iwamatsu88a4c2e2009-06-25 16:33:04 +0900414 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900415 goto err_phy_cfg;
416 }
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900417 phy = port_info->phydev;
418 phy_startup(phy);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900419
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900420 val = 0;
421
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900422 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900423 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900424 printf(SHETHER_NAME ": 100Base/");
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000425#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900426 outl(GECMR_100B, GECMR(port));
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900427#elif defined(CONFIG_CPU_SH7757)
428 outl(1, RTRATE(port));
429#elif defined(CONFIG_CPU_SH7724)
430 val = ECMR_RTM;
431#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900432 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900433 printf(SHETHER_NAME ": 10Base/");
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000434#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900435 outl(GECMR_10B, GECMR(port));
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900436#elif defined(CONFIG_CPU_SH7757)
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900437 outl(0, RTRATE(port));
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900438#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900439 }
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000440#if defined(CONFIG_CPU_SH7763) || defined(CONFIG_CPU_SH7734)
441 else if (phy->speed == 1000) {
442 printf(SHETHER_NAME ": 1000Base/");
443 outl(GECMR_1000B, GECMR(port));
444 }
445#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900446
447 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900448 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900449 printf("Full\n");
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900450 outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900451 } else {
452 printf("Half\n");
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900453 outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900454 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900455
456 return ret;
457
458err_phy_cfg:
459 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900460}
461
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900462static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900463{
464 /*
465 * Enable the e-dmac receiver only. The transmitter will be enabled when
466 * we have something to transmit
467 */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900468 outl(EDRRR_R, EDRRR(eth->port));
469}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900470
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900471static void sh_eth_stop(struct sh_eth_dev *eth)
472{
473 outl(~EDRRR_R, EDRRR(eth->port));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900474}
475
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900476int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900477{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900478 int ret = 0;
479 struct sh_eth_dev *eth = dev->priv;
480
481 ret = sh_eth_reset(eth);
482 if (ret)
483 goto err;
484
485 ret = sh_eth_desc_init(eth);
486 if (ret)
487 goto err;
488
489 ret = sh_eth_config(eth, bd);
490 if (ret)
491 goto err_config;
492
493 sh_eth_start(eth);
494
495 return ret;
496
497err_config:
498 sh_eth_tx_desc_free(eth);
499 sh_eth_rx_desc_free(eth);
500
501err:
502 return ret;
503}
504
505void sh_eth_halt(struct eth_device *dev)
506{
507 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900508 sh_eth_stop(eth);
509}
510
511int sh_eth_initialize(bd_t *bd)
512{
513 int ret = 0;
514 struct sh_eth_dev *eth = NULL;
515 struct eth_device *dev = NULL;
516
517 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
518 if (!eth) {
519 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
520 ret = -ENOMEM;
521 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900522 }
523
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900524 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
525 if (!dev) {
526 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
527 ret = -ENOMEM;
528 goto err;
529 }
530 memset(dev, 0, sizeof(struct eth_device));
531 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900532
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900533 eth->port = CONFIG_SH_ETHER_USE_PORT;
534 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
535
536 dev->priv = (void *)eth;
537 dev->iobase = 0;
538 dev->init = sh_eth_init;
539 dev->halt = sh_eth_halt;
540 dev->send = sh_eth_send;
541 dev->recv = sh_eth_recv;
542 eth->port_info[eth->port].dev = dev;
543
544 sprintf(dev->name, SHETHER_NAME);
545
546 /* Register Device to EtherNet subsystem */
547 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900548
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900549 bb_miiphy_buses[0].priv = eth;
550 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
551
Mike Frysingerc527ce92009-02-11 19:14:09 -0500552 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
553 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900554
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900555 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900556
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900557err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900558 if (dev)
559 free(dev);
560
561 if (eth)
562 free(eth);
563
564 printf(SHETHER_NAME ": Failed\n");
565 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900566}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900567
568/******* for bb_miiphy *******/
569static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
570{
571 return 0;
572}
573
574static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
575{
576 struct sh_eth_dev *eth = bus->priv;
577 int port = eth->port;
578
579 outl(inl(PIR(port)) | PIR_MMD, PIR(port));
580
581 return 0;
582}
583
584static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
585{
586 struct sh_eth_dev *eth = bus->priv;
587 int port = eth->port;
588
589 outl(inl(PIR(port)) & ~PIR_MMD, PIR(port));
590
591 return 0;
592}
593
594static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
595{
596 struct sh_eth_dev *eth = bus->priv;
597 int port = eth->port;
598
599 if (v)
600 outl(inl(PIR(port)) | PIR_MDO, PIR(port));
601 else
602 outl(inl(PIR(port)) & ~PIR_MDO, PIR(port));
603
604 return 0;
605}
606
607static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
608{
609 struct sh_eth_dev *eth = bus->priv;
610 int port = eth->port;
611
612 *v = (inl(PIR(port)) & PIR_MDI) >> 3;
613
614 return 0;
615}
616
617static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
618{
619 struct sh_eth_dev *eth = bus->priv;
620 int port = eth->port;
621
622 if (v)
623 outl(inl(PIR(port)) | PIR_MDC, PIR(port));
624 else
625 outl(inl(PIR(port)) & ~PIR_MDC, PIR(port));
626
627 return 0;
628}
629
630static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
631{
632 udelay(10);
633
634 return 0;
635}
636
637struct bb_miiphy_bus bb_miiphy_buses[] = {
638 {
639 .name = "sh_eth",
640 .init = sh_eth_bb_init,
641 .mdio_active = sh_eth_bb_mdio_active,
642 .mdio_tristate = sh_eth_bb_mdio_tristate,
643 .set_mdio = sh_eth_bb_set_mdio,
644 .get_mdio = sh_eth_bb_get_mdio,
645 .set_mdc = sh_eth_bb_set_mdc,
646 .delay = sh_eth_bb_delay,
647 }
648};
649int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);