blob: 8cb600ccaf1daece1297fe5664aa8ccf64b7e6fd [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
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400324static void 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
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400348static void 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 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000359static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000360{
361 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000362
363 switch (mdp->speed) {
364 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000365 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000366 break;
367 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000368 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
369 break;
370 default:
371 break;
372 }
373}
374
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000375/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000376static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000377 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000378 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000379
380 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
381 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
382 .eesipr_value = 0x01ff009f,
383
384 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
385 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
386 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
387 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
388
389 .apr = 1,
390 .mpr = 1,
391 .tpauser = 1,
392 .hw_swap = 1,
393};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000394
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000395static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000396{
397 struct sh_eth_private *mdp = netdev_priv(ndev);
398
399 switch (mdp->speed) {
400 case 10: /* 10BASE */
401 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
402 break;
403 case 100:/* 100BASE */
404 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000405 break;
406 default:
407 break;
408 }
409}
410
411/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000412static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000413 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000414 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000415
416 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
417 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
418 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
419
420 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
421 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
422 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
423 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
424
425 .apr = 1,
426 .mpr = 1,
427 .tpauser = 1,
428 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800429 .rpadir = 1,
430 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000431};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000432
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000433static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000434{
435 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000436
437 switch (mdp->speed) {
438 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000439 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000440 break;
441 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000442 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000443 break;
444 default:
445 break;
446 }
447}
448
449/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000450static struct sh_eth_cpu_data sh7757_data = {
451 .set_duplex = sh_eth_set_duplex,
452 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000453
454 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
455 .rmcr_value = 0x00000001,
456
457 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
458 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
459 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
460 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
461
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000462 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000463 .apr = 1,
464 .mpr = 1,
465 .tpauser = 1,
466 .hw_swap = 1,
467 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000468 .rpadir = 1,
469 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000470};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000471
David S. Millere403d292013-06-07 23:40:41 -0700472#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000473#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
474#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
475static void sh_eth_chip_reset_giga(struct net_device *ndev)
476{
477 int i;
478 unsigned long mahr[2], malr[2];
479
480 /* save MAHR and MALR */
481 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000482 malr[i] = ioread32((void *)GIGA_MALR(i));
483 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000484 }
485
486 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000487 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000488 mdelay(1);
489
490 /* restore MAHR and MALR */
491 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000492 iowrite32(malr[i], (void *)GIGA_MALR(i));
493 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000494 }
495}
496
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000497static void sh_eth_set_rate_giga(struct net_device *ndev)
498{
499 struct sh_eth_private *mdp = netdev_priv(ndev);
500
501 switch (mdp->speed) {
502 case 10: /* 10BASE */
503 sh_eth_write(ndev, 0x00000000, GECMR);
504 break;
505 case 100:/* 100BASE */
506 sh_eth_write(ndev, 0x00000010, GECMR);
507 break;
508 case 1000: /* 1000BASE */
509 sh_eth_write(ndev, 0x00000020, GECMR);
510 break;
511 default:
512 break;
513 }
514}
515
516/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000517static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000518 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000519 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000520 .set_rate = sh_eth_set_rate_giga,
521
522 .ecsr_value = ECSR_ICD | ECSR_MPD,
523 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
524 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
525
526 .tx_check = EESR_TC1 | EESR_FTC,
527 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
528 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
529 EESR_ECI,
530 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
531 EESR_TFE,
532 .fdr_value = 0x0000072f,
533 .rmcr_value = 0x00000001,
534
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000535 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000536 .apr = 1,
537 .mpr = 1,
538 .tpauser = 1,
539 .bculr = 1,
540 .hw_swap = 1,
541 .rpadir = 1,
542 .rpadir_value = 2 << 16,
543 .no_trimd = 1,
544 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000545 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000546};
547
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000548static void sh_eth_chip_reset(struct net_device *ndev)
549{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000550 struct sh_eth_private *mdp = netdev_priv(ndev);
551
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000552 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000553 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000554 mdelay(1);
555}
556
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000557static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000558{
559 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000560
561 switch (mdp->speed) {
562 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000563 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000564 break;
565 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000566 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000567 break;
568 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000569 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000570 break;
571 default:
572 break;
573 }
574}
575
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000576/* SH7734 */
577static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000578 .chip_reset = sh_eth_chip_reset,
579 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000580 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000581
582 .ecsr_value = ECSR_ICD | ECSR_MPD,
583 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
584 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
585
586 .tx_check = EESR_TC1 | EESR_FTC,
587 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
588 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
589 EESR_ECI,
590 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
591 EESR_TFE,
592
593 .apr = 1,
594 .mpr = 1,
595 .tpauser = 1,
596 .bculr = 1,
597 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000598 .no_trimd = 1,
599 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000600 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000601 .hw_crc = 1,
602 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000603};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000604
605/* SH7763 */
606static struct sh_eth_cpu_data sh7763_data = {
607 .chip_reset = sh_eth_chip_reset,
608 .set_duplex = sh_eth_set_duplex,
609 .set_rate = sh_eth_set_rate_gether,
610
611 .ecsr_value = ECSR_ICD | ECSR_MPD,
612 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
613 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
614
615 .tx_check = EESR_TC1 | EESR_FTC,
616 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
617 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
618 EESR_ECI,
619 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
620 EESR_TFE,
621
622 .apr = 1,
623 .mpr = 1,
624 .tpauser = 1,
625 .bculr = 1,
626 .hw_swap = 1,
627 .no_trimd = 1,
628 .no_ade = 1,
629 .tsu = 1,
630 .irq_flags = IRQF_SHARED,
631};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000632
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000633static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000634{
635 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000636
637 /* reset device */
638 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
639 mdelay(1);
640
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000641 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000642}
643
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000644/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000645static struct sh_eth_cpu_data r8a7740_data = {
646 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000647 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000648 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000649
650 .ecsr_value = ECSR_ICD | ECSR_MPD,
651 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
652 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
653
654 .tx_check = EESR_TC1 | EESR_FTC,
655 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
656 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
657 EESR_ECI,
658 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
659 EESR_TFE,
660
661 .apr = 1,
662 .mpr = 1,
663 .tpauser = 1,
664 .bculr = 1,
665 .hw_swap = 1,
666 .no_trimd = 1,
667 .no_ade = 1,
668 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000669 .select_mii = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000670};
671
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000672static struct sh_eth_cpu_data sh7619_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000673 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
674
675 .apr = 1,
676 .mpr = 1,
677 .tpauser = 1,
678 .hw_swap = 1,
679};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000680
681static struct sh_eth_cpu_data sh771x_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000682 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000683 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000684};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000685
686static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
687{
688 if (!cd->ecsr_value)
689 cd->ecsr_value = DEFAULT_ECSR_INIT;
690
691 if (!cd->ecsipr_value)
692 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
693
694 if (!cd->fcftr_value)
695 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
696 DEFAULT_FIFO_F_D_RFD;
697
698 if (!cd->fdr_value)
699 cd->fdr_value = DEFAULT_FDR_INIT;
700
701 if (!cd->rmcr_value)
702 cd->rmcr_value = DEFAULT_RMCR_VALUE;
703
704 if (!cd->tx_check)
705 cd->tx_check = DEFAULT_TX_CHECK;
706
707 if (!cd->eesr_err_check)
708 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
709
710 if (!cd->tx_error_check)
711 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
712}
713
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000714static int sh_eth_check_reset(struct net_device *ndev)
715{
716 int ret = 0;
717 int cnt = 100;
718
719 while (cnt > 0) {
720 if (!(sh_eth_read(ndev, EDMR) & 0x3))
721 break;
722 mdelay(1);
723 cnt--;
724 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400725 if (cnt <= 0) {
726 pr_err("Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000727 ret = -ETIMEDOUT;
728 }
729 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000730}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000731
732static int sh_eth_reset(struct net_device *ndev)
733{
734 struct sh_eth_private *mdp = netdev_priv(ndev);
735 int ret = 0;
736
737 if (sh_eth_is_gether(mdp)) {
738 sh_eth_write(ndev, EDSR_ENALL, EDSR);
739 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
740 EDMR);
741
742 ret = sh_eth_check_reset(ndev);
743 if (ret)
744 goto out;
745
746 /* Table Init */
747 sh_eth_write(ndev, 0x0, TDLAR);
748 sh_eth_write(ndev, 0x0, TDFAR);
749 sh_eth_write(ndev, 0x0, TDFXR);
750 sh_eth_write(ndev, 0x0, TDFFR);
751 sh_eth_write(ndev, 0x0, RDLAR);
752 sh_eth_write(ndev, 0x0, RDFAR);
753 sh_eth_write(ndev, 0x0, RDFXR);
754 sh_eth_write(ndev, 0x0, RDFFR);
755
756 /* Reset HW CRC register */
757 if (mdp->cd->hw_crc)
758 sh_eth_write(ndev, 0x0, CSMR);
759
760 /* Select MII mode */
761 if (mdp->cd->select_mii)
762 sh_eth_select_mii(ndev);
763 } else {
764 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
765 EDMR);
766 mdelay(3);
767 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
768 EDMR);
769 }
770
771out:
772 return ret;
773}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000774
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000775#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000776static void sh_eth_set_receive_align(struct sk_buff *skb)
777{
778 int reserve;
779
780 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
781 if (reserve)
782 skb_reserve(skb, reserve);
783}
784#else
785static void sh_eth_set_receive_align(struct sk_buff *skb)
786{
787 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
788}
789#endif
790
791
Yoshinori Sato71557a32008-08-06 19:49:00 -0400792/* CPU <-> EDMAC endian convert */
793static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
794{
795 switch (mdp->edmac_endian) {
796 case EDMAC_LITTLE_ENDIAN:
797 return cpu_to_le32(x);
798 case EDMAC_BIG_ENDIAN:
799 return cpu_to_be32(x);
800 }
801 return x;
802}
803
804static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
805{
806 switch (mdp->edmac_endian) {
807 case EDMAC_LITTLE_ENDIAN:
808 return le32_to_cpu(x);
809 case EDMAC_BIG_ENDIAN:
810 return be32_to_cpu(x);
811 }
812 return x;
813}
814
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700815/*
816 * Program the hardware MAC address from dev->dev_addr.
817 */
818static void update_mac_address(struct net_device *ndev)
819{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000820 sh_eth_write(ndev,
821 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
822 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
823 sh_eth_write(ndev,
824 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700825}
826
827/*
828 * Get MAC address from SuperH MAC address register
829 *
830 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
831 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
832 * When you want use this device, you must set MAC address in bootloader.
833 *
834 */
Magnus Damm748031f2009-10-09 00:17:14 +0000835static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700836{
Magnus Damm748031f2009-10-09 00:17:14 +0000837 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
838 memcpy(ndev->dev_addr, mac, 6);
839 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000840 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
841 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
842 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
843 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
844 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
845 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000846 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700847}
848
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000849static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
850{
851 if (sh_eth_is_gether(mdp))
852 return EDTRR_TRNS_GETHER;
853 else
854 return EDTRR_TRNS_ETHER;
855}
856
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700857struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000858 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700859 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000860 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700861 u32 mmd_msk;/* MMD */
862 u32 mdo_msk;
863 u32 mdi_msk;
864 u32 mdc_msk;
865};
866
867/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000868static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700869{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000870 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700871}
872
873/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000874static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700875{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000876 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700877}
878
879/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000880static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700881{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000882 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700883}
884
885/* Data I/O pin control */
886static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
887{
888 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000889
890 if (bitbang->set_gate)
891 bitbang->set_gate(bitbang->addr);
892
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700893 if (bit)
894 bb_set(bitbang->addr, bitbang->mmd_msk);
895 else
896 bb_clr(bitbang->addr, bitbang->mmd_msk);
897}
898
899/* Set bit data*/
900static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
901{
902 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
903
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000904 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->mdo_msk);
909 else
910 bb_clr(bitbang->addr, bitbang->mdo_msk);
911}
912
913/* Get bit data*/
914static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
915{
916 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000917
918 if (bitbang->set_gate)
919 bitbang->set_gate(bitbang->addr);
920
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700921 return bb_read(bitbang->addr, bitbang->mdi_msk);
922}
923
924/* MDC pin control */
925static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
926{
927 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
928
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000929 if (bitbang->set_gate)
930 bitbang->set_gate(bitbang->addr);
931
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700932 if (bit)
933 bb_set(bitbang->addr, bitbang->mdc_msk);
934 else
935 bb_clr(bitbang->addr, bitbang->mdc_msk);
936}
937
938/* mdio bus control struct */
939static struct mdiobb_ops bb_ops = {
940 .owner = THIS_MODULE,
941 .set_mdc = sh_mdc_ctrl,
942 .set_mdio_dir = sh_mmd_ctrl,
943 .set_mdio_data = sh_set_mdio,
944 .get_mdio_data = sh_get_mdio,
945};
946
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700947/* free skb and descriptor buffer */
948static void sh_eth_ring_free(struct net_device *ndev)
949{
950 struct sh_eth_private *mdp = netdev_priv(ndev);
951 int i;
952
953 /* Free Rx skb ringbuffer */
954 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000955 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 if (mdp->rx_skbuff[i])
957 dev_kfree_skb(mdp->rx_skbuff[i]);
958 }
959 }
960 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000961 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700962
963 /* Free Tx skb ringbuffer */
964 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000965 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700966 if (mdp->tx_skbuff[i])
967 dev_kfree_skb(mdp->tx_skbuff[i]);
968 }
969 }
970 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000971 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700972}
973
974/* format skb and descriptor buffer */
975static void sh_eth_ring_format(struct net_device *ndev)
976{
977 struct sh_eth_private *mdp = netdev_priv(ndev);
978 int i;
979 struct sk_buff *skb;
980 struct sh_eth_rxdesc *rxdesc = NULL;
981 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000982 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
983 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700984
985 mdp->cur_rx = mdp->cur_tx = 0;
986 mdp->dirty_rx = mdp->dirty_tx = 0;
987
988 memset(mdp->rx_ring, 0, rx_ringsize);
989
990 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000991 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700992 /* skb */
993 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000994 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700995 mdp->rx_skbuff[i] = skb;
996 if (skb == NULL)
997 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000998 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000999 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001000 sh_eth_set_receive_align(skb);
1001
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001002 /* RX descriptor */
1003 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001004 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -04001005 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001006
1007 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001008 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001009 /* Rx descriptor address set */
1010 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001011 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001012 if (sh_eth_is_gether(mdp))
1013 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001014 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001015 }
1016
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001017 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001018
1019 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001020 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001021
1022 memset(mdp->tx_ring, 0, tx_ringsize);
1023
1024 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001025 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001026 mdp->tx_skbuff[i] = NULL;
1027 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001028 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001030 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001031 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001032 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001033 if (sh_eth_is_gether(mdp))
1034 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001035 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001036 }
1037
Yoshinori Sato71557a32008-08-06 19:49:00 -04001038 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001039}
1040
1041/* Get skb and descriptor buffer */
1042static int sh_eth_ring_init(struct net_device *ndev)
1043{
1044 struct sh_eth_private *mdp = netdev_priv(ndev);
1045 int rx_ringsize, tx_ringsize, ret = 0;
1046
1047 /*
1048 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1049 * card needs room to do 8 byte alignment, +2 so we can reserve
1050 * the first 2 bytes, and +16 gets room for the status word from the
1051 * card.
1052 */
1053 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1054 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001055 if (mdp->cd->rpadir)
1056 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001057
1058 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001059 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1060 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001061 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001062 ret = -ENOMEM;
1063 return ret;
1064 }
1065
Joe Perchesb2adaca2013-02-03 17:43:58 +00001066 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1067 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001068 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001069 ret = -ENOMEM;
1070 goto skb_ring_free;
1071 }
1072
1073 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001074 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001075 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001076 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001077 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 ret = -ENOMEM;
1079 goto desc_ring_free;
1080 }
1081
1082 mdp->dirty_rx = 0;
1083
1084 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001085 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001086 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001087 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001088 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001089 ret = -ENOMEM;
1090 goto desc_ring_free;
1091 }
1092 return ret;
1093
1094desc_ring_free:
1095 /* free DMA buffer */
1096 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1097
1098skb_ring_free:
1099 /* Free Rx and Tx skb ring buffer */
1100 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001101 mdp->tx_ring = NULL;
1102 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001103
1104 return ret;
1105}
1106
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001107static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1108{
1109 int ringsize;
1110
1111 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001112 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001113 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1114 mdp->rx_desc_dma);
1115 mdp->rx_ring = NULL;
1116 }
1117
1118 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001119 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001120 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1121 mdp->tx_desc_dma);
1122 mdp->tx_ring = NULL;
1123 }
1124}
1125
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001126static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001127{
1128 int ret = 0;
1129 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001130 u32 val;
1131
1132 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001133 ret = sh_eth_reset(ndev);
1134 if (ret)
1135 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001136
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001137 /* Descriptor format */
1138 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001139 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001140 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001141
1142 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001143 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001145#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001146 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001147 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001148 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001149#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001150 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001151
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001152 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001153 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1154 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001156 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001157 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001158
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001159 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001160
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001161 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001162 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001163
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001164 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001165
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001166 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001167 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001168
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001169 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001170 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1171 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001172
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001173 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001174 if (start)
1175 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001176
1177 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001178 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001179 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1180
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001181 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001182
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001183 if (mdp->cd->set_rate)
1184 mdp->cd->set_rate(ndev);
1185
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001186 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001187 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001188
1189 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001190 if (start)
1191 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001192
1193 /* Set MAC address */
1194 update_mac_address(ndev);
1195
1196 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001197 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001198 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001199 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001200 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001201 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001202 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001203
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001204 if (start) {
1205 /* Setting the Rx mode will start the Rx process. */
1206 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001207
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001208 netif_start_queue(ndev);
1209 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001210
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001211out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212 return ret;
1213}
1214
1215/* free Tx skb function */
1216static int sh_eth_txfree(struct net_device *ndev)
1217{
1218 struct sh_eth_private *mdp = netdev_priv(ndev);
1219 struct sh_eth_txdesc *txdesc;
1220 int freeNum = 0;
1221 int entry = 0;
1222
1223 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001224 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001225 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001226 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001227 break;
1228 /* Free the original skb. */
1229 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001230 dma_unmap_single(&ndev->dev, txdesc->addr,
1231 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1233 mdp->tx_skbuff[entry] = NULL;
1234 freeNum++;
1235 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001236 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001237 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001238 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001239
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001240 ndev->stats.tx_packets++;
1241 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001242 }
1243 return freeNum;
1244}
1245
1246/* Packet receive function */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001247static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001248{
1249 struct sh_eth_private *mdp = netdev_priv(ndev);
1250 struct sh_eth_rxdesc *rxdesc;
1251
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001252 int entry = mdp->cur_rx % mdp->num_rx_ring;
1253 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001254 struct sk_buff *skb;
1255 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001256 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001257
1258 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001259 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1260 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001261 pkt_len = rxdesc->frame_length;
1262
1263 if (--boguscnt < 0)
1264 break;
1265
1266 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001267 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001268
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001269#if defined(CONFIG_ARCH_R8A7740)
1270 /*
1271 * In case of almost all GETHER/ETHERs, the Receive Frame State
1272 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1273 * bit 0. However, in case of the R8A7740's GETHER, the RFS
1274 * bits are from bit 25 to bit 16. So, the driver needs right
1275 * shifting by 16.
1276 */
1277 desc_status >>= 16;
1278#endif
1279
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001280 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1281 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001282 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001283 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001284 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001286 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001287 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001288 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001289 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001290 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001291 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001292 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001293 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001294 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001295 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001296 if (!mdp->cd->hw_swap)
1297 sh_eth_soft_swap(
1298 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1299 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300 skb = mdp->rx_skbuff[entry];
1301 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001302 if (mdp->cd->rpadir)
1303 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001304 skb_put(skb, pkt_len);
1305 skb->protocol = eth_type_trans(skb, ndev);
1306 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001307 ndev->stats.rx_packets++;
1308 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001309 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001310 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001311 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001312 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001313 }
1314
1315 /* Refill the Rx ring buffers. */
1316 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001317 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001318 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001319 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001320 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001321
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001322 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001323 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001324 mdp->rx_skbuff[entry] = skb;
1325 if (skb == NULL)
1326 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001327 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001328 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001329 sh_eth_set_receive_align(skb);
1330
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001331 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001332 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001333 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001334 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001335 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001336 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001337 else
1338 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001339 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001340 }
1341
1342 /* Restart Rx engine if stopped. */
1343 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001344 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001345 /* fix the values for the next receiving if RDE is set */
1346 if (intr_status & EESR_RDE)
1347 mdp->cur_rx = mdp->dirty_rx =
1348 (sh_eth_read(ndev, RDFAR) -
1349 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001350 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001351 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001352
1353 return 0;
1354}
1355
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001356static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001357{
1358 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001359 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1360 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001361}
1362
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001363static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001364{
1365 /* enable 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
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001370/* error control function */
1371static void sh_eth_error(struct net_device *ndev, int intr_status)
1372{
1373 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001375 u32 link_stat;
1376 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377
1378 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001379 felic_stat = sh_eth_read(ndev, ECSR);
1380 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001382 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001383 if (felic_stat & ECSR_LCHNG) {
1384 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001385 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001386 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001387 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001388 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001389 if (mdp->ether_link_active_low)
1390 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001391 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001392 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001393 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001394 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001395 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001396 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1397 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001398 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001399 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1400 ECSR);
1401 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1402 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001403 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001404 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405 }
1406 }
1407 }
1408
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001409ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001410 if (intr_status & EESR_TWB) {
1411 /* Write buck end. unused write back interrupt */
1412 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001413 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001414 if (netif_msg_tx_err(mdp))
1415 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001416 }
1417
1418 if (intr_status & EESR_RABT) {
1419 /* Receive Abort int */
1420 if (intr_status & EESR_RFRMER) {
1421 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001422 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001423 if (netif_msg_rx_err(mdp))
1424 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001425 }
1426 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001427
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001428 if (intr_status & EESR_TDE) {
1429 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001430 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001431 if (netif_msg_tx_err(mdp))
1432 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1433 }
1434
1435 if (intr_status & EESR_TFE) {
1436 /* FIFO under flow */
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 FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001440 }
1441
1442 if (intr_status & EESR_RDE) {
1443 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001444 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001445
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001446 if (netif_msg_rx_err(mdp))
1447 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001449
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001450 if (intr_status & EESR_RFE) {
1451 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001452 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001453 if (netif_msg_rx_err(mdp))
1454 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1455 }
1456
1457 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1458 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001459 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001460 if (netif_msg_tx_err(mdp))
1461 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001462 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001463
1464 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1465 if (mdp->cd->no_ade)
1466 mask &= ~EESR_ADE;
1467 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001468 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001469 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001470 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001471 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1472 intr_status, mdp->cur_tx);
1473 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001474 mdp->dirty_tx, (u32) ndev->state, edtrr);
1475 /* dirty buffer free */
1476 sh_eth_txfree(ndev);
1477
1478 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001479 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001481 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001482 }
1483 /* wakeup */
1484 netif_wake_queue(ndev);
1485 }
1486}
1487
1488static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1489{
1490 struct net_device *ndev = netdev;
1491 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001492 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001493 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001494 unsigned long intr_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001495
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001496 spin_lock(&mdp->lock);
1497
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001498 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001499 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001500 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1501 * enabled since it's the one that comes thru regardless of the mask,
1502 * and we need to fully handle it in sh_eth_error() in order to quench
1503 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1504 */
1505 intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001507 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1508 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001509 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001510 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001511 ret = IRQ_HANDLED;
1512 } else
1513 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001514
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001515 if (intr_status & (EESR_FRC | /* Frame recv*/
1516 EESR_RMAF | /* Multi cast address recv*/
1517 EESR_RRF | /* Bit frame recv */
1518 EESR_RTLF | /* Long frame recv*/
1519 EESR_RTSF | /* short frame recv */
1520 EESR_PRE | /* PHY-LSI recv error */
1521 EESR_CERF)){ /* recv frame CRC error */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001522 sh_eth_rx(ndev, intr_status);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001523 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001524
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001525 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001526 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001527 sh_eth_txfree(ndev);
1528 netif_wake_queue(ndev);
1529 }
1530
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001531 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001532 sh_eth_error(ndev, intr_status);
1533
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001534other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535 spin_unlock(&mdp->lock);
1536
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001537 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001538}
1539
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001540/* PHY state control function */
1541static void sh_eth_adjust_link(struct net_device *ndev)
1542{
1543 struct sh_eth_private *mdp = netdev_priv(ndev);
1544 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001545 int new_state = 0;
1546
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001547 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001548 if (phydev->duplex != mdp->duplex) {
1549 new_state = 1;
1550 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001551 if (mdp->cd->set_duplex)
1552 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001553 }
1554
1555 if (phydev->speed != mdp->speed) {
1556 new_state = 1;
1557 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001558 if (mdp->cd->set_rate)
1559 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001560 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001561 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001562 sh_eth_write(ndev,
1563 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001564 new_state = 1;
1565 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001566 if (mdp->cd->no_psr || mdp->no_ether_link)
1567 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001568 }
1569 } else if (mdp->link) {
1570 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001571 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001572 mdp->speed = 0;
1573 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001574 if (mdp->cd->no_psr || mdp->no_ether_link)
1575 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001576 }
1577
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001578 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 phy_print_status(phydev);
1580}
1581
1582/* PHY init function */
1583static int sh_eth_phy_init(struct net_device *ndev)
1584{
1585 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001586 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001587 struct phy_device *phydev = NULL;
1588
Kay Sieversfb28ad352008-11-10 13:55:14 -08001589 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001590 mdp->mii_bus->id , mdp->phy_id);
1591
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001592 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001593 mdp->speed = 0;
1594 mdp->duplex = -1;
1595
1596 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001597 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001598 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001599 if (IS_ERR(phydev)) {
1600 dev_err(&ndev->dev, "phy_connect failed\n");
1601 return PTR_ERR(phydev);
1602 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001603
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001604 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001605 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001606
1607 mdp->phydev = phydev;
1608
1609 return 0;
1610}
1611
1612/* PHY control start function */
1613static int sh_eth_phy_start(struct net_device *ndev)
1614{
1615 struct sh_eth_private *mdp = netdev_priv(ndev);
1616 int ret;
1617
1618 ret = sh_eth_phy_init(ndev);
1619 if (ret)
1620 return ret;
1621
1622 /* reset phy - this also wakes it from PDOWN */
1623 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1624 phy_start(mdp->phydev);
1625
1626 return 0;
1627}
1628
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001629static int sh_eth_get_settings(struct net_device *ndev,
1630 struct ethtool_cmd *ecmd)
1631{
1632 struct sh_eth_private *mdp = netdev_priv(ndev);
1633 unsigned long flags;
1634 int ret;
1635
1636 spin_lock_irqsave(&mdp->lock, flags);
1637 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1638 spin_unlock_irqrestore(&mdp->lock, flags);
1639
1640 return ret;
1641}
1642
1643static int sh_eth_set_settings(struct net_device *ndev,
1644 struct ethtool_cmd *ecmd)
1645{
1646 struct sh_eth_private *mdp = netdev_priv(ndev);
1647 unsigned long flags;
1648 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001649
1650 spin_lock_irqsave(&mdp->lock, flags);
1651
1652 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001653 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001654
1655 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1656 if (ret)
1657 goto error_exit;
1658
1659 if (ecmd->duplex == DUPLEX_FULL)
1660 mdp->duplex = 1;
1661 else
1662 mdp->duplex = 0;
1663
1664 if (mdp->cd->set_duplex)
1665 mdp->cd->set_duplex(ndev);
1666
1667error_exit:
1668 mdelay(1);
1669
1670 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001671 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001672
1673 spin_unlock_irqrestore(&mdp->lock, flags);
1674
1675 return ret;
1676}
1677
1678static int sh_eth_nway_reset(struct net_device *ndev)
1679{
1680 struct sh_eth_private *mdp = netdev_priv(ndev);
1681 unsigned long flags;
1682 int ret;
1683
1684 spin_lock_irqsave(&mdp->lock, flags);
1685 ret = phy_start_aneg(mdp->phydev);
1686 spin_unlock_irqrestore(&mdp->lock, flags);
1687
1688 return ret;
1689}
1690
1691static u32 sh_eth_get_msglevel(struct net_device *ndev)
1692{
1693 struct sh_eth_private *mdp = netdev_priv(ndev);
1694 return mdp->msg_enable;
1695}
1696
1697static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1698{
1699 struct sh_eth_private *mdp = netdev_priv(ndev);
1700 mdp->msg_enable = value;
1701}
1702
1703static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1704 "rx_current", "tx_current",
1705 "rx_dirty", "tx_dirty",
1706};
1707#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1708
1709static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1710{
1711 switch (sset) {
1712 case ETH_SS_STATS:
1713 return SH_ETH_STATS_LEN;
1714 default:
1715 return -EOPNOTSUPP;
1716 }
1717}
1718
1719static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1720 struct ethtool_stats *stats, u64 *data)
1721{
1722 struct sh_eth_private *mdp = netdev_priv(ndev);
1723 int i = 0;
1724
1725 /* device-specific stats */
1726 data[i++] = mdp->cur_rx;
1727 data[i++] = mdp->cur_tx;
1728 data[i++] = mdp->dirty_rx;
1729 data[i++] = mdp->dirty_tx;
1730}
1731
1732static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1733{
1734 switch (stringset) {
1735 case ETH_SS_STATS:
1736 memcpy(data, *sh_eth_gstrings_stats,
1737 sizeof(sh_eth_gstrings_stats));
1738 break;
1739 }
1740}
1741
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001742static void sh_eth_get_ringparam(struct net_device *ndev,
1743 struct ethtool_ringparam *ring)
1744{
1745 struct sh_eth_private *mdp = netdev_priv(ndev);
1746
1747 ring->rx_max_pending = RX_RING_MAX;
1748 ring->tx_max_pending = TX_RING_MAX;
1749 ring->rx_pending = mdp->num_rx_ring;
1750 ring->tx_pending = mdp->num_tx_ring;
1751}
1752
1753static int sh_eth_set_ringparam(struct net_device *ndev,
1754 struct ethtool_ringparam *ring)
1755{
1756 struct sh_eth_private *mdp = netdev_priv(ndev);
1757 int ret;
1758
1759 if (ring->tx_pending > TX_RING_MAX ||
1760 ring->rx_pending > RX_RING_MAX ||
1761 ring->tx_pending < TX_RING_MIN ||
1762 ring->rx_pending < RX_RING_MIN)
1763 return -EINVAL;
1764 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1765 return -EINVAL;
1766
1767 if (netif_running(ndev)) {
1768 netif_tx_disable(ndev);
1769 /* Disable interrupts by clearing the interrupt mask. */
1770 sh_eth_write(ndev, 0x0000, EESIPR);
1771 /* Stop the chip's Tx and Rx processes. */
1772 sh_eth_write(ndev, 0, EDTRR);
1773 sh_eth_write(ndev, 0, EDRRR);
1774 synchronize_irq(ndev->irq);
1775 }
1776
1777 /* Free all the skbuffs in the Rx queue. */
1778 sh_eth_ring_free(ndev);
1779 /* Free DMA buffer */
1780 sh_eth_free_dma_buffer(mdp);
1781
1782 /* Set new parameters */
1783 mdp->num_rx_ring = ring->rx_pending;
1784 mdp->num_tx_ring = ring->tx_pending;
1785
1786 ret = sh_eth_ring_init(ndev);
1787 if (ret < 0) {
1788 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1789 return ret;
1790 }
1791 ret = sh_eth_dev_init(ndev, false);
1792 if (ret < 0) {
1793 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1794 return ret;
1795 }
1796
1797 if (netif_running(ndev)) {
1798 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1799 /* Setting the Rx mode will start the Rx process. */
1800 sh_eth_write(ndev, EDRRR_R, EDRRR);
1801 netif_wake_queue(ndev);
1802 }
1803
1804 return 0;
1805}
1806
stephen hemminger9b07be42012-01-04 12:59:49 +00001807static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001808 .get_settings = sh_eth_get_settings,
1809 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001810 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001811 .get_msglevel = sh_eth_get_msglevel,
1812 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001813 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001814 .get_strings = sh_eth_get_strings,
1815 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1816 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001817 .get_ringparam = sh_eth_get_ringparam,
1818 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001819};
1820
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001821/* network device open function */
1822static int sh_eth_open(struct net_device *ndev)
1823{
1824 int ret = 0;
1825 struct sh_eth_private *mdp = netdev_priv(ndev);
1826
Magnus Dammbcd51492009-10-09 00:20:04 +00001827 pm_runtime_get_sync(&mdp->pdev->dev);
1828
Joe Perchesa0607fd2009-11-18 23:29:17 -08001829 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001830 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001831 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001832 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001833 return ret;
1834 }
1835
1836 /* Descriptor set */
1837 ret = sh_eth_ring_init(ndev);
1838 if (ret)
1839 goto out_free_irq;
1840
1841 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001842 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001843 if (ret)
1844 goto out_free_irq;
1845
1846 /* PHY control start*/
1847 ret = sh_eth_phy_start(ndev);
1848 if (ret)
1849 goto out_free_irq;
1850
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001851 return ret;
1852
1853out_free_irq:
1854 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001855 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001856 return ret;
1857}
1858
1859/* Timeout function */
1860static void sh_eth_tx_timeout(struct net_device *ndev)
1861{
1862 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001863 struct sh_eth_rxdesc *rxdesc;
1864 int i;
1865
1866 netif_stop_queue(ndev);
1867
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001868 if (netif_msg_timer(mdp))
1869 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001870 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001871
1872 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001873 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001874
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001875 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001876 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001877 rxdesc = &mdp->rx_ring[i];
1878 rxdesc->status = 0;
1879 rxdesc->addr = 0xBADF00D0;
1880 if (mdp->rx_skbuff[i])
1881 dev_kfree_skb(mdp->rx_skbuff[i]);
1882 mdp->rx_skbuff[i] = NULL;
1883 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001884 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001885 if (mdp->tx_skbuff[i])
1886 dev_kfree_skb(mdp->tx_skbuff[i]);
1887 mdp->tx_skbuff[i] = NULL;
1888 }
1889
1890 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001891 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001892}
1893
1894/* Packet transmit function */
1895static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1896{
1897 struct sh_eth_private *mdp = netdev_priv(ndev);
1898 struct sh_eth_txdesc *txdesc;
1899 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001900 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001901
1902 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001903 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001904 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001905 if (netif_msg_tx_queued(mdp))
1906 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001907 netif_stop_queue(ndev);
1908 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001909 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001910 }
1911 }
1912 spin_unlock_irqrestore(&mdp->lock, flags);
1913
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001914 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001915 mdp->tx_skbuff[entry] = skb;
1916 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001917 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001918 if (!mdp->cd->hw_swap)
1919 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1920 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001921 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1922 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001923 if (skb->len < ETHERSMALL)
1924 txdesc->buffer_length = ETHERSMALL;
1925 else
1926 txdesc->buffer_length = skb->len;
1927
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001928 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001929 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001930 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001931 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001932
1933 mdp->cur_tx++;
1934
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001935 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1936 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001937
Patrick McHardy6ed10652009-06-23 06:03:08 +00001938 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001939}
1940
1941/* device close function */
1942static int sh_eth_close(struct net_device *ndev)
1943{
1944 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001945
1946 netif_stop_queue(ndev);
1947
1948 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001949 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001950
1951 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001952 sh_eth_write(ndev, 0, EDTRR);
1953 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001954
1955 /* PHY Disconnect */
1956 if (mdp->phydev) {
1957 phy_stop(mdp->phydev);
1958 phy_disconnect(mdp->phydev);
1959 }
1960
1961 free_irq(ndev->irq, ndev);
1962
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001963 /* Free all the skbuffs in the Rx queue. */
1964 sh_eth_ring_free(ndev);
1965
1966 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001967 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001968
Magnus Dammbcd51492009-10-09 00:20:04 +00001969 pm_runtime_put_sync(&mdp->pdev->dev);
1970
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001971 return 0;
1972}
1973
1974static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1975{
1976 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001977
Magnus Dammbcd51492009-10-09 00:20:04 +00001978 pm_runtime_get_sync(&mdp->pdev->dev);
1979
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001980 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001981 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001982 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001983 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001984 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001985 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001986 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001987 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001988 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001989 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001990 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1991 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001992 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001993 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1994 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001995 pm_runtime_put_sync(&mdp->pdev->dev);
1996
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001997 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001998}
1999
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002000/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002001static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
2002 int cmd)
2003{
2004 struct sh_eth_private *mdp = netdev_priv(ndev);
2005 struct phy_device *phydev = mdp->phydev;
2006
2007 if (!netif_running(ndev))
2008 return -EINVAL;
2009
2010 if (!phydev)
2011 return -ENODEV;
2012
Richard Cochran28b04112010-07-17 08:48:55 +00002013 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002014}
2015
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002016/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2017static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2018 int entry)
2019{
2020 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2021}
2022
2023static u32 sh_eth_tsu_get_post_mask(int entry)
2024{
2025 return 0x0f << (28 - ((entry % 8) * 4));
2026}
2027
2028static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2029{
2030 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2031}
2032
2033static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2034 int entry)
2035{
2036 struct sh_eth_private *mdp = netdev_priv(ndev);
2037 u32 tmp;
2038 void *reg_offset;
2039
2040 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2041 tmp = ioread32(reg_offset);
2042 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2043}
2044
2045static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2046 int entry)
2047{
2048 struct sh_eth_private *mdp = netdev_priv(ndev);
2049 u32 post_mask, ref_mask, tmp;
2050 void *reg_offset;
2051
2052 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2053 post_mask = sh_eth_tsu_get_post_mask(entry);
2054 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2055
2056 tmp = ioread32(reg_offset);
2057 iowrite32(tmp & ~post_mask, reg_offset);
2058
2059 /* If other port enables, the function returns "true" */
2060 return tmp & ref_mask;
2061}
2062
2063static int sh_eth_tsu_busy(struct net_device *ndev)
2064{
2065 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2066 struct sh_eth_private *mdp = netdev_priv(ndev);
2067
2068 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2069 udelay(10);
2070 timeout--;
2071 if (timeout <= 0) {
2072 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2073 return -ETIMEDOUT;
2074 }
2075 }
2076
2077 return 0;
2078}
2079
2080static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2081 const u8 *addr)
2082{
2083 u32 val;
2084
2085 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2086 iowrite32(val, reg);
2087 if (sh_eth_tsu_busy(ndev) < 0)
2088 return -EBUSY;
2089
2090 val = addr[4] << 8 | addr[5];
2091 iowrite32(val, reg + 4);
2092 if (sh_eth_tsu_busy(ndev) < 0)
2093 return -EBUSY;
2094
2095 return 0;
2096}
2097
2098static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2099{
2100 u32 val;
2101
2102 val = ioread32(reg);
2103 addr[0] = (val >> 24) & 0xff;
2104 addr[1] = (val >> 16) & 0xff;
2105 addr[2] = (val >> 8) & 0xff;
2106 addr[3] = val & 0xff;
2107 val = ioread32(reg + 4);
2108 addr[4] = (val >> 8) & 0xff;
2109 addr[5] = val & 0xff;
2110}
2111
2112
2113static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2114{
2115 struct sh_eth_private *mdp = netdev_priv(ndev);
2116 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2117 int i;
2118 u8 c_addr[ETH_ALEN];
2119
2120 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2121 sh_eth_tsu_read_entry(reg_offset, c_addr);
2122 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2123 return i;
2124 }
2125
2126 return -ENOENT;
2127}
2128
2129static int sh_eth_tsu_find_empty(struct net_device *ndev)
2130{
2131 u8 blank[ETH_ALEN];
2132 int entry;
2133
2134 memset(blank, 0, sizeof(blank));
2135 entry = sh_eth_tsu_find_entry(ndev, blank);
2136 return (entry < 0) ? -ENOMEM : entry;
2137}
2138
2139static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2140 int entry)
2141{
2142 struct sh_eth_private *mdp = netdev_priv(ndev);
2143 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2144 int ret;
2145 u8 blank[ETH_ALEN];
2146
2147 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2148 ~(1 << (31 - entry)), TSU_TEN);
2149
2150 memset(blank, 0, sizeof(blank));
2151 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2152 if (ret < 0)
2153 return ret;
2154 return 0;
2155}
2156
2157static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2158{
2159 struct sh_eth_private *mdp = netdev_priv(ndev);
2160 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2161 int i, ret;
2162
2163 if (!mdp->cd->tsu)
2164 return 0;
2165
2166 i = sh_eth_tsu_find_entry(ndev, addr);
2167 if (i < 0) {
2168 /* No entry found, create one */
2169 i = sh_eth_tsu_find_empty(ndev);
2170 if (i < 0)
2171 return -ENOMEM;
2172 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2173 if (ret < 0)
2174 return ret;
2175
2176 /* Enable the entry */
2177 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2178 (1 << (31 - i)), TSU_TEN);
2179 }
2180
2181 /* Entry found or created, enable POST */
2182 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2183
2184 return 0;
2185}
2186
2187static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2188{
2189 struct sh_eth_private *mdp = netdev_priv(ndev);
2190 int i, ret;
2191
2192 if (!mdp->cd->tsu)
2193 return 0;
2194
2195 i = sh_eth_tsu_find_entry(ndev, addr);
2196 if (i) {
2197 /* Entry found */
2198 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2199 goto done;
2200
2201 /* Disable the entry if both ports was disabled */
2202 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2203 if (ret < 0)
2204 return ret;
2205 }
2206done:
2207 return 0;
2208}
2209
2210static int sh_eth_tsu_purge_all(struct net_device *ndev)
2211{
2212 struct sh_eth_private *mdp = netdev_priv(ndev);
2213 int i, ret;
2214
2215 if (unlikely(!mdp->cd->tsu))
2216 return 0;
2217
2218 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2219 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2220 continue;
2221
2222 /* Disable the entry if both ports was disabled */
2223 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2224 if (ret < 0)
2225 return ret;
2226 }
2227
2228 return 0;
2229}
2230
2231static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2232{
2233 struct sh_eth_private *mdp = netdev_priv(ndev);
2234 u8 addr[ETH_ALEN];
2235 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2236 int i;
2237
2238 if (unlikely(!mdp->cd->tsu))
2239 return;
2240
2241 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2242 sh_eth_tsu_read_entry(reg_offset, addr);
2243 if (is_multicast_ether_addr(addr))
2244 sh_eth_tsu_del_entry(ndev, addr);
2245 }
2246}
2247
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002248/* Multicast reception directions set */
2249static void sh_eth_set_multicast_list(struct net_device *ndev)
2250{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002251 struct sh_eth_private *mdp = netdev_priv(ndev);
2252 u32 ecmr_bits;
2253 int mcast_all = 0;
2254 unsigned long flags;
2255
2256 spin_lock_irqsave(&mdp->lock, flags);
2257 /*
2258 * Initial condition is MCT = 1, PRM = 0.
2259 * Depending on ndev->flags, set PRM or clear MCT
2260 */
2261 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2262
2263 if (!(ndev->flags & IFF_MULTICAST)) {
2264 sh_eth_tsu_purge_mcast(ndev);
2265 mcast_all = 1;
2266 }
2267 if (ndev->flags & IFF_ALLMULTI) {
2268 sh_eth_tsu_purge_mcast(ndev);
2269 ecmr_bits &= ~ECMR_MCT;
2270 mcast_all = 1;
2271 }
2272
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002273 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002274 sh_eth_tsu_purge_all(ndev);
2275 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2276 } else if (mdp->cd->tsu) {
2277 struct netdev_hw_addr *ha;
2278 netdev_for_each_mc_addr(ha, ndev) {
2279 if (mcast_all && is_multicast_ether_addr(ha->addr))
2280 continue;
2281
2282 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2283 if (!mcast_all) {
2284 sh_eth_tsu_purge_mcast(ndev);
2285 ecmr_bits &= ~ECMR_MCT;
2286 mcast_all = 1;
2287 }
2288 }
2289 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002290 } else {
2291 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002292 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002293 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002294
2295 /* update the ethernet mode */
2296 sh_eth_write(ndev, ecmr_bits, ECMR);
2297
2298 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002299}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002300
2301static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2302{
2303 if (!mdp->port)
2304 return TSU_VTAG0;
2305 else
2306 return TSU_VTAG1;
2307}
2308
Patrick McHardy80d5c362013-04-19 02:04:28 +00002309static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2310 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002311{
2312 struct sh_eth_private *mdp = netdev_priv(ndev);
2313 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2314
2315 if (unlikely(!mdp->cd->tsu))
2316 return -EPERM;
2317
2318 /* No filtering if vid = 0 */
2319 if (!vid)
2320 return 0;
2321
2322 mdp->vlan_num_ids++;
2323
2324 /*
2325 * The controller has one VLAN tag HW filter. So, if the filter is
2326 * already enabled, the driver disables it and the filte
2327 */
2328 if (mdp->vlan_num_ids > 1) {
2329 /* disable VLAN filter */
2330 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2331 return 0;
2332 }
2333
2334 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2335 vtag_reg_index);
2336
2337 return 0;
2338}
2339
Patrick McHardy80d5c362013-04-19 02:04:28 +00002340static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2341 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002342{
2343 struct sh_eth_private *mdp = netdev_priv(ndev);
2344 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2345
2346 if (unlikely(!mdp->cd->tsu))
2347 return -EPERM;
2348
2349 /* No filtering if vid = 0 */
2350 if (!vid)
2351 return 0;
2352
2353 mdp->vlan_num_ids--;
2354 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2355
2356 return 0;
2357}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002358
2359/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002360static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002361{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002362 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2363 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2364 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2365 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2366 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2367 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2368 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2369 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2370 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2371 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002372 if (sh_eth_is_gether(mdp)) {
2373 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2374 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2375 } else {
2376 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2377 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2378 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002379 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2380 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2381 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2382 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2383 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2384 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2385 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002386}
2387
2388/* MDIO bus release function */
2389static int sh_mdio_release(struct net_device *ndev)
2390{
2391 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2392
2393 /* unregister mdio bus */
2394 mdiobus_unregister(bus);
2395
2396 /* remove mdio bus info from net_device */
2397 dev_set_drvdata(&ndev->dev, NULL);
2398
2399 /* free bitbang info */
2400 free_mdio_bitbang(bus);
2401
2402 return 0;
2403}
2404
2405/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002406static int sh_mdio_init(struct net_device *ndev, int id,
2407 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002408{
2409 int ret, i;
2410 struct bb_info *bitbang;
2411 struct sh_eth_private *mdp = netdev_priv(ndev);
2412
2413 /* create bit control struct for PHY */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002414 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2415 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002416 if (!bitbang) {
2417 ret = -ENOMEM;
2418 goto out;
2419 }
2420
2421 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002422 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002423 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002424 bitbang->mdi_msk = PIR_MDI;
2425 bitbang->mdo_msk = PIR_MDO;
2426 bitbang->mmd_msk = PIR_MMD;
2427 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002428 bitbang->ctrl.ops = &bb_ops;
2429
Stefan Weilc2e07b32010-08-03 19:44:52 +02002430 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002431 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2432 if (!mdp->mii_bus) {
2433 ret = -ENOMEM;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002434 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002435 }
2436
2437 /* Hook up MII support for ethtool */
2438 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002439 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002440 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002441 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002442
2443 /* PHY IRQ */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002444 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2445 sizeof(int) * PHY_MAX_ADDR,
2446 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002447 if (!mdp->mii_bus->irq) {
2448 ret = -ENOMEM;
2449 goto out_free_bus;
2450 }
2451
2452 for (i = 0; i < PHY_MAX_ADDR; i++)
2453 mdp->mii_bus->irq[i] = PHY_POLL;
2454
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002455 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002456 ret = mdiobus_register(mdp->mii_bus);
2457 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002458 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002459
2460 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2461
2462 return 0;
2463
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002464out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002465 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002466
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002467out:
2468 return ret;
2469}
2470
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002471static const u16 *sh_eth_get_register_offset(int register_type)
2472{
2473 const u16 *reg_offset = NULL;
2474
2475 switch (register_type) {
2476 case SH_ETH_REG_GIGABIT:
2477 reg_offset = sh_eth_offset_gigabit;
2478 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002479 case SH_ETH_REG_FAST_RCAR:
2480 reg_offset = sh_eth_offset_fast_rcar;
2481 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002482 case SH_ETH_REG_FAST_SH4:
2483 reg_offset = sh_eth_offset_fast_sh4;
2484 break;
2485 case SH_ETH_REG_FAST_SH3_SH2:
2486 reg_offset = sh_eth_offset_fast_sh3_sh2;
2487 break;
2488 default:
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +00002489 pr_err("Unknown register type (%d)\n", register_type);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002490 break;
2491 }
2492
2493 return reg_offset;
2494}
2495
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002496static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002497 .ndo_open = sh_eth_open,
2498 .ndo_stop = sh_eth_close,
2499 .ndo_start_xmit = sh_eth_start_xmit,
2500 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002501 .ndo_tx_timeout = sh_eth_tx_timeout,
2502 .ndo_do_ioctl = sh_eth_do_ioctl,
2503 .ndo_validate_addr = eth_validate_addr,
2504 .ndo_set_mac_address = eth_mac_addr,
2505 .ndo_change_mtu = eth_change_mtu,
2506};
2507
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002508static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2509 .ndo_open = sh_eth_open,
2510 .ndo_stop = sh_eth_close,
2511 .ndo_start_xmit = sh_eth_start_xmit,
2512 .ndo_get_stats = sh_eth_get_stats,
2513 .ndo_set_rx_mode = sh_eth_set_multicast_list,
2514 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2515 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2516 .ndo_tx_timeout = sh_eth_tx_timeout,
2517 .ndo_do_ioctl = sh_eth_do_ioctl,
2518 .ndo_validate_addr = eth_validate_addr,
2519 .ndo_set_mac_address = eth_mac_addr,
2520 .ndo_change_mtu = eth_change_mtu,
2521};
2522
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002523static int sh_eth_drv_probe(struct platform_device *pdev)
2524{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002525 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002526 struct resource *res;
2527 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002528 struct sh_eth_private *mdp = NULL;
Sergei Shtylyov564044b2013-03-21 10:39:22 +00002529 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002530 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002531
2532 /* get base addr */
2533 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2534 if (unlikely(res == NULL)) {
2535 dev_err(&pdev->dev, "invalid resource\n");
2536 ret = -EINVAL;
2537 goto out;
2538 }
2539
2540 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2541 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002542 ret = -ENOMEM;
2543 goto out;
2544 }
2545
2546 /* The sh Ether-specific entries in the device structure. */
2547 ndev->base_addr = res->start;
2548 devno = pdev->id;
2549 if (devno < 0)
2550 devno = 0;
2551
2552 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002553 ret = platform_get_irq(pdev, 0);
2554 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002555 ret = -ENODEV;
2556 goto out_release;
2557 }
roel kluincc3c0802008-09-10 19:22:44 +02002558 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002559
2560 SET_NETDEV_DEV(ndev, &pdev->dev);
2561
2562 /* Fill in the fields of the device structure with ethernet values. */
2563 ether_setup(ndev);
2564
2565 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002566 mdp->num_tx_ring = TX_RING_SIZE;
2567 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002568 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2569 if (IS_ERR(mdp->addr)) {
2570 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002571 goto out_release;
2572 }
2573
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002574 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002575 mdp->pdev = pdev;
2576 pm_runtime_enable(&pdev->dev);
2577 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002578
2579 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002580 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002581 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002582 /* EDMAC endian */
2583 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002584 mdp->no_ether_link = pd->no_ether_link;
2585 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002586 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002587
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002588 /* set cpu data */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00002589 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002590 sh_eth_set_default_cpu_data(mdp->cd);
2591
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002592 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002593 if (mdp->cd->tsu)
2594 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
2595 else
2596 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002597 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002598 ndev->watchdog_timeo = TX_TIMEOUT;
2599
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002600 /* debug message level */
2601 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002602
2603 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002604 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002605 if (!is_valid_ether_addr(ndev->dev_addr)) {
2606 dev_warn(&pdev->dev,
2607 "no valid MAC address supplied, using a random one.\n");
2608 eth_hw_addr_random(ndev);
2609 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002610
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002611 /* ioremap the TSU registers */
2612 if (mdp->cd->tsu) {
2613 struct resource *rtsu;
2614 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002615 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2616 if (IS_ERR(mdp->tsu_addr)) {
2617 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002618 goto out_release;
2619 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002620 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002621 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002622 }
2623
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002624 /* initialize first or needed device */
2625 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002626 if (mdp->cd->chip_reset)
2627 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002628
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002629 if (mdp->cd->tsu) {
2630 /* TSU init (Init only)*/
2631 sh_eth_tsu_init(mdp);
2632 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002633 }
2634
2635 /* network device register */
2636 ret = register_netdev(ndev);
2637 if (ret)
2638 goto out_release;
2639
2640 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002641 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002642 if (ret)
2643 goto out_unregister;
2644
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002645 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002646 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2647 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002648
2649 platform_set_drvdata(pdev, ndev);
2650
2651 return ret;
2652
2653out_unregister:
2654 unregister_netdev(ndev);
2655
2656out_release:
2657 /* net_dev free */
2658 if (ndev)
2659 free_netdev(ndev);
2660
2661out:
2662 return ret;
2663}
2664
2665static int sh_eth_drv_remove(struct platform_device *pdev)
2666{
2667 struct net_device *ndev = platform_get_drvdata(pdev);
2668
2669 sh_mdio_release(ndev);
2670 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00002671 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002672 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002673
2674 return 0;
2675}
2676
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002677#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002678static int sh_eth_runtime_nop(struct device *dev)
2679{
2680 /*
2681 * Runtime PM callback shared between ->runtime_suspend()
2682 * and ->runtime_resume(). Simply returns success.
2683 *
2684 * This driver re-initializes all registers after
2685 * pm_runtime_get_sync() anyway so there is no need
2686 * to save and restore registers here.
2687 */
2688 return 0;
2689}
2690
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002691static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002692 .runtime_suspend = sh_eth_runtime_nop,
2693 .runtime_resume = sh_eth_runtime_nop,
2694};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002695#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2696#else
2697#define SH_ETH_PM_OPS NULL
2698#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00002699
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002700static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00002701 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00002702 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00002703 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002704 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00002705 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
2706 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002707 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00002708 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00002709 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002710 { }
2711};
2712MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
2713
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002714static struct platform_driver sh_eth_driver = {
2715 .probe = sh_eth_drv_probe,
2716 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002717 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002718 .driver = {
2719 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002720 .pm = SH_ETH_PM_OPS,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002721 },
2722};
2723
Axel Lindb62f682011-11-27 16:44:17 +00002724module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002725
2726MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2727MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2728MODULE_LICENSE("GPL v2");