blob: 2234a945ae5b0de43371bc22ee7f555e583e5727 [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 Iwamatsub7feacf2013-06-06 09:50:30 +0000316static void __maybe_unused sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000317{
318 u32 value = 0x0;
319 struct sh_eth_private *mdp = netdev_priv(ndev);
320
321 switch (mdp->phy_interface) {
322 case PHY_INTERFACE_MODE_GMII:
323 value = 0x2;
324 break;
325 case PHY_INTERFACE_MODE_MII:
326 value = 0x1;
327 break;
328 case PHY_INTERFACE_MODE_RMII:
329 value = 0x0;
330 break;
331 default:
332 pr_warn("PHY interface mode was not setup. Set to MII.\n");
333 value = 0x1;
334 break;
335 }
336
337 sh_eth_write(ndev, value, RMII_MII);
338}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000339
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000340static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000341{
342 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000343
344 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000345 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000346 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000347 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000348}
349
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000350/* There is CPU dependent code */
351#if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
352#define SH_ETH_RESET_DEFAULT 1
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000353static void sh_eth_set_rate(struct net_device *ndev)
354{
355 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000356
357 switch (mdp->speed) {
358 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000359 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000360 break;
361 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000362 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
363 break;
364 default:
365 break;
366 }
367}
368
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000369/* R8A7778/9 */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000370static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
371 .set_duplex = sh_eth_set_duplex,
372 .set_rate = sh_eth_set_rate,
373
374 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
375 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
376 .eesipr_value = 0x01ff009f,
377
378 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
379 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
380 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
381 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
382
383 .apr = 1,
384 .mpr = 1,
385 .tpauser = 1,
386 .hw_swap = 1,
387};
388#elif defined(CONFIG_CPU_SUBTYPE_SH7724)
389#define SH_ETH_RESET_DEFAULT 1
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000390
391static void sh_eth_set_rate(struct net_device *ndev)
392{
393 struct sh_eth_private *mdp = netdev_priv(ndev);
394
395 switch (mdp->speed) {
396 case 10: /* 10BASE */
397 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
398 break;
399 case 100:/* 100BASE */
400 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000401 break;
402 default:
403 break;
404 }
405}
406
407/* SH7724 */
408static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
409 .set_duplex = sh_eth_set_duplex,
410 .set_rate = sh_eth_set_rate,
411
412 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
413 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
414 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
415
416 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
417 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
418 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
419 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
420
421 .apr = 1,
422 .mpr = 1,
423 .tpauser = 1,
424 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800425 .rpadir = 1,
426 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000427};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000428#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000429#define SH_ETH_HAS_BOTH_MODULES 1
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000430static int sh_eth_check_reset(struct net_device *ndev);
431
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000432static void sh_eth_set_rate(struct net_device *ndev)
433{
434 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000435
436 switch (mdp->speed) {
437 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000438 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000439 break;
440 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000441 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000442 break;
443 default:
444 break;
445 }
446}
447
448/* SH7757 */
449static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
450 .set_duplex = sh_eth_set_duplex,
451 .set_rate = sh_eth_set_rate,
452
453 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
454 .rmcr_value = 0x00000001,
455
456 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
457 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
458 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
459 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
460
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000461 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000462 .apr = 1,
463 .mpr = 1,
464 .tpauser = 1,
465 .hw_swap = 1,
466 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000467 .rpadir = 1,
468 .rpadir_value = 2 << 16,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000469};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000470
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000471#define SH_GIGA_ETH_BASE 0xfee00000
472#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
473#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
474static void sh_eth_chip_reset_giga(struct net_device *ndev)
475{
476 int i;
477 unsigned long mahr[2], malr[2];
478
479 /* save MAHR and MALR */
480 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000481 malr[i] = ioread32((void *)GIGA_MALR(i));
482 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000483 }
484
485 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000486 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000487 mdelay(1);
488
489 /* restore MAHR and MALR */
490 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000491 iowrite32(malr[i], (void *)GIGA_MALR(i));
492 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000493 }
494}
495
496static int sh_eth_is_gether(struct sh_eth_private *mdp);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000497static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000498{
499 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000500 int ret = 0;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000501
502 if (sh_eth_is_gether(mdp)) {
Nobuhiro Iwamatsuddcd91c2013-06-06 09:44:27 +0000503 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000504 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
505 EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000506
507 ret = sh_eth_check_reset(ndev);
508 if (ret)
509 goto out;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000510
511 /* Table Init */
512 sh_eth_write(ndev, 0x0, TDLAR);
513 sh_eth_write(ndev, 0x0, TDFAR);
514 sh_eth_write(ndev, 0x0, TDFXR);
515 sh_eth_write(ndev, 0x0, TDFFR);
516 sh_eth_write(ndev, 0x0, RDLAR);
517 sh_eth_write(ndev, 0x0, RDFAR);
518 sh_eth_write(ndev, 0x0, RDFXR);
519 sh_eth_write(ndev, 0x0, RDFFR);
520 } else {
521 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
522 EDMR);
523 mdelay(3);
524 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
525 EDMR);
526 }
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000527
528out:
529 return ret;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000530}
531
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000532static void sh_eth_set_rate_giga(struct net_device *ndev)
533{
534 struct sh_eth_private *mdp = netdev_priv(ndev);
535
536 switch (mdp->speed) {
537 case 10: /* 10BASE */
538 sh_eth_write(ndev, 0x00000000, GECMR);
539 break;
540 case 100:/* 100BASE */
541 sh_eth_write(ndev, 0x00000010, GECMR);
542 break;
543 case 1000: /* 1000BASE */
544 sh_eth_write(ndev, 0x00000020, GECMR);
545 break;
546 default:
547 break;
548 }
549}
550
551/* SH7757(GETHERC) */
552static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
553 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000554 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000555 .set_rate = sh_eth_set_rate_giga,
556
557 .ecsr_value = ECSR_ICD | ECSR_MPD,
558 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
559 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
560
561 .tx_check = EESR_TC1 | EESR_FTC,
562 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
563 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
564 EESR_ECI,
565 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
566 EESR_TFE,
567 .fdr_value = 0x0000072f,
568 .rmcr_value = 0x00000001,
569
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000570 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000571 .apr = 1,
572 .mpr = 1,
573 .tpauser = 1,
574 .bculr = 1,
575 .hw_swap = 1,
576 .rpadir = 1,
577 .rpadir_value = 2 << 16,
578 .no_trimd = 1,
579 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000580 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000581};
582
583static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
584{
585 if (sh_eth_is_gether(mdp))
586 return &sh_eth_my_cpu_data_giga;
587 else
588 return &sh_eth_my_cpu_data;
589}
590
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000591#elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000592static int sh_eth_check_reset(struct net_device *ndev);
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000593static void sh_eth_reset_hw_crc(struct net_device *ndev);
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000594
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000595static void sh_eth_chip_reset(struct net_device *ndev)
596{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000597 struct sh_eth_private *mdp = netdev_priv(ndev);
598
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000599 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000600 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000601 mdelay(1);
602}
603
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000604static void sh_eth_set_rate(struct net_device *ndev)
605{
606 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000607
608 switch (mdp->speed) {
609 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000610 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000611 break;
612 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000613 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000614 break;
615 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000616 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000617 break;
618 default:
619 break;
620 }
621}
622
623/* sh7763 */
624static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
625 .chip_reset = sh_eth_chip_reset,
626 .set_duplex = sh_eth_set_duplex,
627 .set_rate = sh_eth_set_rate,
628
629 .ecsr_value = ECSR_ICD | ECSR_MPD,
630 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
631 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
632
633 .tx_check = EESR_TC1 | EESR_FTC,
634 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
635 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
636 EESR_ECI,
637 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
638 EESR_TFE,
639
640 .apr = 1,
641 .mpr = 1,
642 .tpauser = 1,
643 .bculr = 1,
644 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000645 .no_trimd = 1,
646 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000647 .tsu = 1,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000648#if defined(CONFIG_CPU_SUBTYPE_SH7734)
649 .hw_crc = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000650 .select_mii = 1,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000651#else
652 .irq_flags = IRQF_SHARED,
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000653#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000654};
655
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000656static int sh_eth_reset(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000657{
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000658 int ret = 0;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000659
660 sh_eth_write(ndev, EDSR_ENALL, EDSR);
661 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000662
663 ret = sh_eth_check_reset(ndev);
664 if (ret)
665 goto out;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000666
667 /* Table Init */
668 sh_eth_write(ndev, 0x0, TDLAR);
669 sh_eth_write(ndev, 0x0, TDFAR);
670 sh_eth_write(ndev, 0x0, TDFXR);
671 sh_eth_write(ndev, 0x0, TDFFR);
672 sh_eth_write(ndev, 0x0, RDLAR);
673 sh_eth_write(ndev, 0x0, RDFAR);
674 sh_eth_write(ndev, 0x0, RDFXR);
675 sh_eth_write(ndev, 0x0, RDFFR);
676
677 /* Reset HW CRC register */
678 sh_eth_reset_hw_crc(ndev);
679
680 /* Select MII mode */
681 if (sh_eth_my_cpu_data.select_mii)
682 sh_eth_select_mii(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000683out:
684 return ret;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000685}
686
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +0000687static void sh_eth_reset_hw_crc(struct net_device *ndev)
688{
689 if (sh_eth_my_cpu_data.hw_crc)
690 sh_eth_write(ndev, 0x0, CSMR);
691}
692
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000693#elif defined(CONFIG_ARCH_R8A7740)
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000694static int sh_eth_check_reset(struct net_device *ndev);
695
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000696static void sh_eth_chip_reset(struct net_device *ndev)
697{
698 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000699
700 /* reset device */
701 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
702 mdelay(1);
703
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000704 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000705}
706
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000707static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000708{
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000709 int ret = 0;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000710
711 sh_eth_write(ndev, EDSR_ENALL, EDSR);
712 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000713
714 ret = sh_eth_check_reset(ndev);
715 if (ret)
716 goto out;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000717
718 /* Table Init */
719 sh_eth_write(ndev, 0x0, TDLAR);
720 sh_eth_write(ndev, 0x0, TDFAR);
721 sh_eth_write(ndev, 0x0, TDFXR);
722 sh_eth_write(ndev, 0x0, TDFFR);
723 sh_eth_write(ndev, 0x0, RDLAR);
724 sh_eth_write(ndev, 0x0, RDFAR);
725 sh_eth_write(ndev, 0x0, RDFXR);
726 sh_eth_write(ndev, 0x0, RDFFR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000727
728out:
729 return ret;
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000730}
731
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000732static void sh_eth_set_rate(struct net_device *ndev)
733{
734 struct sh_eth_private *mdp = netdev_priv(ndev);
735
736 switch (mdp->speed) {
737 case 10: /* 10BASE */
738 sh_eth_write(ndev, GECMR_10, GECMR);
739 break;
740 case 100:/* 100BASE */
741 sh_eth_write(ndev, GECMR_100, GECMR);
742 break;
743 case 1000: /* 1000BASE */
744 sh_eth_write(ndev, GECMR_1000, GECMR);
745 break;
746 default:
747 break;
748 }
749}
750
751/* R8A7740 */
752static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
753 .chip_reset = sh_eth_chip_reset,
754 .set_duplex = sh_eth_set_duplex,
755 .set_rate = sh_eth_set_rate,
756
757 .ecsr_value = ECSR_ICD | ECSR_MPD,
758 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
759 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
760
761 .tx_check = EESR_TC1 | EESR_FTC,
762 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
763 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
764 EESR_ECI,
765 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
766 EESR_TFE,
767
768 .apr = 1,
769 .mpr = 1,
770 .tpauser = 1,
771 .bculr = 1,
772 .hw_swap = 1,
773 .no_trimd = 1,
774 .no_ade = 1,
775 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000776 .select_mii = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000777};
778
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000779#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
780#define SH_ETH_RESET_DEFAULT 1
781static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
782 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
783
784 .apr = 1,
785 .mpr = 1,
786 .tpauser = 1,
787 .hw_swap = 1,
788};
789#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
790#define SH_ETH_RESET_DEFAULT 1
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000791static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
792 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000793 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000794};
795#endif
796
797static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
798{
799 if (!cd->ecsr_value)
800 cd->ecsr_value = DEFAULT_ECSR_INIT;
801
802 if (!cd->ecsipr_value)
803 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
804
805 if (!cd->fcftr_value)
806 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
807 DEFAULT_FIFO_F_D_RFD;
808
809 if (!cd->fdr_value)
810 cd->fdr_value = DEFAULT_FDR_INIT;
811
812 if (!cd->rmcr_value)
813 cd->rmcr_value = DEFAULT_RMCR_VALUE;
814
815 if (!cd->tx_check)
816 cd->tx_check = DEFAULT_TX_CHECK;
817
818 if (!cd->eesr_err_check)
819 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
820
821 if (!cd->tx_error_check)
822 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
823}
824
825#if defined(SH_ETH_RESET_DEFAULT)
826/* Chip Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000827static int sh_eth_reset(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000828{
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000829 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000830 mdelay(3);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000831 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000832
833 return 0;
834}
835#else
836static int sh_eth_check_reset(struct net_device *ndev)
837{
838 int ret = 0;
839 int cnt = 100;
840
841 while (cnt > 0) {
842 if (!(sh_eth_read(ndev, EDMR) & 0x3))
843 break;
844 mdelay(1);
845 cnt--;
846 }
847 if (cnt < 0) {
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +0000848 pr_err("Device reset fail\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000849 ret = -ETIMEDOUT;
850 }
851 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000852}
853#endif
854
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000855#if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000856static void sh_eth_set_receive_align(struct sk_buff *skb)
857{
858 int reserve;
859
860 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
861 if (reserve)
862 skb_reserve(skb, reserve);
863}
864#else
865static void sh_eth_set_receive_align(struct sk_buff *skb)
866{
867 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
868}
869#endif
870
871
Yoshinori Sato71557a32008-08-06 19:49:00 -0400872/* CPU <-> EDMAC endian convert */
873static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
874{
875 switch (mdp->edmac_endian) {
876 case EDMAC_LITTLE_ENDIAN:
877 return cpu_to_le32(x);
878 case EDMAC_BIG_ENDIAN:
879 return cpu_to_be32(x);
880 }
881 return x;
882}
883
884static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
885{
886 switch (mdp->edmac_endian) {
887 case EDMAC_LITTLE_ENDIAN:
888 return le32_to_cpu(x);
889 case EDMAC_BIG_ENDIAN:
890 return be32_to_cpu(x);
891 }
892 return x;
893}
894
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700895/*
896 * Program the hardware MAC address from dev->dev_addr.
897 */
898static void update_mac_address(struct net_device *ndev)
899{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000900 sh_eth_write(ndev,
901 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
902 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
903 sh_eth_write(ndev,
904 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700905}
906
907/*
908 * Get MAC address from SuperH MAC address register
909 *
910 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
911 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
912 * When you want use this device, you must set MAC address in bootloader.
913 *
914 */
Magnus Damm748031f2009-10-09 00:17:14 +0000915static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700916{
Magnus Damm748031f2009-10-09 00:17:14 +0000917 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
918 memcpy(ndev->dev_addr, mac, 6);
919 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000920 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
921 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
922 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
923 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
924 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
925 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000926 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700927}
928
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000929static int sh_eth_is_gether(struct sh_eth_private *mdp)
930{
931 if (mdp->reg_offset == sh_eth_offset_gigabit)
932 return 1;
933 else
934 return 0;
935}
936
937static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
938{
939 if (sh_eth_is_gether(mdp))
940 return EDTRR_TRNS_GETHER;
941 else
942 return EDTRR_TRNS_ETHER;
943}
944
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700945struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000946 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700947 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000948 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700949 u32 mmd_msk;/* MMD */
950 u32 mdo_msk;
951 u32 mdi_msk;
952 u32 mdc_msk;
953};
954
955/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000956static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700957{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000958 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700959}
960
961/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000962static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700963{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000964 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700965}
966
967/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000968static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700969{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000970 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700971}
972
973/* Data I/O pin control */
974static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
975{
976 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000977
978 if (bitbang->set_gate)
979 bitbang->set_gate(bitbang->addr);
980
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700981 if (bit)
982 bb_set(bitbang->addr, bitbang->mmd_msk);
983 else
984 bb_clr(bitbang->addr, bitbang->mmd_msk);
985}
986
987/* Set bit data*/
988static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
989{
990 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
991
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +0000992 if (bitbang->set_gate)
993 bitbang->set_gate(bitbang->addr);
994
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700995 if (bit)
996 bb_set(bitbang->addr, bitbang->mdo_msk);
997 else
998 bb_clr(bitbang->addr, bitbang->mdo_msk);
999}
1000
1001/* Get bit data*/
1002static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1003{
1004 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001005
1006 if (bitbang->set_gate)
1007 bitbang->set_gate(bitbang->addr);
1008
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001009 return bb_read(bitbang->addr, bitbang->mdi_msk);
1010}
1011
1012/* MDC pin control */
1013static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1014{
1015 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1016
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001017 if (bitbang->set_gate)
1018 bitbang->set_gate(bitbang->addr);
1019
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020 if (bit)
1021 bb_set(bitbang->addr, bitbang->mdc_msk);
1022 else
1023 bb_clr(bitbang->addr, bitbang->mdc_msk);
1024}
1025
1026/* mdio bus control struct */
1027static struct mdiobb_ops bb_ops = {
1028 .owner = THIS_MODULE,
1029 .set_mdc = sh_mdc_ctrl,
1030 .set_mdio_dir = sh_mmd_ctrl,
1031 .set_mdio_data = sh_set_mdio,
1032 .get_mdio_data = sh_get_mdio,
1033};
1034
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035/* free skb and descriptor buffer */
1036static void sh_eth_ring_free(struct net_device *ndev)
1037{
1038 struct sh_eth_private *mdp = netdev_priv(ndev);
1039 int i;
1040
1041 /* Free Rx skb ringbuffer */
1042 if (mdp->rx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001043 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001044 if (mdp->rx_skbuff[i])
1045 dev_kfree_skb(mdp->rx_skbuff[i]);
1046 }
1047 }
1048 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001049 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001050
1051 /* Free Tx skb ringbuffer */
1052 if (mdp->tx_skbuff) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001053 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001054 if (mdp->tx_skbuff[i])
1055 dev_kfree_skb(mdp->tx_skbuff[i]);
1056 }
1057 }
1058 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001059 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001060}
1061
1062/* format skb and descriptor buffer */
1063static void sh_eth_ring_format(struct net_device *ndev)
1064{
1065 struct sh_eth_private *mdp = netdev_priv(ndev);
1066 int i;
1067 struct sk_buff *skb;
1068 struct sh_eth_rxdesc *rxdesc = NULL;
1069 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001070 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1071 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001072
1073 mdp->cur_rx = mdp->cur_tx = 0;
1074 mdp->dirty_rx = mdp->dirty_tx = 0;
1075
1076 memset(mdp->rx_ring, 0, rx_ringsize);
1077
1078 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001079 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001080 /* skb */
1081 mdp->rx_skbuff[i] = NULL;
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001082 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001083 mdp->rx_skbuff[i] = skb;
1084 if (skb == NULL)
1085 break;
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001086 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001087 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001088 sh_eth_set_receive_align(skb);
1089
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001090 /* RX descriptor */
1091 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001092 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -04001093 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001094
1095 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001096 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001097 /* Rx descriptor address set */
1098 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001099 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001100 if (sh_eth_is_gether(mdp))
1101 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001102 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001103 }
1104
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001105 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001106
1107 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001108 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001109
1110 memset(mdp->tx_ring, 0, tx_ringsize);
1111
1112 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001113 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001114 mdp->tx_skbuff[i] = NULL;
1115 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001116 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001117 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001118 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001119 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001120 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001121 if (sh_eth_is_gether(mdp))
1122 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001123 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001124 }
1125
Yoshinori Sato71557a32008-08-06 19:49:00 -04001126 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001127}
1128
1129/* Get skb and descriptor buffer */
1130static int sh_eth_ring_init(struct net_device *ndev)
1131{
1132 struct sh_eth_private *mdp = netdev_priv(ndev);
1133 int rx_ringsize, tx_ringsize, ret = 0;
1134
1135 /*
1136 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1137 * card needs room to do 8 byte alignment, +2 so we can reserve
1138 * the first 2 bytes, and +16 gets room for the status word from the
1139 * card.
1140 */
1141 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1142 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001143 if (mdp->cd->rpadir)
1144 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001145
1146 /* Allocate RX and TX skb rings */
Joe Perchesb2adaca2013-02-03 17:43:58 +00001147 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1148 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001149 if (!mdp->rx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001150 ret = -ENOMEM;
1151 return ret;
1152 }
1153
Joe Perchesb2adaca2013-02-03 17:43:58 +00001154 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1155 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001156 if (!mdp->tx_skbuff) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001157 ret = -ENOMEM;
1158 goto skb_ring_free;
1159 }
1160
1161 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001162 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001163 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001164 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001165 if (!mdp->rx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001166 ret = -ENOMEM;
1167 goto desc_ring_free;
1168 }
1169
1170 mdp->dirty_rx = 0;
1171
1172 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001173 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001174 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001175 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001176 if (!mdp->tx_ring) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001177 ret = -ENOMEM;
1178 goto desc_ring_free;
1179 }
1180 return ret;
1181
1182desc_ring_free:
1183 /* free DMA buffer */
1184 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1185
1186skb_ring_free:
1187 /* Free Rx and Tx skb ring buffer */
1188 sh_eth_ring_free(ndev);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001189 mdp->tx_ring = NULL;
1190 mdp->rx_ring = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001191
1192 return ret;
1193}
1194
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001195static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1196{
1197 int ringsize;
1198
1199 if (mdp->rx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001200 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001201 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1202 mdp->rx_desc_dma);
1203 mdp->rx_ring = NULL;
1204 }
1205
1206 if (mdp->tx_ring) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001207 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001208 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1209 mdp->tx_desc_dma);
1210 mdp->tx_ring = NULL;
1211 }
1212}
1213
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001214static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001215{
1216 int ret = 0;
1217 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001218 u32 val;
1219
1220 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001221 ret = sh_eth_reset(ndev);
1222 if (ret)
1223 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001224
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001225 /* Descriptor format */
1226 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001227 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001228 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001229
1230 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001231 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001233#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001234 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001235 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001236 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001237#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001238 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001239
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001240 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001241 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1242 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001243
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001244 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001245 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001246
Yoshihiro Shimoda2ecbb782012-06-26 19:59:58 +00001247 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001248
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001249 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001250 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001251
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001252 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001253
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001254 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001255 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001256
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001257 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001258 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1259 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001260
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001261 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001262 if (start)
1263 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001264
1265 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001266 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1268
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001269 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001270
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001271 if (mdp->cd->set_rate)
1272 mdp->cd->set_rate(ndev);
1273
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001274 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001275 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001276
1277 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001278 if (start)
1279 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001280
1281 /* Set MAC address */
1282 update_mac_address(ndev);
1283
1284 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001285 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001286 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001287 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001288 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001289 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001290 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001291
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001292 if (start) {
1293 /* Setting the Rx mode will start the Rx process. */
1294 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001295
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001296 netif_start_queue(ndev);
1297 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001298
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001299out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001300 return ret;
1301}
1302
1303/* free Tx skb function */
1304static int sh_eth_txfree(struct net_device *ndev)
1305{
1306 struct sh_eth_private *mdp = netdev_priv(ndev);
1307 struct sh_eth_txdesc *txdesc;
1308 int freeNum = 0;
1309 int entry = 0;
1310
1311 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001312 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001313 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001314 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001315 break;
1316 /* Free the original skb. */
1317 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001318 dma_unmap_single(&ndev->dev, txdesc->addr,
1319 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001320 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1321 mdp->tx_skbuff[entry] = NULL;
1322 freeNum++;
1323 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001324 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001325 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001326 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001327
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001328 ndev->stats.tx_packets++;
1329 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001330 }
1331 return freeNum;
1332}
1333
1334/* Packet receive function */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001335static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001336{
1337 struct sh_eth_private *mdp = netdev_priv(ndev);
1338 struct sh_eth_rxdesc *rxdesc;
1339
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001340 int entry = mdp->cur_rx % mdp->num_rx_ring;
1341 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001342 struct sk_buff *skb;
1343 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001344 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001345
1346 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001347 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1348 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001349 pkt_len = rxdesc->frame_length;
1350
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +00001351#if defined(CONFIG_ARCH_R8A7740)
1352 desc_status >>= 16;
1353#endif
1354
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001355 if (--boguscnt < 0)
1356 break;
1357
1358 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001359 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001360
1361 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1362 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001363 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001364 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001365 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001366 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001367 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001368 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001369 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001370 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001371 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001372 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001373 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001375 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001376 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001377 if (!mdp->cd->hw_swap)
1378 sh_eth_soft_swap(
1379 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1380 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381 skb = mdp->rx_skbuff[entry];
1382 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001383 if (mdp->cd->rpadir)
1384 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001385 skb_put(skb, pkt_len);
1386 skb->protocol = eth_type_trans(skb, ndev);
1387 netif_rx(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001388 ndev->stats.rx_packets++;
1389 ndev->stats.rx_bytes += pkt_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001391 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001392 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001393 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394 }
1395
1396 /* Refill the Rx ring buffers. */
1397 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001398 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001399 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001400 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001401 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001402
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001403 if (mdp->rx_skbuff[entry] == NULL) {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001404 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405 mdp->rx_skbuff[entry] = skb;
1406 if (skb == NULL)
1407 break; /* Better luck next round. */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001408 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +00001409 DMA_FROM_DEVICE);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001410 sh_eth_set_receive_align(skb);
1411
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001412 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001413 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001414 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001415 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001416 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001417 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001418 else
1419 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001420 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001421 }
1422
1423 /* Restart Rx engine if stopped. */
1424 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001425 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001426 /* fix the values for the next receiving if RDE is set */
1427 if (intr_status & EESR_RDE)
1428 mdp->cur_rx = mdp->dirty_rx =
1429 (sh_eth_read(ndev, RDFAR) -
1430 sh_eth_read(ndev, RDLAR)) >> 4;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001431 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001432 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433
1434 return 0;
1435}
1436
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001437static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001438{
1439 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001440 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1441 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001442}
1443
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001444static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001445{
1446 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001447 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1448 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001449}
1450
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001451/* error control function */
1452static void sh_eth_error(struct net_device *ndev, int intr_status)
1453{
1454 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001456 u32 link_stat;
1457 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001458
1459 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001460 felic_stat = sh_eth_read(ndev, ECSR);
1461 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001462 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001463 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001464 if (felic_stat & ECSR_LCHNG) {
1465 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001466 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001467 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001468 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001469 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001470 if (mdp->ether_link_active_low)
1471 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001472 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001473 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001474 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001475 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001476 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001477 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1478 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001479 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001480 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1481 ECSR);
1482 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1483 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001484 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001485 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001486 }
1487 }
1488 }
1489
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001490ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001491 if (intr_status & EESR_TWB) {
1492 /* Write buck end. unused write back interrupt */
1493 if (intr_status & EESR_TABT) /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001494 ndev->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001495 if (netif_msg_tx_err(mdp))
1496 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001497 }
1498
1499 if (intr_status & EESR_RABT) {
1500 /* Receive Abort int */
1501 if (intr_status & EESR_RFRMER) {
1502 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001503 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001504 if (netif_msg_rx_err(mdp))
1505 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506 }
1507 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001508
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001509 if (intr_status & EESR_TDE) {
1510 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001511 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001512 if (netif_msg_tx_err(mdp))
1513 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1514 }
1515
1516 if (intr_status & EESR_TFE) {
1517 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001518 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001519 if (netif_msg_tx_err(mdp))
1520 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001521 }
1522
1523 if (intr_status & EESR_RDE) {
1524 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001525 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001526
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001527 if (netif_msg_rx_err(mdp))
1528 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001530
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531 if (intr_status & EESR_RFE) {
1532 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001533 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001534 if (netif_msg_rx_err(mdp))
1535 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1536 }
1537
1538 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1539 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001540 ndev->stats.tx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001541 if (netif_msg_tx_err(mdp))
1542 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001543 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001544
1545 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1546 if (mdp->cd->no_ade)
1547 mask &= ~EESR_ADE;
1548 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001549 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001550 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001552 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1553 intr_status, mdp->cur_tx);
1554 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001555 mdp->dirty_tx, (u32) ndev->state, edtrr);
1556 /* dirty buffer free */
1557 sh_eth_txfree(ndev);
1558
1559 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001560 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001561 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001562 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001563 }
1564 /* wakeup */
1565 netif_wake_queue(ndev);
1566 }
1567}
1568
1569static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1570{
1571 struct net_device *ndev = netdev;
1572 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001573 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001574 irqreturn_t ret = IRQ_NONE;
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001575 unsigned long intr_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001576
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001577 spin_lock(&mdp->lock);
1578
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001579 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001580 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001581 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1582 * enabled since it's the one that comes thru regardless of the mask,
1583 * and we need to fully handle it in sh_eth_error() in order to quench
1584 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1585 */
1586 intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001587 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001588 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1589 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001590 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001591 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001592 ret = IRQ_HANDLED;
1593 } else
1594 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001596 if (intr_status & (EESR_FRC | /* Frame recv*/
1597 EESR_RMAF | /* Multi cast address recv*/
1598 EESR_RRF | /* Bit frame recv */
1599 EESR_RTLF | /* Long frame recv*/
1600 EESR_RTSF | /* short frame recv */
1601 EESR_PRE | /* PHY-LSI recv error */
1602 EESR_CERF)){ /* recv frame CRC error */
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001603 sh_eth_rx(ndev, intr_status);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001604 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001605
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001606 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001607 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001608 sh_eth_txfree(ndev);
1609 netif_wake_queue(ndev);
1610 }
1611
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001612 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001613 sh_eth_error(ndev, intr_status);
1614
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001615other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 spin_unlock(&mdp->lock);
1617
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001618 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001619}
1620
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001621/* PHY state control function */
1622static void sh_eth_adjust_link(struct net_device *ndev)
1623{
1624 struct sh_eth_private *mdp = netdev_priv(ndev);
1625 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001626 int new_state = 0;
1627
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001628 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001629 if (phydev->duplex != mdp->duplex) {
1630 new_state = 1;
1631 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001632 if (mdp->cd->set_duplex)
1633 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001634 }
1635
1636 if (phydev->speed != mdp->speed) {
1637 new_state = 1;
1638 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001639 if (mdp->cd->set_rate)
1640 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001641 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001642 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001643 sh_eth_write(ndev,
1644 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001645 new_state = 1;
1646 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001647 if (mdp->cd->no_psr || mdp->no_ether_link)
1648 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001649 }
1650 } else if (mdp->link) {
1651 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001652 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001653 mdp->speed = 0;
1654 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001655 if (mdp->cd->no_psr || mdp->no_ether_link)
1656 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001657 }
1658
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001659 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001660 phy_print_status(phydev);
1661}
1662
1663/* PHY init function */
1664static int sh_eth_phy_init(struct net_device *ndev)
1665{
1666 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001667 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001668 struct phy_device *phydev = NULL;
1669
Kay Sieversfb28ad32008-11-10 13:55:14 -08001670 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001671 mdp->mii_bus->id , mdp->phy_id);
1672
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001673 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001674 mdp->speed = 0;
1675 mdp->duplex = -1;
1676
1677 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001678 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Florian Fainellif9a8f832013-01-14 00:52:52 +00001679 mdp->phy_interface);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001680 if (IS_ERR(phydev)) {
1681 dev_err(&ndev->dev, "phy_connect failed\n");
1682 return PTR_ERR(phydev);
1683 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001684
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001685 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001686 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001687
1688 mdp->phydev = phydev;
1689
1690 return 0;
1691}
1692
1693/* PHY control start function */
1694static int sh_eth_phy_start(struct net_device *ndev)
1695{
1696 struct sh_eth_private *mdp = netdev_priv(ndev);
1697 int ret;
1698
1699 ret = sh_eth_phy_init(ndev);
1700 if (ret)
1701 return ret;
1702
1703 /* reset phy - this also wakes it from PDOWN */
1704 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1705 phy_start(mdp->phydev);
1706
1707 return 0;
1708}
1709
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001710static int sh_eth_get_settings(struct net_device *ndev,
1711 struct ethtool_cmd *ecmd)
1712{
1713 struct sh_eth_private *mdp = netdev_priv(ndev);
1714 unsigned long flags;
1715 int ret;
1716
1717 spin_lock_irqsave(&mdp->lock, flags);
1718 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1719 spin_unlock_irqrestore(&mdp->lock, flags);
1720
1721 return ret;
1722}
1723
1724static int sh_eth_set_settings(struct net_device *ndev,
1725 struct ethtool_cmd *ecmd)
1726{
1727 struct sh_eth_private *mdp = netdev_priv(ndev);
1728 unsigned long flags;
1729 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001730
1731 spin_lock_irqsave(&mdp->lock, flags);
1732
1733 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001734 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001735
1736 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1737 if (ret)
1738 goto error_exit;
1739
1740 if (ecmd->duplex == DUPLEX_FULL)
1741 mdp->duplex = 1;
1742 else
1743 mdp->duplex = 0;
1744
1745 if (mdp->cd->set_duplex)
1746 mdp->cd->set_duplex(ndev);
1747
1748error_exit:
1749 mdelay(1);
1750
1751 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001752 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001753
1754 spin_unlock_irqrestore(&mdp->lock, flags);
1755
1756 return ret;
1757}
1758
1759static int sh_eth_nway_reset(struct net_device *ndev)
1760{
1761 struct sh_eth_private *mdp = netdev_priv(ndev);
1762 unsigned long flags;
1763 int ret;
1764
1765 spin_lock_irqsave(&mdp->lock, flags);
1766 ret = phy_start_aneg(mdp->phydev);
1767 spin_unlock_irqrestore(&mdp->lock, flags);
1768
1769 return ret;
1770}
1771
1772static u32 sh_eth_get_msglevel(struct net_device *ndev)
1773{
1774 struct sh_eth_private *mdp = netdev_priv(ndev);
1775 return mdp->msg_enable;
1776}
1777
1778static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1779{
1780 struct sh_eth_private *mdp = netdev_priv(ndev);
1781 mdp->msg_enable = value;
1782}
1783
1784static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1785 "rx_current", "tx_current",
1786 "rx_dirty", "tx_dirty",
1787};
1788#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1789
1790static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1791{
1792 switch (sset) {
1793 case ETH_SS_STATS:
1794 return SH_ETH_STATS_LEN;
1795 default:
1796 return -EOPNOTSUPP;
1797 }
1798}
1799
1800static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1801 struct ethtool_stats *stats, u64 *data)
1802{
1803 struct sh_eth_private *mdp = netdev_priv(ndev);
1804 int i = 0;
1805
1806 /* device-specific stats */
1807 data[i++] = mdp->cur_rx;
1808 data[i++] = mdp->cur_tx;
1809 data[i++] = mdp->dirty_rx;
1810 data[i++] = mdp->dirty_tx;
1811}
1812
1813static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1814{
1815 switch (stringset) {
1816 case ETH_SS_STATS:
1817 memcpy(data, *sh_eth_gstrings_stats,
1818 sizeof(sh_eth_gstrings_stats));
1819 break;
1820 }
1821}
1822
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001823static void sh_eth_get_ringparam(struct net_device *ndev,
1824 struct ethtool_ringparam *ring)
1825{
1826 struct sh_eth_private *mdp = netdev_priv(ndev);
1827
1828 ring->rx_max_pending = RX_RING_MAX;
1829 ring->tx_max_pending = TX_RING_MAX;
1830 ring->rx_pending = mdp->num_rx_ring;
1831 ring->tx_pending = mdp->num_tx_ring;
1832}
1833
1834static int sh_eth_set_ringparam(struct net_device *ndev,
1835 struct ethtool_ringparam *ring)
1836{
1837 struct sh_eth_private *mdp = netdev_priv(ndev);
1838 int ret;
1839
1840 if (ring->tx_pending > TX_RING_MAX ||
1841 ring->rx_pending > RX_RING_MAX ||
1842 ring->tx_pending < TX_RING_MIN ||
1843 ring->rx_pending < RX_RING_MIN)
1844 return -EINVAL;
1845 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1846 return -EINVAL;
1847
1848 if (netif_running(ndev)) {
1849 netif_tx_disable(ndev);
1850 /* Disable interrupts by clearing the interrupt mask. */
1851 sh_eth_write(ndev, 0x0000, EESIPR);
1852 /* Stop the chip's Tx and Rx processes. */
1853 sh_eth_write(ndev, 0, EDTRR);
1854 sh_eth_write(ndev, 0, EDRRR);
1855 synchronize_irq(ndev->irq);
1856 }
1857
1858 /* Free all the skbuffs in the Rx queue. */
1859 sh_eth_ring_free(ndev);
1860 /* Free DMA buffer */
1861 sh_eth_free_dma_buffer(mdp);
1862
1863 /* Set new parameters */
1864 mdp->num_rx_ring = ring->rx_pending;
1865 mdp->num_tx_ring = ring->tx_pending;
1866
1867 ret = sh_eth_ring_init(ndev);
1868 if (ret < 0) {
1869 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1870 return ret;
1871 }
1872 ret = sh_eth_dev_init(ndev, false);
1873 if (ret < 0) {
1874 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1875 return ret;
1876 }
1877
1878 if (netif_running(ndev)) {
1879 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1880 /* Setting the Rx mode will start the Rx process. */
1881 sh_eth_write(ndev, EDRRR_R, EDRRR);
1882 netif_wake_queue(ndev);
1883 }
1884
1885 return 0;
1886}
1887
stephen hemminger9b07be42012-01-04 12:59:49 +00001888static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001889 .get_settings = sh_eth_get_settings,
1890 .set_settings = sh_eth_set_settings,
stephen hemminger9b07be42012-01-04 12:59:49 +00001891 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001892 .get_msglevel = sh_eth_get_msglevel,
1893 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00001894 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001895 .get_strings = sh_eth_get_strings,
1896 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1897 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001898 .get_ringparam = sh_eth_get_ringparam,
1899 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001900};
1901
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001902/* network device open function */
1903static int sh_eth_open(struct net_device *ndev)
1904{
1905 int ret = 0;
1906 struct sh_eth_private *mdp = netdev_priv(ndev);
1907
Magnus Dammbcd51492009-10-09 00:20:04 +00001908 pm_runtime_get_sync(&mdp->pdev->dev);
1909
Joe Perchesa0607fd2009-11-18 23:29:17 -08001910 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001911 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001912 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001913 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001914 return ret;
1915 }
1916
1917 /* Descriptor set */
1918 ret = sh_eth_ring_init(ndev);
1919 if (ret)
1920 goto out_free_irq;
1921
1922 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001923 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001924 if (ret)
1925 goto out_free_irq;
1926
1927 /* PHY control start*/
1928 ret = sh_eth_phy_start(ndev);
1929 if (ret)
1930 goto out_free_irq;
1931
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001932 return ret;
1933
1934out_free_irq:
1935 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001936 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001937 return ret;
1938}
1939
1940/* Timeout function */
1941static void sh_eth_tx_timeout(struct net_device *ndev)
1942{
1943 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001944 struct sh_eth_rxdesc *rxdesc;
1945 int i;
1946
1947 netif_stop_queue(ndev);
1948
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001949 if (netif_msg_timer(mdp))
1950 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001951 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001952
1953 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001954 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001955
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001956 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001957 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001958 rxdesc = &mdp->rx_ring[i];
1959 rxdesc->status = 0;
1960 rxdesc->addr = 0xBADF00D0;
1961 if (mdp->rx_skbuff[i])
1962 dev_kfree_skb(mdp->rx_skbuff[i]);
1963 mdp->rx_skbuff[i] = NULL;
1964 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001965 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001966 if (mdp->tx_skbuff[i])
1967 dev_kfree_skb(mdp->tx_skbuff[i]);
1968 mdp->tx_skbuff[i] = NULL;
1969 }
1970
1971 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001972 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001973}
1974
1975/* Packet transmit function */
1976static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1977{
1978 struct sh_eth_private *mdp = netdev_priv(ndev);
1979 struct sh_eth_txdesc *txdesc;
1980 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001981 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001982
1983 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001984 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001985 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001986 if (netif_msg_tx_queued(mdp))
1987 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001988 netif_stop_queue(ndev);
1989 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001990 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001991 }
1992 }
1993 spin_unlock_irqrestore(&mdp->lock, flags);
1994
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001995 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001996 mdp->tx_skbuff[entry] = skb;
1997 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001998 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001999 if (!mdp->cd->hw_swap)
2000 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2001 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002002 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2003 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002004 if (skb->len < ETHERSMALL)
2005 txdesc->buffer_length = ETHERSMALL;
2006 else
2007 txdesc->buffer_length = skb->len;
2008
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002009 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002010 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002011 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002012 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002013
2014 mdp->cur_tx++;
2015
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002016 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2017 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002018
Patrick McHardy6ed10652009-06-23 06:03:08 +00002019 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002020}
2021
2022/* device close function */
2023static int sh_eth_close(struct net_device *ndev)
2024{
2025 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002026
2027 netif_stop_queue(ndev);
2028
2029 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002030 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002031
2032 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002033 sh_eth_write(ndev, 0, EDTRR);
2034 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002035
2036 /* PHY Disconnect */
2037 if (mdp->phydev) {
2038 phy_stop(mdp->phydev);
2039 phy_disconnect(mdp->phydev);
2040 }
2041
2042 free_irq(ndev->irq, ndev);
2043
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002044 /* Free all the skbuffs in the Rx queue. */
2045 sh_eth_ring_free(ndev);
2046
2047 /* free DMA buffer */
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00002048 sh_eth_free_dma_buffer(mdp);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002049
Magnus Dammbcd51492009-10-09 00:20:04 +00002050 pm_runtime_put_sync(&mdp->pdev->dev);
2051
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002052 return 0;
2053}
2054
2055static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2056{
2057 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002058
Magnus Dammbcd51492009-10-09 00:20:04 +00002059 pm_runtime_get_sync(&mdp->pdev->dev);
2060
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002061 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002062 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002063 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002064 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002065 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002066 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002067 if (sh_eth_is_gether(mdp)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002068 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002069 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002070 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002071 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
2072 } else {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002073 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002074 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
2075 }
Magnus Dammbcd51492009-10-09 00:20:04 +00002076 pm_runtime_put_sync(&mdp->pdev->dev);
2077
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002078 return &ndev->stats;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002079}
2080
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002081/* ioctl to device function */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002082static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
2083 int cmd)
2084{
2085 struct sh_eth_private *mdp = netdev_priv(ndev);
2086 struct phy_device *phydev = mdp->phydev;
2087
2088 if (!netif_running(ndev))
2089 return -EINVAL;
2090
2091 if (!phydev)
2092 return -ENODEV;
2093
Richard Cochran28b04112010-07-17 08:48:55 +00002094 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002095}
2096
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002097/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2098static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2099 int entry)
2100{
2101 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2102}
2103
2104static u32 sh_eth_tsu_get_post_mask(int entry)
2105{
2106 return 0x0f << (28 - ((entry % 8) * 4));
2107}
2108
2109static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2110{
2111 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2112}
2113
2114static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2115 int entry)
2116{
2117 struct sh_eth_private *mdp = netdev_priv(ndev);
2118 u32 tmp;
2119 void *reg_offset;
2120
2121 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2122 tmp = ioread32(reg_offset);
2123 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2124}
2125
2126static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2127 int entry)
2128{
2129 struct sh_eth_private *mdp = netdev_priv(ndev);
2130 u32 post_mask, ref_mask, tmp;
2131 void *reg_offset;
2132
2133 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2134 post_mask = sh_eth_tsu_get_post_mask(entry);
2135 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2136
2137 tmp = ioread32(reg_offset);
2138 iowrite32(tmp & ~post_mask, reg_offset);
2139
2140 /* If other port enables, the function returns "true" */
2141 return tmp & ref_mask;
2142}
2143
2144static int sh_eth_tsu_busy(struct net_device *ndev)
2145{
2146 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2147 struct sh_eth_private *mdp = netdev_priv(ndev);
2148
2149 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2150 udelay(10);
2151 timeout--;
2152 if (timeout <= 0) {
2153 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2154 return -ETIMEDOUT;
2155 }
2156 }
2157
2158 return 0;
2159}
2160
2161static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2162 const u8 *addr)
2163{
2164 u32 val;
2165
2166 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2167 iowrite32(val, reg);
2168 if (sh_eth_tsu_busy(ndev) < 0)
2169 return -EBUSY;
2170
2171 val = addr[4] << 8 | addr[5];
2172 iowrite32(val, reg + 4);
2173 if (sh_eth_tsu_busy(ndev) < 0)
2174 return -EBUSY;
2175
2176 return 0;
2177}
2178
2179static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2180{
2181 u32 val;
2182
2183 val = ioread32(reg);
2184 addr[0] = (val >> 24) & 0xff;
2185 addr[1] = (val >> 16) & 0xff;
2186 addr[2] = (val >> 8) & 0xff;
2187 addr[3] = val & 0xff;
2188 val = ioread32(reg + 4);
2189 addr[4] = (val >> 8) & 0xff;
2190 addr[5] = val & 0xff;
2191}
2192
2193
2194static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2195{
2196 struct sh_eth_private *mdp = netdev_priv(ndev);
2197 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2198 int i;
2199 u8 c_addr[ETH_ALEN];
2200
2201 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2202 sh_eth_tsu_read_entry(reg_offset, c_addr);
2203 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2204 return i;
2205 }
2206
2207 return -ENOENT;
2208}
2209
2210static int sh_eth_tsu_find_empty(struct net_device *ndev)
2211{
2212 u8 blank[ETH_ALEN];
2213 int entry;
2214
2215 memset(blank, 0, sizeof(blank));
2216 entry = sh_eth_tsu_find_entry(ndev, blank);
2217 return (entry < 0) ? -ENOMEM : entry;
2218}
2219
2220static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2221 int entry)
2222{
2223 struct sh_eth_private *mdp = netdev_priv(ndev);
2224 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2225 int ret;
2226 u8 blank[ETH_ALEN];
2227
2228 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2229 ~(1 << (31 - entry)), TSU_TEN);
2230
2231 memset(blank, 0, sizeof(blank));
2232 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2233 if (ret < 0)
2234 return ret;
2235 return 0;
2236}
2237
2238static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2239{
2240 struct sh_eth_private *mdp = netdev_priv(ndev);
2241 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2242 int i, ret;
2243
2244 if (!mdp->cd->tsu)
2245 return 0;
2246
2247 i = sh_eth_tsu_find_entry(ndev, addr);
2248 if (i < 0) {
2249 /* No entry found, create one */
2250 i = sh_eth_tsu_find_empty(ndev);
2251 if (i < 0)
2252 return -ENOMEM;
2253 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2254 if (ret < 0)
2255 return ret;
2256
2257 /* Enable the entry */
2258 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2259 (1 << (31 - i)), TSU_TEN);
2260 }
2261
2262 /* Entry found or created, enable POST */
2263 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2264
2265 return 0;
2266}
2267
2268static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2269{
2270 struct sh_eth_private *mdp = netdev_priv(ndev);
2271 int i, ret;
2272
2273 if (!mdp->cd->tsu)
2274 return 0;
2275
2276 i = sh_eth_tsu_find_entry(ndev, addr);
2277 if (i) {
2278 /* Entry found */
2279 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2280 goto done;
2281
2282 /* Disable the entry if both ports was disabled */
2283 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2284 if (ret < 0)
2285 return ret;
2286 }
2287done:
2288 return 0;
2289}
2290
2291static int sh_eth_tsu_purge_all(struct net_device *ndev)
2292{
2293 struct sh_eth_private *mdp = netdev_priv(ndev);
2294 int i, ret;
2295
2296 if (unlikely(!mdp->cd->tsu))
2297 return 0;
2298
2299 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2300 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2301 continue;
2302
2303 /* Disable the entry if both ports was disabled */
2304 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2305 if (ret < 0)
2306 return ret;
2307 }
2308
2309 return 0;
2310}
2311
2312static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2313{
2314 struct sh_eth_private *mdp = netdev_priv(ndev);
2315 u8 addr[ETH_ALEN];
2316 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2317 int i;
2318
2319 if (unlikely(!mdp->cd->tsu))
2320 return;
2321
2322 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2323 sh_eth_tsu_read_entry(reg_offset, addr);
2324 if (is_multicast_ether_addr(addr))
2325 sh_eth_tsu_del_entry(ndev, addr);
2326 }
2327}
2328
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002329/* Multicast reception directions set */
2330static void sh_eth_set_multicast_list(struct net_device *ndev)
2331{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002332 struct sh_eth_private *mdp = netdev_priv(ndev);
2333 u32 ecmr_bits;
2334 int mcast_all = 0;
2335 unsigned long flags;
2336
2337 spin_lock_irqsave(&mdp->lock, flags);
2338 /*
2339 * Initial condition is MCT = 1, PRM = 0.
2340 * Depending on ndev->flags, set PRM or clear MCT
2341 */
2342 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2343
2344 if (!(ndev->flags & IFF_MULTICAST)) {
2345 sh_eth_tsu_purge_mcast(ndev);
2346 mcast_all = 1;
2347 }
2348 if (ndev->flags & IFF_ALLMULTI) {
2349 sh_eth_tsu_purge_mcast(ndev);
2350 ecmr_bits &= ~ECMR_MCT;
2351 mcast_all = 1;
2352 }
2353
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002354 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002355 sh_eth_tsu_purge_all(ndev);
2356 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2357 } else if (mdp->cd->tsu) {
2358 struct netdev_hw_addr *ha;
2359 netdev_for_each_mc_addr(ha, ndev) {
2360 if (mcast_all && is_multicast_ether_addr(ha->addr))
2361 continue;
2362
2363 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2364 if (!mcast_all) {
2365 sh_eth_tsu_purge_mcast(ndev);
2366 ecmr_bits &= ~ECMR_MCT;
2367 mcast_all = 1;
2368 }
2369 }
2370 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002371 } else {
2372 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002373 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002374 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002375
2376 /* update the ethernet mode */
2377 sh_eth_write(ndev, ecmr_bits, ECMR);
2378
2379 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002380}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002381
2382static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2383{
2384 if (!mdp->port)
2385 return TSU_VTAG0;
2386 else
2387 return TSU_VTAG1;
2388}
2389
Patrick McHardy80d5c362013-04-19 02:04:28 +00002390static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2391 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002392{
2393 struct sh_eth_private *mdp = netdev_priv(ndev);
2394 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2395
2396 if (unlikely(!mdp->cd->tsu))
2397 return -EPERM;
2398
2399 /* No filtering if vid = 0 */
2400 if (!vid)
2401 return 0;
2402
2403 mdp->vlan_num_ids++;
2404
2405 /*
2406 * The controller has one VLAN tag HW filter. So, if the filter is
2407 * already enabled, the driver disables it and the filte
2408 */
2409 if (mdp->vlan_num_ids > 1) {
2410 /* disable VLAN filter */
2411 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2412 return 0;
2413 }
2414
2415 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2416 vtag_reg_index);
2417
2418 return 0;
2419}
2420
Patrick McHardy80d5c362013-04-19 02:04:28 +00002421static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2422 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002423{
2424 struct sh_eth_private *mdp = netdev_priv(ndev);
2425 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2426
2427 if (unlikely(!mdp->cd->tsu))
2428 return -EPERM;
2429
2430 /* No filtering if vid = 0 */
2431 if (!vid)
2432 return 0;
2433
2434 mdp->vlan_num_ids--;
2435 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2436
2437 return 0;
2438}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002439
2440/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002441static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002442{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002443 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2444 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2445 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2446 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2447 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2448 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2449 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2450 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2451 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2452 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002453 if (sh_eth_is_gether(mdp)) {
2454 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2455 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2456 } else {
2457 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2458 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2459 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002460 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2461 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2462 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2463 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2464 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2465 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2466 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002467}
2468
2469/* MDIO bus release function */
2470static int sh_mdio_release(struct net_device *ndev)
2471{
2472 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2473
2474 /* unregister mdio bus */
2475 mdiobus_unregister(bus);
2476
2477 /* remove mdio bus info from net_device */
2478 dev_set_drvdata(&ndev->dev, NULL);
2479
2480 /* free bitbang info */
2481 free_mdio_bitbang(bus);
2482
2483 return 0;
2484}
2485
2486/* MDIO bus init function */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002487static int sh_mdio_init(struct net_device *ndev, int id,
2488 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002489{
2490 int ret, i;
2491 struct bb_info *bitbang;
2492 struct sh_eth_private *mdp = netdev_priv(ndev);
2493
2494 /* create bit control struct for PHY */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002495 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2496 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002497 if (!bitbang) {
2498 ret = -ENOMEM;
2499 goto out;
2500 }
2501
2502 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002503 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002504 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002505 bitbang->mdi_msk = PIR_MDI;
2506 bitbang->mdo_msk = PIR_MDO;
2507 bitbang->mmd_msk = PIR_MMD;
2508 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002509 bitbang->ctrl.ops = &bb_ops;
2510
Stefan Weilc2e07b32010-08-03 19:44:52 +02002511 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002512 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2513 if (!mdp->mii_bus) {
2514 ret = -ENOMEM;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002515 goto out;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002516 }
2517
2518 /* Hook up MII support for ethtool */
2519 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00002520 mdp->mii_bus->parent = &ndev->dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002521 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Nobuhiro Iwamatsu34aa6f12012-01-16 16:50:16 +00002522 mdp->pdev->name, id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002523
2524 /* PHY IRQ */
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002525 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2526 sizeof(int) * PHY_MAX_ADDR,
2527 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002528 if (!mdp->mii_bus->irq) {
2529 ret = -ENOMEM;
2530 goto out_free_bus;
2531 }
2532
2533 for (i = 0; i < PHY_MAX_ADDR; i++)
2534 mdp->mii_bus->irq[i] = PHY_POLL;
2535
YOSHIFUJI Hideaki / 吉藤英明8f6352f2012-11-02 04:45:07 +00002536 /* register mdio bus */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002537 ret = mdiobus_register(mdp->mii_bus);
2538 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002539 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002540
2541 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2542
2543 return 0;
2544
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002545out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002546 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002547
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002548out:
2549 return ret;
2550}
2551
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002552static const u16 *sh_eth_get_register_offset(int register_type)
2553{
2554 const u16 *reg_offset = NULL;
2555
2556 switch (register_type) {
2557 case SH_ETH_REG_GIGABIT:
2558 reg_offset = sh_eth_offset_gigabit;
2559 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002560 case SH_ETH_REG_FAST_RCAR:
2561 reg_offset = sh_eth_offset_fast_rcar;
2562 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002563 case SH_ETH_REG_FAST_SH4:
2564 reg_offset = sh_eth_offset_fast_sh4;
2565 break;
2566 case SH_ETH_REG_FAST_SH3_SH2:
2567 reg_offset = sh_eth_offset_fast_sh3_sh2;
2568 break;
2569 default:
Nobuhiro Iwamatsu14c33262013-03-20 22:46:55 +00002570 pr_err("Unknown register type (%d)\n", register_type);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002571 break;
2572 }
2573
2574 return reg_offset;
2575}
2576
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002577static struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002578 .ndo_open = sh_eth_open,
2579 .ndo_stop = sh_eth_close,
2580 .ndo_start_xmit = sh_eth_start_xmit,
2581 .ndo_get_stats = sh_eth_get_stats,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002582 .ndo_tx_timeout = sh_eth_tx_timeout,
2583 .ndo_do_ioctl = sh_eth_do_ioctl,
2584 .ndo_validate_addr = eth_validate_addr,
2585 .ndo_set_mac_address = eth_mac_addr,
2586 .ndo_change_mtu = eth_change_mtu,
2587};
2588
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002589static int sh_eth_drv_probe(struct platform_device *pdev)
2590{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002591 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002592 struct resource *res;
2593 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002594 struct sh_eth_private *mdp = NULL;
Sergei Shtylyov564044b2013-03-21 10:39:22 +00002595 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002596
2597 /* get base addr */
2598 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2599 if (unlikely(res == NULL)) {
2600 dev_err(&pdev->dev, "invalid resource\n");
2601 ret = -EINVAL;
2602 goto out;
2603 }
2604
2605 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2606 if (!ndev) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002607 ret = -ENOMEM;
2608 goto out;
2609 }
2610
2611 /* The sh Ether-specific entries in the device structure. */
2612 ndev->base_addr = res->start;
2613 devno = pdev->id;
2614 if (devno < 0)
2615 devno = 0;
2616
2617 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02002618 ret = platform_get_irq(pdev, 0);
2619 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002620 ret = -ENODEV;
2621 goto out_release;
2622 }
roel kluincc3c0802008-09-10 19:22:44 +02002623 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002624
2625 SET_NETDEV_DEV(ndev, &pdev->dev);
2626
2627 /* Fill in the fields of the device structure with ethernet values. */
2628 ether_setup(ndev);
2629
2630 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002631 mdp->num_tx_ring = TX_RING_SIZE;
2632 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002633 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2634 if (IS_ERR(mdp->addr)) {
2635 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002636 goto out_release;
2637 }
2638
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002639 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00002640 mdp->pdev = pdev;
2641 pm_runtime_enable(&pdev->dev);
2642 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002643
2644 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04002645 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00002646 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04002647 /* EDMAC endian */
2648 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00002649 mdp->no_ether_link = pd->no_ether_link;
2650 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002651 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002652
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002653 /* set cpu data */
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002654#if defined(SH_ETH_HAS_BOTH_MODULES)
2655 mdp->cd = sh_eth_get_cpu_data(mdp);
2656#else
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002657 mdp->cd = &sh_eth_my_cpu_data;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00002658#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002659 sh_eth_set_default_cpu_data(mdp->cd);
2660
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002661 /* set function */
Nobuhiro Iwamatsu9f861342013-06-06 09:48:09 +00002662 if (mdp->cd->tsu) {
2663 sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
2664 sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
2665 sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
2666 sh_eth_vlan_rx_kill_vid;
2667 }
2668
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002669 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002670 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002671 ndev->watchdog_timeo = TX_TIMEOUT;
2672
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002673 /* debug message level */
2674 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002675
2676 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00002677 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00002678 if (!is_valid_ether_addr(ndev->dev_addr)) {
2679 dev_warn(&pdev->dev,
2680 "no valid MAC address supplied, using a random one.\n");
2681 eth_hw_addr_random(ndev);
2682 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002683
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002684 /* ioremap the TSU registers */
2685 if (mdp->cd->tsu) {
2686 struct resource *rtsu;
2687 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002688 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2689 if (IS_ERR(mdp->tsu_addr)) {
2690 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00002691 goto out_release;
2692 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002693 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00002694 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00002695 }
2696
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00002697 /* initialize first or needed device */
2698 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002699 if (mdp->cd->chip_reset)
2700 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002701
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00002702 if (mdp->cd->tsu) {
2703 /* TSU init (Init only)*/
2704 sh_eth_tsu_init(mdp);
2705 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002706 }
2707
2708 /* network device register */
2709 ret = register_netdev(ndev);
2710 if (ret)
2711 goto out_release;
2712
2713 /* mdio bus init */
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002714 ret = sh_mdio_init(ndev, pdev->id, pd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002715 if (ret)
2716 goto out_unregister;
2717
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002718 /* print device information */
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08002719 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2720 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002721
2722 platform_set_drvdata(pdev, ndev);
2723
2724 return ret;
2725
2726out_unregister:
2727 unregister_netdev(ndev);
2728
2729out_release:
2730 /* net_dev free */
2731 if (ndev)
2732 free_netdev(ndev);
2733
2734out:
2735 return ret;
2736}
2737
2738static int sh_eth_drv_remove(struct platform_device *pdev)
2739{
2740 struct net_device *ndev = platform_get_drvdata(pdev);
2741
2742 sh_mdio_release(ndev);
2743 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00002744 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002745 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002746
2747 return 0;
2748}
2749
Magnus Dammbcd51492009-10-09 00:20:04 +00002750static int sh_eth_runtime_nop(struct device *dev)
2751{
2752 /*
2753 * Runtime PM callback shared between ->runtime_suspend()
2754 * and ->runtime_resume(). Simply returns success.
2755 *
2756 * This driver re-initializes all registers after
2757 * pm_runtime_get_sync() anyway so there is no need
2758 * to save and restore registers here.
2759 */
2760 return 0;
2761}
2762
2763static struct dev_pm_ops sh_eth_dev_pm_ops = {
2764 .runtime_suspend = sh_eth_runtime_nop,
2765 .runtime_resume = sh_eth_runtime_nop,
2766};
2767
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002768static struct platform_driver sh_eth_driver = {
2769 .probe = sh_eth_drv_probe,
2770 .remove = sh_eth_drv_remove,
2771 .driver = {
2772 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00002773 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002774 },
2775};
2776
Axel Lindb62f682011-11-27 16:44:17 +00002777module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002778
2779MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2780MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2781MODULE_LICENSE("GPL v2");