blob: 3426d32a1803c7fc7c19fe9249737e009aefbfda [file] [log] [blame]
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001/*
2 * SuperH Ethernet device driver
3 *
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00005 * Copyright (C) 2008-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
22 */
23
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070024#include <linux/init.h>
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070028#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070029#include <linux/dma-mapping.h>
30#include <linux/etherdevice.h>
31#include <linux/delay.h>
32#include <linux/platform_device.h>
33#include <linux/mdio-bitbang.h>
34#include <linux/netdevice.h>
35#include <linux/phy.h>
36#include <linux/cache.h>
37#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000038#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000041#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000042#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000043#include <linux/sh_eth.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070044
45#include "sh_eth.h"
46
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000047#define SH_ETH_DEF_MSG_ENABLE \
48 (NETIF_MSG_LINK | \
49 NETIF_MSG_TIMER | \
50 NETIF_MSG_RX_ERR| \
51 NETIF_MSG_TX_ERR)
52
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000053static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
54 [EDSR] = 0x0000,
55 [EDMR] = 0x0400,
56 [EDTRR] = 0x0408,
57 [EDRRR] = 0x0410,
58 [EESR] = 0x0428,
59 [EESIPR] = 0x0430,
60 [TDLAR] = 0x0010,
61 [TDFAR] = 0x0014,
62 [TDFXR] = 0x0018,
63 [TDFFR] = 0x001c,
64 [RDLAR] = 0x0030,
65 [RDFAR] = 0x0034,
66 [RDFXR] = 0x0038,
67 [RDFFR] = 0x003c,
68 [TRSCER] = 0x0438,
69 [RMFCR] = 0x0440,
70 [TFTR] = 0x0448,
71 [FDR] = 0x0450,
72 [RMCR] = 0x0458,
73 [RPADIR] = 0x0460,
74 [FCFTR] = 0x0468,
75 [CSMR] = 0x04E4,
76
77 [ECMR] = 0x0500,
78 [ECSR] = 0x0510,
79 [ECSIPR] = 0x0518,
80 [PIR] = 0x0520,
81 [PSR] = 0x0528,
82 [PIPR] = 0x052c,
83 [RFLR] = 0x0508,
84 [APR] = 0x0554,
85 [MPR] = 0x0558,
86 [PFTCR] = 0x055c,
87 [PFRCR] = 0x0560,
88 [TPAUSER] = 0x0564,
89 [GECMR] = 0x05b0,
90 [BCULR] = 0x05b4,
91 [MAHR] = 0x05c0,
92 [MALR] = 0x05c8,
93 [TROCR] = 0x0700,
94 [CDCR] = 0x0708,
95 [LCCR] = 0x0710,
96 [CEFCR] = 0x0740,
97 [FRECR] = 0x0748,
98 [TSFRCR] = 0x0750,
99 [TLFRCR] = 0x0758,
100 [RFCR] = 0x0760,
101 [CERCR] = 0x0768,
102 [CEECR] = 0x0770,
103 [MAFCR] = 0x0778,
104 [RMII_MII] = 0x0790,
105
106 [ARSTR] = 0x0000,
107 [TSU_CTRST] = 0x0004,
108 [TSU_FWEN0] = 0x0010,
109 [TSU_FWEN1] = 0x0014,
110 [TSU_FCM] = 0x0018,
111 [TSU_BSYSL0] = 0x0020,
112 [TSU_BSYSL1] = 0x0024,
113 [TSU_PRISL0] = 0x0028,
114 [TSU_PRISL1] = 0x002c,
115 [TSU_FWSL0] = 0x0030,
116 [TSU_FWSL1] = 0x0034,
117 [TSU_FWSLC] = 0x0038,
118 [TSU_QTAG0] = 0x0040,
119 [TSU_QTAG1] = 0x0044,
120 [TSU_FWSR] = 0x0050,
121 [TSU_FWINMK] = 0x0054,
122 [TSU_ADQT0] = 0x0048,
123 [TSU_ADQT1] = 0x004c,
124 [TSU_VTAG0] = 0x0058,
125 [TSU_VTAG1] = 0x005c,
126 [TSU_ADSBSY] = 0x0060,
127 [TSU_TEN] = 0x0064,
128 [TSU_POST1] = 0x0070,
129 [TSU_POST2] = 0x0074,
130 [TSU_POST3] = 0x0078,
131 [TSU_POST4] = 0x007c,
132 [TSU_ADRH0] = 0x0100,
133 [TSU_ADRL0] = 0x0104,
134 [TSU_ADRH31] = 0x01f8,
135 [TSU_ADRL31] = 0x01fc,
136
137 [TXNLCR0] = 0x0080,
138 [TXALCR0] = 0x0084,
139 [RXNLCR0] = 0x0088,
140 [RXALCR0] = 0x008c,
141 [FWNLCR0] = 0x0090,
142 [FWALCR0] = 0x0094,
143 [TXNLCR1] = 0x00a0,
144 [TXALCR1] = 0x00a0,
145 [RXNLCR1] = 0x00a8,
146 [RXALCR1] = 0x00ac,
147 [FWNLCR1] = 0x00b0,
148 [FWALCR1] = 0x00b4,
149};
150
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000151static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
152 [ECMR] = 0x0300,
153 [RFLR] = 0x0308,
154 [ECSR] = 0x0310,
155 [ECSIPR] = 0x0318,
156 [PIR] = 0x0320,
157 [PSR] = 0x0328,
158 [RDMLR] = 0x0340,
159 [IPGR] = 0x0350,
160 [APR] = 0x0354,
161 [MPR] = 0x0358,
162 [RFCF] = 0x0360,
163 [TPAUSER] = 0x0364,
164 [TPAUSECR] = 0x0368,
165 [MAHR] = 0x03c0,
166 [MALR] = 0x03c8,
167 [TROCR] = 0x03d0,
168 [CDCR] = 0x03d4,
169 [LCCR] = 0x03d8,
170 [CNDCR] = 0x03dc,
171 [CEFCR] = 0x03e4,
172 [FRECR] = 0x03e8,
173 [TSFRCR] = 0x03ec,
174 [TLFRCR] = 0x03f0,
175 [RFCR] = 0x03f4,
176 [MAFCR] = 0x03f8,
177
178 [EDMR] = 0x0200,
179 [EDTRR] = 0x0208,
180 [EDRRR] = 0x0210,
181 [TDLAR] = 0x0218,
182 [RDLAR] = 0x0220,
183 [EESR] = 0x0228,
184 [EESIPR] = 0x0230,
185 [TRSCER] = 0x0238,
186 [RMFCR] = 0x0240,
187 [TFTR] = 0x0248,
188 [FDR] = 0x0250,
189 [RMCR] = 0x0258,
190 [TFUCR] = 0x0264,
191 [RFOCR] = 0x0268,
192 [FCFTR] = 0x0270,
193 [TRIMD] = 0x027c,
194};
195
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000196static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
197 [ECMR] = 0x0100,
198 [RFLR] = 0x0108,
199 [ECSR] = 0x0110,
200 [ECSIPR] = 0x0118,
201 [PIR] = 0x0120,
202 [PSR] = 0x0128,
203 [RDMLR] = 0x0140,
204 [IPGR] = 0x0150,
205 [APR] = 0x0154,
206 [MPR] = 0x0158,
207 [TPAUSER] = 0x0164,
208 [RFCF] = 0x0160,
209 [TPAUSECR] = 0x0168,
210 [BCFRR] = 0x016c,
211 [MAHR] = 0x01c0,
212 [MALR] = 0x01c8,
213 [TROCR] = 0x01d0,
214 [CDCR] = 0x01d4,
215 [LCCR] = 0x01d8,
216 [CNDCR] = 0x01dc,
217 [CEFCR] = 0x01e4,
218 [FRECR] = 0x01e8,
219 [TSFRCR] = 0x01ec,
220 [TLFRCR] = 0x01f0,
221 [RFCR] = 0x01f4,
222 [MAFCR] = 0x01f8,
223 [RTRATE] = 0x01fc,
224
225 [EDMR] = 0x0000,
226 [EDTRR] = 0x0008,
227 [EDRRR] = 0x0010,
228 [TDLAR] = 0x0018,
229 [RDLAR] = 0x0020,
230 [EESR] = 0x0028,
231 [EESIPR] = 0x0030,
232 [TRSCER] = 0x0038,
233 [RMFCR] = 0x0040,
234 [TFTR] = 0x0048,
235 [FDR] = 0x0050,
236 [RMCR] = 0x0058,
237 [TFUCR] = 0x0064,
238 [RFOCR] = 0x0068,
239 [FCFTR] = 0x0070,
240 [RPADIR] = 0x0078,
241 [TRIMD] = 0x007c,
242 [RBWAR] = 0x00c8,
243 [RDFAR] = 0x00cc,
244 [TBRAR] = 0x00d4,
245 [TDFAR] = 0x00d8,
246};
247
248static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
249 [ECMR] = 0x0160,
250 [ECSR] = 0x0164,
251 [ECSIPR] = 0x0168,
252 [PIR] = 0x016c,
253 [MAHR] = 0x0170,
254 [MALR] = 0x0174,
255 [RFLR] = 0x0178,
256 [PSR] = 0x017c,
257 [TROCR] = 0x0180,
258 [CDCR] = 0x0184,
259 [LCCR] = 0x0188,
260 [CNDCR] = 0x018c,
261 [CEFCR] = 0x0194,
262 [FRECR] = 0x0198,
263 [TSFRCR] = 0x019c,
264 [TLFRCR] = 0x01a0,
265 [RFCR] = 0x01a4,
266 [MAFCR] = 0x01a8,
267 [IPGR] = 0x01b4,
268 [APR] = 0x01b8,
269 [MPR] = 0x01bc,
270 [TPAUSER] = 0x01c4,
271 [BCFR] = 0x01cc,
272
273 [ARSTR] = 0x0000,
274 [TSU_CTRST] = 0x0004,
275 [TSU_FWEN0] = 0x0010,
276 [TSU_FWEN1] = 0x0014,
277 [TSU_FCM] = 0x0018,
278 [TSU_BSYSL0] = 0x0020,
279 [TSU_BSYSL1] = 0x0024,
280 [TSU_PRISL0] = 0x0028,
281 [TSU_PRISL1] = 0x002c,
282 [TSU_FWSL0] = 0x0030,
283 [TSU_FWSL1] = 0x0034,
284 [TSU_FWSLC] = 0x0038,
285 [TSU_QTAGM0] = 0x0040,
286 [TSU_QTAGM1] = 0x0044,
287 [TSU_ADQT0] = 0x0048,
288 [TSU_ADQT1] = 0x004c,
289 [TSU_FWSR] = 0x0050,
290 [TSU_FWINMK] = 0x0054,
291 [TSU_ADSBSY] = 0x0060,
292 [TSU_TEN] = 0x0064,
293 [TSU_POST1] = 0x0070,
294 [TSU_POST2] = 0x0074,
295 [TSU_POST3] = 0x0078,
296 [TSU_POST4] = 0x007c,
297
298 [TXNLCR0] = 0x0080,
299 [TXALCR0] = 0x0084,
300 [RXNLCR0] = 0x0088,
301 [RXALCR0] = 0x008c,
302 [FWNLCR0] = 0x0090,
303 [FWALCR0] = 0x0094,
304 [TXNLCR1] = 0x00a0,
305 [TXALCR1] = 0x00a0,
306 [RXNLCR1] = 0x00a8,
307 [RXALCR1] = 0x00ac,
308 [FWNLCR1] = 0x00b0,
309 [FWALCR1] = 0x00b4,
310
311 [TSU_ADRH0] = 0x0100,
312 [TSU_ADRL0] = 0x0104,
313 [TSU_ADRL31] = 0x01fc,
314};
315
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000316static int sh_eth_is_gether(struct sh_eth_private *mdp)
317{
318 if (mdp->reg_offset == sh_eth_offset_gigabit)
319 return 1;
320 else
321 return 0;
322}
323
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400324static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000325{
326 u32 value = 0x0;
327 struct sh_eth_private *mdp = netdev_priv(ndev);
328
329 switch (mdp->phy_interface) {
330 case PHY_INTERFACE_MODE_GMII:
331 value = 0x2;
332 break;
333 case PHY_INTERFACE_MODE_MII:
334 value = 0x1;
335 break;
336 case PHY_INTERFACE_MODE_RMII:
337 value = 0x0;
338 break;
339 default:
340 pr_warn("PHY interface mode was not setup. Set to MII.\n");
341 value = 0x1;
342 break;
343 }
344
345 sh_eth_write(ndev, value, RMII_MII);
346}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000347
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400348static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000349{
350 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000351
352 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000353 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000354 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000355 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000356}
357
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000358/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000359static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000360{
361 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000362
363 switch (mdp->speed) {
364 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000365 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000366 break;
367 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000368 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
369 break;
370 default:
371 break;
372 }
373}
374
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000375/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000376static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000377 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000378 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000379
380 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
381 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
382 .eesipr_value = 0x01ff009f,
383
384 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400385 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
386 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
387 EESR_ECI,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000388
389 .apr = 1,
390 .mpr = 1,
391 .tpauser = 1,
392 .hw_swap = 1,
393};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000394
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000395static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000396{
397 struct sh_eth_private *mdp = netdev_priv(ndev);
398
399 switch (mdp->speed) {
400 case 10: /* 10BASE */
401 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
402 break;
403 case 100:/* 100BASE */
404 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000405 break;
406 default:
407 break;
408 }
409}
410
411/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000412static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000413 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000414 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000415
416 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
417 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400418 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000419
420 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400421 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
422 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
423 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000424
425 .apr = 1,
426 .mpr = 1,
427 .tpauser = 1,
428 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800429 .rpadir = 1,
430 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000431};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000432
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000433static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000434{
435 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000436
437 switch (mdp->speed) {
438 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000439 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000440 break;
441 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000442 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000443 break;
444 default:
445 break;
446 }
447}
448
449/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000450static struct sh_eth_cpu_data sh7757_data = {
451 .set_duplex = sh_eth_set_duplex,
452 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000453
454 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
455 .rmcr_value = 0x00000001,
456
457 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400458 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
459 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
460 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000461
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000462 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000463 .apr = 1,
464 .mpr = 1,
465 .tpauser = 1,
466 .hw_swap = 1,
467 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000468 .rpadir = 1,
469 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000470};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000471
David S. Millere403d292013-06-07 23:40:41 -0700472#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000473#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
474#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
475static void sh_eth_chip_reset_giga(struct net_device *ndev)
476{
477 int i;
478 unsigned long mahr[2], malr[2];
479
480 /* save MAHR and MALR */
481 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000482 malr[i] = ioread32((void *)GIGA_MALR(i));
483 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000484 }
485
486 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000487 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000488 mdelay(1);
489
490 /* restore MAHR and MALR */
491 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000492 iowrite32(malr[i], (void *)GIGA_MALR(i));
493 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000494 }
495}
496
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000497static void sh_eth_set_rate_giga(struct net_device *ndev)
498{
499 struct sh_eth_private *mdp = netdev_priv(ndev);
500
501 switch (mdp->speed) {
502 case 10: /* 10BASE */
503 sh_eth_write(ndev, 0x00000000, GECMR);
504 break;
505 case 100:/* 100BASE */
506 sh_eth_write(ndev, 0x00000010, GECMR);
507 break;
508 case 1000: /* 1000BASE */
509 sh_eth_write(ndev, 0x00000020, GECMR);
510 break;
511 default:
512 break;
513 }
514}
515
516/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000517static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000518 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000519 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000520 .set_rate = sh_eth_set_rate_giga,
521
522 .ecsr_value = ECSR_ICD | ECSR_MPD,
523 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
524 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
525
526 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400527 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
528 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
529 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000530 .fdr_value = 0x0000072f,
531 .rmcr_value = 0x00000001,
532
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000533 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000534 .apr = 1,
535 .mpr = 1,
536 .tpauser = 1,
537 .bculr = 1,
538 .hw_swap = 1,
539 .rpadir = 1,
540 .rpadir_value = 2 << 16,
541 .no_trimd = 1,
542 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000543 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000544};
545
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000546static void sh_eth_chip_reset(struct net_device *ndev)
547{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000548 struct sh_eth_private *mdp = netdev_priv(ndev);
549
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000550 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000551 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000552 mdelay(1);
553}
554
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000555static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000556{
557 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000558
559 switch (mdp->speed) {
560 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000561 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000562 break;
563 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000564 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000565 break;
566 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000567 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000568 break;
569 default:
570 break;
571 }
572}
573
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000574/* SH7734 */
575static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000576 .chip_reset = sh_eth_chip_reset,
577 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000578 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000579
580 .ecsr_value = ECSR_ICD | ECSR_MPD,
581 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
582 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
583
584 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400585 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
586 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
587 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000588
589 .apr = 1,
590 .mpr = 1,
591 .tpauser = 1,
592 .bculr = 1,
593 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000594 .no_trimd = 1,
595 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000596 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000597 .hw_crc = 1,
598 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000599};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000600
601/* SH7763 */
602static struct sh_eth_cpu_data sh7763_data = {
603 .chip_reset = sh_eth_chip_reset,
604 .set_duplex = sh_eth_set_duplex,
605 .set_rate = sh_eth_set_rate_gether,
606
607 .ecsr_value = ECSR_ICD | ECSR_MPD,
608 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
609 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
610
611 .tx_check = EESR_TC1 | EESR_FTC,
612 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
613 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
614 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000615
616 .apr = 1,
617 .mpr = 1,
618 .tpauser = 1,
619 .bculr = 1,
620 .hw_swap = 1,
621 .no_trimd = 1,
622 .no_ade = 1,
623 .tsu = 1,
624 .irq_flags = IRQF_SHARED,
625};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000626
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000627static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000628{
629 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000630
631 /* reset device */
632 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
633 mdelay(1);
634
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000635 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000636}
637
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000638/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000639static struct sh_eth_cpu_data r8a7740_data = {
640 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000641 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000642 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000643
644 .ecsr_value = ECSR_ICD | ECSR_MPD,
645 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
646 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
647
648 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400649 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
650 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
651 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000652
653 .apr = 1,
654 .mpr = 1,
655 .tpauser = 1,
656 .bculr = 1,
657 .hw_swap = 1,
658 .no_trimd = 1,
659 .no_ade = 1,
660 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000661 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400662 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000663};
664
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000665static struct sh_eth_cpu_data sh7619_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000666 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
667
668 .apr = 1,
669 .mpr = 1,
670 .tpauser = 1,
671 .hw_swap = 1,
672};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000673
674static struct sh_eth_cpu_data sh771x_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000675 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000676 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000677};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000678
679static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
680{
681 if (!cd->ecsr_value)
682 cd->ecsr_value = DEFAULT_ECSR_INIT;
683
684 if (!cd->ecsipr_value)
685 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
686
687 if (!cd->fcftr_value)
688 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
689 DEFAULT_FIFO_F_D_RFD;
690
691 if (!cd->fdr_value)
692 cd->fdr_value = DEFAULT_FDR_INIT;
693
694 if (!cd->rmcr_value)
695 cd->rmcr_value = DEFAULT_RMCR_VALUE;
696
697 if (!cd->tx_check)
698 cd->tx_check = DEFAULT_TX_CHECK;
699
700 if (!cd->eesr_err_check)
701 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000702}
703
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000704static int sh_eth_check_reset(struct net_device *ndev)
705{
706 int ret = 0;
707 int cnt = 100;
708
709 while (cnt > 0) {
710 if (!(sh_eth_read(ndev, EDMR) & 0x3))
711 break;
712 mdelay(1);
713 cnt--;
714 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400715 if (cnt <= 0) {
716 pr_err("Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000717 ret = -ETIMEDOUT;
718 }
719 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000720}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000721
722static int sh_eth_reset(struct net_device *ndev)
723{
724 struct sh_eth_private *mdp = netdev_priv(ndev);
725 int ret = 0;
726
727 if (sh_eth_is_gether(mdp)) {
728 sh_eth_write(ndev, EDSR_ENALL, EDSR);
729 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
730 EDMR);
731
732 ret = sh_eth_check_reset(ndev);
733 if (ret)
734 goto out;
735
736 /* Table Init */
737 sh_eth_write(ndev, 0x0, TDLAR);
738 sh_eth_write(ndev, 0x0, TDFAR);
739 sh_eth_write(ndev, 0x0, TDFXR);
740 sh_eth_write(ndev, 0x0, TDFFR);
741 sh_eth_write(ndev, 0x0, RDLAR);
742 sh_eth_write(ndev, 0x0, RDFAR);
743 sh_eth_write(ndev, 0x0, RDFXR);
744 sh_eth_write(ndev, 0x0, RDFFR);
745
746 /* Reset HW CRC register */
747 if (mdp->cd->hw_crc)
748 sh_eth_write(ndev, 0x0, CSMR);
749
750 /* Select MII mode */
751 if (mdp->cd->select_mii)
752 sh_eth_select_mii(ndev);
753 } else {
754 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
755 EDMR);
756 mdelay(3);
757 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
758 EDMR);
759 }
760
761out:
762 return ret;
763}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000764
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000765#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000766static void sh_eth_set_receive_align(struct sk_buff *skb)
767{
768 int reserve;
769
770 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
771 if (reserve)
772 skb_reserve(skb, reserve);
773}
774#else
775static void sh_eth_set_receive_align(struct sk_buff *skb)
776{
777 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
778}
779#endif
780
781
Yoshinori Sato71557a32008-08-06 19:49:00 -0400782/* CPU <-> EDMAC endian convert */
783static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
784{
785 switch (mdp->edmac_endian) {
786 case EDMAC_LITTLE_ENDIAN:
787 return cpu_to_le32(x);
788 case EDMAC_BIG_ENDIAN:
789 return cpu_to_be32(x);
790 }
791 return x;
792}
793
794static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
795{
796 switch (mdp->edmac_endian) {
797 case EDMAC_LITTLE_ENDIAN:
798 return le32_to_cpu(x);
799 case EDMAC_BIG_ENDIAN:
800 return be32_to_cpu(x);
801 }
802 return x;
803}
804
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700805/*
806 * Program the hardware MAC address from dev->dev_addr.
807 */
808static void update_mac_address(struct net_device *ndev)
809{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000810 sh_eth_write(ndev,
811 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
812 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
813 sh_eth_write(ndev,
814 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700815}
816
817/*
818 * Get MAC address from SuperH MAC address register
819 *
820 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
821 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
822 * When you want use this device, you must set MAC address in bootloader.
823 *
824 */
Magnus Damm748031f2009-10-09 00:17:14 +0000825static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700826{
Magnus Damm748031f2009-10-09 00:17:14 +0000827 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
828 memcpy(ndev->dev_addr, mac, 6);
829 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000830 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
831 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
832 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
833 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
834 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
835 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000836 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700837}
838
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000839static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
840{
841 if (sh_eth_is_gether(mdp))
842 return EDTRR_TRNS_GETHER;
843 else
844 return EDTRR_TRNS_ETHER;
845}
846
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700847struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000848 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700849 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000850 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700851 u32 mmd_msk;/* MMD */
852 u32 mdo_msk;
853 u32 mdi_msk;
854 u32 mdc_msk;
855};
856
857/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000858static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700859{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000860 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700861}
862
863/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000864static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700865{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000866 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700867}
868
869/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000870static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700871{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000872 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700873}
874
875/* Data I/O pin control */
876static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
877{
878 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000879
880 if (bitbang->set_gate)
881 bitbang->set_gate(bitbang->addr);
882
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700883 if (bit)
884 bb_set(bitbang->addr, bitbang->mmd_msk);
885 else
886 bb_clr(bitbang->addr, bitbang->mmd_msk);
887}
888
889/* Set bit data*/
890static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
891{
892 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
893
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000894 if (bitbang->set_gate)
895 bitbang->set_gate(bitbang->addr);
896
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700897 if (bit)
898 bb_set(bitbang->addr, bitbang->mdo_msk);
899 else
900 bb_clr(bitbang->addr, bitbang->mdo_msk);
901}
902
903/* Get bit data*/
904static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
905{
906 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000907
908 if (bitbang->set_gate)
909 bitbang->set_gate(bitbang->addr);
910
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700911 return bb_read(bitbang->addr, bitbang->mdi_msk);
912}
913
914/* MDC pin control */
915static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
916{
917 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
918
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000919 if (bitbang->set_gate)
920 bitbang->set_gate(bitbang->addr);
921
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700922 if (bit)
923 bb_set(bitbang->addr, bitbang->mdc_msk);
924 else
925 bb_clr(bitbang->addr, bitbang->mdc_msk);
926}
927
928/* mdio bus control struct */
929static struct mdiobb_ops bb_ops = {
930 .owner = THIS_MODULE,
931 .set_mdc = sh_mdc_ctrl,
932 .set_mdio_dir = sh_mmd_ctrl,
933 .set_mdio_data = sh_set_mdio,
934 .get_mdio_data = sh_get_mdio,
935};
936
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700937/* free skb and descriptor buffer */
938static void sh_eth_ring_free(struct net_device *ndev)
939{
940 struct sh_eth_private *mdp = netdev_priv(ndev);
941 int i;
942
943 /* Free Rx skb ringbuffer */
944 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000945 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700946 if (mdp->rx_skbuff[i])
947 dev_kfree_skb(mdp->rx_skbuff[i]);
948 }
949 }
950 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000951 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700952
953 /* Free Tx skb ringbuffer */
954 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000955 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 if (mdp->tx_skbuff[i])
957 dev_kfree_skb(mdp->tx_skbuff[i]);
958 }
959 }
960 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000961 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700962}
963
964/* format skb and descriptor buffer */
965static void sh_eth_ring_format(struct net_device *ndev)
966{
967 struct sh_eth_private *mdp = netdev_priv(ndev);
968 int i;
969 struct sk_buff *skb;
970 struct sh_eth_rxdesc *rxdesc = NULL;
971 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000972 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
973 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700974
975 mdp->cur_rx = mdp->cur_tx = 0;
976 mdp->dirty_rx = mdp->dirty_tx = 0;
977
978 memset(mdp->rx_ring, 0, rx_ringsize);
979
980 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000981 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700982 /* skb */
983 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000984 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700985 mdp->rx_skbuff[i] = skb;
986 if (skb == NULL)
987 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +0000988 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000989 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000990 sh_eth_set_receive_align(skb);
991
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700992 /* RX descriptor */
993 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000994 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400995 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700996
997 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000998 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900999 /* Rx descriptor address set */
1000 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001001 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001002 if (sh_eth_is_gether(mdp))
1003 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001004 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001005 }
1006
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001007 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001008
1009 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001010 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001011
1012 memset(mdp->tx_ring, 0, tx_ringsize);
1013
1014 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001015 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016 mdp->tx_skbuff[i] = NULL;
1017 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001018 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001019 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001020 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001021 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001022 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001023 if (sh_eth_is_gether(mdp))
1024 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001025 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001026 }
1027
Yoshinori Sato71557a32008-08-06 19:49:00 -04001028 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029}
1030
1031/* Get skb and descriptor buffer */
1032static int sh_eth_ring_init(struct net_device *ndev)
1033{
1034 struct sh_eth_private *mdp = netdev_priv(ndev);
1035 int rx_ringsize, tx_ringsize, ret = 0;
1036
1037 /*
1038 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1039 * card needs room to do 8 byte alignment, +2 so we can reserve
1040 * the first 2 bytes, and +16 gets room for the status word from the
1041 * card.
1042 */
1043 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1044 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001045 if (mdp->cd->rpadir)
1046 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001047
1048 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001049 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1050 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001051 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001052 ret = -ENOMEM;
1053 return ret;
1054 }
1055
Joe Perchesb2adaca2013-02-03 17:43:58 +00001056 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1057 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001058 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001059 ret = -ENOMEM;
1060 goto skb_ring_free;
1061 }
1062
1063 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001064 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001065 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001066 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001067 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001068 ret = -ENOMEM;
1069 goto desc_ring_free;
1070 }
1071
1072 mdp->dirty_rx = 0;
1073
1074 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001075 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001076 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001077 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001079 ret = -ENOMEM;
1080 goto desc_ring_free;
1081 }
1082 return ret;
1083
1084desc_ring_free:
1085 /* free DMA buffer */
1086 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1087
1088skb_ring_free:
1089 /* Free Rx and Tx skb ring buffer */
1090 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001091 mdp->tx_ring = NULL;
1092 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001093
1094 return ret;
1095}
1096
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001097static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1098{
1099 int ringsize;
1100
1101 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001102 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001103 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1104 mdp->rx_desc_dma);
1105 mdp->rx_ring = NULL;
1106 }
1107
1108 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001109 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001110 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1111 mdp->tx_desc_dma);
1112 mdp->tx_ring = NULL;
1113 }
1114}
1115
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001116static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001117{
1118 int ret = 0;
1119 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001120 u32 val;
1121
1122 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001123 ret = sh_eth_reset(ndev);
1124 if (ret)
1125 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001127 /* Descriptor format */
1128 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001129 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001130 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001131
1132 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001133 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001134
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001135#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001136 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001137 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001138 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001139#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001140 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001141
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001142 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001143 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1144 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001145
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001146 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001147 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001148
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001149 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001150
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001151 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001152 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001153
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001154 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001155
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001156 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001157 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001158
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001159 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001160 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1161 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001162
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001163 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001164 if (start)
1165 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001166
1167 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001168 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001169 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1170
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001171 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001172
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001173 if (mdp->cd->set_rate)
1174 mdp->cd->set_rate(ndev);
1175
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001176 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001177 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001178
1179 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001180 if (start)
1181 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182
1183 /* Set MAC address */
1184 update_mac_address(ndev);
1185
1186 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001187 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001188 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001189 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001190 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001191 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001192 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001193
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001194 if (start) {
1195 /* Setting the Rx mode will start the Rx process. */
1196 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001197
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001198 netif_start_queue(ndev);
1199 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001200
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001201out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001202 return ret;
1203}
1204
1205/* free Tx skb function */
1206static int sh_eth_txfree(struct net_device *ndev)
1207{
1208 struct sh_eth_private *mdp = netdev_priv(ndev);
1209 struct sh_eth_txdesc *txdesc;
1210 int freeNum = 0;
1211 int entry = 0;
1212
1213 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001214 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001215 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001216 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001217 break;
1218 /* Free the original skb. */
1219 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001220 dma_unmap_single(&ndev->dev, txdesc->addr,
1221 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001222 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1223 mdp->tx_skbuff[entry] = NULL;
1224 freeNum++;
1225 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001226 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001227 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001228 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001229
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001230 ndev->stats.tx_packets++;
1231 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232 }
1233 return freeNum;
1234}
1235
1236/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001237static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001238{
1239 struct sh_eth_private *mdp = netdev_priv(ndev);
1240 struct sh_eth_rxdesc *rxdesc;
1241
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001242 int entry = mdp->cur_rx % mdp->num_rx_ring;
1243 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001244 struct sk_buff *skb;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001245 int exceeded = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001246 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001247 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001248
1249 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001250 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1251 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001252 pkt_len = rxdesc->frame_length;
1253
1254 if (--boguscnt < 0)
1255 break;
1256
Sergei Shtylyov37191092013-06-19 23:30:23 +04001257 if (*quota <= 0) {
1258 exceeded = 1;
1259 break;
1260 }
1261 (*quota)--;
1262
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001263 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001264 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001265
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001266 /*
1267 * In case of almost all GETHER/ETHERs, the Receive Frame State
1268 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1269 * bit 0. However, in case of the R8A7740's GETHER, the RFS
1270 * bits are from bit 25 to bit 16. So, the driver needs right
1271 * shifting by 16.
1272 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001273 if (mdp->cd->shift_rd0)
1274 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001275
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001276 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1277 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001278 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001279 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001280 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001281 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001282 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001283 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001284 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001286 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001287 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001288 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001289 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001290 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001291 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001292 if (!mdp->cd->hw_swap)
1293 sh_eth_soft_swap(
1294 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1295 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001296 skb = mdp->rx_skbuff[entry];
1297 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001298 if (mdp->cd->rpadir)
1299 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300 skb_put(skb, pkt_len);
1301 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001302 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001303 ndev->stats.rx_packets++;
1304 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001306 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001307 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001308 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001309 }
1310
1311 /* Refill the Rx ring buffers. */
1312 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001313 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001314 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001315 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001316 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001317
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001318 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001319 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001320 mdp->rx_skbuff[entry] = skb;
1321 if (skb == NULL)
1322 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001323 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001324 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001325 sh_eth_set_receive_align(skb);
1326
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001327 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001328 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001329 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001330 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001331 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001332 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001333 else
1334 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001335 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001336 }
1337
1338 /* Restart Rx engine if stopped. */
1339 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001340 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001341 /* fix the values for the next receiving if RDE is set */
1342 if (intr_status & EESR_RDE)
1343 mdp->cur_rx = mdp->dirty_rx =
1344 (sh_eth_read(ndev, RDFAR) -
1345 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001346 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001347 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001348
Sergei Shtylyov37191092013-06-19 23:30:23 +04001349 return exceeded;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001350}
1351
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001352static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001353{
1354 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001355 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1356 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001357}
1358
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001359static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001360{
1361 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001362 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1363 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001364}
1365
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001366/* error control function */
1367static void sh_eth_error(struct net_device *ndev, int intr_status)
1368{
1369 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001370 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001371 u32 link_stat;
1372 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001373
1374 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001375 felic_stat = sh_eth_read(ndev, ECSR);
1376 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001378 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001379 if (felic_stat & ECSR_LCHNG) {
1380 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001381 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001382 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001383 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001384 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001385 if (mdp->ether_link_active_low)
1386 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001387 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001388 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001389 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001390 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001391 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001392 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1393 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001395 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1396 ECSR);
1397 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1398 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001399 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001400 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 }
1402 }
1403 }
1404
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001405ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001406 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001407 /* Unused write back interrupt */
1408 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001409 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001410 if (netif_msg_tx_err(mdp))
1411 dev_err(&ndev->dev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001412 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001413 }
1414
1415 if (intr_status & EESR_RABT) {
1416 /* Receive Abort int */
1417 if (intr_status & EESR_RFRMER) {
1418 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001419 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001420 if (netif_msg_rx_err(mdp))
1421 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422 }
1423 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001424
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001425 if (intr_status & EESR_TDE) {
1426 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001427 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001428 if (netif_msg_tx_err(mdp))
1429 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1430 }
1431
1432 if (intr_status & EESR_TFE) {
1433 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001434 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001435 if (netif_msg_tx_err(mdp))
1436 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001437 }
1438
1439 if (intr_status & EESR_RDE) {
1440 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001441 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001442
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001443 if (netif_msg_rx_err(mdp))
1444 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001445 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001446
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447 if (intr_status & EESR_RFE) {
1448 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001449 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001450 if (netif_msg_rx_err(mdp))
1451 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1452 }
1453
1454 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1455 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001456 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001457 if (netif_msg_tx_err(mdp))
1458 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001459 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001460
1461 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1462 if (mdp->cd->no_ade)
1463 mask &= ~EESR_ADE;
1464 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001465 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001466 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001467 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001468 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1469 intr_status, mdp->cur_tx);
1470 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001471 mdp->dirty_tx, (u32) ndev->state, edtrr);
1472 /* dirty buffer free */
1473 sh_eth_txfree(ndev);
1474
1475 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001476 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001478 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001479 }
1480 /* wakeup */
1481 netif_wake_queue(ndev);
1482 }
1483}
1484
1485static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1486{
1487 struct net_device *ndev = netdev;
1488 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001489 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001490 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001491 unsigned long intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001492
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001493 spin_lock(&mdp->lock);
1494
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001495 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001496 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001497 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1498 * enabled since it's the one that comes thru regardless of the mask,
1499 * and we need to fully handle it in sh_eth_error() in order to quench
1500 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1501 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001502 intr_enable = sh_eth_read(ndev, EESIPR);
1503 intr_status &= intr_enable | DMAC_M_ECI;
1504 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001505 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001506 else
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001507 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001508
Sergei Shtylyov37191092013-06-19 23:30:23 +04001509 if (intr_status & EESR_RX_CHECK) {
1510 if (napi_schedule_prep(&mdp->napi)) {
1511 /* Mask Rx interrupts */
1512 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1513 EESIPR);
1514 __napi_schedule(&mdp->napi);
1515 } else {
1516 dev_warn(&ndev->dev,
1517 "ignoring interrupt, status 0x%08lx, mask 0x%08lx.\n",
1518 intr_status, intr_enable);
1519 }
1520 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001521
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001522 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001523 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001524 /* Clear Tx interrupts */
1525 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1526
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001527 sh_eth_txfree(ndev);
1528 netif_wake_queue(ndev);
1529 }
1530
Sergei Shtylyov37191092013-06-19 23:30:23 +04001531 if (intr_status & cd->eesr_err_check) {
1532 /* Clear error interrupts */
1533 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1534
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001536 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001537
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001538other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001539 spin_unlock(&mdp->lock);
1540
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001541 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001542}
1543
Sergei Shtylyov37191092013-06-19 23:30:23 +04001544static int sh_eth_poll(struct napi_struct *napi, int budget)
1545{
1546 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1547 napi);
1548 struct net_device *ndev = napi->dev;
1549 int quota = budget;
1550 unsigned long intr_status;
1551
1552 for (;;) {
1553 intr_status = sh_eth_read(ndev, EESR);
1554 if (!(intr_status & EESR_RX_CHECK))
1555 break;
1556 /* Clear Rx interrupts */
1557 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1558
1559 if (sh_eth_rx(ndev, intr_status, &quota))
1560 goto out;
1561 }
1562
1563 napi_complete(napi);
1564
1565 /* Reenable Rx interrupts */
1566 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1567out:
1568 return budget - quota;
1569}
1570
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001571/* PHY state control function */
1572static void sh_eth_adjust_link(struct net_device *ndev)
1573{
1574 struct sh_eth_private *mdp = netdev_priv(ndev);
1575 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001576 int new_state = 0;
1577
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001578 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 if (phydev->duplex != mdp->duplex) {
1580 new_state = 1;
1581 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001582 if (mdp->cd->set_duplex)
1583 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001584 }
1585
1586 if (phydev->speed != mdp->speed) {
1587 new_state = 1;
1588 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001589 if (mdp->cd->set_rate)
1590 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001591 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001592 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001593 sh_eth_write(ndev,
1594 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595 new_state = 1;
1596 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001597 if (mdp->cd->no_psr || mdp->no_ether_link)
1598 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001599 }
1600 } else if (mdp->link) {
1601 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001602 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001603 mdp->speed = 0;
1604 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001605 if (mdp->cd->no_psr || mdp->no_ether_link)
1606 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001607 }
1608
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001609 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610 phy_print_status(phydev);
1611}
1612
1613/* PHY init function */
1614static int sh_eth_phy_init(struct net_device *ndev)
1615{
1616 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001617 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001618 struct phy_device *phydev = NULL;
1619
Kay Sieversfb28ad32008-11-10 13:55:14 -08001620 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001621 mdp->mii_bus->id , mdp->phy_id);
1622
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001623 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001624 mdp->speed = 0;
1625 mdp->duplex = -1;
1626
1627 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001628 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001629 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001630 if (IS_ERR(phydev)) {
1631 dev_err(&ndev->dev, "phy_connect failed\n");
1632 return PTR_ERR(phydev);
1633 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001634
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001635 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001636 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001637
1638 mdp->phydev = phydev;
1639
1640 return 0;
1641}
1642
1643/* PHY control start function */
1644static int sh_eth_phy_start(struct net_device *ndev)
1645{
1646 struct sh_eth_private *mdp = netdev_priv(ndev);
1647 int ret;
1648
1649 ret = sh_eth_phy_init(ndev);
1650 if (ret)
1651 return ret;
1652
1653 /* reset phy - this also wakes it from PDOWN */
1654 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1655 phy_start(mdp->phydev);
1656
1657 return 0;
1658}
1659
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001660static int sh_eth_get_settings(struct net_device *ndev,
1661 struct ethtool_cmd *ecmd)
1662{
1663 struct sh_eth_private *mdp = netdev_priv(ndev);
1664 unsigned long flags;
1665 int ret;
1666
1667 spin_lock_irqsave(&mdp->lock, flags);
1668 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1669 spin_unlock_irqrestore(&mdp->lock, flags);
1670
1671 return ret;
1672}
1673
1674static int sh_eth_set_settings(struct net_device *ndev,
1675 struct ethtool_cmd *ecmd)
1676{
1677 struct sh_eth_private *mdp = netdev_priv(ndev);
1678 unsigned long flags;
1679 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001680
1681 spin_lock_irqsave(&mdp->lock, flags);
1682
1683 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001684 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001685
1686 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1687 if (ret)
1688 goto error_exit;
1689
1690 if (ecmd->duplex == DUPLEX_FULL)
1691 mdp->duplex = 1;
1692 else
1693 mdp->duplex = 0;
1694
1695 if (mdp->cd->set_duplex)
1696 mdp->cd->set_duplex(ndev);
1697
1698error_exit:
1699 mdelay(1);
1700
1701 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001702 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001703
1704 spin_unlock_irqrestore(&mdp->lock, flags);
1705
1706 return ret;
1707}
1708
1709static int sh_eth_nway_reset(struct net_device *ndev)
1710{
1711 struct sh_eth_private *mdp = netdev_priv(ndev);
1712 unsigned long flags;
1713 int ret;
1714
1715 spin_lock_irqsave(&mdp->lock, flags);
1716 ret = phy_start_aneg(mdp->phydev);
1717 spin_unlock_irqrestore(&mdp->lock, flags);
1718
1719 return ret;
1720}
1721
1722static u32 sh_eth_get_msglevel(struct net_device *ndev)
1723{
1724 struct sh_eth_private *mdp = netdev_priv(ndev);
1725 return mdp->msg_enable;
1726}
1727
1728static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1729{
1730 struct sh_eth_private *mdp = netdev_priv(ndev);
1731 mdp->msg_enable = value;
1732}
1733
1734static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1735 "rx_current", "tx_current",
1736 "rx_dirty", "tx_dirty",
1737};
1738#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1739
1740static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1741{
1742 switch (sset) {
1743 case ETH_SS_STATS:
1744 return SH_ETH_STATS_LEN;
1745 default:
1746 return -EOPNOTSUPP;
1747 }
1748}
1749
1750static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1751 struct ethtool_stats *stats, u64 *data)
1752{
1753 struct sh_eth_private *mdp = netdev_priv(ndev);
1754 int i = 0;
1755
1756 /* device-specific stats */
1757 data[i++] = mdp->cur_rx;
1758 data[i++] = mdp->cur_tx;
1759 data[i++] = mdp->dirty_rx;
1760 data[i++] = mdp->dirty_tx;
1761}
1762
1763static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1764{
1765 switch (stringset) {
1766 case ETH_SS_STATS:
1767 memcpy(data, *sh_eth_gstrings_stats,
1768 sizeof(sh_eth_gstrings_stats));
1769 break;
1770 }
1771}
1772
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001773static void sh_eth_get_ringparam(struct net_device *ndev,
1774 struct ethtool_ringparam *ring)
1775{
1776 struct sh_eth_private *mdp = netdev_priv(ndev);
1777
1778 ring->rx_max_pending = RX_RING_MAX;
1779 ring->tx_max_pending = TX_RING_MAX;
1780 ring->rx_pending = mdp->num_rx_ring;
1781 ring->tx_pending = mdp->num_tx_ring;
1782}
1783
1784static int sh_eth_set_ringparam(struct net_device *ndev,
1785 struct ethtool_ringparam *ring)
1786{
1787 struct sh_eth_private *mdp = netdev_priv(ndev);
1788 int ret;
1789
1790 if (ring->tx_pending > TX_RING_MAX ||
1791 ring->rx_pending > RX_RING_MAX ||
1792 ring->tx_pending < TX_RING_MIN ||
1793 ring->rx_pending < RX_RING_MIN)
1794 return -EINVAL;
1795 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1796 return -EINVAL;
1797
1798 if (netif_running(ndev)) {
1799 netif_tx_disable(ndev);
1800 /* Disable interrupts by clearing the interrupt mask. */
1801 sh_eth_write(ndev, 0x0000, EESIPR);
1802 /* Stop the chip's Tx and Rx processes. */
1803 sh_eth_write(ndev, 0, EDTRR);
1804 sh_eth_write(ndev, 0, EDRRR);
1805 synchronize_irq(ndev->irq);
1806 }
1807
1808 /* Free all the skbuffs in the Rx queue. */
1809 sh_eth_ring_free(ndev);
1810 /* Free DMA buffer */
1811 sh_eth_free_dma_buffer(mdp);
1812
1813 /* Set new parameters */
1814 mdp->num_rx_ring = ring->rx_pending;
1815 mdp->num_tx_ring = ring->tx_pending;
1816
1817 ret = sh_eth_ring_init(ndev);
1818 if (ret < 0) {
1819 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1820 return ret;
1821 }
1822 ret = sh_eth_dev_init(ndev, false);
1823 if (ret < 0) {
1824 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1825 return ret;
1826 }
1827
1828 if (netif_running(ndev)) {
1829 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1830 /* Setting the Rx mode will start the Rx process. */
1831 sh_eth_write(ndev, EDRRR_R, EDRRR);
1832 netif_wake_queue(ndev);
1833 }
1834
1835 return 0;
1836}
1837
stephen hemminger9b07be42012-01-04 12:59:49 +00001838static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001839 .get_settings = sh_eth_get_settings,
1840 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001841 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001842 .get_msglevel = sh_eth_get_msglevel,
1843 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001844 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001845 .get_strings = sh_eth_get_strings,
1846 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1847 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001848 .get_ringparam = sh_eth_get_ringparam,
1849 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001850};
1851
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001852/* network device open function */
1853static int sh_eth_open(struct net_device *ndev)
1854{
1855 int ret = 0;
1856 struct sh_eth_private *mdp = netdev_priv(ndev);
1857
Magnus Dammbcd51492009-10-09 00:20:04 +00001858 pm_runtime_get_sync(&mdp->pdev->dev);
1859
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04001860 napi_enable(&mdp->napi);
1861
Joe Perchesa0607fd2009-11-18 23:29:17 -08001862 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001863 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001864 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001865 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04001866 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001867 }
1868
1869 /* Descriptor set */
1870 ret = sh_eth_ring_init(ndev);
1871 if (ret)
1872 goto out_free_irq;
1873
1874 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001875 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001876 if (ret)
1877 goto out_free_irq;
1878
1879 /* PHY control start*/
1880 ret = sh_eth_phy_start(ndev);
1881 if (ret)
1882 goto out_free_irq;
1883
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001884 return ret;
1885
1886out_free_irq:
1887 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04001888out_napi_off:
1889 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00001890 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001891 return ret;
1892}
1893
1894/* Timeout function */
1895static void sh_eth_tx_timeout(struct net_device *ndev)
1896{
1897 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001898 struct sh_eth_rxdesc *rxdesc;
1899 int i;
1900
1901 netif_stop_queue(ndev);
1902
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001903 if (netif_msg_timer(mdp))
1904 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001905 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001906
1907 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001908 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001909
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001910 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001911 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001912 rxdesc = &mdp->rx_ring[i];
1913 rxdesc->status = 0;
1914 rxdesc->addr = 0xBADF00D0;
1915 if (mdp->rx_skbuff[i])
1916 dev_kfree_skb(mdp->rx_skbuff[i]);
1917 mdp->rx_skbuff[i] = NULL;
1918 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001919 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001920 if (mdp->tx_skbuff[i])
1921 dev_kfree_skb(mdp->tx_skbuff[i]);
1922 mdp->tx_skbuff[i] = NULL;
1923 }
1924
1925 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001926 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001927}
1928
1929/* Packet transmit function */
1930static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1931{
1932 struct sh_eth_private *mdp = netdev_priv(ndev);
1933 struct sh_eth_txdesc *txdesc;
1934 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001935 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001936
1937 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001938 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001939 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001940 if (netif_msg_tx_queued(mdp))
1941 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001942 netif_stop_queue(ndev);
1943 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001944 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001945 }
1946 }
1947 spin_unlock_irqrestore(&mdp->lock, flags);
1948
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001949 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001950 mdp->tx_skbuff[entry] = skb;
1951 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001952 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001953 if (!mdp->cd->hw_swap)
1954 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1955 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001956 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1957 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001958 if (skb->len < ETHERSMALL)
1959 txdesc->buffer_length = ETHERSMALL;
1960 else
1961 txdesc->buffer_length = skb->len;
1962
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001963 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001964 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001965 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001966 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001967
1968 mdp->cur_tx++;
1969
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001970 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1971 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001972
Patrick McHardy6ed10652009-06-23 06:03:08 +00001973 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001974}
1975
1976/* device close function */
1977static int sh_eth_close(struct net_device *ndev)
1978{
1979 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001980
1981 netif_stop_queue(ndev);
1982
1983 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001984 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001985
1986 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001987 sh_eth_write(ndev, 0, EDTRR);
1988 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001989
1990 /* PHY Disconnect */
1991 if (mdp->phydev) {
1992 phy_stop(mdp->phydev);
1993 phy_disconnect(mdp->phydev);
1994 }
1995
1996 free_irq(ndev->irq, ndev);
1997
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04001998 napi_disable(&mdp->napi);
1999
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002000 /* Free all the skbuffs in the Rx queue. */
2001 sh_eth_ring_free(ndev);
2002
2003 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00002004 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002005
Magnus Dammbcd51492009-10-09 00:20:04 +00002006 pm_runtime_put_sync(&mdp->pdev->dev);
2007
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002008 return 0;
2009}
2010
2011static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2012{
2013 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002014
Magnus Dammbcd51492009-10-09 00:20:04 +00002015 pm_runtime_get_sync(&mdp->pdev->dev);
2016
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002017 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002018 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002019 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002020 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002021 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002022 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002023 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002024 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002025 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002026 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002027 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
2028 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002029 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002030 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2031 }
Magnus Dammbcd51492009-10-09 00:20:04 +00002032 pm_runtime_put_sync(&mdp->pdev->dev);
2033
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002034 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002035}
2036
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002037/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002038static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
2039 int cmd)
2040{
2041 struct sh_eth_private *mdp = netdev_priv(ndev);
2042 struct phy_device *phydev = mdp->phydev;
2043
2044 if (!netif_running(ndev))
2045 return -EINVAL;
2046
2047 if (!phydev)
2048 return -ENODEV;
2049
Richard Cochran28b04112010-07-17 08:48:55 +00002050 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002051}
2052
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002053/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2054static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2055 int entry)
2056{
2057 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2058}
2059
2060static u32 sh_eth_tsu_get_post_mask(int entry)
2061{
2062 return 0x0f << (28 - ((entry % 8) * 4));
2063}
2064
2065static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2066{
2067 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2068}
2069
2070static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2071 int entry)
2072{
2073 struct sh_eth_private *mdp = netdev_priv(ndev);
2074 u32 tmp;
2075 void *reg_offset;
2076
2077 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2078 tmp = ioread32(reg_offset);
2079 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2080}
2081
2082static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2083 int entry)
2084{
2085 struct sh_eth_private *mdp = netdev_priv(ndev);
2086 u32 post_mask, ref_mask, tmp;
2087 void *reg_offset;
2088
2089 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2090 post_mask = sh_eth_tsu_get_post_mask(entry);
2091 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2092
2093 tmp = ioread32(reg_offset);
2094 iowrite32(tmp & ~post_mask, reg_offset);
2095
2096 /* If other port enables, the function returns "true" */
2097 return tmp & ref_mask;
2098}
2099
2100static int sh_eth_tsu_busy(struct net_device *ndev)
2101{
2102 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2103 struct sh_eth_private *mdp = netdev_priv(ndev);
2104
2105 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2106 udelay(10);
2107 timeout--;
2108 if (timeout <= 0) {
2109 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2110 return -ETIMEDOUT;
2111 }
2112 }
2113
2114 return 0;
2115}
2116
2117static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2118 const u8 *addr)
2119{
2120 u32 val;
2121
2122 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2123 iowrite32(val, reg);
2124 if (sh_eth_tsu_busy(ndev) < 0)
2125 return -EBUSY;
2126
2127 val = addr[4] << 8 | addr[5];
2128 iowrite32(val, reg + 4);
2129 if (sh_eth_tsu_busy(ndev) < 0)
2130 return -EBUSY;
2131
2132 return 0;
2133}
2134
2135static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2136{
2137 u32 val;
2138
2139 val = ioread32(reg);
2140 addr[0] = (val >> 24) & 0xff;
2141 addr[1] = (val >> 16) & 0xff;
2142 addr[2] = (val >> 8) & 0xff;
2143 addr[3] = val & 0xff;
2144 val = ioread32(reg + 4);
2145 addr[4] = (val >> 8) & 0xff;
2146 addr[5] = val & 0xff;
2147}
2148
2149
2150static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2151{
2152 struct sh_eth_private *mdp = netdev_priv(ndev);
2153 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2154 int i;
2155 u8 c_addr[ETH_ALEN];
2156
2157 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2158 sh_eth_tsu_read_entry(reg_offset, c_addr);
2159 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2160 return i;
2161 }
2162
2163 return -ENOENT;
2164}
2165
2166static int sh_eth_tsu_find_empty(struct net_device *ndev)
2167{
2168 u8 blank[ETH_ALEN];
2169 int entry;
2170
2171 memset(blank, 0, sizeof(blank));
2172 entry = sh_eth_tsu_find_entry(ndev, blank);
2173 return (entry < 0) ? -ENOMEM : entry;
2174}
2175
2176static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2177 int entry)
2178{
2179 struct sh_eth_private *mdp = netdev_priv(ndev);
2180 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2181 int ret;
2182 u8 blank[ETH_ALEN];
2183
2184 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2185 ~(1 << (31 - entry)), TSU_TEN);
2186
2187 memset(blank, 0, sizeof(blank));
2188 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2189 if (ret < 0)
2190 return ret;
2191 return 0;
2192}
2193
2194static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2195{
2196 struct sh_eth_private *mdp = netdev_priv(ndev);
2197 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2198 int i, ret;
2199
2200 if (!mdp->cd->tsu)
2201 return 0;
2202
2203 i = sh_eth_tsu_find_entry(ndev, addr);
2204 if (i < 0) {
2205 /* No entry found, create one */
2206 i = sh_eth_tsu_find_empty(ndev);
2207 if (i < 0)
2208 return -ENOMEM;
2209 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2210 if (ret < 0)
2211 return ret;
2212
2213 /* Enable the entry */
2214 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2215 (1 << (31 - i)), TSU_TEN);
2216 }
2217
2218 /* Entry found or created, enable POST */
2219 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2220
2221 return 0;
2222}
2223
2224static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2225{
2226 struct sh_eth_private *mdp = netdev_priv(ndev);
2227 int i, ret;
2228
2229 if (!mdp->cd->tsu)
2230 return 0;
2231
2232 i = sh_eth_tsu_find_entry(ndev, addr);
2233 if (i) {
2234 /* Entry found */
2235 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2236 goto done;
2237
2238 /* Disable the entry if both ports was disabled */
2239 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2240 if (ret < 0)
2241 return ret;
2242 }
2243done:
2244 return 0;
2245}
2246
2247static int sh_eth_tsu_purge_all(struct net_device *ndev)
2248{
2249 struct sh_eth_private *mdp = netdev_priv(ndev);
2250 int i, ret;
2251
2252 if (unlikely(!mdp->cd->tsu))
2253 return 0;
2254
2255 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2256 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2257 continue;
2258
2259 /* Disable the entry if both ports was disabled */
2260 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2261 if (ret < 0)
2262 return ret;
2263 }
2264
2265 return 0;
2266}
2267
2268static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2269{
2270 struct sh_eth_private *mdp = netdev_priv(ndev);
2271 u8 addr[ETH_ALEN];
2272 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2273 int i;
2274
2275 if (unlikely(!mdp->cd->tsu))
2276 return;
2277
2278 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2279 sh_eth_tsu_read_entry(reg_offset, addr);
2280 if (is_multicast_ether_addr(addr))
2281 sh_eth_tsu_del_entry(ndev, addr);
2282 }
2283}
2284
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002285/* Multicast reception directions set */
2286static void sh_eth_set_multicast_list(struct net_device *ndev)
2287{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002288 struct sh_eth_private *mdp = netdev_priv(ndev);
2289 u32 ecmr_bits;
2290 int mcast_all = 0;
2291 unsigned long flags;
2292
2293 spin_lock_irqsave(&mdp->lock, flags);
2294 /*
2295 * Initial condition is MCT = 1, PRM = 0.
2296 * Depending on ndev->flags, set PRM or clear MCT
2297 */
2298 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2299
2300 if (!(ndev->flags & IFF_MULTICAST)) {
2301 sh_eth_tsu_purge_mcast(ndev);
2302 mcast_all = 1;
2303 }
2304 if (ndev->flags & IFF_ALLMULTI) {
2305 sh_eth_tsu_purge_mcast(ndev);
2306 ecmr_bits &= ~ECMR_MCT;
2307 mcast_all = 1;
2308 }
2309
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002310 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002311 sh_eth_tsu_purge_all(ndev);
2312 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2313 } else if (mdp->cd->tsu) {
2314 struct netdev_hw_addr *ha;
2315 netdev_for_each_mc_addr(ha, ndev) {
2316 if (mcast_all && is_multicast_ether_addr(ha->addr))
2317 continue;
2318
2319 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2320 if (!mcast_all) {
2321 sh_eth_tsu_purge_mcast(ndev);
2322 ecmr_bits &= ~ECMR_MCT;
2323 mcast_all = 1;
2324 }
2325 }
2326 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002327 } else {
2328 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002329 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002330 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002331
2332 /* update the ethernet mode */
2333 sh_eth_write(ndev, ecmr_bits, ECMR);
2334
2335 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002336}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002337
2338static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2339{
2340 if (!mdp->port)
2341 return TSU_VTAG0;
2342 else
2343 return TSU_VTAG1;
2344}
2345
Patrick McHardy80d5c362013-04-19 02:04:28 +00002346static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2347 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002348{
2349 struct sh_eth_private *mdp = netdev_priv(ndev);
2350 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2351
2352 if (unlikely(!mdp->cd->tsu))
2353 return -EPERM;
2354
2355 /* No filtering if vid = 0 */
2356 if (!vid)
2357 return 0;
2358
2359 mdp->vlan_num_ids++;
2360
2361 /*
2362 * The controller has one VLAN tag HW filter. So, if the filter is
2363 * already enabled, the driver disables it and the filte
2364 */
2365 if (mdp->vlan_num_ids > 1) {
2366 /* disable VLAN filter */
2367 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2368 return 0;
2369 }
2370
2371 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2372 vtag_reg_index);
2373
2374 return 0;
2375}
2376
Patrick McHardy80d5c362013-04-19 02:04:28 +00002377static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2378 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002379{
2380 struct sh_eth_private *mdp = netdev_priv(ndev);
2381 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2382
2383 if (unlikely(!mdp->cd->tsu))
2384 return -EPERM;
2385
2386 /* No filtering if vid = 0 */
2387 if (!vid)
2388 return 0;
2389
2390 mdp->vlan_num_ids--;
2391 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2392
2393 return 0;
2394}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002395
2396/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002397static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002398{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002399 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2400 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2401 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2402 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2403 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2404 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2405 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2406 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2407 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2408 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002409 if (sh_eth_is_gether(mdp)) {
2410 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2411 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2412 } else {
2413 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2414 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2415 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002416 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2417 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2418 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2419 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2420 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2421 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2422 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002423}
2424
2425/* MDIO bus release function */
2426static int sh_mdio_release(struct net_device *ndev)
2427{
2428 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2429
2430 /* unregister mdio bus */
2431 mdiobus_unregister(bus);
2432
2433 /* remove mdio bus info from net_device */
2434 dev_set_drvdata(&ndev->dev, NULL);
2435
2436 /* free bitbang info */
2437 free_mdio_bitbang(bus);
2438
2439 return 0;
2440}
2441
2442/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002443static int sh_mdio_init(struct net_device *ndev, int id,
2444 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002445{
2446 int ret, i;
2447 struct bb_info *bitbang;
2448 struct sh_eth_private *mdp = netdev_priv(ndev);
2449
2450 /* create bit control struct for PHY */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002451 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2452 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002453 if (!bitbang) {
2454 ret = -ENOMEM;
2455 goto out;
2456 }
2457
2458 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002459 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002460 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002461 bitbang->mdi_msk = PIR_MDI;
2462 bitbang->mdo_msk = PIR_MDO;
2463 bitbang->mmd_msk = PIR_MMD;
2464 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002465 bitbang->ctrl.ops = &bb_ops;
2466
Stefan Weilc2e07b32010-08-03 19:44:52 +02002467 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002468 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2469 if (!mdp->mii_bus) {
2470 ret = -ENOMEM;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002471 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002472 }
2473
2474 /* Hook up MII support for ethtool */
2475 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002476 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002477 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002478 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002479
2480 /* PHY IRQ */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002481 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2482 sizeof(int) * PHY_MAX_ADDR,
2483 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002484 if (!mdp->mii_bus->irq) {
2485 ret = -ENOMEM;
2486 goto out_free_bus;
2487 }
2488
2489 for (i = 0; i < PHY_MAX_ADDR; i++)
2490 mdp->mii_bus->irq[i] = PHY_POLL;
2491
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002492 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002493 ret = mdiobus_register(mdp->mii_bus);
2494 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002495 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002496
2497 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2498
2499 return 0;
2500
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002501out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002502 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002503
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002504out:
2505 return ret;
2506}
2507
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002508static const u16 *sh_eth_get_register_offset(int register_type)
2509{
2510 const u16 *reg_offset = NULL;
2511
2512 switch (register_type) {
2513 case SH_ETH_REG_GIGABIT:
2514 reg_offset = sh_eth_offset_gigabit;
2515 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002516 case SH_ETH_REG_FAST_RCAR:
2517 reg_offset = sh_eth_offset_fast_rcar;
2518 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002519 case SH_ETH_REG_FAST_SH4:
2520 reg_offset = sh_eth_offset_fast_sh4;
2521 break;
2522 case SH_ETH_REG_FAST_SH3_SH2:
2523 reg_offset = sh_eth_offset_fast_sh3_sh2;
2524 break;
2525 default:
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +00002526 pr_err("Unknown register type (%d)\n", register_type);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002527 break;
2528 }
2529
2530 return reg_offset;
2531}
2532
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002533static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002534 .ndo_open = sh_eth_open,
2535 .ndo_stop = sh_eth_close,
2536 .ndo_start_xmit = sh_eth_start_xmit,
2537 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002538 .ndo_tx_timeout = sh_eth_tx_timeout,
2539 .ndo_do_ioctl = sh_eth_do_ioctl,
2540 .ndo_validate_addr = eth_validate_addr,
2541 .ndo_set_mac_address = eth_mac_addr,
2542 .ndo_change_mtu = eth_change_mtu,
2543};
2544
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002545static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2546 .ndo_open = sh_eth_open,
2547 .ndo_stop = sh_eth_close,
2548 .ndo_start_xmit = sh_eth_start_xmit,
2549 .ndo_get_stats = sh_eth_get_stats,
2550 .ndo_set_rx_mode = sh_eth_set_multicast_list,
2551 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2552 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2553 .ndo_tx_timeout = sh_eth_tx_timeout,
2554 .ndo_do_ioctl = sh_eth_do_ioctl,
2555 .ndo_validate_addr = eth_validate_addr,
2556 .ndo_set_mac_address = eth_mac_addr,
2557 .ndo_change_mtu = eth_change_mtu,
2558};
2559
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002560static int sh_eth_drv_probe(struct platform_device *pdev)
2561{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002562 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002563 struct resource *res;
2564 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002565 struct sh_eth_private *mdp = NULL;
Sergei Shtylyov564044b2013-03-21 10:39:22 +00002566 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002567 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002568
2569 /* get base addr */
2570 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2571 if (unlikely(res == NULL)) {
2572 dev_err(&pdev->dev, "invalid resource\n");
2573 ret = -EINVAL;
2574 goto out;
2575 }
2576
2577 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2578 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002579 ret = -ENOMEM;
2580 goto out;
2581 }
2582
2583 /* The sh Ether-specific entries in the device structure. */
2584 ndev->base_addr = res->start;
2585 devno = pdev->id;
2586 if (devno < 0)
2587 devno = 0;
2588
2589 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002590 ret = platform_get_irq(pdev, 0);
2591 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002592 ret = -ENODEV;
2593 goto out_release;
2594 }
roel kluincc3c0802008-09-10 19:22:44 +02002595 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002596
2597 SET_NETDEV_DEV(ndev, &pdev->dev);
2598
2599 /* Fill in the fields of the device structure with ethernet values. */
2600 ether_setup(ndev);
2601
2602 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002603 mdp->num_tx_ring = TX_RING_SIZE;
2604 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002605 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2606 if (IS_ERR(mdp->addr)) {
2607 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002608 goto out_release;
2609 }
2610
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002611 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002612 mdp->pdev = pdev;
2613 pm_runtime_enable(&pdev->dev);
2614 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002615
2616 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002617 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002618 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002619 /* EDMAC endian */
2620 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002621 mdp->no_ether_link = pd->no_ether_link;
2622 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002623 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002624
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002625 /* set cpu data */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00002626 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002627 sh_eth_set_default_cpu_data(mdp->cd);
2628
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002629 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002630 if (mdp->cd->tsu)
2631 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
2632 else
2633 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002634 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002635 ndev->watchdog_timeo = TX_TIMEOUT;
2636
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002637 /* debug message level */
2638 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002639
2640 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002641 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002642 if (!is_valid_ether_addr(ndev->dev_addr)) {
2643 dev_warn(&pdev->dev,
2644 "no valid MAC address supplied, using a random one.\n");
2645 eth_hw_addr_random(ndev);
2646 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002647
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002648 /* ioremap the TSU registers */
2649 if (mdp->cd->tsu) {
2650 struct resource *rtsu;
2651 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002652 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2653 if (IS_ERR(mdp->tsu_addr)) {
2654 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002655 goto out_release;
2656 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002657 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002658 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002659 }
2660
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002661 /* initialize first or needed device */
2662 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002663 if (mdp->cd->chip_reset)
2664 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002665
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002666 if (mdp->cd->tsu) {
2667 /* TSU init (Init only)*/
2668 sh_eth_tsu_init(mdp);
2669 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002670 }
2671
Sergei Shtylyov37191092013-06-19 23:30:23 +04002672 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
2673
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002674 /* network device register */
2675 ret = register_netdev(ndev);
2676 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04002677 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002678
2679 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002680 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002681 if (ret)
2682 goto out_unregister;
2683
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002684 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002685 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2686 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002687
2688 platform_set_drvdata(pdev, ndev);
2689
2690 return ret;
2691
2692out_unregister:
2693 unregister_netdev(ndev);
2694
Sergei Shtylyov37191092013-06-19 23:30:23 +04002695out_napi_del:
2696 netif_napi_del(&mdp->napi);
2697
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002698out_release:
2699 /* net_dev free */
2700 if (ndev)
2701 free_netdev(ndev);
2702
2703out:
2704 return ret;
2705}
2706
2707static int sh_eth_drv_remove(struct platform_device *pdev)
2708{
2709 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002710 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002711
2712 sh_mdio_release(ndev);
2713 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04002714 netif_napi_del(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002715 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002716 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002717
2718 return 0;
2719}
2720
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002721#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002722static int sh_eth_runtime_nop(struct device *dev)
2723{
2724 /*
2725 * Runtime PM callback shared between ->runtime_suspend()
2726 * and ->runtime_resume(). Simply returns success.
2727 *
2728 * This driver re-initializes all registers after
2729 * pm_runtime_get_sync() anyway so there is no need
2730 * to save and restore registers here.
2731 */
2732 return 0;
2733}
2734
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002735static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002736 .runtime_suspend = sh_eth_runtime_nop,
2737 .runtime_resume = sh_eth_runtime_nop,
2738};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002739#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2740#else
2741#define SH_ETH_PM_OPS NULL
2742#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00002743
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002744static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00002745 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00002746 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00002747 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002748 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00002749 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
2750 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00002751 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00002752 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00002753 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002754 { }
2755};
2756MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
2757
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002758static struct platform_driver sh_eth_driver = {
2759 .probe = sh_eth_drv_probe,
2760 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002761 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002762 .driver = {
2763 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002764 .pm = SH_ETH_PM_OPS,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002765 },
2766};
2767
Axel Lindb62f682011-11-27 16:44:17 +00002768module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002769
2770MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2771MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2772MODULE_LICENSE("GPL v2");