blob: dcf9f87d021fea201319e5bdc3a3c9b7c05eb43e [file] [log] [blame]
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001/*
2 * SuperH Ethernet device driver
3 *
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09004 * Copyright (C) 2006-2008 Nobuhiro Iwamatsu
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00005 * Copyright (C) 2008-2009 Renesas Solutions Corp.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 */
22
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070023#include <linux/init.h>
24#include <linux/dma-mapping.h>
25#include <linux/etherdevice.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/mdio-bitbang.h>
29#include <linux/netdevice.h>
30#include <linux/phy.h>
31#include <linux/cache.h>
32#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000033#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000035#include <linux/ethtool.h>
Nobuhiro Iwamatsuf568a922009-10-26 13:49:50 +000036#include <asm/cacheflush.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070037
38#include "sh_eth.h"
39
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#define SH_ETH_DEF_MSG_ENABLE \
41 (NETIF_MSG_LINK | \
42 NETIF_MSG_TIMER | \
43 NETIF_MSG_RX_ERR| \
44 NETIF_MSG_TX_ERR)
45
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000046/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000047#if defined(CONFIG_CPU_SUBTYPE_SH7724)
48#define SH_ETH_RESET_DEFAULT 1
49static void sh_eth_set_duplex(struct net_device *ndev)
50{
51 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000052
53 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000054 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000055 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000056 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000057}
58
59static void sh_eth_set_rate(struct net_device *ndev)
60{
61 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000062
63 switch (mdp->speed) {
64 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000065 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000066 break;
67 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000068 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000069 break;
70 default:
71 break;
72 }
73}
74
75/* SH7724 */
76static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
77 .set_duplex = sh_eth_set_duplex,
78 .set_rate = sh_eth_set_rate,
79
80 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
81 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
82 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
83
84 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
85 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
86 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
87 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
88
89 .apr = 1,
90 .mpr = 1,
91 .tpauser = 1,
92 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -080093 .rpadir = 1,
94 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000095};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000096#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +000097#define SH_ETH_HAS_BOTH_MODULES 1
98#define SH_ETH_HAS_TSU 1
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000099static void sh_eth_set_duplex(struct net_device *ndev)
100{
101 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000102
103 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000104 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000105 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000106 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000107}
108
109static void sh_eth_set_rate(struct net_device *ndev)
110{
111 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000112
113 switch (mdp->speed) {
114 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000115 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000116 break;
117 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000118 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000119 break;
120 default:
121 break;
122 }
123}
124
125/* SH7757 */
126static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
127 .set_duplex = sh_eth_set_duplex,
128 .set_rate = sh_eth_set_rate,
129
130 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
131 .rmcr_value = 0x00000001,
132
133 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
134 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
135 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
136 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
137
138 .apr = 1,
139 .mpr = 1,
140 .tpauser = 1,
141 .hw_swap = 1,
142 .no_ade = 1,
143};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000144
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000145#define SH_GIGA_ETH_BASE 0xfee00000
146#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
147#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
148static void sh_eth_chip_reset_giga(struct net_device *ndev)
149{
150 int i;
151 unsigned long mahr[2], malr[2];
152
153 /* save MAHR and MALR */
154 for (i = 0; i < 2; i++) {
155 malr[i] = readl(GIGA_MALR(i));
156 mahr[i] = readl(GIGA_MAHR(i));
157 }
158
159 /* reset device */
160 writel(ARSTR_ARSTR, SH_GIGA_ETH_BASE + 0x1800);
161 mdelay(1);
162
163 /* restore MAHR and MALR */
164 for (i = 0; i < 2; i++) {
165 writel(malr[i], GIGA_MALR(i));
166 writel(mahr[i], GIGA_MAHR(i));
167 }
168}
169
170static int sh_eth_is_gether(struct sh_eth_private *mdp);
171static void sh_eth_reset(struct net_device *ndev)
172{
173 struct sh_eth_private *mdp = netdev_priv(ndev);
174 int cnt = 100;
175
176 if (sh_eth_is_gether(mdp)) {
177 sh_eth_write(ndev, 0x03, EDSR);
178 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
179 EDMR);
180 while (cnt > 0) {
181 if (!(sh_eth_read(ndev, EDMR) & 0x3))
182 break;
183 mdelay(1);
184 cnt--;
185 }
186 if (cnt < 0)
187 printk(KERN_ERR "Device reset fail\n");
188
189 /* Table Init */
190 sh_eth_write(ndev, 0x0, TDLAR);
191 sh_eth_write(ndev, 0x0, TDFAR);
192 sh_eth_write(ndev, 0x0, TDFXR);
193 sh_eth_write(ndev, 0x0, TDFFR);
194 sh_eth_write(ndev, 0x0, RDLAR);
195 sh_eth_write(ndev, 0x0, RDFAR);
196 sh_eth_write(ndev, 0x0, RDFXR);
197 sh_eth_write(ndev, 0x0, RDFFR);
198 } else {
199 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
200 EDMR);
201 mdelay(3);
202 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
203 EDMR);
204 }
205}
206
207static void sh_eth_set_duplex_giga(struct net_device *ndev)
208{
209 struct sh_eth_private *mdp = netdev_priv(ndev);
210
211 if (mdp->duplex) /* Full */
212 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
213 else /* Half */
214 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
215}
216
217static void sh_eth_set_rate_giga(struct net_device *ndev)
218{
219 struct sh_eth_private *mdp = netdev_priv(ndev);
220
221 switch (mdp->speed) {
222 case 10: /* 10BASE */
223 sh_eth_write(ndev, 0x00000000, GECMR);
224 break;
225 case 100:/* 100BASE */
226 sh_eth_write(ndev, 0x00000010, GECMR);
227 break;
228 case 1000: /* 1000BASE */
229 sh_eth_write(ndev, 0x00000020, GECMR);
230 break;
231 default:
232 break;
233 }
234}
235
236/* SH7757(GETHERC) */
237static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
238 .chip_reset = sh_eth_chip_reset_giga,
239 .set_duplex = sh_eth_set_duplex_giga,
240 .set_rate = sh_eth_set_rate_giga,
241
242 .ecsr_value = ECSR_ICD | ECSR_MPD,
243 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
244 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
245
246 .tx_check = EESR_TC1 | EESR_FTC,
247 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
248 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
249 EESR_ECI,
250 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
251 EESR_TFE,
252 .fdr_value = 0x0000072f,
253 .rmcr_value = 0x00000001,
254
255 .apr = 1,
256 .mpr = 1,
257 .tpauser = 1,
258 .bculr = 1,
259 .hw_swap = 1,
260 .rpadir = 1,
261 .rpadir_value = 2 << 16,
262 .no_trimd = 1,
263 .no_ade = 1,
264};
265
266static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
267{
268 if (sh_eth_is_gether(mdp))
269 return &sh_eth_my_cpu_data_giga;
270 else
271 return &sh_eth_my_cpu_data;
272}
273
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000274#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000275#define SH_ETH_HAS_TSU 1
276static void sh_eth_chip_reset(struct net_device *ndev)
277{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000278 struct sh_eth_private *mdp = netdev_priv(ndev);
279
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000280 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000281 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000282 mdelay(1);
283}
284
285static void sh_eth_reset(struct net_device *ndev)
286{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000287 int cnt = 100;
288
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000289 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000290 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000291 while (cnt > 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000292 if (!(sh_eth_read(ndev, EDMR) & 0x3))
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000293 break;
294 mdelay(1);
295 cnt--;
296 }
roel kluin890c8c12009-12-30 01:43:45 +0000297 if (cnt == 0)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000298 printk(KERN_ERR "Device reset fail\n");
299
300 /* Table Init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000301 sh_eth_write(ndev, 0x0, TDLAR);
302 sh_eth_write(ndev, 0x0, TDFAR);
303 sh_eth_write(ndev, 0x0, TDFXR);
304 sh_eth_write(ndev, 0x0, TDFFR);
305 sh_eth_write(ndev, 0x0, RDLAR);
306 sh_eth_write(ndev, 0x0, RDFAR);
307 sh_eth_write(ndev, 0x0, RDFXR);
308 sh_eth_write(ndev, 0x0, RDFFR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000309}
310
311static void sh_eth_set_duplex(struct net_device *ndev)
312{
313 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000314
315 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000316 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000317 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000318 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000319}
320
321static void sh_eth_set_rate(struct net_device *ndev)
322{
323 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000324
325 switch (mdp->speed) {
326 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000327 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000328 break;
329 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000330 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000331 break;
332 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000333 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000334 break;
335 default:
336 break;
337 }
338}
339
340/* sh7763 */
341static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
342 .chip_reset = sh_eth_chip_reset,
343 .set_duplex = sh_eth_set_duplex,
344 .set_rate = sh_eth_set_rate,
345
346 .ecsr_value = ECSR_ICD | ECSR_MPD,
347 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
348 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
349
350 .tx_check = EESR_TC1 | EESR_FTC,
351 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
352 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
353 EESR_ECI,
354 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
355 EESR_TFE,
356
357 .apr = 1,
358 .mpr = 1,
359 .tpauser = 1,
360 .bculr = 1,
361 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000362 .no_trimd = 1,
363 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000364 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000365};
366
367#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
368#define SH_ETH_RESET_DEFAULT 1
369static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
370 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
371
372 .apr = 1,
373 .mpr = 1,
374 .tpauser = 1,
375 .hw_swap = 1,
376};
377#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
378#define SH_ETH_RESET_DEFAULT 1
379#define SH_ETH_HAS_TSU 1
380static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
381 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000382 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000383};
384#endif
385
386static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
387{
388 if (!cd->ecsr_value)
389 cd->ecsr_value = DEFAULT_ECSR_INIT;
390
391 if (!cd->ecsipr_value)
392 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
393
394 if (!cd->fcftr_value)
395 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
396 DEFAULT_FIFO_F_D_RFD;
397
398 if (!cd->fdr_value)
399 cd->fdr_value = DEFAULT_FDR_INIT;
400
401 if (!cd->rmcr_value)
402 cd->rmcr_value = DEFAULT_RMCR_VALUE;
403
404 if (!cd->tx_check)
405 cd->tx_check = DEFAULT_TX_CHECK;
406
407 if (!cd->eesr_err_check)
408 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
409
410 if (!cd->tx_error_check)
411 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
412}
413
414#if defined(SH_ETH_RESET_DEFAULT)
415/* Chip Reset */
416static void sh_eth_reset(struct net_device *ndev)
417{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000418 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000419 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000420 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000421}
422#endif
423
424#if defined(CONFIG_CPU_SH4)
425static void sh_eth_set_receive_align(struct sk_buff *skb)
426{
427 int reserve;
428
429 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
430 if (reserve)
431 skb_reserve(skb, reserve);
432}
433#else
434static void sh_eth_set_receive_align(struct sk_buff *skb)
435{
436 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
437}
438#endif
439
440
Yoshinori Sato71557a32008-08-06 19:49:00 -0400441/* CPU <-> EDMAC endian convert */
442static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
443{
444 switch (mdp->edmac_endian) {
445 case EDMAC_LITTLE_ENDIAN:
446 return cpu_to_le32(x);
447 case EDMAC_BIG_ENDIAN:
448 return cpu_to_be32(x);
449 }
450 return x;
451}
452
453static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
454{
455 switch (mdp->edmac_endian) {
456 case EDMAC_LITTLE_ENDIAN:
457 return le32_to_cpu(x);
458 case EDMAC_BIG_ENDIAN:
459 return be32_to_cpu(x);
460 }
461 return x;
462}
463
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700464/*
465 * Program the hardware MAC address from dev->dev_addr.
466 */
467static void update_mac_address(struct net_device *ndev)
468{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000469 sh_eth_write(ndev,
470 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
471 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
472 sh_eth_write(ndev,
473 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700474}
475
476/*
477 * Get MAC address from SuperH MAC address register
478 *
479 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
480 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
481 * When you want use this device, you must set MAC address in bootloader.
482 *
483 */
Magnus Damm748031f2009-10-09 00:17:14 +0000484static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700485{
Magnus Damm748031f2009-10-09 00:17:14 +0000486 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
487 memcpy(ndev->dev_addr, mac, 6);
488 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000489 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
490 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
491 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
492 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
493 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
494 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000495 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700496}
497
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000498static int sh_eth_is_gether(struct sh_eth_private *mdp)
499{
500 if (mdp->reg_offset == sh_eth_offset_gigabit)
501 return 1;
502 else
503 return 0;
504}
505
506static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
507{
508 if (sh_eth_is_gether(mdp))
509 return EDTRR_TRNS_GETHER;
510 else
511 return EDTRR_TRNS_ETHER;
512}
513
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700514struct bb_info {
515 struct mdiobb_ctrl ctrl;
516 u32 addr;
517 u32 mmd_msk;/* MMD */
518 u32 mdo_msk;
519 u32 mdi_msk;
520 u32 mdc_msk;
521};
522
523/* PHY bit set */
524static void bb_set(u32 addr, u32 msk)
525{
Paul Mundt900fcf02010-11-01 09:29:24 +0000526 writel(readl(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700527}
528
529/* PHY bit clear */
530static void bb_clr(u32 addr, u32 msk)
531{
Paul Mundt900fcf02010-11-01 09:29:24 +0000532 writel((readl(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700533}
534
535/* PHY bit read */
536static int bb_read(u32 addr, u32 msk)
537{
Paul Mundt900fcf02010-11-01 09:29:24 +0000538 return (readl(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700539}
540
541/* Data I/O pin control */
542static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
543{
544 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
545 if (bit)
546 bb_set(bitbang->addr, bitbang->mmd_msk);
547 else
548 bb_clr(bitbang->addr, bitbang->mmd_msk);
549}
550
551/* Set bit data*/
552static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
553{
554 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
555
556 if (bit)
557 bb_set(bitbang->addr, bitbang->mdo_msk);
558 else
559 bb_clr(bitbang->addr, bitbang->mdo_msk);
560}
561
562/* Get bit data*/
563static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
564{
565 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
566 return bb_read(bitbang->addr, bitbang->mdi_msk);
567}
568
569/* MDC pin control */
570static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
571{
572 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
573
574 if (bit)
575 bb_set(bitbang->addr, bitbang->mdc_msk);
576 else
577 bb_clr(bitbang->addr, bitbang->mdc_msk);
578}
579
580/* mdio bus control struct */
581static struct mdiobb_ops bb_ops = {
582 .owner = THIS_MODULE,
583 .set_mdc = sh_mdc_ctrl,
584 .set_mdio_dir = sh_mmd_ctrl,
585 .set_mdio_data = sh_set_mdio,
586 .get_mdio_data = sh_get_mdio,
587};
588
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700589/* free skb and descriptor buffer */
590static void sh_eth_ring_free(struct net_device *ndev)
591{
592 struct sh_eth_private *mdp = netdev_priv(ndev);
593 int i;
594
595 /* Free Rx skb ringbuffer */
596 if (mdp->rx_skbuff) {
597 for (i = 0; i < RX_RING_SIZE; i++) {
598 if (mdp->rx_skbuff[i])
599 dev_kfree_skb(mdp->rx_skbuff[i]);
600 }
601 }
602 kfree(mdp->rx_skbuff);
603
604 /* Free Tx skb ringbuffer */
605 if (mdp->tx_skbuff) {
606 for (i = 0; i < TX_RING_SIZE; i++) {
607 if (mdp->tx_skbuff[i])
608 dev_kfree_skb(mdp->tx_skbuff[i]);
609 }
610 }
611 kfree(mdp->tx_skbuff);
612}
613
614/* format skb and descriptor buffer */
615static void sh_eth_ring_format(struct net_device *ndev)
616{
617 struct sh_eth_private *mdp = netdev_priv(ndev);
618 int i;
619 struct sk_buff *skb;
620 struct sh_eth_rxdesc *rxdesc = NULL;
621 struct sh_eth_txdesc *txdesc = NULL;
622 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
623 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
624
625 mdp->cur_rx = mdp->cur_tx = 0;
626 mdp->dirty_rx = mdp->dirty_tx = 0;
627
628 memset(mdp->rx_ring, 0, rx_ringsize);
629
630 /* build Rx ring buffer */
631 for (i = 0; i < RX_RING_SIZE; i++) {
632 /* skb */
633 mdp->rx_skbuff[i] = NULL;
634 skb = dev_alloc_skb(mdp->rx_buf_sz);
635 mdp->rx_skbuff[i] = skb;
636 if (skb == NULL)
637 break;
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000638 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
639 DMA_FROM_DEVICE);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900640 skb->dev = ndev; /* Mark as being used by this device. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000641 sh_eth_set_receive_align(skb);
642
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700643 /* RX descriptor */
644 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000645 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400646 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700647
648 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000649 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900650 /* Rx descriptor address set */
651 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000652 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000653 if (sh_eth_is_gether(mdp))
654 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900655 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700656 }
657
658 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
659
660 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400661 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700662
663 memset(mdp->tx_ring, 0, tx_ringsize);
664
665 /* build Tx ring buffer */
666 for (i = 0; i < TX_RING_SIZE; i++) {
667 mdp->tx_skbuff[i] = NULL;
668 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400669 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700670 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900671 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400672 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000673 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000674 if (sh_eth_is_gether(mdp))
675 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900676 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700677 }
678
Yoshinori Sato71557a32008-08-06 19:49:00 -0400679 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700680}
681
682/* Get skb and descriptor buffer */
683static int sh_eth_ring_init(struct net_device *ndev)
684{
685 struct sh_eth_private *mdp = netdev_priv(ndev);
686 int rx_ringsize, tx_ringsize, ret = 0;
687
688 /*
689 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
690 * card needs room to do 8 byte alignment, +2 so we can reserve
691 * the first 2 bytes, and +16 gets room for the status word from the
692 * card.
693 */
694 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
695 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800696 if (mdp->cd->rpadir)
697 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700698
699 /* Allocate RX and TX skb rings */
700 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
701 GFP_KERNEL);
702 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000703 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700704 ret = -ENOMEM;
705 return ret;
706 }
707
708 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
709 GFP_KERNEL);
710 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000711 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700712 ret = -ENOMEM;
713 goto skb_ring_free;
714 }
715
716 /* Allocate all Rx descriptors. */
717 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
718 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
719 GFP_KERNEL);
720
721 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000722 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
723 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700724 ret = -ENOMEM;
725 goto desc_ring_free;
726 }
727
728 mdp->dirty_rx = 0;
729
730 /* Allocate all Tx descriptors. */
731 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
732 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
733 GFP_KERNEL);
734 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000735 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
736 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700737 ret = -ENOMEM;
738 goto desc_ring_free;
739 }
740 return ret;
741
742desc_ring_free:
743 /* free DMA buffer */
744 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
745
746skb_ring_free:
747 /* Free Rx and Tx skb ring buffer */
748 sh_eth_ring_free(ndev);
749
750 return ret;
751}
752
753static int sh_eth_dev_init(struct net_device *ndev)
754{
755 int ret = 0;
756 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700757 u_int32_t rx_int_var, tx_int_var;
758 u32 val;
759
760 /* Soft Reset */
761 sh_eth_reset(ndev);
762
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900763 /* Descriptor format */
764 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000765 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000766 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700767
768 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000769 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700770
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000771#if defined(__LITTLE_ENDIAN__)
772 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000773 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000774 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900775#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000776 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700777
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900778 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000779 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
780 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700781
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900782 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000783 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700784
785 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
786 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000787 sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700788
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000789 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000790 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900791
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000792 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900793
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000794 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000795 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700796
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900797 /* Recv frame limit set register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000798 sh_eth_write(ndev, RFLR_VALUE, RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700799
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000800 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
801 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700802
803 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000804 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700805 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
806
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000807 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900808
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000809 if (mdp->cd->set_rate)
810 mdp->cd->set_rate(ndev);
811
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900812 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000813 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900814
815 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000816 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700817
818 /* Set MAC address */
819 update_mac_address(ndev);
820
821 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000822 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000823 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000824 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000825 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000826 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000827 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900828
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700829 /* Setting the Rx mode will start the Rx process. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000830 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700831
832 netif_start_queue(ndev);
833
834 return ret;
835}
836
837/* free Tx skb function */
838static int sh_eth_txfree(struct net_device *ndev)
839{
840 struct sh_eth_private *mdp = netdev_priv(ndev);
841 struct sh_eth_txdesc *txdesc;
842 int freeNum = 0;
843 int entry = 0;
844
845 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
846 entry = mdp->dirty_tx % TX_RING_SIZE;
847 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400848 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700849 break;
850 /* Free the original skb. */
851 if (mdp->tx_skbuff[entry]) {
852 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
853 mdp->tx_skbuff[entry] = NULL;
854 freeNum++;
855 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400856 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700857 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400858 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700859
860 mdp->stats.tx_packets++;
861 mdp->stats.tx_bytes += txdesc->buffer_length;
862 }
863 return freeNum;
864}
865
866/* Packet receive function */
867static int sh_eth_rx(struct net_device *ndev)
868{
869 struct sh_eth_private *mdp = netdev_priv(ndev);
870 struct sh_eth_rxdesc *rxdesc;
871
872 int entry = mdp->cur_rx % RX_RING_SIZE;
873 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
874 struct sk_buff *skb;
875 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000876 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700877
878 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400879 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
880 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700881 pkt_len = rxdesc->frame_length;
882
883 if (--boguscnt < 0)
884 break;
885
886 if (!(desc_status & RDFEND))
887 mdp->stats.rx_length_errors++;
888
889 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
890 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
891 mdp->stats.rx_errors++;
892 if (desc_status & RD_RFS1)
893 mdp->stats.rx_crc_errors++;
894 if (desc_status & RD_RFS2)
895 mdp->stats.rx_frame_errors++;
896 if (desc_status & RD_RFS3)
897 mdp->stats.rx_length_errors++;
898 if (desc_status & RD_RFS4)
899 mdp->stats.rx_length_errors++;
900 if (desc_status & RD_RFS6)
901 mdp->stats.rx_missed_errors++;
902 if (desc_status & RD_RFS10)
903 mdp->stats.rx_over_errors++;
904 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000905 if (!mdp->cd->hw_swap)
906 sh_eth_soft_swap(
907 phys_to_virt(ALIGN(rxdesc->addr, 4)),
908 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700909 skb = mdp->rx_skbuff[entry];
910 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -0800911 if (mdp->cd->rpadir)
912 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700913 skb_put(skb, pkt_len);
914 skb->protocol = eth_type_trans(skb, ndev);
915 netif_rx(skb);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700916 mdp->stats.rx_packets++;
917 mdp->stats.rx_bytes += pkt_len;
918 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400919 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700920 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000921 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700922 }
923
924 /* Refill the Rx ring buffers. */
925 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
926 entry = mdp->dirty_rx % RX_RING_SIZE;
927 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900928 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000929 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900930
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700931 if (mdp->rx_skbuff[entry] == NULL) {
932 skb = dev_alloc_skb(mdp->rx_buf_sz);
933 mdp->rx_skbuff[entry] = skb;
934 if (skb == NULL)
935 break; /* Better luck next round. */
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000936 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
937 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700938 skb->dev = ndev;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000939 sh_eth_set_receive_align(skb);
940
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700941 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000942 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700943 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700944 if (entry >= RX_RING_SIZE - 1)
945 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400946 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700947 else
948 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400949 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700950 }
951
952 /* Restart Rx engine if stopped. */
953 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000954 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R))
955 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956
957 return 0;
958}
959
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000960static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000961{
962 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000963 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
964 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000965}
966
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000967static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000968{
969 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000970 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
971 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000972}
973
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700974/* error control function */
975static void sh_eth_error(struct net_device *ndev, int intr_status)
976{
977 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700978 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000979 u32 link_stat;
980 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700981
982 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000983 felic_stat = sh_eth_read(ndev, ECSR);
984 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700985 if (felic_stat & ECSR_ICD)
986 mdp->stats.tx_carrier_errors++;
987 if (felic_stat & ECSR_LCHNG) {
988 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000989 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000990 if (mdp->link == PHY_DOWN)
991 link_stat = 0;
992 else
993 link_stat = PHY_ST_LINK;
994 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000995 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000996 if (mdp->ether_link_active_low)
997 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000998 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000999 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001000 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001001 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001002 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001003 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1004 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001005 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001006 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1007 ECSR);
1008 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1009 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001010 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001011 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001012 }
1013 }
1014 }
1015
1016 if (intr_status & EESR_TWB) {
1017 /* Write buck end. unused write back interrupt */
1018 if (intr_status & EESR_TABT) /* Transmit Abort int */
1019 mdp->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001020 if (netif_msg_tx_err(mdp))
1021 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001022 }
1023
1024 if (intr_status & EESR_RABT) {
1025 /* Receive Abort int */
1026 if (intr_status & EESR_RFRMER) {
1027 /* Receive Frame Overflow int */
1028 mdp->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001029 if (netif_msg_rx_err(mdp))
1030 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001031 }
1032 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001033
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001034 if (intr_status & EESR_TDE) {
1035 /* Transmit Descriptor Empty int */
1036 mdp->stats.tx_fifo_errors++;
1037 if (netif_msg_tx_err(mdp))
1038 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1039 }
1040
1041 if (intr_status & EESR_TFE) {
1042 /* FIFO under flow */
1043 mdp->stats.tx_fifo_errors++;
1044 if (netif_msg_tx_err(mdp))
1045 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001046 }
1047
1048 if (intr_status & EESR_RDE) {
1049 /* Receive Descriptor Empty int */
1050 mdp->stats.rx_over_errors++;
1051
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001052 if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R)
1053 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001054 if (netif_msg_rx_err(mdp))
1055 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001056 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001057
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001058 if (intr_status & EESR_RFE) {
1059 /* Receive FIFO Overflow int */
1060 mdp->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001061 if (netif_msg_rx_err(mdp))
1062 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1063 }
1064
1065 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1066 /* Address Error */
1067 mdp->stats.tx_fifo_errors++;
1068 if (netif_msg_tx_err(mdp))
1069 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001070 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001071
1072 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1073 if (mdp->cd->no_ade)
1074 mask &= ~EESR_ADE;
1075 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001076 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001077 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001079 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1080 intr_status, mdp->cur_tx);
1081 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001082 mdp->dirty_tx, (u32) ndev->state, edtrr);
1083 /* dirty buffer free */
1084 sh_eth_txfree(ndev);
1085
1086 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001087 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001088 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001089 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001090 }
1091 /* wakeup */
1092 netif_wake_queue(ndev);
1093 }
1094}
1095
1096static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1097{
1098 struct net_device *ndev = netdev;
1099 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001100 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001101 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001102 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001103
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001104 spin_lock(&mdp->lock);
1105
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001106 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001107 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001108 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001109 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1110 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001111 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001112 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001113 ret = IRQ_HANDLED;
1114 } else
1115 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001116
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001117 if (intr_status & (EESR_FRC | /* Frame recv*/
1118 EESR_RMAF | /* Multi cast address recv*/
1119 EESR_RRF | /* Bit frame recv */
1120 EESR_RTLF | /* Long frame recv*/
1121 EESR_RTSF | /* short frame recv */
1122 EESR_PRE | /* PHY-LSI recv error */
1123 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001124 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001125 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001127 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001128 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001129 sh_eth_txfree(ndev);
1130 netif_wake_queue(ndev);
1131 }
1132
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001133 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001134 sh_eth_error(ndev, intr_status);
1135
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001136other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001137 spin_unlock(&mdp->lock);
1138
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001139 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001140}
1141
1142static void sh_eth_timer(unsigned long data)
1143{
1144 struct net_device *ndev = (struct net_device *)data;
1145 struct sh_eth_private *mdp = netdev_priv(ndev);
1146
1147 mod_timer(&mdp->timer, jiffies + (10 * HZ));
1148}
1149
1150/* PHY state control function */
1151static void sh_eth_adjust_link(struct net_device *ndev)
1152{
1153 struct sh_eth_private *mdp = netdev_priv(ndev);
1154 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155 int new_state = 0;
1156
1157 if (phydev->link != PHY_DOWN) {
1158 if (phydev->duplex != mdp->duplex) {
1159 new_state = 1;
1160 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001161 if (mdp->cd->set_duplex)
1162 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001163 }
1164
1165 if (phydev->speed != mdp->speed) {
1166 new_state = 1;
1167 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001168 if (mdp->cd->set_rate)
1169 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170 }
1171 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001172 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_TXF)
1173 | ECMR_DM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001174 new_state = 1;
1175 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001176 }
1177 } else if (mdp->link) {
1178 new_state = 1;
1179 mdp->link = PHY_DOWN;
1180 mdp->speed = 0;
1181 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182 }
1183
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001184 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185 phy_print_status(phydev);
1186}
1187
1188/* PHY init function */
1189static int sh_eth_phy_init(struct net_device *ndev)
1190{
1191 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001192 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001193 struct phy_device *phydev = NULL;
1194
Kay Sieversfb28ad32008-11-10 13:55:14 -08001195 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001196 mdp->mii_bus->id , mdp->phy_id);
1197
1198 mdp->link = PHY_DOWN;
1199 mdp->speed = 0;
1200 mdp->duplex = -1;
1201
1202 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001203 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001204 0, mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001205 if (IS_ERR(phydev)) {
1206 dev_err(&ndev->dev, "phy_connect failed\n");
1207 return PTR_ERR(phydev);
1208 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001209
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001210 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001211 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212
1213 mdp->phydev = phydev;
1214
1215 return 0;
1216}
1217
1218/* PHY control start function */
1219static int sh_eth_phy_start(struct net_device *ndev)
1220{
1221 struct sh_eth_private *mdp = netdev_priv(ndev);
1222 int ret;
1223
1224 ret = sh_eth_phy_init(ndev);
1225 if (ret)
1226 return ret;
1227
1228 /* reset phy - this also wakes it from PDOWN */
1229 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1230 phy_start(mdp->phydev);
1231
1232 return 0;
1233}
1234
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001235static int sh_eth_get_settings(struct net_device *ndev,
1236 struct ethtool_cmd *ecmd)
1237{
1238 struct sh_eth_private *mdp = netdev_priv(ndev);
1239 unsigned long flags;
1240 int ret;
1241
1242 spin_lock_irqsave(&mdp->lock, flags);
1243 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1244 spin_unlock_irqrestore(&mdp->lock, flags);
1245
1246 return ret;
1247}
1248
1249static int sh_eth_set_settings(struct net_device *ndev,
1250 struct ethtool_cmd *ecmd)
1251{
1252 struct sh_eth_private *mdp = netdev_priv(ndev);
1253 unsigned long flags;
1254 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001255
1256 spin_lock_irqsave(&mdp->lock, flags);
1257
1258 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001259 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001260
1261 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1262 if (ret)
1263 goto error_exit;
1264
1265 if (ecmd->duplex == DUPLEX_FULL)
1266 mdp->duplex = 1;
1267 else
1268 mdp->duplex = 0;
1269
1270 if (mdp->cd->set_duplex)
1271 mdp->cd->set_duplex(ndev);
1272
1273error_exit:
1274 mdelay(1);
1275
1276 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001277 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001278
1279 spin_unlock_irqrestore(&mdp->lock, flags);
1280
1281 return ret;
1282}
1283
1284static int sh_eth_nway_reset(struct net_device *ndev)
1285{
1286 struct sh_eth_private *mdp = netdev_priv(ndev);
1287 unsigned long flags;
1288 int ret;
1289
1290 spin_lock_irqsave(&mdp->lock, flags);
1291 ret = phy_start_aneg(mdp->phydev);
1292 spin_unlock_irqrestore(&mdp->lock, flags);
1293
1294 return ret;
1295}
1296
1297static u32 sh_eth_get_msglevel(struct net_device *ndev)
1298{
1299 struct sh_eth_private *mdp = netdev_priv(ndev);
1300 return mdp->msg_enable;
1301}
1302
1303static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1304{
1305 struct sh_eth_private *mdp = netdev_priv(ndev);
1306 mdp->msg_enable = value;
1307}
1308
1309static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1310 "rx_current", "tx_current",
1311 "rx_dirty", "tx_dirty",
1312};
1313#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1314
1315static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1316{
1317 switch (sset) {
1318 case ETH_SS_STATS:
1319 return SH_ETH_STATS_LEN;
1320 default:
1321 return -EOPNOTSUPP;
1322 }
1323}
1324
1325static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1326 struct ethtool_stats *stats, u64 *data)
1327{
1328 struct sh_eth_private *mdp = netdev_priv(ndev);
1329 int i = 0;
1330
1331 /* device-specific stats */
1332 data[i++] = mdp->cur_rx;
1333 data[i++] = mdp->cur_tx;
1334 data[i++] = mdp->dirty_rx;
1335 data[i++] = mdp->dirty_tx;
1336}
1337
1338static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1339{
1340 switch (stringset) {
1341 case ETH_SS_STATS:
1342 memcpy(data, *sh_eth_gstrings_stats,
1343 sizeof(sh_eth_gstrings_stats));
1344 break;
1345 }
1346}
1347
1348static struct ethtool_ops sh_eth_ethtool_ops = {
1349 .get_settings = sh_eth_get_settings,
1350 .set_settings = sh_eth_set_settings,
1351 .nway_reset = sh_eth_nway_reset,
1352 .get_msglevel = sh_eth_get_msglevel,
1353 .set_msglevel = sh_eth_set_msglevel,
1354 .get_link = ethtool_op_get_link,
1355 .get_strings = sh_eth_get_strings,
1356 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1357 .get_sset_count = sh_eth_get_sset_count,
1358};
1359
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001360/* network device open function */
1361static int sh_eth_open(struct net_device *ndev)
1362{
1363 int ret = 0;
1364 struct sh_eth_private *mdp = netdev_priv(ndev);
1365
Magnus Dammbcd51492009-10-09 00:20:04 +00001366 pm_runtime_get_sync(&mdp->pdev->dev);
1367
Joe Perchesa0607fd2009-11-18 23:29:17 -08001368 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001369#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001370 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1371 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001372 IRQF_SHARED,
1373#else
1374 0,
1375#endif
1376 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001378 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001379 return ret;
1380 }
1381
1382 /* Descriptor set */
1383 ret = sh_eth_ring_init(ndev);
1384 if (ret)
1385 goto out_free_irq;
1386
1387 /* device init */
1388 ret = sh_eth_dev_init(ndev);
1389 if (ret)
1390 goto out_free_irq;
1391
1392 /* PHY control start*/
1393 ret = sh_eth_phy_start(ndev);
1394 if (ret)
1395 goto out_free_irq;
1396
1397 /* Set the timer to check for link beat. */
1398 init_timer(&mdp->timer);
1399 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001400 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401
1402 return ret;
1403
1404out_free_irq:
1405 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001406 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001407 return ret;
1408}
1409
1410/* Timeout function */
1411static void sh_eth_tx_timeout(struct net_device *ndev)
1412{
1413 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001414 struct sh_eth_rxdesc *rxdesc;
1415 int i;
1416
1417 netif_stop_queue(ndev);
1418
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001419 if (netif_msg_timer(mdp))
1420 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001421 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422
1423 /* tx_errors count up */
1424 mdp->stats.tx_errors++;
1425
1426 /* timer off */
1427 del_timer_sync(&mdp->timer);
1428
1429 /* Free all the skbuffs in the Rx queue. */
1430 for (i = 0; i < RX_RING_SIZE; i++) {
1431 rxdesc = &mdp->rx_ring[i];
1432 rxdesc->status = 0;
1433 rxdesc->addr = 0xBADF00D0;
1434 if (mdp->rx_skbuff[i])
1435 dev_kfree_skb(mdp->rx_skbuff[i]);
1436 mdp->rx_skbuff[i] = NULL;
1437 }
1438 for (i = 0; i < TX_RING_SIZE; i++) {
1439 if (mdp->tx_skbuff[i])
1440 dev_kfree_skb(mdp->tx_skbuff[i]);
1441 mdp->tx_skbuff[i] = NULL;
1442 }
1443
1444 /* device init */
1445 sh_eth_dev_init(ndev);
1446
1447 /* timer on */
1448 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1449 add_timer(&mdp->timer);
1450}
1451
1452/* Packet transmit function */
1453static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1454{
1455 struct sh_eth_private *mdp = netdev_priv(ndev);
1456 struct sh_eth_txdesc *txdesc;
1457 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001458 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001459
1460 spin_lock_irqsave(&mdp->lock, flags);
1461 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1462 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001463 if (netif_msg_tx_queued(mdp))
1464 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001465 netif_stop_queue(ndev);
1466 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001467 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001468 }
1469 }
1470 spin_unlock_irqrestore(&mdp->lock, flags);
1471
1472 entry = mdp->cur_tx % TX_RING_SIZE;
1473 mdp->tx_skbuff[entry] = skb;
1474 txdesc = &mdp->tx_ring[entry];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001475 txdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001476 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001477 if (!mdp->cd->hw_swap)
1478 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1479 skb->len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 /* write back */
1481 __flush_purge_region(skb->data, skb->len);
1482 if (skb->len < ETHERSMALL)
1483 txdesc->buffer_length = ETHERSMALL;
1484 else
1485 txdesc->buffer_length = skb->len;
1486
1487 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001488 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001489 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001490 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001491
1492 mdp->cur_tx++;
1493
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001494 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1495 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001496
Patrick McHardy6ed10652009-06-23 06:03:08 +00001497 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001498}
1499
1500/* device close function */
1501static int sh_eth_close(struct net_device *ndev)
1502{
1503 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001504 int ringsize;
1505
1506 netif_stop_queue(ndev);
1507
1508 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001509 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001510
1511 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001512 sh_eth_write(ndev, 0, EDTRR);
1513 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001514
1515 /* PHY Disconnect */
1516 if (mdp->phydev) {
1517 phy_stop(mdp->phydev);
1518 phy_disconnect(mdp->phydev);
1519 }
1520
1521 free_irq(ndev->irq, ndev);
1522
1523 del_timer_sync(&mdp->timer);
1524
1525 /* Free all the skbuffs in the Rx queue. */
1526 sh_eth_ring_free(ndev);
1527
1528 /* free DMA buffer */
1529 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1530 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1531
1532 /* free DMA buffer */
1533 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1534 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1535
Magnus Dammbcd51492009-10-09 00:20:04 +00001536 pm_runtime_put_sync(&mdp->pdev->dev);
1537
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001538 return 0;
1539}
1540
1541static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1542{
1543 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001544
Magnus Dammbcd51492009-10-09 00:20:04 +00001545 pm_runtime_get_sync(&mdp->pdev->dev);
1546
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001547 mdp->stats.tx_dropped += sh_eth_read(ndev, TROCR);
1548 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
1549 mdp->stats.collisions += sh_eth_read(ndev, CDCR);
1550 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
1551 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
1552 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001553 if (sh_eth_is_gether(mdp)) {
1554 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
1555 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
1556 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
1557 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1558 } else {
1559 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
1560 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1561 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001562 pm_runtime_put_sync(&mdp->pdev->dev);
1563
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001564 return &mdp->stats;
1565}
1566
1567/* ioctl to device funciotn*/
1568static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1569 int cmd)
1570{
1571 struct sh_eth_private *mdp = netdev_priv(ndev);
1572 struct phy_device *phydev = mdp->phydev;
1573
1574 if (!netif_running(ndev))
1575 return -EINVAL;
1576
1577 if (!phydev)
1578 return -ENODEV;
1579
Richard Cochran28b04112010-07-17 08:48:55 +00001580 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001581}
1582
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001583#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001584/* Multicast reception directions set */
1585static void sh_eth_set_multicast_list(struct net_device *ndev)
1586{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001587 if (ndev->flags & IFF_PROMISC) {
1588 /* Set promiscuous. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001589 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) |
1590 ECMR_PRM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001591 } else {
1592 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001593 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) |
1594 ECMR_MCT, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595 }
1596}
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001597#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001598
1599/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001600static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001601{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001602 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
1603 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
1604 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
1605 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
1606 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
1607 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
1608 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
1609 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
1610 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
1611 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001612 if (sh_eth_is_gether(mdp)) {
1613 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
1614 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
1615 } else {
1616 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
1617 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
1618 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001619 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
1620 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
1621 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
1622 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
1623 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
1624 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
1625 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001626}
1627
1628/* MDIO bus release function */
1629static int sh_mdio_release(struct net_device *ndev)
1630{
1631 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1632
1633 /* unregister mdio bus */
1634 mdiobus_unregister(bus);
1635
1636 /* remove mdio bus info from net_device */
1637 dev_set_drvdata(&ndev->dev, NULL);
1638
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00001639 /* free interrupts memory */
1640 kfree(bus->irq);
1641
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001642 /* free bitbang info */
1643 free_mdio_bitbang(bus);
1644
1645 return 0;
1646}
1647
1648/* MDIO bus init function */
1649static int sh_mdio_init(struct net_device *ndev, int id)
1650{
1651 int ret, i;
1652 struct bb_info *bitbang;
1653 struct sh_eth_private *mdp = netdev_priv(ndev);
1654
1655 /* create bit control struct for PHY */
1656 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1657 if (!bitbang) {
1658 ret = -ENOMEM;
1659 goto out;
1660 }
1661
1662 /* bitbang init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001663 bitbang->addr = ndev->base_addr + mdp->reg_offset[PIR];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001664 bitbang->mdi_msk = 0x08;
1665 bitbang->mdo_msk = 0x04;
1666 bitbang->mmd_msk = 0x02;/* MMD */
1667 bitbang->mdc_msk = 0x01;
1668 bitbang->ctrl.ops = &bb_ops;
1669
Stefan Weilc2e07b32010-08-03 19:44:52 +02001670 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001671 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1672 if (!mdp->mii_bus) {
1673 ret = -ENOMEM;
1674 goto out_free_bitbang;
1675 }
1676
1677 /* Hook up MII support for ethtool */
1678 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001679 mdp->mii_bus->parent = &ndev->dev;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001680 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001681
1682 /* PHY IRQ */
1683 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1684 if (!mdp->mii_bus->irq) {
1685 ret = -ENOMEM;
1686 goto out_free_bus;
1687 }
1688
1689 for (i = 0; i < PHY_MAX_ADDR; i++)
1690 mdp->mii_bus->irq[i] = PHY_POLL;
1691
1692 /* regist mdio bus */
1693 ret = mdiobus_register(mdp->mii_bus);
1694 if (ret)
1695 goto out_free_irq;
1696
1697 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1698
1699 return 0;
1700
1701out_free_irq:
1702 kfree(mdp->mii_bus->irq);
1703
1704out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001705 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001706
1707out_free_bitbang:
1708 kfree(bitbang);
1709
1710out:
1711 return ret;
1712}
1713
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001714static const u16 *sh_eth_get_register_offset(int register_type)
1715{
1716 const u16 *reg_offset = NULL;
1717
1718 switch (register_type) {
1719 case SH_ETH_REG_GIGABIT:
1720 reg_offset = sh_eth_offset_gigabit;
1721 break;
1722 case SH_ETH_REG_FAST_SH4:
1723 reg_offset = sh_eth_offset_fast_sh4;
1724 break;
1725 case SH_ETH_REG_FAST_SH3_SH2:
1726 reg_offset = sh_eth_offset_fast_sh3_sh2;
1727 break;
1728 default:
1729 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
1730 break;
1731 }
1732
1733 return reg_offset;
1734}
1735
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001736static const struct net_device_ops sh_eth_netdev_ops = {
1737 .ndo_open = sh_eth_open,
1738 .ndo_stop = sh_eth_close,
1739 .ndo_start_xmit = sh_eth_start_xmit,
1740 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001741#if defined(SH_ETH_HAS_TSU)
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001742 .ndo_set_multicast_list = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001743#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001744 .ndo_tx_timeout = sh_eth_tx_timeout,
1745 .ndo_do_ioctl = sh_eth_do_ioctl,
1746 .ndo_validate_addr = eth_validate_addr,
1747 .ndo_set_mac_address = eth_mac_addr,
1748 .ndo_change_mtu = eth_change_mtu,
1749};
1750
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001751static int sh_eth_drv_probe(struct platform_device *pdev)
1752{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07001753 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001754 struct resource *res;
1755 struct net_device *ndev = NULL;
1756 struct sh_eth_private *mdp;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001757 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001758
1759 /* get base addr */
1760 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1761 if (unlikely(res == NULL)) {
1762 dev_err(&pdev->dev, "invalid resource\n");
1763 ret = -EINVAL;
1764 goto out;
1765 }
1766
1767 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1768 if (!ndev) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001769 dev_err(&pdev->dev, "Could not allocate device.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001770 ret = -ENOMEM;
1771 goto out;
1772 }
1773
1774 /* The sh Ether-specific entries in the device structure. */
1775 ndev->base_addr = res->start;
1776 devno = pdev->id;
1777 if (devno < 0)
1778 devno = 0;
1779
1780 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001781 ret = platform_get_irq(pdev, 0);
1782 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001783 ret = -ENODEV;
1784 goto out_release;
1785 }
roel kluincc3c0802008-09-10 19:22:44 +02001786 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001787
1788 SET_NETDEV_DEV(ndev, &pdev->dev);
1789
1790 /* Fill in the fields of the device structure with ethernet values. */
1791 ether_setup(ndev);
1792
1793 mdp = netdev_priv(ndev);
1794 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001795 mdp->pdev = pdev;
1796 pm_runtime_enable(&pdev->dev);
1797 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001798
Yoshinori Sato71557a32008-08-06 19:49:00 -04001799 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001801 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001802 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001803 /* EDMAC endian */
1804 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001805 mdp->no_ether_link = pd->no_ether_link;
1806 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001807 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001808
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001809 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001810#if defined(SH_ETH_HAS_BOTH_MODULES)
1811 mdp->cd = sh_eth_get_cpu_data(mdp);
1812#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001813 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001814#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001815 sh_eth_set_default_cpu_data(mdp->cd);
1816
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001817 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001818 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001819 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001820 ndev->watchdog_timeo = TX_TIMEOUT;
1821
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001822 /* debug message level */
1823 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001824 mdp->post_rx = POST_RX >> (devno << 1);
1825 mdp->post_fw = POST_FW >> (devno << 1);
1826
1827 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001828 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001829
1830 /* First device only init */
1831 if (!devno) {
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001832 if (mdp->cd->tsu) {
1833 struct resource *rtsu;
1834 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1835 if (!rtsu) {
1836 dev_err(&pdev->dev, "Not found TSU resource\n");
1837 goto out_release;
1838 }
1839 mdp->tsu_addr = ioremap(rtsu->start,
1840 resource_size(rtsu));
1841 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001842 if (mdp->cd->chip_reset)
1843 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001844
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001845 if (mdp->cd->tsu) {
1846 /* TSU init (Init only)*/
1847 sh_eth_tsu_init(mdp);
1848 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001849 }
1850
1851 /* network device register */
1852 ret = register_netdev(ndev);
1853 if (ret)
1854 goto out_release;
1855
1856 /* mdio bus init */
1857 ret = sh_mdio_init(ndev, pdev->id);
1858 if (ret)
1859 goto out_unregister;
1860
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08001861 /* print device infomation */
1862 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1863 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001864
1865 platform_set_drvdata(pdev, ndev);
1866
1867 return ret;
1868
1869out_unregister:
1870 unregister_netdev(ndev);
1871
1872out_release:
1873 /* net_dev free */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001874 if (mdp->tsu_addr)
1875 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001876 if (ndev)
1877 free_netdev(ndev);
1878
1879out:
1880 return ret;
1881}
1882
1883static int sh_eth_drv_remove(struct platform_device *pdev)
1884{
1885 struct net_device *ndev = platform_get_drvdata(pdev);
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001886 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001887
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001888 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001889 sh_mdio_release(ndev);
1890 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001891 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001892 free_netdev(ndev);
1893 platform_set_drvdata(pdev, NULL);
1894
1895 return 0;
1896}
1897
Magnus Dammbcd51492009-10-09 00:20:04 +00001898static int sh_eth_runtime_nop(struct device *dev)
1899{
1900 /*
1901 * Runtime PM callback shared between ->runtime_suspend()
1902 * and ->runtime_resume(). Simply returns success.
1903 *
1904 * This driver re-initializes all registers after
1905 * pm_runtime_get_sync() anyway so there is no need
1906 * to save and restore registers here.
1907 */
1908 return 0;
1909}
1910
1911static struct dev_pm_ops sh_eth_dev_pm_ops = {
1912 .runtime_suspend = sh_eth_runtime_nop,
1913 .runtime_resume = sh_eth_runtime_nop,
1914};
1915
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001916static struct platform_driver sh_eth_driver = {
1917 .probe = sh_eth_drv_probe,
1918 .remove = sh_eth_drv_remove,
1919 .driver = {
1920 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001921 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001922 },
1923};
1924
1925static int __init sh_eth_init(void)
1926{
1927 return platform_driver_register(&sh_eth_driver);
1928}
1929
1930static void __exit sh_eth_cleanup(void)
1931{
1932 platform_driver_unregister(&sh_eth_driver);
1933}
1934
1935module_init(sh_eth_init);
1936module_exit(sh_eth_cleanup);
1937
1938MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1939MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1940MODULE_LICENSE("GPL v2");