blob: 0699b1ec7e32c92ba18b472ed61536f2a2813d41 [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
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00005 * Copyright (C) 2008-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
22 */
23
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070024#include <linux/init.h>
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070028#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070029#include <linux/dma-mapping.h>
30#include <linux/etherdevice.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/mdio-bitbang.h>
34#include <linux/netdevice.h>
35#include <linux/phy.h>
36#include <linux/cache.h>
37#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000038#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000041#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000042#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000043#include <linux/sh_eth.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070044
45#include "sh_eth.h"
46
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000047#define SH_ETH_DEF_MSG_ENABLE \
48 (NETIF_MSG_LINK | \
49 NETIF_MSG_TIMER | \
50 NETIF_MSG_RX_ERR| \
51 NETIF_MSG_TX_ERR)
52
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000053static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
54 [EDSR] = 0x0000,
55 [EDMR] = 0x0400,
56 [EDTRR] = 0x0408,
57 [EDRRR] = 0x0410,
58 [EESR] = 0x0428,
59 [EESIPR] = 0x0430,
60 [TDLAR] = 0x0010,
61 [TDFAR] = 0x0014,
62 [TDFXR] = 0x0018,
63 [TDFFR] = 0x001c,
64 [RDLAR] = 0x0030,
65 [RDFAR] = 0x0034,
66 [RDFXR] = 0x0038,
67 [RDFFR] = 0x003c,
68 [TRSCER] = 0x0438,
69 [RMFCR] = 0x0440,
70 [TFTR] = 0x0448,
71 [FDR] = 0x0450,
72 [RMCR] = 0x0458,
73 [RPADIR] = 0x0460,
74 [FCFTR] = 0x0468,
75 [CSMR] = 0x04E4,
76
77 [ECMR] = 0x0500,
78 [ECSR] = 0x0510,
79 [ECSIPR] = 0x0518,
80 [PIR] = 0x0520,
81 [PSR] = 0x0528,
82 [PIPR] = 0x052c,
83 [RFLR] = 0x0508,
84 [APR] = 0x0554,
85 [MPR] = 0x0558,
86 [PFTCR] = 0x055c,
87 [PFRCR] = 0x0560,
88 [TPAUSER] = 0x0564,
89 [GECMR] = 0x05b0,
90 [BCULR] = 0x05b4,
91 [MAHR] = 0x05c0,
92 [MALR] = 0x05c8,
93 [TROCR] = 0x0700,
94 [CDCR] = 0x0708,
95 [LCCR] = 0x0710,
96 [CEFCR] = 0x0740,
97 [FRECR] = 0x0748,
98 [TSFRCR] = 0x0750,
99 [TLFRCR] = 0x0758,
100 [RFCR] = 0x0760,
101 [CERCR] = 0x0768,
102 [CEECR] = 0x0770,
103 [MAFCR] = 0x0778,
104 [RMII_MII] = 0x0790,
105
106 [ARSTR] = 0x0000,
107 [TSU_CTRST] = 0x0004,
108 [TSU_FWEN0] = 0x0010,
109 [TSU_FWEN1] = 0x0014,
110 [TSU_FCM] = 0x0018,
111 [TSU_BSYSL0] = 0x0020,
112 [TSU_BSYSL1] = 0x0024,
113 [TSU_PRISL0] = 0x0028,
114 [TSU_PRISL1] = 0x002c,
115 [TSU_FWSL0] = 0x0030,
116 [TSU_FWSL1] = 0x0034,
117 [TSU_FWSLC] = 0x0038,
118 [TSU_QTAG0] = 0x0040,
119 [TSU_QTAG1] = 0x0044,
120 [TSU_FWSR] = 0x0050,
121 [TSU_FWINMK] = 0x0054,
122 [TSU_ADQT0] = 0x0048,
123 [TSU_ADQT1] = 0x004c,
124 [TSU_VTAG0] = 0x0058,
125 [TSU_VTAG1] = 0x005c,
126 [TSU_ADSBSY] = 0x0060,
127 [TSU_TEN] = 0x0064,
128 [TSU_POST1] = 0x0070,
129 [TSU_POST2] = 0x0074,
130 [TSU_POST3] = 0x0078,
131 [TSU_POST4] = 0x007c,
132 [TSU_ADRH0] = 0x0100,
133 [TSU_ADRL0] = 0x0104,
134 [TSU_ADRH31] = 0x01f8,
135 [TSU_ADRL31] = 0x01fc,
136
137 [TXNLCR0] = 0x0080,
138 [TXALCR0] = 0x0084,
139 [RXNLCR0] = 0x0088,
140 [RXALCR0] = 0x008c,
141 [FWNLCR0] = 0x0090,
142 [FWALCR0] = 0x0094,
143 [TXNLCR1] = 0x00a0,
144 [TXALCR1] = 0x00a0,
145 [RXNLCR1] = 0x00a8,
146 [RXALCR1] = 0x00ac,
147 [FWNLCR1] = 0x00b0,
148 [FWALCR1] = 0x00b4,
149};
150
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000151static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
152 [ECMR] = 0x0300,
153 [RFLR] = 0x0308,
154 [ECSR] = 0x0310,
155 [ECSIPR] = 0x0318,
156 [PIR] = 0x0320,
157 [PSR] = 0x0328,
158 [RDMLR] = 0x0340,
159 [IPGR] = 0x0350,
160 [APR] = 0x0354,
161 [MPR] = 0x0358,
162 [RFCF] = 0x0360,
163 [TPAUSER] = 0x0364,
164 [TPAUSECR] = 0x0368,
165 [MAHR] = 0x03c0,
166 [MALR] = 0x03c8,
167 [TROCR] = 0x03d0,
168 [CDCR] = 0x03d4,
169 [LCCR] = 0x03d8,
170 [CNDCR] = 0x03dc,
171 [CEFCR] = 0x03e4,
172 [FRECR] = 0x03e8,
173 [TSFRCR] = 0x03ec,
174 [TLFRCR] = 0x03f0,
175 [RFCR] = 0x03f4,
176 [MAFCR] = 0x03f8,
177
178 [EDMR] = 0x0200,
179 [EDTRR] = 0x0208,
180 [EDRRR] = 0x0210,
181 [TDLAR] = 0x0218,
182 [RDLAR] = 0x0220,
183 [EESR] = 0x0228,
184 [EESIPR] = 0x0230,
185 [TRSCER] = 0x0238,
186 [RMFCR] = 0x0240,
187 [TFTR] = 0x0248,
188 [FDR] = 0x0250,
189 [RMCR] = 0x0258,
190 [TFUCR] = 0x0264,
191 [RFOCR] = 0x0268,
192 [FCFTR] = 0x0270,
193 [TRIMD] = 0x027c,
194};
195
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000196static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
197 [ECMR] = 0x0100,
198 [RFLR] = 0x0108,
199 [ECSR] = 0x0110,
200 [ECSIPR] = 0x0118,
201 [PIR] = 0x0120,
202 [PSR] = 0x0128,
203 [RDMLR] = 0x0140,
204 [IPGR] = 0x0150,
205 [APR] = 0x0154,
206 [MPR] = 0x0158,
207 [TPAUSER] = 0x0164,
208 [RFCF] = 0x0160,
209 [TPAUSECR] = 0x0168,
210 [BCFRR] = 0x016c,
211 [MAHR] = 0x01c0,
212 [MALR] = 0x01c8,
213 [TROCR] = 0x01d0,
214 [CDCR] = 0x01d4,
215 [LCCR] = 0x01d8,
216 [CNDCR] = 0x01dc,
217 [CEFCR] = 0x01e4,
218 [FRECR] = 0x01e8,
219 [TSFRCR] = 0x01ec,
220 [TLFRCR] = 0x01f0,
221 [RFCR] = 0x01f4,
222 [MAFCR] = 0x01f8,
223 [RTRATE] = 0x01fc,
224
225 [EDMR] = 0x0000,
226 [EDTRR] = 0x0008,
227 [EDRRR] = 0x0010,
228 [TDLAR] = 0x0018,
229 [RDLAR] = 0x0020,
230 [EESR] = 0x0028,
231 [EESIPR] = 0x0030,
232 [TRSCER] = 0x0038,
233 [RMFCR] = 0x0040,
234 [TFTR] = 0x0048,
235 [FDR] = 0x0050,
236 [RMCR] = 0x0058,
237 [TFUCR] = 0x0064,
238 [RFOCR] = 0x0068,
239 [FCFTR] = 0x0070,
240 [RPADIR] = 0x0078,
241 [TRIMD] = 0x007c,
242 [RBWAR] = 0x00c8,
243 [RDFAR] = 0x00cc,
244 [TBRAR] = 0x00d4,
245 [TDFAR] = 0x00d8,
246};
247
248static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
249 [ECMR] = 0x0160,
250 [ECSR] = 0x0164,
251 [ECSIPR] = 0x0168,
252 [PIR] = 0x016c,
253 [MAHR] = 0x0170,
254 [MALR] = 0x0174,
255 [RFLR] = 0x0178,
256 [PSR] = 0x017c,
257 [TROCR] = 0x0180,
258 [CDCR] = 0x0184,
259 [LCCR] = 0x0188,
260 [CNDCR] = 0x018c,
261 [CEFCR] = 0x0194,
262 [FRECR] = 0x0198,
263 [TSFRCR] = 0x019c,
264 [TLFRCR] = 0x01a0,
265 [RFCR] = 0x01a4,
266 [MAFCR] = 0x01a8,
267 [IPGR] = 0x01b4,
268 [APR] = 0x01b8,
269 [MPR] = 0x01bc,
270 [TPAUSER] = 0x01c4,
271 [BCFR] = 0x01cc,
272
273 [ARSTR] = 0x0000,
274 [TSU_CTRST] = 0x0004,
275 [TSU_FWEN0] = 0x0010,
276 [TSU_FWEN1] = 0x0014,
277 [TSU_FCM] = 0x0018,
278 [TSU_BSYSL0] = 0x0020,
279 [TSU_BSYSL1] = 0x0024,
280 [TSU_PRISL0] = 0x0028,
281 [TSU_PRISL1] = 0x002c,
282 [TSU_FWSL0] = 0x0030,
283 [TSU_FWSL1] = 0x0034,
284 [TSU_FWSLC] = 0x0038,
285 [TSU_QTAGM0] = 0x0040,
286 [TSU_QTAGM1] = 0x0044,
287 [TSU_ADQT0] = 0x0048,
288 [TSU_ADQT1] = 0x004c,
289 [TSU_FWSR] = 0x0050,
290 [TSU_FWINMK] = 0x0054,
291 [TSU_ADSBSY] = 0x0060,
292 [TSU_TEN] = 0x0064,
293 [TSU_POST1] = 0x0070,
294 [TSU_POST2] = 0x0074,
295 [TSU_POST3] = 0x0078,
296 [TSU_POST4] = 0x007c,
297
298 [TXNLCR0] = 0x0080,
299 [TXALCR0] = 0x0084,
300 [RXNLCR0] = 0x0088,
301 [RXALCR0] = 0x008c,
302 [FWNLCR0] = 0x0090,
303 [FWALCR0] = 0x0094,
304 [TXNLCR1] = 0x00a0,
305 [TXALCR1] = 0x00a0,
306 [RXNLCR1] = 0x00a8,
307 [RXALCR1] = 0x00ac,
308 [FWNLCR1] = 0x00b0,
309 [FWALCR1] = 0x00b4,
310
311 [TSU_ADRH0] = 0x0100,
312 [TSU_ADRL0] = 0x0104,
313 [TSU_ADRL31] = 0x01fc,
314};
315
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000316static int sh_eth_is_gether(struct sh_eth_private *mdp)
317{
318 if (mdp->reg_offset == sh_eth_offset_gigabit)
319 return 1;
320 else
321 return 0;
322}
323
Nobuhiro Iwamatsub7feacf2013-06-06 09:50:30 +0000324static void __maybe_unused sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000325{
326 u32 value = 0x0;
327 struct sh_eth_private *mdp = netdev_priv(ndev);
328
329 switch (mdp->phy_interface) {
330 case PHY_INTERFACE_MODE_GMII:
331 value = 0x2;
332 break;
333 case PHY_INTERFACE_MODE_MII:
334 value = 0x1;
335 break;
336 case PHY_INTERFACE_MODE_RMII:
337 value = 0x0;
338 break;
339 default:
340 pr_warn("PHY interface mode was not setup. Set to MII.\n");
341 value = 0x1;
342 break;
343 }
344
345 sh_eth_write(ndev, value, RMII_MII);
346}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000347
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000348static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000349{
350 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000351
352 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000353 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000354 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000355 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000356}
357
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000358/* There is CPU dependent code */
359#if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000360static void sh_eth_set_rate(struct net_device *ndev)
361{
362 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000363
364 switch (mdp->speed) {
365 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000366 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000367 break;
368 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000369 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
370 break;
371 default:
372 break;
373 }
374}
375
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000376/* R8A7778/9 */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000377static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
378 .set_duplex = sh_eth_set_duplex,
379 .set_rate = sh_eth_set_rate,
380
381 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
382 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
383 .eesipr_value = 0x01ff009f,
384
385 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
386 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
387 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
388 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
389
390 .apr = 1,
391 .mpr = 1,
392 .tpauser = 1,
393 .hw_swap = 1,
394};
395#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000396
397static void sh_eth_set_rate(struct net_device *ndev)
398{
399 struct sh_eth_private *mdp = netdev_priv(ndev);
400
401 switch (mdp->speed) {
402 case 10: /* 10BASE */
403 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
404 break;
405 case 100:/* 100BASE */
406 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000407 break;
408 default:
409 break;
410 }
411}
412
413/* SH7724 */
414static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
415 .set_duplex = sh_eth_set_duplex,
416 .set_rate = sh_eth_set_rate,
417
418 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
419 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
420 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
421
422 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
423 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
424 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
425 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
426
427 .apr = 1,
428 .mpr = 1,
429 .tpauser = 1,
430 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800431 .rpadir = 1,
432 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000433};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000434#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000435#define SH_ETH_HAS_BOTH_MODULES 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000436
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000437static void sh_eth_set_rate(struct net_device *ndev)
438{
439 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000440
441 switch (mdp->speed) {
442 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000443 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000444 break;
445 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000446 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000447 break;
448 default:
449 break;
450 }
451}
452
453/* SH7757 */
454static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
455 .set_duplex = sh_eth_set_duplex,
456 .set_rate = sh_eth_set_rate,
457
458 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
459 .rmcr_value = 0x00000001,
460
461 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
462 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
463 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
464 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
465
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000466 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000467 .apr = 1,
468 .mpr = 1,
469 .tpauser = 1,
470 .hw_swap = 1,
471 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000472 .rpadir = 1,
473 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000474};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000475
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000476#define SH_GIGA_ETH_BASE 0xfee00000
477#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
478#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
479static void sh_eth_chip_reset_giga(struct net_device *ndev)
480{
481 int i;
482 unsigned long mahr[2], malr[2];
483
484 /* save MAHR and MALR */
485 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000486 malr[i] = ioread32((void *)GIGA_MALR(i));
487 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000488 }
489
490 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000491 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000492 mdelay(1);
493
494 /* restore MAHR and MALR */
495 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000496 iowrite32(malr[i], (void *)GIGA_MALR(i));
497 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000498 }
499}
500
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000501static void sh_eth_set_rate_giga(struct net_device *ndev)
502{
503 struct sh_eth_private *mdp = netdev_priv(ndev);
504
505 switch (mdp->speed) {
506 case 10: /* 10BASE */
507 sh_eth_write(ndev, 0x00000000, GECMR);
508 break;
509 case 100:/* 100BASE */
510 sh_eth_write(ndev, 0x00000010, GECMR);
511 break;
512 case 1000: /* 1000BASE */
513 sh_eth_write(ndev, 0x00000020, GECMR);
514 break;
515 default:
516 break;
517 }
518}
519
520/* SH7757(GETHERC) */
521static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
522 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000523 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000524 .set_rate = sh_eth_set_rate_giga,
525
526 .ecsr_value = ECSR_ICD | ECSR_MPD,
527 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
528 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
529
530 .tx_check = EESR_TC1 | EESR_FTC,
531 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
532 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
533 EESR_ECI,
534 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
535 EESR_TFE,
536 .fdr_value = 0x0000072f,
537 .rmcr_value = 0x00000001,
538
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000539 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000540 .apr = 1,
541 .mpr = 1,
542 .tpauser = 1,
543 .bculr = 1,
544 .hw_swap = 1,
545 .rpadir = 1,
546 .rpadir_value = 2 << 16,
547 .no_trimd = 1,
548 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000549 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000550};
551
552static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
553{
554 if (sh_eth_is_gether(mdp))
555 return &sh_eth_my_cpu_data_giga;
556 else
557 return &sh_eth_my_cpu_data;
558}
559
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000560#elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000561
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000562static void sh_eth_chip_reset(struct net_device *ndev)
563{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000564 struct sh_eth_private *mdp = netdev_priv(ndev);
565
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000566 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000567 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000568 mdelay(1);
569}
570
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000571static void sh_eth_set_rate(struct net_device *ndev)
572{
573 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000574
575 switch (mdp->speed) {
576 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000577 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000578 break;
579 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000580 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000581 break;
582 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000583 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000584 break;
585 default:
586 break;
587 }
588}
589
590/* sh7763 */
591static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
592 .chip_reset = sh_eth_chip_reset,
593 .set_duplex = sh_eth_set_duplex,
594 .set_rate = sh_eth_set_rate,
595
596 .ecsr_value = ECSR_ICD | ECSR_MPD,
597 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
598 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
599
600 .tx_check = EESR_TC1 | EESR_FTC,
601 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
602 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
603 EESR_ECI,
604 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
605 EESR_TFE,
606
607 .apr = 1,
608 .mpr = 1,
609 .tpauser = 1,
610 .bculr = 1,
611 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000612 .no_trimd = 1,
613 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000614 .tsu = 1,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000615#if defined(CONFIG_CPU_SUBTYPE_SH7734)
616 .hw_crc = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000617 .select_mii = 1,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000618#else
619 .irq_flags = IRQF_SHARED,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000620#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000621};
622
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000623
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000624#elif defined(CONFIG_ARCH_R8A7740)
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000625
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000626static void sh_eth_chip_reset(struct net_device *ndev)
627{
628 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000629
630 /* reset device */
631 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
632 mdelay(1);
633
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000634 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000635}
636
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000637static void sh_eth_set_rate(struct net_device *ndev)
638{
639 struct sh_eth_private *mdp = netdev_priv(ndev);
640
641 switch (mdp->speed) {
642 case 10: /* 10BASE */
643 sh_eth_write(ndev, GECMR_10, GECMR);
644 break;
645 case 100:/* 100BASE */
646 sh_eth_write(ndev, GECMR_100, GECMR);
647 break;
648 case 1000: /* 1000BASE */
649 sh_eth_write(ndev, GECMR_1000, GECMR);
650 break;
651 default:
652 break;
653 }
654}
655
656/* R8A7740 */
657static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
658 .chip_reset = sh_eth_chip_reset,
659 .set_duplex = sh_eth_set_duplex,
660 .set_rate = sh_eth_set_rate,
661
662 .ecsr_value = ECSR_ICD | ECSR_MPD,
663 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
664 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
665
666 .tx_check = EESR_TC1 | EESR_FTC,
667 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
668 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
669 EESR_ECI,
670 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
671 EESR_TFE,
672
673 .apr = 1,
674 .mpr = 1,
675 .tpauser = 1,
676 .bculr = 1,
677 .hw_swap = 1,
678 .no_trimd = 1,
679 .no_ade = 1,
680 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000681 .select_mii = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000682};
683
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000684#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000685static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
686 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
687
688 .apr = 1,
689 .mpr = 1,
690 .tpauser = 1,
691 .hw_swap = 1,
692};
693#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000694static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
695 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000696 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000697};
698#endif
699
700static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
701{
702 if (!cd->ecsr_value)
703 cd->ecsr_value = DEFAULT_ECSR_INIT;
704
705 if (!cd->ecsipr_value)
706 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
707
708 if (!cd->fcftr_value)
709 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
710 DEFAULT_FIFO_F_D_RFD;
711
712 if (!cd->fdr_value)
713 cd->fdr_value = DEFAULT_FDR_INIT;
714
715 if (!cd->rmcr_value)
716 cd->rmcr_value = DEFAULT_RMCR_VALUE;
717
718 if (!cd->tx_check)
719 cd->tx_check = DEFAULT_TX_CHECK;
720
721 if (!cd->eesr_err_check)
722 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
723
724 if (!cd->tx_error_check)
725 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
726}
727
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000728static int sh_eth_check_reset(struct net_device *ndev)
729{
730 int ret = 0;
731 int cnt = 100;
732
733 while (cnt > 0) {
734 if (!(sh_eth_read(ndev, EDMR) & 0x3))
735 break;
736 mdelay(1);
737 cnt--;
738 }
739 if (cnt < 0) {
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +0000740 pr_err("Device reset fail\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000741 ret = -ETIMEDOUT;
742 }
743 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000744}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000745
746static int sh_eth_reset(struct net_device *ndev)
747{
748 struct sh_eth_private *mdp = netdev_priv(ndev);
749 int ret = 0;
750
751 if (sh_eth_is_gether(mdp)) {
752 sh_eth_write(ndev, EDSR_ENALL, EDSR);
753 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
754 EDMR);
755
756 ret = sh_eth_check_reset(ndev);
757 if (ret)
758 goto out;
759
760 /* Table Init */
761 sh_eth_write(ndev, 0x0, TDLAR);
762 sh_eth_write(ndev, 0x0, TDFAR);
763 sh_eth_write(ndev, 0x0, TDFXR);
764 sh_eth_write(ndev, 0x0, TDFFR);
765 sh_eth_write(ndev, 0x0, RDLAR);
766 sh_eth_write(ndev, 0x0, RDFAR);
767 sh_eth_write(ndev, 0x0, RDFXR);
768 sh_eth_write(ndev, 0x0, RDFFR);
769
770 /* Reset HW CRC register */
771 if (mdp->cd->hw_crc)
772 sh_eth_write(ndev, 0x0, CSMR);
773
774 /* Select MII mode */
775 if (mdp->cd->select_mii)
776 sh_eth_select_mii(ndev);
777 } else {
778 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
779 EDMR);
780 mdelay(3);
781 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
782 EDMR);
783 }
784
785out:
786 return ret;
787}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000788
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000789#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000790static void sh_eth_set_receive_align(struct sk_buff *skb)
791{
792 int reserve;
793
794 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
795 if (reserve)
796 skb_reserve(skb, reserve);
797}
798#else
799static void sh_eth_set_receive_align(struct sk_buff *skb)
800{
801 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
802}
803#endif
804
805
Yoshinori Sato71557a32008-08-06 19:49:00 -0400806/* CPU <-> EDMAC endian convert */
807static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
808{
809 switch (mdp->edmac_endian) {
810 case EDMAC_LITTLE_ENDIAN:
811 return cpu_to_le32(x);
812 case EDMAC_BIG_ENDIAN:
813 return cpu_to_be32(x);
814 }
815 return x;
816}
817
818static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
819{
820 switch (mdp->edmac_endian) {
821 case EDMAC_LITTLE_ENDIAN:
822 return le32_to_cpu(x);
823 case EDMAC_BIG_ENDIAN:
824 return be32_to_cpu(x);
825 }
826 return x;
827}
828
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700829/*
830 * Program the hardware MAC address from dev->dev_addr.
831 */
832static void update_mac_address(struct net_device *ndev)
833{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000834 sh_eth_write(ndev,
835 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
836 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
837 sh_eth_write(ndev,
838 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700839}
840
841/*
842 * Get MAC address from SuperH MAC address register
843 *
844 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
845 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
846 * When you want use this device, you must set MAC address in bootloader.
847 *
848 */
Magnus Damm748031f2009-10-09 00:17:14 +0000849static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700850{
Magnus Damm748031f2009-10-09 00:17:14 +0000851 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
852 memcpy(ndev->dev_addr, mac, 6);
853 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000854 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
855 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
856 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
857 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
858 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
859 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000860 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700861}
862
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000863static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
864{
865 if (sh_eth_is_gether(mdp))
866 return EDTRR_TRNS_GETHER;
867 else
868 return EDTRR_TRNS_ETHER;
869}
870
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700871struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000872 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700873 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000874 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700875 u32 mmd_msk;/* MMD */
876 u32 mdo_msk;
877 u32 mdi_msk;
878 u32 mdc_msk;
879};
880
881/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000882static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700883{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000884 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700885}
886
887/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000888static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700889{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000890 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700891}
892
893/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000894static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700895{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000896 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700897}
898
899/* Data I/O pin control */
900static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
901{
902 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000903
904 if (bitbang->set_gate)
905 bitbang->set_gate(bitbang->addr);
906
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700907 if (bit)
908 bb_set(bitbang->addr, bitbang->mmd_msk);
909 else
910 bb_clr(bitbang->addr, bitbang->mmd_msk);
911}
912
913/* Set bit data*/
914static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
915{
916 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
917
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000918 if (bitbang->set_gate)
919 bitbang->set_gate(bitbang->addr);
920
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700921 if (bit)
922 bb_set(bitbang->addr, bitbang->mdo_msk);
923 else
924 bb_clr(bitbang->addr, bitbang->mdo_msk);
925}
926
927/* Get bit data*/
928static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
929{
930 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000931
932 if (bitbang->set_gate)
933 bitbang->set_gate(bitbang->addr);
934
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700935 return bb_read(bitbang->addr, bitbang->mdi_msk);
936}
937
938/* MDC pin control */
939static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
940{
941 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
942
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000943 if (bitbang->set_gate)
944 bitbang->set_gate(bitbang->addr);
945
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700946 if (bit)
947 bb_set(bitbang->addr, bitbang->mdc_msk);
948 else
949 bb_clr(bitbang->addr, bitbang->mdc_msk);
950}
951
952/* mdio bus control struct */
953static struct mdiobb_ops bb_ops = {
954 .owner = THIS_MODULE,
955 .set_mdc = sh_mdc_ctrl,
956 .set_mdio_dir = sh_mmd_ctrl,
957 .set_mdio_data = sh_set_mdio,
958 .get_mdio_data = sh_get_mdio,
959};
960
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700961/* free skb and descriptor buffer */
962static void sh_eth_ring_free(struct net_device *ndev)
963{
964 struct sh_eth_private *mdp = netdev_priv(ndev);
965 int i;
966
967 /* Free Rx skb ringbuffer */
968 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000969 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700970 if (mdp->rx_skbuff[i])
971 dev_kfree_skb(mdp->rx_skbuff[i]);
972 }
973 }
974 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000975 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700976
977 /* Free Tx skb ringbuffer */
978 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000979 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700980 if (mdp->tx_skbuff[i])
981 dev_kfree_skb(mdp->tx_skbuff[i]);
982 }
983 }
984 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000985 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700986}
987
988/* format skb and descriptor buffer */
989static void sh_eth_ring_format(struct net_device *ndev)
990{
991 struct sh_eth_private *mdp = netdev_priv(ndev);
992 int i;
993 struct sk_buff *skb;
994 struct sh_eth_rxdesc *rxdesc = NULL;
995 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000996 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
997 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700998
999 mdp->cur_rx = mdp->cur_tx = 0;
1000 mdp->dirty_rx = mdp->dirty_tx = 0;
1001
1002 memset(mdp->rx_ring, 0, rx_ringsize);
1003
1004 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001005 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001006 /* skb */
1007 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001008 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001009 mdp->rx_skbuff[i] = skb;
1010 if (skb == NULL)
1011 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001012 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001013 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001014 sh_eth_set_receive_align(skb);
1015
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016 /* RX descriptor */
1017 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001018 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -04001019 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020
1021 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001022 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001023 /* Rx descriptor address set */
1024 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001025 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001026 if (sh_eth_is_gether(mdp))
1027 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001028 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029 }
1030
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001031 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001032
1033 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001034 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035
1036 memset(mdp->tx_ring, 0, tx_ringsize);
1037
1038 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001039 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001040 mdp->tx_skbuff[i] = NULL;
1041 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001042 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001043 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001044 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001045 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001046 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001047 if (sh_eth_is_gether(mdp))
1048 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001049 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001050 }
1051
Yoshinori Sato71557a32008-08-06 19:49:00 -04001052 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001053}
1054
1055/* Get skb and descriptor buffer */
1056static int sh_eth_ring_init(struct net_device *ndev)
1057{
1058 struct sh_eth_private *mdp = netdev_priv(ndev);
1059 int rx_ringsize, tx_ringsize, ret = 0;
1060
1061 /*
1062 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1063 * card needs room to do 8 byte alignment, +2 so we can reserve
1064 * the first 2 bytes, and +16 gets room for the status word from the
1065 * card.
1066 */
1067 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1068 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001069 if (mdp->cd->rpadir)
1070 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001071
1072 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001073 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1074 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001075 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001076 ret = -ENOMEM;
1077 return ret;
1078 }
1079
Joe Perchesb2adaca2013-02-03 17:43:58 +00001080 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1081 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001082 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001083 ret = -ENOMEM;
1084 goto skb_ring_free;
1085 }
1086
1087 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001088 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001089 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001090 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001091 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001092 ret = -ENOMEM;
1093 goto desc_ring_free;
1094 }
1095
1096 mdp->dirty_rx = 0;
1097
1098 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001099 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001100 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001101 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001102 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001103 ret = -ENOMEM;
1104 goto desc_ring_free;
1105 }
1106 return ret;
1107
1108desc_ring_free:
1109 /* free DMA buffer */
1110 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1111
1112skb_ring_free:
1113 /* Free Rx and Tx skb ring buffer */
1114 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001115 mdp->tx_ring = NULL;
1116 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001117
1118 return ret;
1119}
1120
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001121static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1122{
1123 int ringsize;
1124
1125 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001126 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001127 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1128 mdp->rx_desc_dma);
1129 mdp->rx_ring = NULL;
1130 }
1131
1132 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001133 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001134 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1135 mdp->tx_desc_dma);
1136 mdp->tx_ring = NULL;
1137 }
1138}
1139
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001140static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001141{
1142 int ret = 0;
1143 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144 u32 val;
1145
1146 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001147 ret = sh_eth_reset(ndev);
1148 if (ret)
1149 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001150
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001151 /* Descriptor format */
1152 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001153 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001154 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155
1156 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001157 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001158
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001159#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001160 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001161 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001162 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001163#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001164 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001165
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001166 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001167 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1168 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001169
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001170 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001171 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001172
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001173 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001174
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001175 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001176 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001177
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001178 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001179
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001180 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001181 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001183 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001184 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1185 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001186
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001187 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001188 if (start)
1189 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001190
1191 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001192 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001193 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1194
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001195 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001196
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001197 if (mdp->cd->set_rate)
1198 mdp->cd->set_rate(ndev);
1199
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001200 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001201 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001202
1203 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001204 if (start)
1205 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001206
1207 /* Set MAC address */
1208 update_mac_address(ndev);
1209
1210 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001211 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001212 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001213 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001214 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001215 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001216 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001217
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001218 if (start) {
1219 /* Setting the Rx mode will start the Rx process. */
1220 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001221
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001222 netif_start_queue(ndev);
1223 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001224
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001225out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001226 return ret;
1227}
1228
1229/* free Tx skb function */
1230static int sh_eth_txfree(struct net_device *ndev)
1231{
1232 struct sh_eth_private *mdp = netdev_priv(ndev);
1233 struct sh_eth_txdesc *txdesc;
1234 int freeNum = 0;
1235 int entry = 0;
1236
1237 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001238 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001239 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001240 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001241 break;
1242 /* Free the original skb. */
1243 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001244 dma_unmap_single(&ndev->dev, txdesc->addr,
1245 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001246 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1247 mdp->tx_skbuff[entry] = NULL;
1248 freeNum++;
1249 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001250 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001251 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001252 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001253
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001254 ndev->stats.tx_packets++;
1255 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001256 }
1257 return freeNum;
1258}
1259
1260/* Packet receive function */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001261static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001262{
1263 struct sh_eth_private *mdp = netdev_priv(ndev);
1264 struct sh_eth_rxdesc *rxdesc;
1265
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001266 int entry = mdp->cur_rx % mdp->num_rx_ring;
1267 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001268 struct sk_buff *skb;
1269 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001270 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001271
1272 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001273 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1274 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001275 pkt_len = rxdesc->frame_length;
1276
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +00001277#if defined(CONFIG_ARCH_R8A7740)
1278 desc_status >>= 16;
1279#endif
1280
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001281 if (--boguscnt < 0)
1282 break;
1283
1284 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001285 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001286
1287 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1288 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001289 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001290 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001291 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001292 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001293 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001294 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001295 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001296 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001297 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001298 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001299 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001301 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001302 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001303 if (!mdp->cd->hw_swap)
1304 sh_eth_soft_swap(
1305 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1306 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001307 skb = mdp->rx_skbuff[entry];
1308 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001309 if (mdp->cd->rpadir)
1310 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001311 skb_put(skb, pkt_len);
1312 skb->protocol = eth_type_trans(skb, ndev);
1313 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001314 ndev->stats.rx_packets++;
1315 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001316 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001317 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001318 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001319 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001320 }
1321
1322 /* Refill the Rx ring buffers. */
1323 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001324 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001325 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001326 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001327 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001328
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001329 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001330 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001331 mdp->rx_skbuff[entry] = skb;
1332 if (skb == NULL)
1333 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001334 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001335 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001336 sh_eth_set_receive_align(skb);
1337
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001338 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001339 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001340 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001341 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001342 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001343 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001344 else
1345 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001346 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001347 }
1348
1349 /* Restart Rx engine if stopped. */
1350 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001351 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001352 /* fix the values for the next receiving if RDE is set */
1353 if (intr_status & EESR_RDE)
1354 mdp->cur_rx = mdp->dirty_rx =
1355 (sh_eth_read(ndev, RDFAR) -
1356 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001357 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001358 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001359
1360 return 0;
1361}
1362
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001363static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001364{
1365 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001366 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1367 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001368}
1369
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001370static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001371{
1372 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001373 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1374 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001375}
1376
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377/* error control function */
1378static void sh_eth_error(struct net_device *ndev, int intr_status)
1379{
1380 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001382 u32 link_stat;
1383 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001384
1385 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001386 felic_stat = sh_eth_read(ndev, ECSR);
1387 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001388 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001389 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390 if (felic_stat & ECSR_LCHNG) {
1391 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001392 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001393 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001394 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001395 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001396 if (mdp->ether_link_active_low)
1397 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001398 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001399 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001400 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001401 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001402 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001403 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1404 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001406 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1407 ECSR);
1408 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1409 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001410 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001411 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412 }
1413 }
1414 }
1415
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001416ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001417 if (intr_status & EESR_TWB) {
1418 /* Write buck end. unused write back interrupt */
1419 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001420 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001421 if (netif_msg_tx_err(mdp))
1422 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001423 }
1424
1425 if (intr_status & EESR_RABT) {
1426 /* Receive Abort int */
1427 if (intr_status & EESR_RFRMER) {
1428 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001429 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001430 if (netif_msg_rx_err(mdp))
1431 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001432 }
1433 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001434
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001435 if (intr_status & EESR_TDE) {
1436 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001437 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001438 if (netif_msg_tx_err(mdp))
1439 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1440 }
1441
1442 if (intr_status & EESR_TFE) {
1443 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001444 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001445 if (netif_msg_tx_err(mdp))
1446 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447 }
1448
1449 if (intr_status & EESR_RDE) {
1450 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001451 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001452
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001453 if (netif_msg_rx_err(mdp))
1454 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001456
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001457 if (intr_status & EESR_RFE) {
1458 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001459 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001460 if (netif_msg_rx_err(mdp))
1461 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1462 }
1463
1464 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1465 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001466 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001467 if (netif_msg_tx_err(mdp))
1468 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001469 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001470
1471 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1472 if (mdp->cd->no_ade)
1473 mask &= ~EESR_ADE;
1474 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001475 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001476 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001478 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1479 intr_status, mdp->cur_tx);
1480 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001481 mdp->dirty_tx, (u32) ndev->state, edtrr);
1482 /* dirty buffer free */
1483 sh_eth_txfree(ndev);
1484
1485 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001486 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001487 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001488 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001489 }
1490 /* wakeup */
1491 netif_wake_queue(ndev);
1492 }
1493}
1494
1495static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1496{
1497 struct net_device *ndev = netdev;
1498 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001499 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001500 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001501 unsigned long intr_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001502
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001503 spin_lock(&mdp->lock);
1504
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001505 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001506 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001507 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1508 * enabled since it's the one that comes thru regardless of the mask,
1509 * and we need to fully handle it in sh_eth_error() in order to quench
1510 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1511 */
1512 intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001513 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001514 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1515 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001516 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001517 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001518 ret = IRQ_HANDLED;
1519 } else
1520 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001521
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001522 if (intr_status & (EESR_FRC | /* Frame recv*/
1523 EESR_RMAF | /* Multi cast address recv*/
1524 EESR_RRF | /* Bit frame recv */
1525 EESR_RTLF | /* Long frame recv*/
1526 EESR_RTSF | /* short frame recv */
1527 EESR_PRE | /* PHY-LSI recv error */
1528 EESR_CERF)){ /* recv frame CRC error */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001529 sh_eth_rx(ndev, intr_status);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001530 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001532 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001533 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001534 sh_eth_txfree(ndev);
1535 netif_wake_queue(ndev);
1536 }
1537
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001538 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001539 sh_eth_error(ndev, intr_status);
1540
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001541other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001542 spin_unlock(&mdp->lock);
1543
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001544 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001545}
1546
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001547/* PHY state control function */
1548static void sh_eth_adjust_link(struct net_device *ndev)
1549{
1550 struct sh_eth_private *mdp = netdev_priv(ndev);
1551 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001552 int new_state = 0;
1553
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001554 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001555 if (phydev->duplex != mdp->duplex) {
1556 new_state = 1;
1557 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001558 if (mdp->cd->set_duplex)
1559 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001560 }
1561
1562 if (phydev->speed != mdp->speed) {
1563 new_state = 1;
1564 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001565 if (mdp->cd->set_rate)
1566 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001567 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001568 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001569 sh_eth_write(ndev,
1570 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001571 new_state = 1;
1572 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001573 if (mdp->cd->no_psr || mdp->no_ether_link)
1574 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575 }
1576 } else if (mdp->link) {
1577 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001578 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 mdp->speed = 0;
1580 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001581 if (mdp->cd->no_psr || mdp->no_ether_link)
1582 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001583 }
1584
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001585 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001586 phy_print_status(phydev);
1587}
1588
1589/* PHY init function */
1590static int sh_eth_phy_init(struct net_device *ndev)
1591{
1592 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001593 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001594 struct phy_device *phydev = NULL;
1595
Kay Sieversfb28ad352008-11-10 13:55:14 -08001596 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001597 mdp->mii_bus->id , mdp->phy_id);
1598
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001599 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001600 mdp->speed = 0;
1601 mdp->duplex = -1;
1602
1603 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001604 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001605 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001606 if (IS_ERR(phydev)) {
1607 dev_err(&ndev->dev, "phy_connect failed\n");
1608 return PTR_ERR(phydev);
1609 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001610
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001611 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001612 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001613
1614 mdp->phydev = phydev;
1615
1616 return 0;
1617}
1618
1619/* PHY control start function */
1620static int sh_eth_phy_start(struct net_device *ndev)
1621{
1622 struct sh_eth_private *mdp = netdev_priv(ndev);
1623 int ret;
1624
1625 ret = sh_eth_phy_init(ndev);
1626 if (ret)
1627 return ret;
1628
1629 /* reset phy - this also wakes it from PDOWN */
1630 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1631 phy_start(mdp->phydev);
1632
1633 return 0;
1634}
1635
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001636static int sh_eth_get_settings(struct net_device *ndev,
1637 struct ethtool_cmd *ecmd)
1638{
1639 struct sh_eth_private *mdp = netdev_priv(ndev);
1640 unsigned long flags;
1641 int ret;
1642
1643 spin_lock_irqsave(&mdp->lock, flags);
1644 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1645 spin_unlock_irqrestore(&mdp->lock, flags);
1646
1647 return ret;
1648}
1649
1650static int sh_eth_set_settings(struct net_device *ndev,
1651 struct ethtool_cmd *ecmd)
1652{
1653 struct sh_eth_private *mdp = netdev_priv(ndev);
1654 unsigned long flags;
1655 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001656
1657 spin_lock_irqsave(&mdp->lock, flags);
1658
1659 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001660 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001661
1662 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1663 if (ret)
1664 goto error_exit;
1665
1666 if (ecmd->duplex == DUPLEX_FULL)
1667 mdp->duplex = 1;
1668 else
1669 mdp->duplex = 0;
1670
1671 if (mdp->cd->set_duplex)
1672 mdp->cd->set_duplex(ndev);
1673
1674error_exit:
1675 mdelay(1);
1676
1677 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001678 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001679
1680 spin_unlock_irqrestore(&mdp->lock, flags);
1681
1682 return ret;
1683}
1684
1685static int sh_eth_nway_reset(struct net_device *ndev)
1686{
1687 struct sh_eth_private *mdp = netdev_priv(ndev);
1688 unsigned long flags;
1689 int ret;
1690
1691 spin_lock_irqsave(&mdp->lock, flags);
1692 ret = phy_start_aneg(mdp->phydev);
1693 spin_unlock_irqrestore(&mdp->lock, flags);
1694
1695 return ret;
1696}
1697
1698static u32 sh_eth_get_msglevel(struct net_device *ndev)
1699{
1700 struct sh_eth_private *mdp = netdev_priv(ndev);
1701 return mdp->msg_enable;
1702}
1703
1704static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1705{
1706 struct sh_eth_private *mdp = netdev_priv(ndev);
1707 mdp->msg_enable = value;
1708}
1709
1710static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1711 "rx_current", "tx_current",
1712 "rx_dirty", "tx_dirty",
1713};
1714#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1715
1716static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1717{
1718 switch (sset) {
1719 case ETH_SS_STATS:
1720 return SH_ETH_STATS_LEN;
1721 default:
1722 return -EOPNOTSUPP;
1723 }
1724}
1725
1726static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1727 struct ethtool_stats *stats, u64 *data)
1728{
1729 struct sh_eth_private *mdp = netdev_priv(ndev);
1730 int i = 0;
1731
1732 /* device-specific stats */
1733 data[i++] = mdp->cur_rx;
1734 data[i++] = mdp->cur_tx;
1735 data[i++] = mdp->dirty_rx;
1736 data[i++] = mdp->dirty_tx;
1737}
1738
1739static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1740{
1741 switch (stringset) {
1742 case ETH_SS_STATS:
1743 memcpy(data, *sh_eth_gstrings_stats,
1744 sizeof(sh_eth_gstrings_stats));
1745 break;
1746 }
1747}
1748
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001749static void sh_eth_get_ringparam(struct net_device *ndev,
1750 struct ethtool_ringparam *ring)
1751{
1752 struct sh_eth_private *mdp = netdev_priv(ndev);
1753
1754 ring->rx_max_pending = RX_RING_MAX;
1755 ring->tx_max_pending = TX_RING_MAX;
1756 ring->rx_pending = mdp->num_rx_ring;
1757 ring->tx_pending = mdp->num_tx_ring;
1758}
1759
1760static int sh_eth_set_ringparam(struct net_device *ndev,
1761 struct ethtool_ringparam *ring)
1762{
1763 struct sh_eth_private *mdp = netdev_priv(ndev);
1764 int ret;
1765
1766 if (ring->tx_pending > TX_RING_MAX ||
1767 ring->rx_pending > RX_RING_MAX ||
1768 ring->tx_pending < TX_RING_MIN ||
1769 ring->rx_pending < RX_RING_MIN)
1770 return -EINVAL;
1771 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1772 return -EINVAL;
1773
1774 if (netif_running(ndev)) {
1775 netif_tx_disable(ndev);
1776 /* Disable interrupts by clearing the interrupt mask. */
1777 sh_eth_write(ndev, 0x0000, EESIPR);
1778 /* Stop the chip's Tx and Rx processes. */
1779 sh_eth_write(ndev, 0, EDTRR);
1780 sh_eth_write(ndev, 0, EDRRR);
1781 synchronize_irq(ndev->irq);
1782 }
1783
1784 /* Free all the skbuffs in the Rx queue. */
1785 sh_eth_ring_free(ndev);
1786 /* Free DMA buffer */
1787 sh_eth_free_dma_buffer(mdp);
1788
1789 /* Set new parameters */
1790 mdp->num_rx_ring = ring->rx_pending;
1791 mdp->num_tx_ring = ring->tx_pending;
1792
1793 ret = sh_eth_ring_init(ndev);
1794 if (ret < 0) {
1795 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1796 return ret;
1797 }
1798 ret = sh_eth_dev_init(ndev, false);
1799 if (ret < 0) {
1800 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1801 return ret;
1802 }
1803
1804 if (netif_running(ndev)) {
1805 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1806 /* Setting the Rx mode will start the Rx process. */
1807 sh_eth_write(ndev, EDRRR_R, EDRRR);
1808 netif_wake_queue(ndev);
1809 }
1810
1811 return 0;
1812}
1813
stephen hemminger9b07be42012-01-04 12:59:49 +00001814static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001815 .get_settings = sh_eth_get_settings,
1816 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001817 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001818 .get_msglevel = sh_eth_get_msglevel,
1819 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001820 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001821 .get_strings = sh_eth_get_strings,
1822 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1823 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001824 .get_ringparam = sh_eth_get_ringparam,
1825 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001826};
1827
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001828/* network device open function */
1829static int sh_eth_open(struct net_device *ndev)
1830{
1831 int ret = 0;
1832 struct sh_eth_private *mdp = netdev_priv(ndev);
1833
Magnus Dammbcd51492009-10-09 00:20:04 +00001834 pm_runtime_get_sync(&mdp->pdev->dev);
1835
Joe Perchesa0607fd2009-11-18 23:29:17 -08001836 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001837 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001838 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001839 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001840 return ret;
1841 }
1842
1843 /* Descriptor set */
1844 ret = sh_eth_ring_init(ndev);
1845 if (ret)
1846 goto out_free_irq;
1847
1848 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001849 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001850 if (ret)
1851 goto out_free_irq;
1852
1853 /* PHY control start*/
1854 ret = sh_eth_phy_start(ndev);
1855 if (ret)
1856 goto out_free_irq;
1857
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001858 return ret;
1859
1860out_free_irq:
1861 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001862 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001863 return ret;
1864}
1865
1866/* Timeout function */
1867static void sh_eth_tx_timeout(struct net_device *ndev)
1868{
1869 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001870 struct sh_eth_rxdesc *rxdesc;
1871 int i;
1872
1873 netif_stop_queue(ndev);
1874
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001875 if (netif_msg_timer(mdp))
1876 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001877 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001878
1879 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001880 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001881
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001882 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001883 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001884 rxdesc = &mdp->rx_ring[i];
1885 rxdesc->status = 0;
1886 rxdesc->addr = 0xBADF00D0;
1887 if (mdp->rx_skbuff[i])
1888 dev_kfree_skb(mdp->rx_skbuff[i]);
1889 mdp->rx_skbuff[i] = NULL;
1890 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001891 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001892 if (mdp->tx_skbuff[i])
1893 dev_kfree_skb(mdp->tx_skbuff[i]);
1894 mdp->tx_skbuff[i] = NULL;
1895 }
1896
1897 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001898 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001899}
1900
1901/* Packet transmit function */
1902static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1903{
1904 struct sh_eth_private *mdp = netdev_priv(ndev);
1905 struct sh_eth_txdesc *txdesc;
1906 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001907 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001908
1909 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001910 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001911 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001912 if (netif_msg_tx_queued(mdp))
1913 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001914 netif_stop_queue(ndev);
1915 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001916 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001917 }
1918 }
1919 spin_unlock_irqrestore(&mdp->lock, flags);
1920
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001921 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001922 mdp->tx_skbuff[entry] = skb;
1923 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001924 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001925 if (!mdp->cd->hw_swap)
1926 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1927 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001928 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1929 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001930 if (skb->len < ETHERSMALL)
1931 txdesc->buffer_length = ETHERSMALL;
1932 else
1933 txdesc->buffer_length = skb->len;
1934
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001935 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001936 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001937 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001938 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001939
1940 mdp->cur_tx++;
1941
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001942 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1943 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001944
Patrick McHardy6ed10652009-06-23 06:03:08 +00001945 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001946}
1947
1948/* device close function */
1949static int sh_eth_close(struct net_device *ndev)
1950{
1951 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001952
1953 netif_stop_queue(ndev);
1954
1955 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001956 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001957
1958 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001959 sh_eth_write(ndev, 0, EDTRR);
1960 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001961
1962 /* PHY Disconnect */
1963 if (mdp->phydev) {
1964 phy_stop(mdp->phydev);
1965 phy_disconnect(mdp->phydev);
1966 }
1967
1968 free_irq(ndev->irq, ndev);
1969
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001970 /* Free all the skbuffs in the Rx queue. */
1971 sh_eth_ring_free(ndev);
1972
1973 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001974 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001975
Magnus Dammbcd51492009-10-09 00:20:04 +00001976 pm_runtime_put_sync(&mdp->pdev->dev);
1977
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001978 return 0;
1979}
1980
1981static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1982{
1983 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001984
Magnus Dammbcd51492009-10-09 00:20:04 +00001985 pm_runtime_get_sync(&mdp->pdev->dev);
1986
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001987 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001988 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001989 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001990 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001991 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001992 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001993 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001994 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001995 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001996 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001997 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1998 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001999 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002000 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2001 }
Magnus Dammbcd51492009-10-09 00:20:04 +00002002 pm_runtime_put_sync(&mdp->pdev->dev);
2003
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002004 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002005}
2006
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002007/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002008static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
2009 int cmd)
2010{
2011 struct sh_eth_private *mdp = netdev_priv(ndev);
2012 struct phy_device *phydev = mdp->phydev;
2013
2014 if (!netif_running(ndev))
2015 return -EINVAL;
2016
2017 if (!phydev)
2018 return -ENODEV;
2019
Richard Cochran28b04112010-07-17 08:48:55 +00002020 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002021}
2022
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002023/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2024static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2025 int entry)
2026{
2027 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2028}
2029
2030static u32 sh_eth_tsu_get_post_mask(int entry)
2031{
2032 return 0x0f << (28 - ((entry % 8) * 4));
2033}
2034
2035static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2036{
2037 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2038}
2039
2040static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2041 int entry)
2042{
2043 struct sh_eth_private *mdp = netdev_priv(ndev);
2044 u32 tmp;
2045 void *reg_offset;
2046
2047 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2048 tmp = ioread32(reg_offset);
2049 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2050}
2051
2052static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2053 int entry)
2054{
2055 struct sh_eth_private *mdp = netdev_priv(ndev);
2056 u32 post_mask, ref_mask, tmp;
2057 void *reg_offset;
2058
2059 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2060 post_mask = sh_eth_tsu_get_post_mask(entry);
2061 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2062
2063 tmp = ioread32(reg_offset);
2064 iowrite32(tmp & ~post_mask, reg_offset);
2065
2066 /* If other port enables, the function returns "true" */
2067 return tmp & ref_mask;
2068}
2069
2070static int sh_eth_tsu_busy(struct net_device *ndev)
2071{
2072 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2073 struct sh_eth_private *mdp = netdev_priv(ndev);
2074
2075 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2076 udelay(10);
2077 timeout--;
2078 if (timeout <= 0) {
2079 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2080 return -ETIMEDOUT;
2081 }
2082 }
2083
2084 return 0;
2085}
2086
2087static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2088 const u8 *addr)
2089{
2090 u32 val;
2091
2092 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2093 iowrite32(val, reg);
2094 if (sh_eth_tsu_busy(ndev) < 0)
2095 return -EBUSY;
2096
2097 val = addr[4] << 8 | addr[5];
2098 iowrite32(val, reg + 4);
2099 if (sh_eth_tsu_busy(ndev) < 0)
2100 return -EBUSY;
2101
2102 return 0;
2103}
2104
2105static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2106{
2107 u32 val;
2108
2109 val = ioread32(reg);
2110 addr[0] = (val >> 24) & 0xff;
2111 addr[1] = (val >> 16) & 0xff;
2112 addr[2] = (val >> 8) & 0xff;
2113 addr[3] = val & 0xff;
2114 val = ioread32(reg + 4);
2115 addr[4] = (val >> 8) & 0xff;
2116 addr[5] = val & 0xff;
2117}
2118
2119
2120static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2121{
2122 struct sh_eth_private *mdp = netdev_priv(ndev);
2123 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2124 int i;
2125 u8 c_addr[ETH_ALEN];
2126
2127 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2128 sh_eth_tsu_read_entry(reg_offset, c_addr);
2129 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2130 return i;
2131 }
2132
2133 return -ENOENT;
2134}
2135
2136static int sh_eth_tsu_find_empty(struct net_device *ndev)
2137{
2138 u8 blank[ETH_ALEN];
2139 int entry;
2140
2141 memset(blank, 0, sizeof(blank));
2142 entry = sh_eth_tsu_find_entry(ndev, blank);
2143 return (entry < 0) ? -ENOMEM : entry;
2144}
2145
2146static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2147 int entry)
2148{
2149 struct sh_eth_private *mdp = netdev_priv(ndev);
2150 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2151 int ret;
2152 u8 blank[ETH_ALEN];
2153
2154 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2155 ~(1 << (31 - entry)), TSU_TEN);
2156
2157 memset(blank, 0, sizeof(blank));
2158 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2159 if (ret < 0)
2160 return ret;
2161 return 0;
2162}
2163
2164static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2165{
2166 struct sh_eth_private *mdp = netdev_priv(ndev);
2167 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2168 int i, ret;
2169
2170 if (!mdp->cd->tsu)
2171 return 0;
2172
2173 i = sh_eth_tsu_find_entry(ndev, addr);
2174 if (i < 0) {
2175 /* No entry found, create one */
2176 i = sh_eth_tsu_find_empty(ndev);
2177 if (i < 0)
2178 return -ENOMEM;
2179 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2180 if (ret < 0)
2181 return ret;
2182
2183 /* Enable the entry */
2184 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2185 (1 << (31 - i)), TSU_TEN);
2186 }
2187
2188 /* Entry found or created, enable POST */
2189 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2190
2191 return 0;
2192}
2193
2194static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2195{
2196 struct sh_eth_private *mdp = netdev_priv(ndev);
2197 int i, ret;
2198
2199 if (!mdp->cd->tsu)
2200 return 0;
2201
2202 i = sh_eth_tsu_find_entry(ndev, addr);
2203 if (i) {
2204 /* Entry found */
2205 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2206 goto done;
2207
2208 /* Disable the entry if both ports was disabled */
2209 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2210 if (ret < 0)
2211 return ret;
2212 }
2213done:
2214 return 0;
2215}
2216
2217static int sh_eth_tsu_purge_all(struct net_device *ndev)
2218{
2219 struct sh_eth_private *mdp = netdev_priv(ndev);
2220 int i, ret;
2221
2222 if (unlikely(!mdp->cd->tsu))
2223 return 0;
2224
2225 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2226 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2227 continue;
2228
2229 /* Disable the entry if both ports was disabled */
2230 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2231 if (ret < 0)
2232 return ret;
2233 }
2234
2235 return 0;
2236}
2237
2238static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2239{
2240 struct sh_eth_private *mdp = netdev_priv(ndev);
2241 u8 addr[ETH_ALEN];
2242 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2243 int i;
2244
2245 if (unlikely(!mdp->cd->tsu))
2246 return;
2247
2248 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2249 sh_eth_tsu_read_entry(reg_offset, addr);
2250 if (is_multicast_ether_addr(addr))
2251 sh_eth_tsu_del_entry(ndev, addr);
2252 }
2253}
2254
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002255/* Multicast reception directions set */
2256static void sh_eth_set_multicast_list(struct net_device *ndev)
2257{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002258 struct sh_eth_private *mdp = netdev_priv(ndev);
2259 u32 ecmr_bits;
2260 int mcast_all = 0;
2261 unsigned long flags;
2262
2263 spin_lock_irqsave(&mdp->lock, flags);
2264 /*
2265 * Initial condition is MCT = 1, PRM = 0.
2266 * Depending on ndev->flags, set PRM or clear MCT
2267 */
2268 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2269
2270 if (!(ndev->flags & IFF_MULTICAST)) {
2271 sh_eth_tsu_purge_mcast(ndev);
2272 mcast_all = 1;
2273 }
2274 if (ndev->flags & IFF_ALLMULTI) {
2275 sh_eth_tsu_purge_mcast(ndev);
2276 ecmr_bits &= ~ECMR_MCT;
2277 mcast_all = 1;
2278 }
2279
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002280 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002281 sh_eth_tsu_purge_all(ndev);
2282 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2283 } else if (mdp->cd->tsu) {
2284 struct netdev_hw_addr *ha;
2285 netdev_for_each_mc_addr(ha, ndev) {
2286 if (mcast_all && is_multicast_ether_addr(ha->addr))
2287 continue;
2288
2289 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2290 if (!mcast_all) {
2291 sh_eth_tsu_purge_mcast(ndev);
2292 ecmr_bits &= ~ECMR_MCT;
2293 mcast_all = 1;
2294 }
2295 }
2296 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002297 } else {
2298 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002299 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002300 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002301
2302 /* update the ethernet mode */
2303 sh_eth_write(ndev, ecmr_bits, ECMR);
2304
2305 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002306}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002307
2308static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2309{
2310 if (!mdp->port)
2311 return TSU_VTAG0;
2312 else
2313 return TSU_VTAG1;
2314}
2315
Patrick McHardy80d5c362013-04-19 02:04:28 +00002316static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2317 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002318{
2319 struct sh_eth_private *mdp = netdev_priv(ndev);
2320 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2321
2322 if (unlikely(!mdp->cd->tsu))
2323 return -EPERM;
2324
2325 /* No filtering if vid = 0 */
2326 if (!vid)
2327 return 0;
2328
2329 mdp->vlan_num_ids++;
2330
2331 /*
2332 * The controller has one VLAN tag HW filter. So, if the filter is
2333 * already enabled, the driver disables it and the filte
2334 */
2335 if (mdp->vlan_num_ids > 1) {
2336 /* disable VLAN filter */
2337 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2338 return 0;
2339 }
2340
2341 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2342 vtag_reg_index);
2343
2344 return 0;
2345}
2346
Patrick McHardy80d5c362013-04-19 02:04:28 +00002347static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2348 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002349{
2350 struct sh_eth_private *mdp = netdev_priv(ndev);
2351 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2352
2353 if (unlikely(!mdp->cd->tsu))
2354 return -EPERM;
2355
2356 /* No filtering if vid = 0 */
2357 if (!vid)
2358 return 0;
2359
2360 mdp->vlan_num_ids--;
2361 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2362
2363 return 0;
2364}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002365
2366/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002367static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002368{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002369 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2370 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2371 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2372 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2373 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2374 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2375 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2376 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2377 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2378 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002379 if (sh_eth_is_gether(mdp)) {
2380 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2381 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2382 } else {
2383 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2384 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2385 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002386 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2387 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2388 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2389 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2390 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2391 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2392 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002393}
2394
2395/* MDIO bus release function */
2396static int sh_mdio_release(struct net_device *ndev)
2397{
2398 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2399
2400 /* unregister mdio bus */
2401 mdiobus_unregister(bus);
2402
2403 /* remove mdio bus info from net_device */
2404 dev_set_drvdata(&ndev->dev, NULL);
2405
2406 /* free bitbang info */
2407 free_mdio_bitbang(bus);
2408
2409 return 0;
2410}
2411
2412/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002413static int sh_mdio_init(struct net_device *ndev, int id,
2414 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002415{
2416 int ret, i;
2417 struct bb_info *bitbang;
2418 struct sh_eth_private *mdp = netdev_priv(ndev);
2419
2420 /* create bit control struct for PHY */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002421 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2422 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002423 if (!bitbang) {
2424 ret = -ENOMEM;
2425 goto out;
2426 }
2427
2428 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002429 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002430 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002431 bitbang->mdi_msk = PIR_MDI;
2432 bitbang->mdo_msk = PIR_MDO;
2433 bitbang->mmd_msk = PIR_MMD;
2434 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002435 bitbang->ctrl.ops = &bb_ops;
2436
Stefan Weilc2e07b32010-08-03 19:44:52 +02002437 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002438 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2439 if (!mdp->mii_bus) {
2440 ret = -ENOMEM;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002441 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002442 }
2443
2444 /* Hook up MII support for ethtool */
2445 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002446 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002447 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002448 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002449
2450 /* PHY IRQ */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002451 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2452 sizeof(int) * PHY_MAX_ADDR,
2453 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002454 if (!mdp->mii_bus->irq) {
2455 ret = -ENOMEM;
2456 goto out_free_bus;
2457 }
2458
2459 for (i = 0; i < PHY_MAX_ADDR; i++)
2460 mdp->mii_bus->irq[i] = PHY_POLL;
2461
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002462 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002463 ret = mdiobus_register(mdp->mii_bus);
2464 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002465 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002466
2467 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2468
2469 return 0;
2470
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002471out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002472 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002473
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002474out:
2475 return ret;
2476}
2477
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002478static const u16 *sh_eth_get_register_offset(int register_type)
2479{
2480 const u16 *reg_offset = NULL;
2481
2482 switch (register_type) {
2483 case SH_ETH_REG_GIGABIT:
2484 reg_offset = sh_eth_offset_gigabit;
2485 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002486 case SH_ETH_REG_FAST_RCAR:
2487 reg_offset = sh_eth_offset_fast_rcar;
2488 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002489 case SH_ETH_REG_FAST_SH4:
2490 reg_offset = sh_eth_offset_fast_sh4;
2491 break;
2492 case SH_ETH_REG_FAST_SH3_SH2:
2493 reg_offset = sh_eth_offset_fast_sh3_sh2;
2494 break;
2495 default:
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +00002496 pr_err("Unknown register type (%d)\n", register_type);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002497 break;
2498 }
2499
2500 return reg_offset;
2501}
2502
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002503static struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002504 .ndo_open = sh_eth_open,
2505 .ndo_stop = sh_eth_close,
2506 .ndo_start_xmit = sh_eth_start_xmit,
2507 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002508 .ndo_tx_timeout = sh_eth_tx_timeout,
2509 .ndo_do_ioctl = sh_eth_do_ioctl,
2510 .ndo_validate_addr = eth_validate_addr,
2511 .ndo_set_mac_address = eth_mac_addr,
2512 .ndo_change_mtu = eth_change_mtu,
2513};
2514
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002515static int sh_eth_drv_probe(struct platform_device *pdev)
2516{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002517 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002518 struct resource *res;
2519 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002520 struct sh_eth_private *mdp = NULL;
Sergei Shtylyov564044b2013-03-21 10:39:22 +00002521 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002522
2523 /* get base addr */
2524 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2525 if (unlikely(res == NULL)) {
2526 dev_err(&pdev->dev, "invalid resource\n");
2527 ret = -EINVAL;
2528 goto out;
2529 }
2530
2531 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2532 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002533 ret = -ENOMEM;
2534 goto out;
2535 }
2536
2537 /* The sh Ether-specific entries in the device structure. */
2538 ndev->base_addr = res->start;
2539 devno = pdev->id;
2540 if (devno < 0)
2541 devno = 0;
2542
2543 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002544 ret = platform_get_irq(pdev, 0);
2545 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002546 ret = -ENODEV;
2547 goto out_release;
2548 }
roel kluincc3c0802008-09-10 19:22:44 +02002549 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002550
2551 SET_NETDEV_DEV(ndev, &pdev->dev);
2552
2553 /* Fill in the fields of the device structure with ethernet values. */
2554 ether_setup(ndev);
2555
2556 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002557 mdp->num_tx_ring = TX_RING_SIZE;
2558 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002559 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2560 if (IS_ERR(mdp->addr)) {
2561 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002562 goto out_release;
2563 }
2564
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002565 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002566 mdp->pdev = pdev;
2567 pm_runtime_enable(&pdev->dev);
2568 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002569
2570 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002571 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002572 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002573 /* EDMAC endian */
2574 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002575 mdp->no_ether_link = pd->no_ether_link;
2576 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002577 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002578
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002579 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002580#if defined(SH_ETH_HAS_BOTH_MODULES)
2581 mdp->cd = sh_eth_get_cpu_data(mdp);
2582#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002583 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002584#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002585 sh_eth_set_default_cpu_data(mdp->cd);
2586
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002587 /* set function */
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002588 if (mdp->cd->tsu) {
2589 sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
2590 sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
2591 sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
2592 sh_eth_vlan_rx_kill_vid;
2593 }
2594
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002595 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002596 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002597 ndev->watchdog_timeo = TX_TIMEOUT;
2598
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002599 /* debug message level */
2600 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002601
2602 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002603 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002604 if (!is_valid_ether_addr(ndev->dev_addr)) {
2605 dev_warn(&pdev->dev,
2606 "no valid MAC address supplied, using a random one.\n");
2607 eth_hw_addr_random(ndev);
2608 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002609
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002610 /* ioremap the TSU registers */
2611 if (mdp->cd->tsu) {
2612 struct resource *rtsu;
2613 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002614 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2615 if (IS_ERR(mdp->tsu_addr)) {
2616 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002617 goto out_release;
2618 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002619 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002620 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002621 }
2622
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002623 /* initialize first or needed device */
2624 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002625 if (mdp->cd->chip_reset)
2626 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002627
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002628 if (mdp->cd->tsu) {
2629 /* TSU init (Init only)*/
2630 sh_eth_tsu_init(mdp);
2631 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002632 }
2633
2634 /* network device register */
2635 ret = register_netdev(ndev);
2636 if (ret)
2637 goto out_release;
2638
2639 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002640 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002641 if (ret)
2642 goto out_unregister;
2643
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002644 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002645 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2646 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002647
2648 platform_set_drvdata(pdev, ndev);
2649
2650 return ret;
2651
2652out_unregister:
2653 unregister_netdev(ndev);
2654
2655out_release:
2656 /* net_dev free */
2657 if (ndev)
2658 free_netdev(ndev);
2659
2660out:
2661 return ret;
2662}
2663
2664static int sh_eth_drv_remove(struct platform_device *pdev)
2665{
2666 struct net_device *ndev = platform_get_drvdata(pdev);
2667
2668 sh_mdio_release(ndev);
2669 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00002670 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002671 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002672
2673 return 0;
2674}
2675
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002676#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002677static int sh_eth_runtime_nop(struct device *dev)
2678{
2679 /*
2680 * Runtime PM callback shared between ->runtime_suspend()
2681 * and ->runtime_resume(). Simply returns success.
2682 *
2683 * This driver re-initializes all registers after
2684 * pm_runtime_get_sync() anyway so there is no need
2685 * to save and restore registers here.
2686 */
2687 return 0;
2688}
2689
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002690static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002691 .runtime_suspend = sh_eth_runtime_nop,
2692 .runtime_resume = sh_eth_runtime_nop,
2693};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002694#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2695#else
2696#define SH_ETH_PM_OPS NULL
2697#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00002698
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002699static struct platform_driver sh_eth_driver = {
2700 .probe = sh_eth_drv_probe,
2701 .remove = sh_eth_drv_remove,
2702 .driver = {
2703 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002704 .pm = SH_ETH_PM_OPS,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002705 },
2706};
2707
Axel Lindb62f682011-11-27 16:44:17 +00002708module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002709
2710MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2711MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2712MODULE_LICENSE("GPL v2");