blob: 8a72a979ee711262a613ddbe6e0fd3276b6f77d4 [file] [log] [blame]
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001/*
2 * SuperH Ethernet device driver
3 *
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09004 * Copyright (C) 2006-2008 Nobuhiro Iwamatsu
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00005 * Copyright (C) 2008-2009 Renesas Solutions Corp.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 */
22
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070023#include <linux/init.h>
24#include <linux/dma-mapping.h>
25#include <linux/etherdevice.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/mdio-bitbang.h>
29#include <linux/netdevice.h>
30#include <linux/phy.h>
31#include <linux/cache.h>
32#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000033#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000035#include <linux/ethtool.h>
Nobuhiro Iwamatsuf568a922009-10-26 13:49:50 +000036#include <asm/cacheflush.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070037
38#include "sh_eth.h"
39
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#define SH_ETH_DEF_MSG_ENABLE \
41 (NETIF_MSG_LINK | \
42 NETIF_MSG_TIMER | \
43 NETIF_MSG_RX_ERR| \
44 NETIF_MSG_TX_ERR)
45
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000046/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000047#if defined(CONFIG_CPU_SUBTYPE_SH7724)
48#define SH_ETH_RESET_DEFAULT 1
49static void sh_eth_set_duplex(struct net_device *ndev)
50{
51 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000052
53 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000054 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000055 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000056 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000057}
58
59static void sh_eth_set_rate(struct net_device *ndev)
60{
61 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000062
63 switch (mdp->speed) {
64 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000065 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000066 break;
67 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000068 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000069 break;
70 default:
71 break;
72 }
73}
74
75/* SH7724 */
76static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
77 .set_duplex = sh_eth_set_duplex,
78 .set_rate = sh_eth_set_rate,
79
80 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
81 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
82 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
83
84 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
85 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
86 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
87 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
88
89 .apr = 1,
90 .mpr = 1,
91 .tpauser = 1,
92 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -080093 .rpadir = 1,
94 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000095};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000096#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +000097#define SH_ETH_HAS_BOTH_MODULES 1
98#define SH_ETH_HAS_TSU 1
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000099static void sh_eth_set_duplex(struct net_device *ndev)
100{
101 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000102
103 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000104 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000105 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000106 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000107}
108
109static void sh_eth_set_rate(struct net_device *ndev)
110{
111 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000112
113 switch (mdp->speed) {
114 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000115 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000116 break;
117 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000118 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000119 break;
120 default:
121 break;
122 }
123}
124
125/* SH7757 */
126static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
127 .set_duplex = sh_eth_set_duplex,
128 .set_rate = sh_eth_set_rate,
129
130 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
131 .rmcr_value = 0x00000001,
132
133 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
134 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
135 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
136 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
137
138 .apr = 1,
139 .mpr = 1,
140 .tpauser = 1,
141 .hw_swap = 1,
142 .no_ade = 1,
143};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000144
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000145#define SH_GIGA_ETH_BASE 0xfee00000
146#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
147#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
148static void sh_eth_chip_reset_giga(struct net_device *ndev)
149{
150 int i;
151 unsigned long mahr[2], malr[2];
152
153 /* save MAHR and MALR */
154 for (i = 0; i < 2; i++) {
155 malr[i] = readl(GIGA_MALR(i));
156 mahr[i] = readl(GIGA_MAHR(i));
157 }
158
159 /* reset device */
160 writel(ARSTR_ARSTR, SH_GIGA_ETH_BASE + 0x1800);
161 mdelay(1);
162
163 /* restore MAHR and MALR */
164 for (i = 0; i < 2; i++) {
165 writel(malr[i], GIGA_MALR(i));
166 writel(mahr[i], GIGA_MAHR(i));
167 }
168}
169
170static int sh_eth_is_gether(struct sh_eth_private *mdp);
171static void sh_eth_reset(struct net_device *ndev)
172{
173 struct sh_eth_private *mdp = netdev_priv(ndev);
174 int cnt = 100;
175
176 if (sh_eth_is_gether(mdp)) {
177 sh_eth_write(ndev, 0x03, EDSR);
178 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
179 EDMR);
180 while (cnt > 0) {
181 if (!(sh_eth_read(ndev, EDMR) & 0x3))
182 break;
183 mdelay(1);
184 cnt--;
185 }
186 if (cnt < 0)
187 printk(KERN_ERR "Device reset fail\n");
188
189 /* Table Init */
190 sh_eth_write(ndev, 0x0, TDLAR);
191 sh_eth_write(ndev, 0x0, TDFAR);
192 sh_eth_write(ndev, 0x0, TDFXR);
193 sh_eth_write(ndev, 0x0, TDFFR);
194 sh_eth_write(ndev, 0x0, RDLAR);
195 sh_eth_write(ndev, 0x0, RDFAR);
196 sh_eth_write(ndev, 0x0, RDFXR);
197 sh_eth_write(ndev, 0x0, RDFFR);
198 } else {
199 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
200 EDMR);
201 mdelay(3);
202 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
203 EDMR);
204 }
205}
206
207static void sh_eth_set_duplex_giga(struct net_device *ndev)
208{
209 struct sh_eth_private *mdp = netdev_priv(ndev);
210
211 if (mdp->duplex) /* Full */
212 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
213 else /* Half */
214 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
215}
216
217static void sh_eth_set_rate_giga(struct net_device *ndev)
218{
219 struct sh_eth_private *mdp = netdev_priv(ndev);
220
221 switch (mdp->speed) {
222 case 10: /* 10BASE */
223 sh_eth_write(ndev, 0x00000000, GECMR);
224 break;
225 case 100:/* 100BASE */
226 sh_eth_write(ndev, 0x00000010, GECMR);
227 break;
228 case 1000: /* 1000BASE */
229 sh_eth_write(ndev, 0x00000020, GECMR);
230 break;
231 default:
232 break;
233 }
234}
235
236/* SH7757(GETHERC) */
237static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
238 .chip_reset = sh_eth_chip_reset_giga,
239 .set_duplex = sh_eth_set_duplex_giga,
240 .set_rate = sh_eth_set_rate_giga,
241
242 .ecsr_value = ECSR_ICD | ECSR_MPD,
243 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
244 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
245
246 .tx_check = EESR_TC1 | EESR_FTC,
247 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
248 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
249 EESR_ECI,
250 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
251 EESR_TFE,
252 .fdr_value = 0x0000072f,
253 .rmcr_value = 0x00000001,
254
255 .apr = 1,
256 .mpr = 1,
257 .tpauser = 1,
258 .bculr = 1,
259 .hw_swap = 1,
260 .rpadir = 1,
261 .rpadir_value = 2 << 16,
262 .no_trimd = 1,
263 .no_ade = 1,
264};
265
266static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
267{
268 if (sh_eth_is_gether(mdp))
269 return &sh_eth_my_cpu_data_giga;
270 else
271 return &sh_eth_my_cpu_data;
272}
273
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000274#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000275#define SH_ETH_HAS_TSU 1
276static void sh_eth_chip_reset(struct net_device *ndev)
277{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000278 struct sh_eth_private *mdp = netdev_priv(ndev);
279
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000280 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000281 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000282 mdelay(1);
283}
284
285static void sh_eth_reset(struct net_device *ndev)
286{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000287 int cnt = 100;
288
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000289 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000290 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000291 while (cnt > 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000292 if (!(sh_eth_read(ndev, EDMR) & 0x3))
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000293 break;
294 mdelay(1);
295 cnt--;
296 }
roel kluin890c8c12009-12-30 01:43:45 +0000297 if (cnt == 0)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000298 printk(KERN_ERR "Device reset fail\n");
299
300 /* Table Init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000301 sh_eth_write(ndev, 0x0, TDLAR);
302 sh_eth_write(ndev, 0x0, TDFAR);
303 sh_eth_write(ndev, 0x0, TDFXR);
304 sh_eth_write(ndev, 0x0, TDFFR);
305 sh_eth_write(ndev, 0x0, RDLAR);
306 sh_eth_write(ndev, 0x0, RDFAR);
307 sh_eth_write(ndev, 0x0, RDFXR);
308 sh_eth_write(ndev, 0x0, RDFFR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000309}
310
311static void sh_eth_set_duplex(struct net_device *ndev)
312{
313 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000314
315 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000316 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000317 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000318 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000319}
320
321static void sh_eth_set_rate(struct net_device *ndev)
322{
323 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000324
325 switch (mdp->speed) {
326 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000327 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000328 break;
329 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000330 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000331 break;
332 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000333 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000334 break;
335 default:
336 break;
337 }
338}
339
340/* sh7763 */
341static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
342 .chip_reset = sh_eth_chip_reset,
343 .set_duplex = sh_eth_set_duplex,
344 .set_rate = sh_eth_set_rate,
345
346 .ecsr_value = ECSR_ICD | ECSR_MPD,
347 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
348 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
349
350 .tx_check = EESR_TC1 | EESR_FTC,
351 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
352 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
353 EESR_ECI,
354 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
355 EESR_TFE,
356
357 .apr = 1,
358 .mpr = 1,
359 .tpauser = 1,
360 .bculr = 1,
361 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000362 .no_trimd = 1,
363 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000364 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000365};
366
367#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
368#define SH_ETH_RESET_DEFAULT 1
369static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
370 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
371
372 .apr = 1,
373 .mpr = 1,
374 .tpauser = 1,
375 .hw_swap = 1,
376};
377#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
378#define SH_ETH_RESET_DEFAULT 1
379#define SH_ETH_HAS_TSU 1
380static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
381 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000382 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000383};
384#endif
385
386static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
387{
388 if (!cd->ecsr_value)
389 cd->ecsr_value = DEFAULT_ECSR_INIT;
390
391 if (!cd->ecsipr_value)
392 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
393
394 if (!cd->fcftr_value)
395 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
396 DEFAULT_FIFO_F_D_RFD;
397
398 if (!cd->fdr_value)
399 cd->fdr_value = DEFAULT_FDR_INIT;
400
401 if (!cd->rmcr_value)
402 cd->rmcr_value = DEFAULT_RMCR_VALUE;
403
404 if (!cd->tx_check)
405 cd->tx_check = DEFAULT_TX_CHECK;
406
407 if (!cd->eesr_err_check)
408 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
409
410 if (!cd->tx_error_check)
411 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
412}
413
414#if defined(SH_ETH_RESET_DEFAULT)
415/* Chip Reset */
416static void sh_eth_reset(struct net_device *ndev)
417{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000418 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000419 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000420 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000421}
422#endif
423
424#if defined(CONFIG_CPU_SH4)
425static void sh_eth_set_receive_align(struct sk_buff *skb)
426{
427 int reserve;
428
429 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
430 if (reserve)
431 skb_reserve(skb, reserve);
432}
433#else
434static void sh_eth_set_receive_align(struct sk_buff *skb)
435{
436 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
437}
438#endif
439
440
Yoshinori Sato71557a32008-08-06 19:49:00 -0400441/* CPU <-> EDMAC endian convert */
442static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
443{
444 switch (mdp->edmac_endian) {
445 case EDMAC_LITTLE_ENDIAN:
446 return cpu_to_le32(x);
447 case EDMAC_BIG_ENDIAN:
448 return cpu_to_be32(x);
449 }
450 return x;
451}
452
453static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
454{
455 switch (mdp->edmac_endian) {
456 case EDMAC_LITTLE_ENDIAN:
457 return le32_to_cpu(x);
458 case EDMAC_BIG_ENDIAN:
459 return be32_to_cpu(x);
460 }
461 return x;
462}
463
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700464/*
465 * Program the hardware MAC address from dev->dev_addr.
466 */
467static void update_mac_address(struct net_device *ndev)
468{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000469 sh_eth_write(ndev,
470 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
471 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
472 sh_eth_write(ndev,
473 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700474}
475
476/*
477 * Get MAC address from SuperH MAC address register
478 *
479 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
480 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
481 * When you want use this device, you must set MAC address in bootloader.
482 *
483 */
Magnus Damm748031f2009-10-09 00:17:14 +0000484static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700485{
Magnus Damm748031f2009-10-09 00:17:14 +0000486 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
487 memcpy(ndev->dev_addr, mac, 6);
488 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000489 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
490 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
491 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
492 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
493 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
494 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000495 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700496}
497
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000498static int sh_eth_is_gether(struct sh_eth_private *mdp)
499{
500 if (mdp->reg_offset == sh_eth_offset_gigabit)
501 return 1;
502 else
503 return 0;
504}
505
506static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
507{
508 if (sh_eth_is_gether(mdp))
509 return EDTRR_TRNS_GETHER;
510 else
511 return EDTRR_TRNS_ETHER;
512}
513
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700514struct bb_info {
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000515 void (*set_gate)(unsigned long addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700516 struct mdiobb_ctrl ctrl;
517 u32 addr;
518 u32 mmd_msk;/* MMD */
519 u32 mdo_msk;
520 u32 mdi_msk;
521 u32 mdc_msk;
522};
523
524/* PHY bit set */
525static void bb_set(u32 addr, u32 msk)
526{
Paul Mundt900fcf02010-11-01 09:29:24 +0000527 writel(readl(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700528}
529
530/* PHY bit clear */
531static void bb_clr(u32 addr, u32 msk)
532{
Paul Mundt900fcf02010-11-01 09:29:24 +0000533 writel((readl(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700534}
535
536/* PHY bit read */
537static int bb_read(u32 addr, u32 msk)
538{
Paul Mundt900fcf02010-11-01 09:29:24 +0000539 return (readl(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700540}
541
542/* Data I/O pin control */
543static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
544{
545 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000546
547 if (bitbang->set_gate)
548 bitbang->set_gate(bitbang->addr);
549
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700550 if (bit)
551 bb_set(bitbang->addr, bitbang->mmd_msk);
552 else
553 bb_clr(bitbang->addr, bitbang->mmd_msk);
554}
555
556/* Set bit data*/
557static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
558{
559 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
560
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000561 if (bitbang->set_gate)
562 bitbang->set_gate(bitbang->addr);
563
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700564 if (bit)
565 bb_set(bitbang->addr, bitbang->mdo_msk);
566 else
567 bb_clr(bitbang->addr, bitbang->mdo_msk);
568}
569
570/* Get bit data*/
571static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
572{
573 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000574
575 if (bitbang->set_gate)
576 bitbang->set_gate(bitbang->addr);
577
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700578 return bb_read(bitbang->addr, bitbang->mdi_msk);
579}
580
581/* MDC pin control */
582static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
583{
584 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
585
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000586 if (bitbang->set_gate)
587 bitbang->set_gate(bitbang->addr);
588
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700589 if (bit)
590 bb_set(bitbang->addr, bitbang->mdc_msk);
591 else
592 bb_clr(bitbang->addr, bitbang->mdc_msk);
593}
594
595/* mdio bus control struct */
596static struct mdiobb_ops bb_ops = {
597 .owner = THIS_MODULE,
598 .set_mdc = sh_mdc_ctrl,
599 .set_mdio_dir = sh_mmd_ctrl,
600 .set_mdio_data = sh_set_mdio,
601 .get_mdio_data = sh_get_mdio,
602};
603
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700604/* free skb and descriptor buffer */
605static void sh_eth_ring_free(struct net_device *ndev)
606{
607 struct sh_eth_private *mdp = netdev_priv(ndev);
608 int i;
609
610 /* Free Rx skb ringbuffer */
611 if (mdp->rx_skbuff) {
612 for (i = 0; i < RX_RING_SIZE; i++) {
613 if (mdp->rx_skbuff[i])
614 dev_kfree_skb(mdp->rx_skbuff[i]);
615 }
616 }
617 kfree(mdp->rx_skbuff);
618
619 /* Free Tx skb ringbuffer */
620 if (mdp->tx_skbuff) {
621 for (i = 0; i < TX_RING_SIZE; i++) {
622 if (mdp->tx_skbuff[i])
623 dev_kfree_skb(mdp->tx_skbuff[i]);
624 }
625 }
626 kfree(mdp->tx_skbuff);
627}
628
629/* format skb and descriptor buffer */
630static void sh_eth_ring_format(struct net_device *ndev)
631{
632 struct sh_eth_private *mdp = netdev_priv(ndev);
633 int i;
634 struct sk_buff *skb;
635 struct sh_eth_rxdesc *rxdesc = NULL;
636 struct sh_eth_txdesc *txdesc = NULL;
637 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
638 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
639
640 mdp->cur_rx = mdp->cur_tx = 0;
641 mdp->dirty_rx = mdp->dirty_tx = 0;
642
643 memset(mdp->rx_ring, 0, rx_ringsize);
644
645 /* build Rx ring buffer */
646 for (i = 0; i < RX_RING_SIZE; i++) {
647 /* skb */
648 mdp->rx_skbuff[i] = NULL;
649 skb = dev_alloc_skb(mdp->rx_buf_sz);
650 mdp->rx_skbuff[i] = skb;
651 if (skb == NULL)
652 break;
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000653 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
654 DMA_FROM_DEVICE);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900655 skb->dev = ndev; /* Mark as being used by this device. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000656 sh_eth_set_receive_align(skb);
657
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700658 /* RX descriptor */
659 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000660 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400661 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700662
663 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000664 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900665 /* Rx descriptor address set */
666 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000667 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000668 if (sh_eth_is_gether(mdp))
669 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900670 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700671 }
672
673 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
674
675 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400676 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700677
678 memset(mdp->tx_ring, 0, tx_ringsize);
679
680 /* build Tx ring buffer */
681 for (i = 0; i < TX_RING_SIZE; i++) {
682 mdp->tx_skbuff[i] = NULL;
683 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400684 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700685 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900686 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400687 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000688 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000689 if (sh_eth_is_gether(mdp))
690 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900691 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700692 }
693
Yoshinori Sato71557a32008-08-06 19:49:00 -0400694 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700695}
696
697/* Get skb and descriptor buffer */
698static int sh_eth_ring_init(struct net_device *ndev)
699{
700 struct sh_eth_private *mdp = netdev_priv(ndev);
701 int rx_ringsize, tx_ringsize, ret = 0;
702
703 /*
704 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
705 * card needs room to do 8 byte alignment, +2 so we can reserve
706 * the first 2 bytes, and +16 gets room for the status word from the
707 * card.
708 */
709 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
710 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800711 if (mdp->cd->rpadir)
712 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700713
714 /* Allocate RX and TX skb rings */
715 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
716 GFP_KERNEL);
717 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000718 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700719 ret = -ENOMEM;
720 return ret;
721 }
722
723 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
724 GFP_KERNEL);
725 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000726 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700727 ret = -ENOMEM;
728 goto skb_ring_free;
729 }
730
731 /* Allocate all Rx descriptors. */
732 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
733 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
734 GFP_KERNEL);
735
736 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000737 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
738 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700739 ret = -ENOMEM;
740 goto desc_ring_free;
741 }
742
743 mdp->dirty_rx = 0;
744
745 /* Allocate all Tx descriptors. */
746 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
747 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
748 GFP_KERNEL);
749 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000750 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
751 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700752 ret = -ENOMEM;
753 goto desc_ring_free;
754 }
755 return ret;
756
757desc_ring_free:
758 /* free DMA buffer */
759 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
760
761skb_ring_free:
762 /* Free Rx and Tx skb ring buffer */
763 sh_eth_ring_free(ndev);
764
765 return ret;
766}
767
768static int sh_eth_dev_init(struct net_device *ndev)
769{
770 int ret = 0;
771 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700772 u_int32_t rx_int_var, tx_int_var;
773 u32 val;
774
775 /* Soft Reset */
776 sh_eth_reset(ndev);
777
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900778 /* Descriptor format */
779 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000780 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000781 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700782
783 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000784 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700785
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000786#if defined(__LITTLE_ENDIAN__)
787 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000788 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000789 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900790#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000791 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700792
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900793 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000794 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
795 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700796
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900797 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000798 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700799
800 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
801 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000802 sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700803
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000804 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000805 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900806
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000807 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900808
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000809 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000810 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700811
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900812 /* Recv frame limit set register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000813 sh_eth_write(ndev, RFLR_VALUE, RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700814
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000815 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
816 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700817
818 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000819 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700820 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
821
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000822 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900823
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000824 if (mdp->cd->set_rate)
825 mdp->cd->set_rate(ndev);
826
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900827 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000828 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900829
830 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000831 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700832
833 /* Set MAC address */
834 update_mac_address(ndev);
835
836 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000837 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000838 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000839 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000840 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000841 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000842 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900843
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700844 /* Setting the Rx mode will start the Rx process. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000845 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700846
847 netif_start_queue(ndev);
848
849 return ret;
850}
851
852/* free Tx skb function */
853static int sh_eth_txfree(struct net_device *ndev)
854{
855 struct sh_eth_private *mdp = netdev_priv(ndev);
856 struct sh_eth_txdesc *txdesc;
857 int freeNum = 0;
858 int entry = 0;
859
860 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
861 entry = mdp->dirty_tx % TX_RING_SIZE;
862 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400863 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700864 break;
865 /* Free the original skb. */
866 if (mdp->tx_skbuff[entry]) {
867 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
868 mdp->tx_skbuff[entry] = NULL;
869 freeNum++;
870 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400871 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700872 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400873 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700874
875 mdp->stats.tx_packets++;
876 mdp->stats.tx_bytes += txdesc->buffer_length;
877 }
878 return freeNum;
879}
880
881/* Packet receive function */
882static int sh_eth_rx(struct net_device *ndev)
883{
884 struct sh_eth_private *mdp = netdev_priv(ndev);
885 struct sh_eth_rxdesc *rxdesc;
886
887 int entry = mdp->cur_rx % RX_RING_SIZE;
888 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
889 struct sk_buff *skb;
890 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000891 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700892
893 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400894 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
895 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700896 pkt_len = rxdesc->frame_length;
897
898 if (--boguscnt < 0)
899 break;
900
901 if (!(desc_status & RDFEND))
902 mdp->stats.rx_length_errors++;
903
904 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
905 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
906 mdp->stats.rx_errors++;
907 if (desc_status & RD_RFS1)
908 mdp->stats.rx_crc_errors++;
909 if (desc_status & RD_RFS2)
910 mdp->stats.rx_frame_errors++;
911 if (desc_status & RD_RFS3)
912 mdp->stats.rx_length_errors++;
913 if (desc_status & RD_RFS4)
914 mdp->stats.rx_length_errors++;
915 if (desc_status & RD_RFS6)
916 mdp->stats.rx_missed_errors++;
917 if (desc_status & RD_RFS10)
918 mdp->stats.rx_over_errors++;
919 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000920 if (!mdp->cd->hw_swap)
921 sh_eth_soft_swap(
922 phys_to_virt(ALIGN(rxdesc->addr, 4)),
923 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700924 skb = mdp->rx_skbuff[entry];
925 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -0800926 if (mdp->cd->rpadir)
927 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700928 skb_put(skb, pkt_len);
929 skb->protocol = eth_type_trans(skb, ndev);
930 netif_rx(skb);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700931 mdp->stats.rx_packets++;
932 mdp->stats.rx_bytes += pkt_len;
933 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400934 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700935 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000936 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700937 }
938
939 /* Refill the Rx ring buffers. */
940 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
941 entry = mdp->dirty_rx % RX_RING_SIZE;
942 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900943 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000944 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900945
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700946 if (mdp->rx_skbuff[entry] == NULL) {
947 skb = dev_alloc_skb(mdp->rx_buf_sz);
948 mdp->rx_skbuff[entry] = skb;
949 if (skb == NULL)
950 break; /* Better luck next round. */
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000951 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
952 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700953 skb->dev = ndev;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000954 sh_eth_set_receive_align(skb);
955
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700956 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000957 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700958 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700959 if (entry >= RX_RING_SIZE - 1)
960 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400961 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700962 else
963 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400964 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700965 }
966
967 /* Restart Rx engine if stopped. */
968 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000969 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R))
970 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700971
972 return 0;
973}
974
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000975static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000976{
977 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000978 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
979 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000980}
981
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000982static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000983{
984 /* enable 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
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700989/* error control function */
990static void sh_eth_error(struct net_device *ndev, int intr_status)
991{
992 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700993 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000994 u32 link_stat;
995 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700996
997 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000998 felic_stat = sh_eth_read(ndev, ECSR);
999 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001000 if (felic_stat & ECSR_ICD)
1001 mdp->stats.tx_carrier_errors++;
1002 if (felic_stat & ECSR_LCHNG) {
1003 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001004 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001005 if (mdp->link == PHY_DOWN)
1006 link_stat = 0;
1007 else
1008 link_stat = PHY_ST_LINK;
1009 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001010 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001011 if (mdp->ether_link_active_low)
1012 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001013 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001014 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001015 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001016 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001017 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001018 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1019 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001021 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1022 ECSR);
1023 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1024 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001025 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001026 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001027 }
1028 }
1029 }
1030
1031 if (intr_status & EESR_TWB) {
1032 /* Write buck end. unused write back interrupt */
1033 if (intr_status & EESR_TABT) /* Transmit Abort int */
1034 mdp->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001035 if (netif_msg_tx_err(mdp))
1036 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001037 }
1038
1039 if (intr_status & EESR_RABT) {
1040 /* Receive Abort int */
1041 if (intr_status & EESR_RFRMER) {
1042 /* Receive Frame Overflow int */
1043 mdp->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001044 if (netif_msg_rx_err(mdp))
1045 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001046 }
1047 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001048
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001049 if (intr_status & EESR_TDE) {
1050 /* Transmit Descriptor Empty int */
1051 mdp->stats.tx_fifo_errors++;
1052 if (netif_msg_tx_err(mdp))
1053 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1054 }
1055
1056 if (intr_status & EESR_TFE) {
1057 /* FIFO under flow */
1058 mdp->stats.tx_fifo_errors++;
1059 if (netif_msg_tx_err(mdp))
1060 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001061 }
1062
1063 if (intr_status & EESR_RDE) {
1064 /* Receive Descriptor Empty int */
1065 mdp->stats.rx_over_errors++;
1066
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001067 if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R)
1068 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001069 if (netif_msg_rx_err(mdp))
1070 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001071 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001072
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001073 if (intr_status & EESR_RFE) {
1074 /* Receive FIFO Overflow int */
1075 mdp->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001076 if (netif_msg_rx_err(mdp))
1077 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1078 }
1079
1080 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1081 /* Address Error */
1082 mdp->stats.tx_fifo_errors++;
1083 if (netif_msg_tx_err(mdp))
1084 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001085 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001086
1087 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1088 if (mdp->cd->no_ade)
1089 mask &= ~EESR_ADE;
1090 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001091 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001092 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001093 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001094 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1095 intr_status, mdp->cur_tx);
1096 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001097 mdp->dirty_tx, (u32) ndev->state, edtrr);
1098 /* dirty buffer free */
1099 sh_eth_txfree(ndev);
1100
1101 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001102 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001103 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001104 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001105 }
1106 /* wakeup */
1107 netif_wake_queue(ndev);
1108 }
1109}
1110
1111static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1112{
1113 struct net_device *ndev = netdev;
1114 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001115 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001116 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001117 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001118
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001119 spin_lock(&mdp->lock);
1120
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001121 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001122 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001123 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001124 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1125 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001126 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001127 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001128 ret = IRQ_HANDLED;
1129 } else
1130 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001131
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001132 if (intr_status & (EESR_FRC | /* Frame recv*/
1133 EESR_RMAF | /* Multi cast address recv*/
1134 EESR_RRF | /* Bit frame recv */
1135 EESR_RTLF | /* Long frame recv*/
1136 EESR_RTSF | /* short frame recv */
1137 EESR_PRE | /* PHY-LSI recv error */
1138 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001139 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001140 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001141
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001142 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001143 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144 sh_eth_txfree(ndev);
1145 netif_wake_queue(ndev);
1146 }
1147
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001148 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001149 sh_eth_error(ndev, intr_status);
1150
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001151other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001152 spin_unlock(&mdp->lock);
1153
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001154 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155}
1156
1157static void sh_eth_timer(unsigned long data)
1158{
1159 struct net_device *ndev = (struct net_device *)data;
1160 struct sh_eth_private *mdp = netdev_priv(ndev);
1161
1162 mod_timer(&mdp->timer, jiffies + (10 * HZ));
1163}
1164
1165/* PHY state control function */
1166static void sh_eth_adjust_link(struct net_device *ndev)
1167{
1168 struct sh_eth_private *mdp = netdev_priv(ndev);
1169 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170 int new_state = 0;
1171
1172 if (phydev->link != PHY_DOWN) {
1173 if (phydev->duplex != mdp->duplex) {
1174 new_state = 1;
1175 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001176 if (mdp->cd->set_duplex)
1177 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001178 }
1179
1180 if (phydev->speed != mdp->speed) {
1181 new_state = 1;
1182 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001183 if (mdp->cd->set_rate)
1184 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185 }
1186 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001187 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_TXF)
1188 | ECMR_DM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001189 new_state = 1;
1190 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001191 }
1192 } else if (mdp->link) {
1193 new_state = 1;
1194 mdp->link = PHY_DOWN;
1195 mdp->speed = 0;
1196 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001197 }
1198
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001199 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001200 phy_print_status(phydev);
1201}
1202
1203/* PHY init function */
1204static int sh_eth_phy_init(struct net_device *ndev)
1205{
1206 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001207 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001208 struct phy_device *phydev = NULL;
1209
Kay Sieversfb28ad32008-11-10 13:55:14 -08001210 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001211 mdp->mii_bus->id , mdp->phy_id);
1212
1213 mdp->link = PHY_DOWN;
1214 mdp->speed = 0;
1215 mdp->duplex = -1;
1216
1217 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001218 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001219 0, mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220 if (IS_ERR(phydev)) {
1221 dev_err(&ndev->dev, "phy_connect failed\n");
1222 return PTR_ERR(phydev);
1223 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001224
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001225 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001226 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001227
1228 mdp->phydev = phydev;
1229
1230 return 0;
1231}
1232
1233/* PHY control start function */
1234static int sh_eth_phy_start(struct net_device *ndev)
1235{
1236 struct sh_eth_private *mdp = netdev_priv(ndev);
1237 int ret;
1238
1239 ret = sh_eth_phy_init(ndev);
1240 if (ret)
1241 return ret;
1242
1243 /* reset phy - this also wakes it from PDOWN */
1244 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1245 phy_start(mdp->phydev);
1246
1247 return 0;
1248}
1249
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001250static int sh_eth_get_settings(struct net_device *ndev,
1251 struct ethtool_cmd *ecmd)
1252{
1253 struct sh_eth_private *mdp = netdev_priv(ndev);
1254 unsigned long flags;
1255 int ret;
1256
1257 spin_lock_irqsave(&mdp->lock, flags);
1258 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1259 spin_unlock_irqrestore(&mdp->lock, flags);
1260
1261 return ret;
1262}
1263
1264static int sh_eth_set_settings(struct net_device *ndev,
1265 struct ethtool_cmd *ecmd)
1266{
1267 struct sh_eth_private *mdp = netdev_priv(ndev);
1268 unsigned long flags;
1269 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001270
1271 spin_lock_irqsave(&mdp->lock, flags);
1272
1273 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001274 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001275
1276 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1277 if (ret)
1278 goto error_exit;
1279
1280 if (ecmd->duplex == DUPLEX_FULL)
1281 mdp->duplex = 1;
1282 else
1283 mdp->duplex = 0;
1284
1285 if (mdp->cd->set_duplex)
1286 mdp->cd->set_duplex(ndev);
1287
1288error_exit:
1289 mdelay(1);
1290
1291 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001292 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001293
1294 spin_unlock_irqrestore(&mdp->lock, flags);
1295
1296 return ret;
1297}
1298
1299static int sh_eth_nway_reset(struct net_device *ndev)
1300{
1301 struct sh_eth_private *mdp = netdev_priv(ndev);
1302 unsigned long flags;
1303 int ret;
1304
1305 spin_lock_irqsave(&mdp->lock, flags);
1306 ret = phy_start_aneg(mdp->phydev);
1307 spin_unlock_irqrestore(&mdp->lock, flags);
1308
1309 return ret;
1310}
1311
1312static u32 sh_eth_get_msglevel(struct net_device *ndev)
1313{
1314 struct sh_eth_private *mdp = netdev_priv(ndev);
1315 return mdp->msg_enable;
1316}
1317
1318static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1319{
1320 struct sh_eth_private *mdp = netdev_priv(ndev);
1321 mdp->msg_enable = value;
1322}
1323
1324static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1325 "rx_current", "tx_current",
1326 "rx_dirty", "tx_dirty",
1327};
1328#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1329
1330static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1331{
1332 switch (sset) {
1333 case ETH_SS_STATS:
1334 return SH_ETH_STATS_LEN;
1335 default:
1336 return -EOPNOTSUPP;
1337 }
1338}
1339
1340static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1341 struct ethtool_stats *stats, u64 *data)
1342{
1343 struct sh_eth_private *mdp = netdev_priv(ndev);
1344 int i = 0;
1345
1346 /* device-specific stats */
1347 data[i++] = mdp->cur_rx;
1348 data[i++] = mdp->cur_tx;
1349 data[i++] = mdp->dirty_rx;
1350 data[i++] = mdp->dirty_tx;
1351}
1352
1353static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1354{
1355 switch (stringset) {
1356 case ETH_SS_STATS:
1357 memcpy(data, *sh_eth_gstrings_stats,
1358 sizeof(sh_eth_gstrings_stats));
1359 break;
1360 }
1361}
1362
1363static struct ethtool_ops sh_eth_ethtool_ops = {
1364 .get_settings = sh_eth_get_settings,
1365 .set_settings = sh_eth_set_settings,
1366 .nway_reset = sh_eth_nway_reset,
1367 .get_msglevel = sh_eth_get_msglevel,
1368 .set_msglevel = sh_eth_set_msglevel,
1369 .get_link = ethtool_op_get_link,
1370 .get_strings = sh_eth_get_strings,
1371 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1372 .get_sset_count = sh_eth_get_sset_count,
1373};
1374
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001375/* network device open function */
1376static int sh_eth_open(struct net_device *ndev)
1377{
1378 int ret = 0;
1379 struct sh_eth_private *mdp = netdev_priv(ndev);
1380
Magnus Dammbcd51492009-10-09 00:20:04 +00001381 pm_runtime_get_sync(&mdp->pdev->dev);
1382
Joe Perchesa0607fd2009-11-18 23:29:17 -08001383 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001384#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001385 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1386 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001387 IRQF_SHARED,
1388#else
1389 0,
1390#endif
1391 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001392 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001393 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394 return ret;
1395 }
1396
1397 /* Descriptor set */
1398 ret = sh_eth_ring_init(ndev);
1399 if (ret)
1400 goto out_free_irq;
1401
1402 /* device init */
1403 ret = sh_eth_dev_init(ndev);
1404 if (ret)
1405 goto out_free_irq;
1406
1407 /* PHY control start*/
1408 ret = sh_eth_phy_start(ndev);
1409 if (ret)
1410 goto out_free_irq;
1411
1412 /* Set the timer to check for link beat. */
1413 init_timer(&mdp->timer);
1414 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001415 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001416
1417 return ret;
1418
1419out_free_irq:
1420 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001421 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422 return ret;
1423}
1424
1425/* Timeout function */
1426static void sh_eth_tx_timeout(struct net_device *ndev)
1427{
1428 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001429 struct sh_eth_rxdesc *rxdesc;
1430 int i;
1431
1432 netif_stop_queue(ndev);
1433
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001434 if (netif_msg_timer(mdp))
1435 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001436 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001437
1438 /* tx_errors count up */
1439 mdp->stats.tx_errors++;
1440
1441 /* timer off */
1442 del_timer_sync(&mdp->timer);
1443
1444 /* Free all the skbuffs in the Rx queue. */
1445 for (i = 0; i < RX_RING_SIZE; i++) {
1446 rxdesc = &mdp->rx_ring[i];
1447 rxdesc->status = 0;
1448 rxdesc->addr = 0xBADF00D0;
1449 if (mdp->rx_skbuff[i])
1450 dev_kfree_skb(mdp->rx_skbuff[i]);
1451 mdp->rx_skbuff[i] = NULL;
1452 }
1453 for (i = 0; i < TX_RING_SIZE; i++) {
1454 if (mdp->tx_skbuff[i])
1455 dev_kfree_skb(mdp->tx_skbuff[i]);
1456 mdp->tx_skbuff[i] = NULL;
1457 }
1458
1459 /* device init */
1460 sh_eth_dev_init(ndev);
1461
1462 /* timer on */
1463 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1464 add_timer(&mdp->timer);
1465}
1466
1467/* Packet transmit function */
1468static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1469{
1470 struct sh_eth_private *mdp = netdev_priv(ndev);
1471 struct sh_eth_txdesc *txdesc;
1472 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001473 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001474
1475 spin_lock_irqsave(&mdp->lock, flags);
1476 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1477 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001478 if (netif_msg_tx_queued(mdp))
1479 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 netif_stop_queue(ndev);
1481 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001482 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 }
1484 }
1485 spin_unlock_irqrestore(&mdp->lock, flags);
1486
1487 entry = mdp->cur_tx % TX_RING_SIZE;
1488 mdp->tx_skbuff[entry] = skb;
1489 txdesc = &mdp->tx_ring[entry];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001490 txdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001491 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001492 if (!mdp->cd->hw_swap)
1493 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1494 skb->len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001495 /* write back */
1496 __flush_purge_region(skb->data, skb->len);
1497 if (skb->len < ETHERSMALL)
1498 txdesc->buffer_length = ETHERSMALL;
1499 else
1500 txdesc->buffer_length = skb->len;
1501
1502 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001503 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001504 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001505 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506
1507 mdp->cur_tx++;
1508
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001509 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1510 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001511
Patrick McHardy6ed10652009-06-23 06:03:08 +00001512 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001513}
1514
1515/* device close function */
1516static int sh_eth_close(struct net_device *ndev)
1517{
1518 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001519 int ringsize;
1520
1521 netif_stop_queue(ndev);
1522
1523 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001524 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001525
1526 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001527 sh_eth_write(ndev, 0, EDTRR);
1528 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529
1530 /* PHY Disconnect */
1531 if (mdp->phydev) {
1532 phy_stop(mdp->phydev);
1533 phy_disconnect(mdp->phydev);
1534 }
1535
1536 free_irq(ndev->irq, ndev);
1537
1538 del_timer_sync(&mdp->timer);
1539
1540 /* Free all the skbuffs in the Rx queue. */
1541 sh_eth_ring_free(ndev);
1542
1543 /* free DMA buffer */
1544 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1545 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1546
1547 /* free DMA buffer */
1548 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1549 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1550
Magnus Dammbcd51492009-10-09 00:20:04 +00001551 pm_runtime_put_sync(&mdp->pdev->dev);
1552
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001553 return 0;
1554}
1555
1556static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1557{
1558 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001559
Magnus Dammbcd51492009-10-09 00:20:04 +00001560 pm_runtime_get_sync(&mdp->pdev->dev);
1561
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001562 mdp->stats.tx_dropped += sh_eth_read(ndev, TROCR);
1563 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
1564 mdp->stats.collisions += sh_eth_read(ndev, CDCR);
1565 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
1566 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
1567 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001568 if (sh_eth_is_gether(mdp)) {
1569 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
1570 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
1571 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
1572 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1573 } else {
1574 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
1575 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1576 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001577 pm_runtime_put_sync(&mdp->pdev->dev);
1578
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 return &mdp->stats;
1580}
1581
1582/* ioctl to device funciotn*/
1583static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1584 int cmd)
1585{
1586 struct sh_eth_private *mdp = netdev_priv(ndev);
1587 struct phy_device *phydev = mdp->phydev;
1588
1589 if (!netif_running(ndev))
1590 return -EINVAL;
1591
1592 if (!phydev)
1593 return -ENODEV;
1594
Richard Cochran28b04112010-07-17 08:48:55 +00001595 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001596}
1597
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001598#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001599/* Multicast reception directions set */
1600static void sh_eth_set_multicast_list(struct net_device *ndev)
1601{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001602 if (ndev->flags & IFF_PROMISC) {
1603 /* Set promiscuous. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001604 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) |
1605 ECMR_PRM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001606 } else {
1607 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001608 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) |
1609 ECMR_MCT, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610 }
1611}
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001612#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001613
1614/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001615static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001617 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
1618 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
1619 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
1620 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
1621 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
1622 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
1623 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
1624 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
1625 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
1626 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001627 if (sh_eth_is_gether(mdp)) {
1628 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
1629 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
1630 } else {
1631 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
1632 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
1633 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001634 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
1635 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
1636 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
1637 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
1638 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
1639 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
1640 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001641}
1642
1643/* MDIO bus release function */
1644static int sh_mdio_release(struct net_device *ndev)
1645{
1646 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1647
1648 /* unregister mdio bus */
1649 mdiobus_unregister(bus);
1650
1651 /* remove mdio bus info from net_device */
1652 dev_set_drvdata(&ndev->dev, NULL);
1653
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00001654 /* free interrupts memory */
1655 kfree(bus->irq);
1656
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001657 /* free bitbang info */
1658 free_mdio_bitbang(bus);
1659
1660 return 0;
1661}
1662
1663/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001664static int sh_mdio_init(struct net_device *ndev, int id,
1665 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001666{
1667 int ret, i;
1668 struct bb_info *bitbang;
1669 struct sh_eth_private *mdp = netdev_priv(ndev);
1670
1671 /* create bit control struct for PHY */
1672 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1673 if (!bitbang) {
1674 ret = -ENOMEM;
1675 goto out;
1676 }
1677
1678 /* bitbang init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001679 bitbang->addr = ndev->base_addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001680 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001681 bitbang->mdi_msk = 0x08;
1682 bitbang->mdo_msk = 0x04;
1683 bitbang->mmd_msk = 0x02;/* MMD */
1684 bitbang->mdc_msk = 0x01;
1685 bitbang->ctrl.ops = &bb_ops;
1686
Stefan Weilc2e07b32010-08-03 19:44:52 +02001687 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001688 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1689 if (!mdp->mii_bus) {
1690 ret = -ENOMEM;
1691 goto out_free_bitbang;
1692 }
1693
1694 /* Hook up MII support for ethtool */
1695 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001696 mdp->mii_bus->parent = &ndev->dev;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001697 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001698
1699 /* PHY IRQ */
1700 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1701 if (!mdp->mii_bus->irq) {
1702 ret = -ENOMEM;
1703 goto out_free_bus;
1704 }
1705
1706 for (i = 0; i < PHY_MAX_ADDR; i++)
1707 mdp->mii_bus->irq[i] = PHY_POLL;
1708
1709 /* regist mdio bus */
1710 ret = mdiobus_register(mdp->mii_bus);
1711 if (ret)
1712 goto out_free_irq;
1713
1714 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1715
1716 return 0;
1717
1718out_free_irq:
1719 kfree(mdp->mii_bus->irq);
1720
1721out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001722 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001723
1724out_free_bitbang:
1725 kfree(bitbang);
1726
1727out:
1728 return ret;
1729}
1730
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001731static const u16 *sh_eth_get_register_offset(int register_type)
1732{
1733 const u16 *reg_offset = NULL;
1734
1735 switch (register_type) {
1736 case SH_ETH_REG_GIGABIT:
1737 reg_offset = sh_eth_offset_gigabit;
1738 break;
1739 case SH_ETH_REG_FAST_SH4:
1740 reg_offset = sh_eth_offset_fast_sh4;
1741 break;
1742 case SH_ETH_REG_FAST_SH3_SH2:
1743 reg_offset = sh_eth_offset_fast_sh3_sh2;
1744 break;
1745 default:
1746 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
1747 break;
1748 }
1749
1750 return reg_offset;
1751}
1752
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001753static const struct net_device_ops sh_eth_netdev_ops = {
1754 .ndo_open = sh_eth_open,
1755 .ndo_stop = sh_eth_close,
1756 .ndo_start_xmit = sh_eth_start_xmit,
1757 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001758#if defined(SH_ETH_HAS_TSU)
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001759 .ndo_set_multicast_list = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001760#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001761 .ndo_tx_timeout = sh_eth_tx_timeout,
1762 .ndo_do_ioctl = sh_eth_do_ioctl,
1763 .ndo_validate_addr = eth_validate_addr,
1764 .ndo_set_mac_address = eth_mac_addr,
1765 .ndo_change_mtu = eth_change_mtu,
1766};
1767
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001768static int sh_eth_drv_probe(struct platform_device *pdev)
1769{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07001770 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001771 struct resource *res;
1772 struct net_device *ndev = NULL;
1773 struct sh_eth_private *mdp;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001774 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001775
1776 /* get base addr */
1777 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1778 if (unlikely(res == NULL)) {
1779 dev_err(&pdev->dev, "invalid resource\n");
1780 ret = -EINVAL;
1781 goto out;
1782 }
1783
1784 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1785 if (!ndev) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001786 dev_err(&pdev->dev, "Could not allocate device.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001787 ret = -ENOMEM;
1788 goto out;
1789 }
1790
1791 /* The sh Ether-specific entries in the device structure. */
1792 ndev->base_addr = res->start;
1793 devno = pdev->id;
1794 if (devno < 0)
1795 devno = 0;
1796
1797 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001798 ret = platform_get_irq(pdev, 0);
1799 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800 ret = -ENODEV;
1801 goto out_release;
1802 }
roel kluincc3c0802008-09-10 19:22:44 +02001803 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001804
1805 SET_NETDEV_DEV(ndev, &pdev->dev);
1806
1807 /* Fill in the fields of the device structure with ethernet values. */
1808 ether_setup(ndev);
1809
1810 mdp = netdev_priv(ndev);
1811 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001812 mdp->pdev = pdev;
1813 pm_runtime_enable(&pdev->dev);
1814 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001815
Yoshinori Sato71557a32008-08-06 19:49:00 -04001816 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001817 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001818 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001819 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001820 /* EDMAC endian */
1821 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001822 mdp->no_ether_link = pd->no_ether_link;
1823 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001824 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001825
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001826 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001827#if defined(SH_ETH_HAS_BOTH_MODULES)
1828 mdp->cd = sh_eth_get_cpu_data(mdp);
1829#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001830 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001831#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001832 sh_eth_set_default_cpu_data(mdp->cd);
1833
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001834 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001835 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001836 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001837 ndev->watchdog_timeo = TX_TIMEOUT;
1838
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001839 /* debug message level */
1840 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001841 mdp->post_rx = POST_RX >> (devno << 1);
1842 mdp->post_fw = POST_FW >> (devno << 1);
1843
1844 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001845 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001846
1847 /* First device only init */
1848 if (!devno) {
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001849 if (mdp->cd->tsu) {
1850 struct resource *rtsu;
1851 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1852 if (!rtsu) {
1853 dev_err(&pdev->dev, "Not found TSU resource\n");
1854 goto out_release;
1855 }
1856 mdp->tsu_addr = ioremap(rtsu->start,
1857 resource_size(rtsu));
1858 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001859 if (mdp->cd->chip_reset)
1860 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001861
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001862 if (mdp->cd->tsu) {
1863 /* TSU init (Init only)*/
1864 sh_eth_tsu_init(mdp);
1865 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001866 }
1867
1868 /* network device register */
1869 ret = register_netdev(ndev);
1870 if (ret)
1871 goto out_release;
1872
1873 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001874 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001875 if (ret)
1876 goto out_unregister;
1877
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001878 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08001879 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1880 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001881
1882 platform_set_drvdata(pdev, ndev);
1883
1884 return ret;
1885
1886out_unregister:
1887 unregister_netdev(ndev);
1888
1889out_release:
1890 /* net_dev free */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001891 if (mdp->tsu_addr)
1892 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001893 if (ndev)
1894 free_netdev(ndev);
1895
1896out:
1897 return ret;
1898}
1899
1900static int sh_eth_drv_remove(struct platform_device *pdev)
1901{
1902 struct net_device *ndev = platform_get_drvdata(pdev);
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001903 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001904
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001905 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001906 sh_mdio_release(ndev);
1907 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001908 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001909 free_netdev(ndev);
1910 platform_set_drvdata(pdev, NULL);
1911
1912 return 0;
1913}
1914
Magnus Dammbcd51492009-10-09 00:20:04 +00001915static int sh_eth_runtime_nop(struct device *dev)
1916{
1917 /*
1918 * Runtime PM callback shared between ->runtime_suspend()
1919 * and ->runtime_resume(). Simply returns success.
1920 *
1921 * This driver re-initializes all registers after
1922 * pm_runtime_get_sync() anyway so there is no need
1923 * to save and restore registers here.
1924 */
1925 return 0;
1926}
1927
1928static struct dev_pm_ops sh_eth_dev_pm_ops = {
1929 .runtime_suspend = sh_eth_runtime_nop,
1930 .runtime_resume = sh_eth_runtime_nop,
1931};
1932
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001933static struct platform_driver sh_eth_driver = {
1934 .probe = sh_eth_drv_probe,
1935 .remove = sh_eth_drv_remove,
1936 .driver = {
1937 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001938 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001939 },
1940};
1941
1942static int __init sh_eth_init(void)
1943{
1944 return platform_driver_register(&sh_eth_driver);
1945}
1946
1947static void __exit sh_eth_cleanup(void)
1948{
1949 platform_driver_unregister(&sh_eth_driver);
1950}
1951
1952module_init(sh_eth_init);
1953module_exit(sh_eth_cleanup);
1954
1955MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1956MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1957MODULE_LICENSE("GPL v2");