blob: 64e72ebdf8c51609746f636cd86f83983acc4751 [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
Ben Hutchings33657112015-02-26 20:34:14 +000055#define SH_ETH_OFFSET_DEFAULTS \
56 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
57
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000058static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +000059 SH_ETH_OFFSET_DEFAULTS,
60
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000061 [EDSR] = 0x0000,
62 [EDMR] = 0x0400,
63 [EDTRR] = 0x0408,
64 [EDRRR] = 0x0410,
65 [EESR] = 0x0428,
66 [EESIPR] = 0x0430,
67 [TDLAR] = 0x0010,
68 [TDFAR] = 0x0014,
69 [TDFXR] = 0x0018,
70 [TDFFR] = 0x001c,
71 [RDLAR] = 0x0030,
72 [RDFAR] = 0x0034,
73 [RDFXR] = 0x0038,
74 [RDFFR] = 0x003c,
75 [TRSCER] = 0x0438,
76 [RMFCR] = 0x0440,
77 [TFTR] = 0x0448,
78 [FDR] = 0x0450,
79 [RMCR] = 0x0458,
80 [RPADIR] = 0x0460,
81 [FCFTR] = 0x0468,
82 [CSMR] = 0x04E4,
83
84 [ECMR] = 0x0500,
85 [ECSR] = 0x0510,
86 [ECSIPR] = 0x0518,
87 [PIR] = 0x0520,
88 [PSR] = 0x0528,
89 [PIPR] = 0x052c,
90 [RFLR] = 0x0508,
91 [APR] = 0x0554,
92 [MPR] = 0x0558,
93 [PFTCR] = 0x055c,
94 [PFRCR] = 0x0560,
95 [TPAUSER] = 0x0564,
96 [GECMR] = 0x05b0,
97 [BCULR] = 0x05b4,
98 [MAHR] = 0x05c0,
99 [MALR] = 0x05c8,
100 [TROCR] = 0x0700,
101 [CDCR] = 0x0708,
102 [LCCR] = 0x0710,
103 [CEFCR] = 0x0740,
104 [FRECR] = 0x0748,
105 [TSFRCR] = 0x0750,
106 [TLFRCR] = 0x0758,
107 [RFCR] = 0x0760,
108 [CERCR] = 0x0768,
109 [CEECR] = 0x0770,
110 [MAFCR] = 0x0778,
111 [RMII_MII] = 0x0790,
112
113 [ARSTR] = 0x0000,
114 [TSU_CTRST] = 0x0004,
115 [TSU_FWEN0] = 0x0010,
116 [TSU_FWEN1] = 0x0014,
117 [TSU_FCM] = 0x0018,
118 [TSU_BSYSL0] = 0x0020,
119 [TSU_BSYSL1] = 0x0024,
120 [TSU_PRISL0] = 0x0028,
121 [TSU_PRISL1] = 0x002c,
122 [TSU_FWSL0] = 0x0030,
123 [TSU_FWSL1] = 0x0034,
124 [TSU_FWSLC] = 0x0038,
125 [TSU_QTAG0] = 0x0040,
126 [TSU_QTAG1] = 0x0044,
127 [TSU_FWSR] = 0x0050,
128 [TSU_FWINMK] = 0x0054,
129 [TSU_ADQT0] = 0x0048,
130 [TSU_ADQT1] = 0x004c,
131 [TSU_VTAG0] = 0x0058,
132 [TSU_VTAG1] = 0x005c,
133 [TSU_ADSBSY] = 0x0060,
134 [TSU_TEN] = 0x0064,
135 [TSU_POST1] = 0x0070,
136 [TSU_POST2] = 0x0074,
137 [TSU_POST3] = 0x0078,
138 [TSU_POST4] = 0x007c,
139 [TSU_ADRH0] = 0x0100,
140 [TSU_ADRL0] = 0x0104,
141 [TSU_ADRH31] = 0x01f8,
142 [TSU_ADRL31] = 0x01fc,
143
144 [TXNLCR0] = 0x0080,
145 [TXALCR0] = 0x0084,
146 [RXNLCR0] = 0x0088,
147 [RXALCR0] = 0x008c,
148 [FWNLCR0] = 0x0090,
149 [FWALCR0] = 0x0094,
150 [TXNLCR1] = 0x00a0,
151 [TXALCR1] = 0x00a0,
152 [RXNLCR1] = 0x00a8,
153 [RXALCR1] = 0x00ac,
154 [FWNLCR1] = 0x00b0,
155 [FWALCR1] = 0x00b4,
156};
157
Simon Hormandb893472014-01-17 09:22:28 +0900158static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000159 SH_ETH_OFFSET_DEFAULTS,
160
Simon Hormandb893472014-01-17 09:22:28 +0900161 [EDSR] = 0x0000,
162 [EDMR] = 0x0400,
163 [EDTRR] = 0x0408,
164 [EDRRR] = 0x0410,
165 [EESR] = 0x0428,
166 [EESIPR] = 0x0430,
167 [TDLAR] = 0x0010,
168 [TDFAR] = 0x0014,
169 [TDFXR] = 0x0018,
170 [TDFFR] = 0x001c,
171 [RDLAR] = 0x0030,
172 [RDFAR] = 0x0034,
173 [RDFXR] = 0x0038,
174 [RDFFR] = 0x003c,
175 [TRSCER] = 0x0438,
176 [RMFCR] = 0x0440,
177 [TFTR] = 0x0448,
178 [FDR] = 0x0450,
179 [RMCR] = 0x0458,
180 [RPADIR] = 0x0460,
181 [FCFTR] = 0x0468,
182 [CSMR] = 0x04E4,
183
184 [ECMR] = 0x0500,
185 [RFLR] = 0x0508,
186 [ECSR] = 0x0510,
187 [ECSIPR] = 0x0518,
188 [PIR] = 0x0520,
189 [APR] = 0x0554,
190 [MPR] = 0x0558,
191 [PFTCR] = 0x055c,
192 [PFRCR] = 0x0560,
193 [TPAUSER] = 0x0564,
194 [MAHR] = 0x05c0,
195 [MALR] = 0x05c8,
196 [CEFCR] = 0x0740,
197 [FRECR] = 0x0748,
198 [TSFRCR] = 0x0750,
199 [TLFRCR] = 0x0758,
200 [RFCR] = 0x0760,
201 [MAFCR] = 0x0778,
202
203 [ARSTR] = 0x0000,
204 [TSU_CTRST] = 0x0004,
205 [TSU_VTAG0] = 0x0058,
206 [TSU_ADSBSY] = 0x0060,
207 [TSU_TEN] = 0x0064,
208 [TSU_ADRH0] = 0x0100,
209 [TSU_ADRL0] = 0x0104,
210 [TSU_ADRH31] = 0x01f8,
211 [TSU_ADRL31] = 0x01fc,
212
213 [TXNLCR0] = 0x0080,
214 [TXALCR0] = 0x0084,
215 [RXNLCR0] = 0x0088,
216 [RXALCR0] = 0x008C,
217};
218
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000219static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000220 SH_ETH_OFFSET_DEFAULTS,
221
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000222 [ECMR] = 0x0300,
223 [RFLR] = 0x0308,
224 [ECSR] = 0x0310,
225 [ECSIPR] = 0x0318,
226 [PIR] = 0x0320,
227 [PSR] = 0x0328,
228 [RDMLR] = 0x0340,
229 [IPGR] = 0x0350,
230 [APR] = 0x0354,
231 [MPR] = 0x0358,
232 [RFCF] = 0x0360,
233 [TPAUSER] = 0x0364,
234 [TPAUSECR] = 0x0368,
235 [MAHR] = 0x03c0,
236 [MALR] = 0x03c8,
237 [TROCR] = 0x03d0,
238 [CDCR] = 0x03d4,
239 [LCCR] = 0x03d8,
240 [CNDCR] = 0x03dc,
241 [CEFCR] = 0x03e4,
242 [FRECR] = 0x03e8,
243 [TSFRCR] = 0x03ec,
244 [TLFRCR] = 0x03f0,
245 [RFCR] = 0x03f4,
246 [MAFCR] = 0x03f8,
247
248 [EDMR] = 0x0200,
249 [EDTRR] = 0x0208,
250 [EDRRR] = 0x0210,
251 [TDLAR] = 0x0218,
252 [RDLAR] = 0x0220,
253 [EESR] = 0x0228,
254 [EESIPR] = 0x0230,
255 [TRSCER] = 0x0238,
256 [RMFCR] = 0x0240,
257 [TFTR] = 0x0248,
258 [FDR] = 0x0250,
259 [RMCR] = 0x0258,
260 [TFUCR] = 0x0264,
261 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900262 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000263 [FCFTR] = 0x0270,
264 [TRIMD] = 0x027c,
265};
266
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000267static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000268 SH_ETH_OFFSET_DEFAULTS,
269
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000270 [ECMR] = 0x0100,
271 [RFLR] = 0x0108,
272 [ECSR] = 0x0110,
273 [ECSIPR] = 0x0118,
274 [PIR] = 0x0120,
275 [PSR] = 0x0128,
276 [RDMLR] = 0x0140,
277 [IPGR] = 0x0150,
278 [APR] = 0x0154,
279 [MPR] = 0x0158,
280 [TPAUSER] = 0x0164,
281 [RFCF] = 0x0160,
282 [TPAUSECR] = 0x0168,
283 [BCFRR] = 0x016c,
284 [MAHR] = 0x01c0,
285 [MALR] = 0x01c8,
286 [TROCR] = 0x01d0,
287 [CDCR] = 0x01d4,
288 [LCCR] = 0x01d8,
289 [CNDCR] = 0x01dc,
290 [CEFCR] = 0x01e4,
291 [FRECR] = 0x01e8,
292 [TSFRCR] = 0x01ec,
293 [TLFRCR] = 0x01f0,
294 [RFCR] = 0x01f4,
295 [MAFCR] = 0x01f8,
296 [RTRATE] = 0x01fc,
297
298 [EDMR] = 0x0000,
299 [EDTRR] = 0x0008,
300 [EDRRR] = 0x0010,
301 [TDLAR] = 0x0018,
302 [RDLAR] = 0x0020,
303 [EESR] = 0x0028,
304 [EESIPR] = 0x0030,
305 [TRSCER] = 0x0038,
306 [RMFCR] = 0x0040,
307 [TFTR] = 0x0048,
308 [FDR] = 0x0050,
309 [RMCR] = 0x0058,
310 [TFUCR] = 0x0064,
311 [RFOCR] = 0x0068,
312 [FCFTR] = 0x0070,
313 [RPADIR] = 0x0078,
314 [TRIMD] = 0x007c,
315 [RBWAR] = 0x00c8,
316 [RDFAR] = 0x00cc,
317 [TBRAR] = 0x00d4,
318 [TDFAR] = 0x00d8,
319};
320
321static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000322 SH_ETH_OFFSET_DEFAULTS,
323
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400324 [EDMR] = 0x0000,
325 [EDTRR] = 0x0004,
326 [EDRRR] = 0x0008,
327 [TDLAR] = 0x000c,
328 [RDLAR] = 0x0010,
329 [EESR] = 0x0014,
330 [EESIPR] = 0x0018,
331 [TRSCER] = 0x001c,
332 [RMFCR] = 0x0020,
333 [TFTR] = 0x0024,
334 [FDR] = 0x0028,
335 [RMCR] = 0x002c,
336 [EDOCR] = 0x0030,
337 [FCFTR] = 0x0034,
338 [RPADIR] = 0x0038,
339 [TRIMD] = 0x003c,
340 [RBWAR] = 0x0040,
341 [RDFAR] = 0x0044,
342 [TBRAR] = 0x004c,
343 [TDFAR] = 0x0050,
344
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000345 [ECMR] = 0x0160,
346 [ECSR] = 0x0164,
347 [ECSIPR] = 0x0168,
348 [PIR] = 0x016c,
349 [MAHR] = 0x0170,
350 [MALR] = 0x0174,
351 [RFLR] = 0x0178,
352 [PSR] = 0x017c,
353 [TROCR] = 0x0180,
354 [CDCR] = 0x0184,
355 [LCCR] = 0x0188,
356 [CNDCR] = 0x018c,
357 [CEFCR] = 0x0194,
358 [FRECR] = 0x0198,
359 [TSFRCR] = 0x019c,
360 [TLFRCR] = 0x01a0,
361 [RFCR] = 0x01a4,
362 [MAFCR] = 0x01a8,
363 [IPGR] = 0x01b4,
364 [APR] = 0x01b8,
365 [MPR] = 0x01bc,
366 [TPAUSER] = 0x01c4,
367 [BCFR] = 0x01cc,
368
369 [ARSTR] = 0x0000,
370 [TSU_CTRST] = 0x0004,
371 [TSU_FWEN0] = 0x0010,
372 [TSU_FWEN1] = 0x0014,
373 [TSU_FCM] = 0x0018,
374 [TSU_BSYSL0] = 0x0020,
375 [TSU_BSYSL1] = 0x0024,
376 [TSU_PRISL0] = 0x0028,
377 [TSU_PRISL1] = 0x002c,
378 [TSU_FWSL0] = 0x0030,
379 [TSU_FWSL1] = 0x0034,
380 [TSU_FWSLC] = 0x0038,
381 [TSU_QTAGM0] = 0x0040,
382 [TSU_QTAGM1] = 0x0044,
383 [TSU_ADQT0] = 0x0048,
384 [TSU_ADQT1] = 0x004c,
385 [TSU_FWSR] = 0x0050,
386 [TSU_FWINMK] = 0x0054,
387 [TSU_ADSBSY] = 0x0060,
388 [TSU_TEN] = 0x0064,
389 [TSU_POST1] = 0x0070,
390 [TSU_POST2] = 0x0074,
391 [TSU_POST3] = 0x0078,
392 [TSU_POST4] = 0x007c,
393
394 [TXNLCR0] = 0x0080,
395 [TXALCR0] = 0x0084,
396 [RXNLCR0] = 0x0088,
397 [RXALCR0] = 0x008c,
398 [FWNLCR0] = 0x0090,
399 [FWALCR0] = 0x0094,
400 [TXNLCR1] = 0x00a0,
401 [TXALCR1] = 0x00a0,
402 [RXNLCR1] = 0x00a8,
403 [RXALCR1] = 0x00ac,
404 [FWNLCR1] = 0x00b0,
405 [FWALCR1] = 0x00b4,
406
407 [TSU_ADRH0] = 0x0100,
408 [TSU_ADRL0] = 0x0104,
409 [TSU_ADRL31] = 0x01fc,
410};
411
Ben Hutchings740c7f32015-01-27 00:49:32 +0000412static void sh_eth_rcv_snd_disable(struct net_device *ndev);
413static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
414
Simon Horman504c8ca2014-01-17 09:22:27 +0900415static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000416{
Simon Horman504c8ca2014-01-17 09:22:27 +0900417 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000418}
419
Simon Hormandb893472014-01-17 09:22:28 +0900420static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
421{
422 return mdp->reg_offset == sh_eth_offset_fast_rz;
423}
424
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400425static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000426{
427 u32 value = 0x0;
428 struct sh_eth_private *mdp = netdev_priv(ndev);
429
430 switch (mdp->phy_interface) {
431 case PHY_INTERFACE_MODE_GMII:
432 value = 0x2;
433 break;
434 case PHY_INTERFACE_MODE_MII:
435 value = 0x1;
436 break;
437 case PHY_INTERFACE_MODE_RMII:
438 value = 0x0;
439 break;
440 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300441 netdev_warn(ndev,
442 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000443 value = 0x1;
444 break;
445 }
446
447 sh_eth_write(ndev, value, RMII_MII);
448}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000449
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400450static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000451{
452 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000453
454 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000455 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000456 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000457 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000458}
459
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000460/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000461static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000462{
463 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000464
465 switch (mdp->speed) {
466 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000467 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000468 break;
469 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000470 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
471 break;
472 default:
473 break;
474 }
475}
476
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000477/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000478static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000479 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000480 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000481
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400482 .register_type = SH_ETH_REG_FAST_RCAR,
483
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000484 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
485 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
486 .eesipr_value = 0x01ff009f,
487
488 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400489 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
490 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
491 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900492 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000493
494 .apr = 1,
495 .mpr = 1,
496 .tpauser = 1,
497 .hw_swap = 1,
498};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000499
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300500/* R8A7790/1 */
501static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900502 .set_duplex = sh_eth_set_duplex,
503 .set_rate = sh_eth_set_rate_r8a777x,
504
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400505 .register_type = SH_ETH_REG_FAST_RCAR,
506
Simon Hormane18dbf72013-07-23 10:18:05 +0900507 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
508 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
509 .eesipr_value = 0x01ff009f,
510
511 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900512 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
513 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
514 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900515 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900516
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100517 .trscer_err_mask = DESC_I_RINT8,
518
Simon Hormane18dbf72013-07-23 10:18:05 +0900519 .apr = 1,
520 .mpr = 1,
521 .tpauser = 1,
522 .hw_swap = 1,
523 .rmiimode = 1,
524};
525
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000526static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000527{
528 struct sh_eth_private *mdp = netdev_priv(ndev);
529
530 switch (mdp->speed) {
531 case 10: /* 10BASE */
532 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
533 break;
534 case 100:/* 100BASE */
535 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000536 break;
537 default:
538 break;
539 }
540}
541
542/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000543static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000544 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000545 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000546
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400547 .register_type = SH_ETH_REG_FAST_SH4,
548
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000549 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
550 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400551 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000552
553 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400554 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
555 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
556 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000557
558 .apr = 1,
559 .mpr = 1,
560 .tpauser = 1,
561 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800562 .rpadir = 1,
563 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000564};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000565
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000566static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000567{
568 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000569
570 switch (mdp->speed) {
571 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000572 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000573 break;
574 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000575 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000576 break;
577 default:
578 break;
579 }
580}
581
582/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000583static struct sh_eth_cpu_data sh7757_data = {
584 .set_duplex = sh_eth_set_duplex,
585 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000586
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400587 .register_type = SH_ETH_REG_FAST_SH4,
588
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000589 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000590
591 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400592 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
593 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
594 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000595
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000596 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000597 .apr = 1,
598 .mpr = 1,
599 .tpauser = 1,
600 .hw_swap = 1,
601 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000602 .rpadir = 1,
603 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000604};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000605
David S. Millere403d292013-06-07 23:40:41 -0700606#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000607#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
608#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
609static void sh_eth_chip_reset_giga(struct net_device *ndev)
610{
611 int i;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100612 u32 mahr[2], malr[2];
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000613
614 /* save MAHR and MALR */
615 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000616 malr[i] = ioread32((void *)GIGA_MALR(i));
617 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000618 }
619
620 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000621 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000622 mdelay(1);
623
624 /* restore MAHR and MALR */
625 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000626 iowrite32(malr[i], (void *)GIGA_MALR(i));
627 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000628 }
629}
630
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000631static void sh_eth_set_rate_giga(struct net_device *ndev)
632{
633 struct sh_eth_private *mdp = netdev_priv(ndev);
634
635 switch (mdp->speed) {
636 case 10: /* 10BASE */
637 sh_eth_write(ndev, 0x00000000, GECMR);
638 break;
639 case 100:/* 100BASE */
640 sh_eth_write(ndev, 0x00000010, GECMR);
641 break;
642 case 1000: /* 1000BASE */
643 sh_eth_write(ndev, 0x00000020, GECMR);
644 break;
645 default:
646 break;
647 }
648}
649
650/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000651static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000652 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000653 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000654 .set_rate = sh_eth_set_rate_giga,
655
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400656 .register_type = SH_ETH_REG_GIGABIT,
657
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000658 .ecsr_value = ECSR_ICD | ECSR_MPD,
659 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
660 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
661
662 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400663 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
664 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
665 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000666 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000667
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000668 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000669 .apr = 1,
670 .mpr = 1,
671 .tpauser = 1,
672 .bculr = 1,
673 .hw_swap = 1,
674 .rpadir = 1,
675 .rpadir_value = 2 << 16,
676 .no_trimd = 1,
677 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000678 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000679};
680
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000681static void sh_eth_chip_reset(struct net_device *ndev)
682{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000683 struct sh_eth_private *mdp = netdev_priv(ndev);
684
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000685 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000686 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000687 mdelay(1);
688}
689
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000690static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000691{
692 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000693
694 switch (mdp->speed) {
695 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000696 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000697 break;
698 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000699 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000700 break;
701 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000702 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000703 break;
704 default:
705 break;
706 }
707}
708
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000709/* SH7734 */
710static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000711 .chip_reset = sh_eth_chip_reset,
712 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000713 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000714
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400715 .register_type = SH_ETH_REG_GIGABIT,
716
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000717 .ecsr_value = ECSR_ICD | ECSR_MPD,
718 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
719 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
720
721 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400722 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
723 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
724 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000725
726 .apr = 1,
727 .mpr = 1,
728 .tpauser = 1,
729 .bculr = 1,
730 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000731 .no_trimd = 1,
732 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000733 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000734 .hw_crc = 1,
735 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000736};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000737
738/* SH7763 */
739static struct sh_eth_cpu_data sh7763_data = {
740 .chip_reset = sh_eth_chip_reset,
741 .set_duplex = sh_eth_set_duplex,
742 .set_rate = sh_eth_set_rate_gether,
743
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400744 .register_type = SH_ETH_REG_GIGABIT,
745
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000746 .ecsr_value = ECSR_ICD | ECSR_MPD,
747 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
748 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
749
750 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300751 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
752 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000753 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000754
755 .apr = 1,
756 .mpr = 1,
757 .tpauser = 1,
758 .bculr = 1,
759 .hw_swap = 1,
760 .no_trimd = 1,
761 .no_ade = 1,
762 .tsu = 1,
763 .irq_flags = IRQF_SHARED,
764};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000765
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000766static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000767{
768 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000769
770 /* reset device */
771 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
772 mdelay(1);
773
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000774 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000775}
776
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000777/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000778static struct sh_eth_cpu_data r8a7740_data = {
779 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000780 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000781 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000782
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400783 .register_type = SH_ETH_REG_GIGABIT,
784
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000785 .ecsr_value = ECSR_ICD | ECSR_MPD,
786 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
787 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
788
789 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400790 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
791 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
792 EESR_TDE | EESR_ECI,
Simon Hormancc235282013-10-10 14:51:16 +0900793 .fdr_value = 0x0000070f,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000794
795 .apr = 1,
796 .mpr = 1,
797 .tpauser = 1,
798 .bculr = 1,
799 .hw_swap = 1,
Simon Hormancc235282013-10-10 14:51:16 +0900800 .rpadir = 1,
801 .rpadir_value = 2 << 16,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000802 .no_trimd = 1,
803 .no_ade = 1,
804 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000805 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400806 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000807};
808
Simon Hormandb893472014-01-17 09:22:28 +0900809/* R7S72100 */
810static struct sh_eth_cpu_data r7s72100_data = {
811 .chip_reset = sh_eth_chip_reset,
812 .set_duplex = sh_eth_set_duplex,
813
814 .register_type = SH_ETH_REG_FAST_RZ,
815
816 .ecsr_value = ECSR_ICD,
817 .ecsipr_value = ECSIPR_ICDIP,
818 .eesipr_value = 0xff7f009f,
819
820 .tx_check = EESR_TC1 | EESR_FTC,
821 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
822 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
823 EESR_TDE | EESR_ECI,
824 .fdr_value = 0x0000070f,
Simon Hormandb893472014-01-17 09:22:28 +0900825
826 .no_psr = 1,
827 .apr = 1,
828 .mpr = 1,
829 .tpauser = 1,
830 .hw_swap = 1,
831 .rpadir = 1,
832 .rpadir_value = 2 << 16,
833 .no_trimd = 1,
834 .no_ade = 1,
835 .hw_crc = 1,
836 .tsu = 1,
837 .shift_rd0 = 1,
838};
839
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000840static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400841 .register_type = SH_ETH_REG_FAST_SH3_SH2,
842
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000843 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
844
845 .apr = 1,
846 .mpr = 1,
847 .tpauser = 1,
848 .hw_swap = 1,
849};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000850
851static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400852 .register_type = SH_ETH_REG_FAST_SH3_SH2,
853
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000854 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000855 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000856};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000857
858static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
859{
860 if (!cd->ecsr_value)
861 cd->ecsr_value = DEFAULT_ECSR_INIT;
862
863 if (!cd->ecsipr_value)
864 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
865
866 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300867 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000868 DEFAULT_FIFO_F_D_RFD;
869
870 if (!cd->fdr_value)
871 cd->fdr_value = DEFAULT_FDR_INIT;
872
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000873 if (!cd->tx_check)
874 cd->tx_check = DEFAULT_TX_CHECK;
875
876 if (!cd->eesr_err_check)
877 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900878
879 if (!cd->trscer_err_mask)
880 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000881}
882
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000883static int sh_eth_check_reset(struct net_device *ndev)
884{
885 int ret = 0;
886 int cnt = 100;
887
888 while (cnt > 0) {
889 if (!(sh_eth_read(ndev, EDMR) & 0x3))
890 break;
891 mdelay(1);
892 cnt--;
893 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400894 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300895 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000896 ret = -ETIMEDOUT;
897 }
898 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000899}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000900
901static int sh_eth_reset(struct net_device *ndev)
902{
903 struct sh_eth_private *mdp = netdev_priv(ndev);
904 int ret = 0;
905
Simon Hormandb893472014-01-17 09:22:28 +0900906 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000907 sh_eth_write(ndev, EDSR_ENALL, EDSR);
908 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
909 EDMR);
910
911 ret = sh_eth_check_reset(ndev);
912 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100913 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000914
915 /* Table Init */
916 sh_eth_write(ndev, 0x0, TDLAR);
917 sh_eth_write(ndev, 0x0, TDFAR);
918 sh_eth_write(ndev, 0x0, TDFXR);
919 sh_eth_write(ndev, 0x0, TDFFR);
920 sh_eth_write(ndev, 0x0, RDLAR);
921 sh_eth_write(ndev, 0x0, RDFAR);
922 sh_eth_write(ndev, 0x0, RDFXR);
923 sh_eth_write(ndev, 0x0, RDFFR);
924
925 /* Reset HW CRC register */
926 if (mdp->cd->hw_crc)
927 sh_eth_write(ndev, 0x0, CSMR);
928
929 /* Select MII mode */
930 if (mdp->cd->select_mii)
931 sh_eth_select_mii(ndev);
932 } else {
933 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
934 EDMR);
935 mdelay(3);
936 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
937 EDMR);
938 }
939
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000940 return ret;
941}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000942
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000943static void sh_eth_set_receive_align(struct sk_buff *skb)
944{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900945 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000946
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000947 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900948 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000949}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000950
951
Yoshinori Sato71557a32008-08-06 19:49:00 -0400952/* CPU <-> EDMAC endian convert */
953static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
954{
955 switch (mdp->edmac_endian) {
956 case EDMAC_LITTLE_ENDIAN:
957 return cpu_to_le32(x);
958 case EDMAC_BIG_ENDIAN:
959 return cpu_to_be32(x);
960 }
961 return x;
962}
963
964static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
965{
966 switch (mdp->edmac_endian) {
967 case EDMAC_LITTLE_ENDIAN:
968 return le32_to_cpu(x);
969 case EDMAC_BIG_ENDIAN:
970 return be32_to_cpu(x);
971 }
972 return x;
973}
974
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300975/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700976static void update_mac_address(struct net_device *ndev)
977{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000978 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300979 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
980 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000981 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300982 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700983}
984
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300985/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700986 *
987 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
988 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
989 * When you want use this device, you must set MAC address in bootloader.
990 *
991 */
Magnus Damm748031f2009-10-09 00:17:14 +0000992static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700993{
Magnus Damm748031f2009-10-09 00:17:14 +0000994 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700995 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000996 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000997 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
998 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
999 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
1000 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
1001 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
1002 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +00001003 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001004}
1005
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001006static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001007{
Simon Hormandb893472014-01-17 09:22:28 +09001008 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001009 return EDTRR_TRNS_GETHER;
1010 else
1011 return EDTRR_TRNS_ETHER;
1012}
1013
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001014struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001015 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001017 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001018 u32 mmd_msk;/* MMD */
1019 u32 mdo_msk;
1020 u32 mdi_msk;
1021 u32 mdc_msk;
1022};
1023
1024/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001025static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001026{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001027 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001028}
1029
1030/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001031static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001032{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001033 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001034}
1035
1036/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001037static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001038{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001039 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001040}
1041
1042/* Data I/O pin control */
1043static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1044{
1045 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001046
1047 if (bitbang->set_gate)
1048 bitbang->set_gate(bitbang->addr);
1049
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001050 if (bit)
1051 bb_set(bitbang->addr, bitbang->mmd_msk);
1052 else
1053 bb_clr(bitbang->addr, bitbang->mmd_msk);
1054}
1055
1056/* Set bit data*/
1057static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1058{
1059 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1060
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001061 if (bitbang->set_gate)
1062 bitbang->set_gate(bitbang->addr);
1063
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001064 if (bit)
1065 bb_set(bitbang->addr, bitbang->mdo_msk);
1066 else
1067 bb_clr(bitbang->addr, bitbang->mdo_msk);
1068}
1069
1070/* Get bit data*/
1071static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1072{
1073 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001074
1075 if (bitbang->set_gate)
1076 bitbang->set_gate(bitbang->addr);
1077
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 return bb_read(bitbang->addr, bitbang->mdi_msk);
1079}
1080
1081/* MDC pin control */
1082static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1083{
1084 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1085
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001086 if (bitbang->set_gate)
1087 bitbang->set_gate(bitbang->addr);
1088
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001089 if (bit)
1090 bb_set(bitbang->addr, bitbang->mdc_msk);
1091 else
1092 bb_clr(bitbang->addr, bitbang->mdc_msk);
1093}
1094
1095/* mdio bus control struct */
1096static struct mdiobb_ops bb_ops = {
1097 .owner = THIS_MODULE,
1098 .set_mdc = sh_mdc_ctrl,
1099 .set_mdio_dir = sh_mmd_ctrl,
1100 .set_mdio_data = sh_set_mdio,
1101 .get_mdio_data = sh_get_mdio,
1102};
1103
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001104/* free skb and descriptor buffer */
1105static void sh_eth_ring_free(struct net_device *ndev)
1106{
1107 struct sh_eth_private *mdp = netdev_priv(ndev);
1108 int i;
1109
1110 /* Free Rx skb ringbuffer */
1111 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001112 for (i = 0; i < mdp->num_rx_ring; i++)
1113 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001114 }
1115 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001116 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001117
1118 /* Free Tx skb ringbuffer */
1119 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001120 for (i = 0; i < mdp->num_tx_ring; i++)
1121 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001122 }
1123 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001124 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001125}
1126
1127/* format skb and descriptor buffer */
1128static void sh_eth_ring_format(struct net_device *ndev)
1129{
1130 struct sh_eth_private *mdp = netdev_priv(ndev);
1131 int i;
1132 struct sk_buff *skb;
1133 struct sh_eth_rxdesc *rxdesc = NULL;
1134 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001135 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1136 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001137 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001138 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001139
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001140 mdp->cur_rx = 0;
1141 mdp->cur_tx = 0;
1142 mdp->dirty_rx = 0;
1143 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144
1145 memset(mdp->rx_ring, 0, rx_ringsize);
1146
1147 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001148 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001149 /* skb */
1150 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001151 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001152 if (skb == NULL)
1153 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001154 sh_eth_set_receive_align(skb);
1155
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001156 /* RX descriptor */
1157 rxdesc = &mdp->rx_ring[i];
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001158 /* The size of the buffer is a multiple of 16 bytes. */
1159 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001160 dma_addr = dma_map_single(&ndev->dev, skb->data,
1161 rxdesc->buffer_length,
1162 DMA_FROM_DEVICE);
1163 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1164 kfree_skb(skb);
1165 break;
1166 }
1167 mdp->rx_skbuff[i] = skb;
1168 rxdesc->addr = dma_addr;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001169 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001171 /* Rx descriptor address set */
1172 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001173 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001174 if (sh_eth_is_gether(mdp) ||
1175 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001176 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001177 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001178 }
1179
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001180 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001181
1182 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001183 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001184
1185 memset(mdp->tx_ring, 0, tx_ringsize);
1186
1187 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001188 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001189 mdp->tx_skbuff[i] = NULL;
1190 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001191 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001192 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001193 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001194 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001195 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001196 if (sh_eth_is_gether(mdp) ||
1197 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001198 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001199 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001200 }
1201
Yoshinori Sato71557a32008-08-06 19:49:00 -04001202 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203}
1204
1205/* Get skb and descriptor buffer */
1206static int sh_eth_ring_init(struct net_device *ndev)
1207{
1208 struct sh_eth_private *mdp = netdev_priv(ndev);
1209 int rx_ringsize, tx_ringsize, ret = 0;
1210
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001211 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212 * card needs room to do 8 byte alignment, +2 so we can reserve
1213 * the first 2 bytes, and +16 gets room for the status word from the
1214 * card.
1215 */
1216 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1217 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001218 if (mdp->cd->rpadir)
1219 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220
1221 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001222 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1223 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001224 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001225 ret = -ENOMEM;
1226 return ret;
1227 }
1228
Joe Perchesb2adaca2013-02-03 17:43:58 +00001229 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1230 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232 ret = -ENOMEM;
1233 goto skb_ring_free;
1234 }
1235
1236 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001237 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001238 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001239 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001240 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001241 ret = -ENOMEM;
1242 goto desc_ring_free;
1243 }
1244
1245 mdp->dirty_rx = 0;
1246
1247 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001248 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001249 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001250 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001251 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001252 ret = -ENOMEM;
1253 goto desc_ring_free;
1254 }
1255 return ret;
1256
1257desc_ring_free:
1258 /* free DMA buffer */
1259 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1260
1261skb_ring_free:
1262 /* Free Rx and Tx skb ring buffer */
1263 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001264 mdp->tx_ring = NULL;
1265 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001266
1267 return ret;
1268}
1269
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001270static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1271{
1272 int ringsize;
1273
1274 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001275 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001276 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1277 mdp->rx_desc_dma);
1278 mdp->rx_ring = NULL;
1279 }
1280
1281 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001282 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001283 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1284 mdp->tx_desc_dma);
1285 mdp->tx_ring = NULL;
1286 }
1287}
1288
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001289static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001290{
1291 int ret = 0;
1292 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001293 u32 val;
1294
1295 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001296 ret = sh_eth_reset(ndev);
1297 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001298 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001299
Simon Horman55754f12013-07-23 10:18:04 +09001300 if (mdp->cd->rmiimode)
1301 sh_eth_write(ndev, 0x1, RMIIMODE);
1302
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001303 /* Descriptor format */
1304 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001305 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001306 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001307
1308 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001309 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001310
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001311#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001312 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001313 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001314 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001315#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001316 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001317
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001318 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001319 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1320 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001321
Ben Dooks530aa2d2014-06-03 12:21:13 +01001322 /* Frame recv control (enable multiple-packets per rx irq) */
1323 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001324
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001325 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001326
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001327 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001328 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001329
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001330 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001331
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001332 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001333 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001334
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001335 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001336 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1337 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001338
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001339 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001340 if (start) {
1341 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001342 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001343 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001344
1345 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001346 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001347 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1348
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001349 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001350
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001351 if (mdp->cd->set_rate)
1352 mdp->cd->set_rate(ndev);
1353
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001354 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001355 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001356
1357 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001358 if (start)
1359 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001360
1361 /* Set MAC address */
1362 update_mac_address(ndev);
1363
1364 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001365 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001366 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001367 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001368 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001369 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001370 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001371
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001372 if (start) {
1373 /* Setting the Rx mode will start the Rx process. */
1374 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001375
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001376 netif_start_queue(ndev);
1377 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378
1379 return ret;
1380}
1381
Ben Hutchings740c7f32015-01-27 00:49:32 +00001382static void sh_eth_dev_exit(struct net_device *ndev)
1383{
1384 struct sh_eth_private *mdp = netdev_priv(ndev);
1385 int i;
1386
1387 /* Deactivate all TX descriptors, so DMA should stop at next
1388 * packet boundary if it's currently running
1389 */
1390 for (i = 0; i < mdp->num_tx_ring; i++)
1391 mdp->tx_ring[i].status &= ~cpu_to_edmac(mdp, TD_TACT);
1392
1393 /* Disable TX FIFO egress to MAC */
1394 sh_eth_rcv_snd_disable(ndev);
1395
1396 /* Stop RX DMA at next packet boundary */
1397 sh_eth_write(ndev, 0, EDRRR);
1398
1399 /* Aside from TX DMA, we can't tell when the hardware is
1400 * really stopped, so we need to reset to make sure.
1401 * Before doing that, wait for long enough to *probably*
1402 * finish transmitting the last packet and poll stats.
1403 */
1404 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1405 sh_eth_get_stats(ndev);
1406 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001407
1408 /* Set MAC address again */
1409 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001410}
1411
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412/* free Tx skb function */
1413static int sh_eth_txfree(struct net_device *ndev)
1414{
1415 struct sh_eth_private *mdp = netdev_priv(ndev);
1416 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001417 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001418 int entry = 0;
1419
1420 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001421 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001423 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001424 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001425 /* TACT bit must be checked before all the following reads */
1426 rmb();
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001427 /* Free the original skb. */
1428 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001429 dma_unmap_single(&ndev->dev, txdesc->addr,
1430 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001431 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1432 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001433 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001434 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001435 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001436 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001437 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001438
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001439 ndev->stats.tx_packets++;
1440 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001441 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001442 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001443}
1444
1445/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001446static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447{
1448 struct sh_eth_private *mdp = netdev_priv(ndev);
1449 struct sh_eth_rxdesc *rxdesc;
1450
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001451 int entry = mdp->cur_rx % mdp->num_rx_ring;
1452 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001453 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001454 struct sk_buff *skb;
1455 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001456 u32 desc_status;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001457 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001458 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001459
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001460 boguscnt = min(boguscnt, *quota);
1461 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001462 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001463 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001464 /* RACT bit must be checked before all the following reads */
1465 rmb();
Yoshinori Sato71557a32008-08-06 19:49:00 -04001466 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001467 pkt_len = rxdesc->frame_length;
1468
1469 if (--boguscnt < 0)
1470 break;
1471
1472 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001473 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001474
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001475 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001476 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001477 * bit 0. However, in case of the R8A7740 and R7S72100
1478 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001479 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001480 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001481 if (mdp->cd->shift_rd0)
1482 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001483
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001484 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1485 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001486 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001487 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001488 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001489 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001490 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001491 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001492 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001493 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001494 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001495 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001496 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001497 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001498 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001499 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001500 if (!mdp->cd->hw_swap)
1501 sh_eth_soft_swap(
1502 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1503 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001504 skb = mdp->rx_skbuff[entry];
1505 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001506 if (mdp->cd->rpadir)
1507 skb_reserve(skb, NET_IP_ALIGN);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001508 dma_unmap_single(&ndev->dev, rxdesc->addr,
1509 ALIGN(mdp->rx_buf_sz, 16),
1510 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001511 skb_put(skb, pkt_len);
1512 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001513 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001514 ndev->stats.rx_packets++;
1515 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001516 if (desc_status & RD_RFS8)
1517 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001518 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001519 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001520 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001521 }
1522
1523 /* Refill the Rx ring buffers. */
1524 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001525 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001526 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001527 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001528 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001529
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001530 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001531 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001532 if (skb == NULL)
1533 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001534 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001535 dma_addr = dma_map_single(&ndev->dev, skb->data,
1536 rxdesc->buffer_length,
1537 DMA_FROM_DEVICE);
1538 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1539 kfree_skb(skb);
1540 break;
1541 }
1542 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001543
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001544 skb_checksum_none_assert(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001545 rxdesc->addr = dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001546 }
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001547 wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001548 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001549 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001550 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551 else
1552 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001553 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001554 }
1555
1556 /* Restart Rx engine if stopped. */
1557 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001558 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001559 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001560 if (intr_status & EESR_RDE &&
1561 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001562 u32 count = (sh_eth_read(ndev, RDFAR) -
1563 sh_eth_read(ndev, RDLAR)) >> 4;
1564
1565 mdp->cur_rx = count;
1566 mdp->dirty_rx = count;
1567 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001568 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001569 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001570
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001571 *quota -= limit - boguscnt - 1;
1572
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001573 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001574}
1575
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001576static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001577{
1578 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001579 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1580 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001581}
1582
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001583static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001584{
1585 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001586 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1587 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001588}
1589
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001590/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001591static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001592{
1593 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001594 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001595 u32 link_stat;
1596 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001597
1598 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001599 felic_stat = sh_eth_read(ndev, ECSR);
1600 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001601 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001602 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001603 if (felic_stat & ECSR_LCHNG) {
1604 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001605 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001606 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001607 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001608 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001609 if (mdp->ether_link_active_low)
1610 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001611 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001612 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001613 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001614 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001615 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001616 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001617 ~DMAC_M_ECI, EESIPR);
1618 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001619 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001620 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001621 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001622 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001623 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001624 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001625 }
1626 }
1627 }
1628
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001629ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001630 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001631 /* Unused write back interrupt */
1632 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001633 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001634 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001635 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001636 }
1637
1638 if (intr_status & EESR_RABT) {
1639 /* Receive Abort int */
1640 if (intr_status & EESR_RFRMER) {
1641 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001642 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001643 }
1644 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001645
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001646 if (intr_status & EESR_TDE) {
1647 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001648 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001649 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001650 }
1651
1652 if (intr_status & EESR_TFE) {
1653 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001654 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001655 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001656 }
1657
1658 if (intr_status & EESR_RDE) {
1659 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001660 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001661 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001662
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001663 if (intr_status & EESR_RFE) {
1664 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001665 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001666 }
1667
1668 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1669 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001670 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001671 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001672 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001673
1674 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1675 if (mdp->cd->no_ade)
1676 mask &= ~EESR_ADE;
1677 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001678 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001679 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001680
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001681 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001682 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1683 intr_status, mdp->cur_tx, mdp->dirty_tx,
1684 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001685 /* dirty buffer free */
1686 sh_eth_txfree(ndev);
1687
1688 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001689 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001690 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001691 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001692 }
1693 /* wakeup */
1694 netif_wake_queue(ndev);
1695 }
1696}
1697
1698static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1699{
1700 struct net_device *ndev = netdev;
1701 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001702 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001703 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001704 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001705
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001706 spin_lock(&mdp->lock);
1707
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001708 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001709 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001710 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1711 * enabled since it's the one that comes thru regardless of the mask,
1712 * and we need to fully handle it in sh_eth_error() in order to quench
1713 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1714 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001715 intr_enable = sh_eth_read(ndev, EESIPR);
1716 intr_status &= intr_enable | DMAC_M_ECI;
1717 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001718 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001719 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001720 goto out;
1721
1722 if (!likely(mdp->irq_enabled)) {
1723 sh_eth_write(ndev, 0, EESIPR);
1724 goto out;
1725 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001726
Sergei Shtylyov37191092013-06-19 23:30:23 +04001727 if (intr_status & EESR_RX_CHECK) {
1728 if (napi_schedule_prep(&mdp->napi)) {
1729 /* Mask Rx interrupts */
1730 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1731 EESIPR);
1732 __napi_schedule(&mdp->napi);
1733 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001734 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001735 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001736 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001737 }
1738 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001739
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001740 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001741 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001742 /* Clear Tx interrupts */
1743 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1744
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001745 sh_eth_txfree(ndev);
1746 netif_wake_queue(ndev);
1747 }
1748
Sergei Shtylyov37191092013-06-19 23:30:23 +04001749 if (intr_status & cd->eesr_err_check) {
1750 /* Clear error interrupts */
1751 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1752
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001753 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001754 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001755
Ben Hutchings283e38d2015-01-22 12:44:08 +00001756out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001757 spin_unlock(&mdp->lock);
1758
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001759 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001760}
1761
Sergei Shtylyov37191092013-06-19 23:30:23 +04001762static int sh_eth_poll(struct napi_struct *napi, int budget)
1763{
1764 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1765 napi);
1766 struct net_device *ndev = napi->dev;
1767 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001768 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001769
1770 for (;;) {
1771 intr_status = sh_eth_read(ndev, EESR);
1772 if (!(intr_status & EESR_RX_CHECK))
1773 break;
1774 /* Clear Rx interrupts */
1775 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1776
1777 if (sh_eth_rx(ndev, intr_status, &quota))
1778 goto out;
1779 }
1780
1781 napi_complete(napi);
1782
1783 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001784 if (mdp->irq_enabled)
1785 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001786out:
1787 return budget - quota;
1788}
1789
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001790/* PHY state control function */
1791static void sh_eth_adjust_link(struct net_device *ndev)
1792{
1793 struct sh_eth_private *mdp = netdev_priv(ndev);
1794 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001795 int new_state = 0;
1796
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001797 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001798 if (phydev->duplex != mdp->duplex) {
1799 new_state = 1;
1800 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001801 if (mdp->cd->set_duplex)
1802 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 }
1804
1805 if (phydev->speed != mdp->speed) {
1806 new_state = 1;
1807 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001808 if (mdp->cd->set_rate)
1809 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001810 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001811 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001812 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001813 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1814 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001815 new_state = 1;
1816 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001817 if (mdp->cd->no_psr || mdp->no_ether_link)
1818 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001819 }
1820 } else if (mdp->link) {
1821 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001822 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001823 mdp->speed = 0;
1824 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001825 if (mdp->cd->no_psr || mdp->no_ether_link)
1826 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001827 }
1828
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001829 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001830 phy_print_status(phydev);
1831}
1832
1833/* PHY init function */
1834static int sh_eth_phy_init(struct net_device *ndev)
1835{
Ben Dooks702eca02014-03-12 17:47:40 +00001836 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001837 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001838 struct phy_device *phydev = NULL;
1839
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001840 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001841 mdp->speed = 0;
1842 mdp->duplex = -1;
1843
1844 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001845 if (np) {
1846 struct device_node *pn;
1847
1848 pn = of_parse_phandle(np, "phy-handle", 0);
1849 phydev = of_phy_connect(ndev, pn,
1850 sh_eth_adjust_link, 0,
1851 mdp->phy_interface);
1852
1853 if (!phydev)
1854 phydev = ERR_PTR(-ENOENT);
1855 } else {
1856 char phy_id[MII_BUS_ID_SIZE + 3];
1857
1858 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1859 mdp->mii_bus->id, mdp->phy_id);
1860
1861 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1862 mdp->phy_interface);
1863 }
1864
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001865 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001866 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001867 return PTR_ERR(phydev);
1868 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001869
Sergei Shtylyovda246852014-03-15 03:29:14 +03001870 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1871 phydev->addr, phydev->irq, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001872
1873 mdp->phydev = phydev;
1874
1875 return 0;
1876}
1877
1878/* PHY control start function */
1879static int sh_eth_phy_start(struct net_device *ndev)
1880{
1881 struct sh_eth_private *mdp = netdev_priv(ndev);
1882 int ret;
1883
1884 ret = sh_eth_phy_init(ndev);
1885 if (ret)
1886 return ret;
1887
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001888 phy_start(mdp->phydev);
1889
1890 return 0;
1891}
1892
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001893static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001894 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001895{
1896 struct sh_eth_private *mdp = netdev_priv(ndev);
1897 unsigned long flags;
1898 int ret;
1899
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001900 if (!mdp->phydev)
1901 return -ENODEV;
1902
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001903 spin_lock_irqsave(&mdp->lock, flags);
1904 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1905 spin_unlock_irqrestore(&mdp->lock, flags);
1906
1907 return ret;
1908}
1909
1910static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001911 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001912{
1913 struct sh_eth_private *mdp = netdev_priv(ndev);
1914 unsigned long flags;
1915 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001916
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001917 if (!mdp->phydev)
1918 return -ENODEV;
1919
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001920 spin_lock_irqsave(&mdp->lock, flags);
1921
1922 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001923 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001924
1925 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1926 if (ret)
1927 goto error_exit;
1928
1929 if (ecmd->duplex == DUPLEX_FULL)
1930 mdp->duplex = 1;
1931 else
1932 mdp->duplex = 0;
1933
1934 if (mdp->cd->set_duplex)
1935 mdp->cd->set_duplex(ndev);
1936
1937error_exit:
1938 mdelay(1);
1939
1940 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001941 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001942
1943 spin_unlock_irqrestore(&mdp->lock, flags);
1944
1945 return ret;
1946}
1947
1948static int sh_eth_nway_reset(struct net_device *ndev)
1949{
1950 struct sh_eth_private *mdp = netdev_priv(ndev);
1951 unsigned long flags;
1952 int ret;
1953
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001954 if (!mdp->phydev)
1955 return -ENODEV;
1956
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001957 spin_lock_irqsave(&mdp->lock, flags);
1958 ret = phy_start_aneg(mdp->phydev);
1959 spin_unlock_irqrestore(&mdp->lock, flags);
1960
1961 return ret;
1962}
1963
1964static u32 sh_eth_get_msglevel(struct net_device *ndev)
1965{
1966 struct sh_eth_private *mdp = netdev_priv(ndev);
1967 return mdp->msg_enable;
1968}
1969
1970static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1971{
1972 struct sh_eth_private *mdp = netdev_priv(ndev);
1973 mdp->msg_enable = value;
1974}
1975
1976static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1977 "rx_current", "tx_current",
1978 "rx_dirty", "tx_dirty",
1979};
1980#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1981
1982static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1983{
1984 switch (sset) {
1985 case ETH_SS_STATS:
1986 return SH_ETH_STATS_LEN;
1987 default:
1988 return -EOPNOTSUPP;
1989 }
1990}
1991
1992static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001993 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001994{
1995 struct sh_eth_private *mdp = netdev_priv(ndev);
1996 int i = 0;
1997
1998 /* device-specific stats */
1999 data[i++] = mdp->cur_rx;
2000 data[i++] = mdp->cur_tx;
2001 data[i++] = mdp->dirty_rx;
2002 data[i++] = mdp->dirty_tx;
2003}
2004
2005static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2006{
2007 switch (stringset) {
2008 case ETH_SS_STATS:
2009 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002010 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002011 break;
2012 }
2013}
2014
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002015static void sh_eth_get_ringparam(struct net_device *ndev,
2016 struct ethtool_ringparam *ring)
2017{
2018 struct sh_eth_private *mdp = netdev_priv(ndev);
2019
2020 ring->rx_max_pending = RX_RING_MAX;
2021 ring->tx_max_pending = TX_RING_MAX;
2022 ring->rx_pending = mdp->num_rx_ring;
2023 ring->tx_pending = mdp->num_tx_ring;
2024}
2025
2026static int sh_eth_set_ringparam(struct net_device *ndev,
2027 struct ethtool_ringparam *ring)
2028{
2029 struct sh_eth_private *mdp = netdev_priv(ndev);
2030 int ret;
2031
2032 if (ring->tx_pending > TX_RING_MAX ||
2033 ring->rx_pending > RX_RING_MAX ||
2034 ring->tx_pending < TX_RING_MIN ||
2035 ring->rx_pending < RX_RING_MIN)
2036 return -EINVAL;
2037 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2038 return -EINVAL;
2039
2040 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002041 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002042 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002043
Ben Hutchings283e38d2015-01-22 12:44:08 +00002044 /* Serialise with the interrupt handler and NAPI, then
2045 * disable interrupts. We have to clear the
2046 * irq_enabled flag first to ensure that interrupts
2047 * won't be re-enabled.
2048 */
2049 mdp->irq_enabled = false;
2050 synchronize_irq(ndev->irq);
2051 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002052 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002053
Ben Hutchings740c7f32015-01-27 00:49:32 +00002054 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002055
Ben Hutchings084236d2015-01-22 12:41:34 +00002056 /* Free all the skbuffs in the Rx queue. */
2057 sh_eth_ring_free(ndev);
2058 /* Free DMA buffer */
2059 sh_eth_free_dma_buffer(mdp);
2060 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002061
2062 /* Set new parameters */
2063 mdp->num_rx_ring = ring->rx_pending;
2064 mdp->num_tx_ring = ring->tx_pending;
2065
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002066 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002067 ret = sh_eth_ring_init(ndev);
2068 if (ret < 0) {
2069 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2070 __func__);
2071 return ret;
2072 }
2073 ret = sh_eth_dev_init(ndev, false);
2074 if (ret < 0) {
2075 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2076 __func__);
2077 return ret;
2078 }
2079
Ben Hutchings283e38d2015-01-22 12:44:08 +00002080 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002081 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2082 /* Setting the Rx mode will start the Rx process. */
2083 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002084 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002085 }
2086
2087 return 0;
2088}
2089
stephen hemminger9b07be42012-01-04 12:59:49 +00002090static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002091 .get_settings = sh_eth_get_settings,
2092 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00002093 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002094 .get_msglevel = sh_eth_get_msglevel,
2095 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002096 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002097 .get_strings = sh_eth_get_strings,
2098 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2099 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002100 .get_ringparam = sh_eth_get_ringparam,
2101 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002102};
2103
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002104/* network device open function */
2105static int sh_eth_open(struct net_device *ndev)
2106{
2107 int ret = 0;
2108 struct sh_eth_private *mdp = netdev_priv(ndev);
2109
Magnus Dammbcd51492009-10-09 00:20:04 +00002110 pm_runtime_get_sync(&mdp->pdev->dev);
2111
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002112 napi_enable(&mdp->napi);
2113
Joe Perchesa0607fd2009-11-18 23:29:17 -08002114 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002115 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002116 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002117 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002118 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002119 }
2120
2121 /* Descriptor set */
2122 ret = sh_eth_ring_init(ndev);
2123 if (ret)
2124 goto out_free_irq;
2125
2126 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002127 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002128 if (ret)
2129 goto out_free_irq;
2130
2131 /* PHY control start*/
2132 ret = sh_eth_phy_start(ndev);
2133 if (ret)
2134 goto out_free_irq;
2135
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002136 mdp->is_opened = 1;
2137
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002138 return ret;
2139
2140out_free_irq:
2141 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002142out_napi_off:
2143 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002144 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002145 return ret;
2146}
2147
2148/* Timeout function */
2149static void sh_eth_tx_timeout(struct net_device *ndev)
2150{
2151 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002152 struct sh_eth_rxdesc *rxdesc;
2153 int i;
2154
2155 netif_stop_queue(ndev);
2156
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002157 netif_err(mdp, timer, ndev,
2158 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002159 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002160
2161 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002162 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002163
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002164 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002165 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002166 rxdesc = &mdp->rx_ring[i];
2167 rxdesc->status = 0;
2168 rxdesc->addr = 0xBADF00D0;
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002169 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002170 mdp->rx_skbuff[i] = NULL;
2171 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002172 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002173 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002174 mdp->tx_skbuff[i] = NULL;
2175 }
2176
2177 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002178 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002179}
2180
2181/* Packet transmit function */
2182static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2183{
2184 struct sh_eth_private *mdp = netdev_priv(ndev);
2185 struct sh_eth_txdesc *txdesc;
2186 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002187 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002188
2189 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002190 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002191 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002192 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002193 netif_stop_queue(ndev);
2194 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002195 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002196 }
2197 }
2198 spin_unlock_irqrestore(&mdp->lock, flags);
2199
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002200 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002201 return NETDEV_TX_OK;
2202
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002203 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002204 mdp->tx_skbuff[entry] = skb;
2205 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002206 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002207 if (!mdp->cd->hw_swap)
2208 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2209 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002210 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2211 DMA_TO_DEVICE);
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002212 if (dma_mapping_error(&ndev->dev, txdesc->addr)) {
2213 kfree_skb(skb);
2214 return NETDEV_TX_OK;
2215 }
Ben Hutchingseebfb642015-01-22 12:40:13 +00002216 txdesc->buffer_length = skb->len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002217
Ben Hutchings7d7355f2015-03-03 00:52:00 +00002218 wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002219 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002220 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002221 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002222 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002223
2224 mdp->cur_tx++;
2225
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002226 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2227 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002228
Patrick McHardy6ed10652009-06-23 06:03:08 +00002229 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002230}
2231
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002232static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2233{
2234 struct sh_eth_private *mdp = netdev_priv(ndev);
2235
2236 if (sh_eth_is_rz_fast_ether(mdp))
2237 return &ndev->stats;
2238
2239 if (!mdp->is_opened)
2240 return &ndev->stats;
2241
2242 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
2243 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
2244 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
2245 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
2246 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
2247 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
2248
2249 if (sh_eth_is_gether(mdp)) {
2250 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
2251 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
2252 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
2253 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
2254 } else {
2255 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
2256 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2257 }
2258
2259 return &ndev->stats;
2260}
2261
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002262/* device close function */
2263static int sh_eth_close(struct net_device *ndev)
2264{
2265 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002266
2267 netif_stop_queue(ndev);
2268
Ben Hutchings283e38d2015-01-22 12:44:08 +00002269 /* Serialise with the interrupt handler and NAPI, then disable
2270 * interrupts. We have to clear the irq_enabled flag first to
2271 * ensure that interrupts won't be re-enabled.
2272 */
2273 mdp->irq_enabled = false;
2274 synchronize_irq(ndev->irq);
2275 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002276 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002277
Ben Hutchings740c7f32015-01-27 00:49:32 +00002278 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002279
2280 /* PHY Disconnect */
2281 if (mdp->phydev) {
2282 phy_stop(mdp->phydev);
2283 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002284 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002285 }
2286
2287 free_irq(ndev->irq, ndev);
2288
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002289 /* Free all the skbuffs in the Rx queue. */
2290 sh_eth_ring_free(ndev);
2291
2292 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00002293 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002294
Magnus Dammbcd51492009-10-09 00:20:04 +00002295 pm_runtime_put_sync(&mdp->pdev->dev);
2296
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002297 mdp->is_opened = 0;
2298
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002299 return 0;
2300}
2301
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002302/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002303static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002304{
2305 struct sh_eth_private *mdp = netdev_priv(ndev);
2306 struct phy_device *phydev = mdp->phydev;
2307
2308 if (!netif_running(ndev))
2309 return -EINVAL;
2310
2311 if (!phydev)
2312 return -ENODEV;
2313
Richard Cochran28b04112010-07-17 08:48:55 +00002314 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002315}
2316
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002317/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2318static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2319 int entry)
2320{
2321 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2322}
2323
2324static u32 sh_eth_tsu_get_post_mask(int entry)
2325{
2326 return 0x0f << (28 - ((entry % 8) * 4));
2327}
2328
2329static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2330{
2331 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2332}
2333
2334static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2335 int entry)
2336{
2337 struct sh_eth_private *mdp = netdev_priv(ndev);
2338 u32 tmp;
2339 void *reg_offset;
2340
2341 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2342 tmp = ioread32(reg_offset);
2343 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2344}
2345
2346static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2347 int entry)
2348{
2349 struct sh_eth_private *mdp = netdev_priv(ndev);
2350 u32 post_mask, ref_mask, tmp;
2351 void *reg_offset;
2352
2353 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2354 post_mask = sh_eth_tsu_get_post_mask(entry);
2355 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2356
2357 tmp = ioread32(reg_offset);
2358 iowrite32(tmp & ~post_mask, reg_offset);
2359
2360 /* If other port enables, the function returns "true" */
2361 return tmp & ref_mask;
2362}
2363
2364static int sh_eth_tsu_busy(struct net_device *ndev)
2365{
2366 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2367 struct sh_eth_private *mdp = netdev_priv(ndev);
2368
2369 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2370 udelay(10);
2371 timeout--;
2372 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002373 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002374 return -ETIMEDOUT;
2375 }
2376 }
2377
2378 return 0;
2379}
2380
2381static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2382 const u8 *addr)
2383{
2384 u32 val;
2385
2386 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2387 iowrite32(val, reg);
2388 if (sh_eth_tsu_busy(ndev) < 0)
2389 return -EBUSY;
2390
2391 val = addr[4] << 8 | addr[5];
2392 iowrite32(val, reg + 4);
2393 if (sh_eth_tsu_busy(ndev) < 0)
2394 return -EBUSY;
2395
2396 return 0;
2397}
2398
2399static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2400{
2401 u32 val;
2402
2403 val = ioread32(reg);
2404 addr[0] = (val >> 24) & 0xff;
2405 addr[1] = (val >> 16) & 0xff;
2406 addr[2] = (val >> 8) & 0xff;
2407 addr[3] = val & 0xff;
2408 val = ioread32(reg + 4);
2409 addr[4] = (val >> 8) & 0xff;
2410 addr[5] = val & 0xff;
2411}
2412
2413
2414static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2415{
2416 struct sh_eth_private *mdp = netdev_priv(ndev);
2417 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2418 int i;
2419 u8 c_addr[ETH_ALEN];
2420
2421 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2422 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002423 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002424 return i;
2425 }
2426
2427 return -ENOENT;
2428}
2429
2430static int sh_eth_tsu_find_empty(struct net_device *ndev)
2431{
2432 u8 blank[ETH_ALEN];
2433 int entry;
2434
2435 memset(blank, 0, sizeof(blank));
2436 entry = sh_eth_tsu_find_entry(ndev, blank);
2437 return (entry < 0) ? -ENOMEM : entry;
2438}
2439
2440static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2441 int entry)
2442{
2443 struct sh_eth_private *mdp = netdev_priv(ndev);
2444 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2445 int ret;
2446 u8 blank[ETH_ALEN];
2447
2448 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2449 ~(1 << (31 - entry)), TSU_TEN);
2450
2451 memset(blank, 0, sizeof(blank));
2452 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2453 if (ret < 0)
2454 return ret;
2455 return 0;
2456}
2457
2458static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2459{
2460 struct sh_eth_private *mdp = netdev_priv(ndev);
2461 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2462 int i, ret;
2463
2464 if (!mdp->cd->tsu)
2465 return 0;
2466
2467 i = sh_eth_tsu_find_entry(ndev, addr);
2468 if (i < 0) {
2469 /* No entry found, create one */
2470 i = sh_eth_tsu_find_empty(ndev);
2471 if (i < 0)
2472 return -ENOMEM;
2473 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2474 if (ret < 0)
2475 return ret;
2476
2477 /* Enable the entry */
2478 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2479 (1 << (31 - i)), TSU_TEN);
2480 }
2481
2482 /* Entry found or created, enable POST */
2483 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2484
2485 return 0;
2486}
2487
2488static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2489{
2490 struct sh_eth_private *mdp = netdev_priv(ndev);
2491 int i, ret;
2492
2493 if (!mdp->cd->tsu)
2494 return 0;
2495
2496 i = sh_eth_tsu_find_entry(ndev, addr);
2497 if (i) {
2498 /* Entry found */
2499 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2500 goto done;
2501
2502 /* Disable the entry if both ports was disabled */
2503 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2504 if (ret < 0)
2505 return ret;
2506 }
2507done:
2508 return 0;
2509}
2510
2511static int sh_eth_tsu_purge_all(struct net_device *ndev)
2512{
2513 struct sh_eth_private *mdp = netdev_priv(ndev);
2514 int i, ret;
2515
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002516 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002517 return 0;
2518
2519 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2520 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2521 continue;
2522
2523 /* Disable the entry if both ports was disabled */
2524 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2525 if (ret < 0)
2526 return ret;
2527 }
2528
2529 return 0;
2530}
2531
2532static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2533{
2534 struct sh_eth_private *mdp = netdev_priv(ndev);
2535 u8 addr[ETH_ALEN];
2536 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2537 int i;
2538
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002539 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002540 return;
2541
2542 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2543 sh_eth_tsu_read_entry(reg_offset, addr);
2544 if (is_multicast_ether_addr(addr))
2545 sh_eth_tsu_del_entry(ndev, addr);
2546 }
2547}
2548
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002549/* Update promiscuous flag and multicast filter */
2550static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002551{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002552 struct sh_eth_private *mdp = netdev_priv(ndev);
2553 u32 ecmr_bits;
2554 int mcast_all = 0;
2555 unsigned long flags;
2556
2557 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002558 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002559 * Depending on ndev->flags, set PRM or clear MCT
2560 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002561 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2562 if (mdp->cd->tsu)
2563 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002564
2565 if (!(ndev->flags & IFF_MULTICAST)) {
2566 sh_eth_tsu_purge_mcast(ndev);
2567 mcast_all = 1;
2568 }
2569 if (ndev->flags & IFF_ALLMULTI) {
2570 sh_eth_tsu_purge_mcast(ndev);
2571 ecmr_bits &= ~ECMR_MCT;
2572 mcast_all = 1;
2573 }
2574
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002575 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002576 sh_eth_tsu_purge_all(ndev);
2577 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2578 } else if (mdp->cd->tsu) {
2579 struct netdev_hw_addr *ha;
2580 netdev_for_each_mc_addr(ha, ndev) {
2581 if (mcast_all && is_multicast_ether_addr(ha->addr))
2582 continue;
2583
2584 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2585 if (!mcast_all) {
2586 sh_eth_tsu_purge_mcast(ndev);
2587 ecmr_bits &= ~ECMR_MCT;
2588 mcast_all = 1;
2589 }
2590 }
2591 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002592 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002593
2594 /* update the ethernet mode */
2595 sh_eth_write(ndev, ecmr_bits, ECMR);
2596
2597 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002598}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002599
2600static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2601{
2602 if (!mdp->port)
2603 return TSU_VTAG0;
2604 else
2605 return TSU_VTAG1;
2606}
2607
Patrick McHardy80d5c362013-04-19 02:04:28 +00002608static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2609 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002610{
2611 struct sh_eth_private *mdp = netdev_priv(ndev);
2612 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2613
2614 if (unlikely(!mdp->cd->tsu))
2615 return -EPERM;
2616
2617 /* No filtering if vid = 0 */
2618 if (!vid)
2619 return 0;
2620
2621 mdp->vlan_num_ids++;
2622
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002623 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002624 * already enabled, the driver disables it and the filte
2625 */
2626 if (mdp->vlan_num_ids > 1) {
2627 /* disable VLAN filter */
2628 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2629 return 0;
2630 }
2631
2632 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2633 vtag_reg_index);
2634
2635 return 0;
2636}
2637
Patrick McHardy80d5c362013-04-19 02:04:28 +00002638static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2639 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002640{
2641 struct sh_eth_private *mdp = netdev_priv(ndev);
2642 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2643
2644 if (unlikely(!mdp->cd->tsu))
2645 return -EPERM;
2646
2647 /* No filtering if vid = 0 */
2648 if (!vid)
2649 return 0;
2650
2651 mdp->vlan_num_ids--;
2652 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2653
2654 return 0;
2655}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002656
2657/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002658static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002659{
Simon Hormandb893472014-01-17 09:22:28 +09002660 if (sh_eth_is_rz_fast_ether(mdp)) {
2661 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2662 return;
2663 }
2664
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002665 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2666 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2667 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2668 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2669 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2670 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2671 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2672 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2673 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2674 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002675 if (sh_eth_is_gether(mdp)) {
2676 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2677 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2678 } else {
2679 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2680 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2681 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002682 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2683 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2684 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2685 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2686 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2687 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2688 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002689}
2690
2691/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002692static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002693{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002694 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002695 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002696
2697 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002698 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002699
2700 return 0;
2701}
2702
2703/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002704static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002705 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002706{
2707 int ret, i;
2708 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002709 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002710 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002711
2712 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002713 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002714 if (!bitbang)
2715 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002716
2717 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002718 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002719 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002720 bitbang->mdi_msk = PIR_MDI;
2721 bitbang->mdo_msk = PIR_MDO;
2722 bitbang->mmd_msk = PIR_MMD;
2723 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002724 bitbang->ctrl.ops = &bb_ops;
2725
Stefan Weilc2e07b32010-08-03 19:44:52 +02002726 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002727 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002728 if (!mdp->mii_bus)
2729 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002730
2731 /* Hook up MII support for ethtool */
2732 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002733 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002734 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002735 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002736
2737 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002738 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2739 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002740 if (!mdp->mii_bus->irq) {
2741 ret = -ENOMEM;
2742 goto out_free_bus;
2743 }
2744
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002745 /* register MDIO bus */
2746 if (dev->of_node) {
2747 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002748 } else {
2749 for (i = 0; i < PHY_MAX_ADDR; i++)
2750 mdp->mii_bus->irq[i] = PHY_POLL;
2751 if (pd->phy_irq > 0)
2752 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2753
2754 ret = mdiobus_register(mdp->mii_bus);
2755 }
2756
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002757 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002758 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002759
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002760 return 0;
2761
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002762out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002763 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002764 return ret;
2765}
2766
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002767static const u16 *sh_eth_get_register_offset(int register_type)
2768{
2769 const u16 *reg_offset = NULL;
2770
2771 switch (register_type) {
2772 case SH_ETH_REG_GIGABIT:
2773 reg_offset = sh_eth_offset_gigabit;
2774 break;
Simon Hormandb893472014-01-17 09:22:28 +09002775 case SH_ETH_REG_FAST_RZ:
2776 reg_offset = sh_eth_offset_fast_rz;
2777 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002778 case SH_ETH_REG_FAST_RCAR:
2779 reg_offset = sh_eth_offset_fast_rcar;
2780 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002781 case SH_ETH_REG_FAST_SH4:
2782 reg_offset = sh_eth_offset_fast_sh4;
2783 break;
2784 case SH_ETH_REG_FAST_SH3_SH2:
2785 reg_offset = sh_eth_offset_fast_sh3_sh2;
2786 break;
2787 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002788 break;
2789 }
2790
2791 return reg_offset;
2792}
2793
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002794static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002795 .ndo_open = sh_eth_open,
2796 .ndo_stop = sh_eth_close,
2797 .ndo_start_xmit = sh_eth_start_xmit,
2798 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002799 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002800 .ndo_tx_timeout = sh_eth_tx_timeout,
2801 .ndo_do_ioctl = sh_eth_do_ioctl,
2802 .ndo_validate_addr = eth_validate_addr,
2803 .ndo_set_mac_address = eth_mac_addr,
2804 .ndo_change_mtu = eth_change_mtu,
2805};
2806
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002807static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2808 .ndo_open = sh_eth_open,
2809 .ndo_stop = sh_eth_close,
2810 .ndo_start_xmit = sh_eth_start_xmit,
2811 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002812 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002813 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2814 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2815 .ndo_tx_timeout = sh_eth_tx_timeout,
2816 .ndo_do_ioctl = sh_eth_do_ioctl,
2817 .ndo_validate_addr = eth_validate_addr,
2818 .ndo_set_mac_address = eth_mac_addr,
2819 .ndo_change_mtu = eth_change_mtu,
2820};
2821
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002822#ifdef CONFIG_OF
2823static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2824{
2825 struct device_node *np = dev->of_node;
2826 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002827 const char *mac_addr;
2828
2829 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2830 if (!pdata)
2831 return NULL;
2832
2833 pdata->phy_interface = of_get_phy_mode(np);
2834
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002835 mac_addr = of_get_mac_address(np);
2836 if (mac_addr)
2837 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2838
2839 pdata->no_ether_link =
2840 of_property_read_bool(np, "renesas,no-ether-link");
2841 pdata->ether_link_active_low =
2842 of_property_read_bool(np, "renesas,ether-link-active-low");
2843
2844 return pdata;
2845}
2846
2847static const struct of_device_id sh_eth_match_table[] = {
2848 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
2849 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
2850 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
2851 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
2852 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002853 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002854 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002855 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
2856 { }
2857};
2858MODULE_DEVICE_TABLE(of, sh_eth_match_table);
2859#else
2860static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2861{
2862 return NULL;
2863}
2864#endif
2865
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002866static int sh_eth_drv_probe(struct platform_device *pdev)
2867{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002868 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002869 struct resource *res;
2870 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002871 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09002872 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002873 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002874
2875 /* get base addr */
2876 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002877
2878 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01002879 if (!ndev)
2880 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002881
Ben Dooksb5893a02014-03-21 12:09:14 +01002882 pm_runtime_enable(&pdev->dev);
2883 pm_runtime_get_sync(&pdev->dev);
2884
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002885 devno = pdev->id;
2886 if (devno < 0)
2887 devno = 0;
2888
2889 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002890 ret = platform_get_irq(pdev, 0);
2891 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002892 ret = -ENODEV;
2893 goto out_release;
2894 }
roel kluincc3c0802008-09-10 19:22:44 +02002895 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002896
2897 SET_NETDEV_DEV(ndev, &pdev->dev);
2898
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002899 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002900 mdp->num_tx_ring = TX_RING_SIZE;
2901 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002902 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2903 if (IS_ERR(mdp->addr)) {
2904 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002905 goto out_release;
2906 }
2907
Varka Bhadramc9608042014-10-24 07:42:09 +05302908 ndev->base_addr = res->start;
2909
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002910 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002911 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002912
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002913 if (pdev->dev.of_node)
2914 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03002915 if (!pd) {
2916 dev_err(&pdev->dev, "no platform data\n");
2917 ret = -EINVAL;
2918 goto out_release;
2919 }
2920
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002921 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002922 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002923 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002924 /* EDMAC endian */
2925 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002926 mdp->no_ether_link = pd->no_ether_link;
2927 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002928
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002929 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002930 if (id) {
2931 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
2932 } else {
2933 const struct of_device_id *match;
2934
2935 match = of_match_device(of_match_ptr(sh_eth_match_table),
2936 &pdev->dev);
2937 mdp->cd = (struct sh_eth_cpu_data *)match->data;
2938 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04002939 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03002940 if (!mdp->reg_offset) {
2941 dev_err(&pdev->dev, "Unknown register type (%d)\n",
2942 mdp->cd->register_type);
2943 ret = -EINVAL;
2944 goto out_release;
2945 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002946 sh_eth_set_default_cpu_data(mdp->cd);
2947
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002948 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002949 if (mdp->cd->tsu)
2950 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
2951 else
2952 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002953 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002954 ndev->watchdog_timeo = TX_TIMEOUT;
2955
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002956 /* debug message level */
2957 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002958
2959 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002960 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002961 if (!is_valid_ether_addr(ndev->dev_addr)) {
2962 dev_warn(&pdev->dev,
2963 "no valid MAC address supplied, using a random one.\n");
2964 eth_hw_addr_random(ndev);
2965 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002966
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002967 /* ioremap the TSU registers */
2968 if (mdp->cd->tsu) {
2969 struct resource *rtsu;
2970 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002971 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2972 if (IS_ERR(mdp->tsu_addr)) {
2973 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002974 goto out_release;
2975 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002976 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002977 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002978 }
2979
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002980 /* initialize first or needed device */
2981 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002982 if (mdp->cd->chip_reset)
2983 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002984
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002985 if (mdp->cd->tsu) {
2986 /* TSU init (Init only)*/
2987 sh_eth_tsu_init(mdp);
2988 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002989 }
2990
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09002991 if (mdp->cd->rmiimode)
2992 sh_eth_write(ndev, 0x1, RMIIMODE);
2993
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002994 /* MDIO bus init */
2995 ret = sh_mdio_init(mdp, pd);
2996 if (ret) {
2997 dev_err(&ndev->dev, "failed to initialise MDIO\n");
2998 goto out_release;
2999 }
3000
Sergei Shtylyov37191092013-06-19 23:30:23 +04003001 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3002
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003003 /* network device register */
3004 ret = register_netdev(ndev);
3005 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003006 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003007
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003008 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003009 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3010 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003011
Ben Dooksb5893a02014-03-21 12:09:14 +01003012 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003013 platform_set_drvdata(pdev, ndev);
3014
3015 return ret;
3016
Sergei Shtylyov37191092013-06-19 23:30:23 +04003017out_napi_del:
3018 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003019 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003020
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003021out_release:
3022 /* net_dev free */
3023 if (ndev)
3024 free_netdev(ndev);
3025
Ben Dooksb5893a02014-03-21 12:09:14 +01003026 pm_runtime_put(&pdev->dev);
3027 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003028 return ret;
3029}
3030
3031static int sh_eth_drv_remove(struct platform_device *pdev)
3032{
3033 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003034 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003035
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003036 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003037 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003038 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003039 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003040 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003041
3042 return 0;
3043}
3044
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003045#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003046#ifdef CONFIG_PM_SLEEP
3047static int sh_eth_suspend(struct device *dev)
3048{
3049 struct net_device *ndev = dev_get_drvdata(dev);
3050 int ret = 0;
3051
3052 if (netif_running(ndev)) {
3053 netif_device_detach(ndev);
3054 ret = sh_eth_close(ndev);
3055 }
3056
3057 return ret;
3058}
3059
3060static int sh_eth_resume(struct device *dev)
3061{
3062 struct net_device *ndev = dev_get_drvdata(dev);
3063 int ret = 0;
3064
3065 if (netif_running(ndev)) {
3066 ret = sh_eth_open(ndev);
3067 if (ret < 0)
3068 return ret;
3069 netif_device_attach(ndev);
3070 }
3071
3072 return ret;
3073}
3074#endif
3075
Magnus Dammbcd51492009-10-09 00:20:04 +00003076static int sh_eth_runtime_nop(struct device *dev)
3077{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003078 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003079 * and ->runtime_resume(). Simply returns success.
3080 *
3081 * This driver re-initializes all registers after
3082 * pm_runtime_get_sync() anyway so there is no need
3083 * to save and restore registers here.
3084 */
3085 return 0;
3086}
3087
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003088static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003089 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003090 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003091};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003092#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3093#else
3094#define SH_ETH_PM_OPS NULL
3095#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003096
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003097static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003098 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003099 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003100 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003101 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003102 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3103 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003104 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Simon Hormandb893472014-01-17 09:22:28 +09003105 { "r7s72100-ether", (kernel_ulong_t)&r7s72100_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00003106 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00003107 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyov94a12b12013-12-08 02:59:18 +03003108 { "r8a7790-ether", (kernel_ulong_t)&r8a779x_data },
3109 { "r8a7791-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003110 { "r8a7793-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003111 { "r8a7794-ether", (kernel_ulong_t)&r8a779x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003112 { }
3113};
3114MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3115
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003116static struct platform_driver sh_eth_driver = {
3117 .probe = sh_eth_drv_probe,
3118 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003119 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003120 .driver = {
3121 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003122 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003123 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003124 },
3125};
3126
Axel Lindb62f682011-11-27 16:44:17 +00003127module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003128
3129MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3130MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3131MODULE_LICENSE("GPL v2");