blob: bf5e3cf97c4d89d55b92b66a46ee37787acb934c [file] [log] [blame]
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001/*
2 * SuperH Ethernet device driver
3 *
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2012 Renesas Solutions Corp.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 */
22
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070023#include <linux/init.h>
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070027#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070028#include <linux/dma-mapping.h>
29#include <linux/etherdevice.h>
30#include <linux/delay.h>
31#include <linux/platform_device.h>
32#include <linux/mdio-bitbang.h>
33#include <linux/netdevice.h>
34#include <linux/phy.h>
35#include <linux/cache.h>
36#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000037#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000039#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000040#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000041#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000042#include <linux/sh_eth.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070043
44#include "sh_eth.h"
45
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000046#define SH_ETH_DEF_MSG_ENABLE \
47 (NETIF_MSG_LINK | \
48 NETIF_MSG_TIMER | \
49 NETIF_MSG_RX_ERR| \
50 NETIF_MSG_TX_ERR)
51
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +000052#if defined(CONFIG_CPU_SUBTYPE_SH7734) || \
53 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
54 defined(CONFIG_ARCH_R8A7740)
55static void sh_eth_select_mii(struct net_device *ndev)
56{
57 u32 value = 0x0;
58 struct sh_eth_private *mdp = netdev_priv(ndev);
59
60 switch (mdp->phy_interface) {
61 case PHY_INTERFACE_MODE_GMII:
62 value = 0x2;
63 break;
64 case PHY_INTERFACE_MODE_MII:
65 value = 0x1;
66 break;
67 case PHY_INTERFACE_MODE_RMII:
68 value = 0x0;
69 break;
70 default:
71 pr_warn("PHY interface mode was not setup. Set to MII.\n");
72 value = 0x1;
73 break;
74 }
75
76 sh_eth_write(ndev, value, RMII_MII);
77}
78#endif
79
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000080/* There is CPU dependent code */
Phil Edworthyd0418bb2012-08-14 20:33:29 +000081#if defined(CONFIG_CPU_SUBTYPE_SH7724) || defined(CONFIG_ARCH_R8A7779)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000082#define SH_ETH_RESET_DEFAULT 1
83static void sh_eth_set_duplex(struct net_device *ndev)
84{
85 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000086
87 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000088 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000089 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000090 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000091}
92
93static void sh_eth_set_rate(struct net_device *ndev)
94{
95 struct sh_eth_private *mdp = netdev_priv(ndev);
Phil Edworthyd0418bb2012-08-14 20:33:29 +000096 unsigned int bits = ECMR_RTM;
97
98#if defined(CONFIG_ARCH_R8A7779)
99 bits |= ECMR_ELB;
100#endif
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000101
102 switch (mdp->speed) {
103 case 10: /* 10BASE */
Phil Edworthyd0418bb2012-08-14 20:33:29 +0000104 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~bits, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000105 break;
106 case 100:/* 100BASE */
Phil Edworthyd0418bb2012-08-14 20:33:29 +0000107 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | bits, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000108 break;
109 default:
110 break;
111 }
112}
113
114/* SH7724 */
115static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
116 .set_duplex = sh_eth_set_duplex,
117 .set_rate = sh_eth_set_rate,
118
119 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
120 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
121 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
122
123 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
124 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
125 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
126 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
127
128 .apr = 1,
129 .mpr = 1,
130 .tpauser = 1,
131 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800132 .rpadir = 1,
133 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000134};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000135#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000136#define SH_ETH_HAS_BOTH_MODULES 1
137#define SH_ETH_HAS_TSU 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000138static int sh_eth_check_reset(struct net_device *ndev);
139
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000140static void sh_eth_set_duplex(struct net_device *ndev)
141{
142 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000143
144 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000145 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000146 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000147 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000148}
149
150static void sh_eth_set_rate(struct net_device *ndev)
151{
152 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000153
154 switch (mdp->speed) {
155 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000156 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000157 break;
158 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000159 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000160 break;
161 default:
162 break;
163 }
164}
165
166/* SH7757 */
167static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
168 .set_duplex = sh_eth_set_duplex,
169 .set_rate = sh_eth_set_rate,
170
171 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
172 .rmcr_value = 0x00000001,
173
174 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
175 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
176 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
177 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
178
179 .apr = 1,
180 .mpr = 1,
181 .tpauser = 1,
182 .hw_swap = 1,
183 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000184 .rpadir = 1,
185 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000186};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000187
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000188#define SH_GIGA_ETH_BASE 0xfee00000
189#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
190#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
191static void sh_eth_chip_reset_giga(struct net_device *ndev)
192{
193 int i;
194 unsigned long mahr[2], malr[2];
195
196 /* save MAHR and MALR */
197 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000198 malr[i] = ioread32((void *)GIGA_MALR(i));
199 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000200 }
201
202 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000203 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000204 mdelay(1);
205
206 /* restore MAHR and MALR */
207 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000208 iowrite32(malr[i], (void *)GIGA_MALR(i));
209 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000210 }
211}
212
213static int sh_eth_is_gether(struct sh_eth_private *mdp);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000214static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000215{
216 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000217 int ret = 0;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000218
219 if (sh_eth_is_gether(mdp)) {
220 sh_eth_write(ndev, 0x03, EDSR);
221 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
222 EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000223
224 ret = sh_eth_check_reset(ndev);
225 if (ret)
226 goto out;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000227
228 /* Table Init */
229 sh_eth_write(ndev, 0x0, TDLAR);
230 sh_eth_write(ndev, 0x0, TDFAR);
231 sh_eth_write(ndev, 0x0, TDFXR);
232 sh_eth_write(ndev, 0x0, TDFFR);
233 sh_eth_write(ndev, 0x0, RDLAR);
234 sh_eth_write(ndev, 0x0, RDFAR);
235 sh_eth_write(ndev, 0x0, RDFXR);
236 sh_eth_write(ndev, 0x0, RDFFR);
237 } else {
238 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
239 EDMR);
240 mdelay(3);
241 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
242 EDMR);
243 }
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000244
245out:
246 return ret;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000247}
248
249static void sh_eth_set_duplex_giga(struct net_device *ndev)
250{
251 struct sh_eth_private *mdp = netdev_priv(ndev);
252
253 if (mdp->duplex) /* Full */
254 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
255 else /* Half */
256 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
257}
258
259static void sh_eth_set_rate_giga(struct net_device *ndev)
260{
261 struct sh_eth_private *mdp = netdev_priv(ndev);
262
263 switch (mdp->speed) {
264 case 10: /* 10BASE */
265 sh_eth_write(ndev, 0x00000000, GECMR);
266 break;
267 case 100:/* 100BASE */
268 sh_eth_write(ndev, 0x00000010, GECMR);
269 break;
270 case 1000: /* 1000BASE */
271 sh_eth_write(ndev, 0x00000020, GECMR);
272 break;
273 default:
274 break;
275 }
276}
277
278/* SH7757(GETHERC) */
279static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
280 .chip_reset = sh_eth_chip_reset_giga,
281 .set_duplex = sh_eth_set_duplex_giga,
282 .set_rate = sh_eth_set_rate_giga,
283
284 .ecsr_value = ECSR_ICD | ECSR_MPD,
285 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
286 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
287
288 .tx_check = EESR_TC1 | EESR_FTC,
289 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
290 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
291 EESR_ECI,
292 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
293 EESR_TFE,
294 .fdr_value = 0x0000072f,
295 .rmcr_value = 0x00000001,
296
297 .apr = 1,
298 .mpr = 1,
299 .tpauser = 1,
300 .bculr = 1,
301 .hw_swap = 1,
302 .rpadir = 1,
303 .rpadir_value = 2 << 16,
304 .no_trimd = 1,
305 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000306 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000307};
308
309static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
310{
311 if (sh_eth_is_gether(mdp))
312 return &sh_eth_my_cpu_data_giga;
313 else
314 return &sh_eth_my_cpu_data;
315}
316
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000317#elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000318#define SH_ETH_HAS_TSU 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000319static int sh_eth_check_reset(struct net_device *ndev);
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000320static void sh_eth_reset_hw_crc(struct net_device *ndev);
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000321
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000322static void sh_eth_chip_reset(struct net_device *ndev)
323{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000324 struct sh_eth_private *mdp = netdev_priv(ndev);
325
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000326 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000327 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000328 mdelay(1);
329}
330
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000331static void sh_eth_set_duplex(struct net_device *ndev)
332{
333 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000334
335 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000336 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000337 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000338 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000339}
340
341static void sh_eth_set_rate(struct net_device *ndev)
342{
343 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000344
345 switch (mdp->speed) {
346 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000347 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000348 break;
349 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000350 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000351 break;
352 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000353 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000354 break;
355 default:
356 break;
357 }
358}
359
360/* sh7763 */
361static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
362 .chip_reset = sh_eth_chip_reset,
363 .set_duplex = sh_eth_set_duplex,
364 .set_rate = sh_eth_set_rate,
365
366 .ecsr_value = ECSR_ICD | ECSR_MPD,
367 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
368 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
369
370 .tx_check = EESR_TC1 | EESR_FTC,
371 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
372 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
373 EESR_ECI,
374 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
375 EESR_TFE,
376
377 .apr = 1,
378 .mpr = 1,
379 .tpauser = 1,
380 .bculr = 1,
381 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000382 .no_trimd = 1,
383 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000384 .tsu = 1,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000385#if defined(CONFIG_CPU_SUBTYPE_SH7734)
386 .hw_crc = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000387 .select_mii = 1,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000388#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000389};
390
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000391static int sh_eth_reset(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000392{
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000393 int ret = 0;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000394
395 sh_eth_write(ndev, EDSR_ENALL, EDSR);
396 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000397
398 ret = sh_eth_check_reset(ndev);
399 if (ret)
400 goto out;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000401
402 /* Table Init */
403 sh_eth_write(ndev, 0x0, TDLAR);
404 sh_eth_write(ndev, 0x0, TDFAR);
405 sh_eth_write(ndev, 0x0, TDFXR);
406 sh_eth_write(ndev, 0x0, TDFFR);
407 sh_eth_write(ndev, 0x0, RDLAR);
408 sh_eth_write(ndev, 0x0, RDFAR);
409 sh_eth_write(ndev, 0x0, RDFXR);
410 sh_eth_write(ndev, 0x0, RDFFR);
411
412 /* Reset HW CRC register */
413 sh_eth_reset_hw_crc(ndev);
414
415 /* Select MII mode */
416 if (sh_eth_my_cpu_data.select_mii)
417 sh_eth_select_mii(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000418out:
419 return ret;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000420}
421
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000422static void sh_eth_reset_hw_crc(struct net_device *ndev)
423{
424 if (sh_eth_my_cpu_data.hw_crc)
425 sh_eth_write(ndev, 0x0, CSMR);
426}
427
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000428#elif defined(CONFIG_ARCH_R8A7740)
429#define SH_ETH_HAS_TSU 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000430static int sh_eth_check_reset(struct net_device *ndev);
431
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000432static void sh_eth_chip_reset(struct net_device *ndev)
433{
434 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000435
436 /* reset device */
437 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
438 mdelay(1);
439
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000440 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000441}
442
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000443static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000444{
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000445 int ret = 0;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000446
447 sh_eth_write(ndev, EDSR_ENALL, EDSR);
448 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000449
450 ret = sh_eth_check_reset(ndev);
451 if (ret)
452 goto out;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000453
454 /* Table Init */
455 sh_eth_write(ndev, 0x0, TDLAR);
456 sh_eth_write(ndev, 0x0, TDFAR);
457 sh_eth_write(ndev, 0x0, TDFXR);
458 sh_eth_write(ndev, 0x0, TDFFR);
459 sh_eth_write(ndev, 0x0, RDLAR);
460 sh_eth_write(ndev, 0x0, RDFAR);
461 sh_eth_write(ndev, 0x0, RDFXR);
462 sh_eth_write(ndev, 0x0, RDFFR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000463
464out:
465 return ret;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000466}
467
468static void sh_eth_set_duplex(struct net_device *ndev)
469{
470 struct sh_eth_private *mdp = netdev_priv(ndev);
471
472 if (mdp->duplex) /* Full */
473 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
474 else /* Half */
475 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
476}
477
478static void sh_eth_set_rate(struct net_device *ndev)
479{
480 struct sh_eth_private *mdp = netdev_priv(ndev);
481
482 switch (mdp->speed) {
483 case 10: /* 10BASE */
484 sh_eth_write(ndev, GECMR_10, GECMR);
485 break;
486 case 100:/* 100BASE */
487 sh_eth_write(ndev, GECMR_100, GECMR);
488 break;
489 case 1000: /* 1000BASE */
490 sh_eth_write(ndev, GECMR_1000, GECMR);
491 break;
492 default:
493 break;
494 }
495}
496
497/* R8A7740 */
498static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
499 .chip_reset = sh_eth_chip_reset,
500 .set_duplex = sh_eth_set_duplex,
501 .set_rate = sh_eth_set_rate,
502
503 .ecsr_value = ECSR_ICD | ECSR_MPD,
504 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
505 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
506
507 .tx_check = EESR_TC1 | EESR_FTC,
508 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
509 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
510 EESR_ECI,
511 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
512 EESR_TFE,
513
514 .apr = 1,
515 .mpr = 1,
516 .tpauser = 1,
517 .bculr = 1,
518 .hw_swap = 1,
519 .no_trimd = 1,
520 .no_ade = 1,
521 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000522 .select_mii = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000523};
524
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000525#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
526#define SH_ETH_RESET_DEFAULT 1
527static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
528 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
529
530 .apr = 1,
531 .mpr = 1,
532 .tpauser = 1,
533 .hw_swap = 1,
534};
535#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
536#define SH_ETH_RESET_DEFAULT 1
537#define SH_ETH_HAS_TSU 1
538static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
539 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000540 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000541};
542#endif
543
544static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
545{
546 if (!cd->ecsr_value)
547 cd->ecsr_value = DEFAULT_ECSR_INIT;
548
549 if (!cd->ecsipr_value)
550 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
551
552 if (!cd->fcftr_value)
553 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
554 DEFAULT_FIFO_F_D_RFD;
555
556 if (!cd->fdr_value)
557 cd->fdr_value = DEFAULT_FDR_INIT;
558
559 if (!cd->rmcr_value)
560 cd->rmcr_value = DEFAULT_RMCR_VALUE;
561
562 if (!cd->tx_check)
563 cd->tx_check = DEFAULT_TX_CHECK;
564
565 if (!cd->eesr_err_check)
566 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
567
568 if (!cd->tx_error_check)
569 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
570}
571
572#if defined(SH_ETH_RESET_DEFAULT)
573/* Chip Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000574static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000575{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000576 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000577 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000578 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000579
580 return 0;
581}
582#else
583static int sh_eth_check_reset(struct net_device *ndev)
584{
585 int ret = 0;
586 int cnt = 100;
587
588 while (cnt > 0) {
589 if (!(sh_eth_read(ndev, EDMR) & 0x3))
590 break;
591 mdelay(1);
592 cnt--;
593 }
594 if (cnt < 0) {
595 printk(KERN_ERR "Device reset fail\n");
596 ret = -ETIMEDOUT;
597 }
598 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000599}
600#endif
601
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000602#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000603static void sh_eth_set_receive_align(struct sk_buff *skb)
604{
605 int reserve;
606
607 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
608 if (reserve)
609 skb_reserve(skb, reserve);
610}
611#else
612static void sh_eth_set_receive_align(struct sk_buff *skb)
613{
614 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
615}
616#endif
617
618
Yoshinori Sato71557a32008-08-06 19:49:00 -0400619/* CPU <-> EDMAC endian convert */
620static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
621{
622 switch (mdp->edmac_endian) {
623 case EDMAC_LITTLE_ENDIAN:
624 return cpu_to_le32(x);
625 case EDMAC_BIG_ENDIAN:
626 return cpu_to_be32(x);
627 }
628 return x;
629}
630
631static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
632{
633 switch (mdp->edmac_endian) {
634 case EDMAC_LITTLE_ENDIAN:
635 return le32_to_cpu(x);
636 case EDMAC_BIG_ENDIAN:
637 return be32_to_cpu(x);
638 }
639 return x;
640}
641
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700642/*
643 * Program the hardware MAC address from dev->dev_addr.
644 */
645static void update_mac_address(struct net_device *ndev)
646{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000647 sh_eth_write(ndev,
648 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
649 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
650 sh_eth_write(ndev,
651 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700652}
653
654/*
655 * Get MAC address from SuperH MAC address register
656 *
657 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
658 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
659 * When you want use this device, you must set MAC address in bootloader.
660 *
661 */
Magnus Damm748031f2009-10-09 00:17:14 +0000662static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700663{
Magnus Damm748031f2009-10-09 00:17:14 +0000664 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
665 memcpy(ndev->dev_addr, mac, 6);
666 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000667 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
668 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
669 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
670 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
671 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
672 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000673 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700674}
675
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000676static int sh_eth_is_gether(struct sh_eth_private *mdp)
677{
678 if (mdp->reg_offset == sh_eth_offset_gigabit)
679 return 1;
680 else
681 return 0;
682}
683
684static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
685{
686 if (sh_eth_is_gether(mdp))
687 return EDTRR_TRNS_GETHER;
688 else
689 return EDTRR_TRNS_ETHER;
690}
691
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700692struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000693 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700694 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000695 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700696 u32 mmd_msk;/* MMD */
697 u32 mdo_msk;
698 u32 mdi_msk;
699 u32 mdc_msk;
700};
701
702/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000703static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700704{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000705 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700706}
707
708/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000709static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700710{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000711 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700712}
713
714/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000715static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700716{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000717 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700718}
719
720/* Data I/O pin control */
721static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
722{
723 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000724
725 if (bitbang->set_gate)
726 bitbang->set_gate(bitbang->addr);
727
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700728 if (bit)
729 bb_set(bitbang->addr, bitbang->mmd_msk);
730 else
731 bb_clr(bitbang->addr, bitbang->mmd_msk);
732}
733
734/* Set bit data*/
735static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
736{
737 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
738
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000739 if (bitbang->set_gate)
740 bitbang->set_gate(bitbang->addr);
741
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700742 if (bit)
743 bb_set(bitbang->addr, bitbang->mdo_msk);
744 else
745 bb_clr(bitbang->addr, bitbang->mdo_msk);
746}
747
748/* Get bit data*/
749static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
750{
751 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000752
753 if (bitbang->set_gate)
754 bitbang->set_gate(bitbang->addr);
755
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700756 return bb_read(bitbang->addr, bitbang->mdi_msk);
757}
758
759/* MDC pin control */
760static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
761{
762 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
763
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000764 if (bitbang->set_gate)
765 bitbang->set_gate(bitbang->addr);
766
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700767 if (bit)
768 bb_set(bitbang->addr, bitbang->mdc_msk);
769 else
770 bb_clr(bitbang->addr, bitbang->mdc_msk);
771}
772
773/* mdio bus control struct */
774static struct mdiobb_ops bb_ops = {
775 .owner = THIS_MODULE,
776 .set_mdc = sh_mdc_ctrl,
777 .set_mdio_dir = sh_mmd_ctrl,
778 .set_mdio_data = sh_set_mdio,
779 .get_mdio_data = sh_get_mdio,
780};
781
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700782/* free skb and descriptor buffer */
783static void sh_eth_ring_free(struct net_device *ndev)
784{
785 struct sh_eth_private *mdp = netdev_priv(ndev);
786 int i;
787
788 /* Free Rx skb ringbuffer */
789 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000790 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700791 if (mdp->rx_skbuff[i])
792 dev_kfree_skb(mdp->rx_skbuff[i]);
793 }
794 }
795 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000796 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700797
798 /* Free Tx skb ringbuffer */
799 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000800 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700801 if (mdp->tx_skbuff[i])
802 dev_kfree_skb(mdp->tx_skbuff[i]);
803 }
804 }
805 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000806 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700807}
808
809/* format skb and descriptor buffer */
810static void sh_eth_ring_format(struct net_device *ndev)
811{
812 struct sh_eth_private *mdp = netdev_priv(ndev);
813 int i;
814 struct sk_buff *skb;
815 struct sh_eth_rxdesc *rxdesc = NULL;
816 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000817 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
818 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700819
820 mdp->cur_rx = mdp->cur_tx = 0;
821 mdp->dirty_rx = mdp->dirty_tx = 0;
822
823 memset(mdp->rx_ring, 0, rx_ringsize);
824
825 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000826 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700827 /* skb */
828 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000829 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700830 mdp->rx_skbuff[i] = skb;
831 if (skb == NULL)
832 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000833 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000834 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000835 sh_eth_set_receive_align(skb);
836
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700837 /* RX descriptor */
838 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000839 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400840 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700841
842 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000843 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900844 /* Rx descriptor address set */
845 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000846 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000847 if (sh_eth_is_gether(mdp))
848 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900849 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700850 }
851
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000852 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700853
854 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400855 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700856
857 memset(mdp->tx_ring, 0, tx_ringsize);
858
859 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000860 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700861 mdp->tx_skbuff[i] = NULL;
862 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400863 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700864 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900865 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400866 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000867 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000868 if (sh_eth_is_gether(mdp))
869 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900870 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700871 }
872
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
876/* Get skb and descriptor buffer */
877static int sh_eth_ring_init(struct net_device *ndev)
878{
879 struct sh_eth_private *mdp = netdev_priv(ndev);
880 int rx_ringsize, tx_ringsize, ret = 0;
881
882 /*
883 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
884 * card needs room to do 8 byte alignment, +2 so we can reserve
885 * the first 2 bytes, and +16 gets room for the status word from the
886 * card.
887 */
888 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
889 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800890 if (mdp->cd->rpadir)
891 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700892
893 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +0000894 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
895 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700896 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700897 ret = -ENOMEM;
898 return ret;
899 }
900
Joe Perchesb2adaca2013-02-03 17:43:58 +0000901 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
902 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700903 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700904 ret = -ENOMEM;
905 goto skb_ring_free;
906 }
907
908 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000909 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700910 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
911 GFP_KERNEL);
912
913 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000914 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
915 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700916 ret = -ENOMEM;
917 goto desc_ring_free;
918 }
919
920 mdp->dirty_rx = 0;
921
922 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000923 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700924 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
925 GFP_KERNEL);
926 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000927 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
928 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700929 ret = -ENOMEM;
930 goto desc_ring_free;
931 }
932 return ret;
933
934desc_ring_free:
935 /* free DMA buffer */
936 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
937
938skb_ring_free:
939 /* Free Rx and Tx skb ring buffer */
940 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000941 mdp->tx_ring = NULL;
942 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700943
944 return ret;
945}
946
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000947static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
948{
949 int ringsize;
950
951 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000952 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000953 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
954 mdp->rx_desc_dma);
955 mdp->rx_ring = NULL;
956 }
957
958 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000959 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000960 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
961 mdp->tx_desc_dma);
962 mdp->tx_ring = NULL;
963 }
964}
965
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000966static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700967{
968 int ret = 0;
969 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700970 u32 val;
971
972 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000973 ret = sh_eth_reset(ndev);
974 if (ret)
975 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700976
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900977 /* Descriptor format */
978 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000979 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000980 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700981
982 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000983 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700984
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +0000985#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000986 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000987 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000988 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900989#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000990 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700991
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900992 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000993 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
994 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700995
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900996 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000997 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700998
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +0000999 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001000
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001001 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001002 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001003
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001004 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001005
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001006 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001007 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001008
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001009 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001010 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1011 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001012
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001013 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001014 if (start)
1015 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016
1017 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001018 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001019 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1020
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001021 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001022
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001023 if (mdp->cd->set_rate)
1024 mdp->cd->set_rate(ndev);
1025
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001026 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001027 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001028
1029 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001030 if (start)
1031 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001032
1033 /* Set MAC address */
1034 update_mac_address(ndev);
1035
1036 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001037 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001038 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001039 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001040 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001041 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001042 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001043
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001044 if (start) {
1045 /* Setting the Rx mode will start the Rx process. */
1046 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001047
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001048 netif_start_queue(ndev);
1049 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001050
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001051out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001052 return ret;
1053}
1054
1055/* free Tx skb function */
1056static int sh_eth_txfree(struct net_device *ndev)
1057{
1058 struct sh_eth_private *mdp = netdev_priv(ndev);
1059 struct sh_eth_txdesc *txdesc;
1060 int freeNum = 0;
1061 int entry = 0;
1062
1063 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001064 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001065 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001066 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001067 break;
1068 /* Free the original skb. */
1069 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001070 dma_unmap_single(&ndev->dev, txdesc->addr,
1071 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001072 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1073 mdp->tx_skbuff[entry] = NULL;
1074 freeNum++;
1075 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001076 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001077 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001078 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001079
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001080 ndev->stats.tx_packets++;
1081 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001082 }
1083 return freeNum;
1084}
1085
1086/* Packet receive function */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001087static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001088{
1089 struct sh_eth_private *mdp = netdev_priv(ndev);
1090 struct sh_eth_rxdesc *rxdesc;
1091
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001092 int entry = mdp->cur_rx % mdp->num_rx_ring;
1093 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001094 struct sk_buff *skb;
1095 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001096 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001097
1098 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001099 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1100 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001101 pkt_len = rxdesc->frame_length;
1102
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +00001103#if defined(CONFIG_ARCH_R8A7740)
1104 desc_status >>= 16;
1105#endif
1106
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001107 if (--boguscnt < 0)
1108 break;
1109
1110 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001111 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001112
1113 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1114 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001115 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001116 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001117 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001118 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001119 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001120 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001121 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001122 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001123 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001124 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001125 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001127 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001128 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001129 if (!mdp->cd->hw_swap)
1130 sh_eth_soft_swap(
1131 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1132 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001133 skb = mdp->rx_skbuff[entry];
1134 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001135 if (mdp->cd->rpadir)
1136 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001137 skb_put(skb, pkt_len);
1138 skb->protocol = eth_type_trans(skb, ndev);
1139 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001140 ndev->stats.rx_packets++;
1141 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001142 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001143 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001144 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001145 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001146 }
1147
1148 /* Refill the Rx ring buffers. */
1149 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001150 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001151 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001152 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001153 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001154
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001156 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001157 mdp->rx_skbuff[entry] = skb;
1158 if (skb == NULL)
1159 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001160 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001161 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001162 sh_eth_set_receive_align(skb);
1163
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001164 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001165 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001166 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001167 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001168 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001169 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170 else
1171 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001172 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001173 }
1174
1175 /* Restart Rx engine if stopped. */
1176 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001177 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001178 /* fix the values for the next receiving if RDE is set */
1179 if (intr_status & EESR_RDE)
1180 mdp->cur_rx = mdp->dirty_rx =
1181 (sh_eth_read(ndev, RDFAR) -
1182 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001183 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001184 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185
1186 return 0;
1187}
1188
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001189static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001190{
1191 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001192 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1193 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001194}
1195
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001196static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001197{
1198 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001199 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1200 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001201}
1202
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203/* error control function */
1204static void sh_eth_error(struct net_device *ndev, int intr_status)
1205{
1206 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001207 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001208 u32 link_stat;
1209 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001210
1211 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001212 felic_stat = sh_eth_read(ndev, ECSR);
1213 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001215 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001216 if (felic_stat & ECSR_LCHNG) {
1217 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001218 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001219 if (mdp->link == PHY_DOWN)
1220 link_stat = 0;
1221 else
1222 link_stat = PHY_ST_LINK;
1223 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001224 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001225 if (mdp->ether_link_active_low)
1226 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001227 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001228 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001229 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001230 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001232 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1233 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001234 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001235 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1236 ECSR);
1237 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1238 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001239 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001240 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001241 }
1242 }
1243 }
1244
1245 if (intr_status & EESR_TWB) {
1246 /* Write buck end. unused write back interrupt */
1247 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001248 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001249 if (netif_msg_tx_err(mdp))
1250 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001251 }
1252
1253 if (intr_status & EESR_RABT) {
1254 /* Receive Abort int */
1255 if (intr_status & EESR_RFRMER) {
1256 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001257 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001258 if (netif_msg_rx_err(mdp))
1259 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001260 }
1261 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001262
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001263 if (intr_status & EESR_TDE) {
1264 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001265 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001266 if (netif_msg_tx_err(mdp))
1267 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1268 }
1269
1270 if (intr_status & EESR_TFE) {
1271 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001272 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001273 if (netif_msg_tx_err(mdp))
1274 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001275 }
1276
1277 if (intr_status & EESR_RDE) {
1278 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001279 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001280
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001281 if (netif_msg_rx_err(mdp))
1282 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001283 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001284
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285 if (intr_status & EESR_RFE) {
1286 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001287 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001288 if (netif_msg_rx_err(mdp))
1289 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1290 }
1291
1292 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1293 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001294 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001295 if (netif_msg_tx_err(mdp))
1296 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001297 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001298
1299 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1300 if (mdp->cd->no_ade)
1301 mask &= ~EESR_ADE;
1302 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001303 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001304 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001306 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1307 intr_status, mdp->cur_tx);
1308 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001309 mdp->dirty_tx, (u32) ndev->state, edtrr);
1310 /* dirty buffer free */
1311 sh_eth_txfree(ndev);
1312
1313 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001314 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001315 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001316 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001317 }
1318 /* wakeup */
1319 netif_wake_queue(ndev);
1320 }
1321}
1322
1323static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1324{
1325 struct net_device *ndev = netdev;
1326 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001327 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001328 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001329 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001330
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001331 spin_lock(&mdp->lock);
1332
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001333 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001334 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001335 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001336 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1337 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001338 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001339 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001340 ret = IRQ_HANDLED;
1341 } else
1342 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001343
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001344 if (intr_status & (EESR_FRC | /* Frame recv*/
1345 EESR_RMAF | /* Multi cast address recv*/
1346 EESR_RRF | /* Bit frame recv */
1347 EESR_RTLF | /* Long frame recv*/
1348 EESR_RTSF | /* short frame recv */
1349 EESR_PRE | /* PHY-LSI recv error */
1350 EESR_CERF)){ /* recv frame CRC error */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001351 sh_eth_rx(ndev, intr_status);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001352 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001353
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001354 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001355 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001356 sh_eth_txfree(ndev);
1357 netif_wake_queue(ndev);
1358 }
1359
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001360 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001361 sh_eth_error(ndev, intr_status);
1362
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001363other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001364 spin_unlock(&mdp->lock);
1365
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001366 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001367}
1368
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001369/* PHY state control function */
1370static void sh_eth_adjust_link(struct net_device *ndev)
1371{
1372 struct sh_eth_private *mdp = netdev_priv(ndev);
1373 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 int new_state = 0;
1375
1376 if (phydev->link != PHY_DOWN) {
1377 if (phydev->duplex != mdp->duplex) {
1378 new_state = 1;
1379 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001380 if (mdp->cd->set_duplex)
1381 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001382 }
1383
1384 if (phydev->speed != mdp->speed) {
1385 new_state = 1;
1386 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001387 if (mdp->cd->set_rate)
1388 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001389 }
1390 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001391 sh_eth_write(ndev,
1392 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001393 new_state = 1;
1394 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001395 }
1396 } else if (mdp->link) {
1397 new_state = 1;
1398 mdp->link = PHY_DOWN;
1399 mdp->speed = 0;
1400 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 }
1402
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001403 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001404 phy_print_status(phydev);
1405}
1406
1407/* PHY init function */
1408static int sh_eth_phy_init(struct net_device *ndev)
1409{
1410 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001411 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412 struct phy_device *phydev = NULL;
1413
Kay Sieversfb28ad352008-11-10 13:55:14 -08001414 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001415 mdp->mii_bus->id , mdp->phy_id);
1416
1417 mdp->link = PHY_DOWN;
1418 mdp->speed = 0;
1419 mdp->duplex = -1;
1420
1421 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001422 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001423 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001424 if (IS_ERR(phydev)) {
1425 dev_err(&ndev->dev, "phy_connect failed\n");
1426 return PTR_ERR(phydev);
1427 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001428
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001429 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001430 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001431
1432 mdp->phydev = phydev;
1433
1434 return 0;
1435}
1436
1437/* PHY control start function */
1438static int sh_eth_phy_start(struct net_device *ndev)
1439{
1440 struct sh_eth_private *mdp = netdev_priv(ndev);
1441 int ret;
1442
1443 ret = sh_eth_phy_init(ndev);
1444 if (ret)
1445 return ret;
1446
1447 /* reset phy - this also wakes it from PDOWN */
1448 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1449 phy_start(mdp->phydev);
1450
1451 return 0;
1452}
1453
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001454static int sh_eth_get_settings(struct net_device *ndev,
1455 struct ethtool_cmd *ecmd)
1456{
1457 struct sh_eth_private *mdp = netdev_priv(ndev);
1458 unsigned long flags;
1459 int ret;
1460
1461 spin_lock_irqsave(&mdp->lock, flags);
1462 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1463 spin_unlock_irqrestore(&mdp->lock, flags);
1464
1465 return ret;
1466}
1467
1468static int sh_eth_set_settings(struct net_device *ndev,
1469 struct ethtool_cmd *ecmd)
1470{
1471 struct sh_eth_private *mdp = netdev_priv(ndev);
1472 unsigned long flags;
1473 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001474
1475 spin_lock_irqsave(&mdp->lock, flags);
1476
1477 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001478 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001479
1480 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1481 if (ret)
1482 goto error_exit;
1483
1484 if (ecmd->duplex == DUPLEX_FULL)
1485 mdp->duplex = 1;
1486 else
1487 mdp->duplex = 0;
1488
1489 if (mdp->cd->set_duplex)
1490 mdp->cd->set_duplex(ndev);
1491
1492error_exit:
1493 mdelay(1);
1494
1495 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001496 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001497
1498 spin_unlock_irqrestore(&mdp->lock, flags);
1499
1500 return ret;
1501}
1502
1503static int sh_eth_nway_reset(struct net_device *ndev)
1504{
1505 struct sh_eth_private *mdp = netdev_priv(ndev);
1506 unsigned long flags;
1507 int ret;
1508
1509 spin_lock_irqsave(&mdp->lock, flags);
1510 ret = phy_start_aneg(mdp->phydev);
1511 spin_unlock_irqrestore(&mdp->lock, flags);
1512
1513 return ret;
1514}
1515
1516static u32 sh_eth_get_msglevel(struct net_device *ndev)
1517{
1518 struct sh_eth_private *mdp = netdev_priv(ndev);
1519 return mdp->msg_enable;
1520}
1521
1522static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1523{
1524 struct sh_eth_private *mdp = netdev_priv(ndev);
1525 mdp->msg_enable = value;
1526}
1527
1528static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1529 "rx_current", "tx_current",
1530 "rx_dirty", "tx_dirty",
1531};
1532#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1533
1534static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1535{
1536 switch (sset) {
1537 case ETH_SS_STATS:
1538 return SH_ETH_STATS_LEN;
1539 default:
1540 return -EOPNOTSUPP;
1541 }
1542}
1543
1544static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1545 struct ethtool_stats *stats, u64 *data)
1546{
1547 struct sh_eth_private *mdp = netdev_priv(ndev);
1548 int i = 0;
1549
1550 /* device-specific stats */
1551 data[i++] = mdp->cur_rx;
1552 data[i++] = mdp->cur_tx;
1553 data[i++] = mdp->dirty_rx;
1554 data[i++] = mdp->dirty_tx;
1555}
1556
1557static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1558{
1559 switch (stringset) {
1560 case ETH_SS_STATS:
1561 memcpy(data, *sh_eth_gstrings_stats,
1562 sizeof(sh_eth_gstrings_stats));
1563 break;
1564 }
1565}
1566
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001567static void sh_eth_get_ringparam(struct net_device *ndev,
1568 struct ethtool_ringparam *ring)
1569{
1570 struct sh_eth_private *mdp = netdev_priv(ndev);
1571
1572 ring->rx_max_pending = RX_RING_MAX;
1573 ring->tx_max_pending = TX_RING_MAX;
1574 ring->rx_pending = mdp->num_rx_ring;
1575 ring->tx_pending = mdp->num_tx_ring;
1576}
1577
1578static int sh_eth_set_ringparam(struct net_device *ndev,
1579 struct ethtool_ringparam *ring)
1580{
1581 struct sh_eth_private *mdp = netdev_priv(ndev);
1582 int ret;
1583
1584 if (ring->tx_pending > TX_RING_MAX ||
1585 ring->rx_pending > RX_RING_MAX ||
1586 ring->tx_pending < TX_RING_MIN ||
1587 ring->rx_pending < RX_RING_MIN)
1588 return -EINVAL;
1589 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1590 return -EINVAL;
1591
1592 if (netif_running(ndev)) {
1593 netif_tx_disable(ndev);
1594 /* Disable interrupts by clearing the interrupt mask. */
1595 sh_eth_write(ndev, 0x0000, EESIPR);
1596 /* Stop the chip's Tx and Rx processes. */
1597 sh_eth_write(ndev, 0, EDTRR);
1598 sh_eth_write(ndev, 0, EDRRR);
1599 synchronize_irq(ndev->irq);
1600 }
1601
1602 /* Free all the skbuffs in the Rx queue. */
1603 sh_eth_ring_free(ndev);
1604 /* Free DMA buffer */
1605 sh_eth_free_dma_buffer(mdp);
1606
1607 /* Set new parameters */
1608 mdp->num_rx_ring = ring->rx_pending;
1609 mdp->num_tx_ring = ring->tx_pending;
1610
1611 ret = sh_eth_ring_init(ndev);
1612 if (ret < 0) {
1613 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1614 return ret;
1615 }
1616 ret = sh_eth_dev_init(ndev, false);
1617 if (ret < 0) {
1618 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1619 return ret;
1620 }
1621
1622 if (netif_running(ndev)) {
1623 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1624 /* Setting the Rx mode will start the Rx process. */
1625 sh_eth_write(ndev, EDRRR_R, EDRRR);
1626 netif_wake_queue(ndev);
1627 }
1628
1629 return 0;
1630}
1631
stephen hemminger9b07be42012-01-04 12:59:49 +00001632static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001633 .get_settings = sh_eth_get_settings,
1634 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001635 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001636 .get_msglevel = sh_eth_get_msglevel,
1637 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001638 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001639 .get_strings = sh_eth_get_strings,
1640 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1641 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001642 .get_ringparam = sh_eth_get_ringparam,
1643 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001644};
1645
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001646/* network device open function */
1647static int sh_eth_open(struct net_device *ndev)
1648{
1649 int ret = 0;
1650 struct sh_eth_private *mdp = netdev_priv(ndev);
1651
Magnus Dammbcd51492009-10-09 00:20:04 +00001652 pm_runtime_get_sync(&mdp->pdev->dev);
1653
Joe Perchesa0607fd2009-11-18 23:29:17 -08001654 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001655#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001656 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1657 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001658 IRQF_SHARED,
1659#else
1660 0,
1661#endif
1662 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001663 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001664 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001665 return ret;
1666 }
1667
1668 /* Descriptor set */
1669 ret = sh_eth_ring_init(ndev);
1670 if (ret)
1671 goto out_free_irq;
1672
1673 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001674 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001675 if (ret)
1676 goto out_free_irq;
1677
1678 /* PHY control start*/
1679 ret = sh_eth_phy_start(ndev);
1680 if (ret)
1681 goto out_free_irq;
1682
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 return ret;
1684
1685out_free_irq:
1686 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001687 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001688 return ret;
1689}
1690
1691/* Timeout function */
1692static void sh_eth_tx_timeout(struct net_device *ndev)
1693{
1694 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001695 struct sh_eth_rxdesc *rxdesc;
1696 int i;
1697
1698 netif_stop_queue(ndev);
1699
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001700 if (netif_msg_timer(mdp))
1701 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001702 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001703
1704 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001705 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001706
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001707 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001708 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001709 rxdesc = &mdp->rx_ring[i];
1710 rxdesc->status = 0;
1711 rxdesc->addr = 0xBADF00D0;
1712 if (mdp->rx_skbuff[i])
1713 dev_kfree_skb(mdp->rx_skbuff[i]);
1714 mdp->rx_skbuff[i] = NULL;
1715 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001716 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001717 if (mdp->tx_skbuff[i])
1718 dev_kfree_skb(mdp->tx_skbuff[i]);
1719 mdp->tx_skbuff[i] = NULL;
1720 }
1721
1722 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001723 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001724}
1725
1726/* Packet transmit function */
1727static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1728{
1729 struct sh_eth_private *mdp = netdev_priv(ndev);
1730 struct sh_eth_txdesc *txdesc;
1731 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001732 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001733
1734 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001735 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001736 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001737 if (netif_msg_tx_queued(mdp))
1738 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001739 netif_stop_queue(ndev);
1740 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001741 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001742 }
1743 }
1744 spin_unlock_irqrestore(&mdp->lock, flags);
1745
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001746 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001747 mdp->tx_skbuff[entry] = skb;
1748 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001749 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001750 if (!mdp->cd->hw_swap)
1751 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1752 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001753 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1754 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001755 if (skb->len < ETHERSMALL)
1756 txdesc->buffer_length = ETHERSMALL;
1757 else
1758 txdesc->buffer_length = skb->len;
1759
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001760 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001761 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001762 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001763 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001764
1765 mdp->cur_tx++;
1766
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001767 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1768 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001769
Patrick McHardy6ed10652009-06-23 06:03:08 +00001770 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001771}
1772
1773/* device close function */
1774static int sh_eth_close(struct net_device *ndev)
1775{
1776 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001777
1778 netif_stop_queue(ndev);
1779
1780 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001781 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001782
1783 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001784 sh_eth_write(ndev, 0, EDTRR);
1785 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001786
1787 /* PHY Disconnect */
1788 if (mdp->phydev) {
1789 phy_stop(mdp->phydev);
1790 phy_disconnect(mdp->phydev);
1791 }
1792
1793 free_irq(ndev->irq, ndev);
1794
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001795 /* Free all the skbuffs in the Rx queue. */
1796 sh_eth_ring_free(ndev);
1797
1798 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001799 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800
Magnus Dammbcd51492009-10-09 00:20:04 +00001801 pm_runtime_put_sync(&mdp->pdev->dev);
1802
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 return 0;
1804}
1805
1806static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1807{
1808 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001809
Magnus Dammbcd51492009-10-09 00:20:04 +00001810 pm_runtime_get_sync(&mdp->pdev->dev);
1811
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001812 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001813 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001814 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001815 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001816 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001817 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001818 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001819 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001820 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001821 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001822 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1823 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001824 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001825 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1826 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001827 pm_runtime_put_sync(&mdp->pdev->dev);
1828
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001829 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001830}
1831
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001832/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001833static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1834 int cmd)
1835{
1836 struct sh_eth_private *mdp = netdev_priv(ndev);
1837 struct phy_device *phydev = mdp->phydev;
1838
1839 if (!netif_running(ndev))
1840 return -EINVAL;
1841
1842 if (!phydev)
1843 return -ENODEV;
1844
Richard Cochran28b04112010-07-17 08:48:55 +00001845 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001846}
1847
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001848#if defined(SH_ETH_HAS_TSU)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00001849/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
1850static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
1851 int entry)
1852{
1853 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
1854}
1855
1856static u32 sh_eth_tsu_get_post_mask(int entry)
1857{
1858 return 0x0f << (28 - ((entry % 8) * 4));
1859}
1860
1861static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
1862{
1863 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
1864}
1865
1866static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
1867 int entry)
1868{
1869 struct sh_eth_private *mdp = netdev_priv(ndev);
1870 u32 tmp;
1871 void *reg_offset;
1872
1873 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
1874 tmp = ioread32(reg_offset);
1875 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
1876}
1877
1878static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
1879 int entry)
1880{
1881 struct sh_eth_private *mdp = netdev_priv(ndev);
1882 u32 post_mask, ref_mask, tmp;
1883 void *reg_offset;
1884
1885 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
1886 post_mask = sh_eth_tsu_get_post_mask(entry);
1887 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
1888
1889 tmp = ioread32(reg_offset);
1890 iowrite32(tmp & ~post_mask, reg_offset);
1891
1892 /* If other port enables, the function returns "true" */
1893 return tmp & ref_mask;
1894}
1895
1896static int sh_eth_tsu_busy(struct net_device *ndev)
1897{
1898 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
1899 struct sh_eth_private *mdp = netdev_priv(ndev);
1900
1901 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
1902 udelay(10);
1903 timeout--;
1904 if (timeout <= 0) {
1905 dev_err(&ndev->dev, "%s: timeout\n", __func__);
1906 return -ETIMEDOUT;
1907 }
1908 }
1909
1910 return 0;
1911}
1912
1913static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
1914 const u8 *addr)
1915{
1916 u32 val;
1917
1918 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
1919 iowrite32(val, reg);
1920 if (sh_eth_tsu_busy(ndev) < 0)
1921 return -EBUSY;
1922
1923 val = addr[4] << 8 | addr[5];
1924 iowrite32(val, reg + 4);
1925 if (sh_eth_tsu_busy(ndev) < 0)
1926 return -EBUSY;
1927
1928 return 0;
1929}
1930
1931static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
1932{
1933 u32 val;
1934
1935 val = ioread32(reg);
1936 addr[0] = (val >> 24) & 0xff;
1937 addr[1] = (val >> 16) & 0xff;
1938 addr[2] = (val >> 8) & 0xff;
1939 addr[3] = val & 0xff;
1940 val = ioread32(reg + 4);
1941 addr[4] = (val >> 8) & 0xff;
1942 addr[5] = val & 0xff;
1943}
1944
1945
1946static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
1947{
1948 struct sh_eth_private *mdp = netdev_priv(ndev);
1949 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1950 int i;
1951 u8 c_addr[ETH_ALEN];
1952
1953 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
1954 sh_eth_tsu_read_entry(reg_offset, c_addr);
1955 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
1956 return i;
1957 }
1958
1959 return -ENOENT;
1960}
1961
1962static int sh_eth_tsu_find_empty(struct net_device *ndev)
1963{
1964 u8 blank[ETH_ALEN];
1965 int entry;
1966
1967 memset(blank, 0, sizeof(blank));
1968 entry = sh_eth_tsu_find_entry(ndev, blank);
1969 return (entry < 0) ? -ENOMEM : entry;
1970}
1971
1972static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
1973 int entry)
1974{
1975 struct sh_eth_private *mdp = netdev_priv(ndev);
1976 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1977 int ret;
1978 u8 blank[ETH_ALEN];
1979
1980 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
1981 ~(1 << (31 - entry)), TSU_TEN);
1982
1983 memset(blank, 0, sizeof(blank));
1984 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
1985 if (ret < 0)
1986 return ret;
1987 return 0;
1988}
1989
1990static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
1991{
1992 struct sh_eth_private *mdp = netdev_priv(ndev);
1993 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
1994 int i, ret;
1995
1996 if (!mdp->cd->tsu)
1997 return 0;
1998
1999 i = sh_eth_tsu_find_entry(ndev, addr);
2000 if (i < 0) {
2001 /* No entry found, create one */
2002 i = sh_eth_tsu_find_empty(ndev);
2003 if (i < 0)
2004 return -ENOMEM;
2005 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2006 if (ret < 0)
2007 return ret;
2008
2009 /* Enable the entry */
2010 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2011 (1 << (31 - i)), TSU_TEN);
2012 }
2013
2014 /* Entry found or created, enable POST */
2015 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2016
2017 return 0;
2018}
2019
2020static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2021{
2022 struct sh_eth_private *mdp = netdev_priv(ndev);
2023 int i, ret;
2024
2025 if (!mdp->cd->tsu)
2026 return 0;
2027
2028 i = sh_eth_tsu_find_entry(ndev, addr);
2029 if (i) {
2030 /* Entry found */
2031 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2032 goto done;
2033
2034 /* Disable the entry if both ports was disabled */
2035 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2036 if (ret < 0)
2037 return ret;
2038 }
2039done:
2040 return 0;
2041}
2042
2043static int sh_eth_tsu_purge_all(struct net_device *ndev)
2044{
2045 struct sh_eth_private *mdp = netdev_priv(ndev);
2046 int i, ret;
2047
2048 if (unlikely(!mdp->cd->tsu))
2049 return 0;
2050
2051 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2052 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2053 continue;
2054
2055 /* Disable the entry if both ports was disabled */
2056 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2057 if (ret < 0)
2058 return ret;
2059 }
2060
2061 return 0;
2062}
2063
2064static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2065{
2066 struct sh_eth_private *mdp = netdev_priv(ndev);
2067 u8 addr[ETH_ALEN];
2068 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2069 int i;
2070
2071 if (unlikely(!mdp->cd->tsu))
2072 return;
2073
2074 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2075 sh_eth_tsu_read_entry(reg_offset, addr);
2076 if (is_multicast_ether_addr(addr))
2077 sh_eth_tsu_del_entry(ndev, addr);
2078 }
2079}
2080
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002081/* Multicast reception directions set */
2082static void sh_eth_set_multicast_list(struct net_device *ndev)
2083{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002084 struct sh_eth_private *mdp = netdev_priv(ndev);
2085 u32 ecmr_bits;
2086 int mcast_all = 0;
2087 unsigned long flags;
2088
2089 spin_lock_irqsave(&mdp->lock, flags);
2090 /*
2091 * Initial condition is MCT = 1, PRM = 0.
2092 * Depending on ndev->flags, set PRM or clear MCT
2093 */
2094 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2095
2096 if (!(ndev->flags & IFF_MULTICAST)) {
2097 sh_eth_tsu_purge_mcast(ndev);
2098 mcast_all = 1;
2099 }
2100 if (ndev->flags & IFF_ALLMULTI) {
2101 sh_eth_tsu_purge_mcast(ndev);
2102 ecmr_bits &= ~ECMR_MCT;
2103 mcast_all = 1;
2104 }
2105
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002106 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002107 sh_eth_tsu_purge_all(ndev);
2108 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2109 } else if (mdp->cd->tsu) {
2110 struct netdev_hw_addr *ha;
2111 netdev_for_each_mc_addr(ha, ndev) {
2112 if (mcast_all && is_multicast_ether_addr(ha->addr))
2113 continue;
2114
2115 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2116 if (!mcast_all) {
2117 sh_eth_tsu_purge_mcast(ndev);
2118 ecmr_bits &= ~ECMR_MCT;
2119 mcast_all = 1;
2120 }
2121 }
2122 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002123 } else {
2124 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002125 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002126 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002127
2128 /* update the ethernet mode */
2129 sh_eth_write(ndev, ecmr_bits, ECMR);
2130
2131 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002132}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002133
2134static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2135{
2136 if (!mdp->port)
2137 return TSU_VTAG0;
2138 else
2139 return TSU_VTAG1;
2140}
2141
2142static int sh_eth_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
2143{
2144 struct sh_eth_private *mdp = netdev_priv(ndev);
2145 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2146
2147 if (unlikely(!mdp->cd->tsu))
2148 return -EPERM;
2149
2150 /* No filtering if vid = 0 */
2151 if (!vid)
2152 return 0;
2153
2154 mdp->vlan_num_ids++;
2155
2156 /*
2157 * The controller has one VLAN tag HW filter. So, if the filter is
2158 * already enabled, the driver disables it and the filte
2159 */
2160 if (mdp->vlan_num_ids > 1) {
2161 /* disable VLAN filter */
2162 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2163 return 0;
2164 }
2165
2166 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2167 vtag_reg_index);
2168
2169 return 0;
2170}
2171
2172static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
2173{
2174 struct sh_eth_private *mdp = netdev_priv(ndev);
2175 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2176
2177 if (unlikely(!mdp->cd->tsu))
2178 return -EPERM;
2179
2180 /* No filtering if vid = 0 */
2181 if (!vid)
2182 return 0;
2183
2184 mdp->vlan_num_ids--;
2185 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2186
2187 return 0;
2188}
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002189#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002190
2191/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002192static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002193{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002194 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2195 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2196 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2197 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2198 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2199 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2200 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2201 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2202 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2203 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002204 if (sh_eth_is_gether(mdp)) {
2205 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2206 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2207 } else {
2208 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2209 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2210 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002211 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2212 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2213 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2214 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2215 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2216 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2217 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002218}
2219
2220/* MDIO bus release function */
2221static int sh_mdio_release(struct net_device *ndev)
2222{
Sergei Shtylyov0582b7d2013-03-19 13:40:23 +00002223 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002224 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2225
2226 /* unregister mdio bus */
2227 mdiobus_unregister(bus);
2228
2229 /* remove mdio bus info from net_device */
2230 dev_set_drvdata(&ndev->dev, NULL);
2231
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00002232 /* free interrupts memory */
2233 kfree(bus->irq);
2234
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002235 /* free bitbang info */
2236 free_mdio_bitbang(bus);
2237
Sergei Shtylyov0582b7d2013-03-19 13:40:23 +00002238 /* free bitbang memory */
2239 kfree(mdp->bitbang);
2240
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002241 return 0;
2242}
2243
2244/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002245static int sh_mdio_init(struct net_device *ndev, int id,
2246 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002247{
2248 int ret, i;
2249 struct bb_info *bitbang;
2250 struct sh_eth_private *mdp = netdev_priv(ndev);
2251
2252 /* create bit control struct for PHY */
2253 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
2254 if (!bitbang) {
2255 ret = -ENOMEM;
2256 goto out;
2257 }
2258
2259 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002260 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002261 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002262 bitbang->mdi_msk = 0x08;
2263 bitbang->mdo_msk = 0x04;
2264 bitbang->mmd_msk = 0x02;/* MMD */
2265 bitbang->mdc_msk = 0x01;
2266 bitbang->ctrl.ops = &bb_ops;
2267
Stefan Weilc2e07b32010-08-03 19:44:52 +02002268 /* MII controller setting */
Sergei Shtylyov0582b7d2013-03-19 13:40:23 +00002269 mdp->bitbang = bitbang;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002270 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2271 if (!mdp->mii_bus) {
2272 ret = -ENOMEM;
2273 goto out_free_bitbang;
2274 }
2275
2276 /* Hook up MII support for ethtool */
2277 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002278 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002279 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002280 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002281
2282 /* PHY IRQ */
2283 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
2284 if (!mdp->mii_bus->irq) {
2285 ret = -ENOMEM;
2286 goto out_free_bus;
2287 }
2288
2289 for (i = 0; i < PHY_MAX_ADDR; i++)
2290 mdp->mii_bus->irq[i] = PHY_POLL;
2291
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002292 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002293 ret = mdiobus_register(mdp->mii_bus);
2294 if (ret)
2295 goto out_free_irq;
2296
2297 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2298
2299 return 0;
2300
2301out_free_irq:
2302 kfree(mdp->mii_bus->irq);
2303
2304out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002305 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002306
2307out_free_bitbang:
2308 kfree(bitbang);
2309
2310out:
2311 return ret;
2312}
2313
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002314static const u16 *sh_eth_get_register_offset(int register_type)
2315{
2316 const u16 *reg_offset = NULL;
2317
2318 switch (register_type) {
2319 case SH_ETH_REG_GIGABIT:
2320 reg_offset = sh_eth_offset_gigabit;
2321 break;
2322 case SH_ETH_REG_FAST_SH4:
2323 reg_offset = sh_eth_offset_fast_sh4;
2324 break;
2325 case SH_ETH_REG_FAST_SH3_SH2:
2326 reg_offset = sh_eth_offset_fast_sh3_sh2;
2327 break;
2328 default:
2329 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
2330 break;
2331 }
2332
2333 return reg_offset;
2334}
2335
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002336static const struct net_device_ops sh_eth_netdev_ops = {
2337 .ndo_open = sh_eth_open,
2338 .ndo_stop = sh_eth_close,
2339 .ndo_start_xmit = sh_eth_start_xmit,
2340 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002341#if defined(SH_ETH_HAS_TSU)
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002342 .ndo_set_rx_mode = sh_eth_set_multicast_list,
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002343 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2344 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002345#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002346 .ndo_tx_timeout = sh_eth_tx_timeout,
2347 .ndo_do_ioctl = sh_eth_do_ioctl,
2348 .ndo_validate_addr = eth_validate_addr,
2349 .ndo_set_mac_address = eth_mac_addr,
2350 .ndo_change_mtu = eth_change_mtu,
2351};
2352
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002353static int sh_eth_drv_probe(struct platform_device *pdev)
2354{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002355 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002356 struct resource *res;
2357 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002358 struct sh_eth_private *mdp = NULL;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002359 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002360
2361 /* get base addr */
2362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2363 if (unlikely(res == NULL)) {
2364 dev_err(&pdev->dev, "invalid resource\n");
2365 ret = -EINVAL;
2366 goto out;
2367 }
2368
2369 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2370 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002371 ret = -ENOMEM;
2372 goto out;
2373 }
2374
2375 /* The sh Ether-specific entries in the device structure. */
2376 ndev->base_addr = res->start;
2377 devno = pdev->id;
2378 if (devno < 0)
2379 devno = 0;
2380
2381 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002382 ret = platform_get_irq(pdev, 0);
2383 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002384 ret = -ENODEV;
2385 goto out_release;
2386 }
roel kluincc3c0802008-09-10 19:22:44 +02002387 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002388
2389 SET_NETDEV_DEV(ndev, &pdev->dev);
2390
2391 /* Fill in the fields of the device structure with ethernet values. */
2392 ether_setup(ndev);
2393
2394 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002395 mdp->num_tx_ring = TX_RING_SIZE;
2396 mdp->num_rx_ring = RX_RING_SIZE;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002397 mdp->addr = ioremap(res->start, resource_size(res));
2398 if (mdp->addr == NULL) {
2399 ret = -ENOMEM;
2400 dev_err(&pdev->dev, "ioremap failed.\n");
2401 goto out_release;
2402 }
2403
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002404 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002405 mdp->pdev = pdev;
2406 pm_runtime_enable(&pdev->dev);
2407 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002408
Yoshinori Sato71557a32008-08-06 19:49:00 -04002409 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002410 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002411 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002412 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002413 /* EDMAC endian */
2414 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002415 mdp->no_ether_link = pd->no_ether_link;
2416 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002417 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002418
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002419 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002420#if defined(SH_ETH_HAS_BOTH_MODULES)
2421 mdp->cd = sh_eth_get_cpu_data(mdp);
2422#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002423 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002424#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002425 sh_eth_set_default_cpu_data(mdp->cd);
2426
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002427 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002428 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002429 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002430 ndev->watchdog_timeo = TX_TIMEOUT;
2431
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002432 /* debug message level */
2433 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002434
2435 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002436 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002437
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002438 /* ioremap the TSU registers */
2439 if (mdp->cd->tsu) {
2440 struct resource *rtsu;
2441 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2442 if (!rtsu) {
2443 dev_err(&pdev->dev, "Not found TSU resource\n");
Peter Senna Tschudin043c4782012-10-05 12:40:52 +00002444 ret = -ENODEV;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002445 goto out_release;
2446 }
2447 mdp->tsu_addr = ioremap(rtsu->start,
2448 resource_size(rtsu));
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002449 if (mdp->tsu_addr == NULL) {
2450 ret = -ENOMEM;
2451 dev_err(&pdev->dev, "TSU ioremap failed.\n");
2452 goto out_release;
2453 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002454 mdp->port = devno % 2;
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002455 ndev->features = NETIF_F_HW_VLAN_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002456 }
2457
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002458 /* initialize first or needed device */
2459 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002460 if (mdp->cd->chip_reset)
2461 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002462
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002463 if (mdp->cd->tsu) {
2464 /* TSU init (Init only)*/
2465 sh_eth_tsu_init(mdp);
2466 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002467 }
2468
2469 /* network device register */
2470 ret = register_netdev(ndev);
2471 if (ret)
2472 goto out_release;
2473
2474 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002475 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002476 if (ret)
2477 goto out_unregister;
2478
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002479 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002480 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2481 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002482
2483 platform_set_drvdata(pdev, ndev);
2484
2485 return ret;
2486
2487out_unregister:
2488 unregister_netdev(ndev);
2489
2490out_release:
2491 /* net_dev free */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002492 if (mdp && mdp->addr)
2493 iounmap(mdp->addr);
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002494 if (mdp && mdp->tsu_addr)
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002495 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002496 if (ndev)
2497 free_netdev(ndev);
2498
2499out:
2500 return ret;
2501}
2502
2503static int sh_eth_drv_remove(struct platform_device *pdev)
2504{
2505 struct net_device *ndev = platform_get_drvdata(pdev);
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002506 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002507
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002508 if (mdp->cd->tsu)
2509 iounmap(mdp->tsu_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002510 sh_mdio_release(ndev);
2511 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00002512 pm_runtime_disable(&pdev->dev);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002513 iounmap(mdp->addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002514 free_netdev(ndev);
2515 platform_set_drvdata(pdev, NULL);
2516
2517 return 0;
2518}
2519
Magnus Dammbcd51492009-10-09 00:20:04 +00002520static int sh_eth_runtime_nop(struct device *dev)
2521{
2522 /*
2523 * Runtime PM callback shared between ->runtime_suspend()
2524 * and ->runtime_resume(). Simply returns success.
2525 *
2526 * This driver re-initializes all registers after
2527 * pm_runtime_get_sync() anyway so there is no need
2528 * to save and restore registers here.
2529 */
2530 return 0;
2531}
2532
2533static struct dev_pm_ops sh_eth_dev_pm_ops = {
2534 .runtime_suspend = sh_eth_runtime_nop,
2535 .runtime_resume = sh_eth_runtime_nop,
2536};
2537
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002538static struct platform_driver sh_eth_driver = {
2539 .probe = sh_eth_drv_probe,
2540 .remove = sh_eth_drv_remove,
2541 .driver = {
2542 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00002543 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002544 },
2545};
2546
Axel Lindb62f682011-11-27 16:44:17 +00002547module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002548
2549MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2550MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2551MODULE_LICENSE("GPL v2");