blob: da620ecd415115022503a52aaa6f9a0973da49e8 [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
Nobuhiro Iwamatsub7feacf2013-06-06 09:50:30 +0000324static void __maybe_unused 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
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000348static void __maybe_unused 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 */
359#if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000360static void sh_eth_set_rate(struct net_device *ndev)
361{
362 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000363
364 switch (mdp->speed) {
365 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000366 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000367 break;
368 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000369 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
370 break;
371 default:
372 break;
373 }
374}
375
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000376/* R8A7778/9 */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000377static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
378 .set_duplex = sh_eth_set_duplex,
379 .set_rate = sh_eth_set_rate,
380
381 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
382 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
383 .eesipr_value = 0x01ff009f,
384
385 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
386 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
387 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
388 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
389
390 .apr = 1,
391 .mpr = 1,
392 .tpauser = 1,
393 .hw_swap = 1,
394};
395#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000396
397static void sh_eth_set_rate(struct net_device *ndev)
398{
399 struct sh_eth_private *mdp = netdev_priv(ndev);
400
401 switch (mdp->speed) {
402 case 10: /* 10BASE */
403 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
404 break;
405 case 100:/* 100BASE */
406 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000407 break;
408 default:
409 break;
410 }
411}
412
413/* SH7724 */
414static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
415 .set_duplex = sh_eth_set_duplex,
416 .set_rate = sh_eth_set_rate,
417
418 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
419 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
420 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
421
422 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
423 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
424 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
425 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
426
427 .apr = 1,
428 .mpr = 1,
429 .tpauser = 1,
430 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800431 .rpadir = 1,
432 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000433};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000434#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000435#define SH_ETH_HAS_BOTH_MODULES 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000436
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000437static void sh_eth_set_rate(struct net_device *ndev)
438{
439 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000440
441 switch (mdp->speed) {
442 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000443 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000444 break;
445 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000446 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000447 break;
448 default:
449 break;
450 }
451}
452
453/* SH7757 */
454static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
455 .set_duplex = sh_eth_set_duplex,
456 .set_rate = sh_eth_set_rate,
457
458 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
459 .rmcr_value = 0x00000001,
460
461 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
462 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
463 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
464 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
465
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000466 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000467 .apr = 1,
468 .mpr = 1,
469 .tpauser = 1,
470 .hw_swap = 1,
471 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000472 .rpadir = 1,
473 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000474};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000475
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000476#define SH_GIGA_ETH_BASE 0xfee00000
477#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
478#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
479static void sh_eth_chip_reset_giga(struct net_device *ndev)
480{
481 int i;
482 unsigned long mahr[2], malr[2];
483
484 /* save MAHR and MALR */
485 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000486 malr[i] = ioread32((void *)GIGA_MALR(i));
487 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000488 }
489
490 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000491 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000492 mdelay(1);
493
494 /* restore MAHR and MALR */
495 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000496 iowrite32(malr[i], (void *)GIGA_MALR(i));
497 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000498 }
499}
500
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000501static void sh_eth_set_rate_giga(struct net_device *ndev)
502{
503 struct sh_eth_private *mdp = netdev_priv(ndev);
504
505 switch (mdp->speed) {
506 case 10: /* 10BASE */
507 sh_eth_write(ndev, 0x00000000, GECMR);
508 break;
509 case 100:/* 100BASE */
510 sh_eth_write(ndev, 0x00000010, GECMR);
511 break;
512 case 1000: /* 1000BASE */
513 sh_eth_write(ndev, 0x00000020, GECMR);
514 break;
515 default:
516 break;
517 }
518}
519
520/* SH7757(GETHERC) */
521static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
522 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000523 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000524 .set_rate = sh_eth_set_rate_giga,
525
526 .ecsr_value = ECSR_ICD | ECSR_MPD,
527 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
528 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
529
530 .tx_check = EESR_TC1 | EESR_FTC,
531 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
532 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
533 EESR_ECI,
534 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
535 EESR_TFE,
536 .fdr_value = 0x0000072f,
537 .rmcr_value = 0x00000001,
538
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000539 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000540 .apr = 1,
541 .mpr = 1,
542 .tpauser = 1,
543 .bculr = 1,
544 .hw_swap = 1,
545 .rpadir = 1,
546 .rpadir_value = 2 << 16,
547 .no_trimd = 1,
548 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000549 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000550};
551
552static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
553{
554 if (sh_eth_is_gether(mdp))
555 return &sh_eth_my_cpu_data_giga;
556 else
557 return &sh_eth_my_cpu_data;
558}
559
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000560#elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000561
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000562static void sh_eth_chip_reset(struct net_device *ndev)
563{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000564 struct sh_eth_private *mdp = netdev_priv(ndev);
565
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000566 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000567 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000568 mdelay(1);
569}
570
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000571static void sh_eth_set_rate(struct net_device *ndev)
572{
573 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000574
575 switch (mdp->speed) {
576 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000577 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000578 break;
579 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000580 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000581 break;
582 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000583 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000584 break;
585 default:
586 break;
587 }
588}
589
590/* sh7763 */
591static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
592 .chip_reset = sh_eth_chip_reset,
593 .set_duplex = sh_eth_set_duplex,
594 .set_rate = sh_eth_set_rate,
595
596 .ecsr_value = ECSR_ICD | ECSR_MPD,
597 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
598 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
599
600 .tx_check = EESR_TC1 | EESR_FTC,
601 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
602 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
603 EESR_ECI,
604 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
605 EESR_TFE,
606
607 .apr = 1,
608 .mpr = 1,
609 .tpauser = 1,
610 .bculr = 1,
611 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000612 .no_trimd = 1,
613 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000614 .tsu = 1,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000615#if defined(CONFIG_CPU_SUBTYPE_SH7734)
616 .hw_crc = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000617 .select_mii = 1,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000618#else
619 .irq_flags = IRQF_SHARED,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000620#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000621};
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000622#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000623
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000624static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000625{
626 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000627
628 /* reset device */
629 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
630 mdelay(1);
631
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000632 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000633}
634
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000635static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000636{
637 struct sh_eth_private *mdp = netdev_priv(ndev);
638
639 switch (mdp->speed) {
640 case 10: /* 10BASE */
641 sh_eth_write(ndev, GECMR_10, GECMR);
642 break;
643 case 100:/* 100BASE */
644 sh_eth_write(ndev, GECMR_100, GECMR);
645 break;
646 case 1000: /* 1000BASE */
647 sh_eth_write(ndev, GECMR_1000, GECMR);
648 break;
649 default:
650 break;
651 }
652}
653
654/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000655static struct sh_eth_cpu_data r8a7740_data = {
656 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000657 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000658 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000659
660 .ecsr_value = ECSR_ICD | ECSR_MPD,
661 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
662 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
663
664 .tx_check = EESR_TC1 | EESR_FTC,
665 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
666 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
667 EESR_ECI,
668 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
669 EESR_TFE,
670
671 .apr = 1,
672 .mpr = 1,
673 .tpauser = 1,
674 .bculr = 1,
675 .hw_swap = 1,
676 .no_trimd = 1,
677 .no_ade = 1,
678 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000679 .select_mii = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000680};
681
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000682static struct sh_eth_cpu_data sh7619_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000683 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
684
685 .apr = 1,
686 .mpr = 1,
687 .tpauser = 1,
688 .hw_swap = 1,
689};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000690
691static struct sh_eth_cpu_data sh771x_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000692 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000693 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000694};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000695
696static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
697{
698 if (!cd->ecsr_value)
699 cd->ecsr_value = DEFAULT_ECSR_INIT;
700
701 if (!cd->ecsipr_value)
702 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
703
704 if (!cd->fcftr_value)
705 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
706 DEFAULT_FIFO_F_D_RFD;
707
708 if (!cd->fdr_value)
709 cd->fdr_value = DEFAULT_FDR_INIT;
710
711 if (!cd->rmcr_value)
712 cd->rmcr_value = DEFAULT_RMCR_VALUE;
713
714 if (!cd->tx_check)
715 cd->tx_check = DEFAULT_TX_CHECK;
716
717 if (!cd->eesr_err_check)
718 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
719
720 if (!cd->tx_error_check)
721 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
722}
723
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000724static int sh_eth_check_reset(struct net_device *ndev)
725{
726 int ret = 0;
727 int cnt = 100;
728
729 while (cnt > 0) {
730 if (!(sh_eth_read(ndev, EDMR) & 0x3))
731 break;
732 mdelay(1);
733 cnt--;
734 }
735 if (cnt < 0) {
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +0000736 pr_err("Device reset fail\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000737 ret = -ETIMEDOUT;
738 }
739 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000740}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000741
742static int sh_eth_reset(struct net_device *ndev)
743{
744 struct sh_eth_private *mdp = netdev_priv(ndev);
745 int ret = 0;
746
747 if (sh_eth_is_gether(mdp)) {
748 sh_eth_write(ndev, EDSR_ENALL, EDSR);
749 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
750 EDMR);
751
752 ret = sh_eth_check_reset(ndev);
753 if (ret)
754 goto out;
755
756 /* Table Init */
757 sh_eth_write(ndev, 0x0, TDLAR);
758 sh_eth_write(ndev, 0x0, TDFAR);
759 sh_eth_write(ndev, 0x0, TDFXR);
760 sh_eth_write(ndev, 0x0, TDFFR);
761 sh_eth_write(ndev, 0x0, RDLAR);
762 sh_eth_write(ndev, 0x0, RDFAR);
763 sh_eth_write(ndev, 0x0, RDFXR);
764 sh_eth_write(ndev, 0x0, RDFFR);
765
766 /* Reset HW CRC register */
767 if (mdp->cd->hw_crc)
768 sh_eth_write(ndev, 0x0, CSMR);
769
770 /* Select MII mode */
771 if (mdp->cd->select_mii)
772 sh_eth_select_mii(ndev);
773 } else {
774 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
775 EDMR);
776 mdelay(3);
777 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
778 EDMR);
779 }
780
781out:
782 return ret;
783}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000784
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000785#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000786static void sh_eth_set_receive_align(struct sk_buff *skb)
787{
788 int reserve;
789
790 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
791 if (reserve)
792 skb_reserve(skb, reserve);
793}
794#else
795static void sh_eth_set_receive_align(struct sk_buff *skb)
796{
797 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
798}
799#endif
800
801
Yoshinori Sato71557a32008-08-06 19:49:00 -0400802/* CPU <-> EDMAC endian convert */
803static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
804{
805 switch (mdp->edmac_endian) {
806 case EDMAC_LITTLE_ENDIAN:
807 return cpu_to_le32(x);
808 case EDMAC_BIG_ENDIAN:
809 return cpu_to_be32(x);
810 }
811 return x;
812}
813
814static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
815{
816 switch (mdp->edmac_endian) {
817 case EDMAC_LITTLE_ENDIAN:
818 return le32_to_cpu(x);
819 case EDMAC_BIG_ENDIAN:
820 return be32_to_cpu(x);
821 }
822 return x;
823}
824
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700825/*
826 * Program the hardware MAC address from dev->dev_addr.
827 */
828static void update_mac_address(struct net_device *ndev)
829{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000830 sh_eth_write(ndev,
831 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
832 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
833 sh_eth_write(ndev,
834 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700835}
836
837/*
838 * Get MAC address from SuperH MAC address register
839 *
840 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
841 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
842 * When you want use this device, you must set MAC address in bootloader.
843 *
844 */
Magnus Damm748031f2009-10-09 00:17:14 +0000845static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700846{
Magnus Damm748031f2009-10-09 00:17:14 +0000847 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
848 memcpy(ndev->dev_addr, mac, 6);
849 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000850 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
851 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
852 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
853 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
854 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
855 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000856 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700857}
858
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000859static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
860{
861 if (sh_eth_is_gether(mdp))
862 return EDTRR_TRNS_GETHER;
863 else
864 return EDTRR_TRNS_ETHER;
865}
866
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700867struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000868 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700869 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000870 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700871 u32 mmd_msk;/* MMD */
872 u32 mdo_msk;
873 u32 mdi_msk;
874 u32 mdc_msk;
875};
876
877/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000878static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700879{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000880 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700881}
882
883/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000884static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700885{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000886 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700887}
888
889/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000890static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700891{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000892 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700893}
894
895/* Data I/O pin control */
896static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
897{
898 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000899
900 if (bitbang->set_gate)
901 bitbang->set_gate(bitbang->addr);
902
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700903 if (bit)
904 bb_set(bitbang->addr, bitbang->mmd_msk);
905 else
906 bb_clr(bitbang->addr, bitbang->mmd_msk);
907}
908
909/* Set bit data*/
910static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
911{
912 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
913
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000914 if (bitbang->set_gate)
915 bitbang->set_gate(bitbang->addr);
916
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700917 if (bit)
918 bb_set(bitbang->addr, bitbang->mdo_msk);
919 else
920 bb_clr(bitbang->addr, bitbang->mdo_msk);
921}
922
923/* Get bit data*/
924static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
925{
926 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000927
928 if (bitbang->set_gate)
929 bitbang->set_gate(bitbang->addr);
930
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700931 return bb_read(bitbang->addr, bitbang->mdi_msk);
932}
933
934/* MDC pin control */
935static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
936{
937 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
938
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000939 if (bitbang->set_gate)
940 bitbang->set_gate(bitbang->addr);
941
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700942 if (bit)
943 bb_set(bitbang->addr, bitbang->mdc_msk);
944 else
945 bb_clr(bitbang->addr, bitbang->mdc_msk);
946}
947
948/* mdio bus control struct */
949static struct mdiobb_ops bb_ops = {
950 .owner = THIS_MODULE,
951 .set_mdc = sh_mdc_ctrl,
952 .set_mdio_dir = sh_mmd_ctrl,
953 .set_mdio_data = sh_set_mdio,
954 .get_mdio_data = sh_get_mdio,
955};
956
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700957/* free skb and descriptor buffer */
958static void sh_eth_ring_free(struct net_device *ndev)
959{
960 struct sh_eth_private *mdp = netdev_priv(ndev);
961 int i;
962
963 /* Free Rx skb ringbuffer */
964 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000965 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700966 if (mdp->rx_skbuff[i])
967 dev_kfree_skb(mdp->rx_skbuff[i]);
968 }
969 }
970 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000971 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700972
973 /* Free Tx skb ringbuffer */
974 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000975 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700976 if (mdp->tx_skbuff[i])
977 dev_kfree_skb(mdp->tx_skbuff[i]);
978 }
979 }
980 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +0000981 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700982}
983
984/* format skb and descriptor buffer */
985static void sh_eth_ring_format(struct net_device *ndev)
986{
987 struct sh_eth_private *mdp = netdev_priv(ndev);
988 int i;
989 struct sk_buff *skb;
990 struct sh_eth_rxdesc *rxdesc = NULL;
991 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +0000992 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
993 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700994
995 mdp->cur_rx = mdp->cur_tx = 0;
996 mdp->dirty_rx = mdp->dirty_tx = 0;
997
998 memset(mdp->rx_ring, 0, rx_ringsize);
999
1000 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001001 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001002 /* skb */
1003 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001004 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001005 mdp->rx_skbuff[i] = skb;
1006 if (skb == NULL)
1007 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001008 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001009 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001010 sh_eth_set_receive_align(skb);
1011
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001012 /* RX descriptor */
1013 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001014 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -04001015 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016
1017 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001018 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001019 /* Rx descriptor address set */
1020 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001021 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001022 if (sh_eth_is_gether(mdp))
1023 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001024 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001025 }
1026
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001027 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001028
1029 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001030 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001031
1032 memset(mdp->tx_ring, 0, tx_ringsize);
1033
1034 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001035 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001036 mdp->tx_skbuff[i] = NULL;
1037 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001038 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001039 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001040 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001041 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001042 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001043 if (sh_eth_is_gether(mdp))
1044 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001045 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001046 }
1047
Yoshinori Sato71557a32008-08-06 19:49:00 -04001048 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001049}
1050
1051/* Get skb and descriptor buffer */
1052static int sh_eth_ring_init(struct net_device *ndev)
1053{
1054 struct sh_eth_private *mdp = netdev_priv(ndev);
1055 int rx_ringsize, tx_ringsize, ret = 0;
1056
1057 /*
1058 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1059 * card needs room to do 8 byte alignment, +2 so we can reserve
1060 * the first 2 bytes, and +16 gets room for the status word from the
1061 * card.
1062 */
1063 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1064 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001065 if (mdp->cd->rpadir)
1066 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001067
1068 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001069 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1070 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001071 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001072 ret = -ENOMEM;
1073 return ret;
1074 }
1075
Joe Perchesb2adaca2013-02-03 17:43:58 +00001076 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1077 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001078 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001079 ret = -ENOMEM;
1080 goto skb_ring_free;
1081 }
1082
1083 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001084 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001085 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001086 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001087 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001088 ret = -ENOMEM;
1089 goto desc_ring_free;
1090 }
1091
1092 mdp->dirty_rx = 0;
1093
1094 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001095 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001096 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001097 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001098 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001099 ret = -ENOMEM;
1100 goto desc_ring_free;
1101 }
1102 return ret;
1103
1104desc_ring_free:
1105 /* free DMA buffer */
1106 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1107
1108skb_ring_free:
1109 /* Free Rx and Tx skb ring buffer */
1110 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001111 mdp->tx_ring = NULL;
1112 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001113
1114 return ret;
1115}
1116
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001117static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1118{
1119 int ringsize;
1120
1121 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001122 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001123 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1124 mdp->rx_desc_dma);
1125 mdp->rx_ring = NULL;
1126 }
1127
1128 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001129 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001130 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1131 mdp->tx_desc_dma);
1132 mdp->tx_ring = NULL;
1133 }
1134}
1135
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001136static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001137{
1138 int ret = 0;
1139 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001140 u32 val;
1141
1142 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001143 ret = sh_eth_reset(ndev);
1144 if (ret)
1145 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001146
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001147 /* Descriptor format */
1148 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001149 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001150 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001151
1152 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001153 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001154
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001155#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001156 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001157 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001158 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001159#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001160 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001161
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001162 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001163 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1164 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001165
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001166 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001167 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001168
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001169 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001171 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001172 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001173
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001174 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001175
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001176 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001177 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001178
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001179 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001180 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1181 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001182
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001183 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001184 if (start)
1185 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001186
1187 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001188 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001189 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1190
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001191 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001192
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001193 if (mdp->cd->set_rate)
1194 mdp->cd->set_rate(ndev);
1195
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001196 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001197 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001198
1199 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001200 if (start)
1201 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001202
1203 /* Set MAC address */
1204 update_mac_address(ndev);
1205
1206 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001207 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001208 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001209 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001210 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001211 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001212 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001213
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001214 if (start) {
1215 /* Setting the Rx mode will start the Rx process. */
1216 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001217
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001218 netif_start_queue(ndev);
1219 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001221out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001222 return ret;
1223}
1224
1225/* free Tx skb function */
1226static int sh_eth_txfree(struct net_device *ndev)
1227{
1228 struct sh_eth_private *mdp = netdev_priv(ndev);
1229 struct sh_eth_txdesc *txdesc;
1230 int freeNum = 0;
1231 int entry = 0;
1232
1233 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001234 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001235 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001236 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001237 break;
1238 /* Free the original skb. */
1239 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001240 dma_unmap_single(&ndev->dev, txdesc->addr,
1241 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001242 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1243 mdp->tx_skbuff[entry] = NULL;
1244 freeNum++;
1245 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001246 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001247 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001248 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001249
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001250 ndev->stats.tx_packets++;
1251 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001252 }
1253 return freeNum;
1254}
1255
1256/* Packet receive function */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001257static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001258{
1259 struct sh_eth_private *mdp = netdev_priv(ndev);
1260 struct sh_eth_rxdesc *rxdesc;
1261
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001262 int entry = mdp->cur_rx % mdp->num_rx_ring;
1263 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001264 struct sk_buff *skb;
1265 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001266 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267
1268 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001269 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1270 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001271 pkt_len = rxdesc->frame_length;
1272
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +00001273#if defined(CONFIG_ARCH_R8A7740)
1274 desc_status >>= 16;
1275#endif
1276
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001277 if (--boguscnt < 0)
1278 break;
1279
1280 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001281 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001282
1283 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1284 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001285 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001286 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001287 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001288 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001289 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001290 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001291 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001292 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001293 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001294 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001295 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001296 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001297 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001298 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001299 if (!mdp->cd->hw_swap)
1300 sh_eth_soft_swap(
1301 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1302 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001303 skb = mdp->rx_skbuff[entry];
1304 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001305 if (mdp->cd->rpadir)
1306 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001307 skb_put(skb, pkt_len);
1308 skb->protocol = eth_type_trans(skb, ndev);
1309 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001310 ndev->stats.rx_packets++;
1311 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001312 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001313 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001314 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001315 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001316 }
1317
1318 /* Refill the Rx ring buffers. */
1319 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001320 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001321 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001322 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001323 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001324
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001325 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001326 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001327 mdp->rx_skbuff[entry] = skb;
1328 if (skb == NULL)
1329 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001330 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001331 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001332 sh_eth_set_receive_align(skb);
1333
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001334 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001335 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001336 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001337 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001338 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001339 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001340 else
1341 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001342 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001343 }
1344
1345 /* Restart Rx engine if stopped. */
1346 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001347 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001348 /* fix the values for the next receiving if RDE is set */
1349 if (intr_status & EESR_RDE)
1350 mdp->cur_rx = mdp->dirty_rx =
1351 (sh_eth_read(ndev, RDFAR) -
1352 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001353 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001354 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001355
1356 return 0;
1357}
1358
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001359static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001360{
1361 /* disable 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
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001366static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001367{
1368 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001369 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1370 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001371}
1372
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001373/* error control function */
1374static void sh_eth_error(struct net_device *ndev, int intr_status)
1375{
1376 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001378 u32 link_stat;
1379 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001380
1381 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001382 felic_stat = sh_eth_read(ndev, ECSR);
1383 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001384 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001385 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001386 if (felic_stat & ECSR_LCHNG) {
1387 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001388 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001389 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001390 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001391 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001392 if (mdp->ether_link_active_low)
1393 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001394 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001395 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001396 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001397 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001398 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001399 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1400 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001402 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1403 ECSR);
1404 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1405 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001406 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001407 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001408 }
1409 }
1410 }
1411
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001412ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001413 if (intr_status & EESR_TWB) {
1414 /* Write buck end. unused write back interrupt */
1415 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001416 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001417 if (netif_msg_tx_err(mdp))
1418 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001419 }
1420
1421 if (intr_status & EESR_RABT) {
1422 /* Receive Abort int */
1423 if (intr_status & EESR_RFRMER) {
1424 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001425 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001426 if (netif_msg_rx_err(mdp))
1427 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428 }
1429 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001430
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001431 if (intr_status & EESR_TDE) {
1432 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001433 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001434 if (netif_msg_tx_err(mdp))
1435 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1436 }
1437
1438 if (intr_status & EESR_TFE) {
1439 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001440 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001441 if (netif_msg_tx_err(mdp))
1442 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001443 }
1444
1445 if (intr_status & EESR_RDE) {
1446 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001447 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001449 if (netif_msg_rx_err(mdp))
1450 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001451 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001452
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001453 if (intr_status & EESR_RFE) {
1454 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001455 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001456 if (netif_msg_rx_err(mdp))
1457 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1458 }
1459
1460 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1461 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001462 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001463 if (netif_msg_tx_err(mdp))
1464 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001465 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001466
1467 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1468 if (mdp->cd->no_ade)
1469 mask &= ~EESR_ADE;
1470 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001471 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001472 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001473 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001474 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1475 intr_status, mdp->cur_tx);
1476 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477 mdp->dirty_tx, (u32) ndev->state, edtrr);
1478 /* dirty buffer free */
1479 sh_eth_txfree(ndev);
1480
1481 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001482 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001484 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001485 }
1486 /* wakeup */
1487 netif_wake_queue(ndev);
1488 }
1489}
1490
1491static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1492{
1493 struct net_device *ndev = netdev;
1494 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001495 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001496 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001497 unsigned long intr_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001498
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001499 spin_lock(&mdp->lock);
1500
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001501 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001502 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001503 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1504 * enabled since it's the one that comes thru regardless of the mask,
1505 * and we need to fully handle it in sh_eth_error() in order to quench
1506 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1507 */
1508 intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001509 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001510 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1511 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001512 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001513 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001514 ret = IRQ_HANDLED;
1515 } else
1516 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001517
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001518 if (intr_status & (EESR_FRC | /* Frame recv*/
1519 EESR_RMAF | /* Multi cast address recv*/
1520 EESR_RRF | /* Bit frame recv */
1521 EESR_RTLF | /* Long frame recv*/
1522 EESR_RTSF | /* short frame recv */
1523 EESR_PRE | /* PHY-LSI recv error */
1524 EESR_CERF)){ /* recv frame CRC error */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001525 sh_eth_rx(ndev, intr_status);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001526 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001527
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001528 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001529 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001530 sh_eth_txfree(ndev);
1531 netif_wake_queue(ndev);
1532 }
1533
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001534 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535 sh_eth_error(ndev, intr_status);
1536
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001537other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001538 spin_unlock(&mdp->lock);
1539
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001540 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001541}
1542
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001543/* PHY state control function */
1544static void sh_eth_adjust_link(struct net_device *ndev)
1545{
1546 struct sh_eth_private *mdp = netdev_priv(ndev);
1547 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001548 int new_state = 0;
1549
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001550 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551 if (phydev->duplex != mdp->duplex) {
1552 new_state = 1;
1553 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001554 if (mdp->cd->set_duplex)
1555 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001556 }
1557
1558 if (phydev->speed != mdp->speed) {
1559 new_state = 1;
1560 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001561 if (mdp->cd->set_rate)
1562 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001563 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001564 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001565 sh_eth_write(ndev,
1566 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001567 new_state = 1;
1568 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001569 if (mdp->cd->no_psr || mdp->no_ether_link)
1570 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001571 }
1572 } else if (mdp->link) {
1573 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001574 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575 mdp->speed = 0;
1576 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001577 if (mdp->cd->no_psr || mdp->no_ether_link)
1578 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579 }
1580
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001581 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001582 phy_print_status(phydev);
1583}
1584
1585/* PHY init function */
1586static int sh_eth_phy_init(struct net_device *ndev)
1587{
1588 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001589 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001590 struct phy_device *phydev = NULL;
1591
Kay Sieversfb28ad32008-11-10 13:55:14 -08001592 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001593 mdp->mii_bus->id , mdp->phy_id);
1594
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001595 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001596 mdp->speed = 0;
1597 mdp->duplex = -1;
1598
1599 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001600 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001601 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001602 if (IS_ERR(phydev)) {
1603 dev_err(&ndev->dev, "phy_connect failed\n");
1604 return PTR_ERR(phydev);
1605 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001606
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001607 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001608 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001609
1610 mdp->phydev = phydev;
1611
1612 return 0;
1613}
1614
1615/* PHY control start function */
1616static int sh_eth_phy_start(struct net_device *ndev)
1617{
1618 struct sh_eth_private *mdp = netdev_priv(ndev);
1619 int ret;
1620
1621 ret = sh_eth_phy_init(ndev);
1622 if (ret)
1623 return ret;
1624
1625 /* reset phy - this also wakes it from PDOWN */
1626 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1627 phy_start(mdp->phydev);
1628
1629 return 0;
1630}
1631
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001632static int sh_eth_get_settings(struct net_device *ndev,
1633 struct ethtool_cmd *ecmd)
1634{
1635 struct sh_eth_private *mdp = netdev_priv(ndev);
1636 unsigned long flags;
1637 int ret;
1638
1639 spin_lock_irqsave(&mdp->lock, flags);
1640 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1641 spin_unlock_irqrestore(&mdp->lock, flags);
1642
1643 return ret;
1644}
1645
1646static int sh_eth_set_settings(struct net_device *ndev,
1647 struct ethtool_cmd *ecmd)
1648{
1649 struct sh_eth_private *mdp = netdev_priv(ndev);
1650 unsigned long flags;
1651 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001652
1653 spin_lock_irqsave(&mdp->lock, flags);
1654
1655 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001656 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001657
1658 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1659 if (ret)
1660 goto error_exit;
1661
1662 if (ecmd->duplex == DUPLEX_FULL)
1663 mdp->duplex = 1;
1664 else
1665 mdp->duplex = 0;
1666
1667 if (mdp->cd->set_duplex)
1668 mdp->cd->set_duplex(ndev);
1669
1670error_exit:
1671 mdelay(1);
1672
1673 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001674 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001675
1676 spin_unlock_irqrestore(&mdp->lock, flags);
1677
1678 return ret;
1679}
1680
1681static int sh_eth_nway_reset(struct net_device *ndev)
1682{
1683 struct sh_eth_private *mdp = netdev_priv(ndev);
1684 unsigned long flags;
1685 int ret;
1686
1687 spin_lock_irqsave(&mdp->lock, flags);
1688 ret = phy_start_aneg(mdp->phydev);
1689 spin_unlock_irqrestore(&mdp->lock, flags);
1690
1691 return ret;
1692}
1693
1694static u32 sh_eth_get_msglevel(struct net_device *ndev)
1695{
1696 struct sh_eth_private *mdp = netdev_priv(ndev);
1697 return mdp->msg_enable;
1698}
1699
1700static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1701{
1702 struct sh_eth_private *mdp = netdev_priv(ndev);
1703 mdp->msg_enable = value;
1704}
1705
1706static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1707 "rx_current", "tx_current",
1708 "rx_dirty", "tx_dirty",
1709};
1710#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1711
1712static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1713{
1714 switch (sset) {
1715 case ETH_SS_STATS:
1716 return SH_ETH_STATS_LEN;
1717 default:
1718 return -EOPNOTSUPP;
1719 }
1720}
1721
1722static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1723 struct ethtool_stats *stats, u64 *data)
1724{
1725 struct sh_eth_private *mdp = netdev_priv(ndev);
1726 int i = 0;
1727
1728 /* device-specific stats */
1729 data[i++] = mdp->cur_rx;
1730 data[i++] = mdp->cur_tx;
1731 data[i++] = mdp->dirty_rx;
1732 data[i++] = mdp->dirty_tx;
1733}
1734
1735static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1736{
1737 switch (stringset) {
1738 case ETH_SS_STATS:
1739 memcpy(data, *sh_eth_gstrings_stats,
1740 sizeof(sh_eth_gstrings_stats));
1741 break;
1742 }
1743}
1744
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001745static void sh_eth_get_ringparam(struct net_device *ndev,
1746 struct ethtool_ringparam *ring)
1747{
1748 struct sh_eth_private *mdp = netdev_priv(ndev);
1749
1750 ring->rx_max_pending = RX_RING_MAX;
1751 ring->tx_max_pending = TX_RING_MAX;
1752 ring->rx_pending = mdp->num_rx_ring;
1753 ring->tx_pending = mdp->num_tx_ring;
1754}
1755
1756static int sh_eth_set_ringparam(struct net_device *ndev,
1757 struct ethtool_ringparam *ring)
1758{
1759 struct sh_eth_private *mdp = netdev_priv(ndev);
1760 int ret;
1761
1762 if (ring->tx_pending > TX_RING_MAX ||
1763 ring->rx_pending > RX_RING_MAX ||
1764 ring->tx_pending < TX_RING_MIN ||
1765 ring->rx_pending < RX_RING_MIN)
1766 return -EINVAL;
1767 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1768 return -EINVAL;
1769
1770 if (netif_running(ndev)) {
1771 netif_tx_disable(ndev);
1772 /* Disable interrupts by clearing the interrupt mask. */
1773 sh_eth_write(ndev, 0x0000, EESIPR);
1774 /* Stop the chip's Tx and Rx processes. */
1775 sh_eth_write(ndev, 0, EDTRR);
1776 sh_eth_write(ndev, 0, EDRRR);
1777 synchronize_irq(ndev->irq);
1778 }
1779
1780 /* Free all the skbuffs in the Rx queue. */
1781 sh_eth_ring_free(ndev);
1782 /* Free DMA buffer */
1783 sh_eth_free_dma_buffer(mdp);
1784
1785 /* Set new parameters */
1786 mdp->num_rx_ring = ring->rx_pending;
1787 mdp->num_tx_ring = ring->tx_pending;
1788
1789 ret = sh_eth_ring_init(ndev);
1790 if (ret < 0) {
1791 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1792 return ret;
1793 }
1794 ret = sh_eth_dev_init(ndev, false);
1795 if (ret < 0) {
1796 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1797 return ret;
1798 }
1799
1800 if (netif_running(ndev)) {
1801 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1802 /* Setting the Rx mode will start the Rx process. */
1803 sh_eth_write(ndev, EDRRR_R, EDRRR);
1804 netif_wake_queue(ndev);
1805 }
1806
1807 return 0;
1808}
1809
stephen hemminger9b07be42012-01-04 12:59:49 +00001810static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001811 .get_settings = sh_eth_get_settings,
1812 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001813 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001814 .get_msglevel = sh_eth_get_msglevel,
1815 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001816 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001817 .get_strings = sh_eth_get_strings,
1818 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1819 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001820 .get_ringparam = sh_eth_get_ringparam,
1821 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001822};
1823
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001824/* network device open function */
1825static int sh_eth_open(struct net_device *ndev)
1826{
1827 int ret = 0;
1828 struct sh_eth_private *mdp = netdev_priv(ndev);
1829
Magnus Dammbcd51492009-10-09 00:20:04 +00001830 pm_runtime_get_sync(&mdp->pdev->dev);
1831
Joe Perchesa0607fd2009-11-18 23:29:17 -08001832 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001833 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001834 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001835 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001836 return ret;
1837 }
1838
1839 /* Descriptor set */
1840 ret = sh_eth_ring_init(ndev);
1841 if (ret)
1842 goto out_free_irq;
1843
1844 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001845 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001846 if (ret)
1847 goto out_free_irq;
1848
1849 /* PHY control start*/
1850 ret = sh_eth_phy_start(ndev);
1851 if (ret)
1852 goto out_free_irq;
1853
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001854 return ret;
1855
1856out_free_irq:
1857 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001858 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001859 return ret;
1860}
1861
1862/* Timeout function */
1863static void sh_eth_tx_timeout(struct net_device *ndev)
1864{
1865 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001866 struct sh_eth_rxdesc *rxdesc;
1867 int i;
1868
1869 netif_stop_queue(ndev);
1870
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001871 if (netif_msg_timer(mdp))
1872 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001873 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001874
1875 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001876 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001877
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001878 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001879 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001880 rxdesc = &mdp->rx_ring[i];
1881 rxdesc->status = 0;
1882 rxdesc->addr = 0xBADF00D0;
1883 if (mdp->rx_skbuff[i])
1884 dev_kfree_skb(mdp->rx_skbuff[i]);
1885 mdp->rx_skbuff[i] = NULL;
1886 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001887 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001888 if (mdp->tx_skbuff[i])
1889 dev_kfree_skb(mdp->tx_skbuff[i]);
1890 mdp->tx_skbuff[i] = NULL;
1891 }
1892
1893 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001894 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001895}
1896
1897/* Packet transmit function */
1898static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1899{
1900 struct sh_eth_private *mdp = netdev_priv(ndev);
1901 struct sh_eth_txdesc *txdesc;
1902 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001903 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001904
1905 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001906 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001907 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001908 if (netif_msg_tx_queued(mdp))
1909 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001910 netif_stop_queue(ndev);
1911 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001912 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001913 }
1914 }
1915 spin_unlock_irqrestore(&mdp->lock, flags);
1916
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001917 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001918 mdp->tx_skbuff[entry] = skb;
1919 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001920 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001921 if (!mdp->cd->hw_swap)
1922 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1923 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001924 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1925 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001926 if (skb->len < ETHERSMALL)
1927 txdesc->buffer_length = ETHERSMALL;
1928 else
1929 txdesc->buffer_length = skb->len;
1930
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001931 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001932 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001933 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001934 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001935
1936 mdp->cur_tx++;
1937
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001938 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1939 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001940
Patrick McHardy6ed10652009-06-23 06:03:08 +00001941 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001942}
1943
1944/* device close function */
1945static int sh_eth_close(struct net_device *ndev)
1946{
1947 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001948
1949 netif_stop_queue(ndev);
1950
1951 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001952 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001953
1954 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001955 sh_eth_write(ndev, 0, EDTRR);
1956 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001957
1958 /* PHY Disconnect */
1959 if (mdp->phydev) {
1960 phy_stop(mdp->phydev);
1961 phy_disconnect(mdp->phydev);
1962 }
1963
1964 free_irq(ndev->irq, ndev);
1965
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001966 /* Free all the skbuffs in the Rx queue. */
1967 sh_eth_ring_free(ndev);
1968
1969 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001970 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001971
Magnus Dammbcd51492009-10-09 00:20:04 +00001972 pm_runtime_put_sync(&mdp->pdev->dev);
1973
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001974 return 0;
1975}
1976
1977static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1978{
1979 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001980
Magnus Dammbcd51492009-10-09 00:20:04 +00001981 pm_runtime_get_sync(&mdp->pdev->dev);
1982
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001983 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001984 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001985 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001986 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001987 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001988 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001989 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001990 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001991 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001992 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001993 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1994 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001995 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001996 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1997 }
Magnus Dammbcd51492009-10-09 00:20:04 +00001998 pm_runtime_put_sync(&mdp->pdev->dev);
1999
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002000 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002001}
2002
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002003/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002004static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
2005 int cmd)
2006{
2007 struct sh_eth_private *mdp = netdev_priv(ndev);
2008 struct phy_device *phydev = mdp->phydev;
2009
2010 if (!netif_running(ndev))
2011 return -EINVAL;
2012
2013 if (!phydev)
2014 return -ENODEV;
2015
Richard Cochran28b04112010-07-17 08:48:55 +00002016 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002017}
2018
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002019/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2020static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2021 int entry)
2022{
2023 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2024}
2025
2026static u32 sh_eth_tsu_get_post_mask(int entry)
2027{
2028 return 0x0f << (28 - ((entry % 8) * 4));
2029}
2030
2031static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2032{
2033 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2034}
2035
2036static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2037 int entry)
2038{
2039 struct sh_eth_private *mdp = netdev_priv(ndev);
2040 u32 tmp;
2041 void *reg_offset;
2042
2043 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2044 tmp = ioread32(reg_offset);
2045 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2046}
2047
2048static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2049 int entry)
2050{
2051 struct sh_eth_private *mdp = netdev_priv(ndev);
2052 u32 post_mask, ref_mask, tmp;
2053 void *reg_offset;
2054
2055 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2056 post_mask = sh_eth_tsu_get_post_mask(entry);
2057 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2058
2059 tmp = ioread32(reg_offset);
2060 iowrite32(tmp & ~post_mask, reg_offset);
2061
2062 /* If other port enables, the function returns "true" */
2063 return tmp & ref_mask;
2064}
2065
2066static int sh_eth_tsu_busy(struct net_device *ndev)
2067{
2068 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2069 struct sh_eth_private *mdp = netdev_priv(ndev);
2070
2071 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2072 udelay(10);
2073 timeout--;
2074 if (timeout <= 0) {
2075 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2076 return -ETIMEDOUT;
2077 }
2078 }
2079
2080 return 0;
2081}
2082
2083static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2084 const u8 *addr)
2085{
2086 u32 val;
2087
2088 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2089 iowrite32(val, reg);
2090 if (sh_eth_tsu_busy(ndev) < 0)
2091 return -EBUSY;
2092
2093 val = addr[4] << 8 | addr[5];
2094 iowrite32(val, reg + 4);
2095 if (sh_eth_tsu_busy(ndev) < 0)
2096 return -EBUSY;
2097
2098 return 0;
2099}
2100
2101static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2102{
2103 u32 val;
2104
2105 val = ioread32(reg);
2106 addr[0] = (val >> 24) & 0xff;
2107 addr[1] = (val >> 16) & 0xff;
2108 addr[2] = (val >> 8) & 0xff;
2109 addr[3] = val & 0xff;
2110 val = ioread32(reg + 4);
2111 addr[4] = (val >> 8) & 0xff;
2112 addr[5] = val & 0xff;
2113}
2114
2115
2116static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2117{
2118 struct sh_eth_private *mdp = netdev_priv(ndev);
2119 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2120 int i;
2121 u8 c_addr[ETH_ALEN];
2122
2123 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2124 sh_eth_tsu_read_entry(reg_offset, c_addr);
2125 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2126 return i;
2127 }
2128
2129 return -ENOENT;
2130}
2131
2132static int sh_eth_tsu_find_empty(struct net_device *ndev)
2133{
2134 u8 blank[ETH_ALEN];
2135 int entry;
2136
2137 memset(blank, 0, sizeof(blank));
2138 entry = sh_eth_tsu_find_entry(ndev, blank);
2139 return (entry < 0) ? -ENOMEM : entry;
2140}
2141
2142static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2143 int entry)
2144{
2145 struct sh_eth_private *mdp = netdev_priv(ndev);
2146 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2147 int ret;
2148 u8 blank[ETH_ALEN];
2149
2150 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2151 ~(1 << (31 - entry)), TSU_TEN);
2152
2153 memset(blank, 0, sizeof(blank));
2154 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2155 if (ret < 0)
2156 return ret;
2157 return 0;
2158}
2159
2160static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2161{
2162 struct sh_eth_private *mdp = netdev_priv(ndev);
2163 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2164 int i, ret;
2165
2166 if (!mdp->cd->tsu)
2167 return 0;
2168
2169 i = sh_eth_tsu_find_entry(ndev, addr);
2170 if (i < 0) {
2171 /* No entry found, create one */
2172 i = sh_eth_tsu_find_empty(ndev);
2173 if (i < 0)
2174 return -ENOMEM;
2175 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2176 if (ret < 0)
2177 return ret;
2178
2179 /* Enable the entry */
2180 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2181 (1 << (31 - i)), TSU_TEN);
2182 }
2183
2184 /* Entry found or created, enable POST */
2185 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2186
2187 return 0;
2188}
2189
2190static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2191{
2192 struct sh_eth_private *mdp = netdev_priv(ndev);
2193 int i, ret;
2194
2195 if (!mdp->cd->tsu)
2196 return 0;
2197
2198 i = sh_eth_tsu_find_entry(ndev, addr);
2199 if (i) {
2200 /* Entry found */
2201 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2202 goto done;
2203
2204 /* Disable the entry if both ports was disabled */
2205 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2206 if (ret < 0)
2207 return ret;
2208 }
2209done:
2210 return 0;
2211}
2212
2213static int sh_eth_tsu_purge_all(struct net_device *ndev)
2214{
2215 struct sh_eth_private *mdp = netdev_priv(ndev);
2216 int i, ret;
2217
2218 if (unlikely(!mdp->cd->tsu))
2219 return 0;
2220
2221 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2222 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2223 continue;
2224
2225 /* Disable the entry if both ports was disabled */
2226 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2227 if (ret < 0)
2228 return ret;
2229 }
2230
2231 return 0;
2232}
2233
2234static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2235{
2236 struct sh_eth_private *mdp = netdev_priv(ndev);
2237 u8 addr[ETH_ALEN];
2238 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2239 int i;
2240
2241 if (unlikely(!mdp->cd->tsu))
2242 return;
2243
2244 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2245 sh_eth_tsu_read_entry(reg_offset, addr);
2246 if (is_multicast_ether_addr(addr))
2247 sh_eth_tsu_del_entry(ndev, addr);
2248 }
2249}
2250
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002251/* Multicast reception directions set */
2252static void sh_eth_set_multicast_list(struct net_device *ndev)
2253{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002254 struct sh_eth_private *mdp = netdev_priv(ndev);
2255 u32 ecmr_bits;
2256 int mcast_all = 0;
2257 unsigned long flags;
2258
2259 spin_lock_irqsave(&mdp->lock, flags);
2260 /*
2261 * Initial condition is MCT = 1, PRM = 0.
2262 * Depending on ndev->flags, set PRM or clear MCT
2263 */
2264 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2265
2266 if (!(ndev->flags & IFF_MULTICAST)) {
2267 sh_eth_tsu_purge_mcast(ndev);
2268 mcast_all = 1;
2269 }
2270 if (ndev->flags & IFF_ALLMULTI) {
2271 sh_eth_tsu_purge_mcast(ndev);
2272 ecmr_bits &= ~ECMR_MCT;
2273 mcast_all = 1;
2274 }
2275
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002276 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002277 sh_eth_tsu_purge_all(ndev);
2278 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2279 } else if (mdp->cd->tsu) {
2280 struct netdev_hw_addr *ha;
2281 netdev_for_each_mc_addr(ha, ndev) {
2282 if (mcast_all && is_multicast_ether_addr(ha->addr))
2283 continue;
2284
2285 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2286 if (!mcast_all) {
2287 sh_eth_tsu_purge_mcast(ndev);
2288 ecmr_bits &= ~ECMR_MCT;
2289 mcast_all = 1;
2290 }
2291 }
2292 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002293 } else {
2294 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002295 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002296 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002297
2298 /* update the ethernet mode */
2299 sh_eth_write(ndev, ecmr_bits, ECMR);
2300
2301 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002302}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002303
2304static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2305{
2306 if (!mdp->port)
2307 return TSU_VTAG0;
2308 else
2309 return TSU_VTAG1;
2310}
2311
Patrick McHardy80d5c362013-04-19 02:04:28 +00002312static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2313 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002314{
2315 struct sh_eth_private *mdp = netdev_priv(ndev);
2316 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2317
2318 if (unlikely(!mdp->cd->tsu))
2319 return -EPERM;
2320
2321 /* No filtering if vid = 0 */
2322 if (!vid)
2323 return 0;
2324
2325 mdp->vlan_num_ids++;
2326
2327 /*
2328 * The controller has one VLAN tag HW filter. So, if the filter is
2329 * already enabled, the driver disables it and the filte
2330 */
2331 if (mdp->vlan_num_ids > 1) {
2332 /* disable VLAN filter */
2333 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2334 return 0;
2335 }
2336
2337 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2338 vtag_reg_index);
2339
2340 return 0;
2341}
2342
Patrick McHardy80d5c362013-04-19 02:04:28 +00002343static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2344 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002345{
2346 struct sh_eth_private *mdp = netdev_priv(ndev);
2347 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2348
2349 if (unlikely(!mdp->cd->tsu))
2350 return -EPERM;
2351
2352 /* No filtering if vid = 0 */
2353 if (!vid)
2354 return 0;
2355
2356 mdp->vlan_num_ids--;
2357 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2358
2359 return 0;
2360}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002361
2362/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002363static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002364{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002365 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2366 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2367 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2368 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2369 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2370 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2371 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2372 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2373 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2374 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002375 if (sh_eth_is_gether(mdp)) {
2376 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2377 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2378 } else {
2379 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2380 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2381 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002382 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2383 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2384 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2385 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2386 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2387 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2388 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002389}
2390
2391/* MDIO bus release function */
2392static int sh_mdio_release(struct net_device *ndev)
2393{
2394 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2395
2396 /* unregister mdio bus */
2397 mdiobus_unregister(bus);
2398
2399 /* remove mdio bus info from net_device */
2400 dev_set_drvdata(&ndev->dev, NULL);
2401
2402 /* free bitbang info */
2403 free_mdio_bitbang(bus);
2404
2405 return 0;
2406}
2407
2408/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002409static int sh_mdio_init(struct net_device *ndev, int id,
2410 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002411{
2412 int ret, i;
2413 struct bb_info *bitbang;
2414 struct sh_eth_private *mdp = netdev_priv(ndev);
2415
2416 /* create bit control struct for PHY */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002417 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2418 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002419 if (!bitbang) {
2420 ret = -ENOMEM;
2421 goto out;
2422 }
2423
2424 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002425 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002426 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002427 bitbang->mdi_msk = PIR_MDI;
2428 bitbang->mdo_msk = PIR_MDO;
2429 bitbang->mmd_msk = PIR_MMD;
2430 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002431 bitbang->ctrl.ops = &bb_ops;
2432
Stefan Weilc2e07b32010-08-03 19:44:52 +02002433 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002434 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2435 if (!mdp->mii_bus) {
2436 ret = -ENOMEM;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002437 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002438 }
2439
2440 /* Hook up MII support for ethtool */
2441 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002442 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002443 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002444 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002445
2446 /* PHY IRQ */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002447 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2448 sizeof(int) * PHY_MAX_ADDR,
2449 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002450 if (!mdp->mii_bus->irq) {
2451 ret = -ENOMEM;
2452 goto out_free_bus;
2453 }
2454
2455 for (i = 0; i < PHY_MAX_ADDR; i++)
2456 mdp->mii_bus->irq[i] = PHY_POLL;
2457
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002458 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002459 ret = mdiobus_register(mdp->mii_bus);
2460 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002461 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002462
2463 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2464
2465 return 0;
2466
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002467out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002468 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002469
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002470out:
2471 return ret;
2472}
2473
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002474static const u16 *sh_eth_get_register_offset(int register_type)
2475{
2476 const u16 *reg_offset = NULL;
2477
2478 switch (register_type) {
2479 case SH_ETH_REG_GIGABIT:
2480 reg_offset = sh_eth_offset_gigabit;
2481 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002482 case SH_ETH_REG_FAST_RCAR:
2483 reg_offset = sh_eth_offset_fast_rcar;
2484 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002485 case SH_ETH_REG_FAST_SH4:
2486 reg_offset = sh_eth_offset_fast_sh4;
2487 break;
2488 case SH_ETH_REG_FAST_SH3_SH2:
2489 reg_offset = sh_eth_offset_fast_sh3_sh2;
2490 break;
2491 default:
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +00002492 pr_err("Unknown register type (%d)\n", register_type);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002493 break;
2494 }
2495
2496 return reg_offset;
2497}
2498
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002499static struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002500 .ndo_open = sh_eth_open,
2501 .ndo_stop = sh_eth_close,
2502 .ndo_start_xmit = sh_eth_start_xmit,
2503 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002504 .ndo_tx_timeout = sh_eth_tx_timeout,
2505 .ndo_do_ioctl = sh_eth_do_ioctl,
2506 .ndo_validate_addr = eth_validate_addr,
2507 .ndo_set_mac_address = eth_mac_addr,
2508 .ndo_change_mtu = eth_change_mtu,
2509};
2510
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002511static int sh_eth_drv_probe(struct platform_device *pdev)
2512{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002513 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002514 struct resource *res;
2515 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002516 struct sh_eth_private *mdp = NULL;
Sergei Shtylyov564044b2013-03-21 10:39:22 +00002517 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002518 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002519
2520 /* get base addr */
2521 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2522 if (unlikely(res == NULL)) {
2523 dev_err(&pdev->dev, "invalid resource\n");
2524 ret = -EINVAL;
2525 goto out;
2526 }
2527
2528 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2529 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002530 ret = -ENOMEM;
2531 goto out;
2532 }
2533
2534 /* The sh Ether-specific entries in the device structure. */
2535 ndev->base_addr = res->start;
2536 devno = pdev->id;
2537 if (devno < 0)
2538 devno = 0;
2539
2540 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002541 ret = platform_get_irq(pdev, 0);
2542 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002543 ret = -ENODEV;
2544 goto out_release;
2545 }
roel kluincc3c0802008-09-10 19:22:44 +02002546 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002547
2548 SET_NETDEV_DEV(ndev, &pdev->dev);
2549
2550 /* Fill in the fields of the device structure with ethernet values. */
2551 ether_setup(ndev);
2552
2553 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002554 mdp->num_tx_ring = TX_RING_SIZE;
2555 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002556 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2557 if (IS_ERR(mdp->addr)) {
2558 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002559 goto out_release;
2560 }
2561
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002562 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002563 mdp->pdev = pdev;
2564 pm_runtime_enable(&pdev->dev);
2565 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002566
2567 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002568 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002569 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002570 /* EDMAC endian */
2571 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002572 mdp->no_ether_link = pd->no_ether_link;
2573 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002574 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002575
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002576 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002577#if defined(SH_ETH_HAS_BOTH_MODULES)
2578 mdp->cd = sh_eth_get_cpu_data(mdp);
2579#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002580 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002581#endif
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002582 if (id->driver_data)
2583 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002584 sh_eth_set_default_cpu_data(mdp->cd);
2585
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002586 /* set function */
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002587 if (mdp->cd->tsu) {
2588 sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
2589 sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
2590 sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
2591 sh_eth_vlan_rx_kill_vid;
2592 }
2593
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002594 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002595 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002596 ndev->watchdog_timeo = TX_TIMEOUT;
2597
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002598 /* debug message level */
2599 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002600
2601 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002602 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002603 if (!is_valid_ether_addr(ndev->dev_addr)) {
2604 dev_warn(&pdev->dev,
2605 "no valid MAC address supplied, using a random one.\n");
2606 eth_hw_addr_random(ndev);
2607 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002608
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002609 /* ioremap the TSU registers */
2610 if (mdp->cd->tsu) {
2611 struct resource *rtsu;
2612 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002613 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2614 if (IS_ERR(mdp->tsu_addr)) {
2615 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002616 goto out_release;
2617 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002618 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002619 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002620 }
2621
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002622 /* initialize first or needed device */
2623 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002624 if (mdp->cd->chip_reset)
2625 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002626
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002627 if (mdp->cd->tsu) {
2628 /* TSU init (Init only)*/
2629 sh_eth_tsu_init(mdp);
2630 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002631 }
2632
2633 /* network device register */
2634 ret = register_netdev(ndev);
2635 if (ret)
2636 goto out_release;
2637
2638 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002639 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002640 if (ret)
2641 goto out_unregister;
2642
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002643 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002644 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2645 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002646
2647 platform_set_drvdata(pdev, ndev);
2648
2649 return ret;
2650
2651out_unregister:
2652 unregister_netdev(ndev);
2653
2654out_release:
2655 /* net_dev free */
2656 if (ndev)
2657 free_netdev(ndev);
2658
2659out:
2660 return ret;
2661}
2662
2663static int sh_eth_drv_remove(struct platform_device *pdev)
2664{
2665 struct net_device *ndev = platform_get_drvdata(pdev);
2666
2667 sh_mdio_release(ndev);
2668 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00002669 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002670 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002671
2672 return 0;
2673}
2674
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002675#ifdef CONFIG_PM
Magnus Dammbcd51492009-10-09 00:20:04 +00002676static int sh_eth_runtime_nop(struct device *dev)
2677{
2678 /*
2679 * Runtime PM callback shared between ->runtime_suspend()
2680 * and ->runtime_resume(). Simply returns success.
2681 *
2682 * This driver re-initializes all registers after
2683 * pm_runtime_get_sync() anyway so there is no need
2684 * to save and restore registers here.
2685 */
2686 return 0;
2687}
2688
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002689static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Magnus Dammbcd51492009-10-09 00:20:04 +00002690 .runtime_suspend = sh_eth_runtime_nop,
2691 .runtime_resume = sh_eth_runtime_nop,
2692};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002693#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2694#else
2695#define SH_ETH_PM_OPS NULL
2696#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00002697
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002698static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00002699 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00002700 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00002701 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002702 { CARDNAME },
2703 { }
2704};
2705MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
2706
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002707static struct platform_driver sh_eth_driver = {
2708 .probe = sh_eth_drv_probe,
2709 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002710 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002711 .driver = {
2712 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00002713 .pm = SH_ETH_PM_OPS,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002714 },
2715};
2716
Axel Lindb62f682011-11-27 16:44:17 +00002717module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002718
2719MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2720MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2721MODULE_LICENSE("GPL v2");