blob: 51268f591405a4d04c1229d9b97d374d13c580d1 [file] [log] [blame]
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001/*
2 * SuperH Ethernet device driver
3 *
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09004 * Copyright (C) 2006-2008 Nobuhiro Iwamatsu
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00005 * Copyright (C) 2008-2009 Renesas Solutions Corp.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 */
22
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070023#include <linux/init.h>
24#include <linux/dma-mapping.h>
25#include <linux/etherdevice.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28#include <linux/mdio-bitbang.h>
29#include <linux/netdevice.h>
30#include <linux/phy.h>
31#include <linux/cache.h>
32#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000033#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000035#include <linux/ethtool.h>
Nobuhiro Iwamatsuf568a922009-10-26 13:49:50 +000036#include <asm/cacheflush.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070037
38#include "sh_eth.h"
39
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000040#define SH_ETH_DEF_MSG_ENABLE \
41 (NETIF_MSG_LINK | \
42 NETIF_MSG_TIMER | \
43 NETIF_MSG_RX_ERR| \
44 NETIF_MSG_TX_ERR)
45
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000046/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000047#if defined(CONFIG_CPU_SUBTYPE_SH7724)
48#define SH_ETH_RESET_DEFAULT 1
49static void sh_eth_set_duplex(struct net_device *ndev)
50{
51 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000052
53 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000054 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000055 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000056 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000057}
58
59static void sh_eth_set_rate(struct net_device *ndev)
60{
61 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000062
63 switch (mdp->speed) {
64 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000065 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000066 break;
67 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +000068 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000069 break;
70 default:
71 break;
72 }
73}
74
75/* SH7724 */
76static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
77 .set_duplex = sh_eth_set_duplex,
78 .set_rate = sh_eth_set_rate,
79
80 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
81 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
82 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
83
84 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
85 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
86 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
87 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
88
89 .apr = 1,
90 .mpr = 1,
91 .tpauser = 1,
92 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -080093 .rpadir = 1,
94 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000095};
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +000096#elif defined(CONFIG_CPU_SUBTYPE_SH7757)
97#define SH_ETH_RESET_DEFAULT 1
98static void sh_eth_set_duplex(struct net_device *ndev)
99{
100 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000101
102 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000103 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000104 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000105 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000106}
107
108static void sh_eth_set_rate(struct net_device *ndev)
109{
110 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000111
112 switch (mdp->speed) {
113 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000114 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000115 break;
116 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000117 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000118 break;
119 default:
120 break;
121 }
122}
123
124/* SH7757 */
125static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
126 .set_duplex = sh_eth_set_duplex,
127 .set_rate = sh_eth_set_rate,
128
129 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
130 .rmcr_value = 0x00000001,
131
132 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
133 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
134 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
135 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
136
137 .apr = 1,
138 .mpr = 1,
139 .tpauser = 1,
140 .hw_swap = 1,
141 .no_ade = 1,
142};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000143
144#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000145#define SH_ETH_HAS_TSU 1
146static void sh_eth_chip_reset(struct net_device *ndev)
147{
148 /* reset device */
Paul Mundt900fcf02010-11-01 09:29:24 +0000149 writel(ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000150 mdelay(1);
151}
152
153static void sh_eth_reset(struct net_device *ndev)
154{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000155 int cnt = 100;
156
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000157 sh_eth_write(ndev, EDSR_ENALL, EDSR);
158 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000159 while (cnt > 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000160 if (!(sh_eth_read(ndev, EDMR) & 0x3))
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000161 break;
162 mdelay(1);
163 cnt--;
164 }
roel kluin890c8c12009-12-30 01:43:45 +0000165 if (cnt == 0)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000166 printk(KERN_ERR "Device reset fail\n");
167
168 /* Table Init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000169 sh_eth_write(ndev, 0x0, TDLAR);
170 sh_eth_write(ndev, 0x0, TDFAR);
171 sh_eth_write(ndev, 0x0, TDFXR);
172 sh_eth_write(ndev, 0x0, TDFFR);
173 sh_eth_write(ndev, 0x0, RDLAR);
174 sh_eth_write(ndev, 0x0, RDFAR);
175 sh_eth_write(ndev, 0x0, RDFXR);
176 sh_eth_write(ndev, 0x0, RDFFR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000177}
178
179static void sh_eth_set_duplex(struct net_device *ndev)
180{
181 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000182
183 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000184 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000185 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000186 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000187}
188
189static void sh_eth_set_rate(struct net_device *ndev)
190{
191 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000192
193 switch (mdp->speed) {
194 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000195 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000196 break;
197 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000198 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000199 break;
200 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000201 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000202 break;
203 default:
204 break;
205 }
206}
207
208/* sh7763 */
209static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
210 .chip_reset = sh_eth_chip_reset,
211 .set_duplex = sh_eth_set_duplex,
212 .set_rate = sh_eth_set_rate,
213
214 .ecsr_value = ECSR_ICD | ECSR_MPD,
215 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
216 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
217
218 .tx_check = EESR_TC1 | EESR_FTC,
219 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
220 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
221 EESR_ECI,
222 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
223 EESR_TFE,
224
225 .apr = 1,
226 .mpr = 1,
227 .tpauser = 1,
228 .bculr = 1,
229 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000230 .no_trimd = 1,
231 .no_ade = 1,
232};
233
234#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
235#define SH_ETH_RESET_DEFAULT 1
236static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
237 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
238
239 .apr = 1,
240 .mpr = 1,
241 .tpauser = 1,
242 .hw_swap = 1,
243};
244#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
245#define SH_ETH_RESET_DEFAULT 1
246#define SH_ETH_HAS_TSU 1
247static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
248 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
249};
250#endif
251
252static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
253{
254 if (!cd->ecsr_value)
255 cd->ecsr_value = DEFAULT_ECSR_INIT;
256
257 if (!cd->ecsipr_value)
258 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
259
260 if (!cd->fcftr_value)
261 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
262 DEFAULT_FIFO_F_D_RFD;
263
264 if (!cd->fdr_value)
265 cd->fdr_value = DEFAULT_FDR_INIT;
266
267 if (!cd->rmcr_value)
268 cd->rmcr_value = DEFAULT_RMCR_VALUE;
269
270 if (!cd->tx_check)
271 cd->tx_check = DEFAULT_TX_CHECK;
272
273 if (!cd->eesr_err_check)
274 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
275
276 if (!cd->tx_error_check)
277 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
278}
279
280#if defined(SH_ETH_RESET_DEFAULT)
281/* Chip Reset */
282static void sh_eth_reset(struct net_device *ndev)
283{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000284 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000285 mdelay(3);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000286 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000287}
288#endif
289
290#if defined(CONFIG_CPU_SH4)
291static void sh_eth_set_receive_align(struct sk_buff *skb)
292{
293 int reserve;
294
295 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
296 if (reserve)
297 skb_reserve(skb, reserve);
298}
299#else
300static void sh_eth_set_receive_align(struct sk_buff *skb)
301{
302 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
303}
304#endif
305
306
Yoshinori Sato71557a32008-08-06 19:49:00 -0400307/* CPU <-> EDMAC endian convert */
308static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
309{
310 switch (mdp->edmac_endian) {
311 case EDMAC_LITTLE_ENDIAN:
312 return cpu_to_le32(x);
313 case EDMAC_BIG_ENDIAN:
314 return cpu_to_be32(x);
315 }
316 return x;
317}
318
319static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
320{
321 switch (mdp->edmac_endian) {
322 case EDMAC_LITTLE_ENDIAN:
323 return le32_to_cpu(x);
324 case EDMAC_BIG_ENDIAN:
325 return be32_to_cpu(x);
326 }
327 return x;
328}
329
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700330/*
331 * Program the hardware MAC address from dev->dev_addr.
332 */
333static void update_mac_address(struct net_device *ndev)
334{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000335 sh_eth_write(ndev,
336 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
337 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
338 sh_eth_write(ndev,
339 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700340}
341
342/*
343 * Get MAC address from SuperH MAC address register
344 *
345 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
346 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
347 * When you want use this device, you must set MAC address in bootloader.
348 *
349 */
Magnus Damm748031f2009-10-09 00:17:14 +0000350static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700351{
Magnus Damm748031f2009-10-09 00:17:14 +0000352 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
353 memcpy(ndev->dev_addr, mac, 6);
354 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000355 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
356 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
357 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
358 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
359 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
360 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +0000361 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700362}
363
364struct bb_info {
365 struct mdiobb_ctrl ctrl;
366 u32 addr;
367 u32 mmd_msk;/* MMD */
368 u32 mdo_msk;
369 u32 mdi_msk;
370 u32 mdc_msk;
371};
372
373/* PHY bit set */
374static void bb_set(u32 addr, u32 msk)
375{
Paul Mundt900fcf02010-11-01 09:29:24 +0000376 writel(readl(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700377}
378
379/* PHY bit clear */
380static void bb_clr(u32 addr, u32 msk)
381{
Paul Mundt900fcf02010-11-01 09:29:24 +0000382 writel((readl(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700383}
384
385/* PHY bit read */
386static int bb_read(u32 addr, u32 msk)
387{
Paul Mundt900fcf02010-11-01 09:29:24 +0000388 return (readl(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700389}
390
391/* Data I/O pin control */
392static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
393{
394 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
395 if (bit)
396 bb_set(bitbang->addr, bitbang->mmd_msk);
397 else
398 bb_clr(bitbang->addr, bitbang->mmd_msk);
399}
400
401/* Set bit data*/
402static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
403{
404 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
405
406 if (bit)
407 bb_set(bitbang->addr, bitbang->mdo_msk);
408 else
409 bb_clr(bitbang->addr, bitbang->mdo_msk);
410}
411
412/* Get bit data*/
413static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
414{
415 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
416 return bb_read(bitbang->addr, bitbang->mdi_msk);
417}
418
419/* MDC pin control */
420static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
421{
422 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
423
424 if (bit)
425 bb_set(bitbang->addr, bitbang->mdc_msk);
426 else
427 bb_clr(bitbang->addr, bitbang->mdc_msk);
428}
429
430/* mdio bus control struct */
431static struct mdiobb_ops bb_ops = {
432 .owner = THIS_MODULE,
433 .set_mdc = sh_mdc_ctrl,
434 .set_mdio_dir = sh_mmd_ctrl,
435 .set_mdio_data = sh_set_mdio,
436 .get_mdio_data = sh_get_mdio,
437};
438
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700439/* free skb and descriptor buffer */
440static void sh_eth_ring_free(struct net_device *ndev)
441{
442 struct sh_eth_private *mdp = netdev_priv(ndev);
443 int i;
444
445 /* Free Rx skb ringbuffer */
446 if (mdp->rx_skbuff) {
447 for (i = 0; i < RX_RING_SIZE; i++) {
448 if (mdp->rx_skbuff[i])
449 dev_kfree_skb(mdp->rx_skbuff[i]);
450 }
451 }
452 kfree(mdp->rx_skbuff);
453
454 /* Free Tx skb ringbuffer */
455 if (mdp->tx_skbuff) {
456 for (i = 0; i < TX_RING_SIZE; i++) {
457 if (mdp->tx_skbuff[i])
458 dev_kfree_skb(mdp->tx_skbuff[i]);
459 }
460 }
461 kfree(mdp->tx_skbuff);
462}
463
464/* format skb and descriptor buffer */
465static void sh_eth_ring_format(struct net_device *ndev)
466{
467 struct sh_eth_private *mdp = netdev_priv(ndev);
468 int i;
469 struct sk_buff *skb;
470 struct sh_eth_rxdesc *rxdesc = NULL;
471 struct sh_eth_txdesc *txdesc = NULL;
472 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
473 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
474
475 mdp->cur_rx = mdp->cur_tx = 0;
476 mdp->dirty_rx = mdp->dirty_tx = 0;
477
478 memset(mdp->rx_ring, 0, rx_ringsize);
479
480 /* build Rx ring buffer */
481 for (i = 0; i < RX_RING_SIZE; i++) {
482 /* skb */
483 mdp->rx_skbuff[i] = NULL;
484 skb = dev_alloc_skb(mdp->rx_buf_sz);
485 mdp->rx_skbuff[i] = skb;
486 if (skb == NULL)
487 break;
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000488 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
489 DMA_FROM_DEVICE);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900490 skb->dev = ndev; /* Mark as being used by this device. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000491 sh_eth_set_receive_align(skb);
492
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700493 /* RX descriptor */
494 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000495 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400496 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700497
498 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000499 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900500 /* Rx descriptor address set */
501 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000502 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900503#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000504 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900505#endif
506 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700507 }
508
509 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
510
511 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400512 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700513
514 memset(mdp->tx_ring, 0, tx_ringsize);
515
516 /* build Tx ring buffer */
517 for (i = 0; i < TX_RING_SIZE; i++) {
518 mdp->tx_skbuff[i] = NULL;
519 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400520 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700521 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900522 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400523 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000524 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900525#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000526 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900527#endif
528 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700529 }
530
Yoshinori Sato71557a32008-08-06 19:49:00 -0400531 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700532}
533
534/* Get skb and descriptor buffer */
535static int sh_eth_ring_init(struct net_device *ndev)
536{
537 struct sh_eth_private *mdp = netdev_priv(ndev);
538 int rx_ringsize, tx_ringsize, ret = 0;
539
540 /*
541 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
542 * card needs room to do 8 byte alignment, +2 so we can reserve
543 * the first 2 bytes, and +16 gets room for the status word from the
544 * card.
545 */
546 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
547 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -0800548 if (mdp->cd->rpadir)
549 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700550
551 /* Allocate RX and TX skb rings */
552 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
553 GFP_KERNEL);
554 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000555 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700556 ret = -ENOMEM;
557 return ret;
558 }
559
560 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
561 GFP_KERNEL);
562 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000563 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700564 ret = -ENOMEM;
565 goto skb_ring_free;
566 }
567
568 /* Allocate all Rx descriptors. */
569 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
570 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
571 GFP_KERNEL);
572
573 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000574 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
575 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700576 ret = -ENOMEM;
577 goto desc_ring_free;
578 }
579
580 mdp->dirty_rx = 0;
581
582 /* Allocate all Tx descriptors. */
583 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
584 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
585 GFP_KERNEL);
586 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000587 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
588 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700589 ret = -ENOMEM;
590 goto desc_ring_free;
591 }
592 return ret;
593
594desc_ring_free:
595 /* free DMA buffer */
596 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
597
598skb_ring_free:
599 /* Free Rx and Tx skb ring buffer */
600 sh_eth_ring_free(ndev);
601
602 return ret;
603}
604
605static int sh_eth_dev_init(struct net_device *ndev)
606{
607 int ret = 0;
608 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700609 u_int32_t rx_int_var, tx_int_var;
610 u32 val;
611
612 /* Soft Reset */
613 sh_eth_reset(ndev);
614
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900615 /* Descriptor format */
616 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000617 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000618 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700619
620 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000621 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700622
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000623#if defined(__LITTLE_ENDIAN__)
624 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000625 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000626 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900627#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000628 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700629
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900630 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000631 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
632 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700633
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900634 /* Frame recv control */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000635 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700636
637 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
638 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000639 sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700640
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000641 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000642 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900643
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000644 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900645
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000646 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000647 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700648
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900649 /* Recv frame limit set register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000650 sh_eth_write(ndev, RFLR_VALUE, RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700651
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000652 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
653 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700654
655 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000656 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700657 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
658
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000659 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900660
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000661 if (mdp->cd->set_rate)
662 mdp->cd->set_rate(ndev);
663
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900664 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000665 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900666
667 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000668 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700669
670 /* Set MAC address */
671 update_mac_address(ndev);
672
673 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000674 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000675 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000676 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000677 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000678 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000679 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900680
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700681 /* Setting the Rx mode will start the Rx process. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000682 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700683
684 netif_start_queue(ndev);
685
686 return ret;
687}
688
689/* free Tx skb function */
690static int sh_eth_txfree(struct net_device *ndev)
691{
692 struct sh_eth_private *mdp = netdev_priv(ndev);
693 struct sh_eth_txdesc *txdesc;
694 int freeNum = 0;
695 int entry = 0;
696
697 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
698 entry = mdp->dirty_tx % TX_RING_SIZE;
699 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400700 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700701 break;
702 /* Free the original skb. */
703 if (mdp->tx_skbuff[entry]) {
704 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
705 mdp->tx_skbuff[entry] = NULL;
706 freeNum++;
707 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400708 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700709 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400710 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700711
712 mdp->stats.tx_packets++;
713 mdp->stats.tx_bytes += txdesc->buffer_length;
714 }
715 return freeNum;
716}
717
718/* Packet receive function */
719static int sh_eth_rx(struct net_device *ndev)
720{
721 struct sh_eth_private *mdp = netdev_priv(ndev);
722 struct sh_eth_rxdesc *rxdesc;
723
724 int entry = mdp->cur_rx % RX_RING_SIZE;
725 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
726 struct sk_buff *skb;
727 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000728 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700729
730 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400731 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
732 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700733 pkt_len = rxdesc->frame_length;
734
735 if (--boguscnt < 0)
736 break;
737
738 if (!(desc_status & RDFEND))
739 mdp->stats.rx_length_errors++;
740
741 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
742 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
743 mdp->stats.rx_errors++;
744 if (desc_status & RD_RFS1)
745 mdp->stats.rx_crc_errors++;
746 if (desc_status & RD_RFS2)
747 mdp->stats.rx_frame_errors++;
748 if (desc_status & RD_RFS3)
749 mdp->stats.rx_length_errors++;
750 if (desc_status & RD_RFS4)
751 mdp->stats.rx_length_errors++;
752 if (desc_status & RD_RFS6)
753 mdp->stats.rx_missed_errors++;
754 if (desc_status & RD_RFS10)
755 mdp->stats.rx_over_errors++;
756 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000757 if (!mdp->cd->hw_swap)
758 sh_eth_soft_swap(
759 phys_to_virt(ALIGN(rxdesc->addr, 4)),
760 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700761 skb = mdp->rx_skbuff[entry];
762 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -0800763 if (mdp->cd->rpadir)
764 skb_reserve(skb, NET_IP_ALIGN);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700765 skb_put(skb, pkt_len);
766 skb->protocol = eth_type_trans(skb, ndev);
767 netif_rx(skb);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700768 mdp->stats.rx_packets++;
769 mdp->stats.rx_bytes += pkt_len;
770 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400771 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700772 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000773 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700774 }
775
776 /* Refill the Rx ring buffers. */
777 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
778 entry = mdp->dirty_rx % RX_RING_SIZE;
779 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900780 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000781 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900782
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700783 if (mdp->rx_skbuff[entry] == NULL) {
784 skb = dev_alloc_skb(mdp->rx_buf_sz);
785 mdp->rx_skbuff[entry] = skb;
786 if (skb == NULL)
787 break; /* Better luck next round. */
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000788 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
789 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700790 skb->dev = ndev;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000791 sh_eth_set_receive_align(skb);
792
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700793 skb_checksum_none_assert(skb);
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000794 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700795 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700796 if (entry >= RX_RING_SIZE - 1)
797 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400798 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700799 else
800 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400801 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700802 }
803
804 /* Restart Rx engine if stopped. */
805 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000806 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R))
807 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700808
809 return 0;
810}
811
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000812static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000813{
814 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000815 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
816 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000817}
818
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000819static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000820{
821 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000822 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
823 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000824}
825
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700826/* error control function */
827static void sh_eth_error(struct net_device *ndev, int intr_status)
828{
829 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700830 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000831 u32 link_stat;
832 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700833
834 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000835 felic_stat = sh_eth_read(ndev, ECSR);
836 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700837 if (felic_stat & ECSR_ICD)
838 mdp->stats.tx_carrier_errors++;
839 if (felic_stat & ECSR_LCHNG) {
840 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000841 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000842 if (mdp->link == PHY_DOWN)
843 link_stat = 0;
844 else
845 link_stat = PHY_ST_LINK;
846 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000847 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000848 if (mdp->ether_link_active_low)
849 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000850 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000851 if (!(link_stat & PHY_ST_LINK))
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000852 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000853 else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700854 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000855 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
856 ~DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700857 /*clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000858 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
859 ECSR);
860 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
861 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700862 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000863 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700864 }
865 }
866 }
867
868 if (intr_status & EESR_TWB) {
869 /* Write buck end. unused write back interrupt */
870 if (intr_status & EESR_TABT) /* Transmit Abort int */
871 mdp->stats.tx_aborted_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000872 if (netif_msg_tx_err(mdp))
873 dev_err(&ndev->dev, "Transmit Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700874 }
875
876 if (intr_status & EESR_RABT) {
877 /* Receive Abort int */
878 if (intr_status & EESR_RFRMER) {
879 /* Receive Frame Overflow int */
880 mdp->stats.rx_frame_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000881 if (netif_msg_rx_err(mdp))
882 dev_err(&ndev->dev, "Receive Abort\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700883 }
884 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000885
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000886 if (intr_status & EESR_TDE) {
887 /* Transmit Descriptor Empty int */
888 mdp->stats.tx_fifo_errors++;
889 if (netif_msg_tx_err(mdp))
890 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
891 }
892
893 if (intr_status & EESR_TFE) {
894 /* FIFO under flow */
895 mdp->stats.tx_fifo_errors++;
896 if (netif_msg_tx_err(mdp))
897 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700898 }
899
900 if (intr_status & EESR_RDE) {
901 /* Receive Descriptor Empty int */
902 mdp->stats.rx_over_errors++;
903
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000904 if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R)
905 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000906 if (netif_msg_rx_err(mdp))
907 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700908 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000909
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700910 if (intr_status & EESR_RFE) {
911 /* Receive FIFO Overflow int */
912 mdp->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +0000913 if (netif_msg_rx_err(mdp))
914 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
915 }
916
917 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
918 /* Address Error */
919 mdp->stats.tx_fifo_errors++;
920 if (netif_msg_tx_err(mdp))
921 dev_err(&ndev->dev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700922 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000923
924 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
925 if (mdp->cd->no_ade)
926 mask &= ~EESR_ADE;
927 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700928 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000929 u32 edtrr = sh_eth_read(ndev, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700930 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000931 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
932 intr_status, mdp->cur_tx);
933 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700934 mdp->dirty_tx, (u32) ndev->state, edtrr);
935 /* dirty buffer free */
936 sh_eth_txfree(ndev);
937
938 /* SH7712 BUG */
939 if (edtrr ^ EDTRR_TRNS) {
940 /* tx dma start */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000941 sh_eth_write(ndev, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700942 }
943 /* wakeup */
944 netif_wake_queue(ndev);
945 }
946}
947
948static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
949{
950 struct net_device *ndev = netdev;
951 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000952 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000953 irqreturn_t ret = IRQ_NONE;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000954 u32 intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700955
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 spin_lock(&mdp->lock);
957
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900958 /* Get interrpt stat */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000959 intr_status = sh_eth_read(ndev, EESR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700960 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000961 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
962 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000963 cd->tx_check | cd->eesr_err_check)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000964 sh_eth_write(ndev, intr_status, EESR);
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000965 ret = IRQ_HANDLED;
966 } else
967 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700968
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900969 if (intr_status & (EESR_FRC | /* Frame recv*/
970 EESR_RMAF | /* Multi cast address recv*/
971 EESR_RRF | /* Bit frame recv */
972 EESR_RTLF | /* Long frame recv*/
973 EESR_RTSF | /* short frame recv */
974 EESR_PRE | /* PHY-LSI recv error */
975 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700976 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900977 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700978
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900979 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000980 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700981 sh_eth_txfree(ndev);
982 netif_wake_queue(ndev);
983 }
984
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000985 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700986 sh_eth_error(ndev, intr_status);
987
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000988other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700989 spin_unlock(&mdp->lock);
990
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000991 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700992}
993
994static void sh_eth_timer(unsigned long data)
995{
996 struct net_device *ndev = (struct net_device *)data;
997 struct sh_eth_private *mdp = netdev_priv(ndev);
998
999 mod_timer(&mdp->timer, jiffies + (10 * HZ));
1000}
1001
1002/* PHY state control function */
1003static void sh_eth_adjust_link(struct net_device *ndev)
1004{
1005 struct sh_eth_private *mdp = netdev_priv(ndev);
1006 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001007 int new_state = 0;
1008
1009 if (phydev->link != PHY_DOWN) {
1010 if (phydev->duplex != mdp->duplex) {
1011 new_state = 1;
1012 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001013 if (mdp->cd->set_duplex)
1014 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001015 }
1016
1017 if (phydev->speed != mdp->speed) {
1018 new_state = 1;
1019 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001020 if (mdp->cd->set_rate)
1021 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001022 }
1023 if (mdp->link == PHY_DOWN) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001024 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_TXF)
1025 | ECMR_DM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001026 new_state = 1;
1027 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001028 }
1029 } else if (mdp->link) {
1030 new_state = 1;
1031 mdp->link = PHY_DOWN;
1032 mdp->speed = 0;
1033 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001034 }
1035
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001036 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001037 phy_print_status(phydev);
1038}
1039
1040/* PHY init function */
1041static int sh_eth_phy_init(struct net_device *ndev)
1042{
1043 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -07001044 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001045 struct phy_device *phydev = NULL;
1046
Kay Sieversfb28ad32008-11-10 13:55:14 -08001047 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001048 mdp->mii_bus->id , mdp->phy_id);
1049
1050 mdp->link = PHY_DOWN;
1051 mdp->speed = 0;
1052 mdp->duplex = -1;
1053
1054 /* Try connect to PHY */
Joe Perchesc061b182010-08-23 18:20:03 +00001055 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001056 0, PHY_INTERFACE_MODE_MII);
1057 if (IS_ERR(phydev)) {
1058 dev_err(&ndev->dev, "phy_connect failed\n");
1059 return PTR_ERR(phydev);
1060 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001061
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001062 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001063 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001064
1065 mdp->phydev = phydev;
1066
1067 return 0;
1068}
1069
1070/* PHY control start function */
1071static int sh_eth_phy_start(struct net_device *ndev)
1072{
1073 struct sh_eth_private *mdp = netdev_priv(ndev);
1074 int ret;
1075
1076 ret = sh_eth_phy_init(ndev);
1077 if (ret)
1078 return ret;
1079
1080 /* reset phy - this also wakes it from PDOWN */
1081 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1082 phy_start(mdp->phydev);
1083
1084 return 0;
1085}
1086
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001087static int sh_eth_get_settings(struct net_device *ndev,
1088 struct ethtool_cmd *ecmd)
1089{
1090 struct sh_eth_private *mdp = netdev_priv(ndev);
1091 unsigned long flags;
1092 int ret;
1093
1094 spin_lock_irqsave(&mdp->lock, flags);
1095 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1096 spin_unlock_irqrestore(&mdp->lock, flags);
1097
1098 return ret;
1099}
1100
1101static int sh_eth_set_settings(struct net_device *ndev,
1102 struct ethtool_cmd *ecmd)
1103{
1104 struct sh_eth_private *mdp = netdev_priv(ndev);
1105 unsigned long flags;
1106 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001107
1108 spin_lock_irqsave(&mdp->lock, flags);
1109
1110 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001111 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001112
1113 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1114 if (ret)
1115 goto error_exit;
1116
1117 if (ecmd->duplex == DUPLEX_FULL)
1118 mdp->duplex = 1;
1119 else
1120 mdp->duplex = 0;
1121
1122 if (mdp->cd->set_duplex)
1123 mdp->cd->set_duplex(ndev);
1124
1125error_exit:
1126 mdelay(1);
1127
1128 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001129 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001130
1131 spin_unlock_irqrestore(&mdp->lock, flags);
1132
1133 return ret;
1134}
1135
1136static int sh_eth_nway_reset(struct net_device *ndev)
1137{
1138 struct sh_eth_private *mdp = netdev_priv(ndev);
1139 unsigned long flags;
1140 int ret;
1141
1142 spin_lock_irqsave(&mdp->lock, flags);
1143 ret = phy_start_aneg(mdp->phydev);
1144 spin_unlock_irqrestore(&mdp->lock, flags);
1145
1146 return ret;
1147}
1148
1149static u32 sh_eth_get_msglevel(struct net_device *ndev)
1150{
1151 struct sh_eth_private *mdp = netdev_priv(ndev);
1152 return mdp->msg_enable;
1153}
1154
1155static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1156{
1157 struct sh_eth_private *mdp = netdev_priv(ndev);
1158 mdp->msg_enable = value;
1159}
1160
1161static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1162 "rx_current", "tx_current",
1163 "rx_dirty", "tx_dirty",
1164};
1165#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1166
1167static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1168{
1169 switch (sset) {
1170 case ETH_SS_STATS:
1171 return SH_ETH_STATS_LEN;
1172 default:
1173 return -EOPNOTSUPP;
1174 }
1175}
1176
1177static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1178 struct ethtool_stats *stats, u64 *data)
1179{
1180 struct sh_eth_private *mdp = netdev_priv(ndev);
1181 int i = 0;
1182
1183 /* device-specific stats */
1184 data[i++] = mdp->cur_rx;
1185 data[i++] = mdp->cur_tx;
1186 data[i++] = mdp->dirty_rx;
1187 data[i++] = mdp->dirty_tx;
1188}
1189
1190static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1191{
1192 switch (stringset) {
1193 case ETH_SS_STATS:
1194 memcpy(data, *sh_eth_gstrings_stats,
1195 sizeof(sh_eth_gstrings_stats));
1196 break;
1197 }
1198}
1199
1200static struct ethtool_ops sh_eth_ethtool_ops = {
1201 .get_settings = sh_eth_get_settings,
1202 .set_settings = sh_eth_set_settings,
1203 .nway_reset = sh_eth_nway_reset,
1204 .get_msglevel = sh_eth_get_msglevel,
1205 .set_msglevel = sh_eth_set_msglevel,
1206 .get_link = ethtool_op_get_link,
1207 .get_strings = sh_eth_get_strings,
1208 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1209 .get_sset_count = sh_eth_get_sset_count,
1210};
1211
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212/* network device open function */
1213static int sh_eth_open(struct net_device *ndev)
1214{
1215 int ret = 0;
1216 struct sh_eth_private *mdp = netdev_priv(ndev);
1217
Magnus Dammbcd51492009-10-09 00:20:04 +00001218 pm_runtime_get_sync(&mdp->pdev->dev);
1219
Joe Perchesa0607fd2009-11-18 23:29:17 -08001220 ret = request_irq(ndev->irq, sh_eth_interrupt,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +00001221#if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001222 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1223 defined(CONFIG_CPU_SUBTYPE_SH7757)
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001224 IRQF_SHARED,
1225#else
1226 0,
1227#endif
1228 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001229 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001230 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231 return ret;
1232 }
1233
1234 /* Descriptor set */
1235 ret = sh_eth_ring_init(ndev);
1236 if (ret)
1237 goto out_free_irq;
1238
1239 /* device init */
1240 ret = sh_eth_dev_init(ndev);
1241 if (ret)
1242 goto out_free_irq;
1243
1244 /* PHY control start*/
1245 ret = sh_eth_phy_start(ndev);
1246 if (ret)
1247 goto out_free_irq;
1248
1249 /* Set the timer to check for link beat. */
1250 init_timer(&mdp->timer);
1251 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001252 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001253
1254 return ret;
1255
1256out_free_irq:
1257 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001258 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001259 return ret;
1260}
1261
1262/* Timeout function */
1263static void sh_eth_tx_timeout(struct net_device *ndev)
1264{
1265 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001266 struct sh_eth_rxdesc *rxdesc;
1267 int i;
1268
1269 netif_stop_queue(ndev);
1270
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001271 if (netif_msg_timer(mdp))
1272 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001273 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001274
1275 /* tx_errors count up */
1276 mdp->stats.tx_errors++;
1277
1278 /* timer off */
1279 del_timer_sync(&mdp->timer);
1280
1281 /* Free all the skbuffs in the Rx queue. */
1282 for (i = 0; i < RX_RING_SIZE; i++) {
1283 rxdesc = &mdp->rx_ring[i];
1284 rxdesc->status = 0;
1285 rxdesc->addr = 0xBADF00D0;
1286 if (mdp->rx_skbuff[i])
1287 dev_kfree_skb(mdp->rx_skbuff[i]);
1288 mdp->rx_skbuff[i] = NULL;
1289 }
1290 for (i = 0; i < TX_RING_SIZE; i++) {
1291 if (mdp->tx_skbuff[i])
1292 dev_kfree_skb(mdp->tx_skbuff[i]);
1293 mdp->tx_skbuff[i] = NULL;
1294 }
1295
1296 /* device init */
1297 sh_eth_dev_init(ndev);
1298
1299 /* timer on */
1300 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1301 add_timer(&mdp->timer);
1302}
1303
1304/* Packet transmit function */
1305static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1306{
1307 struct sh_eth_private *mdp = netdev_priv(ndev);
1308 struct sh_eth_txdesc *txdesc;
1309 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001310 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001311
1312 spin_lock_irqsave(&mdp->lock, flags);
1313 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1314 if (!sh_eth_txfree(ndev)) {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001315 if (netif_msg_tx_queued(mdp))
1316 dev_warn(&ndev->dev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001317 netif_stop_queue(ndev);
1318 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001319 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001320 }
1321 }
1322 spin_unlock_irqrestore(&mdp->lock, flags);
1323
1324 entry = mdp->cur_tx % TX_RING_SIZE;
1325 mdp->tx_skbuff[entry] = skb;
1326 txdesc = &mdp->tx_ring[entry];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001327 txdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001328 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001329 if (!mdp->cd->hw_swap)
1330 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1331 skb->len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001332 /* write back */
1333 __flush_purge_region(skb->data, skb->len);
1334 if (skb->len < ETHERSMALL)
1335 txdesc->buffer_length = ETHERSMALL;
1336 else
1337 txdesc->buffer_length = skb->len;
1338
1339 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001340 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001341 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001342 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001343
1344 mdp->cur_tx++;
1345
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001346 if (!(sh_eth_read(ndev, EDTRR) & EDTRR_TRNS))
1347 sh_eth_write(ndev, EDTRR_TRNS, EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001348
Patrick McHardy6ed10652009-06-23 06:03:08 +00001349 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001350}
1351
1352/* device close function */
1353static int sh_eth_close(struct net_device *ndev)
1354{
1355 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001356 int ringsize;
1357
1358 netif_stop_queue(ndev);
1359
1360 /* Disable interrupts by clearing the interrupt mask. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001361 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001362
1363 /* Stop the chip's Tx and Rx processes. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001364 sh_eth_write(ndev, 0, EDTRR);
1365 sh_eth_write(ndev, 0, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001366
1367 /* PHY Disconnect */
1368 if (mdp->phydev) {
1369 phy_stop(mdp->phydev);
1370 phy_disconnect(mdp->phydev);
1371 }
1372
1373 free_irq(ndev->irq, ndev);
1374
1375 del_timer_sync(&mdp->timer);
1376
1377 /* Free all the skbuffs in the Rx queue. */
1378 sh_eth_ring_free(ndev);
1379
1380 /* free DMA buffer */
1381 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1382 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1383
1384 /* free DMA buffer */
1385 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1386 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1387
Magnus Dammbcd51492009-10-09 00:20:04 +00001388 pm_runtime_put_sync(&mdp->pdev->dev);
1389
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390 return 0;
1391}
1392
1393static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1394{
1395 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001396
Magnus Dammbcd51492009-10-09 00:20:04 +00001397 pm_runtime_get_sync(&mdp->pdev->dev);
1398
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001399 mdp->stats.tx_dropped += sh_eth_read(ndev, TROCR);
1400 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
1401 mdp->stats.collisions += sh_eth_read(ndev, CDCR);
1402 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
1403 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
1404 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001405#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001406 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);/* CERCR */
1407 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
1408 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);/* CEECR */
1409 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001410#else
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001411 mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
1412 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001413#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00001414 pm_runtime_put_sync(&mdp->pdev->dev);
1415
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001416 return &mdp->stats;
1417}
1418
1419/* ioctl to device funciotn*/
1420static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1421 int cmd)
1422{
1423 struct sh_eth_private *mdp = netdev_priv(ndev);
1424 struct phy_device *phydev = mdp->phydev;
1425
1426 if (!netif_running(ndev))
1427 return -EINVAL;
1428
1429 if (!phydev)
1430 return -ENODEV;
1431
Richard Cochran28b04112010-07-17 08:48:55 +00001432 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433}
1434
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001435#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001436/* Multicast reception directions set */
1437static void sh_eth_set_multicast_list(struct net_device *ndev)
1438{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001439 if (ndev->flags & IFF_PROMISC) {
1440 /* Set promiscuous. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001441 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) |
1442 ECMR_PRM, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001443 } else {
1444 /* Normal, unicast/broadcast-only mode. */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001445 sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) |
1446 ECMR_MCT, ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447 }
1448}
1449
1450/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001451static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001452{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001453 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
1454 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
1455 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
1456 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
1457 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
1458 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
1459 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
1460 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
1461 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
1462 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001463#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001464 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
1465 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001466#else
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001467 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
1468 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001469#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001470 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
1471 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
1472 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
1473 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
1474 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
1475 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
1476 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001478#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001479
1480/* MDIO bus release function */
1481static int sh_mdio_release(struct net_device *ndev)
1482{
1483 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1484
1485 /* unregister mdio bus */
1486 mdiobus_unregister(bus);
1487
1488 /* remove mdio bus info from net_device */
1489 dev_set_drvdata(&ndev->dev, NULL);
1490
Denis Kirjanov0f0b4052010-05-20 04:00:59 +00001491 /* free interrupts memory */
1492 kfree(bus->irq);
1493
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001494 /* free bitbang info */
1495 free_mdio_bitbang(bus);
1496
1497 return 0;
1498}
1499
1500/* MDIO bus init function */
1501static int sh_mdio_init(struct net_device *ndev, int id)
1502{
1503 int ret, i;
1504 struct bb_info *bitbang;
1505 struct sh_eth_private *mdp = netdev_priv(ndev);
1506
1507 /* create bit control struct for PHY */
1508 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1509 if (!bitbang) {
1510 ret = -ENOMEM;
1511 goto out;
1512 }
1513
1514 /* bitbang init */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001515 bitbang->addr = ndev->base_addr + mdp->reg_offset[PIR];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001516 bitbang->mdi_msk = 0x08;
1517 bitbang->mdo_msk = 0x04;
1518 bitbang->mmd_msk = 0x02;/* MMD */
1519 bitbang->mdc_msk = 0x01;
1520 bitbang->ctrl.ops = &bb_ops;
1521
Stefan Weilc2e07b32010-08-03 19:44:52 +02001522 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001523 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1524 if (!mdp->mii_bus) {
1525 ret = -ENOMEM;
1526 goto out_free_bitbang;
1527 }
1528
1529 /* Hook up MII support for ethtool */
1530 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001531 mdp->mii_bus->parent = &ndev->dev;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001532 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001533
1534 /* PHY IRQ */
1535 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1536 if (!mdp->mii_bus->irq) {
1537 ret = -ENOMEM;
1538 goto out_free_bus;
1539 }
1540
1541 for (i = 0; i < PHY_MAX_ADDR; i++)
1542 mdp->mii_bus->irq[i] = PHY_POLL;
1543
1544 /* regist mdio bus */
1545 ret = mdiobus_register(mdp->mii_bus);
1546 if (ret)
1547 goto out_free_irq;
1548
1549 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1550
1551 return 0;
1552
1553out_free_irq:
1554 kfree(mdp->mii_bus->irq);
1555
1556out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001557 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001558
1559out_free_bitbang:
1560 kfree(bitbang);
1561
1562out:
1563 return ret;
1564}
1565
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001566static const u16 *sh_eth_get_register_offset(int register_type)
1567{
1568 const u16 *reg_offset = NULL;
1569
1570 switch (register_type) {
1571 case SH_ETH_REG_GIGABIT:
1572 reg_offset = sh_eth_offset_gigabit;
1573 break;
1574 case SH_ETH_REG_FAST_SH4:
1575 reg_offset = sh_eth_offset_fast_sh4;
1576 break;
1577 case SH_ETH_REG_FAST_SH3_SH2:
1578 reg_offset = sh_eth_offset_fast_sh3_sh2;
1579 break;
1580 default:
1581 printk(KERN_ERR "Unknown register type (%d)\n", register_type);
1582 break;
1583 }
1584
1585 return reg_offset;
1586}
1587
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001588static const struct net_device_ops sh_eth_netdev_ops = {
1589 .ndo_open = sh_eth_open,
1590 .ndo_stop = sh_eth_close,
1591 .ndo_start_xmit = sh_eth_start_xmit,
1592 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001593#if defined(SH_ETH_HAS_TSU)
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001594 .ndo_set_multicast_list = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001595#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001596 .ndo_tx_timeout = sh_eth_tx_timeout,
1597 .ndo_do_ioctl = sh_eth_do_ioctl,
1598 .ndo_validate_addr = eth_validate_addr,
1599 .ndo_set_mac_address = eth_mac_addr,
1600 .ndo_change_mtu = eth_change_mtu,
1601};
1602
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001603static int sh_eth_drv_probe(struct platform_device *pdev)
1604{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07001605 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001606 struct resource *res;
1607 struct net_device *ndev = NULL;
1608 struct sh_eth_private *mdp;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001609 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001610
1611 /* get base addr */
1612 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1613 if (unlikely(res == NULL)) {
1614 dev_err(&pdev->dev, "invalid resource\n");
1615 ret = -EINVAL;
1616 goto out;
1617 }
1618
1619 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1620 if (!ndev) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001621 dev_err(&pdev->dev, "Could not allocate device.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622 ret = -ENOMEM;
1623 goto out;
1624 }
1625
1626 /* The sh Ether-specific entries in the device structure. */
1627 ndev->base_addr = res->start;
1628 devno = pdev->id;
1629 if (devno < 0)
1630 devno = 0;
1631
1632 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001633 ret = platform_get_irq(pdev, 0);
1634 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001635 ret = -ENODEV;
1636 goto out_release;
1637 }
roel kluincc3c0802008-09-10 19:22:44 +02001638 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001639
1640 SET_NETDEV_DEV(ndev, &pdev->dev);
1641
1642 /* Fill in the fields of the device structure with ethernet values. */
1643 ether_setup(ndev);
1644
1645 mdp = netdev_priv(ndev);
1646 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001647 mdp->pdev = pdev;
1648 pm_runtime_enable(&pdev->dev);
1649 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001650
Yoshinori Sato71557a32008-08-06 19:49:00 -04001651 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001652 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001653 mdp->phy_id = pd->phy;
1654 /* EDMAC endian */
1655 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001656 mdp->no_ether_link = pd->no_ether_link;
1657 mdp->ether_link_active_low = pd->ether_link_active_low;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001658 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001659
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001660 /* set cpu data */
1661 mdp->cd = &sh_eth_my_cpu_data;
1662 sh_eth_set_default_cpu_data(mdp->cd);
1663
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001664 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001665 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001666 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001667 ndev->watchdog_timeo = TX_TIMEOUT;
1668
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001669 /* debug message level */
1670 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001671 mdp->post_rx = POST_RX >> (devno << 1);
1672 mdp->post_fw = POST_FW >> (devno << 1);
1673
1674 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001675 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001676
1677 /* First device only init */
1678 if (!devno) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001679 if (mdp->cd->chip_reset)
1680 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001681
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001682#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 /* TSU init (Init only)*/
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001684 mdp->tsu_addr = SH_TSU_ADDR;
1685 sh_eth_tsu_init(mdp);
Yoshinori Sato71557a32008-08-06 19:49:00 -04001686#endif
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001687 }
1688
1689 /* network device register */
1690 ret = register_netdev(ndev);
1691 if (ret)
1692 goto out_release;
1693
1694 /* mdio bus init */
1695 ret = sh_mdio_init(ndev, pdev->id);
1696 if (ret)
1697 goto out_unregister;
1698
H Hartley Sweeten6cd9b492009-12-29 20:10:35 -08001699 /* print device infomation */
1700 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1701 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001702
1703 platform_set_drvdata(pdev, ndev);
1704
1705 return ret;
1706
1707out_unregister:
1708 unregister_netdev(ndev);
1709
1710out_release:
1711 /* net_dev free */
1712 if (ndev)
1713 free_netdev(ndev);
1714
1715out:
1716 return ret;
1717}
1718
1719static int sh_eth_drv_remove(struct platform_device *pdev)
1720{
1721 struct net_device *ndev = platform_get_drvdata(pdev);
1722
1723 sh_mdio_release(ndev);
1724 unregister_netdev(ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001725 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001726 free_netdev(ndev);
1727 platform_set_drvdata(pdev, NULL);
1728
1729 return 0;
1730}
1731
Magnus Dammbcd51492009-10-09 00:20:04 +00001732static int sh_eth_runtime_nop(struct device *dev)
1733{
1734 /*
1735 * Runtime PM callback shared between ->runtime_suspend()
1736 * and ->runtime_resume(). Simply returns success.
1737 *
1738 * This driver re-initializes all registers after
1739 * pm_runtime_get_sync() anyway so there is no need
1740 * to save and restore registers here.
1741 */
1742 return 0;
1743}
1744
1745static struct dev_pm_ops sh_eth_dev_pm_ops = {
1746 .runtime_suspend = sh_eth_runtime_nop,
1747 .runtime_resume = sh_eth_runtime_nop,
1748};
1749
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001750static struct platform_driver sh_eth_driver = {
1751 .probe = sh_eth_drv_probe,
1752 .remove = sh_eth_drv_remove,
1753 .driver = {
1754 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001755 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001756 },
1757};
1758
1759static int __init sh_eth_init(void)
1760{
1761 return platform_driver_register(&sh_eth_driver);
1762}
1763
1764static void __exit sh_eth_cleanup(void)
1765{
1766 platform_driver_unregister(&sh_eth_driver);
1767}
1768
1769module_init(sh_eth_init);
1770module_exit(sh_eth_cleanup);
1771
1772MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1773MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1774MODULE_LICENSE("GPL v2");