blob: 1f8240aec08641ee4d21e8cbe7e8967a10d972b1 [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.
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03006 * Copyright (C) 2013-2016 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00007 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070025#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070026#include <linux/dma-mapping.h>
27#include <linux/etherdevice.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/mdio-bitbang.h>
31#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070036#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000039#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000041#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000042#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000043#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000044#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000045#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070046
47#include "sh_eth.h"
48
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000049#define SH_ETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_LINK | \
51 NETIF_MSG_TIMER | \
52 NETIF_MSG_RX_ERR| \
53 NETIF_MSG_TX_ERR)
54
Sergei Shtylyov2274d372015-12-13 01:44:50 +030055#define SH_ETH_OFFSET_INVALID ((u16)~0)
56
Ben Hutchings33657112015-02-26 20:34:14 +000057#define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
59
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000060static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +000061 SH_ETH_OFFSET_DEFAULTS,
62
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000063 [EDSR] = 0x0000,
64 [EDMR] = 0x0400,
65 [EDTRR] = 0x0408,
66 [EDRRR] = 0x0410,
67 [EESR] = 0x0428,
68 [EESIPR] = 0x0430,
69 [TDLAR] = 0x0010,
70 [TDFAR] = 0x0014,
71 [TDFXR] = 0x0018,
72 [TDFFR] = 0x001c,
73 [RDLAR] = 0x0030,
74 [RDFAR] = 0x0034,
75 [RDFXR] = 0x0038,
76 [RDFFR] = 0x003c,
77 [TRSCER] = 0x0438,
78 [RMFCR] = 0x0440,
79 [TFTR] = 0x0448,
80 [FDR] = 0x0450,
81 [RMCR] = 0x0458,
82 [RPADIR] = 0x0460,
83 [FCFTR] = 0x0468,
84 [CSMR] = 0x04E4,
85
86 [ECMR] = 0x0500,
87 [ECSR] = 0x0510,
88 [ECSIPR] = 0x0518,
89 [PIR] = 0x0520,
90 [PSR] = 0x0528,
91 [PIPR] = 0x052c,
92 [RFLR] = 0x0508,
93 [APR] = 0x0554,
94 [MPR] = 0x0558,
95 [PFTCR] = 0x055c,
96 [PFRCR] = 0x0560,
97 [TPAUSER] = 0x0564,
98 [GECMR] = 0x05b0,
99 [BCULR] = 0x05b4,
100 [MAHR] = 0x05c0,
101 [MALR] = 0x05c8,
102 [TROCR] = 0x0700,
103 [CDCR] = 0x0708,
104 [LCCR] = 0x0710,
105 [CEFCR] = 0x0740,
106 [FRECR] = 0x0748,
107 [TSFRCR] = 0x0750,
108 [TLFRCR] = 0x0758,
109 [RFCR] = 0x0760,
110 [CERCR] = 0x0768,
111 [CEECR] = 0x0770,
112 [MAFCR] = 0x0778,
113 [RMII_MII] = 0x0790,
114
115 [ARSTR] = 0x0000,
116 [TSU_CTRST] = 0x0004,
117 [TSU_FWEN0] = 0x0010,
118 [TSU_FWEN1] = 0x0014,
119 [TSU_FCM] = 0x0018,
120 [TSU_BSYSL0] = 0x0020,
121 [TSU_BSYSL1] = 0x0024,
122 [TSU_PRISL0] = 0x0028,
123 [TSU_PRISL1] = 0x002c,
124 [TSU_FWSL0] = 0x0030,
125 [TSU_FWSL1] = 0x0034,
126 [TSU_FWSLC] = 0x0038,
127 [TSU_QTAG0] = 0x0040,
128 [TSU_QTAG1] = 0x0044,
129 [TSU_FWSR] = 0x0050,
130 [TSU_FWINMK] = 0x0054,
131 [TSU_ADQT0] = 0x0048,
132 [TSU_ADQT1] = 0x004c,
133 [TSU_VTAG0] = 0x0058,
134 [TSU_VTAG1] = 0x005c,
135 [TSU_ADSBSY] = 0x0060,
136 [TSU_TEN] = 0x0064,
137 [TSU_POST1] = 0x0070,
138 [TSU_POST2] = 0x0074,
139 [TSU_POST3] = 0x0078,
140 [TSU_POST4] = 0x007c,
141 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000142
143 [TXNLCR0] = 0x0080,
144 [TXALCR0] = 0x0084,
145 [RXNLCR0] = 0x0088,
146 [RXALCR0] = 0x008c,
147 [FWNLCR0] = 0x0090,
148 [FWALCR0] = 0x0094,
149 [TXNLCR1] = 0x00a0,
150 [TXALCR1] = 0x00a0,
151 [RXNLCR1] = 0x00a8,
152 [RXALCR1] = 0x00ac,
153 [FWNLCR1] = 0x00b0,
154 [FWALCR1] = 0x00b4,
155};
156
Simon Hormandb893472014-01-17 09:22:28 +0900157static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000158 SH_ETH_OFFSET_DEFAULTS,
159
Simon Hormandb893472014-01-17 09:22:28 +0900160 [EDSR] = 0x0000,
161 [EDMR] = 0x0400,
162 [EDTRR] = 0x0408,
163 [EDRRR] = 0x0410,
164 [EESR] = 0x0428,
165 [EESIPR] = 0x0430,
166 [TDLAR] = 0x0010,
167 [TDFAR] = 0x0014,
168 [TDFXR] = 0x0018,
169 [TDFFR] = 0x001c,
170 [RDLAR] = 0x0030,
171 [RDFAR] = 0x0034,
172 [RDFXR] = 0x0038,
173 [RDFFR] = 0x003c,
174 [TRSCER] = 0x0438,
175 [RMFCR] = 0x0440,
176 [TFTR] = 0x0448,
177 [FDR] = 0x0450,
178 [RMCR] = 0x0458,
179 [RPADIR] = 0x0460,
180 [FCFTR] = 0x0468,
181 [CSMR] = 0x04E4,
182
183 [ECMR] = 0x0500,
184 [RFLR] = 0x0508,
185 [ECSR] = 0x0510,
186 [ECSIPR] = 0x0518,
187 [PIR] = 0x0520,
188 [APR] = 0x0554,
189 [MPR] = 0x0558,
190 [PFTCR] = 0x055c,
191 [PFRCR] = 0x0560,
192 [TPAUSER] = 0x0564,
193 [MAHR] = 0x05c0,
194 [MALR] = 0x05c8,
195 [CEFCR] = 0x0740,
196 [FRECR] = 0x0748,
197 [TSFRCR] = 0x0750,
198 [TLFRCR] = 0x0758,
199 [RFCR] = 0x0760,
200 [MAFCR] = 0x0778,
201
202 [ARSTR] = 0x0000,
203 [TSU_CTRST] = 0x0004,
204 [TSU_VTAG0] = 0x0058,
205 [TSU_ADSBSY] = 0x0060,
206 [TSU_TEN] = 0x0064,
207 [TSU_ADRH0] = 0x0100,
Simon Hormandb893472014-01-17 09:22:28 +0900208
209 [TXNLCR0] = 0x0080,
210 [TXALCR0] = 0x0084,
211 [RXNLCR0] = 0x0088,
212 [RXALCR0] = 0x008C,
213};
214
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000215static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000216 SH_ETH_OFFSET_DEFAULTS,
217
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000218 [ECMR] = 0x0300,
219 [RFLR] = 0x0308,
220 [ECSR] = 0x0310,
221 [ECSIPR] = 0x0318,
222 [PIR] = 0x0320,
223 [PSR] = 0x0328,
224 [RDMLR] = 0x0340,
225 [IPGR] = 0x0350,
226 [APR] = 0x0354,
227 [MPR] = 0x0358,
228 [RFCF] = 0x0360,
229 [TPAUSER] = 0x0364,
230 [TPAUSECR] = 0x0368,
231 [MAHR] = 0x03c0,
232 [MALR] = 0x03c8,
233 [TROCR] = 0x03d0,
234 [CDCR] = 0x03d4,
235 [LCCR] = 0x03d8,
236 [CNDCR] = 0x03dc,
237 [CEFCR] = 0x03e4,
238 [FRECR] = 0x03e8,
239 [TSFRCR] = 0x03ec,
240 [TLFRCR] = 0x03f0,
241 [RFCR] = 0x03f4,
242 [MAFCR] = 0x03f8,
243
244 [EDMR] = 0x0200,
245 [EDTRR] = 0x0208,
246 [EDRRR] = 0x0210,
247 [TDLAR] = 0x0218,
248 [RDLAR] = 0x0220,
249 [EESR] = 0x0228,
250 [EESIPR] = 0x0230,
251 [TRSCER] = 0x0238,
252 [RMFCR] = 0x0240,
253 [TFTR] = 0x0248,
254 [FDR] = 0x0250,
255 [RMCR] = 0x0258,
256 [TFUCR] = 0x0264,
257 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900258 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000259 [FCFTR] = 0x0270,
260 [TRIMD] = 0x027c,
261};
262
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000263static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000264 SH_ETH_OFFSET_DEFAULTS,
265
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000266 [ECMR] = 0x0100,
267 [RFLR] = 0x0108,
268 [ECSR] = 0x0110,
269 [ECSIPR] = 0x0118,
270 [PIR] = 0x0120,
271 [PSR] = 0x0128,
272 [RDMLR] = 0x0140,
273 [IPGR] = 0x0150,
274 [APR] = 0x0154,
275 [MPR] = 0x0158,
276 [TPAUSER] = 0x0164,
277 [RFCF] = 0x0160,
278 [TPAUSECR] = 0x0168,
279 [BCFRR] = 0x016c,
280 [MAHR] = 0x01c0,
281 [MALR] = 0x01c8,
282 [TROCR] = 0x01d0,
283 [CDCR] = 0x01d4,
284 [LCCR] = 0x01d8,
285 [CNDCR] = 0x01dc,
286 [CEFCR] = 0x01e4,
287 [FRECR] = 0x01e8,
288 [TSFRCR] = 0x01ec,
289 [TLFRCR] = 0x01f0,
290 [RFCR] = 0x01f4,
291 [MAFCR] = 0x01f8,
292 [RTRATE] = 0x01fc,
293
294 [EDMR] = 0x0000,
295 [EDTRR] = 0x0008,
296 [EDRRR] = 0x0010,
297 [TDLAR] = 0x0018,
298 [RDLAR] = 0x0020,
299 [EESR] = 0x0028,
300 [EESIPR] = 0x0030,
301 [TRSCER] = 0x0038,
302 [RMFCR] = 0x0040,
303 [TFTR] = 0x0048,
304 [FDR] = 0x0050,
305 [RMCR] = 0x0058,
306 [TFUCR] = 0x0064,
307 [RFOCR] = 0x0068,
308 [FCFTR] = 0x0070,
309 [RPADIR] = 0x0078,
310 [TRIMD] = 0x007c,
311 [RBWAR] = 0x00c8,
312 [RDFAR] = 0x00cc,
313 [TBRAR] = 0x00d4,
314 [TDFAR] = 0x00d8,
315};
316
317static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000318 SH_ETH_OFFSET_DEFAULTS,
319
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400320 [EDMR] = 0x0000,
321 [EDTRR] = 0x0004,
322 [EDRRR] = 0x0008,
323 [TDLAR] = 0x000c,
324 [RDLAR] = 0x0010,
325 [EESR] = 0x0014,
326 [EESIPR] = 0x0018,
327 [TRSCER] = 0x001c,
328 [RMFCR] = 0x0020,
329 [TFTR] = 0x0024,
330 [FDR] = 0x0028,
331 [RMCR] = 0x002c,
332 [EDOCR] = 0x0030,
333 [FCFTR] = 0x0034,
334 [RPADIR] = 0x0038,
335 [TRIMD] = 0x003c,
336 [RBWAR] = 0x0040,
337 [RDFAR] = 0x0044,
338 [TBRAR] = 0x004c,
339 [TDFAR] = 0x0050,
340
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000341 [ECMR] = 0x0160,
342 [ECSR] = 0x0164,
343 [ECSIPR] = 0x0168,
344 [PIR] = 0x016c,
345 [MAHR] = 0x0170,
346 [MALR] = 0x0174,
347 [RFLR] = 0x0178,
348 [PSR] = 0x017c,
349 [TROCR] = 0x0180,
350 [CDCR] = 0x0184,
351 [LCCR] = 0x0188,
352 [CNDCR] = 0x018c,
353 [CEFCR] = 0x0194,
354 [FRECR] = 0x0198,
355 [TSFRCR] = 0x019c,
356 [TLFRCR] = 0x01a0,
357 [RFCR] = 0x01a4,
358 [MAFCR] = 0x01a8,
359 [IPGR] = 0x01b4,
360 [APR] = 0x01b8,
361 [MPR] = 0x01bc,
362 [TPAUSER] = 0x01c4,
363 [BCFR] = 0x01cc,
364
365 [ARSTR] = 0x0000,
366 [TSU_CTRST] = 0x0004,
367 [TSU_FWEN0] = 0x0010,
368 [TSU_FWEN1] = 0x0014,
369 [TSU_FCM] = 0x0018,
370 [TSU_BSYSL0] = 0x0020,
371 [TSU_BSYSL1] = 0x0024,
372 [TSU_PRISL0] = 0x0028,
373 [TSU_PRISL1] = 0x002c,
374 [TSU_FWSL0] = 0x0030,
375 [TSU_FWSL1] = 0x0034,
376 [TSU_FWSLC] = 0x0038,
377 [TSU_QTAGM0] = 0x0040,
378 [TSU_QTAGM1] = 0x0044,
379 [TSU_ADQT0] = 0x0048,
380 [TSU_ADQT1] = 0x004c,
381 [TSU_FWSR] = 0x0050,
382 [TSU_FWINMK] = 0x0054,
383 [TSU_ADSBSY] = 0x0060,
384 [TSU_TEN] = 0x0064,
385 [TSU_POST1] = 0x0070,
386 [TSU_POST2] = 0x0074,
387 [TSU_POST3] = 0x0078,
388 [TSU_POST4] = 0x007c,
389
390 [TXNLCR0] = 0x0080,
391 [TXALCR0] = 0x0084,
392 [RXNLCR0] = 0x0088,
393 [RXALCR0] = 0x008c,
394 [FWNLCR0] = 0x0090,
395 [FWALCR0] = 0x0094,
396 [TXNLCR1] = 0x00a0,
397 [TXALCR1] = 0x00a0,
398 [RXNLCR1] = 0x00a8,
399 [RXALCR1] = 0x00ac,
400 [FWNLCR1] = 0x00b0,
401 [FWALCR1] = 0x00b4,
402
403 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000404};
405
Ben Hutchings740c7f32015-01-27 00:49:32 +0000406static void sh_eth_rcv_snd_disable(struct net_device *ndev);
407static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
408
Sergei Shtylyov2274d372015-12-13 01:44:50 +0300409static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
410{
411 struct sh_eth_private *mdp = netdev_priv(ndev);
412 u16 offset = mdp->reg_offset[enum_index];
413
414 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
415 return;
416
417 iowrite32(data, mdp->addr + offset);
418}
419
420static u32 sh_eth_read(struct net_device *ndev, int enum_index)
421{
422 struct sh_eth_private *mdp = netdev_priv(ndev);
423 u16 offset = mdp->reg_offset[enum_index];
424
425 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
426 return ~0U;
427
428 return ioread32(mdp->addr + offset);
429}
430
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300431static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
432 u32 set)
433{
434 sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
435 enum_index);
436}
437
Simon Horman504c8ca2014-01-17 09:22:27 +0900438static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000439{
Simon Horman504c8ca2014-01-17 09:22:27 +0900440 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000441}
442
Simon Hormandb893472014-01-17 09:22:28 +0900443static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
444{
445 return mdp->reg_offset == sh_eth_offset_fast_rz;
446}
447
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400448static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000449{
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000450 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +0300451 u32 value;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000452
453 switch (mdp->phy_interface) {
454 case PHY_INTERFACE_MODE_GMII:
455 value = 0x2;
456 break;
457 case PHY_INTERFACE_MODE_MII:
458 value = 0x1;
459 break;
460 case PHY_INTERFACE_MODE_RMII:
461 value = 0x0;
462 break;
463 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300464 netdev_warn(ndev,
465 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000466 value = 0x1;
467 break;
468 }
469
470 sh_eth_write(ndev, value, RMII_MII);
471}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000472
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400473static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000474{
475 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000476
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300477 sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000478}
479
Geert Uytterhoeven99f84be2015-11-24 15:40:57 +0100480static void sh_eth_chip_reset(struct net_device *ndev)
481{
482 struct sh_eth_private *mdp = netdev_priv(ndev);
483
484 /* reset device */
Sergei Shtylyovec65cfc2016-04-24 23:46:15 +0300485 sh_eth_tsu_write(mdp, ARSTR_ARST, ARSTR);
Geert Uytterhoeven99f84be2015-11-24 15:40:57 +0100486 mdelay(1);
487}
488
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100489static void sh_eth_set_rate_gether(struct net_device *ndev)
490{
491 struct sh_eth_private *mdp = netdev_priv(ndev);
492
493 switch (mdp->speed) {
494 case 10: /* 10BASE */
495 sh_eth_write(ndev, GECMR_10, GECMR);
496 break;
497 case 100:/* 100BASE */
498 sh_eth_write(ndev, GECMR_100, GECMR);
499 break;
500 case 1000: /* 1000BASE */
501 sh_eth_write(ndev, GECMR_1000, GECMR);
502 break;
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100503 }
504}
505
Geert Uytterhoeven99f84be2015-11-24 15:40:57 +0100506#ifdef CONFIG_OF
507/* R7S72100 */
508static struct sh_eth_cpu_data r7s72100_data = {
509 .chip_reset = sh_eth_chip_reset,
510 .set_duplex = sh_eth_set_duplex,
511
512 .register_type = SH_ETH_REG_FAST_RZ,
513
514 .ecsr_value = ECSR_ICD,
515 .ecsipr_value = ECSIPR_ICDIP,
516 .eesipr_value = 0xff7f009f,
517
518 .tx_check = EESR_TC1 | EESR_FTC,
519 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
520 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
521 EESR_TDE | EESR_ECI,
522 .fdr_value = 0x0000070f,
523
524 .no_psr = 1,
525 .apr = 1,
526 .mpr = 1,
527 .tpauser = 1,
528 .hw_swap = 1,
529 .rpadir = 1,
530 .rpadir_value = 2 << 16,
531 .no_trimd = 1,
532 .no_ade = 1,
533 .hw_crc = 1,
534 .tsu = 1,
535 .shift_rd0 = 1,
536};
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100537
538static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
539{
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700540 sh_eth_chip_reset(ndev);
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100541
542 sh_eth_select_mii(ndev);
543}
544
545/* R8A7740 */
546static struct sh_eth_cpu_data r8a7740_data = {
547 .chip_reset = sh_eth_chip_reset_r8a7740,
548 .set_duplex = sh_eth_set_duplex,
549 .set_rate = sh_eth_set_rate_gether,
550
551 .register_type = SH_ETH_REG_GIGABIT,
552
553 .ecsr_value = ECSR_ICD | ECSR_MPD,
554 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
555 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
556
557 .tx_check = EESR_TC1 | EESR_FTC,
558 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
559 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
560 EESR_TDE | EESR_ECI,
561 .fdr_value = 0x0000070f,
562
563 .apr = 1,
564 .mpr = 1,
565 .tpauser = 1,
566 .bculr = 1,
567 .hw_swap = 1,
568 .rpadir = 1,
569 .rpadir_value = 2 << 16,
570 .no_trimd = 1,
571 .no_ade = 1,
572 .tsu = 1,
573 .select_mii = 1,
574 .shift_rd0 = 1,
575};
Geert Uytterhoeven99f84be2015-11-24 15:40:57 +0100576
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000577/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000578static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000579{
580 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000581
582 switch (mdp->speed) {
583 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300584 sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000585 break;
586 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300587 sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000588 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000589 }
590}
591
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000592/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000593static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000594 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000595 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000596
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400597 .register_type = SH_ETH_REG_FAST_RCAR,
598
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000599 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
600 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
601 .eesipr_value = 0x01ff009f,
602
603 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400604 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
605 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
606 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900607 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000608
609 .apr = 1,
610 .mpr = 1,
611 .tpauser = 1,
612 .hw_swap = 1,
613};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000614
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300615/* R8A7790/1 */
616static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900617 .set_duplex = sh_eth_set_duplex,
618 .set_rate = sh_eth_set_rate_r8a777x,
619
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400620 .register_type = SH_ETH_REG_FAST_RCAR,
621
Simon Hormane18dbf72013-07-23 10:18:05 +0900622 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
623 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
624 .eesipr_value = 0x01ff009f,
625
626 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900627 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
628 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
629 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900630 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900631
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100632 .trscer_err_mask = DESC_I_RINT8,
633
Simon Hormane18dbf72013-07-23 10:18:05 +0900634 .apr = 1,
635 .mpr = 1,
636 .tpauser = 1,
637 .hw_swap = 1,
638 .rmiimode = 1,
639};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100640#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900641
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000642static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000643{
644 struct sh_eth_private *mdp = netdev_priv(ndev);
645
646 switch (mdp->speed) {
647 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300648 sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000649 break;
650 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300651 sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000652 break;
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000653 }
654}
655
656/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000657static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000658 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000659 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000660
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400661 .register_type = SH_ETH_REG_FAST_SH4,
662
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000663 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
664 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400665 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000666
667 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400668 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
669 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
670 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000671
672 .apr = 1,
673 .mpr = 1,
674 .tpauser = 1,
675 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800676 .rpadir = 1,
677 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000678};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000679
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000680static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000681{
682 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000683
684 switch (mdp->speed) {
685 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000686 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000687 break;
688 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000689 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000690 break;
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000691 }
692}
693
694/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000695static struct sh_eth_cpu_data sh7757_data = {
696 .set_duplex = sh_eth_set_duplex,
697 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000698
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400699 .register_type = SH_ETH_REG_FAST_SH4,
700
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000701 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000702
703 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400704 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
705 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
706 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000707
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000708 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000709 .apr = 1,
710 .mpr = 1,
711 .tpauser = 1,
712 .hw_swap = 1,
713 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000714 .rpadir = 1,
715 .rpadir_value = 2 << 16,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000716 .rtrate = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000717};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000718
David S. Millere403d292013-06-07 23:40:41 -0700719#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000720#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
721#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
722static void sh_eth_chip_reset_giga(struct net_device *ndev)
723{
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100724 u32 mahr[2], malr[2];
Sergei Shtylyov79270922016-05-08 00:08:05 +0300725 int i;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000726
727 /* save MAHR and MALR */
728 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000729 malr[i] = ioread32((void *)GIGA_MALR(i));
730 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000731 }
732
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700733 sh_eth_chip_reset(ndev);
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000734
735 /* restore MAHR and MALR */
736 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000737 iowrite32(malr[i], (void *)GIGA_MALR(i));
738 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000739 }
740}
741
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000742static void sh_eth_set_rate_giga(struct net_device *ndev)
743{
744 struct sh_eth_private *mdp = netdev_priv(ndev);
745
746 switch (mdp->speed) {
747 case 10: /* 10BASE */
748 sh_eth_write(ndev, 0x00000000, GECMR);
749 break;
750 case 100:/* 100BASE */
751 sh_eth_write(ndev, 0x00000010, GECMR);
752 break;
753 case 1000: /* 1000BASE */
754 sh_eth_write(ndev, 0x00000020, GECMR);
755 break;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000756 }
757}
758
759/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000760static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000761 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000762 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000763 .set_rate = sh_eth_set_rate_giga,
764
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400765 .register_type = SH_ETH_REG_GIGABIT,
766
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000767 .ecsr_value = ECSR_ICD | ECSR_MPD,
768 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
769 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
770
771 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400772 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
773 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
774 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000775 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000776
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000777 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000778 .apr = 1,
779 .mpr = 1,
780 .tpauser = 1,
781 .bculr = 1,
782 .hw_swap = 1,
783 .rpadir = 1,
784 .rpadir_value = 2 << 16,
785 .no_trimd = 1,
786 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000787 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000788};
789
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000790/* SH7734 */
791static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000792 .chip_reset = sh_eth_chip_reset,
793 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000794 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000795
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400796 .register_type = SH_ETH_REG_GIGABIT,
797
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000798 .ecsr_value = ECSR_ICD | ECSR_MPD,
799 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
800 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
801
802 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400803 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
804 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
805 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000806
807 .apr = 1,
808 .mpr = 1,
809 .tpauser = 1,
810 .bculr = 1,
811 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000812 .no_trimd = 1,
813 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000814 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000815 .hw_crc = 1,
816 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000817};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000818
819/* SH7763 */
820static struct sh_eth_cpu_data sh7763_data = {
821 .chip_reset = sh_eth_chip_reset,
822 .set_duplex = sh_eth_set_duplex,
823 .set_rate = sh_eth_set_rate_gether,
824
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400825 .register_type = SH_ETH_REG_GIGABIT,
826
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000827 .ecsr_value = ECSR_ICD | ECSR_MPD,
828 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
829 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
830
831 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300832 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
833 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000834 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000835
836 .apr = 1,
837 .mpr = 1,
838 .tpauser = 1,
839 .bculr = 1,
840 .hw_swap = 1,
841 .no_trimd = 1,
842 .no_ade = 1,
843 .tsu = 1,
844 .irq_flags = IRQF_SHARED,
845};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000846
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000847static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400848 .register_type = SH_ETH_REG_FAST_SH3_SH2,
849
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000850 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
851
852 .apr = 1,
853 .mpr = 1,
854 .tpauser = 1,
855 .hw_swap = 1,
856};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000857
858static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400859 .register_type = SH_ETH_REG_FAST_SH3_SH2,
860
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000861 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000862 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000863};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000864
865static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
866{
867 if (!cd->ecsr_value)
868 cd->ecsr_value = DEFAULT_ECSR_INIT;
869
870 if (!cd->ecsipr_value)
871 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
872
873 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300874 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000875 DEFAULT_FIFO_F_D_RFD;
876
877 if (!cd->fdr_value)
878 cd->fdr_value = DEFAULT_FDR_INIT;
879
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000880 if (!cd->tx_check)
881 cd->tx_check = DEFAULT_TX_CHECK;
882
883 if (!cd->eesr_err_check)
884 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900885
886 if (!cd->trscer_err_mask)
887 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000888}
889
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000890static int sh_eth_check_reset(struct net_device *ndev)
891{
892 int ret = 0;
893 int cnt = 100;
894
895 while (cnt > 0) {
Sergei Shtylyov97717ed2016-04-24 23:45:23 +0300896 if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000897 break;
898 mdelay(1);
899 cnt--;
900 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400901 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300902 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000903 ret = -ETIMEDOUT;
904 }
905 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000906}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000907
908static int sh_eth_reset(struct net_device *ndev)
909{
910 struct sh_eth_private *mdp = netdev_priv(ndev);
911 int ret = 0;
912
Simon Hormandb893472014-01-17 09:22:28 +0900913 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000914 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300915 sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000916
917 ret = sh_eth_check_reset(ndev);
918 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100919 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000920
921 /* Table Init */
922 sh_eth_write(ndev, 0x0, TDLAR);
923 sh_eth_write(ndev, 0x0, TDFAR);
924 sh_eth_write(ndev, 0x0, TDFXR);
925 sh_eth_write(ndev, 0x0, TDFFR);
926 sh_eth_write(ndev, 0x0, RDLAR);
927 sh_eth_write(ndev, 0x0, RDFAR);
928 sh_eth_write(ndev, 0x0, RDFXR);
929 sh_eth_write(ndev, 0x0, RDFFR);
930
931 /* Reset HW CRC register */
932 if (mdp->cd->hw_crc)
933 sh_eth_write(ndev, 0x0, CSMR);
934
935 /* Select MII mode */
936 if (mdp->cd->select_mii)
937 sh_eth_select_mii(ndev);
938 } else {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300939 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000940 mdelay(3);
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300941 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000942 }
943
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000944 return ret;
945}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000946
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000947static void sh_eth_set_receive_align(struct sk_buff *skb)
948{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900949 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000950
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000951 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900952 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000953}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000954
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300955/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956static void update_mac_address(struct net_device *ndev)
957{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000958 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300959 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
960 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000961 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300962 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700963}
964
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300965/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700966 *
967 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
968 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
969 * When you want use this device, you must set MAC address in bootloader.
970 *
971 */
Magnus Damm748031f2009-10-09 00:17:14 +0000972static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700973{
Magnus Damm748031f2009-10-09 00:17:14 +0000974 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700975 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000976 } else {
Sergei Shtylyov37742f02015-12-05 00:58:57 +0300977 u32 mahr = sh_eth_read(ndev, MAHR);
978 u32 malr = sh_eth_read(ndev, MALR);
979
980 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
981 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
982 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
983 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
984 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
985 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Magnus Damm748031f2009-10-09 00:17:14 +0000986 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700987}
988
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100989static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000990{
Simon Hormandb893472014-01-17 09:22:28 +0900991 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000992 return EDTRR_TRNS_GETHER;
993 else
994 return EDTRR_TRNS_ETHER;
995}
996
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700997struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000998 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700999 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001000 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001001};
1002
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001003static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001004{
1005 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001006 u32 pir;
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001007
1008 if (bitbang->set_gate)
1009 bitbang->set_gate(bitbang->addr);
1010
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001011 pir = ioread32(bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001012 if (set)
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001013 pir |= mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001014 else
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001015 pir &= ~mask;
1016 iowrite32(pir, bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001017}
1018
1019/* Data I/O pin control */
1020static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1021{
1022 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001023}
1024
1025/* Set bit data*/
1026static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1027{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001028 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029}
1030
1031/* Get bit data*/
1032static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1033{
1034 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001035
1036 if (bitbang->set_gate)
1037 bitbang->set_gate(bitbang->addr);
1038
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001039 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001040}
1041
1042/* MDC pin control */
1043static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1044{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001045 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001046}
1047
1048/* mdio bus control struct */
1049static struct mdiobb_ops bb_ops = {
1050 .owner = THIS_MODULE,
1051 .set_mdc = sh_mdc_ctrl,
1052 .set_mdio_dir = sh_mmd_ctrl,
1053 .set_mdio_data = sh_set_mdio,
1054 .get_mdio_data = sh_get_mdio,
1055};
1056
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001057/* free skb and descriptor buffer */
1058static void sh_eth_ring_free(struct net_device *ndev)
1059{
1060 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001061 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001062
1063 /* Free Rx skb ringbuffer */
1064 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001065 for (i = 0; i < mdp->num_rx_ring; i++)
1066 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001067 }
1068 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001069 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001070
1071 /* Free Tx skb ringbuffer */
1072 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001073 for (i = 0; i < mdp->num_tx_ring; i++)
1074 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001075 }
1076 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001077 mdp->tx_skbuff = NULL;
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001078
1079 if (mdp->rx_ring) {
1080 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1081 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1082 mdp->rx_desc_dma);
1083 mdp->rx_ring = NULL;
1084 }
1085
1086 if (mdp->tx_ring) {
1087 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1088 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1089 mdp->tx_desc_dma);
1090 mdp->tx_ring = NULL;
1091 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001092}
1093
1094/* format skb and descriptor buffer */
1095static void sh_eth_ring_format(struct net_device *ndev)
1096{
1097 struct sh_eth_private *mdp = netdev_priv(ndev);
1098 int i;
1099 struct sk_buff *skb;
1100 struct sh_eth_rxdesc *rxdesc = NULL;
1101 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001102 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1103 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001104 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001105 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001106 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001107
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001108 mdp->cur_rx = 0;
1109 mdp->cur_tx = 0;
1110 mdp->dirty_rx = 0;
1111 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001112
1113 memset(mdp->rx_ring, 0, rx_ringsize);
1114
1115 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001116 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001117 /* skb */
1118 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001119 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001120 if (skb == NULL)
1121 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001122 sh_eth_set_receive_align(skb);
1123
Sergei Shtylyovab857912015-10-24 00:46:03 +03001124 /* The size of the buffer is a multiple of 32 bytes. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001125 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001126 dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len,
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001127 DMA_FROM_DEVICE);
1128 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1129 kfree_skb(skb);
1130 break;
1131 }
1132 mdp->rx_skbuff[i] = skb;
Sergei Shtylyovd0ba9132016-03-08 01:37:09 +03001133
1134 /* RX descriptor */
1135 rxdesc = &mdp->rx_ring[i];
1136 rxdesc->len = cpu_to_le32(buf_len << 16);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001137 rxdesc->addr = cpu_to_le32(dma_addr);
1138 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001139
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001140 /* Rx descriptor address set */
1141 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001142 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001143 if (sh_eth_is_gether(mdp) ||
1144 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001145 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001146 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001147 }
1148
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001149 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001150
1151 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc1b7fca2016-03-08 01:36:28 +03001152 if (rxdesc)
1153 rxdesc->status |= cpu_to_le32(RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001154
1155 memset(mdp->tx_ring, 0, tx_ringsize);
1156
1157 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001158 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001159 mdp->tx_skbuff[i] = NULL;
1160 txdesc = &mdp->tx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001161 txdesc->status = cpu_to_le32(TD_TFP);
1162 txdesc->len = cpu_to_le32(0);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001163 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001164 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001165 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001166 if (sh_eth_is_gether(mdp) ||
1167 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001168 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001169 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170 }
1171
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001172 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001173}
1174
1175/* Get skb and descriptor buffer */
1176static int sh_eth_ring_init(struct net_device *ndev)
1177{
1178 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001179 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001180
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001181 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182 * card needs room to do 8 byte alignment, +2 so we can reserve
1183 * the first 2 bytes, and +16 gets room for the status word from the
1184 * card.
1185 */
1186 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1187 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001188 if (mdp->cd->rpadir)
1189 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001190
1191 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001192 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1193 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001194 if (!mdp->rx_skbuff)
1195 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001196
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001197 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1198 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001199 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001200 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001201
1202 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001203 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001204 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001205 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001206 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001207 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001208
1209 mdp->dirty_rx = 0;
1210
1211 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001212 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001213 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001214 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001215 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001216 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001217 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001218
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001219ring_free:
1220 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001221 sh_eth_ring_free(ndev);
1222
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001223 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001224}
1225
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001226static int sh_eth_dev_init(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001227{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001228 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001229 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001230
1231 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001232 ret = sh_eth_reset(ndev);
1233 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001234 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001235
Simon Horman55754f12013-07-23 10:18:04 +09001236 if (mdp->cd->rmiimode)
1237 sh_eth_write(ndev, 0x1, RMIIMODE);
1238
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001239 /* Descriptor format */
1240 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001241 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001242 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001243
1244 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001245 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001246
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001247#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001248 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001249 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001250 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001251#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001252 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001253
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001254 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001255 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1256 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001257
Ben Dooks530aa2d2014-06-03 12:21:13 +01001258 /* Frame recv control (enable multiple-packets per rx irq) */
1259 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001260
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001261 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001262
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001263 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001264 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001265
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001266 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001267
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001268 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001269 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001270
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001271 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001272 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1273 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001274
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001275 sh_eth_modify(ndev, EESR, 0, 0);
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001276 mdp->irq_enabled = true;
1277 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001278
1279 /* PAUSE Prohibition */
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001280 sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
1281 ECMR_TE | ECMR_RE, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001282
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001283 if (mdp->cd->set_rate)
1284 mdp->cd->set_rate(ndev);
1285
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001286 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001287 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001288
1289 /* E-MAC Interrupt Enable register */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001290 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001291
1292 /* Set MAC address */
1293 update_mac_address(ndev);
1294
1295 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001296 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001297 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001298 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001299 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001300 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001301 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001302
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001303 /* Setting the Rx mode will start the Rx process. */
1304 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305
1306 return ret;
1307}
1308
Ben Hutchings740c7f32015-01-27 00:49:32 +00001309static void sh_eth_dev_exit(struct net_device *ndev)
1310{
1311 struct sh_eth_private *mdp = netdev_priv(ndev);
1312 int i;
1313
1314 /* Deactivate all TX descriptors, so DMA should stop at next
1315 * packet boundary if it's currently running
1316 */
1317 for (i = 0; i < mdp->num_tx_ring; i++)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001318 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001319
1320 /* Disable TX FIFO egress to MAC */
1321 sh_eth_rcv_snd_disable(ndev);
1322
1323 /* Stop RX DMA at next packet boundary */
1324 sh_eth_write(ndev, 0, EDRRR);
1325
1326 /* Aside from TX DMA, we can't tell when the hardware is
1327 * really stopped, so we need to reset to make sure.
1328 * Before doing that, wait for long enough to *probably*
1329 * finish transmitting the last packet and poll stats.
1330 */
1331 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1332 sh_eth_get_stats(ndev);
1333 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001334
1335 /* Set MAC address again */
1336 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001337}
1338
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001339/* free Tx skb function */
1340static int sh_eth_txfree(struct net_device *ndev)
1341{
1342 struct sh_eth_private *mdp = netdev_priv(ndev);
1343 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001344 int free_num = 0;
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001345 int entry;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001346
1347 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001348 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001349 txdesc = &mdp->tx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001350 if (txdesc->status & cpu_to_le32(TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001351 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001352 /* TACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001353 dma_rmb();
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001354 netif_info(mdp, tx_done, ndev,
1355 "tx entry %d status 0x%08x\n",
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001356 entry, le32_to_cpu(txdesc->status));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001357 /* Free the original skb. */
1358 if (mdp->tx_skbuff[entry]) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001359 dma_unmap_single(&ndev->dev, le32_to_cpu(txdesc->addr),
1360 le32_to_cpu(txdesc->len) >> 16,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001361 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001362 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1363 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001364 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001365 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001366 txdesc->status = cpu_to_le32(TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001367 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001368 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001369
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001370 ndev->stats.tx_packets++;
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001371 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001372 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001373 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374}
1375
1376/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001377static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378{
1379 struct sh_eth_private *mdp = netdev_priv(ndev);
1380 struct sh_eth_rxdesc *rxdesc;
1381
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001382 int entry = mdp->cur_rx % mdp->num_rx_ring;
1383 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001384 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001385 struct sk_buff *skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001386 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001387 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001388 dma_addr_t dma_addr;
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001389 u16 pkt_len;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001390 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001391
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001392 boguscnt = min(boguscnt, *quota);
1393 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001395 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001396 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001397 dma_rmb();
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001398 desc_status = le32_to_cpu(rxdesc->status);
1399 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001400
1401 if (--boguscnt < 0)
1402 break;
1403
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001404 netif_info(mdp, rx_status, ndev,
1405 "rx entry %d status 0x%08x len %d\n",
1406 entry, desc_status, pkt_len);
1407
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001408 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001409 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001410
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001411 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001412 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001413 * bit 0. However, in case of the R8A7740 and R7S72100
1414 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001415 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001416 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001417 if (mdp->cd->shift_rd0)
1418 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001419
Sergei Shtylyov248be832015-12-04 01:45:40 +03001420 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001421 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1422 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001423 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001424 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001425 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001426 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001427 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001429 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001430 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001431 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001432 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001433 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001434 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001435 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001436 } else if (skb) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001437 dma_addr = le32_to_cpu(rxdesc->addr);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001438 if (!mdp->cd->hw_swap)
1439 sh_eth_soft_swap(
Sergei Shtylyov12996532015-12-13 23:05:07 +03001440 phys_to_virt(ALIGN(dma_addr, 4)),
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001441 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001442 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001443 if (mdp->cd->rpadir)
1444 skb_reserve(skb, NET_IP_ALIGN);
Sergei Shtylyov12996532015-12-13 23:05:07 +03001445 dma_unmap_single(&ndev->dev, dma_addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001446 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001447 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448 skb_put(skb, pkt_len);
1449 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001450 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001451 ndev->stats.rx_packets++;
1452 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001453 if (desc_status & RD_RFS8)
1454 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001456 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001457 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001458 }
1459
1460 /* Refill the Rx ring buffers. */
1461 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001462 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001463 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001464 /* The size of the buffer is 32 byte boundary. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001465 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001466 rxdesc->len = cpu_to_le32(buf_len << 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001467
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001468 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001469 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001470 if (skb == NULL)
1471 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001472 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001473 dma_addr = dma_map_single(&ndev->dev, skb->data,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001474 buf_len, DMA_FROM_DEVICE);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001475 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1476 kfree_skb(skb);
1477 break;
1478 }
1479 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001480
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001481 skb_checksum_none_assert(skb);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001482 rxdesc->addr = cpu_to_le32(dma_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001484 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001485 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001486 rxdesc->status |=
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001487 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001488 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001489 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001490 }
1491
1492 /* Restart Rx engine if stopped. */
1493 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001494 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001495 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001496 if (intr_status & EESR_RDE &&
1497 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001498 u32 count = (sh_eth_read(ndev, RDFAR) -
1499 sh_eth_read(ndev, RDLAR)) >> 4;
1500
1501 mdp->cur_rx = count;
1502 mdp->dirty_rx = count;
1503 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001504 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001505 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001507 *quota -= limit - boguscnt - 1;
1508
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001509 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001510}
1511
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001512static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001513{
1514 /* disable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001515 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001516}
1517
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001518static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001519{
1520 /* enable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001521 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001522}
1523
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001524/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001525static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001526{
1527 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001528 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001529 u32 link_stat;
1530 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531
1532 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001533 felic_stat = sh_eth_read(ndev, ECSR);
1534 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001536 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001537 if (felic_stat & ECSR_LCHNG) {
1538 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001539 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001540 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001541 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001542 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001543 if (mdp->ether_link_active_low)
1544 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001545 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001546 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001547 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001548 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001549 /* Link Up */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001550 sh_eth_modify(ndev, EESIPR, DMAC_M_ECI, 0);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001551 /* clear int */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001552 sh_eth_modify(ndev, ECSR, 0, 0);
1553 sh_eth_modify(ndev, EESIPR, DMAC_M_ECI,
1554 DMAC_M_ECI);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001555 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001556 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001557 }
1558 }
1559 }
1560
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001561ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001562 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001563 /* Unused write back interrupt */
1564 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001565 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001566 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001567 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001568 }
1569
1570 if (intr_status & EESR_RABT) {
1571 /* Receive Abort int */
1572 if (intr_status & EESR_RFRMER) {
1573 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001574 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575 }
1576 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001577
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001578 if (intr_status & EESR_TDE) {
1579 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001580 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001581 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001582 }
1583
1584 if (intr_status & EESR_TFE) {
1585 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001586 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001587 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001588 }
1589
1590 if (intr_status & EESR_RDE) {
1591 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001592 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001593 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001594
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595 if (intr_status & EESR_RFE) {
1596 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001597 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001598 }
1599
1600 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1601 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001602 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001603 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001604 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001605
1606 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1607 if (mdp->cd->no_ade)
1608 mask &= ~EESR_ADE;
1609 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001611 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001612
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001613 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001614 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1615 intr_status, mdp->cur_tx, mdp->dirty_tx,
1616 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001617 /* dirty buffer free */
1618 sh_eth_txfree(ndev);
1619
1620 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001621 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001623 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001624 }
1625 /* wakeup */
1626 netif_wake_queue(ndev);
1627 }
1628}
1629
1630static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1631{
1632 struct net_device *ndev = netdev;
1633 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001634 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001635 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001636 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001637
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001638 spin_lock(&mdp->lock);
1639
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001640 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001641 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001642 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1643 * enabled since it's the one that comes thru regardless of the mask,
1644 * and we need to fully handle it in sh_eth_error() in order to quench
1645 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1646 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001647 intr_enable = sh_eth_read(ndev, EESIPR);
1648 intr_status &= intr_enable | DMAC_M_ECI;
1649 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001650 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001651 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001652 goto out;
1653
1654 if (!likely(mdp->irq_enabled)) {
1655 sh_eth_write(ndev, 0, EESIPR);
1656 goto out;
1657 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001658
Sergei Shtylyov37191092013-06-19 23:30:23 +04001659 if (intr_status & EESR_RX_CHECK) {
1660 if (napi_schedule_prep(&mdp->napi)) {
1661 /* Mask Rx interrupts */
1662 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1663 EESIPR);
1664 __napi_schedule(&mdp->napi);
1665 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001666 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001667 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001668 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001669 }
1670 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001671
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001672 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001673 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001674 /* Clear Tx interrupts */
1675 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1676
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001677 sh_eth_txfree(ndev);
1678 netif_wake_queue(ndev);
1679 }
1680
Sergei Shtylyov37191092013-06-19 23:30:23 +04001681 if (intr_status & cd->eesr_err_check) {
1682 /* Clear error interrupts */
1683 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1684
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001685 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001686 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001687
Ben Hutchings283e38d2015-01-22 12:44:08 +00001688out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001689 spin_unlock(&mdp->lock);
1690
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001691 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001692}
1693
Sergei Shtylyov37191092013-06-19 23:30:23 +04001694static int sh_eth_poll(struct napi_struct *napi, int budget)
1695{
1696 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1697 napi);
1698 struct net_device *ndev = napi->dev;
1699 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001700 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001701
1702 for (;;) {
1703 intr_status = sh_eth_read(ndev, EESR);
1704 if (!(intr_status & EESR_RX_CHECK))
1705 break;
1706 /* Clear Rx interrupts */
1707 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1708
1709 if (sh_eth_rx(ndev, intr_status, &quota))
1710 goto out;
1711 }
1712
1713 napi_complete(napi);
1714
1715 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001716 if (mdp->irq_enabled)
1717 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001718out:
1719 return budget - quota;
1720}
1721
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001722/* PHY state control function */
1723static void sh_eth_adjust_link(struct net_device *ndev)
1724{
1725 struct sh_eth_private *mdp = netdev_priv(ndev);
Philippe Reynes9fd03752016-08-10 00:04:48 +02001726 struct phy_device *phydev = ndev->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001727 int new_state = 0;
1728
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001729 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001730 if (phydev->duplex != mdp->duplex) {
1731 new_state = 1;
1732 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001733 if (mdp->cd->set_duplex)
1734 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001735 }
1736
1737 if (phydev->speed != mdp->speed) {
1738 new_state = 1;
1739 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001740 if (mdp->cd->set_rate)
1741 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001742 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001743 if (!mdp->link) {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001744 sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001745 new_state = 1;
1746 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001747 if (mdp->cd->no_psr || mdp->no_ether_link)
1748 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001749 }
1750 } else if (mdp->link) {
1751 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001752 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001753 mdp->speed = 0;
1754 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001755 if (mdp->cd->no_psr || mdp->no_ether_link)
1756 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001757 }
1758
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001759 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001760 phy_print_status(phydev);
1761}
1762
1763/* PHY init function */
1764static int sh_eth_phy_init(struct net_device *ndev)
1765{
Ben Dooks702eca02014-03-12 17:47:40 +00001766 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001767 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001768 struct phy_device *phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001769
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001770 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001771 mdp->speed = 0;
1772 mdp->duplex = -1;
1773
1774 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001775 if (np) {
1776 struct device_node *pn;
1777
1778 pn = of_parse_phandle(np, "phy-handle", 0);
1779 phydev = of_phy_connect(ndev, pn,
1780 sh_eth_adjust_link, 0,
1781 mdp->phy_interface);
1782
Peter Chen8da703d2016-08-01 15:02:40 +08001783 of_node_put(pn);
Ben Dooks702eca02014-03-12 17:47:40 +00001784 if (!phydev)
1785 phydev = ERR_PTR(-ENOENT);
1786 } else {
1787 char phy_id[MII_BUS_ID_SIZE + 3];
1788
1789 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1790 mdp->mii_bus->id, mdp->phy_id);
1791
1792 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1793 mdp->phy_interface);
1794 }
1795
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001796 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001797 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001798 return PTR_ERR(phydev);
1799 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001800
Andrew Lunn22209432016-01-06 20:11:13 +01001801 phy_attached_info(phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001802
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 return 0;
1804}
1805
1806/* PHY control start function */
1807static int sh_eth_phy_start(struct net_device *ndev)
1808{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001809 int ret;
1810
1811 ret = sh_eth_phy_init(ndev);
1812 if (ret)
1813 return ret;
1814
Philippe Reynes9fd03752016-08-10 00:04:48 +02001815 phy_start(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001816
1817 return 0;
1818}
1819
Philippe Reynesf08aff42016-08-10 00:04:49 +02001820static int sh_eth_get_link_ksettings(struct net_device *ndev,
1821 struct ethtool_link_ksettings *cmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001822{
1823 struct sh_eth_private *mdp = netdev_priv(ndev);
1824 unsigned long flags;
1825 int ret;
1826
Philippe Reynes9fd03752016-08-10 00:04:48 +02001827 if (!ndev->phydev)
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001828 return -ENODEV;
1829
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001830 spin_lock_irqsave(&mdp->lock, flags);
Philippe Reynesf08aff42016-08-10 00:04:49 +02001831 ret = phy_ethtool_ksettings_get(ndev->phydev, cmd);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001832 spin_unlock_irqrestore(&mdp->lock, flags);
1833
1834 return ret;
1835}
1836
Philippe Reynesf08aff42016-08-10 00:04:49 +02001837static int sh_eth_set_link_ksettings(struct net_device *ndev,
1838 const struct ethtool_link_ksettings *cmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001839{
1840 struct sh_eth_private *mdp = netdev_priv(ndev);
1841 unsigned long flags;
1842 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001843
Philippe Reynes9fd03752016-08-10 00:04:48 +02001844 if (!ndev->phydev)
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001845 return -ENODEV;
1846
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001847 spin_lock_irqsave(&mdp->lock, flags);
1848
1849 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001850 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001851
Philippe Reynesf08aff42016-08-10 00:04:49 +02001852 ret = phy_ethtool_ksettings_set(ndev->phydev, cmd);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001853 if (ret)
1854 goto error_exit;
1855
Philippe Reynesf08aff42016-08-10 00:04:49 +02001856 if (cmd->base.duplex == DUPLEX_FULL)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001857 mdp->duplex = 1;
1858 else
1859 mdp->duplex = 0;
1860
1861 if (mdp->cd->set_duplex)
1862 mdp->cd->set_duplex(ndev);
1863
1864error_exit:
1865 mdelay(1);
1866
1867 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001868 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001869
1870 spin_unlock_irqrestore(&mdp->lock, flags);
1871
1872 return ret;
1873}
1874
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00001875/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1876 * version must be bumped as well. Just adding registers up to that
1877 * limit is fine, as long as the existing register indices don't
1878 * change.
1879 */
1880#define SH_ETH_REG_DUMP_VERSION 1
1881#define SH_ETH_REG_DUMP_MAX_REGS 256
1882
1883static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1884{
1885 struct sh_eth_private *mdp = netdev_priv(ndev);
1886 struct sh_eth_cpu_data *cd = mdp->cd;
1887 u32 *valid_map;
1888 size_t len;
1889
1890 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1891
1892 /* Dump starts with a bitmap that tells ethtool which
1893 * registers are defined for this chip.
1894 */
1895 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1896 if (buf) {
1897 valid_map = buf;
1898 buf += len;
1899 } else {
1900 valid_map = NULL;
1901 }
1902
1903 /* Add a register to the dump, if it has a defined offset.
1904 * This automatically skips most undefined registers, but for
1905 * some it is also necessary to check a capability flag in
1906 * struct sh_eth_cpu_data.
1907 */
1908#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1909#define add_reg_from(reg, read_expr) do { \
1910 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1911 if (buf) { \
1912 mark_reg_valid(reg); \
1913 *buf++ = read_expr; \
1914 } \
1915 ++len; \
1916 } \
1917 } while (0)
1918#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1919#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1920
1921 add_reg(EDSR);
1922 add_reg(EDMR);
1923 add_reg(EDTRR);
1924 add_reg(EDRRR);
1925 add_reg(EESR);
1926 add_reg(EESIPR);
1927 add_reg(TDLAR);
1928 add_reg(TDFAR);
1929 add_reg(TDFXR);
1930 add_reg(TDFFR);
1931 add_reg(RDLAR);
1932 add_reg(RDFAR);
1933 add_reg(RDFXR);
1934 add_reg(RDFFR);
1935 add_reg(TRSCER);
1936 add_reg(RMFCR);
1937 add_reg(TFTR);
1938 add_reg(FDR);
1939 add_reg(RMCR);
1940 add_reg(TFUCR);
1941 add_reg(RFOCR);
1942 if (cd->rmiimode)
1943 add_reg(RMIIMODE);
1944 add_reg(FCFTR);
1945 if (cd->rpadir)
1946 add_reg(RPADIR);
1947 if (!cd->no_trimd)
1948 add_reg(TRIMD);
1949 add_reg(ECMR);
1950 add_reg(ECSR);
1951 add_reg(ECSIPR);
1952 add_reg(PIR);
1953 if (!cd->no_psr)
1954 add_reg(PSR);
1955 add_reg(RDMLR);
1956 add_reg(RFLR);
1957 add_reg(IPGR);
1958 if (cd->apr)
1959 add_reg(APR);
1960 if (cd->mpr)
1961 add_reg(MPR);
1962 add_reg(RFCR);
1963 add_reg(RFCF);
1964 if (cd->tpauser)
1965 add_reg(TPAUSER);
1966 add_reg(TPAUSECR);
1967 add_reg(GECMR);
1968 if (cd->bculr)
1969 add_reg(BCULR);
1970 add_reg(MAHR);
1971 add_reg(MALR);
1972 add_reg(TROCR);
1973 add_reg(CDCR);
1974 add_reg(LCCR);
1975 add_reg(CNDCR);
1976 add_reg(CEFCR);
1977 add_reg(FRECR);
1978 add_reg(TSFRCR);
1979 add_reg(TLFRCR);
1980 add_reg(CERCR);
1981 add_reg(CEECR);
1982 add_reg(MAFCR);
1983 if (cd->rtrate)
1984 add_reg(RTRATE);
1985 if (cd->hw_crc)
1986 add_reg(CSMR);
1987 if (cd->select_mii)
1988 add_reg(RMII_MII);
1989 add_reg(ARSTR);
1990 if (cd->tsu) {
1991 add_tsu_reg(TSU_CTRST);
1992 add_tsu_reg(TSU_FWEN0);
1993 add_tsu_reg(TSU_FWEN1);
1994 add_tsu_reg(TSU_FCM);
1995 add_tsu_reg(TSU_BSYSL0);
1996 add_tsu_reg(TSU_BSYSL1);
1997 add_tsu_reg(TSU_PRISL0);
1998 add_tsu_reg(TSU_PRISL1);
1999 add_tsu_reg(TSU_FWSL0);
2000 add_tsu_reg(TSU_FWSL1);
2001 add_tsu_reg(TSU_FWSLC);
2002 add_tsu_reg(TSU_QTAG0);
2003 add_tsu_reg(TSU_QTAG1);
2004 add_tsu_reg(TSU_QTAGM0);
2005 add_tsu_reg(TSU_QTAGM1);
2006 add_tsu_reg(TSU_FWSR);
2007 add_tsu_reg(TSU_FWINMK);
2008 add_tsu_reg(TSU_ADQT0);
2009 add_tsu_reg(TSU_ADQT1);
2010 add_tsu_reg(TSU_VTAG0);
2011 add_tsu_reg(TSU_VTAG1);
2012 add_tsu_reg(TSU_ADSBSY);
2013 add_tsu_reg(TSU_TEN);
2014 add_tsu_reg(TSU_POST1);
2015 add_tsu_reg(TSU_POST2);
2016 add_tsu_reg(TSU_POST3);
2017 add_tsu_reg(TSU_POST4);
2018 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2019 /* This is the start of a table, not just a single
2020 * register.
2021 */
2022 if (buf) {
2023 unsigned int i;
2024
2025 mark_reg_valid(TSU_ADRH0);
2026 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2027 *buf++ = ioread32(
2028 mdp->tsu_addr +
2029 mdp->reg_offset[TSU_ADRH0] +
2030 i * 4);
2031 }
2032 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2033 }
2034 }
2035
2036#undef mark_reg_valid
2037#undef add_reg_from
2038#undef add_reg
2039#undef add_tsu_reg
2040
2041 return len * 4;
2042}
2043
2044static int sh_eth_get_regs_len(struct net_device *ndev)
2045{
2046 return __sh_eth_get_regs(ndev, NULL);
2047}
2048
2049static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2050 void *buf)
2051{
2052 struct sh_eth_private *mdp = netdev_priv(ndev);
2053
2054 regs->version = SH_ETH_REG_DUMP_VERSION;
2055
2056 pm_runtime_get_sync(&mdp->pdev->dev);
2057 __sh_eth_get_regs(ndev, buf);
2058 pm_runtime_put_sync(&mdp->pdev->dev);
2059}
2060
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002061static int sh_eth_nway_reset(struct net_device *ndev)
2062{
2063 struct sh_eth_private *mdp = netdev_priv(ndev);
2064 unsigned long flags;
2065 int ret;
2066
Philippe Reynes9fd03752016-08-10 00:04:48 +02002067 if (!ndev->phydev)
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002068 return -ENODEV;
2069
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002070 spin_lock_irqsave(&mdp->lock, flags);
Philippe Reynes9fd03752016-08-10 00:04:48 +02002071 ret = phy_start_aneg(ndev->phydev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002072 spin_unlock_irqrestore(&mdp->lock, flags);
2073
2074 return ret;
2075}
2076
2077static u32 sh_eth_get_msglevel(struct net_device *ndev)
2078{
2079 struct sh_eth_private *mdp = netdev_priv(ndev);
2080 return mdp->msg_enable;
2081}
2082
2083static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2084{
2085 struct sh_eth_private *mdp = netdev_priv(ndev);
2086 mdp->msg_enable = value;
2087}
2088
2089static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2090 "rx_current", "tx_current",
2091 "rx_dirty", "tx_dirty",
2092};
2093#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2094
2095static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2096{
2097 switch (sset) {
2098 case ETH_SS_STATS:
2099 return SH_ETH_STATS_LEN;
2100 default:
2101 return -EOPNOTSUPP;
2102 }
2103}
2104
2105static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002106 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002107{
2108 struct sh_eth_private *mdp = netdev_priv(ndev);
2109 int i = 0;
2110
2111 /* device-specific stats */
2112 data[i++] = mdp->cur_rx;
2113 data[i++] = mdp->cur_tx;
2114 data[i++] = mdp->dirty_rx;
2115 data[i++] = mdp->dirty_tx;
2116}
2117
2118static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2119{
2120 switch (stringset) {
2121 case ETH_SS_STATS:
2122 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002123 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002124 break;
2125 }
2126}
2127
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002128static void sh_eth_get_ringparam(struct net_device *ndev,
2129 struct ethtool_ringparam *ring)
2130{
2131 struct sh_eth_private *mdp = netdev_priv(ndev);
2132
2133 ring->rx_max_pending = RX_RING_MAX;
2134 ring->tx_max_pending = TX_RING_MAX;
2135 ring->rx_pending = mdp->num_rx_ring;
2136 ring->tx_pending = mdp->num_tx_ring;
2137}
2138
2139static int sh_eth_set_ringparam(struct net_device *ndev,
2140 struct ethtool_ringparam *ring)
2141{
2142 struct sh_eth_private *mdp = netdev_priv(ndev);
2143 int ret;
2144
2145 if (ring->tx_pending > TX_RING_MAX ||
2146 ring->rx_pending > RX_RING_MAX ||
2147 ring->tx_pending < TX_RING_MIN ||
2148 ring->rx_pending < RX_RING_MIN)
2149 return -EINVAL;
2150 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2151 return -EINVAL;
2152
2153 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002154 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002155 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002156
Ben Hutchings283e38d2015-01-22 12:44:08 +00002157 /* Serialise with the interrupt handler and NAPI, then
2158 * disable interrupts. We have to clear the
2159 * irq_enabled flag first to ensure that interrupts
2160 * won't be re-enabled.
2161 */
2162 mdp->irq_enabled = false;
2163 synchronize_irq(ndev->irq);
2164 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002165 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002166
Ben Hutchings740c7f32015-01-27 00:49:32 +00002167 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002168
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002169 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002170 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002171 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002172
2173 /* Set new parameters */
2174 mdp->num_rx_ring = ring->rx_pending;
2175 mdp->num_tx_ring = ring->tx_pending;
2176
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002177 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002178 ret = sh_eth_ring_init(ndev);
2179 if (ret < 0) {
2180 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2181 __func__);
2182 return ret;
2183 }
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002184 ret = sh_eth_dev_init(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002185 if (ret < 0) {
2186 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2187 __func__);
2188 return ret;
2189 }
2190
Ben Hutchingsbd888912015-01-22 12:40:25 +00002191 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002192 }
2193
2194 return 0;
2195}
2196
stephen hemminger9b07be42012-01-04 12:59:49 +00002197static const struct ethtool_ops sh_eth_ethtool_ops = {
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002198 .get_regs_len = sh_eth_get_regs_len,
2199 .get_regs = sh_eth_get_regs,
stephen hemminger9b07be42012-01-04 12:59:49 +00002200 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002201 .get_msglevel = sh_eth_get_msglevel,
2202 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002203 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002204 .get_strings = sh_eth_get_strings,
2205 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2206 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002207 .get_ringparam = sh_eth_get_ringparam,
2208 .set_ringparam = sh_eth_set_ringparam,
Philippe Reynesf08aff42016-08-10 00:04:49 +02002209 .get_link_ksettings = sh_eth_get_link_ksettings,
2210 .set_link_ksettings = sh_eth_set_link_ksettings,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002211};
2212
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002213/* network device open function */
2214static int sh_eth_open(struct net_device *ndev)
2215{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002216 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03002217 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002218
Magnus Dammbcd51492009-10-09 00:20:04 +00002219 pm_runtime_get_sync(&mdp->pdev->dev);
2220
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002221 napi_enable(&mdp->napi);
2222
Joe Perchesa0607fd2009-11-18 23:29:17 -08002223 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002224 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002225 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002226 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002227 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002228 }
2229
2230 /* Descriptor set */
2231 ret = sh_eth_ring_init(ndev);
2232 if (ret)
2233 goto out_free_irq;
2234
2235 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002236 ret = sh_eth_dev_init(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002237 if (ret)
2238 goto out_free_irq;
2239
2240 /* PHY control start*/
2241 ret = sh_eth_phy_start(ndev);
2242 if (ret)
2243 goto out_free_irq;
2244
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002245 netif_start_queue(ndev);
2246
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002247 mdp->is_opened = 1;
2248
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002249 return ret;
2250
2251out_free_irq:
2252 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002253out_napi_off:
2254 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002255 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002256 return ret;
2257}
2258
2259/* Timeout function */
2260static void sh_eth_tx_timeout(struct net_device *ndev)
2261{
2262 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002263 struct sh_eth_rxdesc *rxdesc;
2264 int i;
2265
2266 netif_stop_queue(ndev);
2267
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002268 netif_err(mdp, timer, ndev,
2269 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002270 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002271
2272 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002273 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002274
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002275 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002276 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002277 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002278 rxdesc->status = cpu_to_le32(0);
2279 rxdesc->addr = cpu_to_le32(0xBADF00D0);
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002280 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002281 mdp->rx_skbuff[i] = NULL;
2282 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002283 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002284 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002285 mdp->tx_skbuff[i] = NULL;
2286 }
2287
2288 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002289 sh_eth_dev_init(ndev);
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002290
2291 netif_start_queue(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002292}
2293
2294/* Packet transmit function */
2295static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2296{
2297 struct sh_eth_private *mdp = netdev_priv(ndev);
2298 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov12996532015-12-13 23:05:07 +03002299 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002300 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002301 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002302
2303 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002304 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002305 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002306 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002307 netif_stop_queue(ndev);
2308 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002309 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002310 }
2311 }
2312 spin_unlock_irqrestore(&mdp->lock, flags);
2313
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002314 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002315 return NETDEV_TX_OK;
2316
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002317 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002318 mdp->tx_skbuff[entry] = skb;
2319 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002320 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002321 if (!mdp->cd->hw_swap)
Sergei Shtylyov3e230992015-12-13 21:27:04 +03002322 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
Sergei Shtylyov12996532015-12-13 23:05:07 +03002323 dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2324 DMA_TO_DEVICE);
2325 if (dma_mapping_error(&ndev->dev, dma_addr)) {
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002326 kfree_skb(skb);
2327 return NETDEV_TX_OK;
2328 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002329 txdesc->addr = cpu_to_le32(dma_addr);
2330 txdesc->len = cpu_to_le32(skb->len << 16);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002331
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002332 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002333 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002334 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002335 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002336 txdesc->status |= cpu_to_le32(TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002337
2338 mdp->cur_tx++;
2339
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002340 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2341 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002342
Patrick McHardy6ed10652009-06-23 06:03:08 +00002343 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002344}
2345
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002346/* The statistics registers have write-clear behaviour, which means we
2347 * will lose any increment between the read and write. We mitigate
2348 * this by only clearing when we read a non-zero value, so we will
2349 * never falsely report a total of zero.
2350 */
2351static void
2352sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2353{
2354 u32 delta = sh_eth_read(ndev, reg);
2355
2356 if (delta) {
2357 *stat += delta;
2358 sh_eth_write(ndev, 0, reg);
2359 }
2360}
2361
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002362static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2363{
2364 struct sh_eth_private *mdp = netdev_priv(ndev);
2365
2366 if (sh_eth_is_rz_fast_ether(mdp))
2367 return &ndev->stats;
2368
2369 if (!mdp->is_opened)
2370 return &ndev->stats;
2371
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002372 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2373 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2374 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002375
2376 if (sh_eth_is_gether(mdp)) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002377 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2378 CERCR);
2379 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2380 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002381 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002382 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2383 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002384 }
2385
2386 return &ndev->stats;
2387}
2388
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002389/* device close function */
2390static int sh_eth_close(struct net_device *ndev)
2391{
2392 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002393
2394 netif_stop_queue(ndev);
2395
Ben Hutchings283e38d2015-01-22 12:44:08 +00002396 /* Serialise with the interrupt handler and NAPI, then disable
2397 * interrupts. We have to clear the irq_enabled flag first to
2398 * ensure that interrupts won't be re-enabled.
2399 */
2400 mdp->irq_enabled = false;
2401 synchronize_irq(ndev->irq);
2402 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002403 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002404
Ben Hutchings740c7f32015-01-27 00:49:32 +00002405 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002406
2407 /* PHY Disconnect */
Philippe Reynes9fd03752016-08-10 00:04:48 +02002408 if (ndev->phydev) {
2409 phy_stop(ndev->phydev);
2410 phy_disconnect(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002411 }
2412
2413 free_irq(ndev->irq, ndev);
2414
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002415 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002416 sh_eth_ring_free(ndev);
2417
Magnus Dammbcd51492009-10-09 00:20:04 +00002418 pm_runtime_put_sync(&mdp->pdev->dev);
2419
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002420 mdp->is_opened = 0;
2421
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002422 return 0;
2423}
2424
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002425/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002426static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002427{
Philippe Reynes9fd03752016-08-10 00:04:48 +02002428 struct phy_device *phydev = ndev->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002429
2430 if (!netif_running(ndev))
2431 return -EINVAL;
2432
2433 if (!phydev)
2434 return -ENODEV;
2435
Richard Cochran28b04112010-07-17 08:48:55 +00002436 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002437}
2438
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002439/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2440static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2441 int entry)
2442{
2443 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2444}
2445
2446static u32 sh_eth_tsu_get_post_mask(int entry)
2447{
2448 return 0x0f << (28 - ((entry % 8) * 4));
2449}
2450
2451static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2452{
2453 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2454}
2455
2456static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2457 int entry)
2458{
2459 struct sh_eth_private *mdp = netdev_priv(ndev);
2460 u32 tmp;
2461 void *reg_offset;
2462
2463 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2464 tmp = ioread32(reg_offset);
2465 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2466}
2467
2468static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2469 int entry)
2470{
2471 struct sh_eth_private *mdp = netdev_priv(ndev);
2472 u32 post_mask, ref_mask, tmp;
2473 void *reg_offset;
2474
2475 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2476 post_mask = sh_eth_tsu_get_post_mask(entry);
2477 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2478
2479 tmp = ioread32(reg_offset);
2480 iowrite32(tmp & ~post_mask, reg_offset);
2481
2482 /* If other port enables, the function returns "true" */
2483 return tmp & ref_mask;
2484}
2485
2486static int sh_eth_tsu_busy(struct net_device *ndev)
2487{
2488 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2489 struct sh_eth_private *mdp = netdev_priv(ndev);
2490
2491 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2492 udelay(10);
2493 timeout--;
2494 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002495 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002496 return -ETIMEDOUT;
2497 }
2498 }
2499
2500 return 0;
2501}
2502
2503static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2504 const u8 *addr)
2505{
2506 u32 val;
2507
2508 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2509 iowrite32(val, reg);
2510 if (sh_eth_tsu_busy(ndev) < 0)
2511 return -EBUSY;
2512
2513 val = addr[4] << 8 | addr[5];
2514 iowrite32(val, reg + 4);
2515 if (sh_eth_tsu_busy(ndev) < 0)
2516 return -EBUSY;
2517
2518 return 0;
2519}
2520
2521static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2522{
2523 u32 val;
2524
2525 val = ioread32(reg);
2526 addr[0] = (val >> 24) & 0xff;
2527 addr[1] = (val >> 16) & 0xff;
2528 addr[2] = (val >> 8) & 0xff;
2529 addr[3] = val & 0xff;
2530 val = ioread32(reg + 4);
2531 addr[4] = (val >> 8) & 0xff;
2532 addr[5] = val & 0xff;
2533}
2534
2535
2536static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2537{
2538 struct sh_eth_private *mdp = netdev_priv(ndev);
2539 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2540 int i;
2541 u8 c_addr[ETH_ALEN];
2542
2543 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2544 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002545 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002546 return i;
2547 }
2548
2549 return -ENOENT;
2550}
2551
2552static int sh_eth_tsu_find_empty(struct net_device *ndev)
2553{
2554 u8 blank[ETH_ALEN];
2555 int entry;
2556
2557 memset(blank, 0, sizeof(blank));
2558 entry = sh_eth_tsu_find_entry(ndev, blank);
2559 return (entry < 0) ? -ENOMEM : entry;
2560}
2561
2562static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2563 int entry)
2564{
2565 struct sh_eth_private *mdp = netdev_priv(ndev);
2566 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2567 int ret;
2568 u8 blank[ETH_ALEN];
2569
2570 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2571 ~(1 << (31 - entry)), TSU_TEN);
2572
2573 memset(blank, 0, sizeof(blank));
2574 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2575 if (ret < 0)
2576 return ret;
2577 return 0;
2578}
2579
2580static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2581{
2582 struct sh_eth_private *mdp = netdev_priv(ndev);
2583 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2584 int i, ret;
2585
2586 if (!mdp->cd->tsu)
2587 return 0;
2588
2589 i = sh_eth_tsu_find_entry(ndev, addr);
2590 if (i < 0) {
2591 /* No entry found, create one */
2592 i = sh_eth_tsu_find_empty(ndev);
2593 if (i < 0)
2594 return -ENOMEM;
2595 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2596 if (ret < 0)
2597 return ret;
2598
2599 /* Enable the entry */
2600 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2601 (1 << (31 - i)), TSU_TEN);
2602 }
2603
2604 /* Entry found or created, enable POST */
2605 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2606
2607 return 0;
2608}
2609
2610static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2611{
2612 struct sh_eth_private *mdp = netdev_priv(ndev);
2613 int i, ret;
2614
2615 if (!mdp->cd->tsu)
2616 return 0;
2617
2618 i = sh_eth_tsu_find_entry(ndev, addr);
2619 if (i) {
2620 /* Entry found */
2621 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2622 goto done;
2623
2624 /* Disable the entry if both ports was disabled */
2625 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2626 if (ret < 0)
2627 return ret;
2628 }
2629done:
2630 return 0;
2631}
2632
2633static int sh_eth_tsu_purge_all(struct net_device *ndev)
2634{
2635 struct sh_eth_private *mdp = netdev_priv(ndev);
2636 int i, ret;
2637
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002638 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002639 return 0;
2640
2641 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2642 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2643 continue;
2644
2645 /* Disable the entry if both ports was disabled */
2646 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2647 if (ret < 0)
2648 return ret;
2649 }
2650
2651 return 0;
2652}
2653
2654static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2655{
2656 struct sh_eth_private *mdp = netdev_priv(ndev);
2657 u8 addr[ETH_ALEN];
2658 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2659 int i;
2660
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002661 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002662 return;
2663
2664 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2665 sh_eth_tsu_read_entry(reg_offset, addr);
2666 if (is_multicast_ether_addr(addr))
2667 sh_eth_tsu_del_entry(ndev, addr);
2668 }
2669}
2670
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002671/* Update promiscuous flag and multicast filter */
2672static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002673{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002674 struct sh_eth_private *mdp = netdev_priv(ndev);
2675 u32 ecmr_bits;
2676 int mcast_all = 0;
2677 unsigned long flags;
2678
2679 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002680 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002681 * Depending on ndev->flags, set PRM or clear MCT
2682 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002683 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2684 if (mdp->cd->tsu)
2685 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002686
2687 if (!(ndev->flags & IFF_MULTICAST)) {
2688 sh_eth_tsu_purge_mcast(ndev);
2689 mcast_all = 1;
2690 }
2691 if (ndev->flags & IFF_ALLMULTI) {
2692 sh_eth_tsu_purge_mcast(ndev);
2693 ecmr_bits &= ~ECMR_MCT;
2694 mcast_all = 1;
2695 }
2696
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002697 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002698 sh_eth_tsu_purge_all(ndev);
2699 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2700 } else if (mdp->cd->tsu) {
2701 struct netdev_hw_addr *ha;
2702 netdev_for_each_mc_addr(ha, ndev) {
2703 if (mcast_all && is_multicast_ether_addr(ha->addr))
2704 continue;
2705
2706 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2707 if (!mcast_all) {
2708 sh_eth_tsu_purge_mcast(ndev);
2709 ecmr_bits &= ~ECMR_MCT;
2710 mcast_all = 1;
2711 }
2712 }
2713 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002714 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002715
2716 /* update the ethernet mode */
2717 sh_eth_write(ndev, ecmr_bits, ECMR);
2718
2719 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002720}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002721
2722static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2723{
2724 if (!mdp->port)
2725 return TSU_VTAG0;
2726 else
2727 return TSU_VTAG1;
2728}
2729
Patrick McHardy80d5c362013-04-19 02:04:28 +00002730static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2731 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002732{
2733 struct sh_eth_private *mdp = netdev_priv(ndev);
2734 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2735
2736 if (unlikely(!mdp->cd->tsu))
2737 return -EPERM;
2738
2739 /* No filtering if vid = 0 */
2740 if (!vid)
2741 return 0;
2742
2743 mdp->vlan_num_ids++;
2744
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002745 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002746 * already enabled, the driver disables it and the filte
2747 */
2748 if (mdp->vlan_num_ids > 1) {
2749 /* disable VLAN filter */
2750 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2751 return 0;
2752 }
2753
2754 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2755 vtag_reg_index);
2756
2757 return 0;
2758}
2759
Patrick McHardy80d5c362013-04-19 02:04:28 +00002760static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2761 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002762{
2763 struct sh_eth_private *mdp = netdev_priv(ndev);
2764 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2765
2766 if (unlikely(!mdp->cd->tsu))
2767 return -EPERM;
2768
2769 /* No filtering if vid = 0 */
2770 if (!vid)
2771 return 0;
2772
2773 mdp->vlan_num_ids--;
2774 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2775
2776 return 0;
2777}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002778
2779/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002780static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002781{
Simon Hormandb893472014-01-17 09:22:28 +09002782 if (sh_eth_is_rz_fast_ether(mdp)) {
2783 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2784 return;
2785 }
2786
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002787 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2788 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2789 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2790 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2791 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2792 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2793 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2794 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2795 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2796 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002797 if (sh_eth_is_gether(mdp)) {
2798 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2799 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2800 } else {
2801 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2802 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2803 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002804 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2805 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2806 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2807 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2808 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2809 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2810 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002811}
2812
2813/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002814static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002815{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002816 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002817 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002818
2819 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002820 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002821
2822 return 0;
2823}
2824
2825/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002826static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002827 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002828{
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002829 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002830 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002831 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002832 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002833
2834 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002835 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002836 if (!bitbang)
2837 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002838
2839 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002840 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002841 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002842 bitbang->ctrl.ops = &bb_ops;
2843
Stefan Weilc2e07b32010-08-03 19:44:52 +02002844 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002845 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002846 if (!mdp->mii_bus)
2847 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002848
2849 /* Hook up MII support for ethtool */
2850 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002851 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002852 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002853 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002854
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002855 /* register MDIO bus */
2856 if (dev->of_node) {
2857 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002858 } else {
Ben Dooks702eca02014-03-12 17:47:40 +00002859 if (pd->phy_irq > 0)
2860 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2861
2862 ret = mdiobus_register(mdp->mii_bus);
2863 }
2864
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002865 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002866 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002867
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002868 return 0;
2869
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002870out_free_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07002871 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002872 return ret;
2873}
2874
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002875static const u16 *sh_eth_get_register_offset(int register_type)
2876{
2877 const u16 *reg_offset = NULL;
2878
2879 switch (register_type) {
2880 case SH_ETH_REG_GIGABIT:
2881 reg_offset = sh_eth_offset_gigabit;
2882 break;
Simon Hormandb893472014-01-17 09:22:28 +09002883 case SH_ETH_REG_FAST_RZ:
2884 reg_offset = sh_eth_offset_fast_rz;
2885 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002886 case SH_ETH_REG_FAST_RCAR:
2887 reg_offset = sh_eth_offset_fast_rcar;
2888 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002889 case SH_ETH_REG_FAST_SH4:
2890 reg_offset = sh_eth_offset_fast_sh4;
2891 break;
2892 case SH_ETH_REG_FAST_SH3_SH2:
2893 reg_offset = sh_eth_offset_fast_sh3_sh2;
2894 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002895 }
2896
2897 return reg_offset;
2898}
2899
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002900static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002901 .ndo_open = sh_eth_open,
2902 .ndo_stop = sh_eth_close,
2903 .ndo_start_xmit = sh_eth_start_xmit,
2904 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002905 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002906 .ndo_tx_timeout = sh_eth_tx_timeout,
2907 .ndo_do_ioctl = sh_eth_do_ioctl,
2908 .ndo_validate_addr = eth_validate_addr,
2909 .ndo_set_mac_address = eth_mac_addr,
2910 .ndo_change_mtu = eth_change_mtu,
2911};
2912
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002913static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2914 .ndo_open = sh_eth_open,
2915 .ndo_stop = sh_eth_close,
2916 .ndo_start_xmit = sh_eth_start_xmit,
2917 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002918 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002919 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2920 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2921 .ndo_tx_timeout = sh_eth_tx_timeout,
2922 .ndo_do_ioctl = sh_eth_do_ioctl,
2923 .ndo_validate_addr = eth_validate_addr,
2924 .ndo_set_mac_address = eth_mac_addr,
2925 .ndo_change_mtu = eth_change_mtu,
2926};
2927
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002928#ifdef CONFIG_OF
2929static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2930{
2931 struct device_node *np = dev->of_node;
2932 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002933 const char *mac_addr;
2934
2935 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2936 if (!pdata)
2937 return NULL;
2938
2939 pdata->phy_interface = of_get_phy_mode(np);
2940
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002941 mac_addr = of_get_mac_address(np);
2942 if (mac_addr)
2943 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2944
2945 pdata->no_ether_link =
2946 of_property_read_bool(np, "renesas,no-ether-link");
2947 pdata->ether_link_active_low =
2948 of_property_read_bool(np, "renesas,ether-link-active-low");
2949
2950 return pdata;
2951}
2952
2953static const struct of_device_id sh_eth_match_table[] = {
2954 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
2955 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
2956 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
2957 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
2958 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002959 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002960 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002961 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
2962 { }
2963};
2964MODULE_DEVICE_TABLE(of, sh_eth_match_table);
2965#else
2966static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2967{
2968 return NULL;
2969}
2970#endif
2971
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002972static int sh_eth_drv_probe(struct platform_device *pdev)
2973{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002974 struct resource *res;
Jingoo Han0b76b862013-08-30 14:00:11 +09002975 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002976 const struct platform_device_id *id = platform_get_device_id(pdev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03002977 struct sh_eth_private *mdp;
2978 struct net_device *ndev;
2979 int ret, devno;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002980
2981 /* get base addr */
2982 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002983
2984 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01002985 if (!ndev)
2986 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002987
Ben Dooksb5893a02014-03-21 12:09:14 +01002988 pm_runtime_enable(&pdev->dev);
2989 pm_runtime_get_sync(&pdev->dev);
2990
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002991 devno = pdev->id;
2992 if (devno < 0)
2993 devno = 0;
2994
roel kluincc3c0802008-09-10 19:22:44 +02002995 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03002996 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002997 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02002998 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002999
3000 SET_NETDEV_DEV(ndev, &pdev->dev);
3001
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003002 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003003 mdp->num_tx_ring = TX_RING_SIZE;
3004 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003005 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3006 if (IS_ERR(mdp->addr)) {
3007 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003008 goto out_release;
3009 }
3010
Varka Bhadramc9608042014-10-24 07:42:09 +05303011 ndev->base_addr = res->start;
3012
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003013 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003014 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003015
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003016 if (pdev->dev.of_node)
3017 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003018 if (!pd) {
3019 dev_err(&pdev->dev, "no platform data\n");
3020 ret = -EINVAL;
3021 goto out_release;
3022 }
3023
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003024 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003025 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003026 mdp->phy_interface = pd->phy_interface;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003027 mdp->no_ether_link = pd->no_ether_link;
3028 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003029
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003030 /* set cpu data */
Wolfram Sang42a67c92016-03-01 17:37:59 +01003031 if (id)
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003032 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Wolfram Sang42a67c92016-03-01 17:37:59 +01003033 else
3034 mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003035
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003036 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003037 if (!mdp->reg_offset) {
3038 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3039 mdp->cd->register_type);
3040 ret = -EINVAL;
3041 goto out_release;
3042 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003043 sh_eth_set_default_cpu_data(mdp->cd);
3044
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003045 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003046 if (mdp->cd->tsu)
3047 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3048 else
3049 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003050 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003051 ndev->watchdog_timeo = TX_TIMEOUT;
3052
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003053 /* debug message level */
3054 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003055
3056 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003057 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003058 if (!is_valid_ether_addr(ndev->dev_addr)) {
3059 dev_warn(&pdev->dev,
3060 "no valid MAC address supplied, using a random one.\n");
3061 eth_hw_addr_random(ndev);
3062 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003063
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003064 /* ioremap the TSU registers */
3065 if (mdp->cd->tsu) {
3066 struct resource *rtsu;
3067 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003068 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3069 if (IS_ERR(mdp->tsu_addr)) {
3070 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003071 goto out_release;
3072 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00003073 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00003074 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003075 }
3076
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00003077 /* initialize first or needed device */
3078 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003079 if (mdp->cd->chip_reset)
3080 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003081
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003082 if (mdp->cd->tsu) {
3083 /* TSU init (Init only)*/
3084 sh_eth_tsu_init(mdp);
3085 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003086 }
3087
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003088 if (mdp->cd->rmiimode)
3089 sh_eth_write(ndev, 0x1, RMIIMODE);
3090
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003091 /* MDIO bus init */
3092 ret = sh_mdio_init(mdp, pd);
3093 if (ret) {
3094 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3095 goto out_release;
3096 }
3097
Sergei Shtylyov37191092013-06-19 23:30:23 +04003098 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3099
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003100 /* network device register */
3101 ret = register_netdev(ndev);
3102 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003103 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003104
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003105 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003106 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3107 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003108
Ben Dooksb5893a02014-03-21 12:09:14 +01003109 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003110 platform_set_drvdata(pdev, ndev);
3111
3112 return ret;
3113
Sergei Shtylyov37191092013-06-19 23:30:23 +04003114out_napi_del:
3115 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003116 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003117
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003118out_release:
3119 /* net_dev free */
3120 if (ndev)
3121 free_netdev(ndev);
3122
Ben Dooksb5893a02014-03-21 12:09:14 +01003123 pm_runtime_put(&pdev->dev);
3124 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003125 return ret;
3126}
3127
3128static int sh_eth_drv_remove(struct platform_device *pdev)
3129{
3130 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003131 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003132
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003133 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003134 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003135 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003136 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003137 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003138
3139 return 0;
3140}
3141
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003142#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003143#ifdef CONFIG_PM_SLEEP
3144static int sh_eth_suspend(struct device *dev)
3145{
3146 struct net_device *ndev = dev_get_drvdata(dev);
3147 int ret = 0;
3148
3149 if (netif_running(ndev)) {
3150 netif_device_detach(ndev);
3151 ret = sh_eth_close(ndev);
3152 }
3153
3154 return ret;
3155}
3156
3157static int sh_eth_resume(struct device *dev)
3158{
3159 struct net_device *ndev = dev_get_drvdata(dev);
3160 int ret = 0;
3161
3162 if (netif_running(ndev)) {
3163 ret = sh_eth_open(ndev);
3164 if (ret < 0)
3165 return ret;
3166 netif_device_attach(ndev);
3167 }
3168
3169 return ret;
3170}
3171#endif
3172
Magnus Dammbcd51492009-10-09 00:20:04 +00003173static int sh_eth_runtime_nop(struct device *dev)
3174{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003175 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003176 * and ->runtime_resume(). Simply returns success.
3177 *
3178 * This driver re-initializes all registers after
3179 * pm_runtime_get_sync() anyway so there is no need
3180 * to save and restore registers here.
3181 */
3182 return 0;
3183}
3184
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003185static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003186 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003187 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003188};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003189#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3190#else
3191#define SH_ETH_PM_OPS NULL
3192#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003193
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003194static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003195 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003196 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003197 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003198 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003199 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3200 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003201 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003202 { }
3203};
3204MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3205
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003206static struct platform_driver sh_eth_driver = {
3207 .probe = sh_eth_drv_probe,
3208 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003209 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003210 .driver = {
3211 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003212 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003213 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003214 },
3215};
3216
Axel Lindb62f682011-11-27 16:44:17 +00003217module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003218
3219MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3220MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3221MODULE_LICENSE("GPL v2");