blob: ef3a3521b83537a78e86970d17b1dd6ee477b2b6 [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 Iwamatsu86a74ff2008-06-09 16:33:56 -070036
37#include "sh_eth.h"
38
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000039#define SH_ETH_DEF_MSG_ENABLE \
40 (NETIF_MSG_LINK | \
41 NETIF_MSG_TIMER | \
42 NETIF_MSG_RX_ERR| \
43 NETIF_MSG_TX_ERR)
44
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000045/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000046#if defined(CONFIG_CPU_SUBTYPE_SH7724)
47#define SH_ETH_RESET_DEFAULT 1
48static void sh_eth_set_duplex(struct net_device *ndev)
49{
50 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000051
52 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000053 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000054 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000055 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000056}
57
58static void sh_eth_set_rate(struct net_device *ndev)
59{
60 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000061
62 switch (mdp->speed) {
63 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000064 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000065 break;
66 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000067 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000068 break;
69 default:
70 break;
71 }
72}
73
74/* SH7724 */
75static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
76 .set_duplex = sh_eth_set_duplex,
77 .set_rate = sh_eth_set_rate,
78
79 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
80 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
81 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
82
83 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
84 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
85 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
86 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
87
88 .apr = 1,
89 .mpr = 1,
90 .tpauser = 1,
91 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -080092 .rpadir = 1,
93 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000094};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000095#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +000096#define SH_ETH_HAS_BOTH_MODULES 1
97#define SH_ETH_HAS_TSU 1
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000098static void sh_eth_set_duplex(struct net_device *ndev)
99{
100 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000101
102 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000103 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000104 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000105 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000106}
107
108static void sh_eth_set_rate(struct net_device *ndev)
109{
110 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000111
112 switch (mdp->speed) {
113 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000114 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000115 break;
116 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000117 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000118 break;
119 default:
120 break;
121 }
122}
123
124/* SH7757 */
125static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
126 .set_duplex = sh_eth_set_duplex,
127 .set_rate = sh_eth_set_rate,
128
129 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
130 .rmcr_value = 0x00000001,
131
132 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
133 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
134 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
135 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
136
137 .apr = 1,
138 .mpr = 1,
139 .tpauser = 1,
140 .hw_swap = 1,
141 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000142 .rpadir = 1,
143 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000144};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000145
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000146#define SH_GIGA_ETH_BASE 0xfee00000
147#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
148#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
149static void sh_eth_chip_reset_giga(struct net_device *ndev)
150{
151 int i;
152 unsigned long mahr[2], malr[2];
153
154 /* save MAHR and MALR */
155 for (i = 0; i < 2; i++) {
156 malr[i] = readl(GIGA_MALR(i));
157 mahr[i] = readl(GIGA_MAHR(i));
158 }
159
160 /* reset device */
161 writel(ARSTR_ARSTR, SH_GIGA_ETH_BASE + 0x1800);
162 mdelay(1);
163
164 /* restore MAHR and MALR */
165 for (i = 0; i < 2; i++) {
166 writel(malr[i], GIGA_MALR(i));
167 writel(mahr[i], GIGA_MAHR(i));
168 }
169}
170
171static int sh_eth_is_gether(struct sh_eth_private *mdp);
172static void sh_eth_reset(struct net_device *ndev)
173{
174 struct sh_eth_private *mdp = netdev_priv(ndev);
175 int cnt = 100;
176
177 if (sh_eth_is_gether(mdp)) {
178 sh_eth_write(ndev, 0x03, EDSR);
179 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
180 EDMR);
181 while (cnt > 0) {
182 if (!(sh_eth_read(ndev, EDMR) & 0x3))
183 break;
184 mdelay(1);
185 cnt--;
186 }
187 if (cnt < 0)
188 printk(KERN_ERR "Device reset fail\n");
189
190 /* Table Init */
191 sh_eth_write(ndev, 0x0, TDLAR);
192 sh_eth_write(ndev, 0x0, TDFAR);
193 sh_eth_write(ndev, 0x0, TDFXR);
194 sh_eth_write(ndev, 0x0, TDFFR);
195 sh_eth_write(ndev, 0x0, RDLAR);
196 sh_eth_write(ndev, 0x0, RDFAR);
197 sh_eth_write(ndev, 0x0, RDFXR);
198 sh_eth_write(ndev, 0x0, RDFFR);
199 } else {
200 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
201 EDMR);
202 mdelay(3);
203 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
204 EDMR);
205 }
206}
207
208static void sh_eth_set_duplex_giga(struct net_device *ndev)
209{
210 struct sh_eth_private *mdp = netdev_priv(ndev);
211
212 if (mdp->duplex) /* Full */
213 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
214 else /* Half */
215 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
216}
217
218static void sh_eth_set_rate_giga(struct net_device *ndev)
219{
220 struct sh_eth_private *mdp = netdev_priv(ndev);
221
222 switch (mdp->speed) {
223 case 10: /* 10BASE */
224 sh_eth_write(ndev, 0x00000000, GECMR);
225 break;
226 case 100:/* 100BASE */
227 sh_eth_write(ndev, 0x00000010, GECMR);
228 break;
229 case 1000: /* 1000BASE */
230 sh_eth_write(ndev, 0x00000020, GECMR);
231 break;
232 default:
233 break;
234 }
235}
236
237/* SH7757(GETHERC) */
238static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
239 .chip_reset = sh_eth_chip_reset_giga,
240 .set_duplex = sh_eth_set_duplex_giga,
241 .set_rate = sh_eth_set_rate_giga,
242
243 .ecsr_value = ECSR_ICD | ECSR_MPD,
244 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
245 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
246
247 .tx_check = EESR_TC1 | EESR_FTC,
248 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
249 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
250 EESR_ECI,
251 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
252 EESR_TFE,
253 .fdr_value = 0x0000072f,
254 .rmcr_value = 0x00000001,
255
256 .apr = 1,
257 .mpr = 1,
258 .tpauser = 1,
259 .bculr = 1,
260 .hw_swap = 1,
261 .rpadir = 1,
262 .rpadir_value = 2 << 16,
263 .no_trimd = 1,
264 .no_ade = 1,
265};
266
267static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
268{
269 if (sh_eth_is_gether(mdp))
270 return &sh_eth_my_cpu_data_giga;
271 else
272 return &sh_eth_my_cpu_data;
273}
274
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000275#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000276#define SH_ETH_HAS_TSU 1
277static void sh_eth_chip_reset(struct net_device *ndev)
278{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000279 struct sh_eth_private *mdp = netdev_priv(ndev);
280
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000281 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000282 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000283 mdelay(1);
284}
285
286static void sh_eth_reset(struct net_device *ndev)
287{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000288 int cnt = 100;
289
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000290 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000291 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000292 while (cnt > 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000293 if (!(sh_eth_read(ndev, EDMR) & 0x3))
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000294 break;
295 mdelay(1);
296 cnt--;
297 }
roel kluin890c8c12009-12-30 01:43:45 +0000298 if (cnt == 0)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000299 printk(KERN_ERR "Device reset fail\n");
300
301 /* Table Init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000302 sh_eth_write(ndev, 0x0, TDLAR);
303 sh_eth_write(ndev, 0x0, TDFAR);
304 sh_eth_write(ndev, 0x0, TDFXR);
305 sh_eth_write(ndev, 0x0, TDFFR);
306 sh_eth_write(ndev, 0x0, RDLAR);
307 sh_eth_write(ndev, 0x0, RDFAR);
308 sh_eth_write(ndev, 0x0, RDFXR);
309 sh_eth_write(ndev, 0x0, RDFFR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000310}
311
312static void sh_eth_set_duplex(struct net_device *ndev)
313{
314 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000315
316 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000317 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000318 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000319 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000320}
321
322static void sh_eth_set_rate(struct net_device *ndev)
323{
324 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000325
326 switch (mdp->speed) {
327 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000328 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000329 break;
330 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000331 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000332 break;
333 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000334 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000335 break;
336 default:
337 break;
338 }
339}
340
341/* sh7763 */
342static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
343 .chip_reset = sh_eth_chip_reset,
344 .set_duplex = sh_eth_set_duplex,
345 .set_rate = sh_eth_set_rate,
346
347 .ecsr_value = ECSR_ICD | ECSR_MPD,
348 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
349 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
350
351 .tx_check = EESR_TC1 | EESR_FTC,
352 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
353 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
354 EESR_ECI,
355 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
356 EESR_TFE,
357
358 .apr = 1,
359 .mpr = 1,
360 .tpauser = 1,
361 .bculr = 1,
362 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000363 .no_trimd = 1,
364 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000365 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000366};
367
368#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
369#define SH_ETH_RESET_DEFAULT 1
370static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
371 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
372
373 .apr = 1,
374 .mpr = 1,
375 .tpauser = 1,
376 .hw_swap = 1,
377};
378#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
379#define SH_ETH_RESET_DEFAULT 1
380#define SH_ETH_HAS_TSU 1
381static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
382 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000383 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000384};
385#endif
386
387static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
388{
389 if (!cd->ecsr_value)
390 cd->ecsr_value = DEFAULT_ECSR_INIT;
391
392 if (!cd->ecsipr_value)
393 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
394
395 if (!cd->fcftr_value)
396 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
397 DEFAULT_FIFO_F_D_RFD;
398
399 if (!cd->fdr_value)
400 cd->fdr_value = DEFAULT_FDR_INIT;
401
402 if (!cd->rmcr_value)
403 cd->rmcr_value = DEFAULT_RMCR_VALUE;
404
405 if (!cd->tx_check)
406 cd->tx_check = DEFAULT_TX_CHECK;
407
408 if (!cd->eesr_err_check)
409 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
410
411 if (!cd->tx_error_check)
412 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
413}
414
415#if defined(SH_ETH_RESET_DEFAULT)
416/* Chip Reset */
417static void sh_eth_reset(struct net_device *ndev)
418{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000419 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000420 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000421 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000422}
423#endif
424
425#if defined(CONFIG_CPU_SH4)
426static void sh_eth_set_receive_align(struct sk_buff *skb)
427{
428 int reserve;
429
430 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
431 if (reserve)
432 skb_reserve(skb, reserve);
433}
434#else
435static void sh_eth_set_receive_align(struct sk_buff *skb)
436{
437 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
438}
439#endif
440
441
Yoshinori Sato71557a32008-08-06 19:49:00 -0400442/* CPU <-> EDMAC endian convert */
443static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
444{
445 switch (mdp->edmac_endian) {
446 case EDMAC_LITTLE_ENDIAN:
447 return cpu_to_le32(x);
448 case EDMAC_BIG_ENDIAN:
449 return cpu_to_be32(x);
450 }
451 return x;
452}
453
454static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
455{
456 switch (mdp->edmac_endian) {
457 case EDMAC_LITTLE_ENDIAN:
458 return le32_to_cpu(x);
459 case EDMAC_BIG_ENDIAN:
460 return be32_to_cpu(x);
461 }
462 return x;
463}
464
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700465/*
466 * Program the hardware MAC address from dev->dev_addr.
467 */
468static void update_mac_address(struct net_device *ndev)
469{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000470 sh_eth_write(ndev,
471 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
472 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
473 sh_eth_write(ndev,
474 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700475}
476
477/*
478 * Get MAC address from SuperH MAC address register
479 *
480 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
481 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
482 * When you want use this device, you must set MAC address in bootloader.
483 *
484 */
Magnus Damm748031f2009-10-09 00:17:14 +0000485static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700486{
Magnus Damm748031f2009-10-09 00:17:14 +0000487 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
488 memcpy(ndev->dev_addr, mac, 6);
489 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000490 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
491 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
492 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
493 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
494 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
495 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000496 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700497}
498
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000499static int sh_eth_is_gether(struct sh_eth_private *mdp)
500{
501 if (mdp->reg_offset == sh_eth_offset_gigabit)
502 return 1;
503 else
504 return 0;
505}
506
507static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
508{
509 if (sh_eth_is_gether(mdp))
510 return EDTRR_TRNS_GETHER;
511 else
512 return EDTRR_TRNS_ETHER;
513}
514
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700515struct bb_info {
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000516 void (*set_gate)(unsigned long addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700517 struct mdiobb_ctrl ctrl;
518 u32 addr;
519 u32 mmd_msk;/* MMD */
520 u32 mdo_msk;
521 u32 mdi_msk;
522 u32 mdc_msk;
523};
524
525/* PHY bit set */
526static void bb_set(u32 addr, u32 msk)
527{
Paul Mundt900fcf02010-11-01 09:29:24 +0000528 writel(readl(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700529}
530
531/* PHY bit clear */
532static void bb_clr(u32 addr, u32 msk)
533{
Paul Mundt900fcf02010-11-01 09:29:24 +0000534 writel((readl(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700535}
536
537/* PHY bit read */
538static int bb_read(u32 addr, u32 msk)
539{
Paul Mundt900fcf02010-11-01 09:29:24 +0000540 return (readl(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700541}
542
543/* Data I/O pin control */
544static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
545{
546 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000547
548 if (bitbang->set_gate)
549 bitbang->set_gate(bitbang->addr);
550
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700551 if (bit)
552 bb_set(bitbang->addr, bitbang->mmd_msk);
553 else
554 bb_clr(bitbang->addr, bitbang->mmd_msk);
555}
556
557/* Set bit data*/
558static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
559{
560 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
561
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000562 if (bitbang->set_gate)
563 bitbang->set_gate(bitbang->addr);
564
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700565 if (bit)
566 bb_set(bitbang->addr, bitbang->mdo_msk);
567 else
568 bb_clr(bitbang->addr, bitbang->mdo_msk);
569}
570
571/* Get bit data*/
572static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
573{
574 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000575
576 if (bitbang->set_gate)
577 bitbang->set_gate(bitbang->addr);
578
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700579 return bb_read(bitbang->addr, bitbang->mdi_msk);
580}
581
582/* MDC pin control */
583static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
584{
585 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
586
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000587 if (bitbang->set_gate)
588 bitbang->set_gate(bitbang->addr);
589
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700590 if (bit)
591 bb_set(bitbang->addr, bitbang->mdc_msk);
592 else
593 bb_clr(bitbang->addr, bitbang->mdc_msk);
594}
595
596/* mdio bus control struct */
597static struct mdiobb_ops bb_ops = {
598 .owner = THIS_MODULE,
599 .set_mdc = sh_mdc_ctrl,
600 .set_mdio_dir = sh_mmd_ctrl,
601 .set_mdio_data = sh_set_mdio,
602 .get_mdio_data = sh_get_mdio,
603};
604
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700605/* free skb and descriptor buffer */
606static void sh_eth_ring_free(struct net_device *ndev)
607{
608 struct sh_eth_private *mdp = netdev_priv(ndev);
609 int i;
610
611 /* Free Rx skb ringbuffer */
612 if (mdp->rx_skbuff) {
613 for (i = 0; i < RX_RING_SIZE; i++) {
614 if (mdp->rx_skbuff[i])
615 dev_kfree_skb(mdp->rx_skbuff[i]);
616 }
617 }
618 kfree(mdp->rx_skbuff);
619
620 /* Free Tx skb ringbuffer */
621 if (mdp->tx_skbuff) {
622 for (i = 0; i < TX_RING_SIZE; i++) {
623 if (mdp->tx_skbuff[i])
624 dev_kfree_skb(mdp->tx_skbuff[i]);
625 }
626 }
627 kfree(mdp->tx_skbuff);
628}
629
630/* format skb and descriptor buffer */
631static void sh_eth_ring_format(struct net_device *ndev)
632{
633 struct sh_eth_private *mdp = netdev_priv(ndev);
634 int i;
635 struct sk_buff *skb;
636 struct sh_eth_rxdesc *rxdesc = NULL;
637 struct sh_eth_txdesc *txdesc = NULL;
638 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
639 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
640
641 mdp->cur_rx = mdp->cur_tx = 0;
642 mdp->dirty_rx = mdp->dirty_tx = 0;
643
644 memset(mdp->rx_ring, 0, rx_ringsize);
645
646 /* build Rx ring buffer */
647 for (i = 0; i < RX_RING_SIZE; i++) {
648 /* skb */
649 mdp->rx_skbuff[i] = NULL;
650 skb = dev_alloc_skb(mdp->rx_buf_sz);
651 mdp->rx_skbuff[i] = skb;
652 if (skb == NULL)
653 break;
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000654 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
655 DMA_FROM_DEVICE);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900656 skb->dev = ndev; /* Mark as being used by this device. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000657 sh_eth_set_receive_align(skb);
658
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700659 /* RX descriptor */
660 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000661 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400662 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700663
664 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000665 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900666 /* Rx descriptor address set */
667 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000668 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000669 if (sh_eth_is_gether(mdp))
670 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900671 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700672 }
673
674 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
675
676 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400677 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700678
679 memset(mdp->tx_ring, 0, tx_ringsize);
680
681 /* build Tx ring buffer */
682 for (i = 0; i < TX_RING_SIZE; i++) {
683 mdp->tx_skbuff[i] = NULL;
684 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400685 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700686 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900687 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400688 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000689 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000690 if (sh_eth_is_gether(mdp))
691 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900692 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700693 }
694
Yoshinori Sato71557a32008-08-06 19:49:00 -0400695 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700696}
697
698/* Get skb and descriptor buffer */
699static int sh_eth_ring_init(struct net_device *ndev)
700{
701 struct sh_eth_private *mdp = netdev_priv(ndev);
702 int rx_ringsize, tx_ringsize, ret = 0;
703
704 /*
705 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
706 * card needs room to do 8 byte alignment, +2 so we can reserve
707 * the first 2 bytes, and +16 gets room for the status word from the
708 * card.
709 */
710 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
711 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800712 if (mdp->cd->rpadir)
713 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700714
715 /* Allocate RX and TX skb rings */
716 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
717 GFP_KERNEL);
718 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000719 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700720 ret = -ENOMEM;
721 return ret;
722 }
723
724 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
725 GFP_KERNEL);
726 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000727 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700728 ret = -ENOMEM;
729 goto skb_ring_free;
730 }
731
732 /* Allocate all Rx descriptors. */
733 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
734 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
735 GFP_KERNEL);
736
737 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000738 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
739 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700740 ret = -ENOMEM;
741 goto desc_ring_free;
742 }
743
744 mdp->dirty_rx = 0;
745
746 /* Allocate all Tx descriptors. */
747 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
748 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
749 GFP_KERNEL);
750 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000751 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
752 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700753 ret = -ENOMEM;
754 goto desc_ring_free;
755 }
756 return ret;
757
758desc_ring_free:
759 /* free DMA buffer */
760 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
761
762skb_ring_free:
763 /* Free Rx and Tx skb ring buffer */
764 sh_eth_ring_free(ndev);
765
766 return ret;
767}
768
769static int sh_eth_dev_init(struct net_device *ndev)
770{
771 int ret = 0;
772 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700773 u_int32_t rx_int_var, tx_int_var;
774 u32 val;
775
776 /* Soft Reset */
777 sh_eth_reset(ndev);
778
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900779 /* Descriptor format */
780 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000781 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000782 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700783
784 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000785 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700786
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000787#if defined(__LITTLE_ENDIAN__)
788 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000789 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000790 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900791#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000792 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700793
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900794 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000795 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
796 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700797
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900798 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000799 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700800
801 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
802 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000803 sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700804
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000805 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000806 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900807
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000808 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900809
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000810 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000811 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700812
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900813 /* Recv frame limit set register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000814 sh_eth_write(ndev, RFLR_VALUE, RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700815
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000816 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
817 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700818
819 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000820 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700821 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
822
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000823 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900824
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000825 if (mdp->cd->set_rate)
826 mdp->cd->set_rate(ndev);
827
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900828 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000829 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900830
831 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000832 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700833
834 /* Set MAC address */
835 update_mac_address(ndev);
836
837 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000838 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000839 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000840 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000841 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000842 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000843 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900844
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700845 /* Setting the Rx mode will start the Rx process. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000846 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700847
848 netif_start_queue(ndev);
849
850 return ret;
851}
852
853/* free Tx skb function */
854static int sh_eth_txfree(struct net_device *ndev)
855{
856 struct sh_eth_private *mdp = netdev_priv(ndev);
857 struct sh_eth_txdesc *txdesc;
858 int freeNum = 0;
859 int entry = 0;
860
861 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
862 entry = mdp->dirty_tx % TX_RING_SIZE;
863 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400864 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700865 break;
866 /* Free the original skb. */
867 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +0000868 dma_unmap_single(&ndev->dev, txdesc->addr,
869 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700870 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
871 mdp->tx_skbuff[entry] = NULL;
872 freeNum++;
873 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400874 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700875 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400876 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700877
878 mdp->stats.tx_packets++;
879 mdp->stats.tx_bytes += txdesc->buffer_length;
880 }
881 return freeNum;
882}
883
884/* Packet receive function */
885static int sh_eth_rx(struct net_device *ndev)
886{
887 struct sh_eth_private *mdp = netdev_priv(ndev);
888 struct sh_eth_rxdesc *rxdesc;
889
890 int entry = mdp->cur_rx % RX_RING_SIZE;
891 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
892 struct sk_buff *skb;
893 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000894 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700895
896 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400897 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
898 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700899 pkt_len = rxdesc->frame_length;
900
901 if (--boguscnt < 0)
902 break;
903
904 if (!(desc_status & RDFEND))
905 mdp->stats.rx_length_errors++;
906
907 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
908 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
909 mdp->stats.rx_errors++;
910 if (desc_status & RD_RFS1)
911 mdp->stats.rx_crc_errors++;
912 if (desc_status & RD_RFS2)
913 mdp->stats.rx_frame_errors++;
914 if (desc_status & RD_RFS3)
915 mdp->stats.rx_length_errors++;
916 if (desc_status & RD_RFS4)
917 mdp->stats.rx_length_errors++;
918 if (desc_status & RD_RFS6)
919 mdp->stats.rx_missed_errors++;
920 if (desc_status & RD_RFS10)
921 mdp->stats.rx_over_errors++;
922 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000923 if (!mdp->cd->hw_swap)
924 sh_eth_soft_swap(
925 phys_to_virt(ALIGN(rxdesc->addr, 4)),
926 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700927 skb = mdp->rx_skbuff[entry];
928 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -0800929 if (mdp->cd->rpadir)
930 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700931 skb_put(skb, pkt_len);
932 skb->protocol = eth_type_trans(skb, ndev);
933 netif_rx(skb);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700934 mdp->stats.rx_packets++;
935 mdp->stats.rx_bytes += pkt_len;
936 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400937 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700938 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000939 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700940 }
941
942 /* Refill the Rx ring buffers. */
943 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
944 entry = mdp->dirty_rx % RX_RING_SIZE;
945 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900946 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000947 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900948
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700949 if (mdp->rx_skbuff[entry] == NULL) {
950 skb = dev_alloc_skb(mdp->rx_buf_sz);
951 mdp->rx_skbuff[entry] = skb;
952 if (skb == NULL)
953 break; /* Better luck next round. */
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000954 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
955 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 skb->dev = ndev;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000957 sh_eth_set_receive_align(skb);
958
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700959 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000960 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700961 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700962 if (entry >= RX_RING_SIZE - 1)
963 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400964 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700965 else
966 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400967 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700968 }
969
970 /* Restart Rx engine if stopped. */
971 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000972 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R))
973 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700974
975 return 0;
976}
977
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000978static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000979{
980 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000981 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
982 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000983}
984
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000985static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000986{
987 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000988 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
989 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000990}
991
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700992/* error control function */
993static void sh_eth_error(struct net_device *ndev, int intr_status)
994{
995 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700996 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000997 u32 link_stat;
998 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700999
1000 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001001 felic_stat = sh_eth_read(ndev, ECSR);
1002 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001003 if (felic_stat & ECSR_ICD)
1004 mdp->stats.tx_carrier_errors++;
1005 if (felic_stat & ECSR_LCHNG) {
1006 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001007 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001008 if (mdp->link == PHY_DOWN)
1009 link_stat = 0;
1010 else
1011 link_stat = PHY_ST_LINK;
1012 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001013 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001014 if (mdp->ether_link_active_low)
1015 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001016 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001017 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001018 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001019 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001021 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1022 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001023 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001024 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1025 ECSR);
1026 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1027 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001028 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001029 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001030 }
1031 }
1032 }
1033
1034 if (intr_status & EESR_TWB) {
1035 /* Write buck end. unused write back interrupt */
1036 if (intr_status & EESR_TABT) /* Transmit Abort int */
1037 mdp->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001038 if (netif_msg_tx_err(mdp))
1039 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001040 }
1041
1042 if (intr_status & EESR_RABT) {
1043 /* Receive Abort int */
1044 if (intr_status & EESR_RFRMER) {
1045 /* Receive Frame Overflow int */
1046 mdp->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001047 if (netif_msg_rx_err(mdp))
1048 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001049 }
1050 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001051
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001052 if (intr_status & EESR_TDE) {
1053 /* Transmit Descriptor Empty int */
1054 mdp->stats.tx_fifo_errors++;
1055 if (netif_msg_tx_err(mdp))
1056 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1057 }
1058
1059 if (intr_status & EESR_TFE) {
1060 /* FIFO under flow */
1061 mdp->stats.tx_fifo_errors++;
1062 if (netif_msg_tx_err(mdp))
1063 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001064 }
1065
1066 if (intr_status & EESR_RDE) {
1067 /* Receive Descriptor Empty int */
1068 mdp->stats.rx_over_errors++;
1069
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001070 if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R)
1071 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001072 if (netif_msg_rx_err(mdp))
1073 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001074 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001075
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001076 if (intr_status & EESR_RFE) {
1077 /* Receive FIFO Overflow int */
1078 mdp->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001079 if (netif_msg_rx_err(mdp))
1080 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1081 }
1082
1083 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1084 /* Address Error */
1085 mdp->stats.tx_fifo_errors++;
1086 if (netif_msg_tx_err(mdp))
1087 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001088 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001089
1090 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1091 if (mdp->cd->no_ade)
1092 mask &= ~EESR_ADE;
1093 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001094 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001095 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001096 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001097 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1098 intr_status, mdp->cur_tx);
1099 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001100 mdp->dirty_tx, (u32) ndev->state, edtrr);
1101 /* dirty buffer free */
1102 sh_eth_txfree(ndev);
1103
1104 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001105 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001106 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001107 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001108 }
1109 /* wakeup */
1110 netif_wake_queue(ndev);
1111 }
1112}
1113
1114static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1115{
1116 struct net_device *ndev = netdev;
1117 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001118 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001119 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001120 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001121
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001122 spin_lock(&mdp->lock);
1123
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001124 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001125 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001127 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1128 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001129 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001130 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001131 ret = IRQ_HANDLED;
1132 } else
1133 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001134
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001135 if (intr_status & (EESR_FRC | /* Frame recv*/
1136 EESR_RMAF | /* Multi cast address recv*/
1137 EESR_RRF | /* Bit frame recv */
1138 EESR_RTLF | /* Long frame recv*/
1139 EESR_RTSF | /* short frame recv */
1140 EESR_PRE | /* PHY-LSI recv error */
1141 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001142 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001143 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001145 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001146 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001147 sh_eth_txfree(ndev);
1148 netif_wake_queue(ndev);
1149 }
1150
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001151 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001152 sh_eth_error(ndev, intr_status);
1153
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001154other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155 spin_unlock(&mdp->lock);
1156
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001157 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001158}
1159
1160static void sh_eth_timer(unsigned long data)
1161{
1162 struct net_device *ndev = (struct net_device *)data;
1163 struct sh_eth_private *mdp = netdev_priv(ndev);
1164
1165 mod_timer(&mdp->timer, jiffies + (10 * HZ));
1166}
1167
1168/* PHY state control function */
1169static void sh_eth_adjust_link(struct net_device *ndev)
1170{
1171 struct sh_eth_private *mdp = netdev_priv(ndev);
1172 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001173 int new_state = 0;
1174
1175 if (phydev->link != PHY_DOWN) {
1176 if (phydev->duplex != mdp->duplex) {
1177 new_state = 1;
1178 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001179 if (mdp->cd->set_duplex)
1180 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001181 }
1182
1183 if (phydev->speed != mdp->speed) {
1184 new_state = 1;
1185 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001186 if (mdp->cd->set_rate)
1187 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001188 }
1189 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001190 sh_eth_write(ndev,
1191 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001192 new_state = 1;
1193 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001194 }
1195 } else if (mdp->link) {
1196 new_state = 1;
1197 mdp->link = PHY_DOWN;
1198 mdp->speed = 0;
1199 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001200 }
1201
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001202 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203 phy_print_status(phydev);
1204}
1205
1206/* PHY init function */
1207static int sh_eth_phy_init(struct net_device *ndev)
1208{
1209 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001210 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001211 struct phy_device *phydev = NULL;
1212
Kay Sieversfb28ad32008-11-10 13:55:14 -08001213 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214 mdp->mii_bus->id , mdp->phy_id);
1215
1216 mdp->link = PHY_DOWN;
1217 mdp->speed = 0;
1218 mdp->duplex = -1;
1219
1220 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001221 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001222 0, mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001223 if (IS_ERR(phydev)) {
1224 dev_err(&ndev->dev, "phy_connect failed\n");
1225 return PTR_ERR(phydev);
1226 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001227
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001228 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001229 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001230
1231 mdp->phydev = phydev;
1232
1233 return 0;
1234}
1235
1236/* PHY control start function */
1237static int sh_eth_phy_start(struct net_device *ndev)
1238{
1239 struct sh_eth_private *mdp = netdev_priv(ndev);
1240 int ret;
1241
1242 ret = sh_eth_phy_init(ndev);
1243 if (ret)
1244 return ret;
1245
1246 /* reset phy - this also wakes it from PDOWN */
1247 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1248 phy_start(mdp->phydev);
1249
1250 return 0;
1251}
1252
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001253static int sh_eth_get_settings(struct net_device *ndev,
1254 struct ethtool_cmd *ecmd)
1255{
1256 struct sh_eth_private *mdp = netdev_priv(ndev);
1257 unsigned long flags;
1258 int ret;
1259
1260 spin_lock_irqsave(&mdp->lock, flags);
1261 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1262 spin_unlock_irqrestore(&mdp->lock, flags);
1263
1264 return ret;
1265}
1266
1267static int sh_eth_set_settings(struct net_device *ndev,
1268 struct ethtool_cmd *ecmd)
1269{
1270 struct sh_eth_private *mdp = netdev_priv(ndev);
1271 unsigned long flags;
1272 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001273
1274 spin_lock_irqsave(&mdp->lock, flags);
1275
1276 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001277 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001278
1279 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1280 if (ret)
1281 goto error_exit;
1282
1283 if (ecmd->duplex == DUPLEX_FULL)
1284 mdp->duplex = 1;
1285 else
1286 mdp->duplex = 0;
1287
1288 if (mdp->cd->set_duplex)
1289 mdp->cd->set_duplex(ndev);
1290
1291error_exit:
1292 mdelay(1);
1293
1294 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001295 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001296
1297 spin_unlock_irqrestore(&mdp->lock, flags);
1298
1299 return ret;
1300}
1301
1302static int sh_eth_nway_reset(struct net_device *ndev)
1303{
1304 struct sh_eth_private *mdp = netdev_priv(ndev);
1305 unsigned long flags;
1306 int ret;
1307
1308 spin_lock_irqsave(&mdp->lock, flags);
1309 ret = phy_start_aneg(mdp->phydev);
1310 spin_unlock_irqrestore(&mdp->lock, flags);
1311
1312 return ret;
1313}
1314
1315static u32 sh_eth_get_msglevel(struct net_device *ndev)
1316{
1317 struct sh_eth_private *mdp = netdev_priv(ndev);
1318 return mdp->msg_enable;
1319}
1320
1321static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1322{
1323 struct sh_eth_private *mdp = netdev_priv(ndev);
1324 mdp->msg_enable = value;
1325}
1326
1327static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1328 "rx_current", "tx_current",
1329 "rx_dirty", "tx_dirty",
1330};
1331#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1332
1333static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1334{
1335 switch (sset) {
1336 case ETH_SS_STATS:
1337 return SH_ETH_STATS_LEN;
1338 default:
1339 return -EOPNOTSUPP;
1340 }
1341}
1342
1343static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1344 struct ethtool_stats *stats, u64 *data)
1345{
1346 struct sh_eth_private *mdp = netdev_priv(ndev);
1347 int i = 0;
1348
1349 /* device-specific stats */
1350 data[i++] = mdp->cur_rx;
1351 data[i++] = mdp->cur_tx;
1352 data[i++] = mdp->dirty_rx;
1353 data[i++] = mdp->dirty_tx;
1354}
1355
1356static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1357{
1358 switch (stringset) {
1359 case ETH_SS_STATS:
1360 memcpy(data, *sh_eth_gstrings_stats,
1361 sizeof(sh_eth_gstrings_stats));
1362 break;
1363 }
1364}
1365
1366static struct ethtool_ops sh_eth_ethtool_ops = {
1367 .get_settings = sh_eth_get_settings,
1368 .set_settings = sh_eth_set_settings,
1369 .nway_reset = sh_eth_nway_reset,
1370 .get_msglevel = sh_eth_get_msglevel,
1371 .set_msglevel = sh_eth_set_msglevel,
1372 .get_link = ethtool_op_get_link,
1373 .get_strings = sh_eth_get_strings,
1374 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1375 .get_sset_count = sh_eth_get_sset_count,
1376};
1377
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378/* network device open function */
1379static int sh_eth_open(struct net_device *ndev)
1380{
1381 int ret = 0;
1382 struct sh_eth_private *mdp = netdev_priv(ndev);
1383
Magnus Dammbcd51492009-10-09 00:20:04 +00001384 pm_runtime_get_sync(&mdp->pdev->dev);
1385
Joe Perchesa0607fd2009-11-18 23:29:17 -08001386 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001387#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001388 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1389 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001390 IRQF_SHARED,
1391#else
1392 0,
1393#endif
1394 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001395 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001396 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001397 return ret;
1398 }
1399
1400 /* Descriptor set */
1401 ret = sh_eth_ring_init(ndev);
1402 if (ret)
1403 goto out_free_irq;
1404
1405 /* device init */
1406 ret = sh_eth_dev_init(ndev);
1407 if (ret)
1408 goto out_free_irq;
1409
1410 /* PHY control start*/
1411 ret = sh_eth_phy_start(ndev);
1412 if (ret)
1413 goto out_free_irq;
1414
1415 /* Set the timer to check for link beat. */
1416 init_timer(&mdp->timer);
1417 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001418 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001419
1420 return ret;
1421
1422out_free_irq:
1423 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001424 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001425 return ret;
1426}
1427
1428/* Timeout function */
1429static void sh_eth_tx_timeout(struct net_device *ndev)
1430{
1431 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001432 struct sh_eth_rxdesc *rxdesc;
1433 int i;
1434
1435 netif_stop_queue(ndev);
1436
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001437 if (netif_msg_timer(mdp))
1438 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001439 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001440
1441 /* tx_errors count up */
1442 mdp->stats.tx_errors++;
1443
1444 /* timer off */
1445 del_timer_sync(&mdp->timer);
1446
1447 /* Free all the skbuffs in the Rx queue. */
1448 for (i = 0; i < RX_RING_SIZE; i++) {
1449 rxdesc = &mdp->rx_ring[i];
1450 rxdesc->status = 0;
1451 rxdesc->addr = 0xBADF00D0;
1452 if (mdp->rx_skbuff[i])
1453 dev_kfree_skb(mdp->rx_skbuff[i]);
1454 mdp->rx_skbuff[i] = NULL;
1455 }
1456 for (i = 0; i < TX_RING_SIZE; i++) {
1457 if (mdp->tx_skbuff[i])
1458 dev_kfree_skb(mdp->tx_skbuff[i]);
1459 mdp->tx_skbuff[i] = NULL;
1460 }
1461
1462 /* device init */
1463 sh_eth_dev_init(ndev);
1464
1465 /* timer on */
1466 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1467 add_timer(&mdp->timer);
1468}
1469
1470/* Packet transmit function */
1471static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1472{
1473 struct sh_eth_private *mdp = netdev_priv(ndev);
1474 struct sh_eth_txdesc *txdesc;
1475 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001476 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477
1478 spin_lock_irqsave(&mdp->lock, flags);
1479 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1480 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001481 if (netif_msg_tx_queued(mdp))
1482 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 netif_stop_queue(ndev);
1484 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001485 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001486 }
1487 }
1488 spin_unlock_irqrestore(&mdp->lock, flags);
1489
1490 entry = mdp->cur_tx % TX_RING_SIZE;
1491 mdp->tx_skbuff[entry] = skb;
1492 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001493 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001494 if (!mdp->cd->hw_swap)
1495 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1496 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001497 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1498 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001499 if (skb->len < ETHERSMALL)
1500 txdesc->buffer_length = ETHERSMALL;
1501 else
1502 txdesc->buffer_length = skb->len;
1503
1504 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001505 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001507 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001508
1509 mdp->cur_tx++;
1510
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001511 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1512 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001513
Patrick McHardy6ed10652009-06-23 06:03:08 +00001514 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001515}
1516
1517/* device close function */
1518static int sh_eth_close(struct net_device *ndev)
1519{
1520 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001521 int ringsize;
1522
1523 netif_stop_queue(ndev);
1524
1525 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001526 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001527
1528 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001529 sh_eth_write(ndev, 0, EDTRR);
1530 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531
1532 /* PHY Disconnect */
1533 if (mdp->phydev) {
1534 phy_stop(mdp->phydev);
1535 phy_disconnect(mdp->phydev);
1536 }
1537
1538 free_irq(ndev->irq, ndev);
1539
1540 del_timer_sync(&mdp->timer);
1541
1542 /* Free all the skbuffs in the Rx queue. */
1543 sh_eth_ring_free(ndev);
1544
1545 /* free DMA buffer */
1546 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1547 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1548
1549 /* free DMA buffer */
1550 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1551 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1552
Magnus Dammbcd51492009-10-09 00:20:04 +00001553 pm_runtime_put_sync(&mdp->pdev->dev);
1554
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001555 return 0;
1556}
1557
1558static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1559{
1560 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001561
Magnus Dammbcd51492009-10-09 00:20:04 +00001562 pm_runtime_get_sync(&mdp->pdev->dev);
1563
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001564 mdp->stats.tx_dropped += sh_eth_read(ndev, TROCR);
1565 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
1566 mdp->stats.collisions += sh_eth_read(ndev, CDCR);
1567 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
1568 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
1569 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001570 if (sh_eth_is_gether(mdp)) {
1571 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
1572 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
1573 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
1574 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1575 } else {
1576 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
1577 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1578 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001579 pm_runtime_put_sync(&mdp->pdev->dev);
1580
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001581 return &mdp->stats;
1582}
1583
1584/* ioctl to device funciotn*/
1585static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1586 int cmd)
1587{
1588 struct sh_eth_private *mdp = netdev_priv(ndev);
1589 struct phy_device *phydev = mdp->phydev;
1590
1591 if (!netif_running(ndev))
1592 return -EINVAL;
1593
1594 if (!phydev)
1595 return -ENODEV;
1596
Richard Cochran28b04112010-07-17 08:48:55 +00001597 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001598}
1599
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001600#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001601/* Multicast reception directions set */
1602static void sh_eth_set_multicast_list(struct net_device *ndev)
1603{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001604 if (ndev->flags & IFF_PROMISC) {
1605 /* Set promiscuous. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001606 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) |
1607 ECMR_PRM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001608 } else {
1609 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001610 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) |
1611 ECMR_MCT, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001612 }
1613}
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001614#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001615
1616/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001617static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001618{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001619 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
1620 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
1621 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
1622 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
1623 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
1624 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
1625 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
1626 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
1627 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
1628 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001629 if (sh_eth_is_gether(mdp)) {
1630 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
1631 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
1632 } else {
1633 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
1634 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
1635 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001636 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
1637 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
1638 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
1639 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
1640 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
1641 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
1642 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001643}
1644
1645/* MDIO bus release function */
1646static int sh_mdio_release(struct net_device *ndev)
1647{
1648 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1649
1650 /* unregister mdio bus */
1651 mdiobus_unregister(bus);
1652
1653 /* remove mdio bus info from net_device */
1654 dev_set_drvdata(&ndev->dev, NULL);
1655
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00001656 /* free interrupts memory */
1657 kfree(bus->irq);
1658
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001659 /* free bitbang info */
1660 free_mdio_bitbang(bus);
1661
1662 return 0;
1663}
1664
1665/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001666static int sh_mdio_init(struct net_device *ndev, int id,
1667 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001668{
1669 int ret, i;
1670 struct bb_info *bitbang;
1671 struct sh_eth_private *mdp = netdev_priv(ndev);
1672
1673 /* create bit control struct for PHY */
1674 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1675 if (!bitbang) {
1676 ret = -ENOMEM;
1677 goto out;
1678 }
1679
1680 /* bitbang init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001681 bitbang->addr = ndev->base_addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001682 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 bitbang->mdi_msk = 0x08;
1684 bitbang->mdo_msk = 0x04;
1685 bitbang->mmd_msk = 0x02;/* MMD */
1686 bitbang->mdc_msk = 0x01;
1687 bitbang->ctrl.ops = &bb_ops;
1688
Stefan Weilc2e07b32010-08-03 19:44:52 +02001689 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001690 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1691 if (!mdp->mii_bus) {
1692 ret = -ENOMEM;
1693 goto out_free_bitbang;
1694 }
1695
1696 /* Hook up MII support for ethtool */
1697 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001698 mdp->mii_bus->parent = &ndev->dev;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001699 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001700
1701 /* PHY IRQ */
1702 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1703 if (!mdp->mii_bus->irq) {
1704 ret = -ENOMEM;
1705 goto out_free_bus;
1706 }
1707
1708 for (i = 0; i < PHY_MAX_ADDR; i++)
1709 mdp->mii_bus->irq[i] = PHY_POLL;
1710
1711 /* regist mdio bus */
1712 ret = mdiobus_register(mdp->mii_bus);
1713 if (ret)
1714 goto out_free_irq;
1715
1716 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1717
1718 return 0;
1719
1720out_free_irq:
1721 kfree(mdp->mii_bus->irq);
1722
1723out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001724 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001725
1726out_free_bitbang:
1727 kfree(bitbang);
1728
1729out:
1730 return ret;
1731}
1732
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001733static const u16 *sh_eth_get_register_offset(int register_type)
1734{
1735 const u16 *reg_offset = NULL;
1736
1737 switch (register_type) {
1738 case SH_ETH_REG_GIGABIT:
1739 reg_offset = sh_eth_offset_gigabit;
1740 break;
1741 case SH_ETH_REG_FAST_SH4:
1742 reg_offset = sh_eth_offset_fast_sh4;
1743 break;
1744 case SH_ETH_REG_FAST_SH3_SH2:
1745 reg_offset = sh_eth_offset_fast_sh3_sh2;
1746 break;
1747 default:
1748 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
1749 break;
1750 }
1751
1752 return reg_offset;
1753}
1754
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001755static const struct net_device_ops sh_eth_netdev_ops = {
1756 .ndo_open = sh_eth_open,
1757 .ndo_stop = sh_eth_close,
1758 .ndo_start_xmit = sh_eth_start_xmit,
1759 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001760#if defined(SH_ETH_HAS_TSU)
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001761 .ndo_set_rx_mode = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001762#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001763 .ndo_tx_timeout = sh_eth_tx_timeout,
1764 .ndo_do_ioctl = sh_eth_do_ioctl,
1765 .ndo_validate_addr = eth_validate_addr,
1766 .ndo_set_mac_address = eth_mac_addr,
1767 .ndo_change_mtu = eth_change_mtu,
1768};
1769
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001770static int sh_eth_drv_probe(struct platform_device *pdev)
1771{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07001772 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001773 struct resource *res;
1774 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00001775 struct sh_eth_private *mdp = NULL;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001776 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001777
1778 /* get base addr */
1779 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1780 if (unlikely(res == NULL)) {
1781 dev_err(&pdev->dev, "invalid resource\n");
1782 ret = -EINVAL;
1783 goto out;
1784 }
1785
1786 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1787 if (!ndev) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001788 dev_err(&pdev->dev, "Could not allocate device.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001789 ret = -ENOMEM;
1790 goto out;
1791 }
1792
1793 /* The sh Ether-specific entries in the device structure. */
1794 ndev->base_addr = res->start;
1795 devno = pdev->id;
1796 if (devno < 0)
1797 devno = 0;
1798
1799 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001800 ret = platform_get_irq(pdev, 0);
1801 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001802 ret = -ENODEV;
1803 goto out_release;
1804 }
roel kluincc3c0802008-09-10 19:22:44 +02001805 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001806
1807 SET_NETDEV_DEV(ndev, &pdev->dev);
1808
1809 /* Fill in the fields of the device structure with ethernet values. */
1810 ether_setup(ndev);
1811
1812 mdp = netdev_priv(ndev);
1813 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001814 mdp->pdev = pdev;
1815 pm_runtime_enable(&pdev->dev);
1816 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001817
Yoshinori Sato71557a32008-08-06 19:49:00 -04001818 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001819 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001820 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00001821 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001822 /* EDMAC endian */
1823 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001824 mdp->no_ether_link = pd->no_ether_link;
1825 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001826 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001827
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001828 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001829#if defined(SH_ETH_HAS_BOTH_MODULES)
1830 mdp->cd = sh_eth_get_cpu_data(mdp);
1831#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001832 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001833#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001834 sh_eth_set_default_cpu_data(mdp->cd);
1835
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001836 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001837 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001838 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001839 ndev->watchdog_timeo = TX_TIMEOUT;
1840
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001841 /* debug message level */
1842 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001843 mdp->post_rx = POST_RX >> (devno << 1);
1844 mdp->post_fw = POST_FW >> (devno << 1);
1845
1846 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001847 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001848
1849 /* First device only init */
1850 if (!devno) {
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001851 if (mdp->cd->tsu) {
1852 struct resource *rtsu;
1853 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1854 if (!rtsu) {
1855 dev_err(&pdev->dev, "Not found TSU resource\n");
1856 goto out_release;
1857 }
1858 mdp->tsu_addr = ioremap(rtsu->start,
1859 resource_size(rtsu));
1860 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001861 if (mdp->cd->chip_reset)
1862 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001863
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001864 if (mdp->cd->tsu) {
1865 /* TSU init (Init only)*/
1866 sh_eth_tsu_init(mdp);
1867 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001868 }
1869
1870 /* network device register */
1871 ret = register_netdev(ndev);
1872 if (ret)
1873 goto out_release;
1874
1875 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001876 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001877 if (ret)
1878 goto out_unregister;
1879
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001880 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08001881 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1882 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001883
1884 platform_set_drvdata(pdev, ndev);
1885
1886 return ret;
1887
1888out_unregister:
1889 unregister_netdev(ndev);
1890
1891out_release:
1892 /* net_dev free */
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00001893 if (mdp && mdp->tsu_addr)
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001894 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001895 if (ndev)
1896 free_netdev(ndev);
1897
1898out:
1899 return ret;
1900}
1901
1902static int sh_eth_drv_remove(struct platform_device *pdev)
1903{
1904 struct net_device *ndev = platform_get_drvdata(pdev);
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001905 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001906
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001907 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001908 sh_mdio_release(ndev);
1909 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001910 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001911 free_netdev(ndev);
1912 platform_set_drvdata(pdev, NULL);
1913
1914 return 0;
1915}
1916
Magnus Dammbcd51492009-10-09 00:20:04 +00001917static int sh_eth_runtime_nop(struct device *dev)
1918{
1919 /*
1920 * Runtime PM callback shared between ->runtime_suspend()
1921 * and ->runtime_resume(). Simply returns success.
1922 *
1923 * This driver re-initializes all registers after
1924 * pm_runtime_get_sync() anyway so there is no need
1925 * to save and restore registers here.
1926 */
1927 return 0;
1928}
1929
1930static struct dev_pm_ops sh_eth_dev_pm_ops = {
1931 .runtime_suspend = sh_eth_runtime_nop,
1932 .runtime_resume = sh_eth_runtime_nop,
1933};
1934
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001935static struct platform_driver sh_eth_driver = {
1936 .probe = sh_eth_drv_probe,
1937 .remove = sh_eth_drv_remove,
1938 .driver = {
1939 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001940 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001941 },
1942};
1943
1944static int __init sh_eth_init(void)
1945{
1946 return platform_driver_register(&sh_eth_driver);
1947}
1948
1949static void __exit sh_eth_cleanup(void)
1950{
1951 platform_driver_unregister(&sh_eth_driver);
1952}
1953
1954module_init(sh_eth_init);
1955module_exit(sh_eth_cleanup);
1956
1957MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1958MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1959MODULE_LICENSE("GPL v2");