blob: 1bc44a8d2bf60236aa1205c580314fa7a9d5945f [file] [log] [blame]
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +09001/*
2 * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
3 *
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 *
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 Iwamatsud8f5d502008-11-21 12:04:18 +090027#include <netdev.h>
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +090028#include <miiphy.h>
Nobuhiro Iwamatsu240b7232008-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 Shimoda281aa052011-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 Iwamatsu240b7232008-06-11 21:05:00 +090046
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +090047#define TIMEOUT_CNT 1000
48
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090049int sh_eth_send(struct eth_device *dev, volatile void *packet, int len)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090050{
Nobuhiro Iwamatsud8f5d502008-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 Iwamatsu240b7232008-06-11 21:05:00 +090054
55 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090056 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
57 ret = -EINVAL;
58 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090059 }
60
61 /* packet must be a 4 byte boundary */
62 if ((int)packet & (4 - 1)) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +090063 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
64 ret = -EFAULT;
65 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090066 }
67
68 /* Update tx descriptor */
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +090069 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu240b7232008-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 Iwamatsu71f507c2012-01-11 10:23:51 +090083 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu240b7232008-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 Iwamatsud8f5d502008-11-21 12:04:18 +090088 printf(SHETHER_NAME ": transmit timeout\n");
89 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +090090 goto err;
91 }
92
Nobuhiro Iwamatsu240b7232008-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 Iwamatsud8f5d502008-11-21 12:04:18 +090097 return ret;
98err:
99 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900100}
101
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900102int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900103{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900104 struct sh_eth_dev *eth = dev->priv;
105 int port = eth->port, len = 0;
106 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900107 volatile u8 *packet;
108
109 /* Check if the rx descriptor is ready */
110 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
111 /* Check for errors */
112 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
113 len = port_info->rx_desc_cur->rd1 & 0xffff;
114 packet = (volatile u8 *)
115 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
116 NetReceive(packet, len);
117 }
118
119 /* Make current descriptor available again */
120 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
121 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
122 else
123 port_info->rx_desc_cur->rd0 = RD_RACT;
124
125 /* Point to the next descriptor */
126 port_info->rx_desc_cur++;
127 if (port_info->rx_desc_cur >=
128 port_info->rx_desc_base + NUM_RX_DESC)
129 port_info->rx_desc_cur = port_info->rx_desc_base;
130 }
131
132 /* Restart the receiver if disabled */
133 if (!(inl(EDRRR(port)) & EDRRR_R))
134 outl(EDRRR_R, EDRRR(port));
135
136 return len;
137}
138
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900139static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900140{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900141 int port = eth->port;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900142#if defined(CONFIG_CPU_SH7763)
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900143 int ret = 0, i;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900144
145 /* Start e-dmac transmitter and receiver */
146 outl(EDSR_ENALL, EDSR(port));
147
148 /* Perform a software reset and wait for it to complete */
149 outl(EDMR_SRST, EDMR(port));
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900150 for (i = 0; i < TIMEOUT_CNT ; i++) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900151 if (!(inl(EDMR(port)) & EDMR_SRST))
152 break;
153 udelay(1000);
154 }
155
Nobuhiro Iwamatsu71f507c2012-01-11 10:23:51 +0900156 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900157 printf(SHETHER_NAME ": Software reset timeout\n");
158 ret = -EIO;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900159 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900160
161 return ret;
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900162#else
163 outl(inl(EDMR(port)) | EDMR_SRST, EDMR(port));
164 udelay(3000);
165 outl(inl(EDMR(port)) & ~EDMR_SRST, EDMR(port));
166
167 return 0;
168#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900169}
170
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900171static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900172{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900173 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900174 u32 tmp_addr;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900175 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900176 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900177
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900178 /*
179 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
180 */
181 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900182 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900183 TX_DESC_SIZE - 1);
184 if (!port_info->tx_desc_malloc) {
185 printf(SHETHER_NAME ": malloc failed\n");
186 ret = -ENOMEM;
187 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900188 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900189
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900190 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
191 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +0900192 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900193 /* Make sure we use a P2 address (non-cacheable) */
194 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900195 port_info->tx_desc_cur = port_info->tx_desc_base;
196
197 /* Initialize all descriptors */
198 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
199 cur_tx_desc++, i++) {
200 cur_tx_desc->td0 = 0x00;
201 cur_tx_desc->td1 = 0x00;
202 cur_tx_desc->td2 = 0x00;
203 }
204
205 /* Mark the end of the descriptors */
206 cur_tx_desc--;
207 cur_tx_desc->td0 |= TD_TDLE;
208
209 /* Point the controller to the tx descriptor list. Must use physical
210 addresses */
211 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900212#if defined(CONFIG_CPU_SH7763)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900213 outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
214 outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
215 outl(0x01, TDFFR(port));/* Last discriptor bit */
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900216#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900217
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900218err:
219 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900220}
221
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900222static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900223{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900224 int port = eth->port, i , ret = 0;
225 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900226 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900227 u32 tmp_addr;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900228 u8 *rx_buf;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900229
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900230 /*
231 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
232 */
233 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900234 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900235 RX_DESC_SIZE - 1);
236 if (!port_info->rx_desc_malloc) {
237 printf(SHETHER_NAME ": malloc failed\n");
238 ret = -ENOMEM;
239 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900240 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900241
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900242 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
243 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda281aa052011-01-27 10:06:08 +0900244 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900245 /* Make sure we use a P2 address (non-cacheable) */
246 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
247
248 port_info->rx_desc_cur = port_info->rx_desc_base;
249
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900250 /*
251 * Allocate rx data buffers. They must be 32 bytes aligned and in
252 * P2 area
253 */
254 port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
255 if (!port_info->rx_buf_malloc) {
256 printf(SHETHER_NAME ": malloc failed\n");
257 ret = -ENOMEM;
258 goto err_buf_malloc;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900259 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900260
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900261 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
262 ~(32 - 1));
263 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
264
265 /* Initialize all descriptors */
266 for (cur_rx_desc = port_info->rx_desc_base,
267 rx_buf = port_info->rx_buf_base, i = 0;
268 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
269 cur_rx_desc->rd0 = RD_RACT;
270 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
271 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
272 }
273
274 /* Mark the end of the descriptors */
275 cur_rx_desc--;
276 cur_rx_desc->rd0 |= RD_RDLE;
277
278 /* Point the controller to the rx descriptor list */
279 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900280#if defined(CONFIG_CPU_SH7763)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900281 outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
282 outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
283 outl(RDFFR_RDLF, RDFFR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900284#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900285
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900286 return ret;
287
288err_buf_malloc:
289 free(port_info->rx_desc_malloc);
290 port_info->rx_desc_malloc = NULL;
291
292err:
293 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900294}
295
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900296static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900297{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900298 int port = eth->port;
299 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900300
301 if (port_info->tx_desc_malloc) {
302 free(port_info->tx_desc_malloc);
303 port_info->tx_desc_malloc = NULL;
304 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900305}
306
307static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
308{
309 int port = eth->port;
310 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900311
312 if (port_info->rx_desc_malloc) {
313 free(port_info->rx_desc_malloc);
314 port_info->rx_desc_malloc = NULL;
315 }
316
317 if (port_info->rx_buf_malloc) {
318 free(port_info->rx_buf_malloc);
319 port_info->rx_buf_malloc = NULL;
320 }
321}
322
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900323static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900324{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900325 int ret = 0;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900326
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900327 ret = sh_eth_tx_desc_init(eth);
328 if (ret)
329 goto err_tx_init;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900330
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900331 ret = sh_eth_rx_desc_init(eth);
332 if (ret)
333 goto err_rx_init;
334
335 return ret;
336err_rx_init:
337 sh_eth_tx_desc_free(eth);
338
339err_tx_init:
340 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900341}
342
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900343static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900344{
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900345 int port = eth->port, ret = 0;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900346 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900347 struct eth_device *dev = port_info->dev;
348 struct phy_device *phydev;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900349
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900350 phydev = phy_connect(miiphy_get_dev_by_name(dev->name),
351 port_info->phy_addr, dev, PHY_INTERFACE_MODE_MII);
352 port_info->phydev = phydev;
353 phy_config(phydev);
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900354
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900355 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900356}
357
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900358static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900359{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900360 int port = eth->port, ret = 0;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900361 u32 val;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900362 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingera86bf132009-02-11 19:14:09 -0500363 struct eth_device *dev = port_info->dev;
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900364 struct phy_device *phy;
Nobuhiro Iwamatsu240b7232008-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 Iwamatsu9dfac0a2011-11-14 16:56:59 +0900373#if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900374 outl(0, RPADIR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900375#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900376 outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
377
378 /* Configure e-mac registers */
Yoshihiro Shimoda34cca922011-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 Iwamatsu240b7232008-06-11 21:05:00 +0900383 outl(0, ECSIPR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900384#endif
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900385
386 /* Set Mac address */
Mike Frysingera86bf132009-02-11 19:14:09 -0500387 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
388 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900389 outl(val, MAHR(port));
390
Mike Frysingera86bf132009-02-11 19:14:09 -0500391 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900392 outl(val, MALR(port));
393
394 outl(RFLR_RFL_MIN, RFLR(port));
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900395#if !defined(CONFIG_CPU_SH7757) && !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900396 outl(0, PIPR(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900397#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900398#if !defined(CONFIG_CPU_SH7724)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900399 outl(APR_AP, APR(port));
400 outl(MPR_MP, MPR(port));
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900401#endif
402#if defined(CONFIG_CPU_SH7763)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900403 outl(TPAUSER_TPAUSE, TPAUSER(port));
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900404#elif defined(CONFIG_CPU_SH7757)
405 outl(TPAUSER_UNLIMITED, TPAUSER(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900406#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900407
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900408 /* Configure phy */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900409 ret = sh_eth_phy_config(eth);
410 if (ret) {
Nobuhiro Iwamatsufc4b0a22009-06-25 16:33:04 +0900411 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900412 goto err_phy_cfg;
413 }
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900414 phy = port_info->phydev;
415 phy_startup(phy);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900416
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900417 val = 0;
418
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900419 /* Set the transfer speed */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900420 if (phy->speed == 100) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900421 printf(SHETHER_NAME ": 100Base/");
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900422#ifdef CONFIG_CPU_SH7763
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900423 outl(GECMR_100B, GECMR(port));
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900424#elif defined(CONFIG_CPU_SH7757)
425 outl(1, RTRATE(port));
426#elif defined(CONFIG_CPU_SH7724)
427 val = ECMR_RTM;
428#endif
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900429 } else if (phy->speed == 10) {
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900430 printf(SHETHER_NAME ": 10Base/");
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900431#ifdef CONFIG_CPU_SH7763
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900432 outl(GECMR_10B, GECMR(port));
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900433#elif defined(CONFIG_CPU_SH7757)
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900434 outl(0, RTRATE(port));
Yoshihiro Shimoda34cca922011-01-18 17:53:45 +0900435#endif
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900436 }
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900437
438 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900439 if (phy->duplex) {
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900440 printf("Full\n");
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900441 outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900442 } else {
443 printf("Half\n");
Nobuhiro Iwamatsu9dfac0a2011-11-14 16:56:59 +0900444 outl(val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900445 }
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900446
447 return ret;
448
449err_phy_cfg:
450 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900451}
452
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900453static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900454{
455 /*
456 * Enable the e-dmac receiver only. The transmitter will be enabled when
457 * we have something to transmit
458 */
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900459 outl(EDRRR_R, EDRRR(eth->port));
460}
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900461
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900462static void sh_eth_stop(struct sh_eth_dev *eth)
463{
464 outl(~EDRRR_R, EDRRR(eth->port));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900465}
466
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900467int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900468{
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900469 int ret = 0;
470 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900471
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900472 ret = sh_eth_reset(eth);
473 if (ret)
474 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900475
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900476 ret = sh_eth_desc_init(eth);
477 if (ret)
478 goto err;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900479
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900480 ret = sh_eth_config(eth, bd);
481 if (ret)
482 goto err_config;
483
484 sh_eth_start(eth);
485
486 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900487
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900488err_config:
489 sh_eth_tx_desc_free(eth);
490 sh_eth_rx_desc_free(eth);
491
492err:
493 return ret;
494}
495
496void sh_eth_halt(struct eth_device *dev)
497{
498 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900499 sh_eth_stop(eth);
500}
501
502int sh_eth_initialize(bd_t *bd)
503{
504 int ret = 0;
505 struct sh_eth_dev *eth = NULL;
506 struct eth_device *dev = NULL;
507
508 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
509 if (!eth) {
510 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
511 ret = -ENOMEM;
512 goto err;
513 }
514
515 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
516 if (!dev) {
517 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
518 ret = -ENOMEM;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900519 goto err;
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900520 }
521 memset(dev, 0, sizeof(struct eth_device));
522 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900523
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900524 eth->port = CONFIG_SH_ETHER_USE_PORT;
525 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900526
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900527 dev->priv = (void *)eth;
528 dev->iobase = 0;
529 dev->init = sh_eth_init;
530 dev->halt = sh_eth_halt;
531 dev->send = sh_eth_send;
532 dev->recv = sh_eth_recv;
533 eth->port_info[eth->port].dev = dev;
534
535 sprintf(dev->name, SHETHER_NAME);
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900536
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900537 /* Register Device to EtherNet subsystem */
538 eth_register(dev);
539
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900540 bb_miiphy_buses[0].priv = eth;
541 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
542
Mike Frysingera86bf132009-02-11 19:14:09 -0500543 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
544 puts("Please set MAC address\n");
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900545
546 return ret;
547
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900548err:
Nobuhiro Iwamatsud8f5d502008-11-21 12:04:18 +0900549 if (dev)
550 free(dev);
551
552 if (eth)
553 free(eth);
554
555 printf(SHETHER_NAME ": Failed\n");
556 return ret;
Nobuhiro Iwamatsu240b7232008-06-11 21:05:00 +0900557}
Yoshihiro Shimoda677f6cd2011-10-11 18:10:14 +0900558
559/******* for bb_miiphy *******/
560static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
561{
562 return 0;
563}
564
565static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
566{
567 struct sh_eth_dev *eth = bus->priv;
568 int port = eth->port;
569
570 outl(inl(PIR(port)) | PIR_MMD, PIR(port));
571
572 return 0;
573}
574
575static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
576{
577 struct sh_eth_dev *eth = bus->priv;
578 int port = eth->port;
579
580 outl(inl(PIR(port)) & ~PIR_MMD, PIR(port));
581
582 return 0;
583}
584
585static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
586{
587 struct sh_eth_dev *eth = bus->priv;
588 int port = eth->port;
589
590 if (v)
591 outl(inl(PIR(port)) | PIR_MDO, PIR(port));
592 else
593 outl(inl(PIR(port)) & ~PIR_MDO, PIR(port));
594
595 return 0;
596}
597
598static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
599{
600 struct sh_eth_dev *eth = bus->priv;
601 int port = eth->port;
602
603 *v = (inl(PIR(port)) & PIR_MDI) >> 3;
604
605 return 0;
606}
607
608static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
609{
610 struct sh_eth_dev *eth = bus->priv;
611 int port = eth->port;
612
613 if (v)
614 outl(inl(PIR(port)) | PIR_MDC, PIR(port));
615 else
616 outl(inl(PIR(port)) & ~PIR_MDC, PIR(port));
617
618 return 0;
619}
620
621static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
622{
623 udelay(10);
624
625 return 0;
626}
627
628struct bb_miiphy_bus bb_miiphy_buses[] = {
629 {
630 .name = "sh_eth",
631 .init = sh_eth_bb_init,
632 .mdio_active = sh_eth_bb_mdio_active,
633 .mdio_tristate = sh_eth_bb_mdio_tristate,
634 .set_mdio = sh_eth_bb_set_mdio,
635 .get_mdio = sh_eth_bb_get_mdio,
636 .set_mdc = sh_eth_bb_set_mdc,
637 .delay = sh_eth_bb_delay,
638 }
639};
640int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);