blob: a53644f6a29be81db50ed77549c7945364976a23 [file] [log] [blame]
Sascha Hauer92aa6742006-06-22 07:11:13 +02001/*
2 * drivers/net/netx-eth.c
3 *
4 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Sascha Hauer92aa6742006-06-22 07:11:13 +020020#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/delay.h>
24
25#include <linux/netdevice.h>
26#include <linux/platform_device.h>
27#include <linux/etherdevice.h>
28#include <linux/skbuff.h>
29#include <linux/mii.h>
30
31#include <asm/io.h>
32#include <asm/hardware.h>
33#include <asm/arch/hardware.h>
34#include <asm/arch/netx-regs.h>
35#include <asm/arch/pfifo.h>
36#include <asm/arch/xc.h>
37#include <asm/arch/eth.h>
38
39/* XC Fifo Offsets */
40#define EMPTY_PTR_FIFO(xcno) (0 + ((xcno) << 3)) /* Index of the empty pointer FIFO */
41#define IND_FIFO_PORT_HI(xcno) (1 + ((xcno) << 3)) /* Index of the FIFO where received */
42 /* Data packages are indicated by XC */
43#define IND_FIFO_PORT_LO(xcno) (2 + ((xcno) << 3)) /* Index of the FIFO where received */
44 /* Data packages are indicated by XC */
45#define REQ_FIFO_PORT_HI(xcno) (3 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
46 /* have to be indicated by ARM which */
47 /* shall be sent */
48#define REQ_FIFO_PORT_LO(xcno) (4 + ((xcno) << 3)) /* Index of the FIFO where Data packages */
49 /* have to be indicated by ARM which shall */
50 /* be sent */
51#define CON_FIFO_PORT_HI(xcno) (5 + ((xcno) << 3)) /* Index of the FIFO where sent Data packages */
52 /* are confirmed */
53#define CON_FIFO_PORT_LO(xcno) (6 + ((xcno) << 3)) /* Index of the FIFO where sent Data */
54 /* packages are confirmed */
55#define PFIFO_MASK(xcno) (0x7f << (xcno*8))
56
57#define FIFO_PTR_FRAMELEN_SHIFT 0
58#define FIFO_PTR_FRAMELEN_MASK (0x7ff << 0)
59#define FIFO_PTR_FRAMELEN(len) (((len) << 0) & FIFO_PTR_FRAMELEN_MASK)
60#define FIFO_PTR_TIMETRIG (1<<11)
61#define FIFO_PTR_MULTI_REQ
62#define FIFO_PTR_ORIGIN (1<<14)
63#define FIFO_PTR_VLAN (1<<15)
64#define FIFO_PTR_FRAMENO_SHIFT 16
65#define FIFO_PTR_FRAMENO_MASK (0x3f << 16)
66#define FIFO_PTR_FRAMENO(no) (((no) << 16) & FIFO_PTR_FRAMENO_MASK)
67#define FIFO_PTR_SEGMENT_SHIFT 22
68#define FIFO_PTR_SEGMENT_MASK (0xf << 22)
69#define FIFO_PTR_SEGMENT(seg) (((seg) & 0xf) << 22)
70#define FIFO_PTR_ERROR_SHIFT 28
71#define FIFO_PTR_ERROR_MASK (0xf << 28)
72
73#define ISR_LINK_STATUS_CHANGE (1<<4)
74#define ISR_IND_LO (1<<3)
75#define ISR_CON_LO (1<<2)
76#define ISR_IND_HI (1<<1)
77#define ISR_CON_HI (1<<0)
78
79#define ETH_MAC_LOCAL_CONFIG 0x1560
80#define ETH_MAC_4321 0x1564
81#define ETH_MAC_65 0x1568
82
83#define MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT 16
84#define MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK (0xf<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT)
85#define MAC_TRAFFIC_CLASS_ARRANGEMENT(x) (((x)<<MAC_TRAFFIC_CLASS_ARRANGEMENT_SHIFT) & MAC_TRAFFIC_CLASS_ARRANGEMENT_MASK)
86#define LOCAL_CONFIG_LINK_STATUS_IRQ_EN (1<<24)
87#define LOCAL_CONFIG_CON_LO_IRQ_EN (1<<23)
88#define LOCAL_CONFIG_CON_HI_IRQ_EN (1<<22)
89#define LOCAL_CONFIG_IND_LO_IRQ_EN (1<<21)
90#define LOCAL_CONFIG_IND_HI_IRQ_EN (1<<20)
91
92#define CARDNAME "netx-eth"
93
94/* LSB must be zero */
95#define INTERNAL_PHY_ADR 0x1c
96
97struct netx_eth_priv {
98 void __iomem *sram_base, *xpec_base, *xmac_base;
99 int id;
100 struct net_device_stats stats;
101 struct mii_if_info mii;
102 u32 msg_enable;
103 struct xc *xc;
104 spinlock_t lock;
105};
106
107static void netx_eth_set_multicast_list(struct net_device *ndev)
108{
109 /* implement me */
110}
111
112static int
113netx_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
114{
115 struct netx_eth_priv *priv = netdev_priv(ndev);
116 unsigned char *buf = skb->data;
117 unsigned int len = skb->len;
118
119 spin_lock_irq(&priv->lock);
120 memcpy_toio(priv->sram_base + 1560, (void *)buf, len);
121 if (len < 60) {
122 memset_io(priv->sram_base + 1560 + len, 0, 60 - len);
123 len = 60;
124 }
125
126 pfifo_push(REQ_FIFO_PORT_LO(priv->id),
127 FIFO_PTR_SEGMENT(priv->id) |
128 FIFO_PTR_FRAMENO(1) |
129 FIFO_PTR_FRAMELEN(len));
130
131 ndev->trans_start = jiffies;
132 priv->stats.tx_packets++;
133 priv->stats.tx_bytes += skb->len;
134
135 netif_stop_queue(ndev);
136 spin_unlock_irq(&priv->lock);
137 dev_kfree_skb(skb);
138
139 return 0;
140}
141
142static void netx_eth_receive(struct net_device *ndev)
143{
144 struct netx_eth_priv *priv = netdev_priv(ndev);
145 unsigned int val, frameno, seg, len;
146 unsigned char *data;
147 struct sk_buff *skb;
148
149 val = pfifo_pop(IND_FIFO_PORT_LO(priv->id));
150
151 frameno = (val & FIFO_PTR_FRAMENO_MASK) >> FIFO_PTR_FRAMENO_SHIFT;
152 seg = (val & FIFO_PTR_SEGMENT_MASK) >> FIFO_PTR_SEGMENT_SHIFT;
153 len = (val & FIFO_PTR_FRAMELEN_MASK) >> FIFO_PTR_FRAMELEN_SHIFT;
154
155 skb = dev_alloc_skb(len);
156 if (unlikely(skb == NULL)) {
157 printk(KERN_NOTICE "%s: Low memory, packet dropped.\n",
158 ndev->name);
159 priv->stats.rx_dropped++;
160 return;
161 }
162
163 data = skb_put(skb, len);
164
165 memcpy_fromio(data, priv->sram_base + frameno * 1560, len);
166
167 pfifo_push(EMPTY_PTR_FIFO(priv->id),
168 FIFO_PTR_SEGMENT(seg) | FIFO_PTR_FRAMENO(frameno));
169
170 ndev->last_rx = jiffies;
171 skb->dev = ndev;
172 skb->protocol = eth_type_trans(skb, ndev);
173 netif_rx(skb);
174 priv->stats.rx_packets++;
175 priv->stats.rx_bytes += len;
176}
177
178static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100179netx_eth_interrupt(int irq, void *dev_id)
Sascha Hauer92aa6742006-06-22 07:11:13 +0200180{
181 struct net_device *ndev = dev_id;
182 struct netx_eth_priv *priv = netdev_priv(ndev);
183 int status;
184 unsigned long flags;
185
186 spin_lock_irqsave(&priv->lock, flags);
187
188 status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
189 while (status) {
190 int fill_level;
191 writel(status, NETX_PFIFO_XPEC_ISR(priv->id));
192
193 if ((status & ISR_CON_HI) || (status & ISR_IND_HI))
194 printk("%s: unexpected status: 0x%08x\n",
195 __FUNCTION__, status);
196
197 fill_level =
198 readl(NETX_PFIFO_FILL_LEVEL(IND_FIFO_PORT_LO(priv->id)));
199 while (fill_level--)
200 netx_eth_receive(ndev);
201
202 if (status & ISR_CON_LO)
203 netif_wake_queue(ndev);
204
205 if (status & ISR_LINK_STATUS_CHANGE)
206 mii_check_media(&priv->mii, netif_msg_link(priv), 1);
207
208 status = readl(NETX_PFIFO_XPEC_ISR(priv->id));
209 }
210 spin_unlock_irqrestore(&priv->lock, flags);
211 return IRQ_HANDLED;
212}
213
214static struct net_device_stats *netx_eth_query_statistics(struct net_device *ndev)
215{
216 struct netx_eth_priv *priv = netdev_priv(ndev);
217 return &priv->stats;
218}
219
220static int netx_eth_open(struct net_device *ndev)
221{
222 struct netx_eth_priv *priv = netdev_priv(ndev);
223
224 if (request_irq
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700225 (ndev->irq, &netx_eth_interrupt, IRQF_SHARED, ndev->name, ndev))
Sascha Hauer92aa6742006-06-22 07:11:13 +0200226 return -EAGAIN;
227
228 writel(ndev->dev_addr[0] |
229 ndev->dev_addr[1]<<8 |
230 ndev->dev_addr[2]<<16 |
231 ndev->dev_addr[3]<<24,
232 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
233 writel(ndev->dev_addr[4] |
234 ndev->dev_addr[5]<<8,
235 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
236
237 writel(LOCAL_CONFIG_LINK_STATUS_IRQ_EN |
238 LOCAL_CONFIG_CON_LO_IRQ_EN |
239 LOCAL_CONFIG_CON_HI_IRQ_EN |
240 LOCAL_CONFIG_IND_LO_IRQ_EN |
241 LOCAL_CONFIG_IND_HI_IRQ_EN,
242 priv->xpec_base + NETX_XPEC_RAM_START_OFS +
243 ETH_MAC_LOCAL_CONFIG);
244
245 mii_check_media(&priv->mii, netif_msg_link(priv), 1);
246 netif_start_queue(ndev);
247
248 return 0;
249}
250
251static int netx_eth_close(struct net_device *ndev)
252{
253 struct netx_eth_priv *priv = netdev_priv(ndev);
254
255 netif_stop_queue(ndev);
256
257 writel(0,
258 priv->xpec_base + NETX_XPEC_RAM_START_OFS + ETH_MAC_LOCAL_CONFIG);
259
260 free_irq(ndev->irq, ndev);
261
262 return 0;
263}
264
265static void netx_eth_timeout(struct net_device *ndev)
266{
267 struct netx_eth_priv *priv = netdev_priv(ndev);
268 int i;
269
270 printk(KERN_ERR "%s: transmit timed out, resetting\n", ndev->name);
271
272 spin_lock_irq(&priv->lock);
273
274 xc_reset(priv->xc);
275 xc_start(priv->xc);
276
277 for (i=2; i<=18; i++)
278 pfifo_push(EMPTY_PTR_FIFO(priv->id),
279 FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
280
281 spin_unlock_irq(&priv->lock);
282
283 netif_wake_queue(ndev);
284}
285
286static int
287netx_eth_phy_read(struct net_device *ndev, int phy_id, int reg)
288{
289 unsigned int val;
290
291 val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
292 MIIMU_REGADDR(reg) | MIIMU_PHY_NRES;
293
294 writel(val, NETX_MIIMU);
295 while (readl(NETX_MIIMU) & MIIMU_SNRDY);
296
297 return readl(NETX_MIIMU) >> 16;
298
299}
300
301static void
302netx_eth_phy_write(struct net_device *ndev, int phy_id, int reg, int value)
303{
304 unsigned int val;
305
306 val = MIIMU_SNRDY | MIIMU_PREAMBLE | MIIMU_PHYADDR(phy_id) |
307 MIIMU_REGADDR(reg) | MIIMU_PHY_NRES | MIIMU_OPMODE_WRITE |
308 MIIMU_DATA(value);
309
310 writel(val, NETX_MIIMU);
311 while (readl(NETX_MIIMU) & MIIMU_SNRDY);
312}
313
314static int netx_eth_enable(struct net_device *ndev)
315{
316 struct netx_eth_priv *priv = netdev_priv(ndev);
317 unsigned int mac4321, mac65;
318 int running, i;
319
320 ether_setup(ndev);
321
322 ndev->open = netx_eth_open;
323 ndev->stop = netx_eth_close;
324 ndev->hard_start_xmit = netx_eth_hard_start_xmit;
325 ndev->tx_timeout = netx_eth_timeout;
326 ndev->watchdog_timeo = msecs_to_jiffies(5000);
327 ndev->get_stats = netx_eth_query_statistics;
328 ndev->set_multicast_list = netx_eth_set_multicast_list;
329
330 priv->msg_enable = NETIF_MSG_LINK;
331 priv->mii.phy_id_mask = 0x1f;
332 priv->mii.reg_num_mask = 0x1f;
333 priv->mii.force_media = 0;
334 priv->mii.full_duplex = 0;
335 priv->mii.dev = ndev;
336 priv->mii.mdio_read = netx_eth_phy_read;
337 priv->mii.mdio_write = netx_eth_phy_write;
338 priv->mii.phy_id = INTERNAL_PHY_ADR + priv->id;
339
340 running = xc_running(priv->xc);
341 xc_stop(priv->xc);
342
343 /* if the xc engine is already running, assume the bootloader has
344 * loaded the firmware for us
345 */
346 if (running) {
347 /* get Node Address from hardware */
348 mac4321 = readl(priv->xpec_base +
349 NETX_XPEC_RAM_START_OFS + ETH_MAC_4321);
350 mac65 = readl(priv->xpec_base +
351 NETX_XPEC_RAM_START_OFS + ETH_MAC_65);
352
353 ndev->dev_addr[0] = mac4321 & 0xff;
354 ndev->dev_addr[1] = (mac4321 >> 8) & 0xff;
355 ndev->dev_addr[2] = (mac4321 >> 16) & 0xff;
356 ndev->dev_addr[3] = (mac4321 >> 24) & 0xff;
357 ndev->dev_addr[4] = mac65 & 0xff;
358 ndev->dev_addr[5] = (mac65 >> 8) & 0xff;
359 } else {
360 if (xc_request_firmware(priv->xc)) {
361 printk(CARDNAME ": requesting firmware failed\n");
362 return -ENODEV;
363 }
364 }
365
366 xc_reset(priv->xc);
367 xc_start(priv->xc);
368
369 if (!is_valid_ether_addr(ndev->dev_addr))
370 printk("%s: Invalid ethernet MAC address. Please "
371 "set using ifconfig\n", ndev->name);
372
373 for (i=2; i<=18; i++)
374 pfifo_push(EMPTY_PTR_FIFO(priv->id),
375 FIFO_PTR_FRAMENO(i) | FIFO_PTR_SEGMENT(priv->id));
376
377 return register_netdev(ndev);
378
379}
380
381static int netx_eth_drv_probe(struct platform_device *pdev)
382{
383 struct netx_eth_priv *priv;
384 struct net_device *ndev;
385 struct netxeth_platform_data *pdata;
386 int ret;
387
388 ndev = alloc_etherdev(sizeof (struct netx_eth_priv));
389 if (!ndev) {
390 printk("%s: could not allocate device.\n", CARDNAME);
391 ret = -ENOMEM;
392 goto exit;
393 }
394 SET_MODULE_OWNER(ndev);
395 SET_NETDEV_DEV(ndev, &pdev->dev);
396
397 platform_set_drvdata(pdev, ndev);
398
399 priv = netdev_priv(ndev);
400
401 pdata = (struct netxeth_platform_data *)pdev->dev.platform_data;
402 priv->xc = request_xc(pdata->xcno, &pdev->dev);
403 if (!priv->xc) {
404 dev_err(&pdev->dev, "unable to request xc engine\n");
405 ret = -ENODEV;
406 goto exit_free_netdev;
407 }
408
409 ndev->irq = priv->xc->irq;
410 priv->id = pdev->id;
411 priv->xpec_base = priv->xc->xpec_base;
412 priv->xmac_base = priv->xc->xmac_base;
413 priv->sram_base = priv->xc->sram_base;
414
415 ret = pfifo_request(PFIFO_MASK(priv->id));
416 if (ret) {
417 printk("unable to request PFIFO\n");
418 goto exit_free_xc;
419 }
420
421 ret = netx_eth_enable(ndev);
422 if (ret)
423 goto exit_free_pfifo;
424
425 return 0;
426exit_free_pfifo:
427 pfifo_free(PFIFO_MASK(priv->id));
428exit_free_xc:
429 free_xc(priv->xc);
430exit_free_netdev:
431 platform_set_drvdata(pdev, NULL);
432 free_netdev(ndev);
433exit:
434 return ret;
435}
436
437static int netx_eth_drv_remove(struct platform_device *pdev)
438{
439 struct net_device *ndev = dev_get_drvdata(&pdev->dev);
440 struct netx_eth_priv *priv = netdev_priv(ndev);
441
442 platform_set_drvdata(pdev, NULL);
443
444 unregister_netdev(ndev);
445 xc_stop(priv->xc);
446 free_xc(priv->xc);
447 free_netdev(ndev);
448 pfifo_free(PFIFO_MASK(priv->id));
449
450 return 0;
451}
452
453static int netx_eth_drv_suspend(struct platform_device *pdev, pm_message_t state)
454{
455 dev_err(&pdev->dev, "suspend not implemented\n");
456 return 0;
457}
458
459static int netx_eth_drv_resume(struct platform_device *pdev)
460{
461 dev_err(&pdev->dev, "resume not implemented\n");
462 return 0;
463}
464
465static struct platform_driver netx_eth_driver = {
466 .probe = netx_eth_drv_probe,
467 .remove = netx_eth_drv_remove,
468 .suspend = netx_eth_drv_suspend,
469 .resume = netx_eth_drv_resume,
470 .driver = {
471 .name = CARDNAME,
472 .owner = THIS_MODULE,
473 },
474};
475
476static int __init netx_eth_init(void)
477{
478 unsigned int phy_control, val;
479
480 printk("NetX Ethernet driver\n");
481
482 phy_control = PHY_CONTROL_PHY_ADDRESS(INTERNAL_PHY_ADR>>1) |
483 PHY_CONTROL_PHY1_MODE(PHY_MODE_ALL) |
484 PHY_CONTROL_PHY1_AUTOMDIX |
485 PHY_CONTROL_PHY1_EN |
486 PHY_CONTROL_PHY0_MODE(PHY_MODE_ALL) |
487 PHY_CONTROL_PHY0_AUTOMDIX |
488 PHY_CONTROL_PHY0_EN |
489 PHY_CONTROL_CLK_XLATIN;
490
491 val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
492 writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
493
494 writel(phy_control | PHY_CONTROL_RESET, NETX_SYSTEM_PHY_CONTROL);
495 udelay(100);
496
497 val = readl(NETX_SYSTEM_IOC_ACCESS_KEY);
498 writel(val, NETX_SYSTEM_IOC_ACCESS_KEY);
499
500 writel(phy_control, NETX_SYSTEM_PHY_CONTROL);
501
502 return platform_driver_register(&netx_eth_driver);
503}
504
505static void __exit netx_eth_cleanup(void)
506{
507 platform_driver_unregister(&netx_eth_driver);
508}
509
510module_init(netx_eth_init);
511module_exit(netx_eth_cleanup);
512
513MODULE_AUTHOR("Sascha Hauer, Pengutronix");
514MODULE_LICENSE("GPL");
515