]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/ethernet/renesas/sh_eth.c
net: sh_eth: remove unnecessary members/definitions
[mirror_ubuntu-zesty-kernel.git] / drivers / net / ethernet / renesas / sh_eth.c
CommitLineData
86a74ff2
NI
1/*
2 * SuperH Ethernet device driver
3 *
f0e81fec
NI
4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2012 Renesas Solutions Corp.
86a74ff2
NI
6 *
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
86a74ff2 23#include <linux/init.h>
0654011d
YS
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
6a27cded 27#include <linux/interrupt.h>
86a74ff2
NI
28#include <linux/dma-mapping.h>
29#include <linux/etherdevice.h>
30#include <linux/delay.h>
31#include <linux/platform_device.h>
32#include <linux/mdio-bitbang.h>
33#include <linux/netdevice.h>
34#include <linux/phy.h>
35#include <linux/cache.h>
36#include <linux/io.h>
bcd5149d 37#include <linux/pm_runtime.h>
5a0e3ad6 38#include <linux/slab.h>
dc19e4e5 39#include <linux/ethtool.h>
fdb37a7f 40#include <linux/if_vlan.h>
f0e81fec 41#include <linux/clk.h>
d4fa0e35 42#include <linux/sh_eth.h>
86a74ff2
NI
43
44#include "sh_eth.h"
45
dc19e4e5
NI
46#define SH_ETH_DEF_MSG_ENABLE \
47 (NETIF_MSG_LINK | \
48 NETIF_MSG_TIMER | \
49 NETIF_MSG_RX_ERR| \
50 NETIF_MSG_TX_ERR)
51
5e7a76be
NI
52#if defined(CONFIG_CPU_SUBTYPE_SH7734) || \
53 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
54 defined(CONFIG_ARCH_R8A7740)
55static void sh_eth_select_mii(struct net_device *ndev)
56{
57 u32 value = 0x0;
58 struct sh_eth_private *mdp = netdev_priv(ndev);
59
60 switch (mdp->phy_interface) {
61 case PHY_INTERFACE_MODE_GMII:
62 value = 0x2;
63 break;
64 case PHY_INTERFACE_MODE_MII:
65 value = 0x1;
66 break;
67 case PHY_INTERFACE_MODE_RMII:
68 value = 0x0;
69 break;
70 default:
71 pr_warn("PHY interface mode was not setup. Set to MII.\n");
72 value = 0x1;
73 break;
74 }
75
76 sh_eth_write(ndev, value, RMII_MII);
77}
78#endif
79
380af9e3 80/* There is CPU dependent code */
65ac8851
YS
81#if defined(CONFIG_CPU_SUBTYPE_SH7724)
82#define SH_ETH_RESET_DEFAULT 1
83static void sh_eth_set_duplex(struct net_device *ndev)
84{
85 struct sh_eth_private *mdp = netdev_priv(ndev);
65ac8851
YS
86
87 if (mdp->duplex) /* Full */
4a55530f 88 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
65ac8851 89 else /* Half */
4a55530f 90 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
65ac8851
YS
91}
92
93static void sh_eth_set_rate(struct net_device *ndev)
94{
95 struct sh_eth_private *mdp = netdev_priv(ndev);
65ac8851
YS
96
97 switch (mdp->speed) {
98 case 10: /* 10BASE */
4a55530f 99 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
65ac8851
YS
100 break;
101 case 100:/* 100BASE */
4a55530f 102 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
65ac8851
YS
103 break;
104 default:
105 break;
106 }
107}
108
109/* SH7724 */
110static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
111 .set_duplex = sh_eth_set_duplex,
112 .set_rate = sh_eth_set_rate,
113
114 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
115 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
116 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
117
118 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
119 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
120 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
121 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
122
123 .apr = 1,
124 .mpr = 1,
125 .tpauser = 1,
126 .hw_swap = 1,
503914cf
MD
127 .rpadir = 1,
128 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
65ac8851 129};
f29a3d04 130#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
8fcd4961
YS
131#define SH_ETH_HAS_BOTH_MODULES 1
132#define SH_ETH_HAS_TSU 1
5cee1d37
NI
133static int sh_eth_check_reset(struct net_device *ndev);
134
f29a3d04
YS
135static void sh_eth_set_duplex(struct net_device *ndev)
136{
137 struct sh_eth_private *mdp = netdev_priv(ndev);
f29a3d04
YS
138
139 if (mdp->duplex) /* Full */
4a55530f 140 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
f29a3d04 141 else /* Half */
4a55530f 142 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
f29a3d04
YS
143}
144
145static void sh_eth_set_rate(struct net_device *ndev)
146{
147 struct sh_eth_private *mdp = netdev_priv(ndev);
f29a3d04
YS
148
149 switch (mdp->speed) {
150 case 10: /* 10BASE */
4a55530f 151 sh_eth_write(ndev, 0, RTRATE);
f29a3d04
YS
152 break;
153 case 100:/* 100BASE */
4a55530f 154 sh_eth_write(ndev, 1, RTRATE);
f29a3d04
YS
155 break;
156 default:
157 break;
158 }
159}
160
161/* SH7757 */
162static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
163 .set_duplex = sh_eth_set_duplex,
164 .set_rate = sh_eth_set_rate,
165
166 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
167 .rmcr_value = 0x00000001,
168
169 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
170 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
171 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
172 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
173
174 .apr = 1,
175 .mpr = 1,
176 .tpauser = 1,
177 .hw_swap = 1,
178 .no_ade = 1,
2e98e797
YS
179 .rpadir = 1,
180 .rpadir_value = 2 << 16,
f29a3d04 181};
65ac8851 182
8fcd4961
YS
183#define SH_GIGA_ETH_BASE 0xfee00000
184#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
185#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
186static void sh_eth_chip_reset_giga(struct net_device *ndev)
187{
188 int i;
189 unsigned long mahr[2], malr[2];
190
191 /* save MAHR and MALR */
192 for (i = 0; i < 2; i++) {
ae70644d
YS
193 malr[i] = ioread32((void *)GIGA_MALR(i));
194 mahr[i] = ioread32((void *)GIGA_MAHR(i));
8fcd4961
YS
195 }
196
197 /* reset device */
ae70644d 198 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
8fcd4961
YS
199 mdelay(1);
200
201 /* restore MAHR and MALR */
202 for (i = 0; i < 2; i++) {
ae70644d
YS
203 iowrite32(malr[i], (void *)GIGA_MALR(i));
204 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
8fcd4961
YS
205 }
206}
207
208static int sh_eth_is_gether(struct sh_eth_private *mdp);
5cee1d37 209static int sh_eth_reset(struct net_device *ndev)
8fcd4961
YS
210{
211 struct sh_eth_private *mdp = netdev_priv(ndev);
5cee1d37 212 int ret = 0;
8fcd4961
YS
213
214 if (sh_eth_is_gether(mdp)) {
215 sh_eth_write(ndev, 0x03, EDSR);
216 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
217 EDMR);
5cee1d37
NI
218
219 ret = sh_eth_check_reset(ndev);
220 if (ret)
221 goto out;
8fcd4961
YS
222
223 /* Table Init */
224 sh_eth_write(ndev, 0x0, TDLAR);
225 sh_eth_write(ndev, 0x0, TDFAR);
226 sh_eth_write(ndev, 0x0, TDFXR);
227 sh_eth_write(ndev, 0x0, TDFFR);
228 sh_eth_write(ndev, 0x0, RDLAR);
229 sh_eth_write(ndev, 0x0, RDFAR);
230 sh_eth_write(ndev, 0x0, RDFXR);
231 sh_eth_write(ndev, 0x0, RDFFR);
232 } else {
233 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
234 EDMR);
235 mdelay(3);
236 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
237 EDMR);
238 }
5cee1d37
NI
239
240out:
241 return ret;
8fcd4961
YS
242}
243
244static void sh_eth_set_duplex_giga(struct net_device *ndev)
245{
246 struct sh_eth_private *mdp = netdev_priv(ndev);
247
248 if (mdp->duplex) /* Full */
249 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
250 else /* Half */
251 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
252}
253
254static void sh_eth_set_rate_giga(struct net_device *ndev)
255{
256 struct sh_eth_private *mdp = netdev_priv(ndev);
257
258 switch (mdp->speed) {
259 case 10: /* 10BASE */
260 sh_eth_write(ndev, 0x00000000, GECMR);
261 break;
262 case 100:/* 100BASE */
263 sh_eth_write(ndev, 0x00000010, GECMR);
264 break;
265 case 1000: /* 1000BASE */
266 sh_eth_write(ndev, 0x00000020, GECMR);
267 break;
268 default:
269 break;
270 }
271}
272
273/* SH7757(GETHERC) */
274static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
275 .chip_reset = sh_eth_chip_reset_giga,
276 .set_duplex = sh_eth_set_duplex_giga,
277 .set_rate = sh_eth_set_rate_giga,
278
279 .ecsr_value = ECSR_ICD | ECSR_MPD,
280 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
281 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
282
283 .tx_check = EESR_TC1 | EESR_FTC,
284 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
285 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
286 EESR_ECI,
287 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
288 EESR_TFE,
289 .fdr_value = 0x0000072f,
290 .rmcr_value = 0x00000001,
291
292 .apr = 1,
293 .mpr = 1,
294 .tpauser = 1,
295 .bculr = 1,
296 .hw_swap = 1,
297 .rpadir = 1,
298 .rpadir_value = 2 << 16,
299 .no_trimd = 1,
300 .no_ade = 1,
3acbc971 301 .tsu = 1,
8fcd4961
YS
302};
303
304static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
305{
306 if (sh_eth_is_gether(mdp))
307 return &sh_eth_my_cpu_data_giga;
308 else
309 return &sh_eth_my_cpu_data;
310}
311
f0e81fec 312#elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
380af9e3 313#define SH_ETH_HAS_TSU 1
5cee1d37 314static int sh_eth_check_reset(struct net_device *ndev);
f0e81fec 315static void sh_eth_reset_hw_crc(struct net_device *ndev);
5e7a76be 316
380af9e3
YS
317static void sh_eth_chip_reset(struct net_device *ndev)
318{
4986b996
YS
319 struct sh_eth_private *mdp = netdev_priv(ndev);
320
380af9e3 321 /* reset device */
4986b996 322 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
380af9e3
YS
323 mdelay(1);
324}
325
380af9e3
YS
326static void sh_eth_set_duplex(struct net_device *ndev)
327{
328 struct sh_eth_private *mdp = netdev_priv(ndev);
380af9e3
YS
329
330 if (mdp->duplex) /* Full */
4a55530f 331 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
380af9e3 332 else /* Half */
4a55530f 333 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
380af9e3
YS
334}
335
336static void sh_eth_set_rate(struct net_device *ndev)
337{
338 struct sh_eth_private *mdp = netdev_priv(ndev);
380af9e3
YS
339
340 switch (mdp->speed) {
341 case 10: /* 10BASE */
4a55530f 342 sh_eth_write(ndev, GECMR_10, GECMR);
380af9e3
YS
343 break;
344 case 100:/* 100BASE */
4a55530f 345 sh_eth_write(ndev, GECMR_100, GECMR);
380af9e3
YS
346 break;
347 case 1000: /* 1000BASE */
4a55530f 348 sh_eth_write(ndev, GECMR_1000, GECMR);
380af9e3
YS
349 break;
350 default:
351 break;
352 }
353}
354
355/* sh7763 */
356static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
357 .chip_reset = sh_eth_chip_reset,
358 .set_duplex = sh_eth_set_duplex,
359 .set_rate = sh_eth_set_rate,
360
361 .ecsr_value = ECSR_ICD | ECSR_MPD,
362 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
363 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
364
365 .tx_check = EESR_TC1 | EESR_FTC,
366 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
367 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
368 EESR_ECI,
369 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
370 EESR_TFE,
371
372 .apr = 1,
373 .mpr = 1,
374 .tpauser = 1,
375 .bculr = 1,
376 .hw_swap = 1,
380af9e3
YS
377 .no_trimd = 1,
378 .no_ade = 1,
4986b996 379 .tsu = 1,
f0e81fec
NI
380#if defined(CONFIG_CPU_SUBTYPE_SH7734)
381 .hw_crc = 1,
5e7a76be 382 .select_mii = 1,
f0e81fec 383#endif
380af9e3
YS
384};
385
5cee1d37 386static int sh_eth_reset(struct net_device *ndev)
5e7a76be 387{
5cee1d37 388 int ret = 0;
5e7a76be
NI
389
390 sh_eth_write(ndev, EDSR_ENALL, EDSR);
391 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
5cee1d37
NI
392
393 ret = sh_eth_check_reset(ndev);
394 if (ret)
395 goto out;
5e7a76be
NI
396
397 /* Table Init */
398 sh_eth_write(ndev, 0x0, TDLAR);
399 sh_eth_write(ndev, 0x0, TDFAR);
400 sh_eth_write(ndev, 0x0, TDFXR);
401 sh_eth_write(ndev, 0x0, TDFFR);
402 sh_eth_write(ndev, 0x0, RDLAR);
403 sh_eth_write(ndev, 0x0, RDFAR);
404 sh_eth_write(ndev, 0x0, RDFXR);
405 sh_eth_write(ndev, 0x0, RDFFR);
406
407 /* Reset HW CRC register */
408 sh_eth_reset_hw_crc(ndev);
409
410 /* Select MII mode */
411 if (sh_eth_my_cpu_data.select_mii)
412 sh_eth_select_mii(ndev);
5cee1d37
NI
413out:
414 return ret;
5e7a76be
NI
415}
416
f0e81fec
NI
417static void sh_eth_reset_hw_crc(struct net_device *ndev)
418{
419 if (sh_eth_my_cpu_data.hw_crc)
420 sh_eth_write(ndev, 0x0, CSMR);
421}
422
73a0d907
YS
423#elif defined(CONFIG_ARCH_R8A7740)
424#define SH_ETH_HAS_TSU 1
5cee1d37
NI
425static int sh_eth_check_reset(struct net_device *ndev);
426
73a0d907
YS
427static void sh_eth_chip_reset(struct net_device *ndev)
428{
429 struct sh_eth_private *mdp = netdev_priv(ndev);
73a0d907
YS
430
431 /* reset device */
432 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
433 mdelay(1);
434
5e7a76be 435 sh_eth_select_mii(ndev);
73a0d907
YS
436}
437
5cee1d37 438static int sh_eth_reset(struct net_device *ndev)
73a0d907 439{
5cee1d37 440 int ret = 0;
73a0d907
YS
441
442 sh_eth_write(ndev, EDSR_ENALL, EDSR);
443 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
5cee1d37
NI
444
445 ret = sh_eth_check_reset(ndev);
446 if (ret)
447 goto out;
73a0d907
YS
448
449 /* Table Init */
450 sh_eth_write(ndev, 0x0, TDLAR);
451 sh_eth_write(ndev, 0x0, TDFAR);
452 sh_eth_write(ndev, 0x0, TDFXR);
453 sh_eth_write(ndev, 0x0, TDFFR);
454 sh_eth_write(ndev, 0x0, RDLAR);
455 sh_eth_write(ndev, 0x0, RDFAR);
456 sh_eth_write(ndev, 0x0, RDFXR);
457 sh_eth_write(ndev, 0x0, RDFFR);
5cee1d37
NI
458
459out:
460 return ret;
73a0d907
YS
461}
462
463static void sh_eth_set_duplex(struct net_device *ndev)
464{
465 struct sh_eth_private *mdp = netdev_priv(ndev);
466
467 if (mdp->duplex) /* Full */
468 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
469 else /* Half */
470 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
471}
472
473static void sh_eth_set_rate(struct net_device *ndev)
474{
475 struct sh_eth_private *mdp = netdev_priv(ndev);
476
477 switch (mdp->speed) {
478 case 10: /* 10BASE */
479 sh_eth_write(ndev, GECMR_10, GECMR);
480 break;
481 case 100:/* 100BASE */
482 sh_eth_write(ndev, GECMR_100, GECMR);
483 break;
484 case 1000: /* 1000BASE */
485 sh_eth_write(ndev, GECMR_1000, GECMR);
486 break;
487 default:
488 break;
489 }
490}
491
492/* R8A7740 */
493static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
494 .chip_reset = sh_eth_chip_reset,
495 .set_duplex = sh_eth_set_duplex,
496 .set_rate = sh_eth_set_rate,
497
498 .ecsr_value = ECSR_ICD | ECSR_MPD,
499 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
500 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
501
502 .tx_check = EESR_TC1 | EESR_FTC,
503 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
504 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
505 EESR_ECI,
506 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
507 EESR_TFE,
508
509 .apr = 1,
510 .mpr = 1,
511 .tpauser = 1,
512 .bculr = 1,
513 .hw_swap = 1,
514 .no_trimd = 1,
515 .no_ade = 1,
516 .tsu = 1,
5e7a76be 517 .select_mii = 1,
73a0d907
YS
518};
519
380af9e3
YS
520#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
521#define SH_ETH_RESET_DEFAULT 1
522static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
523 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
524
525 .apr = 1,
526 .mpr = 1,
527 .tpauser = 1,
528 .hw_swap = 1,
529};
530#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
531#define SH_ETH_RESET_DEFAULT 1
532#define SH_ETH_HAS_TSU 1
533static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
534 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
4986b996 535 .tsu = 1,
380af9e3
YS
536};
537#endif
538
539static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
540{
541 if (!cd->ecsr_value)
542 cd->ecsr_value = DEFAULT_ECSR_INIT;
543
544 if (!cd->ecsipr_value)
545 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
546
547 if (!cd->fcftr_value)
548 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
549 DEFAULT_FIFO_F_D_RFD;
550
551 if (!cd->fdr_value)
552 cd->fdr_value = DEFAULT_FDR_INIT;
553
554 if (!cd->rmcr_value)
555 cd->rmcr_value = DEFAULT_RMCR_VALUE;
556
557 if (!cd->tx_check)
558 cd->tx_check = DEFAULT_TX_CHECK;
559
560 if (!cd->eesr_err_check)
561 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
562
563 if (!cd->tx_error_check)
564 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
565}
566
567#if defined(SH_ETH_RESET_DEFAULT)
568/* Chip Reset */
5cee1d37 569static int sh_eth_reset(struct net_device *ndev)
380af9e3 570{
c5ed5368 571 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
380af9e3 572 mdelay(3);
c5ed5368 573 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
5cee1d37
NI
574
575 return 0;
576}
577#else
578static int sh_eth_check_reset(struct net_device *ndev)
579{
580 int ret = 0;
581 int cnt = 100;
582
583 while (cnt > 0) {
584 if (!(sh_eth_read(ndev, EDMR) & 0x3))
585 break;
586 mdelay(1);
587 cnt--;
588 }
589 if (cnt < 0) {
590 printk(KERN_ERR "Device reset fail\n");
591 ret = -ETIMEDOUT;
592 }
593 return ret;
380af9e3
YS
594}
595#endif
596
73a0d907 597#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
380af9e3
YS
598static void sh_eth_set_receive_align(struct sk_buff *skb)
599{
600 int reserve;
601
602 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
603 if (reserve)
604 skb_reserve(skb, reserve);
605}
606#else
607static void sh_eth_set_receive_align(struct sk_buff *skb)
608{
609 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
610}
611#endif
612
613
71557a37
YS
614/* CPU <-> EDMAC endian convert */
615static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
616{
617 switch (mdp->edmac_endian) {
618 case EDMAC_LITTLE_ENDIAN:
619 return cpu_to_le32(x);
620 case EDMAC_BIG_ENDIAN:
621 return cpu_to_be32(x);
622 }
623 return x;
624}
625
626static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
627{
628 switch (mdp->edmac_endian) {
629 case EDMAC_LITTLE_ENDIAN:
630 return le32_to_cpu(x);
631 case EDMAC_BIG_ENDIAN:
632 return be32_to_cpu(x);
633 }
634 return x;
635}
636
86a74ff2
NI
637/*
638 * Program the hardware MAC address from dev->dev_addr.
639 */
640static void update_mac_address(struct net_device *ndev)
641{
4a55530f
YS
642 sh_eth_write(ndev,
643 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
644 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
645 sh_eth_write(ndev,
646 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
86a74ff2
NI
647}
648
649/*
650 * Get MAC address from SuperH MAC address register
651 *
652 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
653 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
654 * When you want use this device, you must set MAC address in bootloader.
655 *
656 */
748031f9 657static void read_mac_address(struct net_device *ndev, unsigned char *mac)
86a74ff2 658{
748031f9
MD
659 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
660 memcpy(ndev->dev_addr, mac, 6);
661 } else {
4a55530f
YS
662 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
663 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
664 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
665 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
666 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
667 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
748031f9 668 }
86a74ff2
NI
669}
670
c5ed5368
YS
671static int sh_eth_is_gether(struct sh_eth_private *mdp)
672{
673 if (mdp->reg_offset == sh_eth_offset_gigabit)
674 return 1;
675 else
676 return 0;
677}
678
679static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
680{
681 if (sh_eth_is_gether(mdp))
682 return EDTRR_TRNS_GETHER;
683 else
684 return EDTRR_TRNS_ETHER;
685}
686
86a74ff2 687struct bb_info {
ae70644d 688 void (*set_gate)(void *addr);
86a74ff2 689 struct mdiobb_ctrl ctrl;
ae70644d 690 void *addr;
86a74ff2
NI
691 u32 mmd_msk;/* MMD */
692 u32 mdo_msk;
693 u32 mdi_msk;
694 u32 mdc_msk;
695};
696
697/* PHY bit set */
ae70644d 698static void bb_set(void *addr, u32 msk)
86a74ff2 699{
ae70644d 700 iowrite32(ioread32(addr) | msk, addr);
86a74ff2
NI
701}
702
703/* PHY bit clear */
ae70644d 704static void bb_clr(void *addr, u32 msk)
86a74ff2 705{
ae70644d 706 iowrite32((ioread32(addr) & ~msk), addr);
86a74ff2
NI
707}
708
709/* PHY bit read */
ae70644d 710static int bb_read(void *addr, u32 msk)
86a74ff2 711{
ae70644d 712 return (ioread32(addr) & msk) != 0;
86a74ff2
NI
713}
714
715/* Data I/O pin control */
716static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
717{
718 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
b3017e6a
YS
719
720 if (bitbang->set_gate)
721 bitbang->set_gate(bitbang->addr);
722
86a74ff2
NI
723 if (bit)
724 bb_set(bitbang->addr, bitbang->mmd_msk);
725 else
726 bb_clr(bitbang->addr, bitbang->mmd_msk);
727}
728
729/* Set bit data*/
730static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
731{
732 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
733
b3017e6a
YS
734 if (bitbang->set_gate)
735 bitbang->set_gate(bitbang->addr);
736
86a74ff2
NI
737 if (bit)
738 bb_set(bitbang->addr, bitbang->mdo_msk);
739 else
740 bb_clr(bitbang->addr, bitbang->mdo_msk);
741}
742
743/* Get bit data*/
744static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
745{
746 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
b3017e6a
YS
747
748 if (bitbang->set_gate)
749 bitbang->set_gate(bitbang->addr);
750
86a74ff2
NI
751 return bb_read(bitbang->addr, bitbang->mdi_msk);
752}
753
754/* MDC pin control */
755static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
756{
757 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
758
b3017e6a
YS
759 if (bitbang->set_gate)
760 bitbang->set_gate(bitbang->addr);
761
86a74ff2
NI
762 if (bit)
763 bb_set(bitbang->addr, bitbang->mdc_msk);
764 else
765 bb_clr(bitbang->addr, bitbang->mdc_msk);
766}
767
768/* mdio bus control struct */
769static struct mdiobb_ops bb_ops = {
770 .owner = THIS_MODULE,
771 .set_mdc = sh_mdc_ctrl,
772 .set_mdio_dir = sh_mmd_ctrl,
773 .set_mdio_data = sh_set_mdio,
774 .get_mdio_data = sh_get_mdio,
775};
776
86a74ff2
NI
777/* free skb and descriptor buffer */
778static void sh_eth_ring_free(struct net_device *ndev)
779{
780 struct sh_eth_private *mdp = netdev_priv(ndev);
781 int i;
782
783 /* Free Rx skb ringbuffer */
784 if (mdp->rx_skbuff) {
785 for (i = 0; i < RX_RING_SIZE; i++) {
786 if (mdp->rx_skbuff[i])
787 dev_kfree_skb(mdp->rx_skbuff[i]);
788 }
789 }
790 kfree(mdp->rx_skbuff);
791
792 /* Free Tx skb ringbuffer */
793 if (mdp->tx_skbuff) {
794 for (i = 0; i < TX_RING_SIZE; i++) {
795 if (mdp->tx_skbuff[i])
796 dev_kfree_skb(mdp->tx_skbuff[i]);
797 }
798 }
799 kfree(mdp->tx_skbuff);
800}
801
802/* format skb and descriptor buffer */
803static void sh_eth_ring_format(struct net_device *ndev)
804{
805 struct sh_eth_private *mdp = netdev_priv(ndev);
806 int i;
807 struct sk_buff *skb;
808 struct sh_eth_rxdesc *rxdesc = NULL;
809 struct sh_eth_txdesc *txdesc = NULL;
810 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
811 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
812
813 mdp->cur_rx = mdp->cur_tx = 0;
814 mdp->dirty_rx = mdp->dirty_tx = 0;
815
816 memset(mdp->rx_ring, 0, rx_ringsize);
817
818 /* build Rx ring buffer */
819 for (i = 0; i < RX_RING_SIZE; i++) {
820 /* skb */
821 mdp->rx_skbuff[i] = NULL;
dae2e9f4 822 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
86a74ff2
NI
823 mdp->rx_skbuff[i] = skb;
824 if (skb == NULL)
825 break;
bb7d92e3 826 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
e88aae7b 827 DMA_FROM_DEVICE);
380af9e3
YS
828 sh_eth_set_receive_align(skb);
829
86a74ff2
NI
830 /* RX descriptor */
831 rxdesc = &mdp->rx_ring[i];
0029d64a 832 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
71557a37 833 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
86a74ff2
NI
834
835 /* The size of the buffer is 16 byte boundary. */
0029d64a 836 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
b0ca2a21
NI
837 /* Rx descriptor address set */
838 if (i == 0) {
4a55530f 839 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
c5ed5368
YS
840 if (sh_eth_is_gether(mdp))
841 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
b0ca2a21 842 }
86a74ff2
NI
843 }
844
845 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
846
847 /* Mark the last entry as wrapping the ring. */
71557a37 848 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
86a74ff2
NI
849
850 memset(mdp->tx_ring, 0, tx_ringsize);
851
852 /* build Tx ring buffer */
853 for (i = 0; i < TX_RING_SIZE; i++) {
854 mdp->tx_skbuff[i] = NULL;
855 txdesc = &mdp->tx_ring[i];
71557a37 856 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
86a74ff2 857 txdesc->buffer_length = 0;
b0ca2a21 858 if (i == 0) {
71557a37 859 /* Tx descriptor address set */
4a55530f 860 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
c5ed5368
YS
861 if (sh_eth_is_gether(mdp))
862 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
b0ca2a21 863 }
86a74ff2
NI
864 }
865
71557a37 866 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
86a74ff2
NI
867}
868
869/* Get skb and descriptor buffer */
870static int sh_eth_ring_init(struct net_device *ndev)
871{
872 struct sh_eth_private *mdp = netdev_priv(ndev);
873 int rx_ringsize, tx_ringsize, ret = 0;
874
875 /*
876 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
877 * card needs room to do 8 byte alignment, +2 so we can reserve
878 * the first 2 bytes, and +16 gets room for the status word from the
879 * card.
880 */
881 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
882 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
503914cf
MD
883 if (mdp->cd->rpadir)
884 mdp->rx_buf_sz += NET_IP_ALIGN;
86a74ff2
NI
885
886 /* Allocate RX and TX skb rings */
887 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
888 GFP_KERNEL);
889 if (!mdp->rx_skbuff) {
380af9e3 890 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
86a74ff2
NI
891 ret = -ENOMEM;
892 return ret;
893 }
894
895 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
896 GFP_KERNEL);
897 if (!mdp->tx_skbuff) {
380af9e3 898 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
86a74ff2
NI
899 ret = -ENOMEM;
900 goto skb_ring_free;
901 }
902
903 /* Allocate all Rx descriptors. */
904 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
905 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
906 GFP_KERNEL);
907
908 if (!mdp->rx_ring) {
380af9e3
YS
909 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
910 rx_ringsize);
86a74ff2
NI
911 ret = -ENOMEM;
912 goto desc_ring_free;
913 }
914
915 mdp->dirty_rx = 0;
916
917 /* Allocate all Tx descriptors. */
918 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
919 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
920 GFP_KERNEL);
921 if (!mdp->tx_ring) {
380af9e3
YS
922 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
923 tx_ringsize);
86a74ff2
NI
924 ret = -ENOMEM;
925 goto desc_ring_free;
926 }
927 return ret;
928
929desc_ring_free:
930 /* free DMA buffer */
931 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
932
933skb_ring_free:
934 /* Free Rx and Tx skb ring buffer */
935 sh_eth_ring_free(ndev);
936
937 return ret;
938}
939
940static int sh_eth_dev_init(struct net_device *ndev)
941{
942 int ret = 0;
943 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
944 u32 val;
945
946 /* Soft Reset */
5cee1d37
NI
947 ret = sh_eth_reset(ndev);
948 if (ret)
949 goto out;
86a74ff2 950
b0ca2a21
NI
951 /* Descriptor format */
952 sh_eth_ring_format(ndev);
380af9e3 953 if (mdp->cd->rpadir)
4a55530f 954 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
86a74ff2
NI
955
956 /* all sh_eth int mask */
4a55530f 957 sh_eth_write(ndev, 0, EESIPR);
86a74ff2 958
10b9194f 959#if defined(__LITTLE_ENDIAN)
380af9e3 960 if (mdp->cd->hw_swap)
4a55530f 961 sh_eth_write(ndev, EDMR_EL, EDMR);
380af9e3 962 else
b0ca2a21 963#endif
4a55530f 964 sh_eth_write(ndev, 0, EDMR);
86a74ff2 965
b0ca2a21 966 /* FIFO size set */
4a55530f
YS
967 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
968 sh_eth_write(ndev, 0, TFTR);
86a74ff2 969
b0ca2a21 970 /* Frame recv control */
4a55530f 971 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
86a74ff2 972
2ecbb783 973 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
86a74ff2 974
380af9e3 975 if (mdp->cd->bculr)
4a55530f 976 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
b0ca2a21 977
4a55530f 978 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
86a74ff2 979
380af9e3 980 if (!mdp->cd->no_trimd)
4a55530f 981 sh_eth_write(ndev, 0, TRIMD);
86a74ff2 982
b0ca2a21 983 /* Recv frame limit set register */
fdb37a7f
YS
984 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
985 RFLR);
86a74ff2 986
4a55530f
YS
987 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
988 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
86a74ff2
NI
989
990 /* PAUSE Prohibition */
4a55530f 991 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
86a74ff2
NI
992 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
993
4a55530f 994 sh_eth_write(ndev, val, ECMR);
b0ca2a21 995
380af9e3
YS
996 if (mdp->cd->set_rate)
997 mdp->cd->set_rate(ndev);
998
b0ca2a21 999 /* E-MAC Status Register clear */
4a55530f 1000 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
b0ca2a21
NI
1001
1002 /* E-MAC Interrupt Enable register */
4a55530f 1003 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
86a74ff2
NI
1004
1005 /* Set MAC address */
1006 update_mac_address(ndev);
1007
1008 /* mask reset */
380af9e3 1009 if (mdp->cd->apr)
4a55530f 1010 sh_eth_write(ndev, APR_AP, APR);
380af9e3 1011 if (mdp->cd->mpr)
4a55530f 1012 sh_eth_write(ndev, MPR_MP, MPR);
380af9e3 1013 if (mdp->cd->tpauser)
4a55530f 1014 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
b0ca2a21 1015
86a74ff2 1016 /* Setting the Rx mode will start the Rx process. */
4a55530f 1017 sh_eth_write(ndev, EDRRR_R, EDRRR);
86a74ff2
NI
1018
1019 netif_start_queue(ndev);
1020
5cee1d37 1021out:
86a74ff2
NI
1022 return ret;
1023}
1024
1025/* free Tx skb function */
1026static int sh_eth_txfree(struct net_device *ndev)
1027{
1028 struct sh_eth_private *mdp = netdev_priv(ndev);
1029 struct sh_eth_txdesc *txdesc;
1030 int freeNum = 0;
1031 int entry = 0;
1032
1033 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
1034 entry = mdp->dirty_tx % TX_RING_SIZE;
1035 txdesc = &mdp->tx_ring[entry];
71557a37 1036 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
86a74ff2
NI
1037 break;
1038 /* Free the original skb. */
1039 if (mdp->tx_skbuff[entry]) {
31fcb99d
YS
1040 dma_unmap_single(&ndev->dev, txdesc->addr,
1041 txdesc->buffer_length, DMA_TO_DEVICE);
86a74ff2
NI
1042 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1043 mdp->tx_skbuff[entry] = NULL;
1044 freeNum++;
1045 }
71557a37 1046 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
86a74ff2 1047 if (entry >= TX_RING_SIZE - 1)
71557a37 1048 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
86a74ff2 1049
bb7d92e3
ED
1050 ndev->stats.tx_packets++;
1051 ndev->stats.tx_bytes += txdesc->buffer_length;
86a74ff2
NI
1052 }
1053 return freeNum;
1054}
1055
1056/* Packet receive function */
a18e08bd 1057static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
86a74ff2
NI
1058{
1059 struct sh_eth_private *mdp = netdev_priv(ndev);
1060 struct sh_eth_rxdesc *rxdesc;
1061
1062 int entry = mdp->cur_rx % RX_RING_SIZE;
1063 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
1064 struct sk_buff *skb;
1065 u16 pkt_len = 0;
380af9e3 1066 u32 desc_status;
86a74ff2
NI
1067
1068 rxdesc = &mdp->rx_ring[entry];
71557a37
YS
1069 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1070 desc_status = edmac_to_cpu(mdp, rxdesc->status);
86a74ff2
NI
1071 pkt_len = rxdesc->frame_length;
1072
73a0d907
YS
1073#if defined(CONFIG_ARCH_R8A7740)
1074 desc_status >>= 16;
1075#endif
1076
86a74ff2
NI
1077 if (--boguscnt < 0)
1078 break;
1079
1080 if (!(desc_status & RDFEND))
bb7d92e3 1081 ndev->stats.rx_length_errors++;
86a74ff2
NI
1082
1083 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1084 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
bb7d92e3 1085 ndev->stats.rx_errors++;
86a74ff2 1086 if (desc_status & RD_RFS1)
bb7d92e3 1087 ndev->stats.rx_crc_errors++;
86a74ff2 1088 if (desc_status & RD_RFS2)
bb7d92e3 1089 ndev->stats.rx_frame_errors++;
86a74ff2 1090 if (desc_status & RD_RFS3)
bb7d92e3 1091 ndev->stats.rx_length_errors++;
86a74ff2 1092 if (desc_status & RD_RFS4)
bb7d92e3 1093 ndev->stats.rx_length_errors++;
86a74ff2 1094 if (desc_status & RD_RFS6)
bb7d92e3 1095 ndev->stats.rx_missed_errors++;
86a74ff2 1096 if (desc_status & RD_RFS10)
bb7d92e3 1097 ndev->stats.rx_over_errors++;
86a74ff2 1098 } else {
380af9e3
YS
1099 if (!mdp->cd->hw_swap)
1100 sh_eth_soft_swap(
1101 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1102 pkt_len + 2);
86a74ff2
NI
1103 skb = mdp->rx_skbuff[entry];
1104 mdp->rx_skbuff[entry] = NULL;
503914cf
MD
1105 if (mdp->cd->rpadir)
1106 skb_reserve(skb, NET_IP_ALIGN);
86a74ff2
NI
1107 skb_put(skb, pkt_len);
1108 skb->protocol = eth_type_trans(skb, ndev);
1109 netif_rx(skb);
bb7d92e3
ED
1110 ndev->stats.rx_packets++;
1111 ndev->stats.rx_bytes += pkt_len;
86a74ff2 1112 }
71557a37 1113 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
86a74ff2 1114 entry = (++mdp->cur_rx) % RX_RING_SIZE;
862df497 1115 rxdesc = &mdp->rx_ring[entry];
86a74ff2
NI
1116 }
1117
1118 /* Refill the Rx ring buffers. */
1119 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
1120 entry = mdp->dirty_rx % RX_RING_SIZE;
1121 rxdesc = &mdp->rx_ring[entry];
b0ca2a21 1122 /* The size of the buffer is 16 byte boundary. */
0029d64a 1123 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
b0ca2a21 1124
86a74ff2 1125 if (mdp->rx_skbuff[entry] == NULL) {
dae2e9f4 1126 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
86a74ff2
NI
1127 mdp->rx_skbuff[entry] = skb;
1128 if (skb == NULL)
1129 break; /* Better luck next round. */
bb7d92e3 1130 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
e88aae7b 1131 DMA_FROM_DEVICE);
380af9e3
YS
1132 sh_eth_set_receive_align(skb);
1133
bc8acf2c 1134 skb_checksum_none_assert(skb);
0029d64a 1135 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
86a74ff2 1136 }
86a74ff2
NI
1137 if (entry >= RX_RING_SIZE - 1)
1138 rxdesc->status |=
71557a37 1139 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
86a74ff2
NI
1140 else
1141 rxdesc->status |=
71557a37 1142 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
86a74ff2
NI
1143 }
1144
1145 /* Restart Rx engine if stopped. */
1146 /* If we don't need to check status, don't. -KDU */
79fba9f5 1147 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
a18e08bd
YS
1148 /* fix the values for the next receiving if RDE is set */
1149 if (intr_status & EESR_RDE)
1150 mdp->cur_rx = mdp->dirty_rx =
1151 (sh_eth_read(ndev, RDFAR) -
1152 sh_eth_read(ndev, RDLAR)) >> 4;
4a55530f 1153 sh_eth_write(ndev, EDRRR_R, EDRRR);
79fba9f5 1154 }
86a74ff2
NI
1155
1156 return 0;
1157}
1158
4a55530f 1159static void sh_eth_rcv_snd_disable(struct net_device *ndev)
dc19e4e5
NI
1160{
1161 /* disable tx and rx */
4a55530f
YS
1162 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1163 ~(ECMR_RE | ECMR_TE), ECMR);
dc19e4e5
NI
1164}
1165
4a55530f 1166static void sh_eth_rcv_snd_enable(struct net_device *ndev)
dc19e4e5
NI
1167{
1168 /* enable tx and rx */
4a55530f
YS
1169 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1170 (ECMR_RE | ECMR_TE), ECMR);
dc19e4e5
NI
1171}
1172
86a74ff2
NI
1173/* error control function */
1174static void sh_eth_error(struct net_device *ndev, int intr_status)
1175{
1176 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2 1177 u32 felic_stat;
380af9e3
YS
1178 u32 link_stat;
1179 u32 mask;
86a74ff2
NI
1180
1181 if (intr_status & EESR_ECI) {
4a55530f
YS
1182 felic_stat = sh_eth_read(ndev, ECSR);
1183 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
86a74ff2 1184 if (felic_stat & ECSR_ICD)
bb7d92e3 1185 ndev->stats.tx_carrier_errors++;
86a74ff2
NI
1186 if (felic_stat & ECSR_LCHNG) {
1187 /* Link Changed */
4923576b 1188 if (mdp->cd->no_psr || mdp->no_ether_link) {
380af9e3
YS
1189 if (mdp->link == PHY_DOWN)
1190 link_stat = 0;
1191 else
1192 link_stat = PHY_ST_LINK;
1193 } else {
4a55530f 1194 link_stat = (sh_eth_read(ndev, PSR));
4923576b
YS
1195 if (mdp->ether_link_active_low)
1196 link_stat = ~link_stat;
380af9e3 1197 }
dc19e4e5 1198 if (!(link_stat & PHY_ST_LINK))
4a55530f 1199 sh_eth_rcv_snd_disable(ndev);
dc19e4e5 1200 else {
86a74ff2 1201 /* Link Up */
4a55530f
YS
1202 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1203 ~DMAC_M_ECI, EESIPR);
86a74ff2 1204 /*clear int */
4a55530f
YS
1205 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1206 ECSR);
1207 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1208 DMAC_M_ECI, EESIPR);
86a74ff2 1209 /* enable tx and rx */
4a55530f 1210 sh_eth_rcv_snd_enable(ndev);
86a74ff2
NI
1211 }
1212 }
1213 }
1214
1215 if (intr_status & EESR_TWB) {
1216 /* Write buck end. unused write back interrupt */
1217 if (intr_status & EESR_TABT) /* Transmit Abort int */
bb7d92e3 1218 ndev->stats.tx_aborted_errors++;
dc19e4e5
NI
1219 if (netif_msg_tx_err(mdp))
1220 dev_err(&ndev->dev, "Transmit Abort\n");
86a74ff2
NI
1221 }
1222
1223 if (intr_status & EESR_RABT) {
1224 /* Receive Abort int */
1225 if (intr_status & EESR_RFRMER) {
1226 /* Receive Frame Overflow int */
bb7d92e3 1227 ndev->stats.rx_frame_errors++;
dc19e4e5
NI
1228 if (netif_msg_rx_err(mdp))
1229 dev_err(&ndev->dev, "Receive Abort\n");
86a74ff2
NI
1230 }
1231 }
380af9e3 1232
dc19e4e5
NI
1233 if (intr_status & EESR_TDE) {
1234 /* Transmit Descriptor Empty int */
bb7d92e3 1235 ndev->stats.tx_fifo_errors++;
dc19e4e5
NI
1236 if (netif_msg_tx_err(mdp))
1237 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1238 }
1239
1240 if (intr_status & EESR_TFE) {
1241 /* FIFO under flow */
bb7d92e3 1242 ndev->stats.tx_fifo_errors++;
dc19e4e5
NI
1243 if (netif_msg_tx_err(mdp))
1244 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
86a74ff2
NI
1245 }
1246
1247 if (intr_status & EESR_RDE) {
1248 /* Receive Descriptor Empty int */
bb7d92e3 1249 ndev->stats.rx_over_errors++;
86a74ff2 1250
dc19e4e5
NI
1251 if (netif_msg_rx_err(mdp))
1252 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
86a74ff2 1253 }
dc19e4e5 1254
86a74ff2
NI
1255 if (intr_status & EESR_RFE) {
1256 /* Receive FIFO Overflow int */
bb7d92e3 1257 ndev->stats.rx_fifo_errors++;
dc19e4e5
NI
1258 if (netif_msg_rx_err(mdp))
1259 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1260 }
1261
1262 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1263 /* Address Error */
bb7d92e3 1264 ndev->stats.tx_fifo_errors++;
dc19e4e5
NI
1265 if (netif_msg_tx_err(mdp))
1266 dev_err(&ndev->dev, "Address Error\n");
86a74ff2 1267 }
380af9e3
YS
1268
1269 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1270 if (mdp->cd->no_ade)
1271 mask &= ~EESR_ADE;
1272 if (intr_status & mask) {
86a74ff2 1273 /* Tx error */
4a55530f 1274 u32 edtrr = sh_eth_read(ndev, EDTRR);
86a74ff2 1275 /* dmesg */
380af9e3
YS
1276 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1277 intr_status, mdp->cur_tx);
1278 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
86a74ff2
NI
1279 mdp->dirty_tx, (u32) ndev->state, edtrr);
1280 /* dirty buffer free */
1281 sh_eth_txfree(ndev);
1282
1283 /* SH7712 BUG */
c5ed5368 1284 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
86a74ff2 1285 /* tx dma start */
c5ed5368 1286 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
86a74ff2
NI
1287 }
1288 /* wakeup */
1289 netif_wake_queue(ndev);
1290 }
1291}
1292
1293static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1294{
1295 struct net_device *ndev = netdev;
1296 struct sh_eth_private *mdp = netdev_priv(ndev);
380af9e3 1297 struct sh_eth_cpu_data *cd = mdp->cd;
0e0fde3c 1298 irqreturn_t ret = IRQ_NONE;
4a55530f 1299 u32 intr_status = 0;
86a74ff2 1300
86a74ff2
NI
1301 spin_lock(&mdp->lock);
1302
b0ca2a21 1303 /* Get interrpt stat */
4a55530f 1304 intr_status = sh_eth_read(ndev, EESR);
86a74ff2 1305 /* Clear interrupt */
0e0fde3c
NI
1306 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1307 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
380af9e3 1308 cd->tx_check | cd->eesr_err_check)) {
4a55530f 1309 sh_eth_write(ndev, intr_status, EESR);
0e0fde3c
NI
1310 ret = IRQ_HANDLED;
1311 } else
1312 goto other_irq;
86a74ff2 1313
b0ca2a21
NI
1314 if (intr_status & (EESR_FRC | /* Frame recv*/
1315 EESR_RMAF | /* Multi cast address recv*/
1316 EESR_RRF | /* Bit frame recv */
1317 EESR_RTLF | /* Long frame recv*/
1318 EESR_RTSF | /* short frame recv */
1319 EESR_PRE | /* PHY-LSI recv error */
1320 EESR_CERF)){ /* recv frame CRC error */
a18e08bd 1321 sh_eth_rx(ndev, intr_status);
b0ca2a21 1322 }
86a74ff2 1323
b0ca2a21 1324 /* Tx Check */
380af9e3 1325 if (intr_status & cd->tx_check) {
86a74ff2
NI
1326 sh_eth_txfree(ndev);
1327 netif_wake_queue(ndev);
1328 }
1329
380af9e3 1330 if (intr_status & cd->eesr_err_check)
86a74ff2
NI
1331 sh_eth_error(ndev, intr_status);
1332
0e0fde3c 1333other_irq:
86a74ff2
NI
1334 spin_unlock(&mdp->lock);
1335
0e0fde3c 1336 return ret;
86a74ff2
NI
1337}
1338
86a74ff2
NI
1339/* PHY state control function */
1340static void sh_eth_adjust_link(struct net_device *ndev)
1341{
1342 struct sh_eth_private *mdp = netdev_priv(ndev);
1343 struct phy_device *phydev = mdp->phydev;
86a74ff2
NI
1344 int new_state = 0;
1345
1346 if (phydev->link != PHY_DOWN) {
1347 if (phydev->duplex != mdp->duplex) {
1348 new_state = 1;
1349 mdp->duplex = phydev->duplex;
380af9e3
YS
1350 if (mdp->cd->set_duplex)
1351 mdp->cd->set_duplex(ndev);
86a74ff2
NI
1352 }
1353
1354 if (phydev->speed != mdp->speed) {
1355 new_state = 1;
1356 mdp->speed = phydev->speed;
380af9e3
YS
1357 if (mdp->cd->set_rate)
1358 mdp->cd->set_rate(ndev);
86a74ff2
NI
1359 }
1360 if (mdp->link == PHY_DOWN) {
91a56152
YS
1361 sh_eth_write(ndev,
1362 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
86a74ff2
NI
1363 new_state = 1;
1364 mdp->link = phydev->link;
86a74ff2
NI
1365 }
1366 } else if (mdp->link) {
1367 new_state = 1;
1368 mdp->link = PHY_DOWN;
1369 mdp->speed = 0;
1370 mdp->duplex = -1;
86a74ff2
NI
1371 }
1372
dc19e4e5 1373 if (new_state && netif_msg_link(mdp))
86a74ff2
NI
1374 phy_print_status(phydev);
1375}
1376
1377/* PHY init function */
1378static int sh_eth_phy_init(struct net_device *ndev)
1379{
1380 struct sh_eth_private *mdp = netdev_priv(ndev);
0a372eb9 1381 char phy_id[MII_BUS_ID_SIZE + 3];
86a74ff2
NI
1382 struct phy_device *phydev = NULL;
1383
fb28ad35 1384 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
86a74ff2
NI
1385 mdp->mii_bus->id , mdp->phy_id);
1386
1387 mdp->link = PHY_DOWN;
1388 mdp->speed = 0;
1389 mdp->duplex = -1;
1390
1391 /* Try connect to PHY */
c061b18d 1392 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
e47c9052 1393 0, mdp->phy_interface);
86a74ff2
NI
1394 if (IS_ERR(phydev)) {
1395 dev_err(&ndev->dev, "phy_connect failed\n");
1396 return PTR_ERR(phydev);
1397 }
380af9e3 1398
86a74ff2 1399 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
380af9e3 1400 phydev->addr, phydev->drv->name);
86a74ff2
NI
1401
1402 mdp->phydev = phydev;
1403
1404 return 0;
1405}
1406
1407/* PHY control start function */
1408static int sh_eth_phy_start(struct net_device *ndev)
1409{
1410 struct sh_eth_private *mdp = netdev_priv(ndev);
1411 int ret;
1412
1413 ret = sh_eth_phy_init(ndev);
1414 if (ret)
1415 return ret;
1416
1417 /* reset phy - this also wakes it from PDOWN */
1418 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1419 phy_start(mdp->phydev);
1420
1421 return 0;
1422}
1423
dc19e4e5
NI
1424static int sh_eth_get_settings(struct net_device *ndev,
1425 struct ethtool_cmd *ecmd)
1426{
1427 struct sh_eth_private *mdp = netdev_priv(ndev);
1428 unsigned long flags;
1429 int ret;
1430
1431 spin_lock_irqsave(&mdp->lock, flags);
1432 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1433 spin_unlock_irqrestore(&mdp->lock, flags);
1434
1435 return ret;
1436}
1437
1438static int sh_eth_set_settings(struct net_device *ndev,
1439 struct ethtool_cmd *ecmd)
1440{
1441 struct sh_eth_private *mdp = netdev_priv(ndev);
1442 unsigned long flags;
1443 int ret;
dc19e4e5
NI
1444
1445 spin_lock_irqsave(&mdp->lock, flags);
1446
1447 /* disable tx and rx */
4a55530f 1448 sh_eth_rcv_snd_disable(ndev);
dc19e4e5
NI
1449
1450 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1451 if (ret)
1452 goto error_exit;
1453
1454 if (ecmd->duplex == DUPLEX_FULL)
1455 mdp->duplex = 1;
1456 else
1457 mdp->duplex = 0;
1458
1459 if (mdp->cd->set_duplex)
1460 mdp->cd->set_duplex(ndev);
1461
1462error_exit:
1463 mdelay(1);
1464
1465 /* enable tx and rx */
4a55530f 1466 sh_eth_rcv_snd_enable(ndev);
dc19e4e5
NI
1467
1468 spin_unlock_irqrestore(&mdp->lock, flags);
1469
1470 return ret;
1471}
1472
1473static int sh_eth_nway_reset(struct net_device *ndev)
1474{
1475 struct sh_eth_private *mdp = netdev_priv(ndev);
1476 unsigned long flags;
1477 int ret;
1478
1479 spin_lock_irqsave(&mdp->lock, flags);
1480 ret = phy_start_aneg(mdp->phydev);
1481 spin_unlock_irqrestore(&mdp->lock, flags);
1482
1483 return ret;
1484}
1485
1486static u32 sh_eth_get_msglevel(struct net_device *ndev)
1487{
1488 struct sh_eth_private *mdp = netdev_priv(ndev);
1489 return mdp->msg_enable;
1490}
1491
1492static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1493{
1494 struct sh_eth_private *mdp = netdev_priv(ndev);
1495 mdp->msg_enable = value;
1496}
1497
1498static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1499 "rx_current", "tx_current",
1500 "rx_dirty", "tx_dirty",
1501};
1502#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1503
1504static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1505{
1506 switch (sset) {
1507 case ETH_SS_STATS:
1508 return SH_ETH_STATS_LEN;
1509 default:
1510 return -EOPNOTSUPP;
1511 }
1512}
1513
1514static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1515 struct ethtool_stats *stats, u64 *data)
1516{
1517 struct sh_eth_private *mdp = netdev_priv(ndev);
1518 int i = 0;
1519
1520 /* device-specific stats */
1521 data[i++] = mdp->cur_rx;
1522 data[i++] = mdp->cur_tx;
1523 data[i++] = mdp->dirty_rx;
1524 data[i++] = mdp->dirty_tx;
1525}
1526
1527static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1528{
1529 switch (stringset) {
1530 case ETH_SS_STATS:
1531 memcpy(data, *sh_eth_gstrings_stats,
1532 sizeof(sh_eth_gstrings_stats));
1533 break;
1534 }
1535}
1536
9b07be4b 1537static const struct ethtool_ops sh_eth_ethtool_ops = {
dc19e4e5
NI
1538 .get_settings = sh_eth_get_settings,
1539 .set_settings = sh_eth_set_settings,
9b07be4b 1540 .nway_reset = sh_eth_nway_reset,
dc19e4e5
NI
1541 .get_msglevel = sh_eth_get_msglevel,
1542 .set_msglevel = sh_eth_set_msglevel,
9b07be4b 1543 .get_link = ethtool_op_get_link,
dc19e4e5
NI
1544 .get_strings = sh_eth_get_strings,
1545 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1546 .get_sset_count = sh_eth_get_sset_count,
1547};
1548
86a74ff2
NI
1549/* network device open function */
1550static int sh_eth_open(struct net_device *ndev)
1551{
1552 int ret = 0;
1553 struct sh_eth_private *mdp = netdev_priv(ndev);
1554
bcd5149d
MD
1555 pm_runtime_get_sync(&mdp->pdev->dev);
1556
a0607fd3 1557 ret = request_irq(ndev->irq, sh_eth_interrupt,
f29a3d04 1558#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
dc19e4e5
NI
1559 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1560 defined(CONFIG_CPU_SUBTYPE_SH7757)
0e0fde3c
NI
1561 IRQF_SHARED,
1562#else
1563 0,
1564#endif
1565 ndev->name, ndev);
86a74ff2 1566 if (ret) {
380af9e3 1567 dev_err(&ndev->dev, "Can not assign IRQ number\n");
86a74ff2
NI
1568 return ret;
1569 }
1570
1571 /* Descriptor set */
1572 ret = sh_eth_ring_init(ndev);
1573 if (ret)
1574 goto out_free_irq;
1575
1576 /* device init */
1577 ret = sh_eth_dev_init(ndev);
1578 if (ret)
1579 goto out_free_irq;
1580
1581 /* PHY control start*/
1582 ret = sh_eth_phy_start(ndev);
1583 if (ret)
1584 goto out_free_irq;
1585
86a74ff2
NI
1586 return ret;
1587
1588out_free_irq:
1589 free_irq(ndev->irq, ndev);
bcd5149d 1590 pm_runtime_put_sync(&mdp->pdev->dev);
86a74ff2
NI
1591 return ret;
1592}
1593
1594/* Timeout function */
1595static void sh_eth_tx_timeout(struct net_device *ndev)
1596{
1597 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
1598 struct sh_eth_rxdesc *rxdesc;
1599 int i;
1600
1601 netif_stop_queue(ndev);
1602
dc19e4e5
NI
1603 if (netif_msg_timer(mdp))
1604 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
4a55530f 1605 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
86a74ff2
NI
1606
1607 /* tx_errors count up */
bb7d92e3 1608 ndev->stats.tx_errors++;
86a74ff2 1609
86a74ff2
NI
1610 /* Free all the skbuffs in the Rx queue. */
1611 for (i = 0; i < RX_RING_SIZE; i++) {
1612 rxdesc = &mdp->rx_ring[i];
1613 rxdesc->status = 0;
1614 rxdesc->addr = 0xBADF00D0;
1615 if (mdp->rx_skbuff[i])
1616 dev_kfree_skb(mdp->rx_skbuff[i]);
1617 mdp->rx_skbuff[i] = NULL;
1618 }
1619 for (i = 0; i < TX_RING_SIZE; i++) {
1620 if (mdp->tx_skbuff[i])
1621 dev_kfree_skb(mdp->tx_skbuff[i]);
1622 mdp->tx_skbuff[i] = NULL;
1623 }
1624
1625 /* device init */
1626 sh_eth_dev_init(ndev);
86a74ff2
NI
1627}
1628
1629/* Packet transmit function */
1630static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1631{
1632 struct sh_eth_private *mdp = netdev_priv(ndev);
1633 struct sh_eth_txdesc *txdesc;
1634 u32 entry;
fb5e2f9b 1635 unsigned long flags;
86a74ff2
NI
1636
1637 spin_lock_irqsave(&mdp->lock, flags);
1638 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1639 if (!sh_eth_txfree(ndev)) {
dc19e4e5
NI
1640 if (netif_msg_tx_queued(mdp))
1641 dev_warn(&ndev->dev, "TxFD exhausted.\n");
86a74ff2
NI
1642 netif_stop_queue(ndev);
1643 spin_unlock_irqrestore(&mdp->lock, flags);
5b548140 1644 return NETDEV_TX_BUSY;
86a74ff2
NI
1645 }
1646 }
1647 spin_unlock_irqrestore(&mdp->lock, flags);
1648
1649 entry = mdp->cur_tx % TX_RING_SIZE;
1650 mdp->tx_skbuff[entry] = skb;
1651 txdesc = &mdp->tx_ring[entry];
86a74ff2 1652 /* soft swap. */
380af9e3
YS
1653 if (!mdp->cd->hw_swap)
1654 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1655 skb->len + 2);
31fcb99d
YS
1656 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1657 DMA_TO_DEVICE);
86a74ff2
NI
1658 if (skb->len < ETHERSMALL)
1659 txdesc->buffer_length = ETHERSMALL;
1660 else
1661 txdesc->buffer_length = skb->len;
1662
1663 if (entry >= TX_RING_SIZE - 1)
71557a37 1664 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
86a74ff2 1665 else
71557a37 1666 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
86a74ff2
NI
1667
1668 mdp->cur_tx++;
1669
c5ed5368
YS
1670 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1671 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
b0ca2a21 1672
6ed10654 1673 return NETDEV_TX_OK;
86a74ff2
NI
1674}
1675
1676/* device close function */
1677static int sh_eth_close(struct net_device *ndev)
1678{
1679 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
1680 int ringsize;
1681
1682 netif_stop_queue(ndev);
1683
1684 /* Disable interrupts by clearing the interrupt mask. */
4a55530f 1685 sh_eth_write(ndev, 0x0000, EESIPR);
86a74ff2
NI
1686
1687 /* Stop the chip's Tx and Rx processes. */
4a55530f
YS
1688 sh_eth_write(ndev, 0, EDTRR);
1689 sh_eth_write(ndev, 0, EDRRR);
86a74ff2
NI
1690
1691 /* PHY Disconnect */
1692 if (mdp->phydev) {
1693 phy_stop(mdp->phydev);
1694 phy_disconnect(mdp->phydev);
1695 }
1696
1697 free_irq(ndev->irq, ndev);
1698
86a74ff2
NI
1699 /* Free all the skbuffs in the Rx queue. */
1700 sh_eth_ring_free(ndev);
1701
1702 /* free DMA buffer */
1703 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1704 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1705
1706 /* free DMA buffer */
1707 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1708 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1709
bcd5149d
MD
1710 pm_runtime_put_sync(&mdp->pdev->dev);
1711
86a74ff2
NI
1712 return 0;
1713}
1714
1715static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1716{
1717 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2 1718
bcd5149d
MD
1719 pm_runtime_get_sync(&mdp->pdev->dev);
1720
bb7d92e3 1721 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
4a55530f 1722 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
bb7d92e3 1723 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
4a55530f 1724 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
bb7d92e3 1725 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
4a55530f 1726 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
c5ed5368 1727 if (sh_eth_is_gether(mdp)) {
bb7d92e3 1728 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
c5ed5368 1729 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
bb7d92e3 1730 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
c5ed5368
YS
1731 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1732 } else {
bb7d92e3 1733 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
c5ed5368
YS
1734 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1735 }
bcd5149d
MD
1736 pm_runtime_put_sync(&mdp->pdev->dev);
1737
bb7d92e3 1738 return &ndev->stats;
86a74ff2
NI
1739}
1740
bb7d92e3 1741/* ioctl to device function */
86a74ff2
NI
1742static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1743 int cmd)
1744{
1745 struct sh_eth_private *mdp = netdev_priv(ndev);
1746 struct phy_device *phydev = mdp->phydev;
1747
1748 if (!netif_running(ndev))
1749 return -EINVAL;
1750
1751 if (!phydev)
1752 return -ENODEV;
1753
28b04113 1754 return phy_mii_ioctl(phydev, rq, cmd);
86a74ff2
NI
1755}
1756
380af9e3 1757#if defined(SH_ETH_HAS_TSU)
6743fe6d
YS
1758/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
1759static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
1760 int entry)
1761{
1762 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
1763}
1764
1765static u32 sh_eth_tsu_get_post_mask(int entry)
1766{
1767 return 0x0f << (28 - ((entry % 8) * 4));
1768}
1769
1770static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
1771{
1772 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
1773}
1774
1775static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
1776 int entry)
1777{
1778 struct sh_eth_private *mdp = netdev_priv(ndev);
1779 u32 tmp;
1780 void *reg_offset;
1781
1782 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
1783 tmp = ioread32(reg_offset);
1784 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
1785}
1786
1787static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
1788 int entry)
1789{
1790 struct sh_eth_private *mdp = netdev_priv(ndev);
1791 u32 post_mask, ref_mask, tmp;
1792 void *reg_offset;
1793
1794 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
1795 post_mask = sh_eth_tsu_get_post_mask(entry);
1796 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
1797
1798 tmp = ioread32(reg_offset);
1799 iowrite32(tmp & ~post_mask, reg_offset);
1800
1801 /* If other port enables, the function returns "true" */
1802 return tmp & ref_mask;
1803}
1804
1805static int sh_eth_tsu_busy(struct net_device *ndev)
1806{
1807 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
1808 struct sh_eth_private *mdp = netdev_priv(ndev);
1809
1810 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
1811 udelay(10);
1812 timeout--;
1813 if (timeout <= 0) {
1814 dev_err(&ndev->dev, "%s: timeout\n", __func__);
1815 return -ETIMEDOUT;
1816 }
1817 }
1818
1819 return 0;
1820}
1821
1822static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
1823 const u8 *addr)
1824{
1825 u32 val;
1826
1827 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
1828 iowrite32(val, reg);
1829 if (sh_eth_tsu_busy(ndev) < 0)
1830 return -EBUSY;
1831
1832 val = addr[4] << 8 | addr[5];
1833 iowrite32(val, reg + 4);
1834 if (sh_eth_tsu_busy(ndev) < 0)
1835 return -EBUSY;
1836
1837 return 0;
1838}
1839
1840static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
1841{
1842 u32 val;
1843
1844 val = ioread32(reg);
1845 addr[0] = (val >> 24) & 0xff;
1846 addr[1] = (val >> 16) & 0xff;
1847 addr[2] = (val >> 8) & 0xff;
1848 addr[3] = val & 0xff;
1849 val = ioread32(reg + 4);
1850 addr[4] = (val >> 8) & 0xff;
1851 addr[5] = val & 0xff;
1852}
1853
1854
1855static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
1856{
1857 struct sh_eth_private *mdp = netdev_priv(ndev);
1858 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1859 int i;
1860 u8 c_addr[ETH_ALEN];
1861
1862 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
1863 sh_eth_tsu_read_entry(reg_offset, c_addr);
1864 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
1865 return i;
1866 }
1867
1868 return -ENOENT;
1869}
1870
1871static int sh_eth_tsu_find_empty(struct net_device *ndev)
1872{
1873 u8 blank[ETH_ALEN];
1874 int entry;
1875
1876 memset(blank, 0, sizeof(blank));
1877 entry = sh_eth_tsu_find_entry(ndev, blank);
1878 return (entry < 0) ? -ENOMEM : entry;
1879}
1880
1881static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
1882 int entry)
1883{
1884 struct sh_eth_private *mdp = netdev_priv(ndev);
1885 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1886 int ret;
1887 u8 blank[ETH_ALEN];
1888
1889 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
1890 ~(1 << (31 - entry)), TSU_TEN);
1891
1892 memset(blank, 0, sizeof(blank));
1893 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
1894 if (ret < 0)
1895 return ret;
1896 return 0;
1897}
1898
1899static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
1900{
1901 struct sh_eth_private *mdp = netdev_priv(ndev);
1902 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1903 int i, ret;
1904
1905 if (!mdp->cd->tsu)
1906 return 0;
1907
1908 i = sh_eth_tsu_find_entry(ndev, addr);
1909 if (i < 0) {
1910 /* No entry found, create one */
1911 i = sh_eth_tsu_find_empty(ndev);
1912 if (i < 0)
1913 return -ENOMEM;
1914 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
1915 if (ret < 0)
1916 return ret;
1917
1918 /* Enable the entry */
1919 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
1920 (1 << (31 - i)), TSU_TEN);
1921 }
1922
1923 /* Entry found or created, enable POST */
1924 sh_eth_tsu_enable_cam_entry_post(ndev, i);
1925
1926 return 0;
1927}
1928
1929static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
1930{
1931 struct sh_eth_private *mdp = netdev_priv(ndev);
1932 int i, ret;
1933
1934 if (!mdp->cd->tsu)
1935 return 0;
1936
1937 i = sh_eth_tsu_find_entry(ndev, addr);
1938 if (i) {
1939 /* Entry found */
1940 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
1941 goto done;
1942
1943 /* Disable the entry if both ports was disabled */
1944 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
1945 if (ret < 0)
1946 return ret;
1947 }
1948done:
1949 return 0;
1950}
1951
1952static int sh_eth_tsu_purge_all(struct net_device *ndev)
1953{
1954 struct sh_eth_private *mdp = netdev_priv(ndev);
1955 int i, ret;
1956
1957 if (unlikely(!mdp->cd->tsu))
1958 return 0;
1959
1960 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
1961 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
1962 continue;
1963
1964 /* Disable the entry if both ports was disabled */
1965 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
1966 if (ret < 0)
1967 return ret;
1968 }
1969
1970 return 0;
1971}
1972
1973static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
1974{
1975 struct sh_eth_private *mdp = netdev_priv(ndev);
1976 u8 addr[ETH_ALEN];
1977 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1978 int i;
1979
1980 if (unlikely(!mdp->cd->tsu))
1981 return;
1982
1983 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
1984 sh_eth_tsu_read_entry(reg_offset, addr);
1985 if (is_multicast_ether_addr(addr))
1986 sh_eth_tsu_del_entry(ndev, addr);
1987 }
1988}
1989
86a74ff2
NI
1990/* Multicast reception directions set */
1991static void sh_eth_set_multicast_list(struct net_device *ndev)
1992{
6743fe6d
YS
1993 struct sh_eth_private *mdp = netdev_priv(ndev);
1994 u32 ecmr_bits;
1995 int mcast_all = 0;
1996 unsigned long flags;
1997
1998 spin_lock_irqsave(&mdp->lock, flags);
1999 /*
2000 * Initial condition is MCT = 1, PRM = 0.
2001 * Depending on ndev->flags, set PRM or clear MCT
2002 */
2003 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2004
2005 if (!(ndev->flags & IFF_MULTICAST)) {
2006 sh_eth_tsu_purge_mcast(ndev);
2007 mcast_all = 1;
2008 }
2009 if (ndev->flags & IFF_ALLMULTI) {
2010 sh_eth_tsu_purge_mcast(ndev);
2011 ecmr_bits &= ~ECMR_MCT;
2012 mcast_all = 1;
2013 }
2014
86a74ff2 2015 if (ndev->flags & IFF_PROMISC) {
6743fe6d
YS
2016 sh_eth_tsu_purge_all(ndev);
2017 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2018 } else if (mdp->cd->tsu) {
2019 struct netdev_hw_addr *ha;
2020 netdev_for_each_mc_addr(ha, ndev) {
2021 if (mcast_all && is_multicast_ether_addr(ha->addr))
2022 continue;
2023
2024 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2025 if (!mcast_all) {
2026 sh_eth_tsu_purge_mcast(ndev);
2027 ecmr_bits &= ~ECMR_MCT;
2028 mcast_all = 1;
2029 }
2030 }
2031 }
86a74ff2
NI
2032 } else {
2033 /* Normal, unicast/broadcast-only mode. */
6743fe6d 2034 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
86a74ff2 2035 }
6743fe6d
YS
2036
2037 /* update the ethernet mode */
2038 sh_eth_write(ndev, ecmr_bits, ECMR);
2039
2040 spin_unlock_irqrestore(&mdp->lock, flags);
86a74ff2 2041}
71cc7c37
YS
2042
2043static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2044{
2045 if (!mdp->port)
2046 return TSU_VTAG0;
2047 else
2048 return TSU_VTAG1;
2049}
2050
2051static int sh_eth_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
2052{
2053 struct sh_eth_private *mdp = netdev_priv(ndev);
2054 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2055
2056 if (unlikely(!mdp->cd->tsu))
2057 return -EPERM;
2058
2059 /* No filtering if vid = 0 */
2060 if (!vid)
2061 return 0;
2062
2063 mdp->vlan_num_ids++;
2064
2065 /*
2066 * The controller has one VLAN tag HW filter. So, if the filter is
2067 * already enabled, the driver disables it and the filte
2068 */
2069 if (mdp->vlan_num_ids > 1) {
2070 /* disable VLAN filter */
2071 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2072 return 0;
2073 }
2074
2075 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2076 vtag_reg_index);
2077
2078 return 0;
2079}
2080
2081static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
2082{
2083 struct sh_eth_private *mdp = netdev_priv(ndev);
2084 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2085
2086 if (unlikely(!mdp->cd->tsu))
2087 return -EPERM;
2088
2089 /* No filtering if vid = 0 */
2090 if (!vid)
2091 return 0;
2092
2093 mdp->vlan_num_ids--;
2094 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2095
2096 return 0;
2097}
4986b996 2098#endif /* SH_ETH_HAS_TSU */
86a74ff2
NI
2099
2100/* SuperH's TSU register init function */
4a55530f 2101static void sh_eth_tsu_init(struct sh_eth_private *mdp)
86a74ff2 2102{
4a55530f
YS
2103 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2104 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2105 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2106 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2107 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2108 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2109 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2110 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2111 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2112 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
c5ed5368
YS
2113 if (sh_eth_is_gether(mdp)) {
2114 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2115 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2116 } else {
2117 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2118 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2119 }
4a55530f
YS
2120 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2121 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2122 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2123 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2124 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2125 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2126 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
86a74ff2
NI
2127}
2128
2129/* MDIO bus release function */
2130static int sh_mdio_release(struct net_device *ndev)
2131{
2132 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2133
2134 /* unregister mdio bus */
2135 mdiobus_unregister(bus);
2136
2137 /* remove mdio bus info from net_device */
2138 dev_set_drvdata(&ndev->dev, NULL);
2139
0f0b405c
DK
2140 /* free interrupts memory */
2141 kfree(bus->irq);
2142
86a74ff2
NI
2143 /* free bitbang info */
2144 free_mdio_bitbang(bus);
2145
2146 return 0;
2147}
2148
2149/* MDIO bus init function */
b3017e6a
YS
2150static int sh_mdio_init(struct net_device *ndev, int id,
2151 struct sh_eth_plat_data *pd)
86a74ff2
NI
2152{
2153 int ret, i;
2154 struct bb_info *bitbang;
2155 struct sh_eth_private *mdp = netdev_priv(ndev);
2156
2157 /* create bit control struct for PHY */
2158 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
2159 if (!bitbang) {
2160 ret = -ENOMEM;
2161 goto out;
2162 }
2163
2164 /* bitbang init */
ae70644d 2165 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
b3017e6a 2166 bitbang->set_gate = pd->set_mdio_gate;
86a74ff2
NI
2167 bitbang->mdi_msk = 0x08;
2168 bitbang->mdo_msk = 0x04;
2169 bitbang->mmd_msk = 0x02;/* MMD */
2170 bitbang->mdc_msk = 0x01;
2171 bitbang->ctrl.ops = &bb_ops;
2172
c2e07b3a 2173 /* MII controller setting */
86a74ff2
NI
2174 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2175 if (!mdp->mii_bus) {
2176 ret = -ENOMEM;
2177 goto out_free_bitbang;
2178 }
2179
2180 /* Hook up MII support for ethtool */
2181 mdp->mii_bus->name = "sh_mii";
18ee49dd 2182 mdp->mii_bus->parent = &ndev->dev;
5278fb54 2183 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
34aa6f14 2184 mdp->pdev->name, id);
86a74ff2
NI
2185
2186 /* PHY IRQ */
2187 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
2188 if (!mdp->mii_bus->irq) {
2189 ret = -ENOMEM;
2190 goto out_free_bus;
2191 }
2192
2193 for (i = 0; i < PHY_MAX_ADDR; i++)
2194 mdp->mii_bus->irq[i] = PHY_POLL;
2195
2196 /* regist mdio bus */
2197 ret = mdiobus_register(mdp->mii_bus);
2198 if (ret)
2199 goto out_free_irq;
2200
2201 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2202
2203 return 0;
2204
2205out_free_irq:
2206 kfree(mdp->mii_bus->irq);
2207
2208out_free_bus:
298cf9be 2209 free_mdio_bitbang(mdp->mii_bus);
86a74ff2
NI
2210
2211out_free_bitbang:
2212 kfree(bitbang);
2213
2214out:
2215 return ret;
2216}
2217
4a55530f
YS
2218static const u16 *sh_eth_get_register_offset(int register_type)
2219{
2220 const u16 *reg_offset = NULL;
2221
2222 switch (register_type) {
2223 case SH_ETH_REG_GIGABIT:
2224 reg_offset = sh_eth_offset_gigabit;
2225 break;
2226 case SH_ETH_REG_FAST_SH4:
2227 reg_offset = sh_eth_offset_fast_sh4;
2228 break;
2229 case SH_ETH_REG_FAST_SH3_SH2:
2230 reg_offset = sh_eth_offset_fast_sh3_sh2;
2231 break;
2232 default:
2233 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
2234 break;
2235 }
2236
2237 return reg_offset;
2238}
2239
ebf84eaa
AB
2240static const struct net_device_ops sh_eth_netdev_ops = {
2241 .ndo_open = sh_eth_open,
2242 .ndo_stop = sh_eth_close,
2243 .ndo_start_xmit = sh_eth_start_xmit,
2244 .ndo_get_stats = sh_eth_get_stats,
380af9e3 2245#if defined(SH_ETH_HAS_TSU)
afc4b13d 2246 .ndo_set_rx_mode = sh_eth_set_multicast_list,
71cc7c37
YS
2247 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2248 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
380af9e3 2249#endif
ebf84eaa
AB
2250 .ndo_tx_timeout = sh_eth_tx_timeout,
2251 .ndo_do_ioctl = sh_eth_do_ioctl,
2252 .ndo_validate_addr = eth_validate_addr,
2253 .ndo_set_mac_address = eth_mac_addr,
2254 .ndo_change_mtu = eth_change_mtu,
2255};
2256
86a74ff2
NI
2257static int sh_eth_drv_probe(struct platform_device *pdev)
2258{
9c38657c 2259 int ret, devno = 0;
86a74ff2
NI
2260 struct resource *res;
2261 struct net_device *ndev = NULL;
ec0d7551 2262 struct sh_eth_private *mdp = NULL;
71557a37 2263 struct sh_eth_plat_data *pd;
86a74ff2
NI
2264
2265 /* get base addr */
2266 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2267 if (unlikely(res == NULL)) {
2268 dev_err(&pdev->dev, "invalid resource\n");
2269 ret = -EINVAL;
2270 goto out;
2271 }
2272
2273 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2274 if (!ndev) {
86a74ff2
NI
2275 ret = -ENOMEM;
2276 goto out;
2277 }
2278
2279 /* The sh Ether-specific entries in the device structure. */
2280 ndev->base_addr = res->start;
2281 devno = pdev->id;
2282 if (devno < 0)
2283 devno = 0;
2284
2285 ndev->dma = -1;
cc3c080d 2286 ret = platform_get_irq(pdev, 0);
2287 if (ret < 0) {
86a74ff2
NI
2288 ret = -ENODEV;
2289 goto out_release;
2290 }
cc3c080d 2291 ndev->irq = ret;
86a74ff2
NI
2292
2293 SET_NETDEV_DEV(ndev, &pdev->dev);
2294
2295 /* Fill in the fields of the device structure with ethernet values. */
2296 ether_setup(ndev);
2297
2298 mdp = netdev_priv(ndev);
ae70644d
YS
2299 mdp->addr = ioremap(res->start, resource_size(res));
2300 if (mdp->addr == NULL) {
2301 ret = -ENOMEM;
2302 dev_err(&pdev->dev, "ioremap failed.\n");
2303 goto out_release;
2304 }
2305
86a74ff2 2306 spin_lock_init(&mdp->lock);
bcd5149d
MD
2307 mdp->pdev = pdev;
2308 pm_runtime_enable(&pdev->dev);
2309 pm_runtime_resume(&pdev->dev);
86a74ff2 2310
71557a37 2311 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
86a74ff2 2312 /* get PHY ID */
71557a37 2313 mdp->phy_id = pd->phy;
e47c9052 2314 mdp->phy_interface = pd->phy_interface;
71557a37
YS
2315 /* EDMAC endian */
2316 mdp->edmac_endian = pd->edmac_endian;
4923576b
YS
2317 mdp->no_ether_link = pd->no_ether_link;
2318 mdp->ether_link_active_low = pd->ether_link_active_low;
4a55530f 2319 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
86a74ff2 2320
380af9e3 2321 /* set cpu data */
8fcd4961
YS
2322#if defined(SH_ETH_HAS_BOTH_MODULES)
2323 mdp->cd = sh_eth_get_cpu_data(mdp);
2324#else
380af9e3 2325 mdp->cd = &sh_eth_my_cpu_data;
8fcd4961 2326#endif
380af9e3
YS
2327 sh_eth_set_default_cpu_data(mdp->cd);
2328
86a74ff2 2329 /* set function */
ebf84eaa 2330 ndev->netdev_ops = &sh_eth_netdev_ops;
dc19e4e5 2331 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
86a74ff2
NI
2332 ndev->watchdog_timeo = TX_TIMEOUT;
2333
dc19e4e5
NI
2334 /* debug message level */
2335 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
86a74ff2
NI
2336
2337 /* read and set MAC address */
748031f9 2338 read_mac_address(ndev, pd->mac_addr);
86a74ff2 2339
6ba88021
YS
2340 /* ioremap the TSU registers */
2341 if (mdp->cd->tsu) {
2342 struct resource *rtsu;
2343 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2344 if (!rtsu) {
2345 dev_err(&pdev->dev, "Not found TSU resource\n");
2346 goto out_release;
2347 }
2348 mdp->tsu_addr = ioremap(rtsu->start,
2349 resource_size(rtsu));
6743fe6d 2350 mdp->port = devno % 2;
71cc7c37 2351 ndev->features = NETIF_F_HW_VLAN_FILTER;
6ba88021
YS
2352 }
2353
150647fb
YS
2354 /* initialize first or needed device */
2355 if (!devno || pd->needs_init) {
380af9e3
YS
2356 if (mdp->cd->chip_reset)
2357 mdp->cd->chip_reset(ndev);
86a74ff2 2358
4986b996
YS
2359 if (mdp->cd->tsu) {
2360 /* TSU init (Init only)*/
2361 sh_eth_tsu_init(mdp);
2362 }
86a74ff2
NI
2363 }
2364
2365 /* network device register */
2366 ret = register_netdev(ndev);
2367 if (ret)
2368 goto out_release;
2369
2370 /* mdio bus init */
b3017e6a 2371 ret = sh_mdio_init(ndev, pdev->id, pd);
86a74ff2
NI
2372 if (ret)
2373 goto out_unregister;
2374
25985edc 2375 /* print device information */
6cd9b49d
HS
2376 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2377 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
86a74ff2
NI
2378
2379 platform_set_drvdata(pdev, ndev);
2380
2381 return ret;
2382
2383out_unregister:
2384 unregister_netdev(ndev);
2385
2386out_release:
2387 /* net_dev free */
ae70644d
YS
2388 if (mdp && mdp->addr)
2389 iounmap(mdp->addr);
ec0d7551 2390 if (mdp && mdp->tsu_addr)
4986b996 2391 iounmap(mdp->tsu_addr);
86a74ff2
NI
2392 if (ndev)
2393 free_netdev(ndev);
2394
2395out:
2396 return ret;
2397}
2398
2399static int sh_eth_drv_remove(struct platform_device *pdev)
2400{
2401 struct net_device *ndev = platform_get_drvdata(pdev);
4986b996 2402 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2 2403
6ba88021
YS
2404 if (mdp->cd->tsu)
2405 iounmap(mdp->tsu_addr);
86a74ff2
NI
2406 sh_mdio_release(ndev);
2407 unregister_netdev(ndev);
bcd5149d 2408 pm_runtime_disable(&pdev->dev);
ae70644d 2409 iounmap(mdp->addr);
86a74ff2
NI
2410 free_netdev(ndev);
2411 platform_set_drvdata(pdev, NULL);
2412
2413 return 0;
2414}
2415
bcd5149d
MD
2416static int sh_eth_runtime_nop(struct device *dev)
2417{
2418 /*
2419 * Runtime PM callback shared between ->runtime_suspend()
2420 * and ->runtime_resume(). Simply returns success.
2421 *
2422 * This driver re-initializes all registers after
2423 * pm_runtime_get_sync() anyway so there is no need
2424 * to save and restore registers here.
2425 */
2426 return 0;
2427}
2428
2429static struct dev_pm_ops sh_eth_dev_pm_ops = {
2430 .runtime_suspend = sh_eth_runtime_nop,
2431 .runtime_resume = sh_eth_runtime_nop,
2432};
2433
86a74ff2
NI
2434static struct platform_driver sh_eth_driver = {
2435 .probe = sh_eth_drv_probe,
2436 .remove = sh_eth_drv_remove,
2437 .driver = {
2438 .name = CARDNAME,
bcd5149d 2439 .pm = &sh_eth_dev_pm_ops,
86a74ff2
NI
2440 },
2441};
2442
db62f684 2443module_platform_driver(sh_eth_driver);
86a74ff2
NI
2444
2445MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2446MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2447MODULE_LICENSE("GPL v2");