blob: 1af07f9de243d613ffd8cd5ec9c96c5351a1d9e8 [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,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000140
141 [TXNLCR0] = 0x0080,
142 [TXALCR0] = 0x0084,
143 [RXNLCR0] = 0x0088,
144 [RXALCR0] = 0x008c,
145 [FWNLCR0] = 0x0090,
146 [FWALCR0] = 0x0094,
147 [TXNLCR1] = 0x00a0,
148 [TXALCR1] = 0x00a0,
149 [RXNLCR1] = 0x00a8,
150 [RXALCR1] = 0x00ac,
151 [FWNLCR1] = 0x00b0,
152 [FWALCR1] = 0x00b4,
153};
154
Simon Hormandb893472014-01-17 09:22:28 +0900155static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000156 SH_ETH_OFFSET_DEFAULTS,
157
Simon Hormandb893472014-01-17 09:22:28 +0900158 [EDSR] = 0x0000,
159 [EDMR] = 0x0400,
160 [EDTRR] = 0x0408,
161 [EDRRR] = 0x0410,
162 [EESR] = 0x0428,
163 [EESIPR] = 0x0430,
164 [TDLAR] = 0x0010,
165 [TDFAR] = 0x0014,
166 [TDFXR] = 0x0018,
167 [TDFFR] = 0x001c,
168 [RDLAR] = 0x0030,
169 [RDFAR] = 0x0034,
170 [RDFXR] = 0x0038,
171 [RDFFR] = 0x003c,
172 [TRSCER] = 0x0438,
173 [RMFCR] = 0x0440,
174 [TFTR] = 0x0448,
175 [FDR] = 0x0450,
176 [RMCR] = 0x0458,
177 [RPADIR] = 0x0460,
178 [FCFTR] = 0x0468,
179 [CSMR] = 0x04E4,
180
181 [ECMR] = 0x0500,
182 [RFLR] = 0x0508,
183 [ECSR] = 0x0510,
184 [ECSIPR] = 0x0518,
185 [PIR] = 0x0520,
186 [APR] = 0x0554,
187 [MPR] = 0x0558,
188 [PFTCR] = 0x055c,
189 [PFRCR] = 0x0560,
190 [TPAUSER] = 0x0564,
191 [MAHR] = 0x05c0,
192 [MALR] = 0x05c8,
193 [CEFCR] = 0x0740,
194 [FRECR] = 0x0748,
195 [TSFRCR] = 0x0750,
196 [TLFRCR] = 0x0758,
197 [RFCR] = 0x0760,
198 [MAFCR] = 0x0778,
199
200 [ARSTR] = 0x0000,
201 [TSU_CTRST] = 0x0004,
202 [TSU_VTAG0] = 0x0058,
203 [TSU_ADSBSY] = 0x0060,
204 [TSU_TEN] = 0x0064,
205 [TSU_ADRH0] = 0x0100,
Simon Hormandb893472014-01-17 09:22:28 +0900206
207 [TXNLCR0] = 0x0080,
208 [TXALCR0] = 0x0084,
209 [RXNLCR0] = 0x0088,
210 [RXALCR0] = 0x008C,
211};
212
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000213static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000214 SH_ETH_OFFSET_DEFAULTS,
215
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000216 [ECMR] = 0x0300,
217 [RFLR] = 0x0308,
218 [ECSR] = 0x0310,
219 [ECSIPR] = 0x0318,
220 [PIR] = 0x0320,
221 [PSR] = 0x0328,
222 [RDMLR] = 0x0340,
223 [IPGR] = 0x0350,
224 [APR] = 0x0354,
225 [MPR] = 0x0358,
226 [RFCF] = 0x0360,
227 [TPAUSER] = 0x0364,
228 [TPAUSECR] = 0x0368,
229 [MAHR] = 0x03c0,
230 [MALR] = 0x03c8,
231 [TROCR] = 0x03d0,
232 [CDCR] = 0x03d4,
233 [LCCR] = 0x03d8,
234 [CNDCR] = 0x03dc,
235 [CEFCR] = 0x03e4,
236 [FRECR] = 0x03e8,
237 [TSFRCR] = 0x03ec,
238 [TLFRCR] = 0x03f0,
239 [RFCR] = 0x03f4,
240 [MAFCR] = 0x03f8,
241
242 [EDMR] = 0x0200,
243 [EDTRR] = 0x0208,
244 [EDRRR] = 0x0210,
245 [TDLAR] = 0x0218,
246 [RDLAR] = 0x0220,
247 [EESR] = 0x0228,
248 [EESIPR] = 0x0230,
249 [TRSCER] = 0x0238,
250 [RMFCR] = 0x0240,
251 [TFTR] = 0x0248,
252 [FDR] = 0x0250,
253 [RMCR] = 0x0258,
254 [TFUCR] = 0x0264,
255 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900256 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000257 [FCFTR] = 0x0270,
258 [TRIMD] = 0x027c,
259};
260
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000261static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000262 SH_ETH_OFFSET_DEFAULTS,
263
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000264 [ECMR] = 0x0100,
265 [RFLR] = 0x0108,
266 [ECSR] = 0x0110,
267 [ECSIPR] = 0x0118,
268 [PIR] = 0x0120,
269 [PSR] = 0x0128,
270 [RDMLR] = 0x0140,
271 [IPGR] = 0x0150,
272 [APR] = 0x0154,
273 [MPR] = 0x0158,
274 [TPAUSER] = 0x0164,
275 [RFCF] = 0x0160,
276 [TPAUSECR] = 0x0168,
277 [BCFRR] = 0x016c,
278 [MAHR] = 0x01c0,
279 [MALR] = 0x01c8,
280 [TROCR] = 0x01d0,
281 [CDCR] = 0x01d4,
282 [LCCR] = 0x01d8,
283 [CNDCR] = 0x01dc,
284 [CEFCR] = 0x01e4,
285 [FRECR] = 0x01e8,
286 [TSFRCR] = 0x01ec,
287 [TLFRCR] = 0x01f0,
288 [RFCR] = 0x01f4,
289 [MAFCR] = 0x01f8,
290 [RTRATE] = 0x01fc,
291
292 [EDMR] = 0x0000,
293 [EDTRR] = 0x0008,
294 [EDRRR] = 0x0010,
295 [TDLAR] = 0x0018,
296 [RDLAR] = 0x0020,
297 [EESR] = 0x0028,
298 [EESIPR] = 0x0030,
299 [TRSCER] = 0x0038,
300 [RMFCR] = 0x0040,
301 [TFTR] = 0x0048,
302 [FDR] = 0x0050,
303 [RMCR] = 0x0058,
304 [TFUCR] = 0x0064,
305 [RFOCR] = 0x0068,
306 [FCFTR] = 0x0070,
307 [RPADIR] = 0x0078,
308 [TRIMD] = 0x007c,
309 [RBWAR] = 0x00c8,
310 [RDFAR] = 0x00cc,
311 [TBRAR] = 0x00d4,
312 [TDFAR] = 0x00d8,
313};
314
315static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000316 SH_ETH_OFFSET_DEFAULTS,
317
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400318 [EDMR] = 0x0000,
319 [EDTRR] = 0x0004,
320 [EDRRR] = 0x0008,
321 [TDLAR] = 0x000c,
322 [RDLAR] = 0x0010,
323 [EESR] = 0x0014,
324 [EESIPR] = 0x0018,
325 [TRSCER] = 0x001c,
326 [RMFCR] = 0x0020,
327 [TFTR] = 0x0024,
328 [FDR] = 0x0028,
329 [RMCR] = 0x002c,
330 [EDOCR] = 0x0030,
331 [FCFTR] = 0x0034,
332 [RPADIR] = 0x0038,
333 [TRIMD] = 0x003c,
334 [RBWAR] = 0x0040,
335 [RDFAR] = 0x0044,
336 [TBRAR] = 0x004c,
337 [TDFAR] = 0x0050,
338
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000339 [ECMR] = 0x0160,
340 [ECSR] = 0x0164,
341 [ECSIPR] = 0x0168,
342 [PIR] = 0x016c,
343 [MAHR] = 0x0170,
344 [MALR] = 0x0174,
345 [RFLR] = 0x0178,
346 [PSR] = 0x017c,
347 [TROCR] = 0x0180,
348 [CDCR] = 0x0184,
349 [LCCR] = 0x0188,
350 [CNDCR] = 0x018c,
351 [CEFCR] = 0x0194,
352 [FRECR] = 0x0198,
353 [TSFRCR] = 0x019c,
354 [TLFRCR] = 0x01a0,
355 [RFCR] = 0x01a4,
356 [MAFCR] = 0x01a8,
357 [IPGR] = 0x01b4,
358 [APR] = 0x01b8,
359 [MPR] = 0x01bc,
360 [TPAUSER] = 0x01c4,
361 [BCFR] = 0x01cc,
362
363 [ARSTR] = 0x0000,
364 [TSU_CTRST] = 0x0004,
365 [TSU_FWEN0] = 0x0010,
366 [TSU_FWEN1] = 0x0014,
367 [TSU_FCM] = 0x0018,
368 [TSU_BSYSL0] = 0x0020,
369 [TSU_BSYSL1] = 0x0024,
370 [TSU_PRISL0] = 0x0028,
371 [TSU_PRISL1] = 0x002c,
372 [TSU_FWSL0] = 0x0030,
373 [TSU_FWSL1] = 0x0034,
374 [TSU_FWSLC] = 0x0038,
375 [TSU_QTAGM0] = 0x0040,
376 [TSU_QTAGM1] = 0x0044,
377 [TSU_ADQT0] = 0x0048,
378 [TSU_ADQT1] = 0x004c,
379 [TSU_FWSR] = 0x0050,
380 [TSU_FWINMK] = 0x0054,
381 [TSU_ADSBSY] = 0x0060,
382 [TSU_TEN] = 0x0064,
383 [TSU_POST1] = 0x0070,
384 [TSU_POST2] = 0x0074,
385 [TSU_POST3] = 0x0078,
386 [TSU_POST4] = 0x007c,
387
388 [TXNLCR0] = 0x0080,
389 [TXALCR0] = 0x0084,
390 [RXNLCR0] = 0x0088,
391 [RXALCR0] = 0x008c,
392 [FWNLCR0] = 0x0090,
393 [FWALCR0] = 0x0094,
394 [TXNLCR1] = 0x00a0,
395 [TXALCR1] = 0x00a0,
396 [RXNLCR1] = 0x00a8,
397 [RXALCR1] = 0x00ac,
398 [FWNLCR1] = 0x00b0,
399 [FWALCR1] = 0x00b4,
400
401 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000402};
403
Ben Hutchings740c7f32015-01-27 00:49:32 +0000404static void sh_eth_rcv_snd_disable(struct net_device *ndev);
405static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
406
Simon Horman504c8ca2014-01-17 09:22:27 +0900407static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000408{
Simon Horman504c8ca2014-01-17 09:22:27 +0900409 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000410}
411
Simon Hormandb893472014-01-17 09:22:28 +0900412static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
413{
414 return mdp->reg_offset == sh_eth_offset_fast_rz;
415}
416
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400417static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000418{
419 u32 value = 0x0;
420 struct sh_eth_private *mdp = netdev_priv(ndev);
421
422 switch (mdp->phy_interface) {
423 case PHY_INTERFACE_MODE_GMII:
424 value = 0x2;
425 break;
426 case PHY_INTERFACE_MODE_MII:
427 value = 0x1;
428 break;
429 case PHY_INTERFACE_MODE_RMII:
430 value = 0x0;
431 break;
432 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300433 netdev_warn(ndev,
434 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000435 value = 0x1;
436 break;
437 }
438
439 sh_eth_write(ndev, value, RMII_MII);
440}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000441
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400442static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000443{
444 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000445
446 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000447 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000448 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000449 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000450}
451
Geert Uytterhoeven99f84be2015-11-24 15:40:57 +0100452static void sh_eth_chip_reset(struct net_device *ndev)
453{
454 struct sh_eth_private *mdp = netdev_priv(ndev);
455
456 /* reset device */
457 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
458 mdelay(1);
459}
460
461#ifdef CONFIG_OF
462/* R7S72100 */
463static struct sh_eth_cpu_data r7s72100_data = {
464 .chip_reset = sh_eth_chip_reset,
465 .set_duplex = sh_eth_set_duplex,
466
467 .register_type = SH_ETH_REG_FAST_RZ,
468
469 .ecsr_value = ECSR_ICD,
470 .ecsipr_value = ECSIPR_ICDIP,
471 .eesipr_value = 0xff7f009f,
472
473 .tx_check = EESR_TC1 | EESR_FTC,
474 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
475 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
476 EESR_TDE | EESR_ECI,
477 .fdr_value = 0x0000070f,
478
479 .no_psr = 1,
480 .apr = 1,
481 .mpr = 1,
482 .tpauser = 1,
483 .hw_swap = 1,
484 .rpadir = 1,
485 .rpadir_value = 2 << 16,
486 .no_trimd = 1,
487 .no_ade = 1,
488 .hw_crc = 1,
489 .tsu = 1,
490 .shift_rd0 = 1,
491};
492#endif /* CONFIG_OF */
493
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000494/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000495static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000496{
497 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000498
499 switch (mdp->speed) {
500 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000501 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000502 break;
503 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000504 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
505 break;
506 default:
507 break;
508 }
509}
510
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000511/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000512static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000513 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000514 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000515
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400516 .register_type = SH_ETH_REG_FAST_RCAR,
517
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000518 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
519 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
520 .eesipr_value = 0x01ff009f,
521
522 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400523 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
524 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
525 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900526 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000527
528 .apr = 1,
529 .mpr = 1,
530 .tpauser = 1,
531 .hw_swap = 1,
532};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000533
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100534#ifdef CONFIG_OF
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300535/* R8A7790/1 */
536static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900537 .set_duplex = sh_eth_set_duplex,
538 .set_rate = sh_eth_set_rate_r8a777x,
539
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400540 .register_type = SH_ETH_REG_FAST_RCAR,
541
Simon Hormane18dbf72013-07-23 10:18:05 +0900542 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
543 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
544 .eesipr_value = 0x01ff009f,
545
546 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900547 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
548 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
549 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900550 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900551
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100552 .trscer_err_mask = DESC_I_RINT8,
553
Simon Hormane18dbf72013-07-23 10:18:05 +0900554 .apr = 1,
555 .mpr = 1,
556 .tpauser = 1,
557 .hw_swap = 1,
558 .rmiimode = 1,
559};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100560#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900561
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000562static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000563{
564 struct sh_eth_private *mdp = netdev_priv(ndev);
565
566 switch (mdp->speed) {
567 case 10: /* 10BASE */
568 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
569 break;
570 case 100:/* 100BASE */
571 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000572 break;
573 default:
574 break;
575 }
576}
577
578/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000579static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000580 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000581 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000582
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400583 .register_type = SH_ETH_REG_FAST_SH4,
584
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000585 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
586 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400587 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000588
589 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400590 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
591 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
592 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000593
594 .apr = 1,
595 .mpr = 1,
596 .tpauser = 1,
597 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800598 .rpadir = 1,
599 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000600};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000601
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000602static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000603{
604 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000605
606 switch (mdp->speed) {
607 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000608 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000609 break;
610 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000611 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000612 break;
613 default:
614 break;
615 }
616}
617
618/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000619static struct sh_eth_cpu_data sh7757_data = {
620 .set_duplex = sh_eth_set_duplex,
621 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000622
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400623 .register_type = SH_ETH_REG_FAST_SH4,
624
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000625 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000626
627 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400628 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
629 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
630 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000631
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000632 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000633 .apr = 1,
634 .mpr = 1,
635 .tpauser = 1,
636 .hw_swap = 1,
637 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000638 .rpadir = 1,
639 .rpadir_value = 2 << 16,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000640 .rtrate = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000641};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000642
David S. Millere403d292013-06-07 23:40:41 -0700643#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000644#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
645#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
646static void sh_eth_chip_reset_giga(struct net_device *ndev)
647{
648 int i;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100649 u32 mahr[2], malr[2];
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000650
651 /* save MAHR and MALR */
652 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000653 malr[i] = ioread32((void *)GIGA_MALR(i));
654 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000655 }
656
657 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000658 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000659 mdelay(1);
660
661 /* restore MAHR and MALR */
662 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000663 iowrite32(malr[i], (void *)GIGA_MALR(i));
664 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000665 }
666}
667
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000668static void sh_eth_set_rate_giga(struct net_device *ndev)
669{
670 struct sh_eth_private *mdp = netdev_priv(ndev);
671
672 switch (mdp->speed) {
673 case 10: /* 10BASE */
674 sh_eth_write(ndev, 0x00000000, GECMR);
675 break;
676 case 100:/* 100BASE */
677 sh_eth_write(ndev, 0x00000010, GECMR);
678 break;
679 case 1000: /* 1000BASE */
680 sh_eth_write(ndev, 0x00000020, GECMR);
681 break;
682 default:
683 break;
684 }
685}
686
687/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000688static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000689 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000690 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000691 .set_rate = sh_eth_set_rate_giga,
692
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400693 .register_type = SH_ETH_REG_GIGABIT,
694
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000695 .ecsr_value = ECSR_ICD | ECSR_MPD,
696 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
697 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
698
699 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400700 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
701 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
702 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000703 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000704
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000705 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000706 .apr = 1,
707 .mpr = 1,
708 .tpauser = 1,
709 .bculr = 1,
710 .hw_swap = 1,
711 .rpadir = 1,
712 .rpadir_value = 2 << 16,
713 .no_trimd = 1,
714 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000715 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000716};
717
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000718static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000719{
720 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000721
722 switch (mdp->speed) {
723 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000724 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000725 break;
726 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000727 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000728 break;
729 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000730 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000731 break;
732 default:
733 break;
734 }
735}
736
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000737/* SH7734 */
738static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000739 .chip_reset = sh_eth_chip_reset,
740 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000741 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000742
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400743 .register_type = SH_ETH_REG_GIGABIT,
744
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000745 .ecsr_value = ECSR_ICD | ECSR_MPD,
746 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
747 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
748
749 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400750 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
751 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
752 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000753
754 .apr = 1,
755 .mpr = 1,
756 .tpauser = 1,
757 .bculr = 1,
758 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000759 .no_trimd = 1,
760 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000761 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000762 .hw_crc = 1,
763 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000764};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000765
766/* SH7763 */
767static struct sh_eth_cpu_data sh7763_data = {
768 .chip_reset = sh_eth_chip_reset,
769 .set_duplex = sh_eth_set_duplex,
770 .set_rate = sh_eth_set_rate_gether,
771
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400772 .register_type = SH_ETH_REG_GIGABIT,
773
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000774 .ecsr_value = ECSR_ICD | ECSR_MPD,
775 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
776 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
777
778 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300779 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
780 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000781 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000782
783 .apr = 1,
784 .mpr = 1,
785 .tpauser = 1,
786 .bculr = 1,
787 .hw_swap = 1,
788 .no_trimd = 1,
789 .no_ade = 1,
790 .tsu = 1,
791 .irq_flags = IRQF_SHARED,
792};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000793
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000794static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000795{
796 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000797
798 /* reset device */
799 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
800 mdelay(1);
801
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000802 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000803}
804
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000805/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000806static struct sh_eth_cpu_data r8a7740_data = {
807 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000808 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000809 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000810
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400811 .register_type = SH_ETH_REG_GIGABIT,
812
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000813 .ecsr_value = ECSR_ICD | ECSR_MPD,
814 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
815 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
816
817 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400818 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
819 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
820 EESR_TDE | EESR_ECI,
Simon Hormancc235282013-10-10 14:51:16 +0900821 .fdr_value = 0x0000070f,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000822
823 .apr = 1,
824 .mpr = 1,
825 .tpauser = 1,
826 .bculr = 1,
827 .hw_swap = 1,
Simon Hormancc235282013-10-10 14:51:16 +0900828 .rpadir = 1,
829 .rpadir_value = 2 << 16,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000830 .no_trimd = 1,
831 .no_ade = 1,
832 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000833 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400834 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000835};
836
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000837static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400838 .register_type = SH_ETH_REG_FAST_SH3_SH2,
839
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000840 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
841
842 .apr = 1,
843 .mpr = 1,
844 .tpauser = 1,
845 .hw_swap = 1,
846};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000847
848static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400849 .register_type = SH_ETH_REG_FAST_SH3_SH2,
850
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000851 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000852 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000853};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000854
855static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
856{
857 if (!cd->ecsr_value)
858 cd->ecsr_value = DEFAULT_ECSR_INIT;
859
860 if (!cd->ecsipr_value)
861 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
862
863 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300864 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000865 DEFAULT_FIFO_F_D_RFD;
866
867 if (!cd->fdr_value)
868 cd->fdr_value = DEFAULT_FDR_INIT;
869
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000870 if (!cd->tx_check)
871 cd->tx_check = DEFAULT_TX_CHECK;
872
873 if (!cd->eesr_err_check)
874 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900875
876 if (!cd->trscer_err_mask)
877 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000878}
879
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000880static int sh_eth_check_reset(struct net_device *ndev)
881{
882 int ret = 0;
883 int cnt = 100;
884
885 while (cnt > 0) {
886 if (!(sh_eth_read(ndev, EDMR) & 0x3))
887 break;
888 mdelay(1);
889 cnt--;
890 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400891 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300892 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000893 ret = -ETIMEDOUT;
894 }
895 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000896}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000897
898static int sh_eth_reset(struct net_device *ndev)
899{
900 struct sh_eth_private *mdp = netdev_priv(ndev);
901 int ret = 0;
902
Simon Hormandb893472014-01-17 09:22:28 +0900903 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000904 sh_eth_write(ndev, EDSR_ENALL, EDSR);
905 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
906 EDMR);
907
908 ret = sh_eth_check_reset(ndev);
909 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100910 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000911
912 /* Table Init */
913 sh_eth_write(ndev, 0x0, TDLAR);
914 sh_eth_write(ndev, 0x0, TDFAR);
915 sh_eth_write(ndev, 0x0, TDFXR);
916 sh_eth_write(ndev, 0x0, TDFFR);
917 sh_eth_write(ndev, 0x0, RDLAR);
918 sh_eth_write(ndev, 0x0, RDFAR);
919 sh_eth_write(ndev, 0x0, RDFXR);
920 sh_eth_write(ndev, 0x0, RDFFR);
921
922 /* Reset HW CRC register */
923 if (mdp->cd->hw_crc)
924 sh_eth_write(ndev, 0x0, CSMR);
925
926 /* Select MII mode */
927 if (mdp->cd->select_mii)
928 sh_eth_select_mii(ndev);
929 } else {
930 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
931 EDMR);
932 mdelay(3);
933 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
934 EDMR);
935 }
936
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000937 return ret;
938}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000939
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000940static void sh_eth_set_receive_align(struct sk_buff *skb)
941{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900942 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000943
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000944 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900945 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000946}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000947
948
Yoshinori Sato71557a32008-08-06 19:49:00 -0400949/* CPU <-> EDMAC endian convert */
950static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
951{
952 switch (mdp->edmac_endian) {
953 case EDMAC_LITTLE_ENDIAN:
954 return cpu_to_le32(x);
955 case EDMAC_BIG_ENDIAN:
956 return cpu_to_be32(x);
957 }
958 return x;
959}
960
961static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
962{
963 switch (mdp->edmac_endian) {
964 case EDMAC_LITTLE_ENDIAN:
965 return le32_to_cpu(x);
966 case EDMAC_BIG_ENDIAN:
967 return be32_to_cpu(x);
968 }
969 return x;
970}
971
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300972/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700973static void update_mac_address(struct net_device *ndev)
974{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000975 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300976 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
977 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000978 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300979 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700980}
981
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300982/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700983 *
984 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
985 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
986 * When you want use this device, you must set MAC address in bootloader.
987 *
988 */
Magnus Damm748031f2009-10-09 00:17:14 +0000989static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700990{
Magnus Damm748031f2009-10-09 00:17:14 +0000991 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700992 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000993 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000994 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
995 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
996 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
997 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
998 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
999 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +00001000 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001001}
1002
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001003static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001004{
Simon Hormandb893472014-01-17 09:22:28 +09001005 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001006 return EDTRR_TRNS_GETHER;
1007 else
1008 return EDTRR_TRNS_ETHER;
1009}
1010
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001011struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001012 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001013 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001014 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001015 u32 mmd_msk;/* MMD */
1016 u32 mdo_msk;
1017 u32 mdi_msk;
1018 u32 mdc_msk;
1019};
1020
1021/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001022static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001023{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001024 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001025}
1026
1027/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001028static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001030 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001031}
1032
1033/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001034static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001036 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001037}
1038
1039/* Data I/O pin control */
1040static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1041{
1042 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001043
1044 if (bitbang->set_gate)
1045 bitbang->set_gate(bitbang->addr);
1046
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001047 if (bit)
1048 bb_set(bitbang->addr, bitbang->mmd_msk);
1049 else
1050 bb_clr(bitbang->addr, bitbang->mmd_msk);
1051}
1052
1053/* Set bit data*/
1054static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1055{
1056 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1057
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001058 if (bitbang->set_gate)
1059 bitbang->set_gate(bitbang->addr);
1060
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001061 if (bit)
1062 bb_set(bitbang->addr, bitbang->mdo_msk);
1063 else
1064 bb_clr(bitbang->addr, bitbang->mdo_msk);
1065}
1066
1067/* Get bit data*/
1068static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1069{
1070 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001071
1072 if (bitbang->set_gate)
1073 bitbang->set_gate(bitbang->addr);
1074
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001075 return bb_read(bitbang->addr, bitbang->mdi_msk);
1076}
1077
1078/* MDC pin control */
1079static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1080{
1081 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1082
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001083 if (bitbang->set_gate)
1084 bitbang->set_gate(bitbang->addr);
1085
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001086 if (bit)
1087 bb_set(bitbang->addr, bitbang->mdc_msk);
1088 else
1089 bb_clr(bitbang->addr, bitbang->mdc_msk);
1090}
1091
1092/* mdio bus control struct */
1093static struct mdiobb_ops bb_ops = {
1094 .owner = THIS_MODULE,
1095 .set_mdc = sh_mdc_ctrl,
1096 .set_mdio_dir = sh_mmd_ctrl,
1097 .set_mdio_data = sh_set_mdio,
1098 .get_mdio_data = sh_get_mdio,
1099};
1100
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001101/* free skb and descriptor buffer */
1102static void sh_eth_ring_free(struct net_device *ndev)
1103{
1104 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001105 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001106
1107 /* Free Rx skb ringbuffer */
1108 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001109 for (i = 0; i < mdp->num_rx_ring; i++)
1110 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001111 }
1112 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001113 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001114
1115 /* Free Tx skb ringbuffer */
1116 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001117 for (i = 0; i < mdp->num_tx_ring; i++)
1118 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001119 }
1120 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001121 mdp->tx_skbuff = NULL;
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001122
1123 if (mdp->rx_ring) {
1124 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1125 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1126 mdp->rx_desc_dma);
1127 mdp->rx_ring = NULL;
1128 }
1129
1130 if (mdp->tx_ring) {
1131 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1132 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1133 mdp->tx_desc_dma);
1134 mdp->tx_ring = NULL;
1135 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001136}
1137
1138/* format skb and descriptor buffer */
1139static void sh_eth_ring_format(struct net_device *ndev)
1140{
1141 struct sh_eth_private *mdp = netdev_priv(ndev);
1142 int i;
1143 struct sk_buff *skb;
1144 struct sh_eth_rxdesc *rxdesc = NULL;
1145 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001146 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1147 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001148 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001149 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001150
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001151 mdp->cur_rx = 0;
1152 mdp->cur_tx = 0;
1153 mdp->dirty_rx = 0;
1154 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155
1156 memset(mdp->rx_ring, 0, rx_ringsize);
1157
1158 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001159 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001160 /* skb */
1161 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001162 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001163 if (skb == NULL)
1164 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001165 sh_eth_set_receive_align(skb);
1166
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001167 /* RX descriptor */
1168 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001169 /* The size of the buffer is a multiple of 32 bytes. */
1170 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 32);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001171 dma_addr = dma_map_single(&ndev->dev, skb->data,
1172 rxdesc->buffer_length,
1173 DMA_FROM_DEVICE);
1174 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1175 kfree_skb(skb);
1176 break;
1177 }
1178 mdp->rx_skbuff[i] = skb;
1179 rxdesc->addr = dma_addr;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001180 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001181
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001182 /* Rx descriptor address set */
1183 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001184 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001185 if (sh_eth_is_gether(mdp) ||
1186 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001187 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001188 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001189 }
1190
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001191 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001192
1193 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc2380412015-11-03 01:28:07 +03001194 rxdesc->status |= cpu_to_edmac(mdp, RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001195
1196 memset(mdp->tx_ring, 0, tx_ringsize);
1197
1198 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001199 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001200 mdp->tx_skbuff[i] = NULL;
1201 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001202 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001204 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001205 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001206 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001207 if (sh_eth_is_gether(mdp) ||
1208 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001209 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001210 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001211 }
1212
Yoshinori Sato71557a32008-08-06 19:49:00 -04001213 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214}
1215
1216/* Get skb and descriptor buffer */
1217static int sh_eth_ring_init(struct net_device *ndev)
1218{
1219 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001220 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001221
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001222 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001223 * card needs room to do 8 byte alignment, +2 so we can reserve
1224 * the first 2 bytes, and +16 gets room for the status word from the
1225 * card.
1226 */
1227 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1228 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001229 if (mdp->cd->rpadir)
1230 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231
1232 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001233 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1234 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001235 if (!mdp->rx_skbuff)
1236 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001237
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001238 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1239 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001240 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001241 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001242
1243 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001244 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001245 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001246 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001247 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001248 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001249
1250 mdp->dirty_rx = 0;
1251
1252 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001253 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001254 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001255 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001256 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001257 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001258 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001259
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001260ring_free:
1261 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001262 sh_eth_ring_free(ndev);
1263
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001264 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001265}
1266
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001267static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001268{
1269 int ret = 0;
1270 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001271 u32 val;
1272
1273 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001274 ret = sh_eth_reset(ndev);
1275 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001276 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001277
Simon Horman55754f12013-07-23 10:18:04 +09001278 if (mdp->cd->rmiimode)
1279 sh_eth_write(ndev, 0x1, RMIIMODE);
1280
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001281 /* Descriptor format */
1282 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001283 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001284 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285
1286 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001287 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001288
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001289#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001290 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001291 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001292 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001293#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001294 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001295
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001296 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001297 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1298 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001299
Ben Dooks530aa2d2014-06-03 12:21:13 +01001300 /* Frame recv control (enable multiple-packets per rx irq) */
1301 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001302
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001303 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001304
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001305 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001306 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001307
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001308 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001309
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001310 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001311 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001312
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001313 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001314 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1315 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001316
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001317 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001318 if (start) {
1319 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001320 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001321 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001322
1323 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001324 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001325 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1326
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001327 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001328
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001329 if (mdp->cd->set_rate)
1330 mdp->cd->set_rate(ndev);
1331
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001332 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001333 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001334
1335 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001336 if (start)
1337 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001338
1339 /* Set MAC address */
1340 update_mac_address(ndev);
1341
1342 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001343 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001344 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001345 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001346 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001347 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001348 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001349
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001350 if (start) {
1351 /* Setting the Rx mode will start the Rx process. */
1352 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001353
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001354 netif_start_queue(ndev);
1355 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001356
1357 return ret;
1358}
1359
Ben Hutchings740c7f32015-01-27 00:49:32 +00001360static void sh_eth_dev_exit(struct net_device *ndev)
1361{
1362 struct sh_eth_private *mdp = netdev_priv(ndev);
1363 int i;
1364
1365 /* Deactivate all TX descriptors, so DMA should stop at next
1366 * packet boundary if it's currently running
1367 */
1368 for (i = 0; i < mdp->num_tx_ring; i++)
1369 mdp->tx_ring[i].status &= ~cpu_to_edmac(mdp, TD_TACT);
1370
1371 /* Disable TX FIFO egress to MAC */
1372 sh_eth_rcv_snd_disable(ndev);
1373
1374 /* Stop RX DMA at next packet boundary */
1375 sh_eth_write(ndev, 0, EDRRR);
1376
1377 /* Aside from TX DMA, we can't tell when the hardware is
1378 * really stopped, so we need to reset to make sure.
1379 * Before doing that, wait for long enough to *probably*
1380 * finish transmitting the last packet and poll stats.
1381 */
1382 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1383 sh_eth_get_stats(ndev);
1384 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001385
1386 /* Set MAC address again */
1387 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001388}
1389
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390/* free Tx skb function */
1391static int sh_eth_txfree(struct net_device *ndev)
1392{
1393 struct sh_eth_private *mdp = netdev_priv(ndev);
1394 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001395 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001396 int entry = 0;
1397
1398 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001399 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001400 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001401 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001402 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001403 /* TACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001404 dma_rmb();
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001405 netif_info(mdp, tx_done, ndev,
1406 "tx entry %d status 0x%08x\n",
1407 entry, edmac_to_cpu(mdp, txdesc->status));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001408 /* Free the original skb. */
1409 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001410 dma_unmap_single(&ndev->dev, txdesc->addr,
1411 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1413 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001414 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001415 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001416 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001417 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001418 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001419
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001420 ndev->stats.tx_packets++;
1421 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001423 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001424}
1425
1426/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001427static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428{
1429 struct sh_eth_private *mdp = netdev_priv(ndev);
1430 struct sh_eth_rxdesc *rxdesc;
1431
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001432 int entry = mdp->cur_rx % mdp->num_rx_ring;
1433 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001434 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001435 struct sk_buff *skb;
1436 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001437 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001438 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001439 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001440
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001441 boguscnt = min(boguscnt, *quota);
1442 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001443 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001444 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001445 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001446 dma_rmb();
Yoshinori Sato71557a32008-08-06 19:49:00 -04001447 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448 pkt_len = rxdesc->frame_length;
1449
1450 if (--boguscnt < 0)
1451 break;
1452
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001453 netif_info(mdp, rx_status, ndev,
1454 "rx entry %d status 0x%08x len %d\n",
1455 entry, desc_status, pkt_len);
1456
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001457 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001458 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001459
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001460 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001461 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001462 * bit 0. However, in case of the R8A7740 and R7S72100
1463 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001464 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001465 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001466 if (mdp->cd->shift_rd0)
1467 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001468
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001469 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1470 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001471 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001472 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001473 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001474 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001475 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001476 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001477 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001478 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001479 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001481 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001482 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001483 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001484 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001485 if (!mdp->cd->hw_swap)
1486 sh_eth_soft_swap(
1487 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1488 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001489 skb = mdp->rx_skbuff[entry];
1490 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001491 if (mdp->cd->rpadir)
1492 skb_reserve(skb, NET_IP_ALIGN);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001493 dma_unmap_single(&ndev->dev, rxdesc->addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001494 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001495 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001496 skb_put(skb, pkt_len);
1497 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001498 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001499 ndev->stats.rx_packets++;
1500 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001501 if (desc_status & RD_RFS8)
1502 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001503 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001504 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001505 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506 }
1507
1508 /* Refill the Rx ring buffers. */
1509 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001510 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001511 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001512 /* The size of the buffer is 32 byte boundary. */
1513 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 32);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001514
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001515 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001516 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001517 if (skb == NULL)
1518 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001519 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001520 dma_addr = dma_map_single(&ndev->dev, skb->data,
1521 rxdesc->buffer_length,
1522 DMA_FROM_DEVICE);
1523 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1524 kfree_skb(skb);
1525 break;
1526 }
1527 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001528
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001529 skb_checksum_none_assert(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001530 rxdesc->addr = dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001532 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001533 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001534 rxdesc->status |=
Sergei Shtylyovc2380412015-11-03 01:28:07 +03001535 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001536 else
1537 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001538 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001539 }
1540
1541 /* Restart Rx engine if stopped. */
1542 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001543 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001544 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001545 if (intr_status & EESR_RDE &&
1546 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001547 u32 count = (sh_eth_read(ndev, RDFAR) -
1548 sh_eth_read(ndev, RDLAR)) >> 4;
1549
1550 mdp->cur_rx = count;
1551 mdp->dirty_rx = count;
1552 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001553 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001554 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001555
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001556 *quota -= limit - boguscnt - 1;
1557
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001558 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001559}
1560
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001561static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001562{
1563 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001564 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1565 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001566}
1567
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001568static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001569{
1570 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001571 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1572 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001573}
1574
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001576static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001577{
1578 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001580 u32 link_stat;
1581 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001582
1583 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001584 felic_stat = sh_eth_read(ndev, ECSR);
1585 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001586 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001587 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001588 if (felic_stat & ECSR_LCHNG) {
1589 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001590 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001591 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001592 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001593 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001594 if (mdp->ether_link_active_low)
1595 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001596 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001597 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001598 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001599 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001600 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001601 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001602 ~DMAC_M_ECI, EESIPR);
1603 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001604 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001605 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001606 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001607 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001608 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001609 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610 }
1611 }
1612 }
1613
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001614ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001615 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001616 /* Unused write back interrupt */
1617 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001618 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001619 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001620 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001621 }
1622
1623 if (intr_status & EESR_RABT) {
1624 /* Receive Abort int */
1625 if (intr_status & EESR_RFRMER) {
1626 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001627 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001628 }
1629 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001630
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001631 if (intr_status & EESR_TDE) {
1632 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001633 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001634 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001635 }
1636
1637 if (intr_status & EESR_TFE) {
1638 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001639 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001640 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001641 }
1642
1643 if (intr_status & EESR_RDE) {
1644 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001645 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001646 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001647
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001648 if (intr_status & EESR_RFE) {
1649 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001650 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001651 }
1652
1653 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1654 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001655 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001656 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001657 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001658
1659 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1660 if (mdp->cd->no_ade)
1661 mask &= ~EESR_ADE;
1662 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001663 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001664 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001665
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001666 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001667 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1668 intr_status, mdp->cur_tx, mdp->dirty_tx,
1669 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001670 /* dirty buffer free */
1671 sh_eth_txfree(ndev);
1672
1673 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001674 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001675 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001676 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001677 }
1678 /* wakeup */
1679 netif_wake_queue(ndev);
1680 }
1681}
1682
1683static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1684{
1685 struct net_device *ndev = netdev;
1686 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001687 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001688 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001689 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001690
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001691 spin_lock(&mdp->lock);
1692
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001693 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001694 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001695 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1696 * enabled since it's the one that comes thru regardless of the mask,
1697 * and we need to fully handle it in sh_eth_error() in order to quench
1698 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1699 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001700 intr_enable = sh_eth_read(ndev, EESIPR);
1701 intr_status &= intr_enable | DMAC_M_ECI;
1702 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001703 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001704 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001705 goto out;
1706
1707 if (!likely(mdp->irq_enabled)) {
1708 sh_eth_write(ndev, 0, EESIPR);
1709 goto out;
1710 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001711
Sergei Shtylyov37191092013-06-19 23:30:23 +04001712 if (intr_status & EESR_RX_CHECK) {
1713 if (napi_schedule_prep(&mdp->napi)) {
1714 /* Mask Rx interrupts */
1715 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1716 EESIPR);
1717 __napi_schedule(&mdp->napi);
1718 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001719 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001720 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001721 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001722 }
1723 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001724
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001725 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001726 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001727 /* Clear Tx interrupts */
1728 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1729
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001730 sh_eth_txfree(ndev);
1731 netif_wake_queue(ndev);
1732 }
1733
Sergei Shtylyov37191092013-06-19 23:30:23 +04001734 if (intr_status & cd->eesr_err_check) {
1735 /* Clear error interrupts */
1736 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1737
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001738 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001739 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001740
Ben Hutchings283e38d2015-01-22 12:44:08 +00001741out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001742 spin_unlock(&mdp->lock);
1743
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001744 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001745}
1746
Sergei Shtylyov37191092013-06-19 23:30:23 +04001747static int sh_eth_poll(struct napi_struct *napi, int budget)
1748{
1749 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1750 napi);
1751 struct net_device *ndev = napi->dev;
1752 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001753 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001754
1755 for (;;) {
1756 intr_status = sh_eth_read(ndev, EESR);
1757 if (!(intr_status & EESR_RX_CHECK))
1758 break;
1759 /* Clear Rx interrupts */
1760 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1761
1762 if (sh_eth_rx(ndev, intr_status, &quota))
1763 goto out;
1764 }
1765
1766 napi_complete(napi);
1767
1768 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001769 if (mdp->irq_enabled)
1770 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001771out:
1772 return budget - quota;
1773}
1774
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001775/* PHY state control function */
1776static void sh_eth_adjust_link(struct net_device *ndev)
1777{
1778 struct sh_eth_private *mdp = netdev_priv(ndev);
1779 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001780 int new_state = 0;
1781
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001782 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001783 if (phydev->duplex != mdp->duplex) {
1784 new_state = 1;
1785 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001786 if (mdp->cd->set_duplex)
1787 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001788 }
1789
1790 if (phydev->speed != mdp->speed) {
1791 new_state = 1;
1792 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001793 if (mdp->cd->set_rate)
1794 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001795 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001796 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001797 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001798 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1799 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800 new_state = 1;
1801 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001802 if (mdp->cd->no_psr || mdp->no_ether_link)
1803 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001804 }
1805 } else if (mdp->link) {
1806 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001807 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001808 mdp->speed = 0;
1809 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001810 if (mdp->cd->no_psr || mdp->no_ether_link)
1811 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001812 }
1813
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001814 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001815 phy_print_status(phydev);
1816}
1817
1818/* PHY init function */
1819static int sh_eth_phy_init(struct net_device *ndev)
1820{
Ben Dooks702eca02014-03-12 17:47:40 +00001821 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001822 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001823 struct phy_device *phydev = NULL;
1824
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001825 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001826 mdp->speed = 0;
1827 mdp->duplex = -1;
1828
1829 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001830 if (np) {
1831 struct device_node *pn;
1832
1833 pn = of_parse_phandle(np, "phy-handle", 0);
1834 phydev = of_phy_connect(ndev, pn,
1835 sh_eth_adjust_link, 0,
1836 mdp->phy_interface);
1837
1838 if (!phydev)
1839 phydev = ERR_PTR(-ENOENT);
1840 } else {
1841 char phy_id[MII_BUS_ID_SIZE + 3];
1842
1843 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1844 mdp->mii_bus->id, mdp->phy_id);
1845
1846 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1847 mdp->phy_interface);
1848 }
1849
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001850 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001851 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001852 return PTR_ERR(phydev);
1853 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001854
Sergei Shtylyovda246852014-03-15 03:29:14 +03001855 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1856 phydev->addr, phydev->irq, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001857
1858 mdp->phydev = phydev;
1859
1860 return 0;
1861}
1862
1863/* PHY control start function */
1864static int sh_eth_phy_start(struct net_device *ndev)
1865{
1866 struct sh_eth_private *mdp = netdev_priv(ndev);
1867 int ret;
1868
1869 ret = sh_eth_phy_init(ndev);
1870 if (ret)
1871 return ret;
1872
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001873 phy_start(mdp->phydev);
1874
1875 return 0;
1876}
1877
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001878static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001879 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001880{
1881 struct sh_eth_private *mdp = netdev_priv(ndev);
1882 unsigned long flags;
1883 int ret;
1884
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001885 if (!mdp->phydev)
1886 return -ENODEV;
1887
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001888 spin_lock_irqsave(&mdp->lock, flags);
1889 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1890 spin_unlock_irqrestore(&mdp->lock, flags);
1891
1892 return ret;
1893}
1894
1895static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001896 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001897{
1898 struct sh_eth_private *mdp = netdev_priv(ndev);
1899 unsigned long flags;
1900 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001901
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001902 if (!mdp->phydev)
1903 return -ENODEV;
1904
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001905 spin_lock_irqsave(&mdp->lock, flags);
1906
1907 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001908 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001909
1910 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1911 if (ret)
1912 goto error_exit;
1913
1914 if (ecmd->duplex == DUPLEX_FULL)
1915 mdp->duplex = 1;
1916 else
1917 mdp->duplex = 0;
1918
1919 if (mdp->cd->set_duplex)
1920 mdp->cd->set_duplex(ndev);
1921
1922error_exit:
1923 mdelay(1);
1924
1925 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001926 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001927
1928 spin_unlock_irqrestore(&mdp->lock, flags);
1929
1930 return ret;
1931}
1932
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00001933/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1934 * version must be bumped as well. Just adding registers up to that
1935 * limit is fine, as long as the existing register indices don't
1936 * change.
1937 */
1938#define SH_ETH_REG_DUMP_VERSION 1
1939#define SH_ETH_REG_DUMP_MAX_REGS 256
1940
1941static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1942{
1943 struct sh_eth_private *mdp = netdev_priv(ndev);
1944 struct sh_eth_cpu_data *cd = mdp->cd;
1945 u32 *valid_map;
1946 size_t len;
1947
1948 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1949
1950 /* Dump starts with a bitmap that tells ethtool which
1951 * registers are defined for this chip.
1952 */
1953 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1954 if (buf) {
1955 valid_map = buf;
1956 buf += len;
1957 } else {
1958 valid_map = NULL;
1959 }
1960
1961 /* Add a register to the dump, if it has a defined offset.
1962 * This automatically skips most undefined registers, but for
1963 * some it is also necessary to check a capability flag in
1964 * struct sh_eth_cpu_data.
1965 */
1966#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1967#define add_reg_from(reg, read_expr) do { \
1968 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1969 if (buf) { \
1970 mark_reg_valid(reg); \
1971 *buf++ = read_expr; \
1972 } \
1973 ++len; \
1974 } \
1975 } while (0)
1976#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1977#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1978
1979 add_reg(EDSR);
1980 add_reg(EDMR);
1981 add_reg(EDTRR);
1982 add_reg(EDRRR);
1983 add_reg(EESR);
1984 add_reg(EESIPR);
1985 add_reg(TDLAR);
1986 add_reg(TDFAR);
1987 add_reg(TDFXR);
1988 add_reg(TDFFR);
1989 add_reg(RDLAR);
1990 add_reg(RDFAR);
1991 add_reg(RDFXR);
1992 add_reg(RDFFR);
1993 add_reg(TRSCER);
1994 add_reg(RMFCR);
1995 add_reg(TFTR);
1996 add_reg(FDR);
1997 add_reg(RMCR);
1998 add_reg(TFUCR);
1999 add_reg(RFOCR);
2000 if (cd->rmiimode)
2001 add_reg(RMIIMODE);
2002 add_reg(FCFTR);
2003 if (cd->rpadir)
2004 add_reg(RPADIR);
2005 if (!cd->no_trimd)
2006 add_reg(TRIMD);
2007 add_reg(ECMR);
2008 add_reg(ECSR);
2009 add_reg(ECSIPR);
2010 add_reg(PIR);
2011 if (!cd->no_psr)
2012 add_reg(PSR);
2013 add_reg(RDMLR);
2014 add_reg(RFLR);
2015 add_reg(IPGR);
2016 if (cd->apr)
2017 add_reg(APR);
2018 if (cd->mpr)
2019 add_reg(MPR);
2020 add_reg(RFCR);
2021 add_reg(RFCF);
2022 if (cd->tpauser)
2023 add_reg(TPAUSER);
2024 add_reg(TPAUSECR);
2025 add_reg(GECMR);
2026 if (cd->bculr)
2027 add_reg(BCULR);
2028 add_reg(MAHR);
2029 add_reg(MALR);
2030 add_reg(TROCR);
2031 add_reg(CDCR);
2032 add_reg(LCCR);
2033 add_reg(CNDCR);
2034 add_reg(CEFCR);
2035 add_reg(FRECR);
2036 add_reg(TSFRCR);
2037 add_reg(TLFRCR);
2038 add_reg(CERCR);
2039 add_reg(CEECR);
2040 add_reg(MAFCR);
2041 if (cd->rtrate)
2042 add_reg(RTRATE);
2043 if (cd->hw_crc)
2044 add_reg(CSMR);
2045 if (cd->select_mii)
2046 add_reg(RMII_MII);
2047 add_reg(ARSTR);
2048 if (cd->tsu) {
2049 add_tsu_reg(TSU_CTRST);
2050 add_tsu_reg(TSU_FWEN0);
2051 add_tsu_reg(TSU_FWEN1);
2052 add_tsu_reg(TSU_FCM);
2053 add_tsu_reg(TSU_BSYSL0);
2054 add_tsu_reg(TSU_BSYSL1);
2055 add_tsu_reg(TSU_PRISL0);
2056 add_tsu_reg(TSU_PRISL1);
2057 add_tsu_reg(TSU_FWSL0);
2058 add_tsu_reg(TSU_FWSL1);
2059 add_tsu_reg(TSU_FWSLC);
2060 add_tsu_reg(TSU_QTAG0);
2061 add_tsu_reg(TSU_QTAG1);
2062 add_tsu_reg(TSU_QTAGM0);
2063 add_tsu_reg(TSU_QTAGM1);
2064 add_tsu_reg(TSU_FWSR);
2065 add_tsu_reg(TSU_FWINMK);
2066 add_tsu_reg(TSU_ADQT0);
2067 add_tsu_reg(TSU_ADQT1);
2068 add_tsu_reg(TSU_VTAG0);
2069 add_tsu_reg(TSU_VTAG1);
2070 add_tsu_reg(TSU_ADSBSY);
2071 add_tsu_reg(TSU_TEN);
2072 add_tsu_reg(TSU_POST1);
2073 add_tsu_reg(TSU_POST2);
2074 add_tsu_reg(TSU_POST3);
2075 add_tsu_reg(TSU_POST4);
2076 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2077 /* This is the start of a table, not just a single
2078 * register.
2079 */
2080 if (buf) {
2081 unsigned int i;
2082
2083 mark_reg_valid(TSU_ADRH0);
2084 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2085 *buf++ = ioread32(
2086 mdp->tsu_addr +
2087 mdp->reg_offset[TSU_ADRH0] +
2088 i * 4);
2089 }
2090 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2091 }
2092 }
2093
2094#undef mark_reg_valid
2095#undef add_reg_from
2096#undef add_reg
2097#undef add_tsu_reg
2098
2099 return len * 4;
2100}
2101
2102static int sh_eth_get_regs_len(struct net_device *ndev)
2103{
2104 return __sh_eth_get_regs(ndev, NULL);
2105}
2106
2107static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2108 void *buf)
2109{
2110 struct sh_eth_private *mdp = netdev_priv(ndev);
2111
2112 regs->version = SH_ETH_REG_DUMP_VERSION;
2113
2114 pm_runtime_get_sync(&mdp->pdev->dev);
2115 __sh_eth_get_regs(ndev, buf);
2116 pm_runtime_put_sync(&mdp->pdev->dev);
2117}
2118
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002119static int sh_eth_nway_reset(struct net_device *ndev)
2120{
2121 struct sh_eth_private *mdp = netdev_priv(ndev);
2122 unsigned long flags;
2123 int ret;
2124
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002125 if (!mdp->phydev)
2126 return -ENODEV;
2127
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002128 spin_lock_irqsave(&mdp->lock, flags);
2129 ret = phy_start_aneg(mdp->phydev);
2130 spin_unlock_irqrestore(&mdp->lock, flags);
2131
2132 return ret;
2133}
2134
2135static u32 sh_eth_get_msglevel(struct net_device *ndev)
2136{
2137 struct sh_eth_private *mdp = netdev_priv(ndev);
2138 return mdp->msg_enable;
2139}
2140
2141static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2142{
2143 struct sh_eth_private *mdp = netdev_priv(ndev);
2144 mdp->msg_enable = value;
2145}
2146
2147static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2148 "rx_current", "tx_current",
2149 "rx_dirty", "tx_dirty",
2150};
2151#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2152
2153static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2154{
2155 switch (sset) {
2156 case ETH_SS_STATS:
2157 return SH_ETH_STATS_LEN;
2158 default:
2159 return -EOPNOTSUPP;
2160 }
2161}
2162
2163static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002164 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002165{
2166 struct sh_eth_private *mdp = netdev_priv(ndev);
2167 int i = 0;
2168
2169 /* device-specific stats */
2170 data[i++] = mdp->cur_rx;
2171 data[i++] = mdp->cur_tx;
2172 data[i++] = mdp->dirty_rx;
2173 data[i++] = mdp->dirty_tx;
2174}
2175
2176static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2177{
2178 switch (stringset) {
2179 case ETH_SS_STATS:
2180 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002181 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002182 break;
2183 }
2184}
2185
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002186static void sh_eth_get_ringparam(struct net_device *ndev,
2187 struct ethtool_ringparam *ring)
2188{
2189 struct sh_eth_private *mdp = netdev_priv(ndev);
2190
2191 ring->rx_max_pending = RX_RING_MAX;
2192 ring->tx_max_pending = TX_RING_MAX;
2193 ring->rx_pending = mdp->num_rx_ring;
2194 ring->tx_pending = mdp->num_tx_ring;
2195}
2196
2197static int sh_eth_set_ringparam(struct net_device *ndev,
2198 struct ethtool_ringparam *ring)
2199{
2200 struct sh_eth_private *mdp = netdev_priv(ndev);
2201 int ret;
2202
2203 if (ring->tx_pending > TX_RING_MAX ||
2204 ring->rx_pending > RX_RING_MAX ||
2205 ring->tx_pending < TX_RING_MIN ||
2206 ring->rx_pending < RX_RING_MIN)
2207 return -EINVAL;
2208 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2209 return -EINVAL;
2210
2211 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002212 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002213 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002214
Ben Hutchings283e38d2015-01-22 12:44:08 +00002215 /* Serialise with the interrupt handler and NAPI, then
2216 * disable interrupts. We have to clear the
2217 * irq_enabled flag first to ensure that interrupts
2218 * won't be re-enabled.
2219 */
2220 mdp->irq_enabled = false;
2221 synchronize_irq(ndev->irq);
2222 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002223 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002224
Ben Hutchings740c7f32015-01-27 00:49:32 +00002225 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002226
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002227 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002228 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002229 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002230
2231 /* Set new parameters */
2232 mdp->num_rx_ring = ring->rx_pending;
2233 mdp->num_tx_ring = ring->tx_pending;
2234
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002235 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002236 ret = sh_eth_ring_init(ndev);
2237 if (ret < 0) {
2238 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2239 __func__);
2240 return ret;
2241 }
2242 ret = sh_eth_dev_init(ndev, false);
2243 if (ret < 0) {
2244 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2245 __func__);
2246 return ret;
2247 }
2248
Ben Hutchings283e38d2015-01-22 12:44:08 +00002249 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002250 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2251 /* Setting the Rx mode will start the Rx process. */
2252 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002253 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002254 }
2255
2256 return 0;
2257}
2258
stephen hemminger9b07be42012-01-04 12:59:49 +00002259static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002260 .get_settings = sh_eth_get_settings,
2261 .set_settings = sh_eth_set_settings,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002262 .get_regs_len = sh_eth_get_regs_len,
2263 .get_regs = sh_eth_get_regs,
stephen hemminger9b07be42012-01-04 12:59:49 +00002264 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002265 .get_msglevel = sh_eth_get_msglevel,
2266 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002267 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002268 .get_strings = sh_eth_get_strings,
2269 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2270 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002271 .get_ringparam = sh_eth_get_ringparam,
2272 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002273};
2274
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002275/* network device open function */
2276static int sh_eth_open(struct net_device *ndev)
2277{
2278 int ret = 0;
2279 struct sh_eth_private *mdp = netdev_priv(ndev);
2280
Magnus Dammbcd51492009-10-09 00:20:04 +00002281 pm_runtime_get_sync(&mdp->pdev->dev);
2282
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002283 napi_enable(&mdp->napi);
2284
Joe Perchesa0607fd2009-11-18 23:29:17 -08002285 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002286 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002287 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002288 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002289 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002290 }
2291
2292 /* Descriptor set */
2293 ret = sh_eth_ring_init(ndev);
2294 if (ret)
2295 goto out_free_irq;
2296
2297 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002298 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002299 if (ret)
2300 goto out_free_irq;
2301
2302 /* PHY control start*/
2303 ret = sh_eth_phy_start(ndev);
2304 if (ret)
2305 goto out_free_irq;
2306
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002307 mdp->is_opened = 1;
2308
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002309 return ret;
2310
2311out_free_irq:
2312 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002313out_napi_off:
2314 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002315 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002316 return ret;
2317}
2318
2319/* Timeout function */
2320static void sh_eth_tx_timeout(struct net_device *ndev)
2321{
2322 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002323 struct sh_eth_rxdesc *rxdesc;
2324 int i;
2325
2326 netif_stop_queue(ndev);
2327
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002328 netif_err(mdp, timer, ndev,
2329 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002330 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002331
2332 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002333 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002334
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002335 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002336 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002337 rxdesc = &mdp->rx_ring[i];
2338 rxdesc->status = 0;
2339 rxdesc->addr = 0xBADF00D0;
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002340 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002341 mdp->rx_skbuff[i] = NULL;
2342 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002343 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002344 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002345 mdp->tx_skbuff[i] = NULL;
2346 }
2347
2348 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002349 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002350}
2351
2352/* Packet transmit function */
2353static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2354{
2355 struct sh_eth_private *mdp = netdev_priv(ndev);
2356 struct sh_eth_txdesc *txdesc;
2357 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002358 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002359
2360 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002361 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002362 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002363 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002364 netif_stop_queue(ndev);
2365 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002366 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002367 }
2368 }
2369 spin_unlock_irqrestore(&mdp->lock, flags);
2370
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002371 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002372 return NETDEV_TX_OK;
2373
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002374 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002375 mdp->tx_skbuff[entry] = skb;
2376 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002377 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002378 if (!mdp->cd->hw_swap)
2379 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2380 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002381 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2382 DMA_TO_DEVICE);
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002383 if (dma_mapping_error(&ndev->dev, txdesc->addr)) {
2384 kfree_skb(skb);
2385 return NETDEV_TX_OK;
2386 }
Ben Hutchingseebfb642015-01-22 12:40:13 +00002387 txdesc->buffer_length = skb->len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002388
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002389 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002390 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002391 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002392 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002393 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002394
2395 mdp->cur_tx++;
2396
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002397 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2398 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002399
Patrick McHardy6ed10652009-06-23 06:03:08 +00002400 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002401}
2402
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002403/* The statistics registers have write-clear behaviour, which means we
2404 * will lose any increment between the read and write. We mitigate
2405 * this by only clearing when we read a non-zero value, so we will
2406 * never falsely report a total of zero.
2407 */
2408static void
2409sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2410{
2411 u32 delta = sh_eth_read(ndev, reg);
2412
2413 if (delta) {
2414 *stat += delta;
2415 sh_eth_write(ndev, 0, reg);
2416 }
2417}
2418
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002419static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2420{
2421 struct sh_eth_private *mdp = netdev_priv(ndev);
2422
2423 if (sh_eth_is_rz_fast_ether(mdp))
2424 return &ndev->stats;
2425
2426 if (!mdp->is_opened)
2427 return &ndev->stats;
2428
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002429 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2430 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2431 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002432
2433 if (sh_eth_is_gether(mdp)) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002434 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2435 CERCR);
2436 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2437 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002438 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002439 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2440 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002441 }
2442
2443 return &ndev->stats;
2444}
2445
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002446/* device close function */
2447static int sh_eth_close(struct net_device *ndev)
2448{
2449 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002450
2451 netif_stop_queue(ndev);
2452
Ben Hutchings283e38d2015-01-22 12:44:08 +00002453 /* Serialise with the interrupt handler and NAPI, then disable
2454 * interrupts. We have to clear the irq_enabled flag first to
2455 * ensure that interrupts won't be re-enabled.
2456 */
2457 mdp->irq_enabled = false;
2458 synchronize_irq(ndev->irq);
2459 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002460 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002461
Ben Hutchings740c7f32015-01-27 00:49:32 +00002462 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002463
2464 /* PHY Disconnect */
2465 if (mdp->phydev) {
2466 phy_stop(mdp->phydev);
2467 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002468 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002469 }
2470
2471 free_irq(ndev->irq, ndev);
2472
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002473 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002474 sh_eth_ring_free(ndev);
2475
Magnus Dammbcd51492009-10-09 00:20:04 +00002476 pm_runtime_put_sync(&mdp->pdev->dev);
2477
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002478 mdp->is_opened = 0;
2479
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002480 return 0;
2481}
2482
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002483/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002484static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002485{
2486 struct sh_eth_private *mdp = netdev_priv(ndev);
2487 struct phy_device *phydev = mdp->phydev;
2488
2489 if (!netif_running(ndev))
2490 return -EINVAL;
2491
2492 if (!phydev)
2493 return -ENODEV;
2494
Richard Cochran28b04112010-07-17 08:48:55 +00002495 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002496}
2497
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002498/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2499static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2500 int entry)
2501{
2502 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2503}
2504
2505static u32 sh_eth_tsu_get_post_mask(int entry)
2506{
2507 return 0x0f << (28 - ((entry % 8) * 4));
2508}
2509
2510static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2511{
2512 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2513}
2514
2515static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2516 int entry)
2517{
2518 struct sh_eth_private *mdp = netdev_priv(ndev);
2519 u32 tmp;
2520 void *reg_offset;
2521
2522 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2523 tmp = ioread32(reg_offset);
2524 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2525}
2526
2527static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2528 int entry)
2529{
2530 struct sh_eth_private *mdp = netdev_priv(ndev);
2531 u32 post_mask, ref_mask, tmp;
2532 void *reg_offset;
2533
2534 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2535 post_mask = sh_eth_tsu_get_post_mask(entry);
2536 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2537
2538 tmp = ioread32(reg_offset);
2539 iowrite32(tmp & ~post_mask, reg_offset);
2540
2541 /* If other port enables, the function returns "true" */
2542 return tmp & ref_mask;
2543}
2544
2545static int sh_eth_tsu_busy(struct net_device *ndev)
2546{
2547 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2548 struct sh_eth_private *mdp = netdev_priv(ndev);
2549
2550 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2551 udelay(10);
2552 timeout--;
2553 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002554 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002555 return -ETIMEDOUT;
2556 }
2557 }
2558
2559 return 0;
2560}
2561
2562static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2563 const u8 *addr)
2564{
2565 u32 val;
2566
2567 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2568 iowrite32(val, reg);
2569 if (sh_eth_tsu_busy(ndev) < 0)
2570 return -EBUSY;
2571
2572 val = addr[4] << 8 | addr[5];
2573 iowrite32(val, reg + 4);
2574 if (sh_eth_tsu_busy(ndev) < 0)
2575 return -EBUSY;
2576
2577 return 0;
2578}
2579
2580static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2581{
2582 u32 val;
2583
2584 val = ioread32(reg);
2585 addr[0] = (val >> 24) & 0xff;
2586 addr[1] = (val >> 16) & 0xff;
2587 addr[2] = (val >> 8) & 0xff;
2588 addr[3] = val & 0xff;
2589 val = ioread32(reg + 4);
2590 addr[4] = (val >> 8) & 0xff;
2591 addr[5] = val & 0xff;
2592}
2593
2594
2595static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2596{
2597 struct sh_eth_private *mdp = netdev_priv(ndev);
2598 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2599 int i;
2600 u8 c_addr[ETH_ALEN];
2601
2602 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2603 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002604 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002605 return i;
2606 }
2607
2608 return -ENOENT;
2609}
2610
2611static int sh_eth_tsu_find_empty(struct net_device *ndev)
2612{
2613 u8 blank[ETH_ALEN];
2614 int entry;
2615
2616 memset(blank, 0, sizeof(blank));
2617 entry = sh_eth_tsu_find_entry(ndev, blank);
2618 return (entry < 0) ? -ENOMEM : entry;
2619}
2620
2621static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2622 int entry)
2623{
2624 struct sh_eth_private *mdp = netdev_priv(ndev);
2625 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2626 int ret;
2627 u8 blank[ETH_ALEN];
2628
2629 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2630 ~(1 << (31 - entry)), TSU_TEN);
2631
2632 memset(blank, 0, sizeof(blank));
2633 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2634 if (ret < 0)
2635 return ret;
2636 return 0;
2637}
2638
2639static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2640{
2641 struct sh_eth_private *mdp = netdev_priv(ndev);
2642 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2643 int i, ret;
2644
2645 if (!mdp->cd->tsu)
2646 return 0;
2647
2648 i = sh_eth_tsu_find_entry(ndev, addr);
2649 if (i < 0) {
2650 /* No entry found, create one */
2651 i = sh_eth_tsu_find_empty(ndev);
2652 if (i < 0)
2653 return -ENOMEM;
2654 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2655 if (ret < 0)
2656 return ret;
2657
2658 /* Enable the entry */
2659 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2660 (1 << (31 - i)), TSU_TEN);
2661 }
2662
2663 /* Entry found or created, enable POST */
2664 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2665
2666 return 0;
2667}
2668
2669static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2670{
2671 struct sh_eth_private *mdp = netdev_priv(ndev);
2672 int i, ret;
2673
2674 if (!mdp->cd->tsu)
2675 return 0;
2676
2677 i = sh_eth_tsu_find_entry(ndev, addr);
2678 if (i) {
2679 /* Entry found */
2680 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2681 goto done;
2682
2683 /* Disable the entry if both ports was disabled */
2684 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2685 if (ret < 0)
2686 return ret;
2687 }
2688done:
2689 return 0;
2690}
2691
2692static int sh_eth_tsu_purge_all(struct net_device *ndev)
2693{
2694 struct sh_eth_private *mdp = netdev_priv(ndev);
2695 int i, ret;
2696
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002697 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002698 return 0;
2699
2700 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2701 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2702 continue;
2703
2704 /* Disable the entry if both ports was disabled */
2705 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2706 if (ret < 0)
2707 return ret;
2708 }
2709
2710 return 0;
2711}
2712
2713static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2714{
2715 struct sh_eth_private *mdp = netdev_priv(ndev);
2716 u8 addr[ETH_ALEN];
2717 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2718 int i;
2719
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002720 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002721 return;
2722
2723 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2724 sh_eth_tsu_read_entry(reg_offset, addr);
2725 if (is_multicast_ether_addr(addr))
2726 sh_eth_tsu_del_entry(ndev, addr);
2727 }
2728}
2729
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002730/* Update promiscuous flag and multicast filter */
2731static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002732{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002733 struct sh_eth_private *mdp = netdev_priv(ndev);
2734 u32 ecmr_bits;
2735 int mcast_all = 0;
2736 unsigned long flags;
2737
2738 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002739 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002740 * Depending on ndev->flags, set PRM or clear MCT
2741 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002742 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2743 if (mdp->cd->tsu)
2744 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002745
2746 if (!(ndev->flags & IFF_MULTICAST)) {
2747 sh_eth_tsu_purge_mcast(ndev);
2748 mcast_all = 1;
2749 }
2750 if (ndev->flags & IFF_ALLMULTI) {
2751 sh_eth_tsu_purge_mcast(ndev);
2752 ecmr_bits &= ~ECMR_MCT;
2753 mcast_all = 1;
2754 }
2755
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002756 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002757 sh_eth_tsu_purge_all(ndev);
2758 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2759 } else if (mdp->cd->tsu) {
2760 struct netdev_hw_addr *ha;
2761 netdev_for_each_mc_addr(ha, ndev) {
2762 if (mcast_all && is_multicast_ether_addr(ha->addr))
2763 continue;
2764
2765 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2766 if (!mcast_all) {
2767 sh_eth_tsu_purge_mcast(ndev);
2768 ecmr_bits &= ~ECMR_MCT;
2769 mcast_all = 1;
2770 }
2771 }
2772 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002773 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002774
2775 /* update the ethernet mode */
2776 sh_eth_write(ndev, ecmr_bits, ECMR);
2777
2778 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002779}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002780
2781static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2782{
2783 if (!mdp->port)
2784 return TSU_VTAG0;
2785 else
2786 return TSU_VTAG1;
2787}
2788
Patrick McHardy80d5c362013-04-19 02:04:28 +00002789static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2790 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002791{
2792 struct sh_eth_private *mdp = netdev_priv(ndev);
2793 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2794
2795 if (unlikely(!mdp->cd->tsu))
2796 return -EPERM;
2797
2798 /* No filtering if vid = 0 */
2799 if (!vid)
2800 return 0;
2801
2802 mdp->vlan_num_ids++;
2803
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002804 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002805 * already enabled, the driver disables it and the filte
2806 */
2807 if (mdp->vlan_num_ids > 1) {
2808 /* disable VLAN filter */
2809 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2810 return 0;
2811 }
2812
2813 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2814 vtag_reg_index);
2815
2816 return 0;
2817}
2818
Patrick McHardy80d5c362013-04-19 02:04:28 +00002819static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2820 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002821{
2822 struct sh_eth_private *mdp = netdev_priv(ndev);
2823 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2824
2825 if (unlikely(!mdp->cd->tsu))
2826 return -EPERM;
2827
2828 /* No filtering if vid = 0 */
2829 if (!vid)
2830 return 0;
2831
2832 mdp->vlan_num_ids--;
2833 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2834
2835 return 0;
2836}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002837
2838/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002839static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002840{
Simon Hormandb893472014-01-17 09:22:28 +09002841 if (sh_eth_is_rz_fast_ether(mdp)) {
2842 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2843 return;
2844 }
2845
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002846 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2847 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2848 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2849 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2850 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2851 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2852 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2853 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2854 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2855 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002856 if (sh_eth_is_gether(mdp)) {
2857 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2858 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2859 } else {
2860 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2861 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2862 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002863 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2864 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2865 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2866 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2867 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2868 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2869 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002870}
2871
2872/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002873static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002874{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002875 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002876 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002877
2878 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002879 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002880
2881 return 0;
2882}
2883
2884/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002885static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002886 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002887{
2888 int ret, i;
2889 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002890 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002891 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002892
2893 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002894 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002895 if (!bitbang)
2896 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002897
2898 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002899 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002900 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002901 bitbang->mdi_msk = PIR_MDI;
2902 bitbang->mdo_msk = PIR_MDO;
2903 bitbang->mmd_msk = PIR_MMD;
2904 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002905 bitbang->ctrl.ops = &bb_ops;
2906
Stefan Weilc2e07b32010-08-03 19:44:52 +02002907 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002908 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002909 if (!mdp->mii_bus)
2910 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002911
2912 /* Hook up MII support for ethtool */
2913 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002914 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002915 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002916 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002917
2918 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002919 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2920 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002921 if (!mdp->mii_bus->irq) {
2922 ret = -ENOMEM;
2923 goto out_free_bus;
2924 }
2925
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002926 /* register MDIO bus */
2927 if (dev->of_node) {
2928 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002929 } else {
2930 for (i = 0; i < PHY_MAX_ADDR; i++)
2931 mdp->mii_bus->irq[i] = PHY_POLL;
2932 if (pd->phy_irq > 0)
2933 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2934
2935 ret = mdiobus_register(mdp->mii_bus);
2936 }
2937
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002938 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002939 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002940
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002941 return 0;
2942
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002943out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002944 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002945 return ret;
2946}
2947
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002948static const u16 *sh_eth_get_register_offset(int register_type)
2949{
2950 const u16 *reg_offset = NULL;
2951
2952 switch (register_type) {
2953 case SH_ETH_REG_GIGABIT:
2954 reg_offset = sh_eth_offset_gigabit;
2955 break;
Simon Hormandb893472014-01-17 09:22:28 +09002956 case SH_ETH_REG_FAST_RZ:
2957 reg_offset = sh_eth_offset_fast_rz;
2958 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002959 case SH_ETH_REG_FAST_RCAR:
2960 reg_offset = sh_eth_offset_fast_rcar;
2961 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002962 case SH_ETH_REG_FAST_SH4:
2963 reg_offset = sh_eth_offset_fast_sh4;
2964 break;
2965 case SH_ETH_REG_FAST_SH3_SH2:
2966 reg_offset = sh_eth_offset_fast_sh3_sh2;
2967 break;
2968 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002969 break;
2970 }
2971
2972 return reg_offset;
2973}
2974
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002975static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002976 .ndo_open = sh_eth_open,
2977 .ndo_stop = sh_eth_close,
2978 .ndo_start_xmit = sh_eth_start_xmit,
2979 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002980 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002981 .ndo_tx_timeout = sh_eth_tx_timeout,
2982 .ndo_do_ioctl = sh_eth_do_ioctl,
2983 .ndo_validate_addr = eth_validate_addr,
2984 .ndo_set_mac_address = eth_mac_addr,
2985 .ndo_change_mtu = eth_change_mtu,
2986};
2987
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002988static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2989 .ndo_open = sh_eth_open,
2990 .ndo_stop = sh_eth_close,
2991 .ndo_start_xmit = sh_eth_start_xmit,
2992 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002993 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002994 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2995 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2996 .ndo_tx_timeout = sh_eth_tx_timeout,
2997 .ndo_do_ioctl = sh_eth_do_ioctl,
2998 .ndo_validate_addr = eth_validate_addr,
2999 .ndo_set_mac_address = eth_mac_addr,
3000 .ndo_change_mtu = eth_change_mtu,
3001};
3002
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003003#ifdef CONFIG_OF
3004static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3005{
3006 struct device_node *np = dev->of_node;
3007 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003008 const char *mac_addr;
3009
3010 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3011 if (!pdata)
3012 return NULL;
3013
3014 pdata->phy_interface = of_get_phy_mode(np);
3015
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003016 mac_addr = of_get_mac_address(np);
3017 if (mac_addr)
3018 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
3019
3020 pdata->no_ether_link =
3021 of_property_read_bool(np, "renesas,no-ether-link");
3022 pdata->ether_link_active_low =
3023 of_property_read_bool(np, "renesas,ether-link-active-low");
3024
3025 return pdata;
3026}
3027
3028static const struct of_device_id sh_eth_match_table[] = {
3029 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
3030 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
3031 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
3032 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
3033 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003034 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003035 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003036 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
3037 { }
3038};
3039MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3040#else
3041static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3042{
3043 return NULL;
3044}
3045#endif
3046
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003047static int sh_eth_drv_probe(struct platform_device *pdev)
3048{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07003049 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003050 struct resource *res;
3051 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00003052 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09003053 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003054 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003055
3056 /* get base addr */
3057 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003058
3059 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003060 if (!ndev)
3061 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003062
Ben Dooksb5893a02014-03-21 12:09:14 +01003063 pm_runtime_enable(&pdev->dev);
3064 pm_runtime_get_sync(&pdev->dev);
3065
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003066 devno = pdev->id;
3067 if (devno < 0)
3068 devno = 0;
3069
3070 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02003071 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003072 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003073 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003074 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003075
3076 SET_NETDEV_DEV(ndev, &pdev->dev);
3077
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003078 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003079 mdp->num_tx_ring = TX_RING_SIZE;
3080 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003081 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3082 if (IS_ERR(mdp->addr)) {
3083 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003084 goto out_release;
3085 }
3086
Varka Bhadramc9608042014-10-24 07:42:09 +05303087 ndev->base_addr = res->start;
3088
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003089 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003090 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003091
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003092 if (pdev->dev.of_node)
3093 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003094 if (!pd) {
3095 dev_err(&pdev->dev, "no platform data\n");
3096 ret = -EINVAL;
3097 goto out_release;
3098 }
3099
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003100 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003101 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003102 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04003103 /* EDMAC endian */
3104 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003105 mdp->no_ether_link = pd->no_ether_link;
3106 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003107
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003108 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003109 if (id) {
3110 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
3111 } else {
3112 const struct of_device_id *match;
3113
3114 match = of_match_device(of_match_ptr(sh_eth_match_table),
3115 &pdev->dev);
3116 mdp->cd = (struct sh_eth_cpu_data *)match->data;
3117 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003118 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003119 if (!mdp->reg_offset) {
3120 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3121 mdp->cd->register_type);
3122 ret = -EINVAL;
3123 goto out_release;
3124 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003125 sh_eth_set_default_cpu_data(mdp->cd);
3126
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003127 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003128 if (mdp->cd->tsu)
3129 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3130 else
3131 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003132 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003133 ndev->watchdog_timeo = TX_TIMEOUT;
3134
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003135 /* debug message level */
3136 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003137
3138 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003139 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003140 if (!is_valid_ether_addr(ndev->dev_addr)) {
3141 dev_warn(&pdev->dev,
3142 "no valid MAC address supplied, using a random one.\n");
3143 eth_hw_addr_random(ndev);
3144 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003145
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003146 /* ioremap the TSU registers */
3147 if (mdp->cd->tsu) {
3148 struct resource *rtsu;
3149 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003150 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3151 if (IS_ERR(mdp->tsu_addr)) {
3152 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003153 goto out_release;
3154 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00003155 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00003156 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003157 }
3158
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00003159 /* initialize first or needed device */
3160 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003161 if (mdp->cd->chip_reset)
3162 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003163
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003164 if (mdp->cd->tsu) {
3165 /* TSU init (Init only)*/
3166 sh_eth_tsu_init(mdp);
3167 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003168 }
3169
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003170 if (mdp->cd->rmiimode)
3171 sh_eth_write(ndev, 0x1, RMIIMODE);
3172
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003173 /* MDIO bus init */
3174 ret = sh_mdio_init(mdp, pd);
3175 if (ret) {
3176 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3177 goto out_release;
3178 }
3179
Sergei Shtylyov37191092013-06-19 23:30:23 +04003180 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3181
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003182 /* network device register */
3183 ret = register_netdev(ndev);
3184 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003185 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003186
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003187 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003188 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3189 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003190
Ben Dooksb5893a02014-03-21 12:09:14 +01003191 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003192 platform_set_drvdata(pdev, ndev);
3193
3194 return ret;
3195
Sergei Shtylyov37191092013-06-19 23:30:23 +04003196out_napi_del:
3197 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003198 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003199
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003200out_release:
3201 /* net_dev free */
3202 if (ndev)
3203 free_netdev(ndev);
3204
Ben Dooksb5893a02014-03-21 12:09:14 +01003205 pm_runtime_put(&pdev->dev);
3206 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003207 return ret;
3208}
3209
3210static int sh_eth_drv_remove(struct platform_device *pdev)
3211{
3212 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003213 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003214
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003215 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003216 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003217 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003218 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003219 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003220
3221 return 0;
3222}
3223
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003224#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003225#ifdef CONFIG_PM_SLEEP
3226static int sh_eth_suspend(struct device *dev)
3227{
3228 struct net_device *ndev = dev_get_drvdata(dev);
3229 int ret = 0;
3230
3231 if (netif_running(ndev)) {
3232 netif_device_detach(ndev);
3233 ret = sh_eth_close(ndev);
3234 }
3235
3236 return ret;
3237}
3238
3239static int sh_eth_resume(struct device *dev)
3240{
3241 struct net_device *ndev = dev_get_drvdata(dev);
3242 int ret = 0;
3243
3244 if (netif_running(ndev)) {
3245 ret = sh_eth_open(ndev);
3246 if (ret < 0)
3247 return ret;
3248 netif_device_attach(ndev);
3249 }
3250
3251 return ret;
3252}
3253#endif
3254
Magnus Dammbcd51492009-10-09 00:20:04 +00003255static int sh_eth_runtime_nop(struct device *dev)
3256{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003257 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003258 * and ->runtime_resume(). Simply returns success.
3259 *
3260 * This driver re-initializes all registers after
3261 * pm_runtime_get_sync() anyway so there is no need
3262 * to save and restore registers here.
3263 */
3264 return 0;
3265}
3266
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003267static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003268 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003269 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003270};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003271#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3272#else
3273#define SH_ETH_PM_OPS NULL
3274#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003275
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003276static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003277 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003278 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003279 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003280 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003281 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3282 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003283 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00003284 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00003285 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003286 { }
3287};
3288MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3289
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003290static struct platform_driver sh_eth_driver = {
3291 .probe = sh_eth_drv_probe,
3292 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003293 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003294 .driver = {
3295 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003296 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003297 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003298 },
3299};
3300
Axel Lindb62f682011-11-27 16:44:17 +00003301module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003302
3303MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3304MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3305MODULE_LICENSE("GPL v2");