2 * drivers/net/ifxmips_mii0.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) 2005 Infineon
20 * Rewrite of Infineon IFXMips code, thanks to infineon for the support,
21 * software and hardware
23 * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/errno.h>
30 #include <linux/types.h>
31 #include <linux/interrupt.h>
32 #include <asm/uaccess.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
37 #include <linux/tcp.h>
38 #include <linux/skbuff.h>
40 #include <linux/platform_device.h>
41 #include <linux/ethtool.h>
42 #include <asm/checksum.h>
43 #include <linux/init.h>
44 #include <asm/delay.h>
45 #include <asm/ifxmips/ifxmips.h>
46 #include <asm/ifxmips/ifxmips_mii0.h>
47 #include <asm/ifxmips/ifxmips_dma.h>
48 #include <asm/ifxmips/ifxmips_pmu.h>
50 #define DRVNAME "ifxmips_mii0"
52 static struct net_device
*ifxmips_mii0_dev
;
53 static unsigned char u_boot_ethaddr
[MAX_ADDR_LEN
];
56 ifxmips_write_mdio (u32 phy_addr
, u32 phy_reg
, u16 phy_data
)
58 u32 val
= MDIO_ACC_REQUEST
|
59 ((phy_addr
& MDIO_ACC_ADDR_MASK
) << MDIO_ACC_ADDR_OFFSET
) |
60 ((phy_reg
& MDIO_ACC_REG_MASK
) << MDIO_ACC_REG_OFFSET
) |
63 while (ifxmips_r32(IFXMIPS_PPE32_MDIO_ACC
) & MDIO_ACC_REQUEST
);
64 ifxmips_w32(val
, IFXMIPS_PPE32_MDIO_ACC
);
68 ifxmips_read_mdio (u32 phy_addr
, u32 phy_reg
)
70 u32 val
= MDIO_ACC_REQUEST
| MDIO_ACC_READ
|
71 ((phy_addr
& MDIO_ACC_ADDR_MASK
) << MDIO_ACC_ADDR_OFFSET
) |
72 ((phy_reg
& MDIO_ACC_REG_MASK
) << MDIO_ACC_REG_OFFSET
);
74 ifxmips_w32(val
, IFXMIPS_PPE32_MDIO_ACC
);
75 while (ifxmips_r32(IFXMIPS_PPE32_MDIO_ACC
) & MDIO_ACC_REQUEST
){};
76 val
= ifxmips_r32(IFXMIPS_PPE32_MDIO_ACC
) & MDIO_ACC_VAL_MASK
;
82 ifxmips_switch_open (struct net_device
*dev
)
84 struct switch_priv
* priv
= (struct switch_priv
*)dev
->priv
;
85 struct dma_device_info
* dma_dev
= priv
->dma_device
;
88 for (i
= 0; i
< dma_dev
->max_rx_chan_num
; i
++)
90 if ((dma_dev
->rx_chan
[i
])->control
== IFXMIPS_DMA_CH_ON
)
91 (dma_dev
->rx_chan
[i
])->open(dma_dev
->rx_chan
[i
]);
94 netif_start_queue(dev
);
100 switch_release (struct net_device
*dev
){
101 struct switch_priv
* priv
= (struct switch_priv
*)dev
->priv
;
102 struct dma_device_info
* dma_dev
= priv
->dma_device
;
105 for (i
= 0; i
< dma_dev
->max_rx_chan_num
; i
++)
106 dma_dev
->rx_chan
[i
]->close(dma_dev
->rx_chan
[i
]);
108 netif_stop_queue(dev
);
114 switch_hw_receive (struct net_device
* dev
,struct dma_device_info
* dma_dev
)
116 struct switch_priv
*priv
= (struct switch_priv
*)dev
->priv
;
117 unsigned char* buf
= NULL
;
118 struct sk_buff
*skb
= NULL
;
121 len
= dma_device_read(dma_dev
, &buf
, (void**)&skb
);
123 if (len
>= ETHERNET_PACKET_DMA_BUFFER_SIZE
)
125 printk(KERN_INFO DRVNAME
": packet too large %d\n",len
);
126 goto switch_hw_receive_err_exit
;
133 printk(KERN_INFO DRVNAME
": cannot restore pointer\n");
134 goto switch_hw_receive_err_exit
;
137 if (len
> (skb
->end
- skb
->tail
))
139 printk(KERN_INFO DRVNAME
": BUG, len:%d end:%p tail:%p\n", (len
+4), skb
->end
, skb
->tail
);
140 goto switch_hw_receive_err_exit
;
145 skb
->protocol
= eth_type_trans(skb
, dev
);
148 priv
->stats
.rx_packets
++;
149 priv
->stats
.rx_bytes
+= len
;
153 switch_hw_receive_err_exit
:
157 dev_kfree_skb_any(skb
);
158 priv
->stats
.rx_errors
++;
159 priv
->stats
.rx_dropped
++;
168 switch_hw_tx (char *buf
, int len
, struct net_device
*dev
)
171 struct switch_priv
*priv
= dev
->priv
;
172 struct dma_device_info
* dma_dev
= priv
->dma_device
;
174 ret
= dma_device_write(dma_dev
, buf
, len
, priv
->skb
);
180 switch_tx (struct sk_buff
*skb
, struct net_device
*dev
)
184 struct switch_priv
*priv
= dev
->priv
;
185 struct dma_device_info
* dma_dev
= priv
->dma_device
;
187 len
= skb
->len
< ETH_ZLEN
? ETH_ZLEN
: skb
->len
;
190 dev
->trans_start
= jiffies
;
191 // TODO we got more than 1 dma channel, so we should do something intelligent
192 // here to select one
193 dma_dev
->current_tx_chan
= 0;
197 if (switch_hw_tx(data
, len
, dev
) != len
)
199 dev_kfree_skb_any(skb
);
200 priv
->stats
.tx_errors
++;
201 priv
->stats
.tx_dropped
++;
203 priv
->stats
.tx_packets
++;
204 priv
->stats
.tx_bytes
+=len
;
211 switch_tx_timeout (struct net_device
*dev
)
214 struct switch_priv
* priv
= (struct switch_priv
*)dev
->priv
;
216 priv
->stats
.tx_errors
++;
218 for (i
= 0; i
< priv
->dma_device
->max_tx_chan_num
; i
++)
220 priv
->dma_device
->tx_chan
[i
]->disable_irq(priv
->dma_device
->tx_chan
[i
]);
223 netif_wake_queue(dev
);
229 dma_intr_handler (struct dma_device_info
* dma_dev
, int status
)
236 switch_hw_receive(ifxmips_mii0_dev
, dma_dev
);
239 case TX_BUF_FULL_INT
:
240 printk(KERN_INFO DRVNAME
": tx buffer full\n");
241 netif_stop_queue(ifxmips_mii0_dev
);
242 for (i
= 0; i
< dma_dev
->max_tx_chan_num
; i
++)
244 if ((dma_dev
->tx_chan
[i
])->control
==IFXMIPS_DMA_CH_ON
)
245 dma_dev
->tx_chan
[i
]->enable_irq(dma_dev
->tx_chan
[i
]);
249 case TRANSMIT_CPT_INT
:
250 for (i
= 0; i
< dma_dev
->max_tx_chan_num
; i
++)
251 dma_dev
->tx_chan
[i
]->disable_irq(dma_dev
->tx_chan
[i
]);
253 netif_wake_queue(ifxmips_mii0_dev
);
261 ifxmips_etop_dma_buffer_alloc (int len
, int *byte_offset
, void **opt
)
263 unsigned char *buffer
= NULL
;
264 struct sk_buff
*skb
= NULL
;
266 skb
= dev_alloc_skb(ETHERNET_PACKET_DMA_BUFFER_SIZE
);
270 buffer
= (unsigned char*)(skb
->data
);
272 *(int*)opt
= (int)skb
;
279 ifxmips_etop_dma_buffer_free (unsigned char *dataptr
, void *opt
)
281 struct sk_buff
*skb
= NULL
;
287 skb
= (struct sk_buff
*)opt
;
288 dev_kfree_skb_any(skb
);
292 static struct net_device_stats
*
293 ifxmips_get_stats (struct net_device
*dev
)
295 return (struct net_device_stats
*)dev
->priv
;
299 switch_init (struct net_device
*dev
)
303 struct switch_priv
*priv
;
307 printk(KERN_INFO DRVNAME
": %s is up\n", dev
->name
);
309 dev
->open
= ifxmips_switch_open
;
310 dev
->stop
= switch_release
;
311 dev
->hard_start_xmit
= switch_tx
;
312 dev
->get_stats
= ifxmips_get_stats
;
313 dev
->tx_timeout
= switch_tx_timeout
;
314 dev
->watchdog_timeo
= 10 * HZ
;
316 memset(dev
->priv
, 0, sizeof(struct switch_priv
));
319 priv
->dma_device
= dma_device_reserve("PPE");
321 if (!priv
->dma_device
){
326 priv
->dma_device
->buffer_alloc
= &ifxmips_etop_dma_buffer_alloc
;
327 priv
->dma_device
->buffer_free
= &ifxmips_etop_dma_buffer_free
;
328 priv
->dma_device
->intr_handler
= &dma_intr_handler
;
329 priv
->dma_device
->max_rx_chan_num
= 4;
331 for (i
= 0; i
< priv
->dma_device
->max_rx_chan_num
; i
++)
333 priv
->dma_device
->rx_chan
[i
]->packet_size
= ETHERNET_PACKET_DMA_BUFFER_SIZE
;
334 priv
->dma_device
->rx_chan
[i
]->control
= IFXMIPS_DMA_CH_ON
;
337 for (i
= 0; i
< priv
->dma_device
->max_tx_chan_num
; i
++)
340 priv
->dma_device
->tx_chan
[i
]->control
= IFXMIPS_DMA_CH_ON
;
342 priv
->dma_device
->tx_chan
[i
]->control
= IFXMIPS_DMA_CH_OFF
;
345 dma_device_register(priv
->dma_device
);
347 /*read the mac address from the mac table and put them into the mac table.*/
348 for (i
= 0; i
< 6; i
++)
349 retval
+= u_boot_ethaddr
[i
];
352 /* ethaddr not set in u-boot ? */
355 printk(KERN_INFO DRVNAME
": using default MAC address\n");
356 dev
->dev_addr
[0] = 0x00;
357 dev
->dev_addr
[1] = 0x11;
358 dev
->dev_addr
[2] = 0x22;
359 dev
->dev_addr
[3] = 0x33;
360 dev
->dev_addr
[4] = 0x44;
361 dev
->dev_addr
[5] = 0x55;
363 for (i
= 0; i
< 6; i
++)
364 dev
->dev_addr
[i
] = u_boot_ethaddr
[i
];
371 ifxmips_sw_chip_init (int mode
)
373 ifxmips_pmu_enable(IFXMIPS_PMU_PWDCR_DMA
);
374 ifxmips_pmu_enable(IFXMIPS_PMU_PWDCR_PPE
);
376 if(mode
== REV_MII_MODE
)
377 ifxmips_w32((ifxmips_r32(IFXMIPS_PPE32_CFG
) & PPE32_MII_MASK
) | PPE32_MII_REVERSE
, IFXMIPS_PPE32_CFG
);
378 else if(mode
== MII_MODE
)
379 ifxmips_w32((ifxmips_r32(IFXMIPS_PPE32_CFG
) & PPE32_MII_MASK
) | PPE32_MII_NORMAL
, IFXMIPS_PPE32_CFG
);
381 ifxmips_w32(PPE32_PLEN_UNDER
| PPE32_PLEN_OVER
, IFXMIPS_PPE32_IG_PLEN_CTRL
);
383 ifxmips_w32(PPE32_CGEN
, IFXMIPS_PPE32_ENET_MAC_CFG
);
389 ifxmips_mii_probe(struct platform_device
*dev
)
393 ifxmips_mii0_dev
= alloc_etherdev(sizeof(struct switch_priv
));
395 ifxmips_mii0_dev
->init
= switch_init
;
397 strcpy(ifxmips_mii0_dev
->name
, "eth%d");
399 result
= register_netdev(ifxmips_mii0_dev
);
402 printk(KERN_INFO DRVNAME
": error %i registering device \"%s\"\n", result
, ifxmips_mii0_dev
->name
);
406 /* ifxmips eval kit connects the phy/switch in REV mode */
407 ifxmips_sw_chip_init(REV_MII_MODE
);
408 printk(KERN_INFO DRVNAME
": driver loaded!\n");
415 ifxmips_mii_remove(struct platform_device
*dev
)
417 struct switch_priv
*priv
= (struct switch_priv
*)ifxmips_mii0_dev
->priv
;
419 printk(KERN_INFO DRVNAME
": ifxmips_mii0 cleanup\n");
421 dma_device_unregister(priv
->dma_device
);
422 dma_device_release(priv
->dma_device
);
423 kfree(priv
->dma_device
);
424 kfree(ifxmips_mii0_dev
->priv
);
425 unregister_netdev(ifxmips_mii0_dev
);
431 platform_driver ifxmips_mii_driver
= {
432 .probe
= ifxmips_mii_probe
,
433 .remove
= ifxmips_mii_remove
,
436 .owner
= THIS_MODULE
,
441 ifxmips_mii_init(void)
443 int ret
= platform_driver_register(&ifxmips_mii_driver
);
445 printk(KERN_INFO DRVNAME
": Error registering platfom driver!");
451 ifxmips_mii_cleanup(void)
453 platform_driver_unregister(&ifxmips_mii_driver
);
456 module_init(ifxmips_mii_init
);
457 module_exit(ifxmips_mii_cleanup
);