blob: 4cfd1e512a3a73bb859291308e8dc90f5503fe00 [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
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090029#ifdef CONFIG_SH_ETHER_CACHE_WRITEBACK
30#define flush_cache_wback(addr, len) \
Nobuhiro Iwamatsu870cc232013-08-22 13:22:01 +090031 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 Iwamatsu4ba62c72012-01-11 10:23:51 +090036#define TIMEOUT_CNT 1000
37
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000038int sh_eth_send(struct eth_device *dev, void *packet, int len)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090039{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090040 struct sh_eth_dev *eth = dev->priv;
41 int port = eth->port, ret = 0, timeout;
42 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090043
44 if (!packet || len > 0xffff) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090045 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
46 ret = -EINVAL;
47 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090048 }
49
50 /* packet must be a 4 byte boundary */
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +000051 if ((int)packet & 3) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090052 printf(SHETHER_NAME ": %s: packet not 4 byte alligned\n", __func__);
53 ret = -EFAULT;
54 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090055 }
56
57 /* Update tx descriptor */
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +090058 flush_cache_wback(packet, len);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090059 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
60 port_info->tx_desc_cur->td1 = len << 16;
61 /* Must preserve the end of descriptor list indication */
62 if (port_info->tx_desc_cur->td0 & TD_TDLE)
63 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
64 else
65 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
66
67 /* Restart the transmitter if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +000068 if (!(sh_eth_read(eth, EDTRR) & EDTRR_TRNS))
69 sh_eth_write(eth, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090070
71 /* Wait until packet is transmitted */
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +090072 timeout = TIMEOUT_CNT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090073 while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
74 udelay(100);
75
76 if (timeout < 0) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090077 printf(SHETHER_NAME ": transmit timeout\n");
78 ret = -ETIMEDOUT;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090079 goto err;
80 }
81
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090082 port_info->tx_desc_cur++;
83 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
84 port_info->tx_desc_cur = port_info->tx_desc_base;
85
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090086err:
87 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090088}
89
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090090int sh_eth_recv(struct eth_device *dev)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090091{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +090092 struct sh_eth_dev *eth = dev->priv;
93 int port = eth->port, len = 0;
94 struct sh_eth_info *port_info = &eth->port_info[port];
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000095 uchar *packet;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +090096
97 /* Check if the rx descriptor is ready */
98 if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
99 /* Check for errors */
100 if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
101 len = port_info->rx_desc_cur->rd1 & 0xffff;
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000102 packet = (uchar *)
103 ADDR_TO_P2(port_info->rx_desc_cur->rd2);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900104 NetReceive(packet, len);
105 }
106
107 /* Make current descriptor available again */
108 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
109 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
110 else
111 port_info->rx_desc_cur->rd0 = RD_RACT;
112
113 /* Point to the next descriptor */
114 port_info->rx_desc_cur++;
115 if (port_info->rx_desc_cur >=
116 port_info->rx_desc_base + NUM_RX_DESC)
117 port_info->rx_desc_cur = port_info->rx_desc_base;
118 }
119
120 /* Restart the receiver if disabled */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000121 if (!(sh_eth_read(eth, EDRRR) & EDRRR_R))
122 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900123
124 return len;
125}
126
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900127static int sh_eth_reset(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900128{
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000129#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900130 int ret = 0, i;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900131
132 /* Start e-dmac transmitter and receiver */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000133 sh_eth_write(eth, EDSR_ENALL, EDSR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900134
135 /* Perform a software reset and wait for it to complete */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000136 sh_eth_write(eth, EDMR_SRST, EDMR);
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900137 for (i = 0; i < TIMEOUT_CNT ; i++) {
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000138 if (!(sh_eth_read(eth, EDMR) & EDMR_SRST))
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900139 break;
140 udelay(1000);
141 }
142
Nobuhiro Iwamatsu4ba62c72012-01-11 10:23:51 +0900143 if (i == TIMEOUT_CNT) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900144 printf(SHETHER_NAME ": Software reset timeout\n");
145 ret = -EIO;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900146 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900147
148 return ret;
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900149#else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000150 sh_eth_write(eth, sh_eth_read(eth, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900151 udelay(3000);
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000152 sh_eth_write(eth, sh_eth_read(eth, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900153
154 return 0;
155#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900156}
157
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900158static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900159{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900160 int port = eth->port, i, ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900161 u32 tmp_addr;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900162 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900163 struct tx_desc_s *cur_tx_desc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900164
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900165 /*
166 * Allocate tx descriptors. They must be TX_DESC_SIZE bytes aligned
167 */
168 port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900169 sizeof(struct tx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900170 TX_DESC_SIZE - 1);
171 if (!port_info->tx_desc_malloc) {
172 printf(SHETHER_NAME ": malloc failed\n");
173 ret = -ENOMEM;
174 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900175 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900176
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900177 tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
178 ~(TX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900179 flush_cache_wback(tmp_addr, NUM_TX_DESC * sizeof(struct tx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900180 /* Make sure we use a P2 address (non-cacheable) */
181 port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900182 port_info->tx_desc_cur = port_info->tx_desc_base;
183
184 /* Initialize all descriptors */
185 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
186 cur_tx_desc++, i++) {
187 cur_tx_desc->td0 = 0x00;
188 cur_tx_desc->td1 = 0x00;
189 cur_tx_desc->td2 = 0x00;
190 }
191
192 /* Mark the end of the descriptors */
193 cur_tx_desc--;
194 cur_tx_desc->td0 |= TD_TDLE;
195
196 /* Point the controller to the tx descriptor list. Must use physical
197 addresses */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000198 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000199#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000200 sh_eth_write(eth, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
201 sh_eth_write(eth, ADDR_TO_PHY(cur_tx_desc), TDFXR);
202 sh_eth_write(eth, 0x01, TDFFR);/* Last discriptor bit */
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900203#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900204
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900205err:
206 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900207}
208
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900209static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900210{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900211 int port = eth->port, i , ret = 0;
212 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900213 struct rx_desc_s *cur_rx_desc;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900214 u32 tmp_addr;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900215 u8 *rx_buf;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900216
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900217 /*
218 * Allocate rx descriptors. They must be RX_DESC_SIZE bytes aligned
219 */
220 port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900221 sizeof(struct rx_desc_s) +
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900222 RX_DESC_SIZE - 1);
223 if (!port_info->rx_desc_malloc) {
224 printf(SHETHER_NAME ": malloc failed\n");
225 ret = -ENOMEM;
226 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900227 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900228
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900229 tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
230 ~(RX_DESC_SIZE - 1));
Yoshihiro Shimoda68260aa2011-01-27 10:06:08 +0900231 flush_cache_wback(tmp_addr, NUM_RX_DESC * sizeof(struct rx_desc_s));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900232 /* Make sure we use a P2 address (non-cacheable) */
233 port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
234
235 port_info->rx_desc_cur = port_info->rx_desc_base;
236
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900237 /*
238 * Allocate rx data buffers. They must be 32 bytes aligned and in
239 * P2 area
240 */
241 port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE + 31);
242 if (!port_info->rx_buf_malloc) {
243 printf(SHETHER_NAME ": malloc failed\n");
244 ret = -ENOMEM;
245 goto err_buf_malloc;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900246 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900247
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900248 tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
249 ~(32 - 1));
250 port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
251
252 /* Initialize all descriptors */
253 for (cur_rx_desc = port_info->rx_desc_base,
254 rx_buf = port_info->rx_buf_base, i = 0;
255 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
256 cur_rx_desc->rd0 = RD_RACT;
257 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
258 cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
259 }
260
261 /* Mark the end of the descriptors */
262 cur_rx_desc--;
263 cur_rx_desc->rd0 |= RD_RDLE;
264
265 /* Point the controller to the rx descriptor list */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000266 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000267#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000268 sh_eth_write(eth, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
269 sh_eth_write(eth, ADDR_TO_PHY(cur_rx_desc), RDFXR);
270 sh_eth_write(eth, RDFFR_RDLF, RDFFR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900271#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900272
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900273 return ret;
274
275err_buf_malloc:
276 free(port_info->rx_desc_malloc);
277 port_info->rx_desc_malloc = NULL;
278
279err:
280 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900281}
282
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900283static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900284{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900285 int port = eth->port;
286 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900287
288 if (port_info->tx_desc_malloc) {
289 free(port_info->tx_desc_malloc);
290 port_info->tx_desc_malloc = NULL;
291 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900292}
293
294static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
295{
296 int port = eth->port;
297 struct sh_eth_info *port_info = &eth->port_info[port];
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900298
299 if (port_info->rx_desc_malloc) {
300 free(port_info->rx_desc_malloc);
301 port_info->rx_desc_malloc = NULL;
302 }
303
304 if (port_info->rx_buf_malloc) {
305 free(port_info->rx_buf_malloc);
306 port_info->rx_buf_malloc = NULL;
307 }
308}
309
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900310static int sh_eth_desc_init(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900311{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900312 int ret = 0;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900313
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900314 ret = sh_eth_tx_desc_init(eth);
315 if (ret)
316 goto err_tx_init;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900317
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900318 ret = sh_eth_rx_desc_init(eth);
319 if (ret)
320 goto err_rx_init;
321
322 return ret;
323err_rx_init:
324 sh_eth_tx_desc_free(eth);
325
326err_tx_init:
327 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900328}
329
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900330static int sh_eth_phy_config(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900331{
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900332 int port = eth->port, ret = 0;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900333 struct sh_eth_info *port_info = &eth->port_info[port];
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900334 struct eth_device *dev = port_info->dev;
335 struct phy_device *phydev;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900336
Nobuhiro Iwamatsuee6ec5d2012-02-02 21:28:49 +0000337 phydev = phy_connect(
338 miiphy_get_dev_by_name(dev->name),
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000339 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900340 port_info->phydev = phydev;
341 phy_config(phydev);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900342
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900343 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900344}
345
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900346static int sh_eth_config(struct sh_eth_dev *eth, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900347{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900348 int port = eth->port, ret = 0;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900349 u32 val;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900350 struct sh_eth_info *port_info = &eth->port_info[port];
Mike Frysingerc527ce92009-02-11 19:14:09 -0500351 struct eth_device *dev = port_info->dev;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900352 struct phy_device *phy;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900353
354 /* Configure e-dmac registers */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000355 sh_eth_write(eth, (sh_eth_read(eth, EDMR) & ~EMDR_DESC_R) | EDMR_EL,
356 EDMR);
357 sh_eth_write(eth, 0, EESIPR);
358 sh_eth_write(eth, 0, TRSCER);
359 sh_eth_write(eth, 0, TFTR);
360 sh_eth_write(eth, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
361 sh_eth_write(eth, RMCR_RST, RMCR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000362#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000363 sh_eth_write(eth, 0, RPADIR);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900364#endif
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000365 sh_eth_write(eth, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900366
367 /* Configure e-mac registers */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000368 sh_eth_write(eth, 0, ECSIPR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900369
370 /* Set Mac address */
Mike Frysingerc527ce92009-02-11 19:14:09 -0500371 val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 |
372 dev->enetaddr[2] << 8 | dev->enetaddr[3];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000373 sh_eth_write(eth, val, MAHR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900374
Mike Frysingerc527ce92009-02-11 19:14:09 -0500375 val = dev->enetaddr[4] << 8 | dev->enetaddr[5];
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000376 sh_eth_write(eth, val, MALR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900377
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000378 sh_eth_write(eth, RFLR_RFL_MIN, RFLR);
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000379#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000380 sh_eth_write(eth, 0, PIPR);
381 sh_eth_write(eth, APR_AP, APR);
382 sh_eth_write(eth, MPR_MP, MPR);
383 sh_eth_write(eth, TPAUSER_TPAUSE, TPAUSER);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900384#endif
385
Nobuhiro Iwamatsudcd5a592012-08-02 22:08:40 +0000386#if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000387 sh_eth_write(eth, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000388#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900389 /* Configure phy */
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900390 ret = sh_eth_phy_config(eth);
391 if (ret) {
Nobuhiro Iwamatsu88a4c2e2009-06-25 16:33:04 +0900392 printf(SHETHER_NAME ": phy config timeout\n");
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900393 goto err_phy_cfg;
394 }
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900395 phy = port_info->phydev;
Timur Tabi11af8d62012-07-09 08:52:43 +0000396 ret = phy_startup(phy);
397 if (ret) {
398 printf(SHETHER_NAME ": phy startup failure\n");
399 return ret;
400 }
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900401
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900402 val = 0;
403
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900404 /* Set the transfer speed */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900405 if (phy->speed == 100) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900406 printf(SHETHER_NAME ": 100Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000407#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000408 sh_eth_write(eth, GECMR_100B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000409#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000410 sh_eth_write(eth, 1, RTRATE);
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900411#elif defined(CONFIG_CPU_SH7724)
412 val = ECMR_RTM;
413#endif
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900414 } else if (phy->speed == 10) {
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900415 printf(SHETHER_NAME ": 10Base/");
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000416#if defined(SH_ETH_TYPE_GETHER)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000417 sh_eth_write(eth, GECMR_10B, GECMR);
Yoshihiro Shimodae3bb3252012-11-04 15:54:30 +0000418#elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000419 sh_eth_write(eth, 0, RTRATE);
Yoshihiro Shimoda903de462011-01-18 17:53:45 +0900420#endif
Nobuhiro Iwamatsu3bb4cc32011-11-14 16:56:59 +0900421 }
Yoshihiro Shimoda26235092012-06-26 16:38:06 +0000422#if defined(SH_ETH_TYPE_GETHER)
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000423 else if (phy->speed == 1000) {
424 printf(SHETHER_NAME ": 1000Base/");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000425 sh_eth_write(eth, GECMR_1000B, GECMR);
Nobuhiro Iwamatsu4398d552012-05-15 15:49:39 +0000426 }
427#endif
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900428
429 /* Check if full duplex mode is supported by the phy */
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900430 if (phy->duplex) {
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900431 printf("Full\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000432 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM),
433 ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900434 } else {
435 printf("Half\n");
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000436 sh_eth_write(eth, val | (ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900437 }
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900438
439 return ret;
440
441err_phy_cfg:
442 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900443}
444
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900445static void sh_eth_start(struct sh_eth_dev *eth)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900446{
447 /*
448 * Enable the e-dmac receiver only. The transmitter will be enabled when
449 * we have something to transmit
450 */
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000451 sh_eth_write(eth, EDRRR_R, EDRRR);
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900452}
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900453
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900454static void sh_eth_stop(struct sh_eth_dev *eth)
455{
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000456 sh_eth_write(eth, ~EDRRR_R, EDRRR);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900457}
458
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900459int sh_eth_init(struct eth_device *dev, bd_t *bd)
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900460{
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900461 int ret = 0;
462 struct sh_eth_dev *eth = dev->priv;
463
464 ret = sh_eth_reset(eth);
465 if (ret)
466 goto err;
467
468 ret = sh_eth_desc_init(eth);
469 if (ret)
470 goto err;
471
472 ret = sh_eth_config(eth, bd);
473 if (ret)
474 goto err_config;
475
476 sh_eth_start(eth);
477
478 return ret;
479
480err_config:
481 sh_eth_tx_desc_free(eth);
482 sh_eth_rx_desc_free(eth);
483
484err:
485 return ret;
486}
487
488void sh_eth_halt(struct eth_device *dev)
489{
490 struct sh_eth_dev *eth = dev->priv;
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900491 sh_eth_stop(eth);
492}
493
494int sh_eth_initialize(bd_t *bd)
495{
496 int ret = 0;
497 struct sh_eth_dev *eth = NULL;
498 struct eth_device *dev = NULL;
499
500 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
501 if (!eth) {
502 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
503 ret = -ENOMEM;
504 goto err;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900505 }
506
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900507 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
508 if (!dev) {
509 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
510 ret = -ENOMEM;
511 goto err;
512 }
513 memset(dev, 0, sizeof(struct eth_device));
514 memset(eth, 0, sizeof(struct sh_eth_dev));
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900515
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900516 eth->port = CONFIG_SH_ETHER_USE_PORT;
517 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
518
519 dev->priv = (void *)eth;
520 dev->iobase = 0;
521 dev->init = sh_eth_init;
522 dev->halt = sh_eth_halt;
523 dev->send = sh_eth_send;
524 dev->recv = sh_eth_recv;
525 eth->port_info[eth->port].dev = dev;
526
527 sprintf(dev->name, SHETHER_NAME);
528
529 /* Register Device to EtherNet subsystem */
530 eth_register(dev);
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900531
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900532 bb_miiphy_buses[0].priv = eth;
533 miiphy_register(dev->name, bb_miiphy_read, bb_miiphy_write);
534
Mike Frysingerc527ce92009-02-11 19:14:09 -0500535 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr))
536 puts("Please set MAC address\n");
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900537
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900538 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900539
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900540err:
Nobuhiro Iwamatsubd3980c2008-11-21 12:04:18 +0900541 if (dev)
542 free(dev);
543
544 if (eth)
545 free(eth);
546
547 printf(SHETHER_NAME ": Failed\n");
548 return ret;
Nobuhiro Iwamatsu9751ee02008-06-11 21:05:00 +0900549}
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900550
551/******* for bb_miiphy *******/
552static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
553{
554 return 0;
555}
556
557static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
558{
559 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900560
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000561 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900562
563 return 0;
564}
565
566static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
567{
568 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900569
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000570 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MMD, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900571
572 return 0;
573}
574
575static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
576{
577 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900578
579 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000580 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900581 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000582 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDO, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900583
584 return 0;
585}
586
587static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
588{
589 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900590
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000591 *v = (sh_eth_read(eth, PIR) & PIR_MDI) >> 3;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900592
593 return 0;
594}
595
596static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
597{
598 struct sh_eth_dev *eth = bus->priv;
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900599
600 if (v)
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000601 sh_eth_write(eth, sh_eth_read(eth, PIR) | PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900602 else
Yoshihiro Shimoda49afb8c2012-06-26 16:38:09 +0000603 sh_eth_write(eth, sh_eth_read(eth, PIR) & ~PIR_MDC, PIR);
Yoshihiro Shimodabd1024b2011-10-11 18:10:14 +0900604
605 return 0;
606}
607
608static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
609{
610 udelay(10);
611
612 return 0;
613}
614
615struct bb_miiphy_bus bb_miiphy_buses[] = {
616 {
617 .name = "sh_eth",
618 .init = sh_eth_bb_init,
619 .mdio_active = sh_eth_bb_mdio_active,
620 .mdio_tristate = sh_eth_bb_mdio_tristate,
621 .set_mdio = sh_eth_bb_set_mdio,
622 .get_mdio = sh_eth_bb_get_mdio,
623 .set_mdc = sh_eth_bb_set_mdc,
624 .delay = sh_eth_bb_delay,
625 }
626};
627int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);