blob: 1a69504ed9c5df7863e9be2befbd65d32aa7397b [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>
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070027#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070028#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>
David S. Miller8decf862011-09-22 03:23:13 -040037#include <linux/interrupt.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000038#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#include <linux/ethtool.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000041#include <linux/sh_eth.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070042
43#include "sh_eth.h"
44
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000045#define SH_ETH_DEF_MSG_ENABLE \
46 (NETIF_MSG_LINK | \
47 NETIF_MSG_TIMER | \
48 NETIF_MSG_RX_ERR| \
49 NETIF_MSG_TX_ERR)
50
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000051/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000052#if defined(CONFIG_CPU_SUBTYPE_SH7724)
53#define SH_ETH_RESET_DEFAULT 1
54static void sh_eth_set_duplex(struct net_device *ndev)
55{
56 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000057
58 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000059 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000060 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000061 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000062}
63
64static void sh_eth_set_rate(struct net_device *ndev)
65{
66 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000067
68 switch (mdp->speed) {
69 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000070 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000071 break;
72 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000073 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000074 break;
75 default:
76 break;
77 }
78}
79
80/* SH7724 */
81static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
82 .set_duplex = sh_eth_set_duplex,
83 .set_rate = sh_eth_set_rate,
84
85 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
86 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
87 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
88
89 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
90 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
91 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
92 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
93
94 .apr = 1,
95 .mpr = 1,
96 .tpauser = 1,
97 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -080098 .rpadir = 1,
99 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000100};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000101#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000102#define SH_ETH_HAS_BOTH_MODULES 1
103#define SH_ETH_HAS_TSU 1
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000104static void sh_eth_set_duplex(struct net_device *ndev)
105{
106 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000107
108 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000109 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000110 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000111 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000112}
113
114static void sh_eth_set_rate(struct net_device *ndev)
115{
116 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000117
118 switch (mdp->speed) {
119 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000120 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000121 break;
122 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000123 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000124 break;
125 default:
126 break;
127 }
128}
129
130/* SH7757 */
131static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
132 .set_duplex = sh_eth_set_duplex,
133 .set_rate = sh_eth_set_rate,
134
135 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
136 .rmcr_value = 0x00000001,
137
138 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
139 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
140 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
141 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
142
143 .apr = 1,
144 .mpr = 1,
145 .tpauser = 1,
146 .hw_swap = 1,
147 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000148 .rpadir = 1,
149 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000150};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000151
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000152#define SH_GIGA_ETH_BASE 0xfee00000
153#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
154#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
155static void sh_eth_chip_reset_giga(struct net_device *ndev)
156{
157 int i;
158 unsigned long mahr[2], malr[2];
159
160 /* save MAHR and MALR */
161 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000162 malr[i] = ioread32((void *)GIGA_MALR(i));
163 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000164 }
165
166 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000167 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000168 mdelay(1);
169
170 /* restore MAHR and MALR */
171 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000172 iowrite32(malr[i], (void *)GIGA_MALR(i));
173 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000174 }
175}
176
177static int sh_eth_is_gether(struct sh_eth_private *mdp);
178static void sh_eth_reset(struct net_device *ndev)
179{
180 struct sh_eth_private *mdp = netdev_priv(ndev);
181 int cnt = 100;
182
183 if (sh_eth_is_gether(mdp)) {
184 sh_eth_write(ndev, 0x03, EDSR);
185 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
186 EDMR);
187 while (cnt > 0) {
188 if (!(sh_eth_read(ndev, EDMR) & 0x3))
189 break;
190 mdelay(1);
191 cnt--;
192 }
193 if (cnt < 0)
194 printk(KERN_ERR "Device reset fail\n");
195
196 /* Table Init */
197 sh_eth_write(ndev, 0x0, TDLAR);
198 sh_eth_write(ndev, 0x0, TDFAR);
199 sh_eth_write(ndev, 0x0, TDFXR);
200 sh_eth_write(ndev, 0x0, TDFFR);
201 sh_eth_write(ndev, 0x0, RDLAR);
202 sh_eth_write(ndev, 0x0, RDFAR);
203 sh_eth_write(ndev, 0x0, RDFXR);
204 sh_eth_write(ndev, 0x0, RDFFR);
205 } else {
206 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
207 EDMR);
208 mdelay(3);
209 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
210 EDMR);
211 }
212}
213
214static void sh_eth_set_duplex_giga(struct net_device *ndev)
215{
216 struct sh_eth_private *mdp = netdev_priv(ndev);
217
218 if (mdp->duplex) /* Full */
219 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
220 else /* Half */
221 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
222}
223
224static void sh_eth_set_rate_giga(struct net_device *ndev)
225{
226 struct sh_eth_private *mdp = netdev_priv(ndev);
227
228 switch (mdp->speed) {
229 case 10: /* 10BASE */
230 sh_eth_write(ndev, 0x00000000, GECMR);
231 break;
232 case 100:/* 100BASE */
233 sh_eth_write(ndev, 0x00000010, GECMR);
234 break;
235 case 1000: /* 1000BASE */
236 sh_eth_write(ndev, 0x00000020, GECMR);
237 break;
238 default:
239 break;
240 }
241}
242
243/* SH7757(GETHERC) */
244static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
245 .chip_reset = sh_eth_chip_reset_giga,
246 .set_duplex = sh_eth_set_duplex_giga,
247 .set_rate = sh_eth_set_rate_giga,
248
249 .ecsr_value = ECSR_ICD | ECSR_MPD,
250 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
251 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
252
253 .tx_check = EESR_TC1 | EESR_FTC,
254 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
255 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
256 EESR_ECI,
257 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
258 EESR_TFE,
259 .fdr_value = 0x0000072f,
260 .rmcr_value = 0x00000001,
261
262 .apr = 1,
263 .mpr = 1,
264 .tpauser = 1,
265 .bculr = 1,
266 .hw_swap = 1,
267 .rpadir = 1,
268 .rpadir_value = 2 << 16,
269 .no_trimd = 1,
270 .no_ade = 1,
271};
272
273static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
274{
275 if (sh_eth_is_gether(mdp))
276 return &sh_eth_my_cpu_data_giga;
277 else
278 return &sh_eth_my_cpu_data;
279}
280
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000281#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000282#define SH_ETH_HAS_TSU 1
283static void sh_eth_chip_reset(struct net_device *ndev)
284{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000285 struct sh_eth_private *mdp = netdev_priv(ndev);
286
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000287 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000288 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000289 mdelay(1);
290}
291
292static void sh_eth_reset(struct net_device *ndev)
293{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000294 int cnt = 100;
295
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000296 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000297 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000298 while (cnt > 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000299 if (!(sh_eth_read(ndev, EDMR) & 0x3))
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000300 break;
301 mdelay(1);
302 cnt--;
303 }
roel kluin890c8c12009-12-30 01:43:45 +0000304 if (cnt == 0)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000305 printk(KERN_ERR "Device reset fail\n");
306
307 /* Table Init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000308 sh_eth_write(ndev, 0x0, TDLAR);
309 sh_eth_write(ndev, 0x0, TDFAR);
310 sh_eth_write(ndev, 0x0, TDFXR);
311 sh_eth_write(ndev, 0x0, TDFFR);
312 sh_eth_write(ndev, 0x0, RDLAR);
313 sh_eth_write(ndev, 0x0, RDFAR);
314 sh_eth_write(ndev, 0x0, RDFXR);
315 sh_eth_write(ndev, 0x0, RDFFR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000316}
317
318static void sh_eth_set_duplex(struct net_device *ndev)
319{
320 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000321
322 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000323 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000324 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000325 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000326}
327
328static void sh_eth_set_rate(struct net_device *ndev)
329{
330 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000331
332 switch (mdp->speed) {
333 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000334 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000335 break;
336 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000337 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000338 break;
339 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000340 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000341 break;
342 default:
343 break;
344 }
345}
346
347/* sh7763 */
348static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
349 .chip_reset = sh_eth_chip_reset,
350 .set_duplex = sh_eth_set_duplex,
351 .set_rate = sh_eth_set_rate,
352
353 .ecsr_value = ECSR_ICD | ECSR_MPD,
354 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
355 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
356
357 .tx_check = EESR_TC1 | EESR_FTC,
358 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
359 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
360 EESR_ECI,
361 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
362 EESR_TFE,
363
364 .apr = 1,
365 .mpr = 1,
366 .tpauser = 1,
367 .bculr = 1,
368 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000369 .no_trimd = 1,
370 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000371 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000372};
373
374#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
375#define SH_ETH_RESET_DEFAULT 1
376static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
377 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
378
379 .apr = 1,
380 .mpr = 1,
381 .tpauser = 1,
382 .hw_swap = 1,
383};
384#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
385#define SH_ETH_RESET_DEFAULT 1
386#define SH_ETH_HAS_TSU 1
387static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
388 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000389 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000390};
391#endif
392
393static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
394{
395 if (!cd->ecsr_value)
396 cd->ecsr_value = DEFAULT_ECSR_INIT;
397
398 if (!cd->ecsipr_value)
399 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
400
401 if (!cd->fcftr_value)
402 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
403 DEFAULT_FIFO_F_D_RFD;
404
405 if (!cd->fdr_value)
406 cd->fdr_value = DEFAULT_FDR_INIT;
407
408 if (!cd->rmcr_value)
409 cd->rmcr_value = DEFAULT_RMCR_VALUE;
410
411 if (!cd->tx_check)
412 cd->tx_check = DEFAULT_TX_CHECK;
413
414 if (!cd->eesr_err_check)
415 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
416
417 if (!cd->tx_error_check)
418 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
419}
420
421#if defined(SH_ETH_RESET_DEFAULT)
422/* Chip Reset */
423static void sh_eth_reset(struct net_device *ndev)
424{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000425 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000426 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000427 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000428}
429#endif
430
431#if defined(CONFIG_CPU_SH4)
432static void sh_eth_set_receive_align(struct sk_buff *skb)
433{
434 int reserve;
435
436 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
437 if (reserve)
438 skb_reserve(skb, reserve);
439}
440#else
441static void sh_eth_set_receive_align(struct sk_buff *skb)
442{
443 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
444}
445#endif
446
447
Yoshinori Sato71557a32008-08-06 19:49:00 -0400448/* CPU <-> EDMAC endian convert */
449static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
450{
451 switch (mdp->edmac_endian) {
452 case EDMAC_LITTLE_ENDIAN:
453 return cpu_to_le32(x);
454 case EDMAC_BIG_ENDIAN:
455 return cpu_to_be32(x);
456 }
457 return x;
458}
459
460static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
461{
462 switch (mdp->edmac_endian) {
463 case EDMAC_LITTLE_ENDIAN:
464 return le32_to_cpu(x);
465 case EDMAC_BIG_ENDIAN:
466 return be32_to_cpu(x);
467 }
468 return x;
469}
470
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700471/*
472 * Program the hardware MAC address from dev->dev_addr.
473 */
474static void update_mac_address(struct net_device *ndev)
475{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000476 sh_eth_write(ndev,
477 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
478 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
479 sh_eth_write(ndev,
480 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700481}
482
483/*
484 * Get MAC address from SuperH MAC address register
485 *
486 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
487 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
488 * When you want use this device, you must set MAC address in bootloader.
489 *
490 */
Magnus Damm748031f2009-10-09 00:17:14 +0000491static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700492{
Magnus Damm748031f2009-10-09 00:17:14 +0000493 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
494 memcpy(ndev->dev_addr, mac, 6);
495 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000496 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
497 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
498 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
499 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
500 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
501 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000502 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700503}
504
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000505static int sh_eth_is_gether(struct sh_eth_private *mdp)
506{
507 if (mdp->reg_offset == sh_eth_offset_gigabit)
508 return 1;
509 else
510 return 0;
511}
512
513static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
514{
515 if (sh_eth_is_gether(mdp))
516 return EDTRR_TRNS_GETHER;
517 else
518 return EDTRR_TRNS_ETHER;
519}
520
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700521struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000522 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700523 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000524 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700525 u32 mmd_msk;/* MMD */
526 u32 mdo_msk;
527 u32 mdi_msk;
528 u32 mdc_msk;
529};
530
531/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000532static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700533{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000534 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700535}
536
537/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000538static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700539{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000540 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700541}
542
543/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000544static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700545{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000546 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700547}
548
549/* Data I/O pin control */
550static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
551{
552 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000553
554 if (bitbang->set_gate)
555 bitbang->set_gate(bitbang->addr);
556
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700557 if (bit)
558 bb_set(bitbang->addr, bitbang->mmd_msk);
559 else
560 bb_clr(bitbang->addr, bitbang->mmd_msk);
561}
562
563/* Set bit data*/
564static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
565{
566 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
567
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000568 if (bitbang->set_gate)
569 bitbang->set_gate(bitbang->addr);
570
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700571 if (bit)
572 bb_set(bitbang->addr, bitbang->mdo_msk);
573 else
574 bb_clr(bitbang->addr, bitbang->mdo_msk);
575}
576
577/* Get bit data*/
578static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
579{
580 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000581
582 if (bitbang->set_gate)
583 bitbang->set_gate(bitbang->addr);
584
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700585 return bb_read(bitbang->addr, bitbang->mdi_msk);
586}
587
588/* MDC pin control */
589static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
590{
591 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
592
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000593 if (bitbang->set_gate)
594 bitbang->set_gate(bitbang->addr);
595
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700596 if (bit)
597 bb_set(bitbang->addr, bitbang->mdc_msk);
598 else
599 bb_clr(bitbang->addr, bitbang->mdc_msk);
600}
601
602/* mdio bus control struct */
603static struct mdiobb_ops bb_ops = {
604 .owner = THIS_MODULE,
605 .set_mdc = sh_mdc_ctrl,
606 .set_mdio_dir = sh_mmd_ctrl,
607 .set_mdio_data = sh_set_mdio,
608 .get_mdio_data = sh_get_mdio,
609};
610
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700611/* free skb and descriptor buffer */
612static void sh_eth_ring_free(struct net_device *ndev)
613{
614 struct sh_eth_private *mdp = netdev_priv(ndev);
615 int i;
616
617 /* Free Rx skb ringbuffer */
618 if (mdp->rx_skbuff) {
619 for (i = 0; i < RX_RING_SIZE; i++) {
620 if (mdp->rx_skbuff[i])
621 dev_kfree_skb(mdp->rx_skbuff[i]);
622 }
623 }
624 kfree(mdp->rx_skbuff);
625
626 /* Free Tx skb ringbuffer */
627 if (mdp->tx_skbuff) {
628 for (i = 0; i < TX_RING_SIZE; i++) {
629 if (mdp->tx_skbuff[i])
630 dev_kfree_skb(mdp->tx_skbuff[i]);
631 }
632 }
633 kfree(mdp->tx_skbuff);
634}
635
636/* format skb and descriptor buffer */
637static void sh_eth_ring_format(struct net_device *ndev)
638{
639 struct sh_eth_private *mdp = netdev_priv(ndev);
640 int i;
641 struct sk_buff *skb;
642 struct sh_eth_rxdesc *rxdesc = NULL;
643 struct sh_eth_txdesc *txdesc = NULL;
644 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
645 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
646
647 mdp->cur_rx = mdp->cur_tx = 0;
648 mdp->dirty_rx = mdp->dirty_tx = 0;
649
650 memset(mdp->rx_ring, 0, rx_ringsize);
651
652 /* build Rx ring buffer */
653 for (i = 0; i < RX_RING_SIZE; i++) {
654 /* skb */
655 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000656 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700657 mdp->rx_skbuff[i] = skb;
658 if (skb == NULL)
659 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000660 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000661 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000662 sh_eth_set_receive_align(skb);
663
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700664 /* RX descriptor */
665 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000666 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400667 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700668
669 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000670 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900671 /* Rx descriptor address set */
672 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000673 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000674 if (sh_eth_is_gether(mdp))
675 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900676 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700677 }
678
679 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
680
681 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400682 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700683
684 memset(mdp->tx_ring, 0, tx_ringsize);
685
686 /* build Tx ring buffer */
687 for (i = 0; i < TX_RING_SIZE; i++) {
688 mdp->tx_skbuff[i] = NULL;
689 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400690 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700691 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900692 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400693 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000694 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000695 if (sh_eth_is_gether(mdp))
696 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900697 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700698 }
699
Yoshinori Sato71557a32008-08-06 19:49:00 -0400700 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700701}
702
703/* Get skb and descriptor buffer */
704static int sh_eth_ring_init(struct net_device *ndev)
705{
706 struct sh_eth_private *mdp = netdev_priv(ndev);
707 int rx_ringsize, tx_ringsize, ret = 0;
708
709 /*
710 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
711 * card needs room to do 8 byte alignment, +2 so we can reserve
712 * the first 2 bytes, and +16 gets room for the status word from the
713 * card.
714 */
715 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
716 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800717 if (mdp->cd->rpadir)
718 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700719
720 /* Allocate RX and TX skb rings */
721 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
722 GFP_KERNEL);
723 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000724 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700725 ret = -ENOMEM;
726 return ret;
727 }
728
729 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
730 GFP_KERNEL);
731 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000732 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700733 ret = -ENOMEM;
734 goto skb_ring_free;
735 }
736
737 /* Allocate all Rx descriptors. */
738 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
739 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
740 GFP_KERNEL);
741
742 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000743 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
744 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700745 ret = -ENOMEM;
746 goto desc_ring_free;
747 }
748
749 mdp->dirty_rx = 0;
750
751 /* Allocate all Tx descriptors. */
752 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
753 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
754 GFP_KERNEL);
755 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000756 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
757 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700758 ret = -ENOMEM;
759 goto desc_ring_free;
760 }
761 return ret;
762
763desc_ring_free:
764 /* free DMA buffer */
765 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
766
767skb_ring_free:
768 /* Free Rx and Tx skb ring buffer */
769 sh_eth_ring_free(ndev);
770
771 return ret;
772}
773
774static int sh_eth_dev_init(struct net_device *ndev)
775{
776 int ret = 0;
777 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700778 u_int32_t rx_int_var, tx_int_var;
779 u32 val;
780
781 /* Soft Reset */
782 sh_eth_reset(ndev);
783
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900784 /* Descriptor format */
785 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000786 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000787 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700788
789 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000790 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700791
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000792#if defined(__LITTLE_ENDIAN__)
793 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000794 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000795 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900796#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000797 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700798
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900799 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000800 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
801 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700802
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900803 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000804 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700805
806 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
807 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000808 sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700809
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000810 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000811 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900812
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000813 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900814
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000815 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000816 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700817
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900818 /* Recv frame limit set register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000819 sh_eth_write(ndev, RFLR_VALUE, RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700820
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000821 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
822 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700823
824 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000825 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700826 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
827
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000828 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900829
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000830 if (mdp->cd->set_rate)
831 mdp->cd->set_rate(ndev);
832
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900833 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000834 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900835
836 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000837 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700838
839 /* Set MAC address */
840 update_mac_address(ndev);
841
842 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000843 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000844 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000845 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000846 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000847 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000848 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900849
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700850 /* Setting the Rx mode will start the Rx process. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000851 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700852
853 netif_start_queue(ndev);
854
855 return ret;
856}
857
858/* free Tx skb function */
859static int sh_eth_txfree(struct net_device *ndev)
860{
861 struct sh_eth_private *mdp = netdev_priv(ndev);
862 struct sh_eth_txdesc *txdesc;
863 int freeNum = 0;
864 int entry = 0;
865
866 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
867 entry = mdp->dirty_tx % TX_RING_SIZE;
868 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400869 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700870 break;
871 /* Free the original skb. */
872 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +0000873 dma_unmap_single(&ndev->dev, txdesc->addr,
874 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700875 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
876 mdp->tx_skbuff[entry] = NULL;
877 freeNum++;
878 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400879 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700880 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400881 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700882
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000883 ndev->stats.tx_packets++;
884 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700885 }
886 return freeNum;
887}
888
889/* Packet receive function */
890static int sh_eth_rx(struct net_device *ndev)
891{
892 struct sh_eth_private *mdp = netdev_priv(ndev);
893 struct sh_eth_rxdesc *rxdesc;
894
895 int entry = mdp->cur_rx % RX_RING_SIZE;
896 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
897 struct sk_buff *skb;
898 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000899 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700900
901 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400902 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
903 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700904 pkt_len = rxdesc->frame_length;
905
906 if (--boguscnt < 0)
907 break;
908
909 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000910 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700911
912 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
913 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000914 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700915 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000916 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700917 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000918 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700919 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000920 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700921 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000922 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700923 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000924 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700925 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000926 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700927 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000928 if (!mdp->cd->hw_swap)
929 sh_eth_soft_swap(
930 phys_to_virt(ALIGN(rxdesc->addr, 4)),
931 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700932 skb = mdp->rx_skbuff[entry];
933 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -0800934 if (mdp->cd->rpadir)
935 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700936 skb_put(skb, pkt_len);
937 skb->protocol = eth_type_trans(skb, ndev);
938 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000939 ndev->stats.rx_packets++;
940 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700941 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400942 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700943 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000944 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700945 }
946
947 /* Refill the Rx ring buffers. */
948 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
949 entry = mdp->dirty_rx % RX_RING_SIZE;
950 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900951 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000952 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900953
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700954 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000955 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 mdp->rx_skbuff[entry] = skb;
957 if (skb == NULL)
958 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000959 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000960 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000961 sh_eth_set_receive_align(skb);
962
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700963 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000964 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700965 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700966 if (entry >= RX_RING_SIZE - 1)
967 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400968 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700969 else
970 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400971 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700972 }
973
974 /* Restart Rx engine if stopped. */
975 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000976 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R))
977 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700978
979 return 0;
980}
981
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000982static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000983{
984 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000985 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
986 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000987}
988
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000989static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000990{
991 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000992 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
993 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000994}
995
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700996/* error control function */
997static void sh_eth_error(struct net_device *ndev, int intr_status)
998{
999 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001000 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001001 u32 link_stat;
1002 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001003
1004 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001005 felic_stat = sh_eth_read(ndev, ECSR);
1006 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001007 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001008 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001009 if (felic_stat & ECSR_LCHNG) {
1010 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001011 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001012 if (mdp->link == PHY_DOWN)
1013 link_stat = 0;
1014 else
1015 link_stat = PHY_ST_LINK;
1016 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001017 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001018 if (mdp->ether_link_active_low)
1019 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001020 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001021 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001022 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001023 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001024 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001025 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1026 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001027 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001028 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1029 ECSR);
1030 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1031 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001032 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001033 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001034 }
1035 }
1036 }
1037
1038 if (intr_status & EESR_TWB) {
1039 /* Write buck end. unused write back interrupt */
1040 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001041 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001042 if (netif_msg_tx_err(mdp))
1043 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001044 }
1045
1046 if (intr_status & EESR_RABT) {
1047 /* Receive Abort int */
1048 if (intr_status & EESR_RFRMER) {
1049 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001050 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001051 if (netif_msg_rx_err(mdp))
1052 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001053 }
1054 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001055
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001056 if (intr_status & EESR_TDE) {
1057 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001058 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001059 if (netif_msg_tx_err(mdp))
1060 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1061 }
1062
1063 if (intr_status & EESR_TFE) {
1064 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001065 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001066 if (netif_msg_tx_err(mdp))
1067 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001068 }
1069
1070 if (intr_status & EESR_RDE) {
1071 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001072 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001073
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001074 if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R)
1075 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001076 if (netif_msg_rx_err(mdp))
1077 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001079
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001080 if (intr_status & EESR_RFE) {
1081 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001082 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001083 if (netif_msg_rx_err(mdp))
1084 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1085 }
1086
1087 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1088 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001089 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001090 if (netif_msg_tx_err(mdp))
1091 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001092 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001093
1094 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1095 if (mdp->cd->no_ade)
1096 mask &= ~EESR_ADE;
1097 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001098 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001099 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001100 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001101 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1102 intr_status, mdp->cur_tx);
1103 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001104 mdp->dirty_tx, (u32) ndev->state, edtrr);
1105 /* dirty buffer free */
1106 sh_eth_txfree(ndev);
1107
1108 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001109 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001110 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001111 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001112 }
1113 /* wakeup */
1114 netif_wake_queue(ndev);
1115 }
1116}
1117
1118static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1119{
1120 struct net_device *ndev = netdev;
1121 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001122 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001123 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001124 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001125
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126 spin_lock(&mdp->lock);
1127
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001128 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001129 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001130 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001131 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1132 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001133 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001134 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001135 ret = IRQ_HANDLED;
1136 } else
1137 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001138
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001139 if (intr_status & (EESR_FRC | /* Frame recv*/
1140 EESR_RMAF | /* Multi cast address recv*/
1141 EESR_RRF | /* Bit frame recv */
1142 EESR_RTLF | /* Long frame recv*/
1143 EESR_RTSF | /* short frame recv */
1144 EESR_PRE | /* PHY-LSI recv error */
1145 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001146 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001147 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001148
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001149 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001150 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001151 sh_eth_txfree(ndev);
1152 netif_wake_queue(ndev);
1153 }
1154
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001155 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001156 sh_eth_error(ndev, intr_status);
1157
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001158other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001159 spin_unlock(&mdp->lock);
1160
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001161 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001162}
1163
1164static void sh_eth_timer(unsigned long data)
1165{
1166 struct net_device *ndev = (struct net_device *)data;
1167 struct sh_eth_private *mdp = netdev_priv(ndev);
1168
1169 mod_timer(&mdp->timer, jiffies + (10 * HZ));
1170}
1171
1172/* PHY state control function */
1173static void sh_eth_adjust_link(struct net_device *ndev)
1174{
1175 struct sh_eth_private *mdp = netdev_priv(ndev);
1176 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001177 int new_state = 0;
1178
1179 if (phydev->link != PHY_DOWN) {
1180 if (phydev->duplex != mdp->duplex) {
1181 new_state = 1;
1182 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001183 if (mdp->cd->set_duplex)
1184 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185 }
1186
1187 if (phydev->speed != mdp->speed) {
1188 new_state = 1;
1189 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001190 if (mdp->cd->set_rate)
1191 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001192 }
1193 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001194 sh_eth_write(ndev,
1195 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001196 new_state = 1;
1197 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001198 }
1199 } else if (mdp->link) {
1200 new_state = 1;
1201 mdp->link = PHY_DOWN;
1202 mdp->speed = 0;
1203 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001204 }
1205
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001206 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001207 phy_print_status(phydev);
1208}
1209
1210/* PHY init function */
1211static int sh_eth_phy_init(struct net_device *ndev)
1212{
1213 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001214 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001215 struct phy_device *phydev = NULL;
1216
Kay Sieversfb28ad352008-11-10 13:55:14 -08001217 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001218 mdp->mii_bus->id , mdp->phy_id);
1219
1220 mdp->link = PHY_DOWN;
1221 mdp->speed = 0;
1222 mdp->duplex = -1;
1223
1224 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001225 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001226 0, mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001227 if (IS_ERR(phydev)) {
1228 dev_err(&ndev->dev, "phy_connect failed\n");
1229 return PTR_ERR(phydev);
1230 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001231
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001233 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001234
1235 mdp->phydev = phydev;
1236
1237 return 0;
1238}
1239
1240/* PHY control start function */
1241static int sh_eth_phy_start(struct net_device *ndev)
1242{
1243 struct sh_eth_private *mdp = netdev_priv(ndev);
1244 int ret;
1245
1246 ret = sh_eth_phy_init(ndev);
1247 if (ret)
1248 return ret;
1249
1250 /* reset phy - this also wakes it from PDOWN */
1251 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1252 phy_start(mdp->phydev);
1253
1254 return 0;
1255}
1256
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001257static int sh_eth_get_settings(struct net_device *ndev,
1258 struct ethtool_cmd *ecmd)
1259{
1260 struct sh_eth_private *mdp = netdev_priv(ndev);
1261 unsigned long flags;
1262 int ret;
1263
1264 spin_lock_irqsave(&mdp->lock, flags);
1265 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1266 spin_unlock_irqrestore(&mdp->lock, flags);
1267
1268 return ret;
1269}
1270
1271static int sh_eth_set_settings(struct net_device *ndev,
1272 struct ethtool_cmd *ecmd)
1273{
1274 struct sh_eth_private *mdp = netdev_priv(ndev);
1275 unsigned long flags;
1276 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001277
1278 spin_lock_irqsave(&mdp->lock, flags);
1279
1280 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001281 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001282
1283 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1284 if (ret)
1285 goto error_exit;
1286
1287 if (ecmd->duplex == DUPLEX_FULL)
1288 mdp->duplex = 1;
1289 else
1290 mdp->duplex = 0;
1291
1292 if (mdp->cd->set_duplex)
1293 mdp->cd->set_duplex(ndev);
1294
1295error_exit:
1296 mdelay(1);
1297
1298 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001299 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001300
1301 spin_unlock_irqrestore(&mdp->lock, flags);
1302
1303 return ret;
1304}
1305
1306static int sh_eth_nway_reset(struct net_device *ndev)
1307{
1308 struct sh_eth_private *mdp = netdev_priv(ndev);
1309 unsigned long flags;
1310 int ret;
1311
1312 spin_lock_irqsave(&mdp->lock, flags);
1313 ret = phy_start_aneg(mdp->phydev);
1314 spin_unlock_irqrestore(&mdp->lock, flags);
1315
1316 return ret;
1317}
1318
1319static u32 sh_eth_get_msglevel(struct net_device *ndev)
1320{
1321 struct sh_eth_private *mdp = netdev_priv(ndev);
1322 return mdp->msg_enable;
1323}
1324
1325static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1326{
1327 struct sh_eth_private *mdp = netdev_priv(ndev);
1328 mdp->msg_enable = value;
1329}
1330
1331static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1332 "rx_current", "tx_current",
1333 "rx_dirty", "tx_dirty",
1334};
1335#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1336
1337static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1338{
1339 switch (sset) {
1340 case ETH_SS_STATS:
1341 return SH_ETH_STATS_LEN;
1342 default:
1343 return -EOPNOTSUPP;
1344 }
1345}
1346
1347static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1348 struct ethtool_stats *stats, u64 *data)
1349{
1350 struct sh_eth_private *mdp = netdev_priv(ndev);
1351 int i = 0;
1352
1353 /* device-specific stats */
1354 data[i++] = mdp->cur_rx;
1355 data[i++] = mdp->cur_tx;
1356 data[i++] = mdp->dirty_rx;
1357 data[i++] = mdp->dirty_tx;
1358}
1359
1360static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1361{
1362 switch (stringset) {
1363 case ETH_SS_STATS:
1364 memcpy(data, *sh_eth_gstrings_stats,
1365 sizeof(sh_eth_gstrings_stats));
1366 break;
1367 }
1368}
1369
stephen hemminger9b07be42012-01-04 12:59:49 +00001370static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001371 .get_settings = sh_eth_get_settings,
1372 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001373 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001374 .get_msglevel = sh_eth_get_msglevel,
1375 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001376 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001377 .get_strings = sh_eth_get_strings,
1378 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1379 .get_sset_count = sh_eth_get_sset_count,
1380};
1381
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001382/* network device open function */
1383static int sh_eth_open(struct net_device *ndev)
1384{
1385 int ret = 0;
1386 struct sh_eth_private *mdp = netdev_priv(ndev);
1387
Magnus Dammbcd51492009-10-09 00:20:04 +00001388 pm_runtime_get_sync(&mdp->pdev->dev);
1389
Joe Perchesa0607fd2009-11-18 23:29:17 -08001390 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001391#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001392 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1393 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001394 IRQF_SHARED,
1395#else
1396 0,
1397#endif
1398 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001399 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001400 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 return ret;
1402 }
1403
1404 /* Descriptor set */
1405 ret = sh_eth_ring_init(ndev);
1406 if (ret)
1407 goto out_free_irq;
1408
1409 /* device init */
1410 ret = sh_eth_dev_init(ndev);
1411 if (ret)
1412 goto out_free_irq;
1413
1414 /* PHY control start*/
1415 ret = sh_eth_phy_start(ndev);
1416 if (ret)
1417 goto out_free_irq;
1418
1419 /* Set the timer to check for link beat. */
1420 init_timer(&mdp->timer);
1421 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001422 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001423
1424 return ret;
1425
1426out_free_irq:
1427 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001428 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001429 return ret;
1430}
1431
1432/* Timeout function */
1433static void sh_eth_tx_timeout(struct net_device *ndev)
1434{
1435 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001436 struct sh_eth_rxdesc *rxdesc;
1437 int i;
1438
1439 netif_stop_queue(ndev);
1440
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001441 if (netif_msg_timer(mdp))
1442 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001443 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001444
1445 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001446 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447
1448 /* timer off */
1449 del_timer_sync(&mdp->timer);
1450
1451 /* Free all the skbuffs in the Rx queue. */
1452 for (i = 0; i < RX_RING_SIZE; i++) {
1453 rxdesc = &mdp->rx_ring[i];
1454 rxdesc->status = 0;
1455 rxdesc->addr = 0xBADF00D0;
1456 if (mdp->rx_skbuff[i])
1457 dev_kfree_skb(mdp->rx_skbuff[i]);
1458 mdp->rx_skbuff[i] = NULL;
1459 }
1460 for (i = 0; i < TX_RING_SIZE; i++) {
1461 if (mdp->tx_skbuff[i])
1462 dev_kfree_skb(mdp->tx_skbuff[i]);
1463 mdp->tx_skbuff[i] = NULL;
1464 }
1465
1466 /* device init */
1467 sh_eth_dev_init(ndev);
1468
1469 /* timer on */
1470 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1471 add_timer(&mdp->timer);
1472}
1473
1474/* Packet transmit function */
1475static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1476{
1477 struct sh_eth_private *mdp = netdev_priv(ndev);
1478 struct sh_eth_txdesc *txdesc;
1479 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001480 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001481
1482 spin_lock_irqsave(&mdp->lock, flags);
1483 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1484 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001485 if (netif_msg_tx_queued(mdp))
1486 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001487 netif_stop_queue(ndev);
1488 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001489 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001490 }
1491 }
1492 spin_unlock_irqrestore(&mdp->lock, flags);
1493
1494 entry = mdp->cur_tx % TX_RING_SIZE;
1495 mdp->tx_skbuff[entry] = skb;
1496 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001497 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001498 if (!mdp->cd->hw_swap)
1499 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1500 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001501 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1502 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001503 if (skb->len < ETHERSMALL)
1504 txdesc->buffer_length = ETHERSMALL;
1505 else
1506 txdesc->buffer_length = skb->len;
1507
1508 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001509 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001510 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001511 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001512
1513 mdp->cur_tx++;
1514
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001515 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1516 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001517
Patrick McHardy6ed10652009-06-23 06:03:08 +00001518 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001519}
1520
1521/* device close function */
1522static int sh_eth_close(struct net_device *ndev)
1523{
1524 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001525 int ringsize;
1526
1527 netif_stop_queue(ndev);
1528
1529 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001530 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531
1532 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001533 sh_eth_write(ndev, 0, EDTRR);
1534 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535
1536 /* PHY Disconnect */
1537 if (mdp->phydev) {
1538 phy_stop(mdp->phydev);
1539 phy_disconnect(mdp->phydev);
1540 }
1541
1542 free_irq(ndev->irq, ndev);
1543
1544 del_timer_sync(&mdp->timer);
1545
1546 /* Free all the skbuffs in the Rx queue. */
1547 sh_eth_ring_free(ndev);
1548
1549 /* free DMA buffer */
1550 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1551 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1552
1553 /* free DMA buffer */
1554 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1555 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1556
Magnus Dammbcd51492009-10-09 00:20:04 +00001557 pm_runtime_put_sync(&mdp->pdev->dev);
1558
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001559 return 0;
1560}
1561
1562static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1563{
1564 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001565
Magnus Dammbcd51492009-10-09 00:20:04 +00001566 pm_runtime_get_sync(&mdp->pdev->dev);
1567
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001568 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001569 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001570 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001571 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001572 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001573 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001574 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001575 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001576 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001577 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001578 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1579 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001580 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001581 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1582 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001583 pm_runtime_put_sync(&mdp->pdev->dev);
1584
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001585 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001586}
1587
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001588/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001589static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1590 int cmd)
1591{
1592 struct sh_eth_private *mdp = netdev_priv(ndev);
1593 struct phy_device *phydev = mdp->phydev;
1594
1595 if (!netif_running(ndev))
1596 return -EINVAL;
1597
1598 if (!phydev)
1599 return -ENODEV;
1600
Richard Cochran28b04112010-07-17 08:48:55 +00001601 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001602}
1603
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001604#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001605/* Multicast reception directions set */
1606static void sh_eth_set_multicast_list(struct net_device *ndev)
1607{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001608 if (ndev->flags & IFF_PROMISC) {
1609 /* Set promiscuous. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001610 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) |
1611 ECMR_PRM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001612 } else {
1613 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001614 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) |
1615 ECMR_MCT, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 }
1617}
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001618#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001619
1620/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001621static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001623 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
1624 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
1625 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
1626 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
1627 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
1628 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
1629 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
1630 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
1631 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
1632 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001633 if (sh_eth_is_gether(mdp)) {
1634 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
1635 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
1636 } else {
1637 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
1638 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
1639 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001640 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
1641 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
1642 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
1643 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
1644 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
1645 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
1646 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001647}
1648
1649/* MDIO bus release function */
1650static int sh_mdio_release(struct net_device *ndev)
1651{
1652 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1653
1654 /* unregister mdio bus */
1655 mdiobus_unregister(bus);
1656
1657 /* remove mdio bus info from net_device */
1658 dev_set_drvdata(&ndev->dev, NULL);
1659
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00001660 /* free interrupts memory */
1661 kfree(bus->irq);
1662
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001663 /* free bitbang info */
1664 free_mdio_bitbang(bus);
1665
1666 return 0;
1667}
1668
1669/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001670static int sh_mdio_init(struct net_device *ndev, int id,
1671 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001672{
1673 int ret, i;
1674 struct bb_info *bitbang;
1675 struct sh_eth_private *mdp = netdev_priv(ndev);
1676
1677 /* create bit control struct for PHY */
1678 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1679 if (!bitbang) {
1680 ret = -ENOMEM;
1681 goto out;
1682 }
1683
1684 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001685 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001686 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001687 bitbang->mdi_msk = 0x08;
1688 bitbang->mdo_msk = 0x04;
1689 bitbang->mmd_msk = 0x02;/* MMD */
1690 bitbang->mdc_msk = 0x01;
1691 bitbang->ctrl.ops = &bb_ops;
1692
Stefan Weilc2e07b32010-08-03 19:44:52 +02001693 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001694 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1695 if (!mdp->mii_bus) {
1696 ret = -ENOMEM;
1697 goto out_free_bitbang;
1698 }
1699
1700 /* Hook up MII support for ethtool */
1701 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001702 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00001703 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00001704 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001705
1706 /* PHY IRQ */
1707 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1708 if (!mdp->mii_bus->irq) {
1709 ret = -ENOMEM;
1710 goto out_free_bus;
1711 }
1712
1713 for (i = 0; i < PHY_MAX_ADDR; i++)
1714 mdp->mii_bus->irq[i] = PHY_POLL;
1715
1716 /* regist mdio bus */
1717 ret = mdiobus_register(mdp->mii_bus);
1718 if (ret)
1719 goto out_free_irq;
1720
1721 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1722
1723 return 0;
1724
1725out_free_irq:
1726 kfree(mdp->mii_bus->irq);
1727
1728out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001729 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001730
1731out_free_bitbang:
1732 kfree(bitbang);
1733
1734out:
1735 return ret;
1736}
1737
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001738static const u16 *sh_eth_get_register_offset(int register_type)
1739{
1740 const u16 *reg_offset = NULL;
1741
1742 switch (register_type) {
1743 case SH_ETH_REG_GIGABIT:
1744 reg_offset = sh_eth_offset_gigabit;
1745 break;
1746 case SH_ETH_REG_FAST_SH4:
1747 reg_offset = sh_eth_offset_fast_sh4;
1748 break;
1749 case SH_ETH_REG_FAST_SH3_SH2:
1750 reg_offset = sh_eth_offset_fast_sh3_sh2;
1751 break;
1752 default:
1753 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
1754 break;
1755 }
1756
1757 return reg_offset;
1758}
1759
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001760static const struct net_device_ops sh_eth_netdev_ops = {
1761 .ndo_open = sh_eth_open,
1762 .ndo_stop = sh_eth_close,
1763 .ndo_start_xmit = sh_eth_start_xmit,
1764 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001765#if defined(SH_ETH_HAS_TSU)
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001766 .ndo_set_rx_mode = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001767#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001768 .ndo_tx_timeout = sh_eth_tx_timeout,
1769 .ndo_do_ioctl = sh_eth_do_ioctl,
1770 .ndo_validate_addr = eth_validate_addr,
1771 .ndo_set_mac_address = eth_mac_addr,
1772 .ndo_change_mtu = eth_change_mtu,
1773};
1774
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001775static int sh_eth_drv_probe(struct platform_device *pdev)
1776{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07001777 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001778 struct resource *res;
1779 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00001780 struct sh_eth_private *mdp = NULL;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001781 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001782
1783 /* get base addr */
1784 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1785 if (unlikely(res == NULL)) {
1786 dev_err(&pdev->dev, "invalid resource\n");
1787 ret = -EINVAL;
1788 goto out;
1789 }
1790
1791 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1792 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001793 ret = -ENOMEM;
1794 goto out;
1795 }
1796
1797 /* The sh Ether-specific entries in the device structure. */
1798 ndev->base_addr = res->start;
1799 devno = pdev->id;
1800 if (devno < 0)
1801 devno = 0;
1802
1803 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001804 ret = platform_get_irq(pdev, 0);
1805 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001806 ret = -ENODEV;
1807 goto out_release;
1808 }
roel kluincc3c0802008-09-10 19:22:44 +02001809 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001810
1811 SET_NETDEV_DEV(ndev, &pdev->dev);
1812
1813 /* Fill in the fields of the device structure with ethernet values. */
1814 ether_setup(ndev);
1815
1816 mdp = netdev_priv(ndev);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001817 mdp->addr = ioremap(res->start, resource_size(res));
1818 if (mdp->addr == NULL) {
1819 ret = -ENOMEM;
1820 dev_err(&pdev->dev, "ioremap failed.\n");
1821 goto out_release;
1822 }
1823
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001824 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001825 mdp->pdev = pdev;
1826 pm_runtime_enable(&pdev->dev);
1827 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001828
Yoshinori Sato71557a32008-08-06 19:49:00 -04001829 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001830 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001831 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001832 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001833 /* EDMAC endian */
1834 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001835 mdp->no_ether_link = pd->no_ether_link;
1836 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001837 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001838
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001839 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001840#if defined(SH_ETH_HAS_BOTH_MODULES)
1841 mdp->cd = sh_eth_get_cpu_data(mdp);
1842#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001843 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001844#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001845 sh_eth_set_default_cpu_data(mdp->cd);
1846
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001847 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001848 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001849 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001850 ndev->watchdog_timeo = TX_TIMEOUT;
1851
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001852 /* debug message level */
1853 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001854 mdp->post_rx = POST_RX >> (devno << 1);
1855 mdp->post_fw = POST_FW >> (devno << 1);
1856
1857 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001858 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001859
1860 /* First device only init */
1861 if (!devno) {
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001862 if (mdp->cd->tsu) {
1863 struct resource *rtsu;
1864 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1865 if (!rtsu) {
1866 dev_err(&pdev->dev, "Not found TSU resource\n");
1867 goto out_release;
1868 }
1869 mdp->tsu_addr = ioremap(rtsu->start,
1870 resource_size(rtsu));
1871 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001872 if (mdp->cd->chip_reset)
1873 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001874
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001875 if (mdp->cd->tsu) {
1876 /* TSU init (Init only)*/
1877 sh_eth_tsu_init(mdp);
1878 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001879 }
1880
1881 /* network device register */
1882 ret = register_netdev(ndev);
1883 if (ret)
1884 goto out_release;
1885
1886 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001887 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001888 if (ret)
1889 goto out_unregister;
1890
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001891 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08001892 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1893 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001894
1895 platform_set_drvdata(pdev, ndev);
1896
1897 return ret;
1898
1899out_unregister:
1900 unregister_netdev(ndev);
1901
1902out_release:
1903 /* net_dev free */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001904 if (mdp && mdp->addr)
1905 iounmap(mdp->addr);
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00001906 if (mdp && mdp->tsu_addr)
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001907 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001908 if (ndev)
1909 free_netdev(ndev);
1910
1911out:
1912 return ret;
1913}
1914
1915static int sh_eth_drv_remove(struct platform_device *pdev)
1916{
1917 struct net_device *ndev = platform_get_drvdata(pdev);
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001918 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001919
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001920 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001921 sh_mdio_release(ndev);
1922 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001923 pm_runtime_disable(&pdev->dev);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001924 iounmap(mdp->addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001925 free_netdev(ndev);
1926 platform_set_drvdata(pdev, NULL);
1927
1928 return 0;
1929}
1930
Magnus Dammbcd51492009-10-09 00:20:04 +00001931static int sh_eth_runtime_nop(struct device *dev)
1932{
1933 /*
1934 * Runtime PM callback shared between ->runtime_suspend()
1935 * and ->runtime_resume(). Simply returns success.
1936 *
1937 * This driver re-initializes all registers after
1938 * pm_runtime_get_sync() anyway so there is no need
1939 * to save and restore registers here.
1940 */
1941 return 0;
1942}
1943
1944static struct dev_pm_ops sh_eth_dev_pm_ops = {
1945 .runtime_suspend = sh_eth_runtime_nop,
1946 .runtime_resume = sh_eth_runtime_nop,
1947};
1948
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001949static struct platform_driver sh_eth_driver = {
1950 .probe = sh_eth_drv_probe,
1951 .remove = sh_eth_drv_remove,
1952 .driver = {
1953 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001954 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001955 },
1956};
1957
Axel Lindb62f682011-11-27 16:44:17 +00001958module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001959
1960MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1961MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1962MODULE_LICENSE("GPL v2");