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