blob: 491a558695d3c9ef1a2768c2b5c0301c37ad2229 [file] [log] [blame]
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001/* SuperH Ethernet device driver
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002 *
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003 * Copyright (C) 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyovb356e972014-02-18 03:12:43 +03005 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2014 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00007 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070025#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070026#include <linux/dma-mapping.h>
27#include <linux/etherdevice.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/mdio-bitbang.h>
31#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070036#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000039#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000041#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000042#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000043#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000044#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000045#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070046
47#include "sh_eth.h"
48
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000049#define SH_ETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_LINK | \
51 NETIF_MSG_TIMER | \
52 NETIF_MSG_RX_ERR| \
53 NETIF_MSG_TX_ERR)
54
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000055static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
56 [EDSR] = 0x0000,
57 [EDMR] = 0x0400,
58 [EDTRR] = 0x0408,
59 [EDRRR] = 0x0410,
60 [EESR] = 0x0428,
61 [EESIPR] = 0x0430,
62 [TDLAR] = 0x0010,
63 [TDFAR] = 0x0014,
64 [TDFXR] = 0x0018,
65 [TDFFR] = 0x001c,
66 [RDLAR] = 0x0030,
67 [RDFAR] = 0x0034,
68 [RDFXR] = 0x0038,
69 [RDFFR] = 0x003c,
70 [TRSCER] = 0x0438,
71 [RMFCR] = 0x0440,
72 [TFTR] = 0x0448,
73 [FDR] = 0x0450,
74 [RMCR] = 0x0458,
75 [RPADIR] = 0x0460,
76 [FCFTR] = 0x0468,
77 [CSMR] = 0x04E4,
78
79 [ECMR] = 0x0500,
80 [ECSR] = 0x0510,
81 [ECSIPR] = 0x0518,
82 [PIR] = 0x0520,
83 [PSR] = 0x0528,
84 [PIPR] = 0x052c,
85 [RFLR] = 0x0508,
86 [APR] = 0x0554,
87 [MPR] = 0x0558,
88 [PFTCR] = 0x055c,
89 [PFRCR] = 0x0560,
90 [TPAUSER] = 0x0564,
91 [GECMR] = 0x05b0,
92 [BCULR] = 0x05b4,
93 [MAHR] = 0x05c0,
94 [MALR] = 0x05c8,
95 [TROCR] = 0x0700,
96 [CDCR] = 0x0708,
97 [LCCR] = 0x0710,
98 [CEFCR] = 0x0740,
99 [FRECR] = 0x0748,
100 [TSFRCR] = 0x0750,
101 [TLFRCR] = 0x0758,
102 [RFCR] = 0x0760,
103 [CERCR] = 0x0768,
104 [CEECR] = 0x0770,
105 [MAFCR] = 0x0778,
106 [RMII_MII] = 0x0790,
107
108 [ARSTR] = 0x0000,
109 [TSU_CTRST] = 0x0004,
110 [TSU_FWEN0] = 0x0010,
111 [TSU_FWEN1] = 0x0014,
112 [TSU_FCM] = 0x0018,
113 [TSU_BSYSL0] = 0x0020,
114 [TSU_BSYSL1] = 0x0024,
115 [TSU_PRISL0] = 0x0028,
116 [TSU_PRISL1] = 0x002c,
117 [TSU_FWSL0] = 0x0030,
118 [TSU_FWSL1] = 0x0034,
119 [TSU_FWSLC] = 0x0038,
120 [TSU_QTAG0] = 0x0040,
121 [TSU_QTAG1] = 0x0044,
122 [TSU_FWSR] = 0x0050,
123 [TSU_FWINMK] = 0x0054,
124 [TSU_ADQT0] = 0x0048,
125 [TSU_ADQT1] = 0x004c,
126 [TSU_VTAG0] = 0x0058,
127 [TSU_VTAG1] = 0x005c,
128 [TSU_ADSBSY] = 0x0060,
129 [TSU_TEN] = 0x0064,
130 [TSU_POST1] = 0x0070,
131 [TSU_POST2] = 0x0074,
132 [TSU_POST3] = 0x0078,
133 [TSU_POST4] = 0x007c,
134 [TSU_ADRH0] = 0x0100,
135 [TSU_ADRL0] = 0x0104,
136 [TSU_ADRH31] = 0x01f8,
137 [TSU_ADRL31] = 0x01fc,
138
139 [TXNLCR0] = 0x0080,
140 [TXALCR0] = 0x0084,
141 [RXNLCR0] = 0x0088,
142 [RXALCR0] = 0x008c,
143 [FWNLCR0] = 0x0090,
144 [FWALCR0] = 0x0094,
145 [TXNLCR1] = 0x00a0,
146 [TXALCR1] = 0x00a0,
147 [RXNLCR1] = 0x00a8,
148 [RXALCR1] = 0x00ac,
149 [FWNLCR1] = 0x00b0,
150 [FWALCR1] = 0x00b4,
151};
152
Simon Hormandb893472014-01-17 09:22:28 +0900153static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
154 [EDSR] = 0x0000,
155 [EDMR] = 0x0400,
156 [EDTRR] = 0x0408,
157 [EDRRR] = 0x0410,
158 [EESR] = 0x0428,
159 [EESIPR] = 0x0430,
160 [TDLAR] = 0x0010,
161 [TDFAR] = 0x0014,
162 [TDFXR] = 0x0018,
163 [TDFFR] = 0x001c,
164 [RDLAR] = 0x0030,
165 [RDFAR] = 0x0034,
166 [RDFXR] = 0x0038,
167 [RDFFR] = 0x003c,
168 [TRSCER] = 0x0438,
169 [RMFCR] = 0x0440,
170 [TFTR] = 0x0448,
171 [FDR] = 0x0450,
172 [RMCR] = 0x0458,
173 [RPADIR] = 0x0460,
174 [FCFTR] = 0x0468,
175 [CSMR] = 0x04E4,
176
177 [ECMR] = 0x0500,
178 [RFLR] = 0x0508,
179 [ECSR] = 0x0510,
180 [ECSIPR] = 0x0518,
181 [PIR] = 0x0520,
182 [APR] = 0x0554,
183 [MPR] = 0x0558,
184 [PFTCR] = 0x055c,
185 [PFRCR] = 0x0560,
186 [TPAUSER] = 0x0564,
187 [MAHR] = 0x05c0,
188 [MALR] = 0x05c8,
189 [CEFCR] = 0x0740,
190 [FRECR] = 0x0748,
191 [TSFRCR] = 0x0750,
192 [TLFRCR] = 0x0758,
193 [RFCR] = 0x0760,
194 [MAFCR] = 0x0778,
195
196 [ARSTR] = 0x0000,
197 [TSU_CTRST] = 0x0004,
198 [TSU_VTAG0] = 0x0058,
199 [TSU_ADSBSY] = 0x0060,
200 [TSU_TEN] = 0x0064,
201 [TSU_ADRH0] = 0x0100,
202 [TSU_ADRL0] = 0x0104,
203 [TSU_ADRH31] = 0x01f8,
204 [TSU_ADRL31] = 0x01fc,
205
206 [TXNLCR0] = 0x0080,
207 [TXALCR0] = 0x0084,
208 [RXNLCR0] = 0x0088,
209 [RXALCR0] = 0x008C,
210};
211
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000212static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
213 [ECMR] = 0x0300,
214 [RFLR] = 0x0308,
215 [ECSR] = 0x0310,
216 [ECSIPR] = 0x0318,
217 [PIR] = 0x0320,
218 [PSR] = 0x0328,
219 [RDMLR] = 0x0340,
220 [IPGR] = 0x0350,
221 [APR] = 0x0354,
222 [MPR] = 0x0358,
223 [RFCF] = 0x0360,
224 [TPAUSER] = 0x0364,
225 [TPAUSECR] = 0x0368,
226 [MAHR] = 0x03c0,
227 [MALR] = 0x03c8,
228 [TROCR] = 0x03d0,
229 [CDCR] = 0x03d4,
230 [LCCR] = 0x03d8,
231 [CNDCR] = 0x03dc,
232 [CEFCR] = 0x03e4,
233 [FRECR] = 0x03e8,
234 [TSFRCR] = 0x03ec,
235 [TLFRCR] = 0x03f0,
236 [RFCR] = 0x03f4,
237 [MAFCR] = 0x03f8,
238
239 [EDMR] = 0x0200,
240 [EDTRR] = 0x0208,
241 [EDRRR] = 0x0210,
242 [TDLAR] = 0x0218,
243 [RDLAR] = 0x0220,
244 [EESR] = 0x0228,
245 [EESIPR] = 0x0230,
246 [TRSCER] = 0x0238,
247 [RMFCR] = 0x0240,
248 [TFTR] = 0x0248,
249 [FDR] = 0x0250,
250 [RMCR] = 0x0258,
251 [TFUCR] = 0x0264,
252 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900253 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000254 [FCFTR] = 0x0270,
255 [TRIMD] = 0x027c,
256};
257
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000258static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
259 [ECMR] = 0x0100,
260 [RFLR] = 0x0108,
261 [ECSR] = 0x0110,
262 [ECSIPR] = 0x0118,
263 [PIR] = 0x0120,
264 [PSR] = 0x0128,
265 [RDMLR] = 0x0140,
266 [IPGR] = 0x0150,
267 [APR] = 0x0154,
268 [MPR] = 0x0158,
269 [TPAUSER] = 0x0164,
270 [RFCF] = 0x0160,
271 [TPAUSECR] = 0x0168,
272 [BCFRR] = 0x016c,
273 [MAHR] = 0x01c0,
274 [MALR] = 0x01c8,
275 [TROCR] = 0x01d0,
276 [CDCR] = 0x01d4,
277 [LCCR] = 0x01d8,
278 [CNDCR] = 0x01dc,
279 [CEFCR] = 0x01e4,
280 [FRECR] = 0x01e8,
281 [TSFRCR] = 0x01ec,
282 [TLFRCR] = 0x01f0,
283 [RFCR] = 0x01f4,
284 [MAFCR] = 0x01f8,
285 [RTRATE] = 0x01fc,
286
287 [EDMR] = 0x0000,
288 [EDTRR] = 0x0008,
289 [EDRRR] = 0x0010,
290 [TDLAR] = 0x0018,
291 [RDLAR] = 0x0020,
292 [EESR] = 0x0028,
293 [EESIPR] = 0x0030,
294 [TRSCER] = 0x0038,
295 [RMFCR] = 0x0040,
296 [TFTR] = 0x0048,
297 [FDR] = 0x0050,
298 [RMCR] = 0x0058,
299 [TFUCR] = 0x0064,
300 [RFOCR] = 0x0068,
301 [FCFTR] = 0x0070,
302 [RPADIR] = 0x0078,
303 [TRIMD] = 0x007c,
304 [RBWAR] = 0x00c8,
305 [RDFAR] = 0x00cc,
306 [TBRAR] = 0x00d4,
307 [TDFAR] = 0x00d8,
308};
309
310static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400311 [EDMR] = 0x0000,
312 [EDTRR] = 0x0004,
313 [EDRRR] = 0x0008,
314 [TDLAR] = 0x000c,
315 [RDLAR] = 0x0010,
316 [EESR] = 0x0014,
317 [EESIPR] = 0x0018,
318 [TRSCER] = 0x001c,
319 [RMFCR] = 0x0020,
320 [TFTR] = 0x0024,
321 [FDR] = 0x0028,
322 [RMCR] = 0x002c,
323 [EDOCR] = 0x0030,
324 [FCFTR] = 0x0034,
325 [RPADIR] = 0x0038,
326 [TRIMD] = 0x003c,
327 [RBWAR] = 0x0040,
328 [RDFAR] = 0x0044,
329 [TBRAR] = 0x004c,
330 [TDFAR] = 0x0050,
331
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000332 [ECMR] = 0x0160,
333 [ECSR] = 0x0164,
334 [ECSIPR] = 0x0168,
335 [PIR] = 0x016c,
336 [MAHR] = 0x0170,
337 [MALR] = 0x0174,
338 [RFLR] = 0x0178,
339 [PSR] = 0x017c,
340 [TROCR] = 0x0180,
341 [CDCR] = 0x0184,
342 [LCCR] = 0x0188,
343 [CNDCR] = 0x018c,
344 [CEFCR] = 0x0194,
345 [FRECR] = 0x0198,
346 [TSFRCR] = 0x019c,
347 [TLFRCR] = 0x01a0,
348 [RFCR] = 0x01a4,
349 [MAFCR] = 0x01a8,
350 [IPGR] = 0x01b4,
351 [APR] = 0x01b8,
352 [MPR] = 0x01bc,
353 [TPAUSER] = 0x01c4,
354 [BCFR] = 0x01cc,
355
356 [ARSTR] = 0x0000,
357 [TSU_CTRST] = 0x0004,
358 [TSU_FWEN0] = 0x0010,
359 [TSU_FWEN1] = 0x0014,
360 [TSU_FCM] = 0x0018,
361 [TSU_BSYSL0] = 0x0020,
362 [TSU_BSYSL1] = 0x0024,
363 [TSU_PRISL0] = 0x0028,
364 [TSU_PRISL1] = 0x002c,
365 [TSU_FWSL0] = 0x0030,
366 [TSU_FWSL1] = 0x0034,
367 [TSU_FWSLC] = 0x0038,
368 [TSU_QTAGM0] = 0x0040,
369 [TSU_QTAGM1] = 0x0044,
370 [TSU_ADQT0] = 0x0048,
371 [TSU_ADQT1] = 0x004c,
372 [TSU_FWSR] = 0x0050,
373 [TSU_FWINMK] = 0x0054,
374 [TSU_ADSBSY] = 0x0060,
375 [TSU_TEN] = 0x0064,
376 [TSU_POST1] = 0x0070,
377 [TSU_POST2] = 0x0074,
378 [TSU_POST3] = 0x0078,
379 [TSU_POST4] = 0x007c,
380
381 [TXNLCR0] = 0x0080,
382 [TXALCR0] = 0x0084,
383 [RXNLCR0] = 0x0088,
384 [RXALCR0] = 0x008c,
385 [FWNLCR0] = 0x0090,
386 [FWALCR0] = 0x0094,
387 [TXNLCR1] = 0x00a0,
388 [TXALCR1] = 0x00a0,
389 [RXNLCR1] = 0x00a8,
390 [RXALCR1] = 0x00ac,
391 [FWNLCR1] = 0x00b0,
392 [FWALCR1] = 0x00b4,
393
394 [TSU_ADRH0] = 0x0100,
395 [TSU_ADRL0] = 0x0104,
396 [TSU_ADRL31] = 0x01fc,
397};
398
Simon Horman504c8ca2014-01-17 09:22:27 +0900399static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000400{
Simon Horman504c8ca2014-01-17 09:22:27 +0900401 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000402}
403
Simon Hormandb893472014-01-17 09:22:28 +0900404static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
405{
406 return mdp->reg_offset == sh_eth_offset_fast_rz;
407}
408
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400409static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000410{
411 u32 value = 0x0;
412 struct sh_eth_private *mdp = netdev_priv(ndev);
413
414 switch (mdp->phy_interface) {
415 case PHY_INTERFACE_MODE_GMII:
416 value = 0x2;
417 break;
418 case PHY_INTERFACE_MODE_MII:
419 value = 0x1;
420 break;
421 case PHY_INTERFACE_MODE_RMII:
422 value = 0x0;
423 break;
424 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300425 netdev_warn(ndev,
426 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000427 value = 0x1;
428 break;
429 }
430
431 sh_eth_write(ndev, value, RMII_MII);
432}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000433
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400434static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000435{
436 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000437
438 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000439 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000440 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000441 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000442}
443
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000444/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000445static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000446{
447 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000448
449 switch (mdp->speed) {
450 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000451 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000452 break;
453 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000454 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
455 break;
456 default:
457 break;
458 }
459}
460
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000461/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000462static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000463 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000464 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000465
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400466 .register_type = SH_ETH_REG_FAST_RCAR,
467
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000468 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
469 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
470 .eesipr_value = 0x01ff009f,
471
472 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400473 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
474 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
475 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900476 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000477
478 .apr = 1,
479 .mpr = 1,
480 .tpauser = 1,
481 .hw_swap = 1,
482};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000483
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300484/* R8A7790/1 */
485static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900486 .set_duplex = sh_eth_set_duplex,
487 .set_rate = sh_eth_set_rate_r8a777x,
488
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400489 .register_type = SH_ETH_REG_FAST_RCAR,
490
Simon Hormane18dbf72013-07-23 10:18:05 +0900491 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
492 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
493 .eesipr_value = 0x01ff009f,
494
495 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900496 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
497 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
498 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900499 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900500
501 .apr = 1,
502 .mpr = 1,
503 .tpauser = 1,
504 .hw_swap = 1,
505 .rmiimode = 1,
Kouei Abefd9af072013-08-30 12:41:08 +0900506 .shift_rd0 = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900507};
508
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000509static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000510{
511 struct sh_eth_private *mdp = netdev_priv(ndev);
512
513 switch (mdp->speed) {
514 case 10: /* 10BASE */
515 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
516 break;
517 case 100:/* 100BASE */
518 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000519 break;
520 default:
521 break;
522 }
523}
524
525/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000526static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000527 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000528 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000529
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400530 .register_type = SH_ETH_REG_FAST_SH4,
531
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000532 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
533 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400534 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000535
536 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400537 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
538 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
539 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000540
541 .apr = 1,
542 .mpr = 1,
543 .tpauser = 1,
544 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800545 .rpadir = 1,
546 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000547};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000548
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000549static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000550{
551 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000552
553 switch (mdp->speed) {
554 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000555 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000556 break;
557 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000558 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000559 break;
560 default:
561 break;
562 }
563}
564
565/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000566static struct sh_eth_cpu_data sh7757_data = {
567 .set_duplex = sh_eth_set_duplex,
568 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000569
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400570 .register_type = SH_ETH_REG_FAST_SH4,
571
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000572 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000573
574 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400575 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
576 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
577 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000578
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000579 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000580 .apr = 1,
581 .mpr = 1,
582 .tpauser = 1,
583 .hw_swap = 1,
584 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000585 .rpadir = 1,
586 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000587};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000588
David S. Millere403d292013-06-07 23:40:41 -0700589#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000590#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
591#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
592static void sh_eth_chip_reset_giga(struct net_device *ndev)
593{
594 int i;
595 unsigned long mahr[2], malr[2];
596
597 /* save MAHR and MALR */
598 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000599 malr[i] = ioread32((void *)GIGA_MALR(i));
600 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000601 }
602
603 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000604 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000605 mdelay(1);
606
607 /* restore MAHR and MALR */
608 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000609 iowrite32(malr[i], (void *)GIGA_MALR(i));
610 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000611 }
612}
613
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000614static void sh_eth_set_rate_giga(struct net_device *ndev)
615{
616 struct sh_eth_private *mdp = netdev_priv(ndev);
617
618 switch (mdp->speed) {
619 case 10: /* 10BASE */
620 sh_eth_write(ndev, 0x00000000, GECMR);
621 break;
622 case 100:/* 100BASE */
623 sh_eth_write(ndev, 0x00000010, GECMR);
624 break;
625 case 1000: /* 1000BASE */
626 sh_eth_write(ndev, 0x00000020, GECMR);
627 break;
628 default:
629 break;
630 }
631}
632
633/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000634static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000635 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000636 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000637 .set_rate = sh_eth_set_rate_giga,
638
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400639 .register_type = SH_ETH_REG_GIGABIT,
640
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000641 .ecsr_value = ECSR_ICD | ECSR_MPD,
642 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
643 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
644
645 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400646 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
647 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
648 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000649 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000650
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000651 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000652 .apr = 1,
653 .mpr = 1,
654 .tpauser = 1,
655 .bculr = 1,
656 .hw_swap = 1,
657 .rpadir = 1,
658 .rpadir_value = 2 << 16,
659 .no_trimd = 1,
660 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000661 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000662};
663
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000664static void sh_eth_chip_reset(struct net_device *ndev)
665{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000666 struct sh_eth_private *mdp = netdev_priv(ndev);
667
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000668 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000669 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000670 mdelay(1);
671}
672
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000673static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000674{
675 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000676
677 switch (mdp->speed) {
678 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000679 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000680 break;
681 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000682 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000683 break;
684 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000685 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000686 break;
687 default:
688 break;
689 }
690}
691
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000692/* SH7734 */
693static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000694 .chip_reset = sh_eth_chip_reset,
695 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000696 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000697
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400698 .register_type = SH_ETH_REG_GIGABIT,
699
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000700 .ecsr_value = ECSR_ICD | ECSR_MPD,
701 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
702 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
703
704 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400705 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
706 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
707 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000708
709 .apr = 1,
710 .mpr = 1,
711 .tpauser = 1,
712 .bculr = 1,
713 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000714 .no_trimd = 1,
715 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000716 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000717 .hw_crc = 1,
718 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000719};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000720
721/* SH7763 */
722static struct sh_eth_cpu_data sh7763_data = {
723 .chip_reset = sh_eth_chip_reset,
724 .set_duplex = sh_eth_set_duplex,
725 .set_rate = sh_eth_set_rate_gether,
726
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400727 .register_type = SH_ETH_REG_GIGABIT,
728
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000729 .ecsr_value = ECSR_ICD | ECSR_MPD,
730 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
731 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
732
733 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300734 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
735 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000736 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000737
738 .apr = 1,
739 .mpr = 1,
740 .tpauser = 1,
741 .bculr = 1,
742 .hw_swap = 1,
743 .no_trimd = 1,
744 .no_ade = 1,
745 .tsu = 1,
746 .irq_flags = IRQF_SHARED,
747};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000748
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000749static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000750{
751 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000752
753 /* reset device */
754 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
755 mdelay(1);
756
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000757 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000758}
759
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000760/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000761static struct sh_eth_cpu_data r8a7740_data = {
762 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000763 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000764 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000765
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400766 .register_type = SH_ETH_REG_GIGABIT,
767
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000768 .ecsr_value = ECSR_ICD | ECSR_MPD,
769 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
770 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
771
772 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400773 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
774 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
775 EESR_TDE | EESR_ECI,
Simon Hormancc235282013-10-10 14:51:16 +0900776 .fdr_value = 0x0000070f,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000777
778 .apr = 1,
779 .mpr = 1,
780 .tpauser = 1,
781 .bculr = 1,
782 .hw_swap = 1,
Simon Hormancc235282013-10-10 14:51:16 +0900783 .rpadir = 1,
784 .rpadir_value = 2 << 16,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000785 .no_trimd = 1,
786 .no_ade = 1,
787 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000788 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400789 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000790};
791
Simon Hormandb893472014-01-17 09:22:28 +0900792/* R7S72100 */
793static struct sh_eth_cpu_data r7s72100_data = {
794 .chip_reset = sh_eth_chip_reset,
795 .set_duplex = sh_eth_set_duplex,
796
797 .register_type = SH_ETH_REG_FAST_RZ,
798
799 .ecsr_value = ECSR_ICD,
800 .ecsipr_value = ECSIPR_ICDIP,
801 .eesipr_value = 0xff7f009f,
802
803 .tx_check = EESR_TC1 | EESR_FTC,
804 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
805 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
806 EESR_TDE | EESR_ECI,
807 .fdr_value = 0x0000070f,
Simon Hormandb893472014-01-17 09:22:28 +0900808
809 .no_psr = 1,
810 .apr = 1,
811 .mpr = 1,
812 .tpauser = 1,
813 .hw_swap = 1,
814 .rpadir = 1,
815 .rpadir_value = 2 << 16,
816 .no_trimd = 1,
817 .no_ade = 1,
818 .hw_crc = 1,
819 .tsu = 1,
820 .shift_rd0 = 1,
821};
822
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000823static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400824 .register_type = SH_ETH_REG_FAST_SH3_SH2,
825
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000826 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
827
828 .apr = 1,
829 .mpr = 1,
830 .tpauser = 1,
831 .hw_swap = 1,
832};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000833
834static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400835 .register_type = SH_ETH_REG_FAST_SH3_SH2,
836
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000837 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000838 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000839};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000840
841static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
842{
843 if (!cd->ecsr_value)
844 cd->ecsr_value = DEFAULT_ECSR_INIT;
845
846 if (!cd->ecsipr_value)
847 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
848
849 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300850 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000851 DEFAULT_FIFO_F_D_RFD;
852
853 if (!cd->fdr_value)
854 cd->fdr_value = DEFAULT_FDR_INIT;
855
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000856 if (!cd->tx_check)
857 cd->tx_check = DEFAULT_TX_CHECK;
858
859 if (!cd->eesr_err_check)
860 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000861}
862
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000863static int sh_eth_check_reset(struct net_device *ndev)
864{
865 int ret = 0;
866 int cnt = 100;
867
868 while (cnt > 0) {
869 if (!(sh_eth_read(ndev, EDMR) & 0x3))
870 break;
871 mdelay(1);
872 cnt--;
873 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400874 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300875 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000876 ret = -ETIMEDOUT;
877 }
878 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000879}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000880
881static int sh_eth_reset(struct net_device *ndev)
882{
883 struct sh_eth_private *mdp = netdev_priv(ndev);
884 int ret = 0;
885
Simon Hormandb893472014-01-17 09:22:28 +0900886 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000887 sh_eth_write(ndev, EDSR_ENALL, EDSR);
888 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
889 EDMR);
890
891 ret = sh_eth_check_reset(ndev);
892 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100893 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000894
895 /* Table Init */
896 sh_eth_write(ndev, 0x0, TDLAR);
897 sh_eth_write(ndev, 0x0, TDFAR);
898 sh_eth_write(ndev, 0x0, TDFXR);
899 sh_eth_write(ndev, 0x0, TDFFR);
900 sh_eth_write(ndev, 0x0, RDLAR);
901 sh_eth_write(ndev, 0x0, RDFAR);
902 sh_eth_write(ndev, 0x0, RDFXR);
903 sh_eth_write(ndev, 0x0, RDFFR);
904
905 /* Reset HW CRC register */
906 if (mdp->cd->hw_crc)
907 sh_eth_write(ndev, 0x0, CSMR);
908
909 /* Select MII mode */
910 if (mdp->cd->select_mii)
911 sh_eth_select_mii(ndev);
912 } else {
913 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
914 EDMR);
915 mdelay(3);
916 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
917 EDMR);
918 }
919
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000920 return ret;
921}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000922
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000923static void sh_eth_set_receive_align(struct sk_buff *skb)
924{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900925 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000926
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000927 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900928 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000929}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000930
931
Yoshinori Sato71557a32008-08-06 19:49:00 -0400932/* CPU <-> EDMAC endian convert */
933static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
934{
935 switch (mdp->edmac_endian) {
936 case EDMAC_LITTLE_ENDIAN:
937 return cpu_to_le32(x);
938 case EDMAC_BIG_ENDIAN:
939 return cpu_to_be32(x);
940 }
941 return x;
942}
943
944static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
945{
946 switch (mdp->edmac_endian) {
947 case EDMAC_LITTLE_ENDIAN:
948 return le32_to_cpu(x);
949 case EDMAC_BIG_ENDIAN:
950 return be32_to_cpu(x);
951 }
952 return x;
953}
954
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 {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000977 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
978 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
979 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
980 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
981 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
982 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000983 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700984}
985
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000986static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
987{
Simon Hormandb893472014-01-17 09:22:28 +0900988 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000989 return EDTRR_TRNS_GETHER;
990 else
991 return EDTRR_TRNS_ETHER;
992}
993
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700994struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000995 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700996 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000997 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700998 u32 mmd_msk;/* MMD */
999 u32 mdo_msk;
1000 u32 mdi_msk;
1001 u32 mdc_msk;
1002};
1003
1004/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001005static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001006{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001007 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001008}
1009
1010/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001011static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001012{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001013 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001014}
1015
1016/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001017static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001018{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001019 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020}
1021
1022/* Data I/O pin control */
1023static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1024{
1025 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001026
1027 if (bitbang->set_gate)
1028 bitbang->set_gate(bitbang->addr);
1029
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001030 if (bit)
1031 bb_set(bitbang->addr, bitbang->mmd_msk);
1032 else
1033 bb_clr(bitbang->addr, bitbang->mmd_msk);
1034}
1035
1036/* Set bit data*/
1037static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1038{
1039 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1040
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001041 if (bitbang->set_gate)
1042 bitbang->set_gate(bitbang->addr);
1043
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001044 if (bit)
1045 bb_set(bitbang->addr, bitbang->mdo_msk);
1046 else
1047 bb_clr(bitbang->addr, bitbang->mdo_msk);
1048}
1049
1050/* Get bit data*/
1051static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1052{
1053 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001054
1055 if (bitbang->set_gate)
1056 bitbang->set_gate(bitbang->addr);
1057
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001058 return bb_read(bitbang->addr, bitbang->mdi_msk);
1059}
1060
1061/* MDC pin control */
1062static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1063{
1064 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1065
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001066 if (bitbang->set_gate)
1067 bitbang->set_gate(bitbang->addr);
1068
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001069 if (bit)
1070 bb_set(bitbang->addr, bitbang->mdc_msk);
1071 else
1072 bb_clr(bitbang->addr, bitbang->mdc_msk);
1073}
1074
1075/* mdio bus control struct */
1076static struct mdiobb_ops bb_ops = {
1077 .owner = THIS_MODULE,
1078 .set_mdc = sh_mdc_ctrl,
1079 .set_mdio_dir = sh_mmd_ctrl,
1080 .set_mdio_data = sh_set_mdio,
1081 .get_mdio_data = sh_get_mdio,
1082};
1083
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001084/* free skb and descriptor buffer */
1085static void sh_eth_ring_free(struct net_device *ndev)
1086{
1087 struct sh_eth_private *mdp = netdev_priv(ndev);
1088 int i;
1089
1090 /* Free Rx skb ringbuffer */
1091 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001092 for (i = 0; i < mdp->num_rx_ring; i++)
1093 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001094 }
1095 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001096 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001097
1098 /* Free Tx skb ringbuffer */
1099 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001100 for (i = 0; i < mdp->num_tx_ring; i++)
1101 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001102 }
1103 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001104 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001105}
1106
1107/* format skb and descriptor buffer */
1108static void sh_eth_ring_format(struct net_device *ndev)
1109{
1110 struct sh_eth_private *mdp = netdev_priv(ndev);
1111 int i;
1112 struct sk_buff *skb;
1113 struct sh_eth_rxdesc *rxdesc = NULL;
1114 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001115 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1116 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001117 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001118
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001119 mdp->cur_rx = 0;
1120 mdp->cur_tx = 0;
1121 mdp->dirty_rx = 0;
1122 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001123
1124 memset(mdp->rx_ring, 0, rx_ringsize);
1125
1126 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001127 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001128 /* skb */
1129 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001130 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001131 mdp->rx_skbuff[i] = skb;
1132 if (skb == NULL)
1133 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001134 sh_eth_set_receive_align(skb);
1135
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001136 /* RX descriptor */
1137 rxdesc = &mdp->rx_ring[i];
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001138 /* The size of the buffer is a multiple of 16 bytes. */
1139 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
1140 dma_map_single(&ndev->dev, skb->data, rxdesc->buffer_length,
1141 DMA_FROM_DEVICE);
Mitsuhiro Kimura450fa212014-12-08 19:46:21 +09001142 rxdesc->addr = virt_to_phys(skb->data);
Yoshinori Sato71557a32008-08-06 19:49:00 -04001143 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001145 /* Rx descriptor address set */
1146 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001147 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001148 if (sh_eth_is_gether(mdp) ||
1149 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001150 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001151 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001152 }
1153
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001154 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155
1156 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001157 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001158
1159 memset(mdp->tx_ring, 0, tx_ringsize);
1160
1161 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001162 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001163 mdp->tx_skbuff[i] = NULL;
1164 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001165 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001166 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001167 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001168 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001169 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001170 if (sh_eth_is_gether(mdp) ||
1171 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001172 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001173 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001174 }
1175
Yoshinori Sato71557a32008-08-06 19:49:00 -04001176 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001177}
1178
1179/* Get skb and descriptor buffer */
1180static int sh_eth_ring_init(struct net_device *ndev)
1181{
1182 struct sh_eth_private *mdp = netdev_priv(ndev);
1183 int rx_ringsize, tx_ringsize, ret = 0;
1184
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001185 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001186 * card needs room to do 8 byte alignment, +2 so we can reserve
1187 * the first 2 bytes, and +16 gets room for the status word from the
1188 * card.
1189 */
1190 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1191 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001192 if (mdp->cd->rpadir)
1193 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001194
1195 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001196 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1197 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001198 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001199 ret = -ENOMEM;
1200 return ret;
1201 }
1202
Joe Perchesb2adaca2013-02-03 17:43:58 +00001203 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1204 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001205 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001206 ret = -ENOMEM;
1207 goto skb_ring_free;
1208 }
1209
1210 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001211 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001213 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001215 ret = -ENOMEM;
1216 goto desc_ring_free;
1217 }
1218
1219 mdp->dirty_rx = 0;
1220
1221 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001222 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001223 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001224 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001225 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001226 ret = -ENOMEM;
1227 goto desc_ring_free;
1228 }
1229 return ret;
1230
1231desc_ring_free:
1232 /* free DMA buffer */
1233 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1234
1235skb_ring_free:
1236 /* Free Rx and Tx skb ring buffer */
1237 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001238 mdp->tx_ring = NULL;
1239 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001240
1241 return ret;
1242}
1243
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001244static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1245{
1246 int ringsize;
1247
1248 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001249 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001250 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1251 mdp->rx_desc_dma);
1252 mdp->rx_ring = NULL;
1253 }
1254
1255 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001256 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001257 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1258 mdp->tx_desc_dma);
1259 mdp->tx_ring = NULL;
1260 }
1261}
1262
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001263static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001264{
1265 int ret = 0;
1266 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267 u32 val;
1268
1269 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001270 ret = sh_eth_reset(ndev);
1271 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001272 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001273
Simon Horman55754f12013-07-23 10:18:04 +09001274 if (mdp->cd->rmiimode)
1275 sh_eth_write(ndev, 0x1, RMIIMODE);
1276
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001277 /* Descriptor format */
1278 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001279 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001280 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001281
1282 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001283 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001284
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001285#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001286 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001287 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001288 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001289#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001290 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001291
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001292 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001293 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1294 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001295
Ben Dooks530aa2d2014-06-03 12:21:13 +01001296 /* Frame recv control (enable multiple-packets per rx irq) */
1297 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001298
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001299 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001301 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001302 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001303
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001304 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001305
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001306 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001307 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001308
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001309 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001310 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1311 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001312
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001313 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001314 if (start)
1315 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001316
1317 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001318 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001319 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1320
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001321 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001322
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001323 if (mdp->cd->set_rate)
1324 mdp->cd->set_rate(ndev);
1325
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001326 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001327 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001328
1329 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001330 if (start)
1331 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001332
1333 /* Set MAC address */
1334 update_mac_address(ndev);
1335
1336 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001337 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001338 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001339 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001340 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001341 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001342 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001343
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001344 if (start) {
1345 /* Setting the Rx mode will start the Rx process. */
1346 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001347
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001348 netif_start_queue(ndev);
1349 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001350
1351 return ret;
1352}
1353
1354/* free Tx skb function */
1355static int sh_eth_txfree(struct net_device *ndev)
1356{
1357 struct sh_eth_private *mdp = netdev_priv(ndev);
1358 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001359 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001360 int entry = 0;
1361
1362 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001363 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001364 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001365 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001366 break;
1367 /* Free the original skb. */
1368 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001369 dma_unmap_single(&ndev->dev, txdesc->addr,
1370 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001371 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1372 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001373 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001375 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001376 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001377 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001379 ndev->stats.tx_packets++;
1380 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001382 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001383}
1384
1385/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001386static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001387{
1388 struct sh_eth_private *mdp = netdev_priv(ndev);
1389 struct sh_eth_rxdesc *rxdesc;
1390
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001391 int entry = mdp->cur_rx % mdp->num_rx_ring;
1392 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001393 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394 struct sk_buff *skb;
1395 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001396 u32 desc_status;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001397 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN - 1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001398
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001399 boguscnt = min(boguscnt, *quota);
1400 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001402 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1403 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001404 pkt_len = rxdesc->frame_length;
1405
1406 if (--boguscnt < 0)
1407 break;
1408
1409 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001410 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001411
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001412 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001413 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Simon Hormandb893472014-01-17 09:22:28 +09001414 * bit 0. However, in case of the R8A7740, R8A779x, and
1415 * R7S72100 the RFS bits are from bit 25 to bit 16. So, the
1416 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001417 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001418 if (mdp->cd->shift_rd0)
1419 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001420
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++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001436 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001437 if (!mdp->cd->hw_swap)
1438 sh_eth_soft_swap(
1439 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1440 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001441 skb = mdp->rx_skbuff[entry];
1442 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001443 if (mdp->cd->rpadir)
1444 skb_reserve(skb, NET_IP_ALIGN);
Kouei Abe7db8e0c2013-08-30 12:41:07 +09001445 dma_sync_single_for_cpu(&ndev->dev, rxdesc->addr,
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001446 ALIGN(mdp->rx_buf_sz, 16),
Kouei Abe7db8e0c2013-08-30 12:41:07 +09001447 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;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001453 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001454 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001455 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001456 }
1457
1458 /* Refill the Rx ring buffers. */
1459 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001460 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001461 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001462 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001463 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001464
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001465 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001466 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001467 mdp->rx_skbuff[entry] = skb;
1468 if (skb == NULL)
1469 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001470 sh_eth_set_receive_align(skb);
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001471 dma_map_single(&ndev->dev, skb->data,
1472 rxdesc->buffer_length, DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001473
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001474 skb_checksum_none_assert(skb);
Mitsuhiro Kimura450fa212014-12-08 19:46:21 +09001475 rxdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001476 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001477 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001478 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001479 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 else
1481 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001482 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 }
1484
1485 /* Restart Rx engine if stopped. */
1486 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001487 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001488 /* fix the values for the next receiving if RDE is set */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001489 if (intr_status & EESR_RDE) {
1490 u32 count = (sh_eth_read(ndev, RDFAR) -
1491 sh_eth_read(ndev, RDLAR)) >> 4;
1492
1493 mdp->cur_rx = count;
1494 mdp->dirty_rx = count;
1495 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001496 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001497 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001498
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001499 *quota -= limit - boguscnt - 1;
1500
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001501 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001502}
1503
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001504static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001505{
1506 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001507 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1508 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001509}
1510
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001511static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001512{
1513 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001514 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1515 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001516}
1517
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001518/* error control function */
1519static void sh_eth_error(struct net_device *ndev, int intr_status)
1520{
1521 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001522 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001523 u32 link_stat;
1524 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001525
1526 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001527 felic_stat = sh_eth_read(ndev, ECSR);
1528 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001530 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531 if (felic_stat & ECSR_LCHNG) {
1532 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001533 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001534 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001535 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001536 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001537 if (mdp->ether_link_active_low)
1538 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001539 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001540 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001541 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001542 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001543 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001544 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001545 ~DMAC_M_ECI, EESIPR);
1546 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001547 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001548 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001549 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001550 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001552 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001553 }
1554 }
1555 }
1556
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001557ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001558 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001559 /* Unused write back interrupt */
1560 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001561 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001562 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001563 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001564 }
1565
1566 if (intr_status & EESR_RABT) {
1567 /* Receive Abort int */
1568 if (intr_status & EESR_RFRMER) {
1569 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001570 ndev->stats.rx_frame_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001571 netif_err(mdp, rx_err, ndev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001572 }
1573 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001574
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001575 if (intr_status & EESR_TDE) {
1576 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001577 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001578 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001579 }
1580
1581 if (intr_status & EESR_TFE) {
1582 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001583 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001584 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001585 }
1586
1587 if (intr_status & EESR_RDE) {
1588 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001589 ndev->stats.rx_over_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001590 netif_err(mdp, rx_err, ndev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001591 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001592
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001593 if (intr_status & EESR_RFE) {
1594 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001595 ndev->stats.rx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001596 netif_err(mdp, rx_err, ndev, "Receive FIFO Overflow\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001597 }
1598
1599 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1600 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001601 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001602 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001603 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001604
1605 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1606 if (mdp->cd->no_ade)
1607 mask &= ~EESR_ADE;
1608 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001609 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001610 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001611
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001612 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001613 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1614 intr_status, mdp->cur_tx, mdp->dirty_tx,
1615 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 /* dirty buffer free */
1617 sh_eth_txfree(ndev);
1618
1619 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001620 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001621 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001622 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001623 }
1624 /* wakeup */
1625 netif_wake_queue(ndev);
1626 }
1627}
1628
1629static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1630{
1631 struct net_device *ndev = netdev;
1632 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001633 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001634 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001635 unsigned long intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001636
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001637 spin_lock(&mdp->lock);
1638
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001639 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001640 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001641 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1642 * enabled since it's the one that comes thru regardless of the mask,
1643 * and we need to fully handle it in sh_eth_error() in order to quench
1644 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1645 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001646 intr_enable = sh_eth_read(ndev, EESIPR);
1647 intr_status &= intr_enable | DMAC_M_ECI;
1648 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001649 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001650 else
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001651 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001652
Sergei Shtylyov37191092013-06-19 23:30:23 +04001653 if (intr_status & EESR_RX_CHECK) {
1654 if (napi_schedule_prep(&mdp->napi)) {
1655 /* Mask Rx interrupts */
1656 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1657 EESIPR);
1658 __napi_schedule(&mdp->napi);
1659 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001660 netdev_warn(ndev,
1661 "ignoring interrupt, status 0x%08lx, mask 0x%08lx.\n",
1662 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001663 }
1664 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001665
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001666 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001667 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001668 /* Clear Tx interrupts */
1669 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1670
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001671 sh_eth_txfree(ndev);
1672 netif_wake_queue(ndev);
1673 }
1674
Sergei Shtylyov37191092013-06-19 23:30:23 +04001675 if (intr_status & cd->eesr_err_check) {
1676 /* Clear error interrupts */
1677 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1678
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001679 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001680 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001681
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001682other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 spin_unlock(&mdp->lock);
1684
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001685 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001686}
1687
Sergei Shtylyov37191092013-06-19 23:30:23 +04001688static int sh_eth_poll(struct napi_struct *napi, int budget)
1689{
1690 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1691 napi);
1692 struct net_device *ndev = napi->dev;
1693 int quota = budget;
1694 unsigned long intr_status;
1695
1696 for (;;) {
1697 intr_status = sh_eth_read(ndev, EESR);
1698 if (!(intr_status & EESR_RX_CHECK))
1699 break;
1700 /* Clear Rx interrupts */
1701 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1702
1703 if (sh_eth_rx(ndev, intr_status, &quota))
1704 goto out;
1705 }
1706
1707 napi_complete(napi);
1708
1709 /* Reenable Rx interrupts */
1710 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1711out:
1712 return budget - quota;
1713}
1714
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001715/* PHY state control function */
1716static void sh_eth_adjust_link(struct net_device *ndev)
1717{
1718 struct sh_eth_private *mdp = netdev_priv(ndev);
1719 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001720 int new_state = 0;
1721
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001722 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001723 if (phydev->duplex != mdp->duplex) {
1724 new_state = 1;
1725 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001726 if (mdp->cd->set_duplex)
1727 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001728 }
1729
1730 if (phydev->speed != mdp->speed) {
1731 new_state = 1;
1732 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001733 if (mdp->cd->set_rate)
1734 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001735 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001736 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001737 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001738 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1739 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001740 new_state = 1;
1741 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001742 if (mdp->cd->no_psr || mdp->no_ether_link)
1743 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001744 }
1745 } else if (mdp->link) {
1746 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001747 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001748 mdp->speed = 0;
1749 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001750 if (mdp->cd->no_psr || mdp->no_ether_link)
1751 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001752 }
1753
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001754 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001755 phy_print_status(phydev);
1756}
1757
1758/* PHY init function */
1759static int sh_eth_phy_init(struct net_device *ndev)
1760{
Ben Dooks702eca02014-03-12 17:47:40 +00001761 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001762 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001763 struct phy_device *phydev = NULL;
1764
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001765 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001766 mdp->speed = 0;
1767 mdp->duplex = -1;
1768
1769 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001770 if (np) {
1771 struct device_node *pn;
1772
1773 pn = of_parse_phandle(np, "phy-handle", 0);
1774 phydev = of_phy_connect(ndev, pn,
1775 sh_eth_adjust_link, 0,
1776 mdp->phy_interface);
1777
1778 if (!phydev)
1779 phydev = ERR_PTR(-ENOENT);
1780 } else {
1781 char phy_id[MII_BUS_ID_SIZE + 3];
1782
1783 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1784 mdp->mii_bus->id, mdp->phy_id);
1785
1786 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1787 mdp->phy_interface);
1788 }
1789
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001790 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001791 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001792 return PTR_ERR(phydev);
1793 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001794
Sergei Shtylyovda246852014-03-15 03:29:14 +03001795 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1796 phydev->addr, phydev->irq, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001797
1798 mdp->phydev = phydev;
1799
1800 return 0;
1801}
1802
1803/* PHY control start function */
1804static int sh_eth_phy_start(struct net_device *ndev)
1805{
1806 struct sh_eth_private *mdp = netdev_priv(ndev);
1807 int ret;
1808
1809 ret = sh_eth_phy_init(ndev);
1810 if (ret)
1811 return ret;
1812
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001813 phy_start(mdp->phydev);
1814
1815 return 0;
1816}
1817
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001818static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001819 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001820{
1821 struct sh_eth_private *mdp = netdev_priv(ndev);
1822 unsigned long flags;
1823 int ret;
1824
1825 spin_lock_irqsave(&mdp->lock, flags);
1826 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1827 spin_unlock_irqrestore(&mdp->lock, flags);
1828
1829 return ret;
1830}
1831
1832static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001833 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001834{
1835 struct sh_eth_private *mdp = netdev_priv(ndev);
1836 unsigned long flags;
1837 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001838
1839 spin_lock_irqsave(&mdp->lock, flags);
1840
1841 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001842 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001843
1844 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1845 if (ret)
1846 goto error_exit;
1847
1848 if (ecmd->duplex == DUPLEX_FULL)
1849 mdp->duplex = 1;
1850 else
1851 mdp->duplex = 0;
1852
1853 if (mdp->cd->set_duplex)
1854 mdp->cd->set_duplex(ndev);
1855
1856error_exit:
1857 mdelay(1);
1858
1859 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001860 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001861
1862 spin_unlock_irqrestore(&mdp->lock, flags);
1863
1864 return ret;
1865}
1866
1867static int sh_eth_nway_reset(struct net_device *ndev)
1868{
1869 struct sh_eth_private *mdp = netdev_priv(ndev);
1870 unsigned long flags;
1871 int ret;
1872
1873 spin_lock_irqsave(&mdp->lock, flags);
1874 ret = phy_start_aneg(mdp->phydev);
1875 spin_unlock_irqrestore(&mdp->lock, flags);
1876
1877 return ret;
1878}
1879
1880static u32 sh_eth_get_msglevel(struct net_device *ndev)
1881{
1882 struct sh_eth_private *mdp = netdev_priv(ndev);
1883 return mdp->msg_enable;
1884}
1885
1886static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1887{
1888 struct sh_eth_private *mdp = netdev_priv(ndev);
1889 mdp->msg_enable = value;
1890}
1891
1892static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1893 "rx_current", "tx_current",
1894 "rx_dirty", "tx_dirty",
1895};
1896#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1897
1898static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1899{
1900 switch (sset) {
1901 case ETH_SS_STATS:
1902 return SH_ETH_STATS_LEN;
1903 default:
1904 return -EOPNOTSUPP;
1905 }
1906}
1907
1908static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001909 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001910{
1911 struct sh_eth_private *mdp = netdev_priv(ndev);
1912 int i = 0;
1913
1914 /* device-specific stats */
1915 data[i++] = mdp->cur_rx;
1916 data[i++] = mdp->cur_tx;
1917 data[i++] = mdp->dirty_rx;
1918 data[i++] = mdp->dirty_tx;
1919}
1920
1921static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1922{
1923 switch (stringset) {
1924 case ETH_SS_STATS:
1925 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001926 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001927 break;
1928 }
1929}
1930
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001931static void sh_eth_get_ringparam(struct net_device *ndev,
1932 struct ethtool_ringparam *ring)
1933{
1934 struct sh_eth_private *mdp = netdev_priv(ndev);
1935
1936 ring->rx_max_pending = RX_RING_MAX;
1937 ring->tx_max_pending = TX_RING_MAX;
1938 ring->rx_pending = mdp->num_rx_ring;
1939 ring->tx_pending = mdp->num_tx_ring;
1940}
1941
1942static int sh_eth_set_ringparam(struct net_device *ndev,
1943 struct ethtool_ringparam *ring)
1944{
1945 struct sh_eth_private *mdp = netdev_priv(ndev);
1946 int ret;
1947
1948 if (ring->tx_pending > TX_RING_MAX ||
1949 ring->rx_pending > RX_RING_MAX ||
1950 ring->tx_pending < TX_RING_MIN ||
1951 ring->rx_pending < RX_RING_MIN)
1952 return -EINVAL;
1953 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1954 return -EINVAL;
1955
1956 if (netif_running(ndev)) {
1957 netif_tx_disable(ndev);
1958 /* Disable interrupts by clearing the interrupt mask. */
1959 sh_eth_write(ndev, 0x0000, EESIPR);
1960 /* Stop the chip's Tx and Rx processes. */
1961 sh_eth_write(ndev, 0, EDTRR);
1962 sh_eth_write(ndev, 0, EDRRR);
1963 synchronize_irq(ndev->irq);
1964 }
1965
1966 /* Free all the skbuffs in the Rx queue. */
1967 sh_eth_ring_free(ndev);
1968 /* Free DMA buffer */
1969 sh_eth_free_dma_buffer(mdp);
1970
1971 /* Set new parameters */
1972 mdp->num_rx_ring = ring->rx_pending;
1973 mdp->num_tx_ring = ring->tx_pending;
1974
1975 ret = sh_eth_ring_init(ndev);
1976 if (ret < 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001977 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n", __func__);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001978 return ret;
1979 }
1980 ret = sh_eth_dev_init(ndev, false);
1981 if (ret < 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001982 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n", __func__);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001983 return ret;
1984 }
1985
1986 if (netif_running(ndev)) {
1987 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1988 /* Setting the Rx mode will start the Rx process. */
1989 sh_eth_write(ndev, EDRRR_R, EDRRR);
1990 netif_wake_queue(ndev);
1991 }
1992
1993 return 0;
1994}
1995
stephen hemminger9b07be42012-01-04 12:59:49 +00001996static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001997 .get_settings = sh_eth_get_settings,
1998 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001999 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002000 .get_msglevel = sh_eth_get_msglevel,
2001 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002002 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002003 .get_strings = sh_eth_get_strings,
2004 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2005 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002006 .get_ringparam = sh_eth_get_ringparam,
2007 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002008};
2009
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002010/* network device open function */
2011static int sh_eth_open(struct net_device *ndev)
2012{
2013 int ret = 0;
2014 struct sh_eth_private *mdp = netdev_priv(ndev);
2015
Magnus Dammbcd51492009-10-09 00:20:04 +00002016 pm_runtime_get_sync(&mdp->pdev->dev);
2017
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002018 napi_enable(&mdp->napi);
2019
Joe Perchesa0607fd2009-11-18 23:29:17 -08002020 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002021 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002022 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002023 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002024 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002025 }
2026
2027 /* Descriptor set */
2028 ret = sh_eth_ring_init(ndev);
2029 if (ret)
2030 goto out_free_irq;
2031
2032 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002033 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002034 if (ret)
2035 goto out_free_irq;
2036
2037 /* PHY control start*/
2038 ret = sh_eth_phy_start(ndev);
2039 if (ret)
2040 goto out_free_irq;
2041
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002042 mdp->is_opened = 1;
2043
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002044 return ret;
2045
2046out_free_irq:
2047 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002048out_napi_off:
2049 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002050 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002051 return ret;
2052}
2053
2054/* Timeout function */
2055static void sh_eth_tx_timeout(struct net_device *ndev)
2056{
2057 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002058 struct sh_eth_rxdesc *rxdesc;
2059 int i;
2060
2061 netif_stop_queue(ndev);
2062
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002063 netif_err(mdp, timer, ndev,
2064 "transmit timed out, status %8.8x, resetting...\n",
2065 (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002066
2067 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002068 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002069
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002070 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002071 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002072 rxdesc = &mdp->rx_ring[i];
2073 rxdesc->status = 0;
2074 rxdesc->addr = 0xBADF00D0;
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002075 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002076 mdp->rx_skbuff[i] = NULL;
2077 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002078 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002079 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002080 mdp->tx_skbuff[i] = NULL;
2081 }
2082
2083 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002084 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002085}
2086
2087/* Packet transmit function */
2088static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2089{
2090 struct sh_eth_private *mdp = netdev_priv(ndev);
2091 struct sh_eth_txdesc *txdesc;
2092 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002093 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002094
2095 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002096 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002097 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002098 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002099 netif_stop_queue(ndev);
2100 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002101 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002102 }
2103 }
2104 spin_unlock_irqrestore(&mdp->lock, flags);
2105
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002106 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002107 mdp->tx_skbuff[entry] = skb;
2108 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002109 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002110 if (!mdp->cd->hw_swap)
2111 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2112 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002113 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2114 DMA_TO_DEVICE);
Sergei Shtylyov730c8c62014-02-14 03:05:42 +03002115 if (skb->len < ETH_ZLEN)
2116 txdesc->buffer_length = ETH_ZLEN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002117 else
2118 txdesc->buffer_length = skb->len;
2119
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002120 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002121 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002122 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002123 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002124
2125 mdp->cur_tx++;
2126
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002127 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2128 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002129
Patrick McHardy6ed10652009-06-23 06:03:08 +00002130 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002131}
2132
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002133static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2134{
2135 struct sh_eth_private *mdp = netdev_priv(ndev);
2136
2137 if (sh_eth_is_rz_fast_ether(mdp))
2138 return &ndev->stats;
2139
2140 if (!mdp->is_opened)
2141 return &ndev->stats;
2142
2143 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
2144 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
2145 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
2146 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
2147 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
2148 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
2149
2150 if (sh_eth_is_gether(mdp)) {
2151 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
2152 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
2153 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
2154 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
2155 } else {
2156 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
2157 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2158 }
2159
2160 return &ndev->stats;
2161}
2162
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002163/* device close function */
2164static int sh_eth_close(struct net_device *ndev)
2165{
2166 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002167
2168 netif_stop_queue(ndev);
2169
2170 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002171 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002172
2173 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002174 sh_eth_write(ndev, 0, EDTRR);
2175 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002176
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002177 sh_eth_get_stats(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002178 /* PHY Disconnect */
2179 if (mdp->phydev) {
2180 phy_stop(mdp->phydev);
2181 phy_disconnect(mdp->phydev);
2182 }
2183
2184 free_irq(ndev->irq, ndev);
2185
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002186 napi_disable(&mdp->napi);
2187
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002188 /* Free all the skbuffs in the Rx queue. */
2189 sh_eth_ring_free(ndev);
2190
2191 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00002192 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002193
Magnus Dammbcd51492009-10-09 00:20:04 +00002194 pm_runtime_put_sync(&mdp->pdev->dev);
2195
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002196 mdp->is_opened = 0;
2197
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002198 return 0;
2199}
2200
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002201/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002202static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002203{
2204 struct sh_eth_private *mdp = netdev_priv(ndev);
2205 struct phy_device *phydev = mdp->phydev;
2206
2207 if (!netif_running(ndev))
2208 return -EINVAL;
2209
2210 if (!phydev)
2211 return -ENODEV;
2212
Richard Cochran28b04112010-07-17 08:48:55 +00002213 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002214}
2215
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002216/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2217static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2218 int entry)
2219{
2220 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2221}
2222
2223static u32 sh_eth_tsu_get_post_mask(int entry)
2224{
2225 return 0x0f << (28 - ((entry % 8) * 4));
2226}
2227
2228static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2229{
2230 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2231}
2232
2233static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2234 int entry)
2235{
2236 struct sh_eth_private *mdp = netdev_priv(ndev);
2237 u32 tmp;
2238 void *reg_offset;
2239
2240 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2241 tmp = ioread32(reg_offset);
2242 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2243}
2244
2245static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2246 int entry)
2247{
2248 struct sh_eth_private *mdp = netdev_priv(ndev);
2249 u32 post_mask, ref_mask, tmp;
2250 void *reg_offset;
2251
2252 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2253 post_mask = sh_eth_tsu_get_post_mask(entry);
2254 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2255
2256 tmp = ioread32(reg_offset);
2257 iowrite32(tmp & ~post_mask, reg_offset);
2258
2259 /* If other port enables, the function returns "true" */
2260 return tmp & ref_mask;
2261}
2262
2263static int sh_eth_tsu_busy(struct net_device *ndev)
2264{
2265 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2266 struct sh_eth_private *mdp = netdev_priv(ndev);
2267
2268 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2269 udelay(10);
2270 timeout--;
2271 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002272 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002273 return -ETIMEDOUT;
2274 }
2275 }
2276
2277 return 0;
2278}
2279
2280static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2281 const u8 *addr)
2282{
2283 u32 val;
2284
2285 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2286 iowrite32(val, reg);
2287 if (sh_eth_tsu_busy(ndev) < 0)
2288 return -EBUSY;
2289
2290 val = addr[4] << 8 | addr[5];
2291 iowrite32(val, reg + 4);
2292 if (sh_eth_tsu_busy(ndev) < 0)
2293 return -EBUSY;
2294
2295 return 0;
2296}
2297
2298static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2299{
2300 u32 val;
2301
2302 val = ioread32(reg);
2303 addr[0] = (val >> 24) & 0xff;
2304 addr[1] = (val >> 16) & 0xff;
2305 addr[2] = (val >> 8) & 0xff;
2306 addr[3] = val & 0xff;
2307 val = ioread32(reg + 4);
2308 addr[4] = (val >> 8) & 0xff;
2309 addr[5] = val & 0xff;
2310}
2311
2312
2313static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2314{
2315 struct sh_eth_private *mdp = netdev_priv(ndev);
2316 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2317 int i;
2318 u8 c_addr[ETH_ALEN];
2319
2320 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2321 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002322 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002323 return i;
2324 }
2325
2326 return -ENOENT;
2327}
2328
2329static int sh_eth_tsu_find_empty(struct net_device *ndev)
2330{
2331 u8 blank[ETH_ALEN];
2332 int entry;
2333
2334 memset(blank, 0, sizeof(blank));
2335 entry = sh_eth_tsu_find_entry(ndev, blank);
2336 return (entry < 0) ? -ENOMEM : entry;
2337}
2338
2339static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2340 int entry)
2341{
2342 struct sh_eth_private *mdp = netdev_priv(ndev);
2343 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2344 int ret;
2345 u8 blank[ETH_ALEN];
2346
2347 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2348 ~(1 << (31 - entry)), TSU_TEN);
2349
2350 memset(blank, 0, sizeof(blank));
2351 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2352 if (ret < 0)
2353 return ret;
2354 return 0;
2355}
2356
2357static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2358{
2359 struct sh_eth_private *mdp = netdev_priv(ndev);
2360 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2361 int i, ret;
2362
2363 if (!mdp->cd->tsu)
2364 return 0;
2365
2366 i = sh_eth_tsu_find_entry(ndev, addr);
2367 if (i < 0) {
2368 /* No entry found, create one */
2369 i = sh_eth_tsu_find_empty(ndev);
2370 if (i < 0)
2371 return -ENOMEM;
2372 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2373 if (ret < 0)
2374 return ret;
2375
2376 /* Enable the entry */
2377 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2378 (1 << (31 - i)), TSU_TEN);
2379 }
2380
2381 /* Entry found or created, enable POST */
2382 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2383
2384 return 0;
2385}
2386
2387static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2388{
2389 struct sh_eth_private *mdp = netdev_priv(ndev);
2390 int i, ret;
2391
2392 if (!mdp->cd->tsu)
2393 return 0;
2394
2395 i = sh_eth_tsu_find_entry(ndev, addr);
2396 if (i) {
2397 /* Entry found */
2398 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2399 goto done;
2400
2401 /* Disable the entry if both ports was disabled */
2402 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2403 if (ret < 0)
2404 return ret;
2405 }
2406done:
2407 return 0;
2408}
2409
2410static int sh_eth_tsu_purge_all(struct net_device *ndev)
2411{
2412 struct sh_eth_private *mdp = netdev_priv(ndev);
2413 int i, ret;
2414
2415 if (unlikely(!mdp->cd->tsu))
2416 return 0;
2417
2418 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2419 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2420 continue;
2421
2422 /* Disable the entry if both ports was disabled */
2423 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2424 if (ret < 0)
2425 return ret;
2426 }
2427
2428 return 0;
2429}
2430
2431static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2432{
2433 struct sh_eth_private *mdp = netdev_priv(ndev);
2434 u8 addr[ETH_ALEN];
2435 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2436 int i;
2437
2438 if (unlikely(!mdp->cd->tsu))
2439 return;
2440
2441 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2442 sh_eth_tsu_read_entry(reg_offset, addr);
2443 if (is_multicast_ether_addr(addr))
2444 sh_eth_tsu_del_entry(ndev, addr);
2445 }
2446}
2447
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002448/* Multicast reception directions set */
2449static void sh_eth_set_multicast_list(struct net_device *ndev)
2450{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002451 struct sh_eth_private *mdp = netdev_priv(ndev);
2452 u32 ecmr_bits;
2453 int mcast_all = 0;
2454 unsigned long flags;
2455
2456 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002457 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002458 * Depending on ndev->flags, set PRM or clear MCT
2459 */
2460 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2461
2462 if (!(ndev->flags & IFF_MULTICAST)) {
2463 sh_eth_tsu_purge_mcast(ndev);
2464 mcast_all = 1;
2465 }
2466 if (ndev->flags & IFF_ALLMULTI) {
2467 sh_eth_tsu_purge_mcast(ndev);
2468 ecmr_bits &= ~ECMR_MCT;
2469 mcast_all = 1;
2470 }
2471
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002472 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002473 sh_eth_tsu_purge_all(ndev);
2474 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2475 } else if (mdp->cd->tsu) {
2476 struct netdev_hw_addr *ha;
2477 netdev_for_each_mc_addr(ha, ndev) {
2478 if (mcast_all && is_multicast_ether_addr(ha->addr))
2479 continue;
2480
2481 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2482 if (!mcast_all) {
2483 sh_eth_tsu_purge_mcast(ndev);
2484 ecmr_bits &= ~ECMR_MCT;
2485 mcast_all = 1;
2486 }
2487 }
2488 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002489 } else {
2490 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002491 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002492 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002493
2494 /* update the ethernet mode */
2495 sh_eth_write(ndev, ecmr_bits, ECMR);
2496
2497 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002498}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002499
2500static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2501{
2502 if (!mdp->port)
2503 return TSU_VTAG0;
2504 else
2505 return TSU_VTAG1;
2506}
2507
Patrick McHardy80d5c362013-04-19 02:04:28 +00002508static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2509 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002510{
2511 struct sh_eth_private *mdp = netdev_priv(ndev);
2512 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2513
2514 if (unlikely(!mdp->cd->tsu))
2515 return -EPERM;
2516
2517 /* No filtering if vid = 0 */
2518 if (!vid)
2519 return 0;
2520
2521 mdp->vlan_num_ids++;
2522
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002523 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002524 * already enabled, the driver disables it and the filte
2525 */
2526 if (mdp->vlan_num_ids > 1) {
2527 /* disable VLAN filter */
2528 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2529 return 0;
2530 }
2531
2532 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2533 vtag_reg_index);
2534
2535 return 0;
2536}
2537
Patrick McHardy80d5c362013-04-19 02:04:28 +00002538static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2539 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002540{
2541 struct sh_eth_private *mdp = netdev_priv(ndev);
2542 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2543
2544 if (unlikely(!mdp->cd->tsu))
2545 return -EPERM;
2546
2547 /* No filtering if vid = 0 */
2548 if (!vid)
2549 return 0;
2550
2551 mdp->vlan_num_ids--;
2552 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2553
2554 return 0;
2555}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002556
2557/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002558static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002559{
Simon Hormandb893472014-01-17 09:22:28 +09002560 if (sh_eth_is_rz_fast_ether(mdp)) {
2561 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2562 return;
2563 }
2564
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002565 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2566 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2567 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2568 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2569 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2570 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2571 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2572 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2573 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2574 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002575 if (sh_eth_is_gether(mdp)) {
2576 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2577 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2578 } else {
2579 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2580 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2581 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002582 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2583 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2584 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2585 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2586 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2587 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2588 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002589}
2590
2591/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002592static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002593{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002594 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002595 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002596
2597 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002598 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002599
2600 return 0;
2601}
2602
2603/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002604static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002605 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002606{
2607 int ret, i;
2608 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002609 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002610 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002611
2612 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002613 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002614 if (!bitbang)
2615 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002616
2617 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002618 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002619 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002620 bitbang->mdi_msk = PIR_MDI;
2621 bitbang->mdo_msk = PIR_MDO;
2622 bitbang->mmd_msk = PIR_MMD;
2623 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002624 bitbang->ctrl.ops = &bb_ops;
2625
Stefan Weilc2e07b32010-08-03 19:44:52 +02002626 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002627 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002628 if (!mdp->mii_bus)
2629 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002630
2631 /* Hook up MII support for ethtool */
2632 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002633 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002634 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002635 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002636
2637 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002638 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2639 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002640 if (!mdp->mii_bus->irq) {
2641 ret = -ENOMEM;
2642 goto out_free_bus;
2643 }
2644
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002645 /* register MDIO bus */
2646 if (dev->of_node) {
2647 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002648 } else {
2649 for (i = 0; i < PHY_MAX_ADDR; i++)
2650 mdp->mii_bus->irq[i] = PHY_POLL;
2651 if (pd->phy_irq > 0)
2652 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2653
2654 ret = mdiobus_register(mdp->mii_bus);
2655 }
2656
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002657 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002658 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002659
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002660 return 0;
2661
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002662out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002663 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002664 return ret;
2665}
2666
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002667static const u16 *sh_eth_get_register_offset(int register_type)
2668{
2669 const u16 *reg_offset = NULL;
2670
2671 switch (register_type) {
2672 case SH_ETH_REG_GIGABIT:
2673 reg_offset = sh_eth_offset_gigabit;
2674 break;
Simon Hormandb893472014-01-17 09:22:28 +09002675 case SH_ETH_REG_FAST_RZ:
2676 reg_offset = sh_eth_offset_fast_rz;
2677 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002678 case SH_ETH_REG_FAST_RCAR:
2679 reg_offset = sh_eth_offset_fast_rcar;
2680 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002681 case SH_ETH_REG_FAST_SH4:
2682 reg_offset = sh_eth_offset_fast_sh4;
2683 break;
2684 case SH_ETH_REG_FAST_SH3_SH2:
2685 reg_offset = sh_eth_offset_fast_sh3_sh2;
2686 break;
2687 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002688 break;
2689 }
2690
2691 return reg_offset;
2692}
2693
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002694static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002695 .ndo_open = sh_eth_open,
2696 .ndo_stop = sh_eth_close,
2697 .ndo_start_xmit = sh_eth_start_xmit,
2698 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002699 .ndo_tx_timeout = sh_eth_tx_timeout,
2700 .ndo_do_ioctl = sh_eth_do_ioctl,
2701 .ndo_validate_addr = eth_validate_addr,
2702 .ndo_set_mac_address = eth_mac_addr,
2703 .ndo_change_mtu = eth_change_mtu,
2704};
2705
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002706static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2707 .ndo_open = sh_eth_open,
2708 .ndo_stop = sh_eth_close,
2709 .ndo_start_xmit = sh_eth_start_xmit,
2710 .ndo_get_stats = sh_eth_get_stats,
2711 .ndo_set_rx_mode = sh_eth_set_multicast_list,
2712 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2713 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2714 .ndo_tx_timeout = sh_eth_tx_timeout,
2715 .ndo_do_ioctl = sh_eth_do_ioctl,
2716 .ndo_validate_addr = eth_validate_addr,
2717 .ndo_set_mac_address = eth_mac_addr,
2718 .ndo_change_mtu = eth_change_mtu,
2719};
2720
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002721#ifdef CONFIG_OF
2722static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2723{
2724 struct device_node *np = dev->of_node;
2725 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002726 const char *mac_addr;
2727
2728 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2729 if (!pdata)
2730 return NULL;
2731
2732 pdata->phy_interface = of_get_phy_mode(np);
2733
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002734 mac_addr = of_get_mac_address(np);
2735 if (mac_addr)
2736 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2737
2738 pdata->no_ether_link =
2739 of_property_read_bool(np, "renesas,no-ether-link");
2740 pdata->ether_link_active_low =
2741 of_property_read_bool(np, "renesas,ether-link-active-low");
2742
2743 return pdata;
2744}
2745
2746static const struct of_device_id sh_eth_match_table[] = {
2747 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
2748 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
2749 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
2750 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
2751 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002752 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002753 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002754 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
2755 { }
2756};
2757MODULE_DEVICE_TABLE(of, sh_eth_match_table);
2758#else
2759static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2760{
2761 return NULL;
2762}
2763#endif
2764
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002765static int sh_eth_drv_probe(struct platform_device *pdev)
2766{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002767 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002768 struct resource *res;
2769 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002770 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09002771 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002772 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002773
2774 /* get base addr */
2775 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002776
2777 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01002778 if (!ndev)
2779 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002780
Ben Dooksb5893a02014-03-21 12:09:14 +01002781 pm_runtime_enable(&pdev->dev);
2782 pm_runtime_get_sync(&pdev->dev);
2783
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002784 devno = pdev->id;
2785 if (devno < 0)
2786 devno = 0;
2787
2788 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002789 ret = platform_get_irq(pdev, 0);
2790 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002791 ret = -ENODEV;
2792 goto out_release;
2793 }
roel kluincc3c0802008-09-10 19:22:44 +02002794 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002795
2796 SET_NETDEV_DEV(ndev, &pdev->dev);
2797
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002798 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002799 mdp->num_tx_ring = TX_RING_SIZE;
2800 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002801 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2802 if (IS_ERR(mdp->addr)) {
2803 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002804 goto out_release;
2805 }
2806
Varka Bhadramc9608042014-10-24 07:42:09 +05302807 ndev->base_addr = res->start;
2808
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002809 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002810 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002811
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002812 if (pdev->dev.of_node)
2813 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03002814 if (!pd) {
2815 dev_err(&pdev->dev, "no platform data\n");
2816 ret = -EINVAL;
2817 goto out_release;
2818 }
2819
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002820 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002821 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002822 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002823 /* EDMAC endian */
2824 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002825 mdp->no_ether_link = pd->no_ether_link;
2826 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002827
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002828 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002829 if (id) {
2830 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
2831 } else {
2832 const struct of_device_id *match;
2833
2834 match = of_match_device(of_match_ptr(sh_eth_match_table),
2835 &pdev->dev);
2836 mdp->cd = (struct sh_eth_cpu_data *)match->data;
2837 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04002838 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03002839 if (!mdp->reg_offset) {
2840 dev_err(&pdev->dev, "Unknown register type (%d)\n",
2841 mdp->cd->register_type);
2842 ret = -EINVAL;
2843 goto out_release;
2844 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002845 sh_eth_set_default_cpu_data(mdp->cd);
2846
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002847 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002848 if (mdp->cd->tsu)
2849 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
2850 else
2851 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002852 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002853 ndev->watchdog_timeo = TX_TIMEOUT;
2854
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002855 /* debug message level */
2856 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002857
2858 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002859 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002860 if (!is_valid_ether_addr(ndev->dev_addr)) {
2861 dev_warn(&pdev->dev,
2862 "no valid MAC address supplied, using a random one.\n");
2863 eth_hw_addr_random(ndev);
2864 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002865
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002866 /* ioremap the TSU registers */
2867 if (mdp->cd->tsu) {
2868 struct resource *rtsu;
2869 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002870 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2871 if (IS_ERR(mdp->tsu_addr)) {
2872 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002873 goto out_release;
2874 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002875 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002876 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002877 }
2878
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002879 /* initialize first or needed device */
2880 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002881 if (mdp->cd->chip_reset)
2882 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002883
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002884 if (mdp->cd->tsu) {
2885 /* TSU init (Init only)*/
2886 sh_eth_tsu_init(mdp);
2887 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002888 }
2889
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09002890 if (mdp->cd->rmiimode)
2891 sh_eth_write(ndev, 0x1, RMIIMODE);
2892
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002893 /* MDIO bus init */
2894 ret = sh_mdio_init(mdp, pd);
2895 if (ret) {
2896 dev_err(&ndev->dev, "failed to initialise MDIO\n");
2897 goto out_release;
2898 }
2899
Sergei Shtylyov37191092013-06-19 23:30:23 +04002900 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
2901
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002902 /* network device register */
2903 ret = register_netdev(ndev);
2904 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04002905 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002906
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002907 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03002908 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
2909 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002910
Ben Dooksb5893a02014-03-21 12:09:14 +01002911 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002912 platform_set_drvdata(pdev, ndev);
2913
2914 return ret;
2915
Sergei Shtylyov37191092013-06-19 23:30:23 +04002916out_napi_del:
2917 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002918 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002919
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002920out_release:
2921 /* net_dev free */
2922 if (ndev)
2923 free_netdev(ndev);
2924
Ben Dooksb5893a02014-03-21 12:09:14 +01002925 pm_runtime_put(&pdev->dev);
2926 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002927 return ret;
2928}
2929
2930static int sh_eth_drv_remove(struct platform_device *pdev)
2931{
2932 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002933 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002934
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002935 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002936 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01002937 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00002938 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002939 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002940
2941 return 0;
2942}
2943
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002944#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002945static int sh_eth_runtime_nop(struct device *dev)
2946{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002947 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00002948 * and ->runtime_resume(). Simply returns success.
2949 *
2950 * This driver re-initializes all registers after
2951 * pm_runtime_get_sync() anyway so there is no need
2952 * to save and restore registers here.
2953 */
2954 return 0;
2955}
2956
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002957static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002958 .runtime_suspend = sh_eth_runtime_nop,
2959 .runtime_resume = sh_eth_runtime_nop,
2960};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002961#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2962#else
2963#define SH_ETH_PM_OPS NULL
2964#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00002965
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002966static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00002967 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00002968 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00002969 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002970 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00002971 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
2972 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002973 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Simon Hormandb893472014-01-17 09:22:28 +09002974 { "r7s72100-ether", (kernel_ulong_t)&r7s72100_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00002975 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00002976 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyov94a12b12013-12-08 02:59:18 +03002977 { "r8a7790-ether", (kernel_ulong_t)&r8a779x_data },
2978 { "r8a7791-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002979 { "r8a7793-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002980 { "r8a7794-ether", (kernel_ulong_t)&r8a779x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002981 { }
2982};
2983MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
2984
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002985static struct platform_driver sh_eth_driver = {
2986 .probe = sh_eth_drv_probe,
2987 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002988 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002989 .driver = {
2990 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002991 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002992 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002993 },
2994};
2995
Axel Lindb62f682011-11-27 16:44:17 +00002996module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002997
2998MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2999MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3000MODULE_LICENSE("GPL v2");