blob: 161181a4b3d6cbd9713315eff8874e6094809ea1 [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>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070034#include "sh_eth.h"
35
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000036/* There is CPU dependent code */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +000037#if defined(CONFIG_CPU_SUBTYPE_SH7724)
38#define SH_ETH_RESET_DEFAULT 1
39static void sh_eth_set_duplex(struct net_device *ndev)
40{
41 struct sh_eth_private *mdp = netdev_priv(ndev);
42 u32 ioaddr = ndev->base_addr;
43
44 if (mdp->duplex) /* Full */
45 ctrl_outl(ctrl_inl(ioaddr + ECMR) | ECMR_DM, ioaddr + ECMR);
46 else /* Half */
47 ctrl_outl(ctrl_inl(ioaddr + ECMR) & ~ECMR_DM, ioaddr + ECMR);
48}
49
50static void sh_eth_set_rate(struct net_device *ndev)
51{
52 struct sh_eth_private *mdp = netdev_priv(ndev);
53 u32 ioaddr = ndev->base_addr;
54
55 switch (mdp->speed) {
56 case 10: /* 10BASE */
57 ctrl_outl(ctrl_inl(ioaddr + ECMR) & ~ECMR_RTM, ioaddr + ECMR);
58 break;
59 case 100:/* 100BASE */
60 ctrl_outl(ctrl_inl(ioaddr + ECMR) | ECMR_RTM, ioaddr + ECMR);
61 break;
62 default:
63 break;
64 }
65}
66
67/* SH7724 */
68static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
69 .set_duplex = sh_eth_set_duplex,
70 .set_rate = sh_eth_set_rate,
71
72 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
73 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
74 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
75
76 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
77 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
78 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
79 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
80
81 .apr = 1,
82 .mpr = 1,
83 .tpauser = 1,
84 .hw_swap = 1,
85};
86
87#elif defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +000088#define SH_ETH_HAS_TSU 1
89static void sh_eth_chip_reset(struct net_device *ndev)
90{
91 /* reset device */
92 ctrl_outl(ARSTR_ARSTR, ARSTR);
93 mdelay(1);
94}
95
96static void sh_eth_reset(struct net_device *ndev)
97{
98 u32 ioaddr = ndev->base_addr;
99 int cnt = 100;
100
101 ctrl_outl(EDSR_ENALL, ioaddr + EDSR);
102 ctrl_outl(ctrl_inl(ioaddr + EDMR) | EDMR_SRST, ioaddr + EDMR);
103 while (cnt > 0) {
104 if (!(ctrl_inl(ioaddr + EDMR) & 0x3))
105 break;
106 mdelay(1);
107 cnt--;
108 }
109 if (cnt < 0)
110 printk(KERN_ERR "Device reset fail\n");
111
112 /* Table Init */
113 ctrl_outl(0x0, ioaddr + TDLAR);
114 ctrl_outl(0x0, ioaddr + TDFAR);
115 ctrl_outl(0x0, ioaddr + TDFXR);
116 ctrl_outl(0x0, ioaddr + TDFFR);
117 ctrl_outl(0x0, ioaddr + RDLAR);
118 ctrl_outl(0x0, ioaddr + RDFAR);
119 ctrl_outl(0x0, ioaddr + RDFXR);
120 ctrl_outl(0x0, ioaddr + RDFFR);
121}
122
123static void sh_eth_set_duplex(struct net_device *ndev)
124{
125 struct sh_eth_private *mdp = netdev_priv(ndev);
126 u32 ioaddr = ndev->base_addr;
127
128 if (mdp->duplex) /* Full */
129 ctrl_outl(ctrl_inl(ioaddr + ECMR) | ECMR_DM, ioaddr + ECMR);
130 else /* Half */
131 ctrl_outl(ctrl_inl(ioaddr + ECMR) & ~ECMR_DM, ioaddr + ECMR);
132}
133
134static void sh_eth_set_rate(struct net_device *ndev)
135{
136 struct sh_eth_private *mdp = netdev_priv(ndev);
137 u32 ioaddr = ndev->base_addr;
138
139 switch (mdp->speed) {
140 case 10: /* 10BASE */
141 ctrl_outl(GECMR_10, ioaddr + GECMR);
142 break;
143 case 100:/* 100BASE */
144 ctrl_outl(GECMR_100, ioaddr + GECMR);
145 break;
146 case 1000: /* 1000BASE */
147 ctrl_outl(GECMR_1000, ioaddr + GECMR);
148 break;
149 default:
150 break;
151 }
152}
153
154/* sh7763 */
155static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
156 .chip_reset = sh_eth_chip_reset,
157 .set_duplex = sh_eth_set_duplex,
158 .set_rate = sh_eth_set_rate,
159
160 .ecsr_value = ECSR_ICD | ECSR_MPD,
161 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
162 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
163
164 .tx_check = EESR_TC1 | EESR_FTC,
165 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
166 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
167 EESR_ECI,
168 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
169 EESR_TFE,
170
171 .apr = 1,
172 .mpr = 1,
173 .tpauser = 1,
174 .bculr = 1,
175 .hw_swap = 1,
176 .rpadir = 1,
177 .no_trimd = 1,
178 .no_ade = 1,
179};
180
181#elif defined(CONFIG_CPU_SUBTYPE_SH7619)
182#define SH_ETH_RESET_DEFAULT 1
183static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
184 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
185
186 .apr = 1,
187 .mpr = 1,
188 .tpauser = 1,
189 .hw_swap = 1,
190};
191#elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
192#define SH_ETH_RESET_DEFAULT 1
193#define SH_ETH_HAS_TSU 1
194static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
195 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
196};
197#endif
198
199static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
200{
201 if (!cd->ecsr_value)
202 cd->ecsr_value = DEFAULT_ECSR_INIT;
203
204 if (!cd->ecsipr_value)
205 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
206
207 if (!cd->fcftr_value)
208 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
209 DEFAULT_FIFO_F_D_RFD;
210
211 if (!cd->fdr_value)
212 cd->fdr_value = DEFAULT_FDR_INIT;
213
214 if (!cd->rmcr_value)
215 cd->rmcr_value = DEFAULT_RMCR_VALUE;
216
217 if (!cd->tx_check)
218 cd->tx_check = DEFAULT_TX_CHECK;
219
220 if (!cd->eesr_err_check)
221 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
222
223 if (!cd->tx_error_check)
224 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
225}
226
227#if defined(SH_ETH_RESET_DEFAULT)
228/* Chip Reset */
229static void sh_eth_reset(struct net_device *ndev)
230{
231 u32 ioaddr = ndev->base_addr;
232
233 ctrl_outl(ctrl_inl(ioaddr + EDMR) | EDMR_SRST, ioaddr + EDMR);
234 mdelay(3);
235 ctrl_outl(ctrl_inl(ioaddr + EDMR) & ~EDMR_SRST, ioaddr + EDMR);
236}
237#endif
238
239#if defined(CONFIG_CPU_SH4)
240static void sh_eth_set_receive_align(struct sk_buff *skb)
241{
242 int reserve;
243
244 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
245 if (reserve)
246 skb_reserve(skb, reserve);
247}
248#else
249static void sh_eth_set_receive_align(struct sk_buff *skb)
250{
251 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
252}
253#endif
254
255
Yoshinori Sato71557a32008-08-06 19:49:00 -0400256/* CPU <-> EDMAC endian convert */
257static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
258{
259 switch (mdp->edmac_endian) {
260 case EDMAC_LITTLE_ENDIAN:
261 return cpu_to_le32(x);
262 case EDMAC_BIG_ENDIAN:
263 return cpu_to_be32(x);
264 }
265 return x;
266}
267
268static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
269{
270 switch (mdp->edmac_endian) {
271 case EDMAC_LITTLE_ENDIAN:
272 return le32_to_cpu(x);
273 case EDMAC_BIG_ENDIAN:
274 return be32_to_cpu(x);
275 }
276 return x;
277}
278
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700279/*
280 * Program the hardware MAC address from dev->dev_addr.
281 */
282static void update_mac_address(struct net_device *ndev)
283{
284 u32 ioaddr = ndev->base_addr;
285
286 ctrl_outl((ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
287 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]),
288 ioaddr + MAHR);
289 ctrl_outl((ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]),
290 ioaddr + MALR);
291}
292
293/*
294 * Get MAC address from SuperH MAC address register
295 *
296 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
297 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
298 * When you want use this device, you must set MAC address in bootloader.
299 *
300 */
Magnus Damm748031f2009-10-09 00:17:14 +0000301static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700302{
303 u32 ioaddr = ndev->base_addr;
304
Magnus Damm748031f2009-10-09 00:17:14 +0000305 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
306 memcpy(ndev->dev_addr, mac, 6);
307 } else {
308 ndev->dev_addr[0] = (ctrl_inl(ioaddr + MAHR) >> 24);
309 ndev->dev_addr[1] = (ctrl_inl(ioaddr + MAHR) >> 16) & 0xFF;
310 ndev->dev_addr[2] = (ctrl_inl(ioaddr + MAHR) >> 8) & 0xFF;
311 ndev->dev_addr[3] = (ctrl_inl(ioaddr + MAHR) & 0xFF);
312 ndev->dev_addr[4] = (ctrl_inl(ioaddr + MALR) >> 8) & 0xFF;
313 ndev->dev_addr[5] = (ctrl_inl(ioaddr + MALR) & 0xFF);
314 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700315}
316
317struct bb_info {
318 struct mdiobb_ctrl ctrl;
319 u32 addr;
320 u32 mmd_msk;/* MMD */
321 u32 mdo_msk;
322 u32 mdi_msk;
323 u32 mdc_msk;
324};
325
326/* PHY bit set */
327static void bb_set(u32 addr, u32 msk)
328{
329 ctrl_outl(ctrl_inl(addr) | msk, addr);
330}
331
332/* PHY bit clear */
333static void bb_clr(u32 addr, u32 msk)
334{
335 ctrl_outl((ctrl_inl(addr) & ~msk), addr);
336}
337
338/* PHY bit read */
339static int bb_read(u32 addr, u32 msk)
340{
341 return (ctrl_inl(addr) & msk) != 0;
342}
343
344/* Data I/O pin control */
345static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
346{
347 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
348 if (bit)
349 bb_set(bitbang->addr, bitbang->mmd_msk);
350 else
351 bb_clr(bitbang->addr, bitbang->mmd_msk);
352}
353
354/* Set bit data*/
355static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
356{
357 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
358
359 if (bit)
360 bb_set(bitbang->addr, bitbang->mdo_msk);
361 else
362 bb_clr(bitbang->addr, bitbang->mdo_msk);
363}
364
365/* Get bit data*/
366static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
367{
368 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
369 return bb_read(bitbang->addr, bitbang->mdi_msk);
370}
371
372/* MDC pin control */
373static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
374{
375 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
376
377 if (bit)
378 bb_set(bitbang->addr, bitbang->mdc_msk);
379 else
380 bb_clr(bitbang->addr, bitbang->mdc_msk);
381}
382
383/* mdio bus control struct */
384static struct mdiobb_ops bb_ops = {
385 .owner = THIS_MODULE,
386 .set_mdc = sh_mdc_ctrl,
387 .set_mdio_dir = sh_mmd_ctrl,
388 .set_mdio_data = sh_set_mdio,
389 .get_mdio_data = sh_get_mdio,
390};
391
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700392/* free skb and descriptor buffer */
393static void sh_eth_ring_free(struct net_device *ndev)
394{
395 struct sh_eth_private *mdp = netdev_priv(ndev);
396 int i;
397
398 /* Free Rx skb ringbuffer */
399 if (mdp->rx_skbuff) {
400 for (i = 0; i < RX_RING_SIZE; i++) {
401 if (mdp->rx_skbuff[i])
402 dev_kfree_skb(mdp->rx_skbuff[i]);
403 }
404 }
405 kfree(mdp->rx_skbuff);
406
407 /* Free Tx skb ringbuffer */
408 if (mdp->tx_skbuff) {
409 for (i = 0; i < TX_RING_SIZE; i++) {
410 if (mdp->tx_skbuff[i])
411 dev_kfree_skb(mdp->tx_skbuff[i]);
412 }
413 }
414 kfree(mdp->tx_skbuff);
415}
416
417/* format skb and descriptor buffer */
418static void sh_eth_ring_format(struct net_device *ndev)
419{
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000420 u32 ioaddr = ndev->base_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700421 struct sh_eth_private *mdp = netdev_priv(ndev);
422 int i;
423 struct sk_buff *skb;
424 struct sh_eth_rxdesc *rxdesc = NULL;
425 struct sh_eth_txdesc *txdesc = NULL;
426 int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE;
427 int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE;
428
429 mdp->cur_rx = mdp->cur_tx = 0;
430 mdp->dirty_rx = mdp->dirty_tx = 0;
431
432 memset(mdp->rx_ring, 0, rx_ringsize);
433
434 /* build Rx ring buffer */
435 for (i = 0; i < RX_RING_SIZE; i++) {
436 /* skb */
437 mdp->rx_skbuff[i] = NULL;
438 skb = dev_alloc_skb(mdp->rx_buf_sz);
439 mdp->rx_skbuff[i] = skb;
440 if (skb == NULL)
441 break;
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000442 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
443 DMA_FROM_DEVICE);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900444 skb->dev = ndev; /* Mark as being used by this device. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000445 sh_eth_set_receive_align(skb);
446
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700447 /* RX descriptor */
448 rxdesc = &mdp->rx_ring[i];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000449 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Yoshinori Sato71557a32008-08-06 19:49:00 -0400450 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700451
452 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000453 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900454 /* Rx descriptor address set */
455 if (i == 0) {
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000456 ctrl_outl(mdp->rx_desc_dma, ioaddr + RDLAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900457#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000458 ctrl_outl(mdp->rx_desc_dma, ioaddr + RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900459#endif
460 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700461 }
462
463 mdp->dirty_rx = (u32) (i - RX_RING_SIZE);
464
465 /* Mark the last entry as wrapping the ring. */
Yoshinori Sato71557a32008-08-06 19:49:00 -0400466 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700467
468 memset(mdp->tx_ring, 0, tx_ringsize);
469
470 /* build Tx ring buffer */
471 for (i = 0; i < TX_RING_SIZE; i++) {
472 mdp->tx_skbuff[i] = NULL;
473 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400474 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700475 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900476 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -0400477 /* Tx descriptor address set */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000478 ctrl_outl(mdp->tx_desc_dma, ioaddr + TDLAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900479#if defined(CONFIG_CPU_SUBTYPE_SH7763)
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000480 ctrl_outl(mdp->tx_desc_dma, ioaddr + TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900481#endif
482 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700483 }
484
Yoshinori Sato71557a32008-08-06 19:49:00 -0400485 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700486}
487
488/* Get skb and descriptor buffer */
489static int sh_eth_ring_init(struct net_device *ndev)
490{
491 struct sh_eth_private *mdp = netdev_priv(ndev);
492 int rx_ringsize, tx_ringsize, ret = 0;
493
494 /*
495 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
496 * card needs room to do 8 byte alignment, +2 so we can reserve
497 * the first 2 bytes, and +16 gets room for the status word from the
498 * card.
499 */
500 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
501 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
502
503 /* Allocate RX and TX skb rings */
504 mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE,
505 GFP_KERNEL);
506 if (!mdp->rx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000507 dev_err(&ndev->dev, "Cannot allocate Rx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700508 ret = -ENOMEM;
509 return ret;
510 }
511
512 mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE,
513 GFP_KERNEL);
514 if (!mdp->tx_skbuff) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000515 dev_err(&ndev->dev, "Cannot allocate Tx skb\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700516 ret = -ENOMEM;
517 goto skb_ring_free;
518 }
519
520 /* Allocate all Rx descriptors. */
521 rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
522 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
523 GFP_KERNEL);
524
525 if (!mdp->rx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000526 dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n",
527 rx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700528 ret = -ENOMEM;
529 goto desc_ring_free;
530 }
531
532 mdp->dirty_rx = 0;
533
534 /* Allocate all Tx descriptors. */
535 tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
536 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
537 GFP_KERNEL);
538 if (!mdp->tx_ring) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000539 dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n",
540 tx_ringsize);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700541 ret = -ENOMEM;
542 goto desc_ring_free;
543 }
544 return ret;
545
546desc_ring_free:
547 /* free DMA buffer */
548 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
549
550skb_ring_free:
551 /* Free Rx and Tx skb ring buffer */
552 sh_eth_ring_free(ndev);
553
554 return ret;
555}
556
557static int sh_eth_dev_init(struct net_device *ndev)
558{
559 int ret = 0;
560 struct sh_eth_private *mdp = netdev_priv(ndev);
561 u32 ioaddr = ndev->base_addr;
562 u_int32_t rx_int_var, tx_int_var;
563 u32 val;
564
565 /* Soft Reset */
566 sh_eth_reset(ndev);
567
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900568 /* Descriptor format */
569 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000570 if (mdp->cd->rpadir)
571 ctrl_outl(mdp->cd->rpadir_value, ioaddr + RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700572
573 /* all sh_eth int mask */
574 ctrl_outl(0, ioaddr + EESIPR);
575
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000576#if defined(__LITTLE_ENDIAN__)
577 if (mdp->cd->hw_swap)
578 ctrl_outl(EDMR_EL, ioaddr + EDMR);
579 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900580#endif
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000581 ctrl_outl(0, ioaddr + EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700582
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900583 /* FIFO size set */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000584 ctrl_outl(mdp->cd->fdr_value, ioaddr + FDR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700585 ctrl_outl(0, ioaddr + TFTR);
586
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900587 /* Frame recv control */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000588 ctrl_outl(mdp->cd->rmcr_value, ioaddr + RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700589
590 rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5;
591 tx_int_var = mdp->tx_int_var = DESC_I_TINT2;
592 ctrl_outl(rx_int_var | tx_int_var, ioaddr + TRSCER);
593
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000594 if (mdp->cd->bculr)
595 ctrl_outl(0x800, ioaddr + BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900596
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000597 ctrl_outl(mdp->cd->fcftr_value, ioaddr + FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900598
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000599 if (!mdp->cd->no_trimd)
600 ctrl_outl(0, ioaddr + TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700601
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900602 /* Recv frame limit set register */
603 ctrl_outl(RFLR_VALUE, ioaddr + RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700604
605 ctrl_outl(ctrl_inl(ioaddr + EESR), ioaddr + EESR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000606 ctrl_outl(mdp->cd->eesipr_value, ioaddr + EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700607
608 /* PAUSE Prohibition */
609 val = (ctrl_inl(ioaddr + ECMR) & ECMR_DM) |
610 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
611
612 ctrl_outl(val, ioaddr + ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900613
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000614 if (mdp->cd->set_rate)
615 mdp->cd->set_rate(ndev);
616
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900617 /* E-MAC Status Register clear */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000618 ctrl_outl(mdp->cd->ecsr_value, ioaddr + ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900619
620 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000621 ctrl_outl(mdp->cd->ecsipr_value, ioaddr + ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700622
623 /* Set MAC address */
624 update_mac_address(ndev);
625
626 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000627 if (mdp->cd->apr)
628 ctrl_outl(APR_AP, ioaddr + APR);
629 if (mdp->cd->mpr)
630 ctrl_outl(MPR_MP, ioaddr + MPR);
631 if (mdp->cd->tpauser)
632 ctrl_outl(TPAUSER_UNLIMITED, ioaddr + TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900633
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700634 /* Setting the Rx mode will start the Rx process. */
635 ctrl_outl(EDRRR_R, ioaddr + EDRRR);
636
637 netif_start_queue(ndev);
638
639 return ret;
640}
641
642/* free Tx skb function */
643static int sh_eth_txfree(struct net_device *ndev)
644{
645 struct sh_eth_private *mdp = netdev_priv(ndev);
646 struct sh_eth_txdesc *txdesc;
647 int freeNum = 0;
648 int entry = 0;
649
650 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
651 entry = mdp->dirty_tx % TX_RING_SIZE;
652 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400653 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700654 break;
655 /* Free the original skb. */
656 if (mdp->tx_skbuff[entry]) {
657 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
658 mdp->tx_skbuff[entry] = NULL;
659 freeNum++;
660 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400661 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700662 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -0400663 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700664
665 mdp->stats.tx_packets++;
666 mdp->stats.tx_bytes += txdesc->buffer_length;
667 }
668 return freeNum;
669}
670
671/* Packet receive function */
672static int sh_eth_rx(struct net_device *ndev)
673{
674 struct sh_eth_private *mdp = netdev_priv(ndev);
675 struct sh_eth_rxdesc *rxdesc;
676
677 int entry = mdp->cur_rx % RX_RING_SIZE;
678 int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx;
679 struct sk_buff *skb;
680 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000681 u32 desc_status;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700682
683 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -0400684 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
685 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700686 pkt_len = rxdesc->frame_length;
687
688 if (--boguscnt < 0)
689 break;
690
691 if (!(desc_status & RDFEND))
692 mdp->stats.rx_length_errors++;
693
694 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
695 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
696 mdp->stats.rx_errors++;
697 if (desc_status & RD_RFS1)
698 mdp->stats.rx_crc_errors++;
699 if (desc_status & RD_RFS2)
700 mdp->stats.rx_frame_errors++;
701 if (desc_status & RD_RFS3)
702 mdp->stats.rx_length_errors++;
703 if (desc_status & RD_RFS4)
704 mdp->stats.rx_length_errors++;
705 if (desc_status & RD_RFS6)
706 mdp->stats.rx_missed_errors++;
707 if (desc_status & RD_RFS10)
708 mdp->stats.rx_over_errors++;
709 } else {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000710 if (!mdp->cd->hw_swap)
711 sh_eth_soft_swap(
712 phys_to_virt(ALIGN(rxdesc->addr, 4)),
713 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700714 skb = mdp->rx_skbuff[entry];
715 mdp->rx_skbuff[entry] = NULL;
716 skb_put(skb, pkt_len);
717 skb->protocol = eth_type_trans(skb, ndev);
718 netif_rx(skb);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700719 mdp->stats.rx_packets++;
720 mdp->stats.rx_bytes += pkt_len;
721 }
Yoshinori Sato71557a32008-08-06 19:49:00 -0400722 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700723 entry = (++mdp->cur_rx) % RX_RING_SIZE;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +0000724 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700725 }
726
727 /* Refill the Rx ring buffers. */
728 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
729 entry = mdp->dirty_rx % RX_RING_SIZE;
730 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900731 /* The size of the buffer is 16 byte boundary. */
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000732 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900733
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700734 if (mdp->rx_skbuff[entry] == NULL) {
735 skb = dev_alloc_skb(mdp->rx_buf_sz);
736 mdp->rx_skbuff[entry] = skb;
737 if (skb == NULL)
738 break; /* Better luck next round. */
Yoshihiro Shimodae88aae72009-05-24 23:52:35 +0000739 dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz,
740 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700741 skb->dev = ndev;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000742 sh_eth_set_receive_align(skb);
743
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900744 skb->ip_summed = CHECKSUM_NONE;
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +0000745 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700746 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700747 if (entry >= RX_RING_SIZE - 1)
748 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400749 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700750 else
751 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -0400752 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700753 }
754
755 /* Restart Rx engine if stopped. */
756 /* If we don't need to check status, don't. -KDU */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900757 if (!(ctrl_inl(ndev->base_addr + EDRRR) & EDRRR_R))
758 ctrl_outl(EDRRR_R, ndev->base_addr + EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700759
760 return 0;
761}
762
763/* error control function */
764static void sh_eth_error(struct net_device *ndev, int intr_status)
765{
766 struct sh_eth_private *mdp = netdev_priv(ndev);
767 u32 ioaddr = ndev->base_addr;
768 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000769 u32 link_stat;
770 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700771
772 if (intr_status & EESR_ECI) {
773 felic_stat = ctrl_inl(ioaddr + ECSR);
774 ctrl_outl(felic_stat, ioaddr + ECSR); /* clear int */
775 if (felic_stat & ECSR_ICD)
776 mdp->stats.tx_carrier_errors++;
777 if (felic_stat & ECSR_LCHNG) {
778 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000779 if (mdp->cd->no_psr || mdp->no_ether_link) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000780 if (mdp->link == PHY_DOWN)
781 link_stat = 0;
782 else
783 link_stat = PHY_ST_LINK;
784 } else {
785 link_stat = (ctrl_inl(ioaddr + PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +0000786 if (mdp->ether_link_active_low)
787 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000788 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700789 if (!(link_stat & PHY_ST_LINK)) {
790 /* Link Down : disable tx and rx */
791 ctrl_outl(ctrl_inl(ioaddr + ECMR) &
792 ~(ECMR_RE | ECMR_TE), ioaddr + ECMR);
793 } else {
794 /* Link Up */
795 ctrl_outl(ctrl_inl(ioaddr + EESIPR) &
796 ~DMAC_M_ECI, ioaddr + EESIPR);
797 /*clear int */
798 ctrl_outl(ctrl_inl(ioaddr + ECSR),
799 ioaddr + ECSR);
800 ctrl_outl(ctrl_inl(ioaddr + EESIPR) |
801 DMAC_M_ECI, ioaddr + EESIPR);
802 /* enable tx and rx */
803 ctrl_outl(ctrl_inl(ioaddr + ECMR) |
804 (ECMR_RE | ECMR_TE), ioaddr + ECMR);
805 }
806 }
807 }
808
809 if (intr_status & EESR_TWB) {
810 /* Write buck end. unused write back interrupt */
811 if (intr_status & EESR_TABT) /* Transmit Abort int */
812 mdp->stats.tx_aborted_errors++;
813 }
814
815 if (intr_status & EESR_RABT) {
816 /* Receive Abort int */
817 if (intr_status & EESR_RFRMER) {
818 /* Receive Frame Overflow int */
819 mdp->stats.rx_frame_errors++;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000820 dev_err(&ndev->dev, "Receive Frame Overflow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700821 }
822 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000823
824 if (!mdp->cd->no_ade) {
825 if (intr_status & EESR_ADE && intr_status & EESR_TDE &&
826 intr_status & EESR_TFE)
827 mdp->stats.tx_fifo_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700828 }
829
830 if (intr_status & EESR_RDE) {
831 /* Receive Descriptor Empty int */
832 mdp->stats.rx_over_errors++;
833
834 if (ctrl_inl(ioaddr + EDRRR) ^ EDRRR_R)
835 ctrl_outl(EDRRR_R, ioaddr + EDRRR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000836 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700837 }
838 if (intr_status & EESR_RFE) {
839 /* Receive FIFO Overflow int */
840 mdp->stats.rx_fifo_errors++;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000841 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700842 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000843
844 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
845 if (mdp->cd->no_ade)
846 mask &= ~EESR_ADE;
847 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700848 /* Tx error */
849 u32 edtrr = ctrl_inl(ndev->base_addr + EDTRR);
850 /* dmesg */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000851 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
852 intr_status, mdp->cur_tx);
853 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700854 mdp->dirty_tx, (u32) ndev->state, edtrr);
855 /* dirty buffer free */
856 sh_eth_txfree(ndev);
857
858 /* SH7712 BUG */
859 if (edtrr ^ EDTRR_TRNS) {
860 /* tx dma start */
861 ctrl_outl(EDTRR_TRNS, ndev->base_addr + EDTRR);
862 }
863 /* wakeup */
864 netif_wake_queue(ndev);
865 }
866}
867
868static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
869{
870 struct net_device *ndev = netdev;
871 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000872 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000873 irqreturn_t ret = IRQ_NONE;
roel kluin37c8ae32009-06-22 07:38:00 +0000874 u32 ioaddr, intr_status = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700875
876 ioaddr = ndev->base_addr;
877 spin_lock(&mdp->lock);
878
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900879 /* Get interrpt stat */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700880 intr_status = ctrl_inl(ioaddr + EESR);
881 /* Clear interrupt */
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000882 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
883 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000884 cd->tx_check | cd->eesr_err_check)) {
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000885 ctrl_outl(intr_status, ioaddr + EESR);
886 ret = IRQ_HANDLED;
887 } else
888 goto other_irq;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700889
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900890 if (intr_status & (EESR_FRC | /* Frame recv*/
891 EESR_RMAF | /* Multi cast address recv*/
892 EESR_RRF | /* Bit frame recv */
893 EESR_RTLF | /* Long frame recv*/
894 EESR_RTSF | /* short frame recv */
895 EESR_PRE | /* PHY-LSI recv error */
896 EESR_CERF)){ /* recv frame CRC error */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700897 sh_eth_rx(ndev);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900898 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700899
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +0900900 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000901 if (intr_status & cd->tx_check) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700902 sh_eth_txfree(ndev);
903 netif_wake_queue(ndev);
904 }
905
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000906 if (intr_status & cd->eesr_err_check)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700907 sh_eth_error(ndev, intr_status);
908
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000909other_irq:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700910 spin_unlock(&mdp->lock);
911
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +0000912 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700913}
914
915static void sh_eth_timer(unsigned long data)
916{
917 struct net_device *ndev = (struct net_device *)data;
918 struct sh_eth_private *mdp = netdev_priv(ndev);
919
920 mod_timer(&mdp->timer, jiffies + (10 * HZ));
921}
922
923/* PHY state control function */
924static void sh_eth_adjust_link(struct net_device *ndev)
925{
926 struct sh_eth_private *mdp = netdev_priv(ndev);
927 struct phy_device *phydev = mdp->phydev;
928 u32 ioaddr = ndev->base_addr;
929 int new_state = 0;
930
931 if (phydev->link != PHY_DOWN) {
932 if (phydev->duplex != mdp->duplex) {
933 new_state = 1;
934 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000935 if (mdp->cd->set_duplex)
936 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700937 }
938
939 if (phydev->speed != mdp->speed) {
940 new_state = 1;
941 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000942 if (mdp->cd->set_rate)
943 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700944 }
945 if (mdp->link == PHY_DOWN) {
946 ctrl_outl((ctrl_inl(ioaddr + ECMR) & ~ECMR_TXF)
947 | ECMR_DM, ioaddr + ECMR);
948 new_state = 1;
949 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700950 }
951 } else if (mdp->link) {
952 new_state = 1;
953 mdp->link = PHY_DOWN;
954 mdp->speed = 0;
955 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700956 }
957
958 if (new_state)
959 phy_print_status(phydev);
960}
961
962/* PHY init function */
963static int sh_eth_phy_init(struct net_device *ndev)
964{
965 struct sh_eth_private *mdp = netdev_priv(ndev);
David S. Miller0a372eb2009-05-26 21:11:09 -0700966 char phy_id[MII_BUS_ID_SIZE + 3];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700967 struct phy_device *phydev = NULL;
968
Kay Sieversfb28ad32008-11-10 13:55:14 -0800969 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700970 mdp->mii_bus->id , mdp->phy_id);
971
972 mdp->link = PHY_DOWN;
973 mdp->speed = 0;
974 mdp->duplex = -1;
975
976 /* Try connect to PHY */
977 phydev = phy_connect(ndev, phy_id, &sh_eth_adjust_link,
978 0, PHY_INTERFACE_MODE_MII);
979 if (IS_ERR(phydev)) {
980 dev_err(&ndev->dev, "phy_connect failed\n");
981 return PTR_ERR(phydev);
982 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000983
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700984 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000985 phydev->addr, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700986
987 mdp->phydev = phydev;
988
989 return 0;
990}
991
992/* PHY control start function */
993static int sh_eth_phy_start(struct net_device *ndev)
994{
995 struct sh_eth_private *mdp = netdev_priv(ndev);
996 int ret;
997
998 ret = sh_eth_phy_init(ndev);
999 if (ret)
1000 return ret;
1001
1002 /* reset phy - this also wakes it from PDOWN */
1003 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1004 phy_start(mdp->phydev);
1005
1006 return 0;
1007}
1008
1009/* network device open function */
1010static int sh_eth_open(struct net_device *ndev)
1011{
1012 int ret = 0;
1013 struct sh_eth_private *mdp = netdev_priv(ndev);
1014
Magnus Dammbcd51492009-10-09 00:20:04 +00001015 pm_runtime_get_sync(&mdp->pdev->dev);
1016
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001017 ret = request_irq(ndev->irq, &sh_eth_interrupt,
1018#if defined(CONFIG_CPU_SUBTYPE_SH7763) || defined(CONFIG_CPU_SUBTYPE_SH7764)
1019 IRQF_SHARED,
1020#else
1021 0,
1022#endif
1023 ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001024 if (ret) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001025 dev_err(&ndev->dev, "Can not assign IRQ number\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001026 return ret;
1027 }
1028
1029 /* Descriptor set */
1030 ret = sh_eth_ring_init(ndev);
1031 if (ret)
1032 goto out_free_irq;
1033
1034 /* device init */
1035 ret = sh_eth_dev_init(ndev);
1036 if (ret)
1037 goto out_free_irq;
1038
1039 /* PHY control start*/
1040 ret = sh_eth_phy_start(ndev);
1041 if (ret)
1042 goto out_free_irq;
1043
1044 /* Set the timer to check for link beat. */
1045 init_timer(&mdp->timer);
1046 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001047 setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001048
1049 return ret;
1050
1051out_free_irq:
1052 free_irq(ndev->irq, ndev);
Magnus Dammbcd51492009-10-09 00:20:04 +00001053 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001054 return ret;
1055}
1056
1057/* Timeout function */
1058static void sh_eth_tx_timeout(struct net_device *ndev)
1059{
1060 struct sh_eth_private *mdp = netdev_priv(ndev);
1061 u32 ioaddr = ndev->base_addr;
1062 struct sh_eth_rxdesc *rxdesc;
1063 int i;
1064
1065 netif_stop_queue(ndev);
1066
1067 /* worning message out. */
1068 printk(KERN_WARNING "%s: transmit timed out, status %8.8x,"
1069 " resetting...\n", ndev->name, (int)ctrl_inl(ioaddr + EESR));
1070
1071 /* tx_errors count up */
1072 mdp->stats.tx_errors++;
1073
1074 /* timer off */
1075 del_timer_sync(&mdp->timer);
1076
1077 /* Free all the skbuffs in the Rx queue. */
1078 for (i = 0; i < RX_RING_SIZE; i++) {
1079 rxdesc = &mdp->rx_ring[i];
1080 rxdesc->status = 0;
1081 rxdesc->addr = 0xBADF00D0;
1082 if (mdp->rx_skbuff[i])
1083 dev_kfree_skb(mdp->rx_skbuff[i]);
1084 mdp->rx_skbuff[i] = NULL;
1085 }
1086 for (i = 0; i < TX_RING_SIZE; i++) {
1087 if (mdp->tx_skbuff[i])
1088 dev_kfree_skb(mdp->tx_skbuff[i]);
1089 mdp->tx_skbuff[i] = NULL;
1090 }
1091
1092 /* device init */
1093 sh_eth_dev_init(ndev);
1094
1095 /* timer on */
1096 mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */
1097 add_timer(&mdp->timer);
1098}
1099
1100/* Packet transmit function */
1101static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1102{
1103 struct sh_eth_private *mdp = netdev_priv(ndev);
1104 struct sh_eth_txdesc *txdesc;
1105 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001106 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001107
1108 spin_lock_irqsave(&mdp->lock, flags);
1109 if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) {
1110 if (!sh_eth_txfree(ndev)) {
1111 netif_stop_queue(ndev);
1112 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00001113 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001114 }
1115 }
1116 spin_unlock_irqrestore(&mdp->lock, flags);
1117
1118 entry = mdp->cur_tx % TX_RING_SIZE;
1119 mdp->tx_skbuff[entry] = skb;
1120 txdesc = &mdp->tx_ring[entry];
Yoshihiro Shimoda0029d642009-05-24 23:53:20 +00001121 txdesc->addr = virt_to_phys(skb->data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001122 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001123 if (!mdp->cd->hw_swap)
1124 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1125 skb->len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126 /* write back */
1127 __flush_purge_region(skb->data, skb->len);
1128 if (skb->len < ETHERSMALL)
1129 txdesc->buffer_length = ETHERSMALL;
1130 else
1131 txdesc->buffer_length = skb->len;
1132
1133 if (entry >= TX_RING_SIZE - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001134 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001135 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04001136 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001137
1138 mdp->cur_tx++;
1139
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001140 if (!(ctrl_inl(ndev->base_addr + EDTRR) & EDTRR_TRNS))
1141 ctrl_outl(EDTRR_TRNS, ndev->base_addr + EDTRR);
1142
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001143 ndev->trans_start = jiffies;
1144
Patrick McHardy6ed10652009-06-23 06:03:08 +00001145 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001146}
1147
1148/* device close function */
1149static int sh_eth_close(struct net_device *ndev)
1150{
1151 struct sh_eth_private *mdp = netdev_priv(ndev);
1152 u32 ioaddr = ndev->base_addr;
1153 int ringsize;
1154
1155 netif_stop_queue(ndev);
1156
1157 /* Disable interrupts by clearing the interrupt mask. */
1158 ctrl_outl(0x0000, ioaddr + EESIPR);
1159
1160 /* Stop the chip's Tx and Rx processes. */
1161 ctrl_outl(0, ioaddr + EDTRR);
1162 ctrl_outl(0, ioaddr + EDRRR);
1163
1164 /* PHY Disconnect */
1165 if (mdp->phydev) {
1166 phy_stop(mdp->phydev);
1167 phy_disconnect(mdp->phydev);
1168 }
1169
1170 free_irq(ndev->irq, ndev);
1171
1172 del_timer_sync(&mdp->timer);
1173
1174 /* Free all the skbuffs in the Rx queue. */
1175 sh_eth_ring_free(ndev);
1176
1177 /* free DMA buffer */
1178 ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE;
1179 dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1180
1181 /* free DMA buffer */
1182 ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE;
1183 dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma);
1184
Magnus Dammbcd51492009-10-09 00:20:04 +00001185 pm_runtime_put_sync(&mdp->pdev->dev);
1186
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001187 return 0;
1188}
1189
1190static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1191{
1192 struct sh_eth_private *mdp = netdev_priv(ndev);
1193 u32 ioaddr = ndev->base_addr;
1194
Magnus Dammbcd51492009-10-09 00:20:04 +00001195 pm_runtime_get_sync(&mdp->pdev->dev);
1196
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001197 mdp->stats.tx_dropped += ctrl_inl(ioaddr + TROCR);
1198 ctrl_outl(0, ioaddr + TROCR); /* (write clear) */
1199 mdp->stats.collisions += ctrl_inl(ioaddr + CDCR);
1200 ctrl_outl(0, ioaddr + CDCR); /* (write clear) */
1201 mdp->stats.tx_carrier_errors += ctrl_inl(ioaddr + LCCR);
1202 ctrl_outl(0, ioaddr + LCCR); /* (write clear) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001203#if defined(CONFIG_CPU_SUBTYPE_SH7763)
1204 mdp->stats.tx_carrier_errors += ctrl_inl(ioaddr + CERCR);/* CERCR */
1205 ctrl_outl(0, ioaddr + CERCR); /* (write clear) */
1206 mdp->stats.tx_carrier_errors += ctrl_inl(ioaddr + CEECR);/* CEECR */
1207 ctrl_outl(0, ioaddr + CEECR); /* (write clear) */
1208#else
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001209 mdp->stats.tx_carrier_errors += ctrl_inl(ioaddr + CNDCR);
1210 ctrl_outl(0, ioaddr + CNDCR); /* (write clear) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001211#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00001212 pm_runtime_put_sync(&mdp->pdev->dev);
1213
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214 return &mdp->stats;
1215}
1216
1217/* ioctl to device funciotn*/
1218static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1219 int cmd)
1220{
1221 struct sh_eth_private *mdp = netdev_priv(ndev);
1222 struct phy_device *phydev = mdp->phydev;
1223
1224 if (!netif_running(ndev))
1225 return -EINVAL;
1226
1227 if (!phydev)
1228 return -ENODEV;
1229
1230 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
1231}
1232
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001233#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001234/* Multicast reception directions set */
1235static void sh_eth_set_multicast_list(struct net_device *ndev)
1236{
1237 u32 ioaddr = ndev->base_addr;
1238
1239 if (ndev->flags & IFF_PROMISC) {
1240 /* Set promiscuous. */
1241 ctrl_outl((ctrl_inl(ioaddr + ECMR) & ~ECMR_MCT) | ECMR_PRM,
1242 ioaddr + ECMR);
1243 } else {
1244 /* Normal, unicast/broadcast-only mode. */
1245 ctrl_outl((ctrl_inl(ioaddr + ECMR) & ~ECMR_PRM) | ECMR_MCT,
1246 ioaddr + ECMR);
1247 }
1248}
1249
1250/* SuperH's TSU register init function */
1251static void sh_eth_tsu_init(u32 ioaddr)
1252{
1253 ctrl_outl(0, ioaddr + TSU_FWEN0); /* Disable forward(0->1) */
1254 ctrl_outl(0, ioaddr + TSU_FWEN1); /* Disable forward(1->0) */
1255 ctrl_outl(0, ioaddr + TSU_FCM); /* forward fifo 3k-3k */
1256 ctrl_outl(0xc, ioaddr + TSU_BSYSL0);
1257 ctrl_outl(0xc, ioaddr + TSU_BSYSL1);
1258 ctrl_outl(0, ioaddr + TSU_PRISL0);
1259 ctrl_outl(0, ioaddr + TSU_PRISL1);
1260 ctrl_outl(0, ioaddr + TSU_FWSL0);
1261 ctrl_outl(0, ioaddr + TSU_FWSL1);
1262 ctrl_outl(TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, ioaddr + TSU_FWSLC);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001263#if defined(CONFIG_CPU_SUBTYPE_SH7763)
1264 ctrl_outl(0, ioaddr + TSU_QTAG0); /* Disable QTAG(0->1) */
1265 ctrl_outl(0, ioaddr + TSU_QTAG1); /* Disable QTAG(1->0) */
1266#else
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267 ctrl_outl(0, ioaddr + TSU_QTAGM0); /* Disable QTAG(0->1) */
1268 ctrl_outl(0, ioaddr + TSU_QTAGM1); /* Disable QTAG(1->0) */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001269#endif
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001270 ctrl_outl(0, ioaddr + TSU_FWSR); /* all interrupt status clear */
1271 ctrl_outl(0, ioaddr + TSU_FWINMK); /* Disable all interrupt */
1272 ctrl_outl(0, ioaddr + TSU_TEN); /* Disable all CAM entry */
1273 ctrl_outl(0, ioaddr + TSU_POST1); /* Disable CAM entry [ 0- 7] */
1274 ctrl_outl(0, ioaddr + TSU_POST2); /* Disable CAM entry [ 8-15] */
1275 ctrl_outl(0, ioaddr + TSU_POST3); /* Disable CAM entry [16-23] */
1276 ctrl_outl(0, ioaddr + TSU_POST4); /* Disable CAM entry [24-31] */
1277}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001278#endif /* SH_ETH_HAS_TSU */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001279
1280/* MDIO bus release function */
1281static int sh_mdio_release(struct net_device *ndev)
1282{
1283 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
1284
1285 /* unregister mdio bus */
1286 mdiobus_unregister(bus);
1287
1288 /* remove mdio bus info from net_device */
1289 dev_set_drvdata(&ndev->dev, NULL);
1290
1291 /* free bitbang info */
1292 free_mdio_bitbang(bus);
1293
1294 return 0;
1295}
1296
1297/* MDIO bus init function */
1298static int sh_mdio_init(struct net_device *ndev, int id)
1299{
1300 int ret, i;
1301 struct bb_info *bitbang;
1302 struct sh_eth_private *mdp = netdev_priv(ndev);
1303
1304 /* create bit control struct for PHY */
1305 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
1306 if (!bitbang) {
1307 ret = -ENOMEM;
1308 goto out;
1309 }
1310
1311 /* bitbang init */
1312 bitbang->addr = ndev->base_addr + PIR;
1313 bitbang->mdi_msk = 0x08;
1314 bitbang->mdo_msk = 0x04;
1315 bitbang->mmd_msk = 0x02;/* MMD */
1316 bitbang->mdc_msk = 0x01;
1317 bitbang->ctrl.ops = &bb_ops;
1318
1319 /* MII contorller setting */
1320 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
1321 if (!mdp->mii_bus) {
1322 ret = -ENOMEM;
1323 goto out_free_bitbang;
1324 }
1325
1326 /* Hook up MII support for ethtool */
1327 mdp->mii_bus->name = "sh_mii";
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +00001328 mdp->mii_bus->parent = &ndev->dev;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00001329 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001330
1331 /* PHY IRQ */
1332 mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1333 if (!mdp->mii_bus->irq) {
1334 ret = -ENOMEM;
1335 goto out_free_bus;
1336 }
1337
1338 for (i = 0; i < PHY_MAX_ADDR; i++)
1339 mdp->mii_bus->irq[i] = PHY_POLL;
1340
1341 /* regist mdio bus */
1342 ret = mdiobus_register(mdp->mii_bus);
1343 if (ret)
1344 goto out_free_irq;
1345
1346 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
1347
1348 return 0;
1349
1350out_free_irq:
1351 kfree(mdp->mii_bus->irq);
1352
1353out_free_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001354 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001355
1356out_free_bitbang:
1357 kfree(bitbang);
1358
1359out:
1360 return ret;
1361}
1362
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001363static const struct net_device_ops sh_eth_netdev_ops = {
1364 .ndo_open = sh_eth_open,
1365 .ndo_stop = sh_eth_close,
1366 .ndo_start_xmit = sh_eth_start_xmit,
1367 .ndo_get_stats = sh_eth_get_stats,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001368#if defined(SH_ETH_HAS_TSU)
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001369 .ndo_set_multicast_list = sh_eth_set_multicast_list,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001370#endif
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001371 .ndo_tx_timeout = sh_eth_tx_timeout,
1372 .ndo_do_ioctl = sh_eth_do_ioctl,
1373 .ndo_validate_addr = eth_validate_addr,
1374 .ndo_set_mac_address = eth_mac_addr,
1375 .ndo_change_mtu = eth_change_mtu,
1376};
1377
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001378static int sh_eth_drv_probe(struct platform_device *pdev)
1379{
1380 int ret, i, devno = 0;
1381 struct resource *res;
1382 struct net_device *ndev = NULL;
1383 struct sh_eth_private *mdp;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001384 struct sh_eth_plat_data *pd;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001385
1386 /* get base addr */
1387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1388 if (unlikely(res == NULL)) {
1389 dev_err(&pdev->dev, "invalid resource\n");
1390 ret = -EINVAL;
1391 goto out;
1392 }
1393
1394 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
1395 if (!ndev) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001396 dev_err(&pdev->dev, "Could not allocate device.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001397 ret = -ENOMEM;
1398 goto out;
1399 }
1400
1401 /* The sh Ether-specific entries in the device structure. */
1402 ndev->base_addr = res->start;
1403 devno = pdev->id;
1404 if (devno < 0)
1405 devno = 0;
1406
1407 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02001408 ret = platform_get_irq(pdev, 0);
1409 if (ret < 0) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001410 ret = -ENODEV;
1411 goto out_release;
1412 }
roel kluincc3c0802008-09-10 19:22:44 +02001413 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001414
1415 SET_NETDEV_DEV(ndev, &pdev->dev);
1416
1417 /* Fill in the fields of the device structure with ethernet values. */
1418 ether_setup(ndev);
1419
1420 mdp = netdev_priv(ndev);
1421 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00001422 mdp->pdev = pdev;
1423 pm_runtime_enable(&pdev->dev);
1424 pm_runtime_resume(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001425
Yoshinori Sato71557a32008-08-06 19:49:00 -04001426 pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001427 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04001428 mdp->phy_id = pd->phy;
1429 /* EDMAC endian */
1430 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001431 mdp->no_ether_link = pd->no_ether_link;
1432 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001434 /* set cpu data */
1435 mdp->cd = &sh_eth_my_cpu_data;
1436 sh_eth_set_default_cpu_data(mdp->cd);
1437
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001438 /* set function */
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00001439 ndev->netdev_ops = &sh_eth_netdev_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001440 ndev->watchdog_timeo = TX_TIMEOUT;
1441
1442 mdp->post_rx = POST_RX >> (devno << 1);
1443 mdp->post_fw = POST_FW >> (devno << 1);
1444
1445 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00001446 read_mac_address(ndev, pd->mac_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447
1448 /* First device only init */
1449 if (!devno) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001450 if (mdp->cd->chip_reset)
1451 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001452
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001453#if defined(SH_ETH_HAS_TSU)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001454 /* TSU init (Init only)*/
1455 sh_eth_tsu_init(SH_TSU_ADDR);
Yoshinori Sato71557a32008-08-06 19:49:00 -04001456#endif
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001457 }
1458
1459 /* network device register */
1460 ret = register_netdev(ndev);
1461 if (ret)
1462 goto out_release;
1463
1464 /* mdio bus init */
1465 ret = sh_mdio_init(ndev, pdev->id);
1466 if (ret)
1467 goto out_unregister;
1468
1469 /* pritnt device infomation */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001470 pr_info("Base address at 0x%x, ",
1471 (u32)ndev->base_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001472
1473 for (i = 0; i < 5; i++)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001474 printk("%02X:", ndev->dev_addr[i]);
1475 printk("%02X, IRQ %d.\n", ndev->dev_addr[i], ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001476
1477 platform_set_drvdata(pdev, ndev);
1478
1479 return ret;
1480
1481out_unregister:
1482 unregister_netdev(ndev);
1483
1484out_release:
1485 /* net_dev free */
1486 if (ndev)
1487 free_netdev(ndev);
1488
1489out:
1490 return ret;
1491}
1492
1493static int sh_eth_drv_remove(struct platform_device *pdev)
1494{
1495 struct net_device *ndev = platform_get_drvdata(pdev);
1496
1497 sh_mdio_release(ndev);
1498 unregister_netdev(ndev);
1499 flush_scheduled_work();
Magnus Dammbcd51492009-10-09 00:20:04 +00001500 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001501 free_netdev(ndev);
1502 platform_set_drvdata(pdev, NULL);
1503
1504 return 0;
1505}
1506
Magnus Dammbcd51492009-10-09 00:20:04 +00001507static int sh_eth_runtime_nop(struct device *dev)
1508{
1509 /*
1510 * Runtime PM callback shared between ->runtime_suspend()
1511 * and ->runtime_resume(). Simply returns success.
1512 *
1513 * This driver re-initializes all registers after
1514 * pm_runtime_get_sync() anyway so there is no need
1515 * to save and restore registers here.
1516 */
1517 return 0;
1518}
1519
1520static struct dev_pm_ops sh_eth_dev_pm_ops = {
1521 .runtime_suspend = sh_eth_runtime_nop,
1522 .runtime_resume = sh_eth_runtime_nop,
1523};
1524
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001525static struct platform_driver sh_eth_driver = {
1526 .probe = sh_eth_drv_probe,
1527 .remove = sh_eth_drv_remove,
1528 .driver = {
1529 .name = CARDNAME,
Magnus Dammbcd51492009-10-09 00:20:04 +00001530 .pm = &sh_eth_dev_pm_ops,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531 },
1532};
1533
1534static int __init sh_eth_init(void)
1535{
1536 return platform_driver_register(&sh_eth_driver);
1537}
1538
1539static void __exit sh_eth_cleanup(void)
1540{
1541 platform_driver_unregister(&sh_eth_driver);
1542}
1543
1544module_init(sh_eth_init);
1545module_exit(sh_eth_cleanup);
1546
1547MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1548MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1549MODULE_LICENSE("GPL v2");