blob: 99a838db032e5177310ec4af55a2aa15383c17bd [file] [log] [blame]
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001/* SuperH Ethernet device driver
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002 *
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003 * Copyright (C) 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyovb356e972014-02-18 03:12:43 +03005 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2014 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00007 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070025#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070026#include <linux/dma-mapping.h>
27#include <linux/etherdevice.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/mdio-bitbang.h>
31#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070036#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000039#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000041#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000042#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000043#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000044#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000045#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070046
47#include "sh_eth.h"
48
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000049#define SH_ETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_LINK | \
51 NETIF_MSG_TIMER | \
52 NETIF_MSG_RX_ERR| \
53 NETIF_MSG_TX_ERR)
54
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000055static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
56 [EDSR] = 0x0000,
57 [EDMR] = 0x0400,
58 [EDTRR] = 0x0408,
59 [EDRRR] = 0x0410,
60 [EESR] = 0x0428,
61 [EESIPR] = 0x0430,
62 [TDLAR] = 0x0010,
63 [TDFAR] = 0x0014,
64 [TDFXR] = 0x0018,
65 [TDFFR] = 0x001c,
66 [RDLAR] = 0x0030,
67 [RDFAR] = 0x0034,
68 [RDFXR] = 0x0038,
69 [RDFFR] = 0x003c,
70 [TRSCER] = 0x0438,
71 [RMFCR] = 0x0440,
72 [TFTR] = 0x0448,
73 [FDR] = 0x0450,
74 [RMCR] = 0x0458,
75 [RPADIR] = 0x0460,
76 [FCFTR] = 0x0468,
77 [CSMR] = 0x04E4,
78
79 [ECMR] = 0x0500,
80 [ECSR] = 0x0510,
81 [ECSIPR] = 0x0518,
82 [PIR] = 0x0520,
83 [PSR] = 0x0528,
84 [PIPR] = 0x052c,
85 [RFLR] = 0x0508,
86 [APR] = 0x0554,
87 [MPR] = 0x0558,
88 [PFTCR] = 0x055c,
89 [PFRCR] = 0x0560,
90 [TPAUSER] = 0x0564,
91 [GECMR] = 0x05b0,
92 [BCULR] = 0x05b4,
93 [MAHR] = 0x05c0,
94 [MALR] = 0x05c8,
95 [TROCR] = 0x0700,
96 [CDCR] = 0x0708,
97 [LCCR] = 0x0710,
98 [CEFCR] = 0x0740,
99 [FRECR] = 0x0748,
100 [TSFRCR] = 0x0750,
101 [TLFRCR] = 0x0758,
102 [RFCR] = 0x0760,
103 [CERCR] = 0x0768,
104 [CEECR] = 0x0770,
105 [MAFCR] = 0x0778,
106 [RMII_MII] = 0x0790,
107
108 [ARSTR] = 0x0000,
109 [TSU_CTRST] = 0x0004,
110 [TSU_FWEN0] = 0x0010,
111 [TSU_FWEN1] = 0x0014,
112 [TSU_FCM] = 0x0018,
113 [TSU_BSYSL0] = 0x0020,
114 [TSU_BSYSL1] = 0x0024,
115 [TSU_PRISL0] = 0x0028,
116 [TSU_PRISL1] = 0x002c,
117 [TSU_FWSL0] = 0x0030,
118 [TSU_FWSL1] = 0x0034,
119 [TSU_FWSLC] = 0x0038,
120 [TSU_QTAG0] = 0x0040,
121 [TSU_QTAG1] = 0x0044,
122 [TSU_FWSR] = 0x0050,
123 [TSU_FWINMK] = 0x0054,
124 [TSU_ADQT0] = 0x0048,
125 [TSU_ADQT1] = 0x004c,
126 [TSU_VTAG0] = 0x0058,
127 [TSU_VTAG1] = 0x005c,
128 [TSU_ADSBSY] = 0x0060,
129 [TSU_TEN] = 0x0064,
130 [TSU_POST1] = 0x0070,
131 [TSU_POST2] = 0x0074,
132 [TSU_POST3] = 0x0078,
133 [TSU_POST4] = 0x007c,
134 [TSU_ADRH0] = 0x0100,
135 [TSU_ADRL0] = 0x0104,
136 [TSU_ADRH31] = 0x01f8,
137 [TSU_ADRL31] = 0x01fc,
138
139 [TXNLCR0] = 0x0080,
140 [TXALCR0] = 0x0084,
141 [RXNLCR0] = 0x0088,
142 [RXALCR0] = 0x008c,
143 [FWNLCR0] = 0x0090,
144 [FWALCR0] = 0x0094,
145 [TXNLCR1] = 0x00a0,
146 [TXALCR1] = 0x00a0,
147 [RXNLCR1] = 0x00a8,
148 [RXALCR1] = 0x00ac,
149 [FWNLCR1] = 0x00b0,
150 [FWALCR1] = 0x00b4,
151};
152
Simon Hormandb893472014-01-17 09:22:28 +0900153static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
154 [EDSR] = 0x0000,
155 [EDMR] = 0x0400,
156 [EDTRR] = 0x0408,
157 [EDRRR] = 0x0410,
158 [EESR] = 0x0428,
159 [EESIPR] = 0x0430,
160 [TDLAR] = 0x0010,
161 [TDFAR] = 0x0014,
162 [TDFXR] = 0x0018,
163 [TDFFR] = 0x001c,
164 [RDLAR] = 0x0030,
165 [RDFAR] = 0x0034,
166 [RDFXR] = 0x0038,
167 [RDFFR] = 0x003c,
168 [TRSCER] = 0x0438,
169 [RMFCR] = 0x0440,
170 [TFTR] = 0x0448,
171 [FDR] = 0x0450,
172 [RMCR] = 0x0458,
173 [RPADIR] = 0x0460,
174 [FCFTR] = 0x0468,
175 [CSMR] = 0x04E4,
176
177 [ECMR] = 0x0500,
178 [RFLR] = 0x0508,
179 [ECSR] = 0x0510,
180 [ECSIPR] = 0x0518,
181 [PIR] = 0x0520,
182 [APR] = 0x0554,
183 [MPR] = 0x0558,
184 [PFTCR] = 0x055c,
185 [PFRCR] = 0x0560,
186 [TPAUSER] = 0x0564,
187 [MAHR] = 0x05c0,
188 [MALR] = 0x05c8,
189 [CEFCR] = 0x0740,
190 [FRECR] = 0x0748,
191 [TSFRCR] = 0x0750,
192 [TLFRCR] = 0x0758,
193 [RFCR] = 0x0760,
194 [MAFCR] = 0x0778,
195
196 [ARSTR] = 0x0000,
197 [TSU_CTRST] = 0x0004,
198 [TSU_VTAG0] = 0x0058,
199 [TSU_ADSBSY] = 0x0060,
200 [TSU_TEN] = 0x0064,
201 [TSU_ADRH0] = 0x0100,
202 [TSU_ADRL0] = 0x0104,
203 [TSU_ADRH31] = 0x01f8,
204 [TSU_ADRL31] = 0x01fc,
205
206 [TXNLCR0] = 0x0080,
207 [TXALCR0] = 0x0084,
208 [RXNLCR0] = 0x0088,
209 [RXALCR0] = 0x008C,
210};
211
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000212static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
213 [ECMR] = 0x0300,
214 [RFLR] = 0x0308,
215 [ECSR] = 0x0310,
216 [ECSIPR] = 0x0318,
217 [PIR] = 0x0320,
218 [PSR] = 0x0328,
219 [RDMLR] = 0x0340,
220 [IPGR] = 0x0350,
221 [APR] = 0x0354,
222 [MPR] = 0x0358,
223 [RFCF] = 0x0360,
224 [TPAUSER] = 0x0364,
225 [TPAUSECR] = 0x0368,
226 [MAHR] = 0x03c0,
227 [MALR] = 0x03c8,
228 [TROCR] = 0x03d0,
229 [CDCR] = 0x03d4,
230 [LCCR] = 0x03d8,
231 [CNDCR] = 0x03dc,
232 [CEFCR] = 0x03e4,
233 [FRECR] = 0x03e8,
234 [TSFRCR] = 0x03ec,
235 [TLFRCR] = 0x03f0,
236 [RFCR] = 0x03f4,
237 [MAFCR] = 0x03f8,
238
239 [EDMR] = 0x0200,
240 [EDTRR] = 0x0208,
241 [EDRRR] = 0x0210,
242 [TDLAR] = 0x0218,
243 [RDLAR] = 0x0220,
244 [EESR] = 0x0228,
245 [EESIPR] = 0x0230,
246 [TRSCER] = 0x0238,
247 [RMFCR] = 0x0240,
248 [TFTR] = 0x0248,
249 [FDR] = 0x0250,
250 [RMCR] = 0x0258,
251 [TFUCR] = 0x0264,
252 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900253 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000254 [FCFTR] = 0x0270,
255 [TRIMD] = 0x027c,
256};
257
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000258static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
259 [ECMR] = 0x0100,
260 [RFLR] = 0x0108,
261 [ECSR] = 0x0110,
262 [ECSIPR] = 0x0118,
263 [PIR] = 0x0120,
264 [PSR] = 0x0128,
265 [RDMLR] = 0x0140,
266 [IPGR] = 0x0150,
267 [APR] = 0x0154,
268 [MPR] = 0x0158,
269 [TPAUSER] = 0x0164,
270 [RFCF] = 0x0160,
271 [TPAUSECR] = 0x0168,
272 [BCFRR] = 0x016c,
273 [MAHR] = 0x01c0,
274 [MALR] = 0x01c8,
275 [TROCR] = 0x01d0,
276 [CDCR] = 0x01d4,
277 [LCCR] = 0x01d8,
278 [CNDCR] = 0x01dc,
279 [CEFCR] = 0x01e4,
280 [FRECR] = 0x01e8,
281 [TSFRCR] = 0x01ec,
282 [TLFRCR] = 0x01f0,
283 [RFCR] = 0x01f4,
284 [MAFCR] = 0x01f8,
285 [RTRATE] = 0x01fc,
286
287 [EDMR] = 0x0000,
288 [EDTRR] = 0x0008,
289 [EDRRR] = 0x0010,
290 [TDLAR] = 0x0018,
291 [RDLAR] = 0x0020,
292 [EESR] = 0x0028,
293 [EESIPR] = 0x0030,
294 [TRSCER] = 0x0038,
295 [RMFCR] = 0x0040,
296 [TFTR] = 0x0048,
297 [FDR] = 0x0050,
298 [RMCR] = 0x0058,
299 [TFUCR] = 0x0064,
300 [RFOCR] = 0x0068,
301 [FCFTR] = 0x0070,
302 [RPADIR] = 0x0078,
303 [TRIMD] = 0x007c,
304 [RBWAR] = 0x00c8,
305 [RDFAR] = 0x00cc,
306 [TBRAR] = 0x00d4,
307 [TDFAR] = 0x00d8,
308};
309
310static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400311 [EDMR] = 0x0000,
312 [EDTRR] = 0x0004,
313 [EDRRR] = 0x0008,
314 [TDLAR] = 0x000c,
315 [RDLAR] = 0x0010,
316 [EESR] = 0x0014,
317 [EESIPR] = 0x0018,
318 [TRSCER] = 0x001c,
319 [RMFCR] = 0x0020,
320 [TFTR] = 0x0024,
321 [FDR] = 0x0028,
322 [RMCR] = 0x002c,
323 [EDOCR] = 0x0030,
324 [FCFTR] = 0x0034,
325 [RPADIR] = 0x0038,
326 [TRIMD] = 0x003c,
327 [RBWAR] = 0x0040,
328 [RDFAR] = 0x0044,
329 [TBRAR] = 0x004c,
330 [TDFAR] = 0x0050,
331
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000332 [ECMR] = 0x0160,
333 [ECSR] = 0x0164,
334 [ECSIPR] = 0x0168,
335 [PIR] = 0x016c,
336 [MAHR] = 0x0170,
337 [MALR] = 0x0174,
338 [RFLR] = 0x0178,
339 [PSR] = 0x017c,
340 [TROCR] = 0x0180,
341 [CDCR] = 0x0184,
342 [LCCR] = 0x0188,
343 [CNDCR] = 0x018c,
344 [CEFCR] = 0x0194,
345 [FRECR] = 0x0198,
346 [TSFRCR] = 0x019c,
347 [TLFRCR] = 0x01a0,
348 [RFCR] = 0x01a4,
349 [MAFCR] = 0x01a8,
350 [IPGR] = 0x01b4,
351 [APR] = 0x01b8,
352 [MPR] = 0x01bc,
353 [TPAUSER] = 0x01c4,
354 [BCFR] = 0x01cc,
355
356 [ARSTR] = 0x0000,
357 [TSU_CTRST] = 0x0004,
358 [TSU_FWEN0] = 0x0010,
359 [TSU_FWEN1] = 0x0014,
360 [TSU_FCM] = 0x0018,
361 [TSU_BSYSL0] = 0x0020,
362 [TSU_BSYSL1] = 0x0024,
363 [TSU_PRISL0] = 0x0028,
364 [TSU_PRISL1] = 0x002c,
365 [TSU_FWSL0] = 0x0030,
366 [TSU_FWSL1] = 0x0034,
367 [TSU_FWSLC] = 0x0038,
368 [TSU_QTAGM0] = 0x0040,
369 [TSU_QTAGM1] = 0x0044,
370 [TSU_ADQT0] = 0x0048,
371 [TSU_ADQT1] = 0x004c,
372 [TSU_FWSR] = 0x0050,
373 [TSU_FWINMK] = 0x0054,
374 [TSU_ADSBSY] = 0x0060,
375 [TSU_TEN] = 0x0064,
376 [TSU_POST1] = 0x0070,
377 [TSU_POST2] = 0x0074,
378 [TSU_POST3] = 0x0078,
379 [TSU_POST4] = 0x007c,
380
381 [TXNLCR0] = 0x0080,
382 [TXALCR0] = 0x0084,
383 [RXNLCR0] = 0x0088,
384 [RXALCR0] = 0x008c,
385 [FWNLCR0] = 0x0090,
386 [FWALCR0] = 0x0094,
387 [TXNLCR1] = 0x00a0,
388 [TXALCR1] = 0x00a0,
389 [RXNLCR1] = 0x00a8,
390 [RXALCR1] = 0x00ac,
391 [FWNLCR1] = 0x00b0,
392 [FWALCR1] = 0x00b4,
393
394 [TSU_ADRH0] = 0x0100,
395 [TSU_ADRL0] = 0x0104,
396 [TSU_ADRL31] = 0x01fc,
397};
398
Simon Horman504c8ca2014-01-17 09:22:27 +0900399static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000400{
Simon Horman504c8ca2014-01-17 09:22:27 +0900401 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000402}
403
Simon Hormandb893472014-01-17 09:22:28 +0900404static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
405{
406 return mdp->reg_offset == sh_eth_offset_fast_rz;
407}
408
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400409static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000410{
411 u32 value = 0x0;
412 struct sh_eth_private *mdp = netdev_priv(ndev);
413
414 switch (mdp->phy_interface) {
415 case PHY_INTERFACE_MODE_GMII:
416 value = 0x2;
417 break;
418 case PHY_INTERFACE_MODE_MII:
419 value = 0x1;
420 break;
421 case PHY_INTERFACE_MODE_RMII:
422 value = 0x0;
423 break;
424 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300425 netdev_warn(ndev,
426 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000427 value = 0x1;
428 break;
429 }
430
431 sh_eth_write(ndev, value, RMII_MII);
432}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000433
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400434static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000435{
436 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000437
438 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000439 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000440 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000441 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000442}
443
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000444/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000445static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000446{
447 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000448
449 switch (mdp->speed) {
450 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000451 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000452 break;
453 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000454 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
455 break;
456 default:
457 break;
458 }
459}
460
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000461/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000462static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000463 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000464 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000465
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400466 .register_type = SH_ETH_REG_FAST_RCAR,
467
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000468 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
469 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
470 .eesipr_value = 0x01ff009f,
471
472 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400473 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
474 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
475 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900476 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000477
478 .apr = 1,
479 .mpr = 1,
480 .tpauser = 1,
481 .hw_swap = 1,
482};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000483
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300484/* R8A7790/1 */
485static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900486 .set_duplex = sh_eth_set_duplex,
487 .set_rate = sh_eth_set_rate_r8a777x,
488
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400489 .register_type = SH_ETH_REG_FAST_RCAR,
490
Simon Hormane18dbf72013-07-23 10:18:05 +0900491 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
492 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
493 .eesipr_value = 0x01ff009f,
494
495 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900496 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
497 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
498 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900499 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900500
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100501 .trscer_err_mask = DESC_I_RINT8,
502
Simon Hormane18dbf72013-07-23 10:18:05 +0900503 .apr = 1,
504 .mpr = 1,
505 .tpauser = 1,
506 .hw_swap = 1,
507 .rmiimode = 1,
Kouei Abefd9af072013-08-30 12:41:08 +0900508 .shift_rd0 = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900509};
510
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000511static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000512{
513 struct sh_eth_private *mdp = netdev_priv(ndev);
514
515 switch (mdp->speed) {
516 case 10: /* 10BASE */
517 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
518 break;
519 case 100:/* 100BASE */
520 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000521 break;
522 default:
523 break;
524 }
525}
526
527/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000528static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000529 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000530 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000531
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400532 .register_type = SH_ETH_REG_FAST_SH4,
533
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000534 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
535 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400536 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000537
538 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400539 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
540 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
541 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000542
543 .apr = 1,
544 .mpr = 1,
545 .tpauser = 1,
546 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800547 .rpadir = 1,
548 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000549};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000550
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000551static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000552{
553 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000554
555 switch (mdp->speed) {
556 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000557 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000558 break;
559 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000560 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000561 break;
562 default:
563 break;
564 }
565}
566
567/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000568static struct sh_eth_cpu_data sh7757_data = {
569 .set_duplex = sh_eth_set_duplex,
570 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000571
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400572 .register_type = SH_ETH_REG_FAST_SH4,
573
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000574 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000575
576 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400577 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
578 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
579 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000580
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000581 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000582 .apr = 1,
583 .mpr = 1,
584 .tpauser = 1,
585 .hw_swap = 1,
586 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000587 .rpadir = 1,
588 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000589};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000590
David S. Millere403d292013-06-07 23:40:41 -0700591#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000592#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
593#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
594static void sh_eth_chip_reset_giga(struct net_device *ndev)
595{
596 int i;
597 unsigned long mahr[2], malr[2];
598
599 /* save MAHR and MALR */
600 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000601 malr[i] = ioread32((void *)GIGA_MALR(i));
602 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000603 }
604
605 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000606 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000607 mdelay(1);
608
609 /* restore MAHR and MALR */
610 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000611 iowrite32(malr[i], (void *)GIGA_MALR(i));
612 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000613 }
614}
615
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000616static void sh_eth_set_rate_giga(struct net_device *ndev)
617{
618 struct sh_eth_private *mdp = netdev_priv(ndev);
619
620 switch (mdp->speed) {
621 case 10: /* 10BASE */
622 sh_eth_write(ndev, 0x00000000, GECMR);
623 break;
624 case 100:/* 100BASE */
625 sh_eth_write(ndev, 0x00000010, GECMR);
626 break;
627 case 1000: /* 1000BASE */
628 sh_eth_write(ndev, 0x00000020, GECMR);
629 break;
630 default:
631 break;
632 }
633}
634
635/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000636static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000637 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000638 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000639 .set_rate = sh_eth_set_rate_giga,
640
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400641 .register_type = SH_ETH_REG_GIGABIT,
642
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000643 .ecsr_value = ECSR_ICD | ECSR_MPD,
644 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
645 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
646
647 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400648 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
649 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
650 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000651 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000652
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000653 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000654 .apr = 1,
655 .mpr = 1,
656 .tpauser = 1,
657 .bculr = 1,
658 .hw_swap = 1,
659 .rpadir = 1,
660 .rpadir_value = 2 << 16,
661 .no_trimd = 1,
662 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000663 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000664};
665
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000666static void sh_eth_chip_reset(struct net_device *ndev)
667{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000668 struct sh_eth_private *mdp = netdev_priv(ndev);
669
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000670 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000671 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000672 mdelay(1);
673}
674
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000675static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000676{
677 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000678
679 switch (mdp->speed) {
680 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000681 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000682 break;
683 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000684 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000685 break;
686 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000687 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000688 break;
689 default:
690 break;
691 }
692}
693
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000694/* SH7734 */
695static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000696 .chip_reset = sh_eth_chip_reset,
697 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000698 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000699
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400700 .register_type = SH_ETH_REG_GIGABIT,
701
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000702 .ecsr_value = ECSR_ICD | ECSR_MPD,
703 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
704 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
705
706 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400707 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
708 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
709 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000710
711 .apr = 1,
712 .mpr = 1,
713 .tpauser = 1,
714 .bculr = 1,
715 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000716 .no_trimd = 1,
717 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000718 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000719 .hw_crc = 1,
720 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000721};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000722
723/* SH7763 */
724static struct sh_eth_cpu_data sh7763_data = {
725 .chip_reset = sh_eth_chip_reset,
726 .set_duplex = sh_eth_set_duplex,
727 .set_rate = sh_eth_set_rate_gether,
728
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400729 .register_type = SH_ETH_REG_GIGABIT,
730
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000731 .ecsr_value = ECSR_ICD | ECSR_MPD,
732 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
733 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
734
735 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300736 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
737 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000738 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000739
740 .apr = 1,
741 .mpr = 1,
742 .tpauser = 1,
743 .bculr = 1,
744 .hw_swap = 1,
745 .no_trimd = 1,
746 .no_ade = 1,
747 .tsu = 1,
748 .irq_flags = IRQF_SHARED,
749};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000750
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000751static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000752{
753 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000754
755 /* reset device */
756 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
757 mdelay(1);
758
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000759 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000760}
761
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000762/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000763static struct sh_eth_cpu_data r8a7740_data = {
764 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000765 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000766 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000767
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400768 .register_type = SH_ETH_REG_GIGABIT,
769
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000770 .ecsr_value = ECSR_ICD | ECSR_MPD,
771 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
772 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
773
774 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400775 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
776 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
777 EESR_TDE | EESR_ECI,
Simon Hormancc235282013-10-10 14:51:16 +0900778 .fdr_value = 0x0000070f,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000779
780 .apr = 1,
781 .mpr = 1,
782 .tpauser = 1,
783 .bculr = 1,
784 .hw_swap = 1,
Simon Hormancc235282013-10-10 14:51:16 +0900785 .rpadir = 1,
786 .rpadir_value = 2 << 16,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000787 .no_trimd = 1,
788 .no_ade = 1,
789 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000790 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400791 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000792};
793
Simon Hormandb893472014-01-17 09:22:28 +0900794/* R7S72100 */
795static struct sh_eth_cpu_data r7s72100_data = {
796 .chip_reset = sh_eth_chip_reset,
797 .set_duplex = sh_eth_set_duplex,
798
799 .register_type = SH_ETH_REG_FAST_RZ,
800
801 .ecsr_value = ECSR_ICD,
802 .ecsipr_value = ECSIPR_ICDIP,
803 .eesipr_value = 0xff7f009f,
804
805 .tx_check = EESR_TC1 | EESR_FTC,
806 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
807 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
808 EESR_TDE | EESR_ECI,
809 .fdr_value = 0x0000070f,
Simon Hormandb893472014-01-17 09:22:28 +0900810
811 .no_psr = 1,
812 .apr = 1,
813 .mpr = 1,
814 .tpauser = 1,
815 .hw_swap = 1,
816 .rpadir = 1,
817 .rpadir_value = 2 << 16,
818 .no_trimd = 1,
819 .no_ade = 1,
820 .hw_crc = 1,
821 .tsu = 1,
822 .shift_rd0 = 1,
823};
824
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000825static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400826 .register_type = SH_ETH_REG_FAST_SH3_SH2,
827
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000828 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
829
830 .apr = 1,
831 .mpr = 1,
832 .tpauser = 1,
833 .hw_swap = 1,
834};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000835
836static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400837 .register_type = SH_ETH_REG_FAST_SH3_SH2,
838
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000839 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000840 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000841};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000842
843static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
844{
845 if (!cd->ecsr_value)
846 cd->ecsr_value = DEFAULT_ECSR_INIT;
847
848 if (!cd->ecsipr_value)
849 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
850
851 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300852 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000853 DEFAULT_FIFO_F_D_RFD;
854
855 if (!cd->fdr_value)
856 cd->fdr_value = DEFAULT_FDR_INIT;
857
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000858 if (!cd->tx_check)
859 cd->tx_check = DEFAULT_TX_CHECK;
860
861 if (!cd->eesr_err_check)
862 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900863
864 if (!cd->trscer_err_mask)
865 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000866}
867
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000868static int sh_eth_check_reset(struct net_device *ndev)
869{
870 int ret = 0;
871 int cnt = 100;
872
873 while (cnt > 0) {
874 if (!(sh_eth_read(ndev, EDMR) & 0x3))
875 break;
876 mdelay(1);
877 cnt--;
878 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400879 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300880 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000881 ret = -ETIMEDOUT;
882 }
883 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000884}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000885
886static int sh_eth_reset(struct net_device *ndev)
887{
888 struct sh_eth_private *mdp = netdev_priv(ndev);
889 int ret = 0;
890
Simon Hormandb893472014-01-17 09:22:28 +0900891 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000892 sh_eth_write(ndev, EDSR_ENALL, EDSR);
893 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
894 EDMR);
895
896 ret = sh_eth_check_reset(ndev);
897 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100898 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000899
900 /* Table Init */
901 sh_eth_write(ndev, 0x0, TDLAR);
902 sh_eth_write(ndev, 0x0, TDFAR);
903 sh_eth_write(ndev, 0x0, TDFXR);
904 sh_eth_write(ndev, 0x0, TDFFR);
905 sh_eth_write(ndev, 0x0, RDLAR);
906 sh_eth_write(ndev, 0x0, RDFAR);
907 sh_eth_write(ndev, 0x0, RDFXR);
908 sh_eth_write(ndev, 0x0, RDFFR);
909
910 /* Reset HW CRC register */
911 if (mdp->cd->hw_crc)
912 sh_eth_write(ndev, 0x0, CSMR);
913
914 /* Select MII mode */
915 if (mdp->cd->select_mii)
916 sh_eth_select_mii(ndev);
917 } else {
918 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
919 EDMR);
920 mdelay(3);
921 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
922 EDMR);
923 }
924
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000925 return ret;
926}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000927
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000928static void sh_eth_set_receive_align(struct sk_buff *skb)
929{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900930 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000931
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000932 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900933 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000934}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000935
936
Yoshinori Sato71557a32008-08-06 19:49:00 -0400937/* CPU <-> EDMAC endian convert */
938static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
939{
940 switch (mdp->edmac_endian) {
941 case EDMAC_LITTLE_ENDIAN:
942 return cpu_to_le32(x);
943 case EDMAC_BIG_ENDIAN:
944 return cpu_to_be32(x);
945 }
946 return x;
947}
948
949static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
950{
951 switch (mdp->edmac_endian) {
952 case EDMAC_LITTLE_ENDIAN:
953 return le32_to_cpu(x);
954 case EDMAC_BIG_ENDIAN:
955 return be32_to_cpu(x);
956 }
957 return x;
958}
959
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300960/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700961static void update_mac_address(struct net_device *ndev)
962{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000963 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300964 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
965 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000966 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300967 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700968}
969
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300970/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700971 *
972 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
973 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
974 * When you want use this device, you must set MAC address in bootloader.
975 *
976 */
Magnus Damm748031f2009-10-09 00:17:14 +0000977static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700978{
Magnus Damm748031f2009-10-09 00:17:14 +0000979 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700980 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000981 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000982 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
983 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
984 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
985 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
986 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
987 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000988 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700989}
990
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000991static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
992{
Simon Hormandb893472014-01-17 09:22:28 +0900993 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000994 return EDTRR_TRNS_GETHER;
995 else
996 return EDTRR_TRNS_ETHER;
997}
998
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700999struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001000 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001001 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001002 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001003 u32 mmd_msk;/* MMD */
1004 u32 mdo_msk;
1005 u32 mdi_msk;
1006 u32 mdc_msk;
1007};
1008
1009/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001010static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001011{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001012 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001013}
1014
1015/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001016static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001017{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001018 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001019}
1020
1021/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001022static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001023{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001024 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001025}
1026
1027/* Data I/O pin control */
1028static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1029{
1030 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001031
1032 if (bitbang->set_gate)
1033 bitbang->set_gate(bitbang->addr);
1034
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035 if (bit)
1036 bb_set(bitbang->addr, bitbang->mmd_msk);
1037 else
1038 bb_clr(bitbang->addr, bitbang->mmd_msk);
1039}
1040
1041/* Set bit data*/
1042static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1043{
1044 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1045
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001046 if (bitbang->set_gate)
1047 bitbang->set_gate(bitbang->addr);
1048
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001049 if (bit)
1050 bb_set(bitbang->addr, bitbang->mdo_msk);
1051 else
1052 bb_clr(bitbang->addr, bitbang->mdo_msk);
1053}
1054
1055/* Get bit data*/
1056static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1057{
1058 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001059
1060 if (bitbang->set_gate)
1061 bitbang->set_gate(bitbang->addr);
1062
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001063 return bb_read(bitbang->addr, bitbang->mdi_msk);
1064}
1065
1066/* MDC pin control */
1067static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1068{
1069 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1070
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001071 if (bitbang->set_gate)
1072 bitbang->set_gate(bitbang->addr);
1073
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001074 if (bit)
1075 bb_set(bitbang->addr, bitbang->mdc_msk);
1076 else
1077 bb_clr(bitbang->addr, bitbang->mdc_msk);
1078}
1079
1080/* mdio bus control struct */
1081static struct mdiobb_ops bb_ops = {
1082 .owner = THIS_MODULE,
1083 .set_mdc = sh_mdc_ctrl,
1084 .set_mdio_dir = sh_mmd_ctrl,
1085 .set_mdio_data = sh_set_mdio,
1086 .get_mdio_data = sh_get_mdio,
1087};
1088
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001089/* free skb and descriptor buffer */
1090static void sh_eth_ring_free(struct net_device *ndev)
1091{
1092 struct sh_eth_private *mdp = netdev_priv(ndev);
1093 int i;
1094
1095 /* Free Rx skb ringbuffer */
1096 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001097 for (i = 0; i < mdp->num_rx_ring; i++)
1098 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001099 }
1100 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001101 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001102
1103 /* Free Tx skb ringbuffer */
1104 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001105 for (i = 0; i < mdp->num_tx_ring; i++)
1106 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001107 }
1108 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001109 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001110}
1111
1112/* format skb and descriptor buffer */
1113static void sh_eth_ring_format(struct net_device *ndev)
1114{
1115 struct sh_eth_private *mdp = netdev_priv(ndev);
1116 int i;
1117 struct sk_buff *skb;
1118 struct sh_eth_rxdesc *rxdesc = NULL;
1119 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001120 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1121 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001122 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001123
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001124 mdp->cur_rx = 0;
1125 mdp->cur_tx = 0;
1126 mdp->dirty_rx = 0;
1127 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001128
1129 memset(mdp->rx_ring, 0, rx_ringsize);
1130
1131 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001132 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001133 /* skb */
1134 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001135 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001136 mdp->rx_skbuff[i] = skb;
1137 if (skb == NULL)
1138 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001139 sh_eth_set_receive_align(skb);
1140
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001141 /* RX descriptor */
1142 rxdesc = &mdp->rx_ring[i];
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001143 /* The size of the buffer is a multiple of 16 bytes. */
1144 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
1145 dma_map_single(&ndev->dev, skb->data, rxdesc->buffer_length,
1146 DMA_FROM_DEVICE);
Mitsuhiro Kimura450fa212014-12-08 19:46:21 +09001147 rxdesc->addr = virt_to_phys(skb->data);
Yoshinori Sato71557a32008-08-06 19:49:00 -04001148 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001149
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001150 /* Rx descriptor address set */
1151 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001152 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001153 if (sh_eth_is_gether(mdp) ||
1154 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001155 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001156 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001157 }
1158
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001159 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001160
1161 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001162 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001163
1164 memset(mdp->tx_ring, 0, tx_ringsize);
1165
1166 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001167 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001168 mdp->tx_skbuff[i] = NULL;
1169 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001170 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001171 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001172 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001173 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001174 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001175 if (sh_eth_is_gether(mdp) ||
1176 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001177 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001178 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001179 }
1180
Yoshinori Sato71557a32008-08-06 19:49:00 -04001181 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182}
1183
1184/* Get skb and descriptor buffer */
1185static int sh_eth_ring_init(struct net_device *ndev)
1186{
1187 struct sh_eth_private *mdp = netdev_priv(ndev);
1188 int rx_ringsize, tx_ringsize, ret = 0;
1189
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001190 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001191 * card needs room to do 8 byte alignment, +2 so we can reserve
1192 * the first 2 bytes, and +16 gets room for the status word from the
1193 * card.
1194 */
1195 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1196 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001197 if (mdp->cd->rpadir)
1198 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001199
1200 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001201 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1202 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001204 ret = -ENOMEM;
1205 return ret;
1206 }
1207
Joe Perchesb2adaca2013-02-03 17:43:58 +00001208 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1209 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001210 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001211 ret = -ENOMEM;
1212 goto skb_ring_free;
1213 }
1214
1215 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001216 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001217 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001218 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001219 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220 ret = -ENOMEM;
1221 goto desc_ring_free;
1222 }
1223
1224 mdp->dirty_rx = 0;
1225
1226 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001227 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001228 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001229 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001230 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231 ret = -ENOMEM;
1232 goto desc_ring_free;
1233 }
1234 return ret;
1235
1236desc_ring_free:
1237 /* free DMA buffer */
1238 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1239
1240skb_ring_free:
1241 /* Free Rx and Tx skb ring buffer */
1242 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001243 mdp->tx_ring = NULL;
1244 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001245
1246 return ret;
1247}
1248
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001249static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1250{
1251 int ringsize;
1252
1253 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001254 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001255 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1256 mdp->rx_desc_dma);
1257 mdp->rx_ring = NULL;
1258 }
1259
1260 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001261 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001262 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1263 mdp->tx_desc_dma);
1264 mdp->tx_ring = NULL;
1265 }
1266}
1267
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001268static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001269{
1270 int ret = 0;
1271 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001272 u32 val;
1273
1274 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001275 ret = sh_eth_reset(ndev);
1276 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001277 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001278
Simon Horman55754f12013-07-23 10:18:04 +09001279 if (mdp->cd->rmiimode)
1280 sh_eth_write(ndev, 0x1, RMIIMODE);
1281
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001282 /* Descriptor format */
1283 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001284 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001285 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001286
1287 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001288 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001289
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001290#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001291 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001292 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001293 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001294#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001295 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001296
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001297 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001298 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1299 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300
Ben Dooks530aa2d2014-06-03 12:21:13 +01001301 /* Frame recv control (enable multiple-packets per rx irq) */
1302 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001303
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001304 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001306 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001307 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001308
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001309 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001310
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001311 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001312 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001313
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001314 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001315 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1316 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001317
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001318 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001319 if (start) {
1320 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001321 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001322 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001323
1324 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001325 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001326 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1327
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001328 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001329
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001330 if (mdp->cd->set_rate)
1331 mdp->cd->set_rate(ndev);
1332
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001333 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001334 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001335
1336 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001337 if (start)
1338 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001339
1340 /* Set MAC address */
1341 update_mac_address(ndev);
1342
1343 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001344 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001345 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001346 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001347 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001348 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001349 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001350
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001351 if (start) {
1352 /* Setting the Rx mode will start the Rx process. */
1353 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001354
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001355 netif_start_queue(ndev);
1356 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001357
1358 return ret;
1359}
1360
1361/* free Tx skb function */
1362static int sh_eth_txfree(struct net_device *ndev)
1363{
1364 struct sh_eth_private *mdp = netdev_priv(ndev);
1365 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001366 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001367 int entry = 0;
1368
1369 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001370 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001371 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001372 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001373 break;
1374 /* Free the original skb. */
1375 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001376 dma_unmap_single(&ndev->dev, txdesc->addr,
1377 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1379 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001380 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001382 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001383 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001384 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001385
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001386 ndev->stats.tx_packets++;
1387 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001388 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001389 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390}
1391
1392/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001393static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394{
1395 struct sh_eth_private *mdp = netdev_priv(ndev);
1396 struct sh_eth_rxdesc *rxdesc;
1397
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001398 int entry = mdp->cur_rx % mdp->num_rx_ring;
1399 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001400 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 struct sk_buff *skb;
1402 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001403 u32 desc_status;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001404 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001406 boguscnt = min(boguscnt, *quota);
1407 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001408 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001409 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1410 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001411 pkt_len = rxdesc->frame_length;
1412
1413 if (--boguscnt < 0)
1414 break;
1415
1416 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001417 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001418
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001419 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001420 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Simon Hormandb893472014-01-17 09:22:28 +09001421 * bit 0. However, in case of the R8A7740, R8A779x, and
1422 * R7S72100 the RFS bits are from bit 25 to bit 16. So, the
1423 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001424 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001425 if (mdp->cd->shift_rd0)
1426 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001427
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1429 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001430 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001431 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001432 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001434 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001435 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001436 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001437 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001438 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001439 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001440 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001441 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001442 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001443 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001444 if (!mdp->cd->hw_swap)
1445 sh_eth_soft_swap(
1446 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1447 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448 skb = mdp->rx_skbuff[entry];
1449 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001450 if (mdp->cd->rpadir)
1451 skb_reserve(skb, NET_IP_ALIGN);
Kouei Abe7db8e0c2013-08-30 12:41:07 +09001452 dma_sync_single_for_cpu(&ndev->dev, rxdesc->addr,
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001453 ALIGN(mdp->rx_buf_sz, 16),
Kouei Abe7db8e0c2013-08-30 12:41:07 +09001454 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 skb_put(skb, pkt_len);
1456 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001457 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001458 ndev->stats.rx_packets++;
1459 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001460 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001461 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001462 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001463 }
1464
1465 /* Refill the Rx ring buffers. */
1466 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001467 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001468 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001469 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001470 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001471
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001472 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001473 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001474 mdp->rx_skbuff[entry] = skb;
1475 if (skb == NULL)
1476 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001477 sh_eth_set_receive_align(skb);
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001478 dma_map_single(&ndev->dev, skb->data,
1479 rxdesc->buffer_length, DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001480
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001481 skb_checksum_none_assert(skb);
Mitsuhiro Kimura450fa212014-12-08 19:46:21 +09001482 rxdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001484 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001485 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001486 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001487 else
1488 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001489 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001490 }
1491
1492 /* Restart Rx engine if stopped. */
1493 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001494 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001495 /* fix the values for the next receiving if RDE is set */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001496 if (intr_status & EESR_RDE) {
1497 u32 count = (sh_eth_read(ndev, RDFAR) -
1498 sh_eth_read(ndev, RDLAR)) >> 4;
1499
1500 mdp->cur_rx = count;
1501 mdp->dirty_rx = count;
1502 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001503 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001504 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001505
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001506 *quota -= limit - boguscnt - 1;
1507
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001508 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001509}
1510
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001511static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001512{
1513 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001514 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1515 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001516}
1517
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001518static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001519{
1520 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001521 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1522 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001523}
1524
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001525/* error control function */
1526static void sh_eth_error(struct net_device *ndev, int intr_status)
1527{
1528 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001530 u32 link_stat;
1531 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001532
1533 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001534 felic_stat = sh_eth_read(ndev, ECSR);
1535 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001536 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001537 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001538 if (felic_stat & ECSR_LCHNG) {
1539 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001540 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001541 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001542 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001543 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001544 if (mdp->ether_link_active_low)
1545 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001546 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001547 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001548 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001549 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001550 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001551 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001552 ~DMAC_M_ECI, EESIPR);
1553 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001554 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001555 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001556 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001557 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001558 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001559 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001560 }
1561 }
1562 }
1563
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001564ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001565 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001566 /* Unused write back interrupt */
1567 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001568 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001569 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001570 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001571 }
1572
1573 if (intr_status & EESR_RABT) {
1574 /* Receive Abort int */
1575 if (intr_status & EESR_RFRMER) {
1576 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001577 ndev->stats.rx_frame_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001578 netif_err(mdp, rx_err, ndev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 }
1580 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001581
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001582 if (intr_status & EESR_TDE) {
1583 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001584 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001585 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001586 }
1587
1588 if (intr_status & EESR_TFE) {
1589 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001590 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001591 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001592 }
1593
1594 if (intr_status & EESR_RDE) {
1595 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001596 ndev->stats.rx_over_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001597 netif_err(mdp, rx_err, ndev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001598 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001599
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001600 if (intr_status & EESR_RFE) {
1601 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001602 ndev->stats.rx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001603 netif_err(mdp, rx_err, ndev, "Receive FIFO Overflow\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001604 }
1605
1606 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1607 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001608 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001609 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001611
1612 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1613 if (mdp->cd->no_ade)
1614 mask &= ~EESR_ADE;
1615 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001617 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001618
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001619 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001620 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1621 intr_status, mdp->cur_tx, mdp->dirty_tx,
1622 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001623 /* dirty buffer free */
1624 sh_eth_txfree(ndev);
1625
1626 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001627 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001628 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001629 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001630 }
1631 /* wakeup */
1632 netif_wake_queue(ndev);
1633 }
1634}
1635
1636static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1637{
1638 struct net_device *ndev = netdev;
1639 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001640 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001641 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001642 unsigned long intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001643
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001644 spin_lock(&mdp->lock);
1645
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001646 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001647 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001648 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1649 * enabled since it's the one that comes thru regardless of the mask,
1650 * and we need to fully handle it in sh_eth_error() in order to quench
1651 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1652 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001653 intr_enable = sh_eth_read(ndev, EESIPR);
1654 intr_status &= intr_enable | DMAC_M_ECI;
1655 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001656 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001657 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001658 goto out;
1659
1660 if (!likely(mdp->irq_enabled)) {
1661 sh_eth_write(ndev, 0, EESIPR);
1662 goto out;
1663 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001664
Sergei Shtylyov37191092013-06-19 23:30:23 +04001665 if (intr_status & EESR_RX_CHECK) {
1666 if (napi_schedule_prep(&mdp->napi)) {
1667 /* Mask Rx interrupts */
1668 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1669 EESIPR);
1670 __napi_schedule(&mdp->napi);
1671 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001672 netdev_warn(ndev,
1673 "ignoring interrupt, status 0x%08lx, mask 0x%08lx.\n",
1674 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001675 }
1676 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001677
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001678 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001679 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001680 /* Clear Tx interrupts */
1681 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1682
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 sh_eth_txfree(ndev);
1684 netif_wake_queue(ndev);
1685 }
1686
Sergei Shtylyov37191092013-06-19 23:30:23 +04001687 if (intr_status & cd->eesr_err_check) {
1688 /* Clear error interrupts */
1689 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1690
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001691 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001692 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001693
Ben Hutchings283e38d2015-01-22 12:44:08 +00001694out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001695 spin_unlock(&mdp->lock);
1696
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001697 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001698}
1699
Sergei Shtylyov37191092013-06-19 23:30:23 +04001700static int sh_eth_poll(struct napi_struct *napi, int budget)
1701{
1702 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1703 napi);
1704 struct net_device *ndev = napi->dev;
1705 int quota = budget;
1706 unsigned long intr_status;
1707
1708 for (;;) {
1709 intr_status = sh_eth_read(ndev, EESR);
1710 if (!(intr_status & EESR_RX_CHECK))
1711 break;
1712 /* Clear Rx interrupts */
1713 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1714
1715 if (sh_eth_rx(ndev, intr_status, &quota))
1716 goto out;
1717 }
1718
1719 napi_complete(napi);
1720
1721 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001722 if (mdp->irq_enabled)
1723 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001724out:
1725 return budget - quota;
1726}
1727
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001728/* PHY state control function */
1729static void sh_eth_adjust_link(struct net_device *ndev)
1730{
1731 struct sh_eth_private *mdp = netdev_priv(ndev);
1732 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001733 int new_state = 0;
1734
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001735 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001736 if (phydev->duplex != mdp->duplex) {
1737 new_state = 1;
1738 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001739 if (mdp->cd->set_duplex)
1740 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001741 }
1742
1743 if (phydev->speed != mdp->speed) {
1744 new_state = 1;
1745 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001746 if (mdp->cd->set_rate)
1747 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001748 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001749 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001750 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001751 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1752 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001753 new_state = 1;
1754 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001755 if (mdp->cd->no_psr || mdp->no_ether_link)
1756 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001757 }
1758 } else if (mdp->link) {
1759 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001760 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001761 mdp->speed = 0;
1762 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001763 if (mdp->cd->no_psr || mdp->no_ether_link)
1764 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001765 }
1766
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001767 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001768 phy_print_status(phydev);
1769}
1770
1771/* PHY init function */
1772static int sh_eth_phy_init(struct net_device *ndev)
1773{
Ben Dooks702eca02014-03-12 17:47:40 +00001774 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001775 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001776 struct phy_device *phydev = NULL;
1777
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001778 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001779 mdp->speed = 0;
1780 mdp->duplex = -1;
1781
1782 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001783 if (np) {
1784 struct device_node *pn;
1785
1786 pn = of_parse_phandle(np, "phy-handle", 0);
1787 phydev = of_phy_connect(ndev, pn,
1788 sh_eth_adjust_link, 0,
1789 mdp->phy_interface);
1790
1791 if (!phydev)
1792 phydev = ERR_PTR(-ENOENT);
1793 } else {
1794 char phy_id[MII_BUS_ID_SIZE + 3];
1795
1796 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1797 mdp->mii_bus->id, mdp->phy_id);
1798
1799 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1800 mdp->phy_interface);
1801 }
1802
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001804 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001805 return PTR_ERR(phydev);
1806 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001807
Sergei Shtylyovda246852014-03-15 03:29:14 +03001808 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1809 phydev->addr, phydev->irq, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001810
1811 mdp->phydev = phydev;
1812
1813 return 0;
1814}
1815
1816/* PHY control start function */
1817static int sh_eth_phy_start(struct net_device *ndev)
1818{
1819 struct sh_eth_private *mdp = netdev_priv(ndev);
1820 int ret;
1821
1822 ret = sh_eth_phy_init(ndev);
1823 if (ret)
1824 return ret;
1825
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001826 phy_start(mdp->phydev);
1827
1828 return 0;
1829}
1830
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001831static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001832 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001833{
1834 struct sh_eth_private *mdp = netdev_priv(ndev);
1835 unsigned long flags;
1836 int ret;
1837
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001838 if (!mdp->phydev)
1839 return -ENODEV;
1840
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001841 spin_lock_irqsave(&mdp->lock, flags);
1842 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1843 spin_unlock_irqrestore(&mdp->lock, flags);
1844
1845 return ret;
1846}
1847
1848static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001849 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001850{
1851 struct sh_eth_private *mdp = netdev_priv(ndev);
1852 unsigned long flags;
1853 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001854
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001855 if (!mdp->phydev)
1856 return -ENODEV;
1857
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001858 spin_lock_irqsave(&mdp->lock, flags);
1859
1860 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001861 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001862
1863 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1864 if (ret)
1865 goto error_exit;
1866
1867 if (ecmd->duplex == DUPLEX_FULL)
1868 mdp->duplex = 1;
1869 else
1870 mdp->duplex = 0;
1871
1872 if (mdp->cd->set_duplex)
1873 mdp->cd->set_duplex(ndev);
1874
1875error_exit:
1876 mdelay(1);
1877
1878 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001879 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001880
1881 spin_unlock_irqrestore(&mdp->lock, flags);
1882
1883 return ret;
1884}
1885
1886static int sh_eth_nway_reset(struct net_device *ndev)
1887{
1888 struct sh_eth_private *mdp = netdev_priv(ndev);
1889 unsigned long flags;
1890 int ret;
1891
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001892 if (!mdp->phydev)
1893 return -ENODEV;
1894
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001895 spin_lock_irqsave(&mdp->lock, flags);
1896 ret = phy_start_aneg(mdp->phydev);
1897 spin_unlock_irqrestore(&mdp->lock, flags);
1898
1899 return ret;
1900}
1901
1902static u32 sh_eth_get_msglevel(struct net_device *ndev)
1903{
1904 struct sh_eth_private *mdp = netdev_priv(ndev);
1905 return mdp->msg_enable;
1906}
1907
1908static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1909{
1910 struct sh_eth_private *mdp = netdev_priv(ndev);
1911 mdp->msg_enable = value;
1912}
1913
1914static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1915 "rx_current", "tx_current",
1916 "rx_dirty", "tx_dirty",
1917};
1918#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1919
1920static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1921{
1922 switch (sset) {
1923 case ETH_SS_STATS:
1924 return SH_ETH_STATS_LEN;
1925 default:
1926 return -EOPNOTSUPP;
1927 }
1928}
1929
1930static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001931 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001932{
1933 struct sh_eth_private *mdp = netdev_priv(ndev);
1934 int i = 0;
1935
1936 /* device-specific stats */
1937 data[i++] = mdp->cur_rx;
1938 data[i++] = mdp->cur_tx;
1939 data[i++] = mdp->dirty_rx;
1940 data[i++] = mdp->dirty_tx;
1941}
1942
1943static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1944{
1945 switch (stringset) {
1946 case ETH_SS_STATS:
1947 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001948 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001949 break;
1950 }
1951}
1952
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001953static void sh_eth_get_ringparam(struct net_device *ndev,
1954 struct ethtool_ringparam *ring)
1955{
1956 struct sh_eth_private *mdp = netdev_priv(ndev);
1957
1958 ring->rx_max_pending = RX_RING_MAX;
1959 ring->tx_max_pending = TX_RING_MAX;
1960 ring->rx_pending = mdp->num_rx_ring;
1961 ring->tx_pending = mdp->num_tx_ring;
1962}
1963
1964static int sh_eth_set_ringparam(struct net_device *ndev,
1965 struct ethtool_ringparam *ring)
1966{
1967 struct sh_eth_private *mdp = netdev_priv(ndev);
1968 int ret;
1969
1970 if (ring->tx_pending > TX_RING_MAX ||
1971 ring->rx_pending > RX_RING_MAX ||
1972 ring->tx_pending < TX_RING_MIN ||
1973 ring->rx_pending < RX_RING_MIN)
1974 return -EINVAL;
1975 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1976 return -EINVAL;
1977
1978 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00001979 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001980 netif_tx_disable(ndev);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001981
1982 /* Serialise with the interrupt handler and NAPI, then
1983 * disable interrupts. We have to clear the
1984 * irq_enabled flag first to ensure that interrupts
1985 * won't be re-enabled.
1986 */
1987 mdp->irq_enabled = false;
1988 synchronize_irq(ndev->irq);
1989 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001990 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001991
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001992 /* Stop the chip's Tx and Rx processes. */
1993 sh_eth_write(ndev, 0, EDTRR);
1994 sh_eth_write(ndev, 0, EDRRR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001995
Ben Hutchings084236d2015-01-22 12:41:34 +00001996 /* Free all the skbuffs in the Rx queue. */
1997 sh_eth_ring_free(ndev);
1998 /* Free DMA buffer */
1999 sh_eth_free_dma_buffer(mdp);
2000 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002001
2002 /* Set new parameters */
2003 mdp->num_rx_ring = ring->rx_pending;
2004 mdp->num_tx_ring = ring->tx_pending;
2005
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002006 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002007 ret = sh_eth_ring_init(ndev);
2008 if (ret < 0) {
2009 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2010 __func__);
2011 return ret;
2012 }
2013 ret = sh_eth_dev_init(ndev, false);
2014 if (ret < 0) {
2015 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2016 __func__);
2017 return ret;
2018 }
2019
Ben Hutchings283e38d2015-01-22 12:44:08 +00002020 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002021 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2022 /* Setting the Rx mode will start the Rx process. */
2023 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002024 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002025 }
2026
2027 return 0;
2028}
2029
stephen hemminger9b07be42012-01-04 12:59:49 +00002030static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002031 .get_settings = sh_eth_get_settings,
2032 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00002033 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002034 .get_msglevel = sh_eth_get_msglevel,
2035 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002036 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002037 .get_strings = sh_eth_get_strings,
2038 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2039 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002040 .get_ringparam = sh_eth_get_ringparam,
2041 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002042};
2043
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002044/* network device open function */
2045static int sh_eth_open(struct net_device *ndev)
2046{
2047 int ret = 0;
2048 struct sh_eth_private *mdp = netdev_priv(ndev);
2049
Magnus Dammbcd51492009-10-09 00:20:04 +00002050 pm_runtime_get_sync(&mdp->pdev->dev);
2051
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002052 napi_enable(&mdp->napi);
2053
Joe Perchesa0607fd2009-11-18 23:29:17 -08002054 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002055 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002056 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002057 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002058 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002059 }
2060
2061 /* Descriptor set */
2062 ret = sh_eth_ring_init(ndev);
2063 if (ret)
2064 goto out_free_irq;
2065
2066 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002067 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002068 if (ret)
2069 goto out_free_irq;
2070
2071 /* PHY control start*/
2072 ret = sh_eth_phy_start(ndev);
2073 if (ret)
2074 goto out_free_irq;
2075
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002076 mdp->is_opened = 1;
2077
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002078 return ret;
2079
2080out_free_irq:
2081 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002082out_napi_off:
2083 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002084 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002085 return ret;
2086}
2087
2088/* Timeout function */
2089static void sh_eth_tx_timeout(struct net_device *ndev)
2090{
2091 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002092 struct sh_eth_rxdesc *rxdesc;
2093 int i;
2094
2095 netif_stop_queue(ndev);
2096
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002097 netif_err(mdp, timer, ndev,
2098 "transmit timed out, status %8.8x, resetting...\n",
2099 (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002100
2101 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002102 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002103
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002104 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002105 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002106 rxdesc = &mdp->rx_ring[i];
2107 rxdesc->status = 0;
2108 rxdesc->addr = 0xBADF00D0;
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002109 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002110 mdp->rx_skbuff[i] = NULL;
2111 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002112 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002113 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002114 mdp->tx_skbuff[i] = NULL;
2115 }
2116
2117 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002118 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002119}
2120
2121/* Packet transmit function */
2122static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2123{
2124 struct sh_eth_private *mdp = netdev_priv(ndev);
2125 struct sh_eth_txdesc *txdesc;
2126 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002127 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002128
2129 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002130 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002131 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002132 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002133 netif_stop_queue(ndev);
2134 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002135 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002136 }
2137 }
2138 spin_unlock_irqrestore(&mdp->lock, flags);
2139
Ben Hutchingseebfb642015-01-22 12:40:13 +00002140 if (skb_padto(skb, ETH_ZLEN))
2141 return NETDEV_TX_OK;
2142
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002143 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002144 mdp->tx_skbuff[entry] = skb;
2145 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002146 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002147 if (!mdp->cd->hw_swap)
2148 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2149 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002150 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2151 DMA_TO_DEVICE);
Ben Hutchingseebfb642015-01-22 12:40:13 +00002152 txdesc->buffer_length = skb->len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002153
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002154 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002155 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002156 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002157 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002158
2159 mdp->cur_tx++;
2160
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002161 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2162 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002163
Patrick McHardy6ed10652009-06-23 06:03:08 +00002164 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002165}
2166
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002167static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2168{
2169 struct sh_eth_private *mdp = netdev_priv(ndev);
2170
2171 if (sh_eth_is_rz_fast_ether(mdp))
2172 return &ndev->stats;
2173
2174 if (!mdp->is_opened)
2175 return &ndev->stats;
2176
2177 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
2178 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
2179 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
2180 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
2181 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
2182 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
2183
2184 if (sh_eth_is_gether(mdp)) {
2185 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
2186 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
2187 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
2188 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
2189 } else {
2190 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
2191 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2192 }
2193
2194 return &ndev->stats;
2195}
2196
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002197/* device close function */
2198static int sh_eth_close(struct net_device *ndev)
2199{
2200 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002201
2202 netif_stop_queue(ndev);
2203
Ben Hutchings283e38d2015-01-22 12:44:08 +00002204 /* Serialise with the interrupt handler and NAPI, then disable
2205 * interrupts. We have to clear the irq_enabled flag first to
2206 * ensure that interrupts won't be re-enabled.
2207 */
2208 mdp->irq_enabled = false;
2209 synchronize_irq(ndev->irq);
2210 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002211 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002212
2213 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002214 sh_eth_write(ndev, 0, EDTRR);
2215 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002216
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002217 sh_eth_get_stats(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002218 /* PHY Disconnect */
2219 if (mdp->phydev) {
2220 phy_stop(mdp->phydev);
2221 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002222 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002223 }
2224
2225 free_irq(ndev->irq, ndev);
2226
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002227 /* Free all the skbuffs in the Rx queue. */
2228 sh_eth_ring_free(ndev);
2229
2230 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00002231 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002232
Magnus Dammbcd51492009-10-09 00:20:04 +00002233 pm_runtime_put_sync(&mdp->pdev->dev);
2234
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002235 mdp->is_opened = 0;
2236
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002237 return 0;
2238}
2239
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002240/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002241static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002242{
2243 struct sh_eth_private *mdp = netdev_priv(ndev);
2244 struct phy_device *phydev = mdp->phydev;
2245
2246 if (!netif_running(ndev))
2247 return -EINVAL;
2248
2249 if (!phydev)
2250 return -ENODEV;
2251
Richard Cochran28b04112010-07-17 08:48:55 +00002252 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002253}
2254
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002255/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2256static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2257 int entry)
2258{
2259 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2260}
2261
2262static u32 sh_eth_tsu_get_post_mask(int entry)
2263{
2264 return 0x0f << (28 - ((entry % 8) * 4));
2265}
2266
2267static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2268{
2269 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2270}
2271
2272static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2273 int entry)
2274{
2275 struct sh_eth_private *mdp = netdev_priv(ndev);
2276 u32 tmp;
2277 void *reg_offset;
2278
2279 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2280 tmp = ioread32(reg_offset);
2281 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2282}
2283
2284static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2285 int entry)
2286{
2287 struct sh_eth_private *mdp = netdev_priv(ndev);
2288 u32 post_mask, ref_mask, tmp;
2289 void *reg_offset;
2290
2291 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2292 post_mask = sh_eth_tsu_get_post_mask(entry);
2293 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2294
2295 tmp = ioread32(reg_offset);
2296 iowrite32(tmp & ~post_mask, reg_offset);
2297
2298 /* If other port enables, the function returns "true" */
2299 return tmp & ref_mask;
2300}
2301
2302static int sh_eth_tsu_busy(struct net_device *ndev)
2303{
2304 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2305 struct sh_eth_private *mdp = netdev_priv(ndev);
2306
2307 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2308 udelay(10);
2309 timeout--;
2310 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002311 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002312 return -ETIMEDOUT;
2313 }
2314 }
2315
2316 return 0;
2317}
2318
2319static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2320 const u8 *addr)
2321{
2322 u32 val;
2323
2324 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2325 iowrite32(val, reg);
2326 if (sh_eth_tsu_busy(ndev) < 0)
2327 return -EBUSY;
2328
2329 val = addr[4] << 8 | addr[5];
2330 iowrite32(val, reg + 4);
2331 if (sh_eth_tsu_busy(ndev) < 0)
2332 return -EBUSY;
2333
2334 return 0;
2335}
2336
2337static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2338{
2339 u32 val;
2340
2341 val = ioread32(reg);
2342 addr[0] = (val >> 24) & 0xff;
2343 addr[1] = (val >> 16) & 0xff;
2344 addr[2] = (val >> 8) & 0xff;
2345 addr[3] = val & 0xff;
2346 val = ioread32(reg + 4);
2347 addr[4] = (val >> 8) & 0xff;
2348 addr[5] = val & 0xff;
2349}
2350
2351
2352static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2353{
2354 struct sh_eth_private *mdp = netdev_priv(ndev);
2355 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2356 int i;
2357 u8 c_addr[ETH_ALEN];
2358
2359 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2360 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002361 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002362 return i;
2363 }
2364
2365 return -ENOENT;
2366}
2367
2368static int sh_eth_tsu_find_empty(struct net_device *ndev)
2369{
2370 u8 blank[ETH_ALEN];
2371 int entry;
2372
2373 memset(blank, 0, sizeof(blank));
2374 entry = sh_eth_tsu_find_entry(ndev, blank);
2375 return (entry < 0) ? -ENOMEM : entry;
2376}
2377
2378static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2379 int entry)
2380{
2381 struct sh_eth_private *mdp = netdev_priv(ndev);
2382 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2383 int ret;
2384 u8 blank[ETH_ALEN];
2385
2386 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2387 ~(1 << (31 - entry)), TSU_TEN);
2388
2389 memset(blank, 0, sizeof(blank));
2390 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2391 if (ret < 0)
2392 return ret;
2393 return 0;
2394}
2395
2396static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2397{
2398 struct sh_eth_private *mdp = netdev_priv(ndev);
2399 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2400 int i, ret;
2401
2402 if (!mdp->cd->tsu)
2403 return 0;
2404
2405 i = sh_eth_tsu_find_entry(ndev, addr);
2406 if (i < 0) {
2407 /* No entry found, create one */
2408 i = sh_eth_tsu_find_empty(ndev);
2409 if (i < 0)
2410 return -ENOMEM;
2411 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2412 if (ret < 0)
2413 return ret;
2414
2415 /* Enable the entry */
2416 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2417 (1 << (31 - i)), TSU_TEN);
2418 }
2419
2420 /* Entry found or created, enable POST */
2421 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2422
2423 return 0;
2424}
2425
2426static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2427{
2428 struct sh_eth_private *mdp = netdev_priv(ndev);
2429 int i, ret;
2430
2431 if (!mdp->cd->tsu)
2432 return 0;
2433
2434 i = sh_eth_tsu_find_entry(ndev, addr);
2435 if (i) {
2436 /* Entry found */
2437 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2438 goto done;
2439
2440 /* Disable the entry if both ports was disabled */
2441 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2442 if (ret < 0)
2443 return ret;
2444 }
2445done:
2446 return 0;
2447}
2448
2449static int sh_eth_tsu_purge_all(struct net_device *ndev)
2450{
2451 struct sh_eth_private *mdp = netdev_priv(ndev);
2452 int i, ret;
2453
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002454 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002455 return 0;
2456
2457 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2458 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2459 continue;
2460
2461 /* Disable the entry if both ports was disabled */
2462 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2463 if (ret < 0)
2464 return ret;
2465 }
2466
2467 return 0;
2468}
2469
2470static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2471{
2472 struct sh_eth_private *mdp = netdev_priv(ndev);
2473 u8 addr[ETH_ALEN];
2474 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2475 int i;
2476
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002477 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002478 return;
2479
2480 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2481 sh_eth_tsu_read_entry(reg_offset, addr);
2482 if (is_multicast_ether_addr(addr))
2483 sh_eth_tsu_del_entry(ndev, addr);
2484 }
2485}
2486
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002487/* Update promiscuous flag and multicast filter */
2488static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002489{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002490 struct sh_eth_private *mdp = netdev_priv(ndev);
2491 u32 ecmr_bits;
2492 int mcast_all = 0;
2493 unsigned long flags;
2494
2495 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002496 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002497 * Depending on ndev->flags, set PRM or clear MCT
2498 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002499 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2500 if (mdp->cd->tsu)
2501 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002502
2503 if (!(ndev->flags & IFF_MULTICAST)) {
2504 sh_eth_tsu_purge_mcast(ndev);
2505 mcast_all = 1;
2506 }
2507 if (ndev->flags & IFF_ALLMULTI) {
2508 sh_eth_tsu_purge_mcast(ndev);
2509 ecmr_bits &= ~ECMR_MCT;
2510 mcast_all = 1;
2511 }
2512
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002513 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002514 sh_eth_tsu_purge_all(ndev);
2515 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2516 } else if (mdp->cd->tsu) {
2517 struct netdev_hw_addr *ha;
2518 netdev_for_each_mc_addr(ha, ndev) {
2519 if (mcast_all && is_multicast_ether_addr(ha->addr))
2520 continue;
2521
2522 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2523 if (!mcast_all) {
2524 sh_eth_tsu_purge_mcast(ndev);
2525 ecmr_bits &= ~ECMR_MCT;
2526 mcast_all = 1;
2527 }
2528 }
2529 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002530 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002531
2532 /* update the ethernet mode */
2533 sh_eth_write(ndev, ecmr_bits, ECMR);
2534
2535 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002536}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002537
2538static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2539{
2540 if (!mdp->port)
2541 return TSU_VTAG0;
2542 else
2543 return TSU_VTAG1;
2544}
2545
Patrick McHardy80d5c362013-04-19 02:04:28 +00002546static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2547 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002548{
2549 struct sh_eth_private *mdp = netdev_priv(ndev);
2550 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2551
2552 if (unlikely(!mdp->cd->tsu))
2553 return -EPERM;
2554
2555 /* No filtering if vid = 0 */
2556 if (!vid)
2557 return 0;
2558
2559 mdp->vlan_num_ids++;
2560
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002561 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002562 * already enabled, the driver disables it and the filte
2563 */
2564 if (mdp->vlan_num_ids > 1) {
2565 /* disable VLAN filter */
2566 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2567 return 0;
2568 }
2569
2570 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2571 vtag_reg_index);
2572
2573 return 0;
2574}
2575
Patrick McHardy80d5c362013-04-19 02:04:28 +00002576static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2577 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002578{
2579 struct sh_eth_private *mdp = netdev_priv(ndev);
2580 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2581
2582 if (unlikely(!mdp->cd->tsu))
2583 return -EPERM;
2584
2585 /* No filtering if vid = 0 */
2586 if (!vid)
2587 return 0;
2588
2589 mdp->vlan_num_ids--;
2590 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2591
2592 return 0;
2593}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002594
2595/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002596static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002597{
Simon Hormandb893472014-01-17 09:22:28 +09002598 if (sh_eth_is_rz_fast_ether(mdp)) {
2599 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2600 return;
2601 }
2602
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002603 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2604 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2605 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2606 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2607 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2608 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2609 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2610 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2611 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2612 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002613 if (sh_eth_is_gether(mdp)) {
2614 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2615 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2616 } else {
2617 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2618 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2619 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002620 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2621 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2622 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2623 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2624 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2625 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2626 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002627}
2628
2629/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002630static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002631{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002632 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002633 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002634
2635 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002636 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002637
2638 return 0;
2639}
2640
2641/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002642static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002643 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002644{
2645 int ret, i;
2646 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002647 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002648 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002649
2650 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002651 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002652 if (!bitbang)
2653 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002654
2655 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002656 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002657 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002658 bitbang->mdi_msk = PIR_MDI;
2659 bitbang->mdo_msk = PIR_MDO;
2660 bitbang->mmd_msk = PIR_MMD;
2661 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002662 bitbang->ctrl.ops = &bb_ops;
2663
Stefan Weilc2e07b32010-08-03 19:44:52 +02002664 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002665 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002666 if (!mdp->mii_bus)
2667 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002668
2669 /* Hook up MII support for ethtool */
2670 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002671 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002672 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002673 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002674
2675 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002676 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2677 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002678 if (!mdp->mii_bus->irq) {
2679 ret = -ENOMEM;
2680 goto out_free_bus;
2681 }
2682
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002683 /* register MDIO bus */
2684 if (dev->of_node) {
2685 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002686 } else {
2687 for (i = 0; i < PHY_MAX_ADDR; i++)
2688 mdp->mii_bus->irq[i] = PHY_POLL;
2689 if (pd->phy_irq > 0)
2690 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2691
2692 ret = mdiobus_register(mdp->mii_bus);
2693 }
2694
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002695 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002696 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002697
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002698 return 0;
2699
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002700out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002701 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002702 return ret;
2703}
2704
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002705static const u16 *sh_eth_get_register_offset(int register_type)
2706{
2707 const u16 *reg_offset = NULL;
2708
2709 switch (register_type) {
2710 case SH_ETH_REG_GIGABIT:
2711 reg_offset = sh_eth_offset_gigabit;
2712 break;
Simon Hormandb893472014-01-17 09:22:28 +09002713 case SH_ETH_REG_FAST_RZ:
2714 reg_offset = sh_eth_offset_fast_rz;
2715 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002716 case SH_ETH_REG_FAST_RCAR:
2717 reg_offset = sh_eth_offset_fast_rcar;
2718 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002719 case SH_ETH_REG_FAST_SH4:
2720 reg_offset = sh_eth_offset_fast_sh4;
2721 break;
2722 case SH_ETH_REG_FAST_SH3_SH2:
2723 reg_offset = sh_eth_offset_fast_sh3_sh2;
2724 break;
2725 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002726 break;
2727 }
2728
2729 return reg_offset;
2730}
2731
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002732static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002733 .ndo_open = sh_eth_open,
2734 .ndo_stop = sh_eth_close,
2735 .ndo_start_xmit = sh_eth_start_xmit,
2736 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002737 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002738 .ndo_tx_timeout = sh_eth_tx_timeout,
2739 .ndo_do_ioctl = sh_eth_do_ioctl,
2740 .ndo_validate_addr = eth_validate_addr,
2741 .ndo_set_mac_address = eth_mac_addr,
2742 .ndo_change_mtu = eth_change_mtu,
2743};
2744
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002745static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2746 .ndo_open = sh_eth_open,
2747 .ndo_stop = sh_eth_close,
2748 .ndo_start_xmit = sh_eth_start_xmit,
2749 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002750 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002751 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2752 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2753 .ndo_tx_timeout = sh_eth_tx_timeout,
2754 .ndo_do_ioctl = sh_eth_do_ioctl,
2755 .ndo_validate_addr = eth_validate_addr,
2756 .ndo_set_mac_address = eth_mac_addr,
2757 .ndo_change_mtu = eth_change_mtu,
2758};
2759
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002760#ifdef CONFIG_OF
2761static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2762{
2763 struct device_node *np = dev->of_node;
2764 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002765 const char *mac_addr;
2766
2767 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2768 if (!pdata)
2769 return NULL;
2770
2771 pdata->phy_interface = of_get_phy_mode(np);
2772
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002773 mac_addr = of_get_mac_address(np);
2774 if (mac_addr)
2775 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2776
2777 pdata->no_ether_link =
2778 of_property_read_bool(np, "renesas,no-ether-link");
2779 pdata->ether_link_active_low =
2780 of_property_read_bool(np, "renesas,ether-link-active-low");
2781
2782 return pdata;
2783}
2784
2785static const struct of_device_id sh_eth_match_table[] = {
2786 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
2787 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
2788 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
2789 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
2790 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002791 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002792 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002793 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
2794 { }
2795};
2796MODULE_DEVICE_TABLE(of, sh_eth_match_table);
2797#else
2798static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2799{
2800 return NULL;
2801}
2802#endif
2803
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002804static int sh_eth_drv_probe(struct platform_device *pdev)
2805{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002806 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002807 struct resource *res;
2808 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002809 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09002810 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002811 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002812
2813 /* get base addr */
2814 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002815
2816 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01002817 if (!ndev)
2818 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002819
Ben Dooksb5893a02014-03-21 12:09:14 +01002820 pm_runtime_enable(&pdev->dev);
2821 pm_runtime_get_sync(&pdev->dev);
2822
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002823 devno = pdev->id;
2824 if (devno < 0)
2825 devno = 0;
2826
2827 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002828 ret = platform_get_irq(pdev, 0);
2829 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002830 ret = -ENODEV;
2831 goto out_release;
2832 }
roel kluincc3c0802008-09-10 19:22:44 +02002833 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002834
2835 SET_NETDEV_DEV(ndev, &pdev->dev);
2836
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002837 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002838 mdp->num_tx_ring = TX_RING_SIZE;
2839 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002840 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2841 if (IS_ERR(mdp->addr)) {
2842 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002843 goto out_release;
2844 }
2845
Varka Bhadramc9608042014-10-24 07:42:09 +05302846 ndev->base_addr = res->start;
2847
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002848 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002849 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002850
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002851 if (pdev->dev.of_node)
2852 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03002853 if (!pd) {
2854 dev_err(&pdev->dev, "no platform data\n");
2855 ret = -EINVAL;
2856 goto out_release;
2857 }
2858
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002859 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002860 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002861 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002862 /* EDMAC endian */
2863 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002864 mdp->no_ether_link = pd->no_ether_link;
2865 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002866
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002867 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002868 if (id) {
2869 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
2870 } else {
2871 const struct of_device_id *match;
2872
2873 match = of_match_device(of_match_ptr(sh_eth_match_table),
2874 &pdev->dev);
2875 mdp->cd = (struct sh_eth_cpu_data *)match->data;
2876 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04002877 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03002878 if (!mdp->reg_offset) {
2879 dev_err(&pdev->dev, "Unknown register type (%d)\n",
2880 mdp->cd->register_type);
2881 ret = -EINVAL;
2882 goto out_release;
2883 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002884 sh_eth_set_default_cpu_data(mdp->cd);
2885
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002886 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002887 if (mdp->cd->tsu)
2888 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
2889 else
2890 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002891 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002892 ndev->watchdog_timeo = TX_TIMEOUT;
2893
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002894 /* debug message level */
2895 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002896
2897 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002898 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002899 if (!is_valid_ether_addr(ndev->dev_addr)) {
2900 dev_warn(&pdev->dev,
2901 "no valid MAC address supplied, using a random one.\n");
2902 eth_hw_addr_random(ndev);
2903 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002904
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002905 /* ioremap the TSU registers */
2906 if (mdp->cd->tsu) {
2907 struct resource *rtsu;
2908 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002909 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2910 if (IS_ERR(mdp->tsu_addr)) {
2911 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002912 goto out_release;
2913 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002914 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002915 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002916 }
2917
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002918 /* initialize first or needed device */
2919 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002920 if (mdp->cd->chip_reset)
2921 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002922
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002923 if (mdp->cd->tsu) {
2924 /* TSU init (Init only)*/
2925 sh_eth_tsu_init(mdp);
2926 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002927 }
2928
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09002929 if (mdp->cd->rmiimode)
2930 sh_eth_write(ndev, 0x1, RMIIMODE);
2931
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002932 /* MDIO bus init */
2933 ret = sh_mdio_init(mdp, pd);
2934 if (ret) {
2935 dev_err(&ndev->dev, "failed to initialise MDIO\n");
2936 goto out_release;
2937 }
2938
Sergei Shtylyov37191092013-06-19 23:30:23 +04002939 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
2940
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002941 /* network device register */
2942 ret = register_netdev(ndev);
2943 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04002944 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002945
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002946 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03002947 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
2948 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002949
Ben Dooksb5893a02014-03-21 12:09:14 +01002950 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002951 platform_set_drvdata(pdev, ndev);
2952
2953 return ret;
2954
Sergei Shtylyov37191092013-06-19 23:30:23 +04002955out_napi_del:
2956 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002957 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002958
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002959out_release:
2960 /* net_dev free */
2961 if (ndev)
2962 free_netdev(ndev);
2963
Ben Dooksb5893a02014-03-21 12:09:14 +01002964 pm_runtime_put(&pdev->dev);
2965 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002966 return ret;
2967}
2968
2969static int sh_eth_drv_remove(struct platform_device *pdev)
2970{
2971 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002972 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002973
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002974 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002975 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002976 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00002977 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002978 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002979
2980 return 0;
2981}
2982
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002983#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002984static int sh_eth_runtime_nop(struct device *dev)
2985{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002986 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00002987 * and ->runtime_resume(). Simply returns success.
2988 *
2989 * This driver re-initializes all registers after
2990 * pm_runtime_get_sync() anyway so there is no need
2991 * to save and restore registers here.
2992 */
2993 return 0;
2994}
2995
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002996static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002997 .runtime_suspend = sh_eth_runtime_nop,
2998 .runtime_resume = sh_eth_runtime_nop,
2999};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003000#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3001#else
3002#define SH_ETH_PM_OPS NULL
3003#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003004
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003005static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003006 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003007 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003008 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003009 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003010 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3011 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003012 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Simon Hormandb893472014-01-17 09:22:28 +09003013 { "r7s72100-ether", (kernel_ulong_t)&r7s72100_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00003014 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00003015 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyov94a12b12013-12-08 02:59:18 +03003016 { "r8a7790-ether", (kernel_ulong_t)&r8a779x_data },
3017 { "r8a7791-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003018 { "r8a7793-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003019 { "r8a7794-ether", (kernel_ulong_t)&r8a779x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003020 { }
3021};
3022MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3023
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003024static struct platform_driver sh_eth_driver = {
3025 .probe = sh_eth_drv_probe,
3026 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003027 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003028 .driver = {
3029 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003030 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003031 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003032 },
3033};
3034
Axel Lindb62f682011-11-27 16:44:17 +00003035module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003036
3037MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3038MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3039MODULE_LICENSE("GPL v2");