2 * ks8842_main.c timberdale KS8842 ethernet driver
3 * Copyright (c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * The Micrel KS8842 behind the timberdale FPGA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
30 #define DRV_NAME "ks8842"
32 /* Timberdale specific Registers */
33 #define REG_TIMB_RST 0x1c
35 /* KS8842 registers */
37 #define REG_SELECT_BANK 0x0e
39 /* bank 0 registers */
40 #define REG_QRFCR 0x04
42 /* bank 2 registers */
47 /* bank 3 registers */
50 /* bank 16 registers */
54 #define REG_TXMIR 0x08
55 #define REG_RXMIR 0x0A
57 /* bank 17 registers */
58 #define REG_TXQCR 0x00
59 #define REG_RXQCR 0x02
60 #define REG_TXFDPR 0x04
61 #define REG_RXFDPR 0x06
62 #define REG_QMU_DATA_LO 0x08
63 #define REG_QMU_DATA_HI 0x0A
65 /* bank 18 registers */
67 #define IRQ_LINK_CHANGE 0x8000
70 #define IRQ_RX_OVERRUN 0x0800
71 #define IRQ_TX_STOPPED 0x0200
72 #define IRQ_RX_STOPPED 0x0100
73 #define IRQ_RX_ERROR 0x0080
74 #define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \
75 IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
78 #define RXSR_VALID 0x8000
79 #define RXSR_BROADCAST 0x80
80 #define RXSR_MULTICAST 0x40
81 #define RXSR_UNICAST 0x20
82 #define RXSR_FRAMETYPE 0x08
83 #define RXSR_TOO_LONG 0x04
84 #define RXSR_RUNT 0x02
85 #define RXSR_CRC_ERROR 0x01
86 #define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR)
88 /* bank 32 registers */
89 #define REG_SW_ID_AND_ENABLE 0x00
90 #define REG_SGCR1 0x02
91 #define REG_SGCR2 0x04
92 #define REG_SGCR3 0x06
94 /* bank 39 registers */
95 #define REG_MACAR1 0x00
96 #define REG_MACAR2 0x02
97 #define REG_MACAR3 0x04
99 /* bank 45 registers */
100 #define REG_P1MBCR 0x00
101 #define REG_P1MBSR 0x02
103 /* bank 46 registers */
104 #define REG_P2MBCR 0x00
105 #define REG_P2MBSR 0x02
107 /* bank 48 registers */
108 #define REG_P1CR2 0x02
110 /* bank 49 registers */
111 #define REG_P1CR4 0x02
112 #define REG_P1SR 0x04
114 struct ks8842_adapter {
115 void __iomem *hw_addr;
117 struct tasklet_struct tasklet;
118 spinlock_t lock; /* spinlock to be interrupt safe */
119 struct platform_device *pdev;
122 static inline void ks8842_select_bank(struct ks8842_adapter *adapter, u16 bank)
124 iowrite16(bank, adapter->hw_addr + REG_SELECT_BANK);
127 static inline void ks8842_write8(struct ks8842_adapter *adapter, u16 bank,
128 u8 value, int offset)
130 ks8842_select_bank(adapter, bank);
131 iowrite8(value, adapter->hw_addr + offset);
134 static inline void ks8842_write16(struct ks8842_adapter *adapter, u16 bank,
135 u16 value, int offset)
137 ks8842_select_bank(adapter, bank);
138 iowrite16(value, adapter->hw_addr + offset);
141 static inline void ks8842_enable_bits(struct ks8842_adapter *adapter, u16 bank,
142 u16 bits, int offset)
145 ks8842_select_bank(adapter, bank);
146 reg = ioread16(adapter->hw_addr + offset);
148 iowrite16(reg, adapter->hw_addr + offset);
151 static inline void ks8842_clear_bits(struct ks8842_adapter *adapter, u16 bank,
152 u16 bits, int offset)
155 ks8842_select_bank(adapter, bank);
156 reg = ioread16(adapter->hw_addr + offset);
158 iowrite16(reg, adapter->hw_addr + offset);
161 static inline void ks8842_write32(struct ks8842_adapter *adapter, u16 bank,
162 u32 value, int offset)
164 ks8842_select_bank(adapter, bank);
165 iowrite32(value, adapter->hw_addr + offset);
168 static inline u8 ks8842_read8(struct ks8842_adapter *adapter, u16 bank,
171 ks8842_select_bank(adapter, bank);
172 return ioread8(adapter->hw_addr + offset);
175 static inline u16 ks8842_read16(struct ks8842_adapter *adapter, u16 bank,
178 ks8842_select_bank(adapter, bank);
179 return ioread16(adapter->hw_addr + offset);
182 static inline u32 ks8842_read32(struct ks8842_adapter *adapter, u16 bank,
185 ks8842_select_bank(adapter, bank);
186 return ioread32(adapter->hw_addr + offset);
189 static void ks8842_reset(struct ks8842_adapter *adapter)
191 /* The KS8842 goes haywire when doing softare reset
192 * a work around in the timberdale IP is implemented to
193 * do a hardware reset instead
194 ks8842_write16(adapter, 3, 1, REG_GRR);
196 iowrite16(0, adapter->hw_addr + REG_GRR);
198 iowrite16(32, adapter->hw_addr + REG_SELECT_BANK);
199 iowrite32(0x1, adapter->hw_addr + REG_TIMB_RST);
203 static void ks8842_update_link_status(struct net_device *netdev,
204 struct ks8842_adapter *adapter)
206 /* check the status of the link */
207 if (ks8842_read16(adapter, 45, REG_P1MBSR) & 0x4) {
208 netif_carrier_on(netdev);
209 netif_wake_queue(netdev);
211 netif_stop_queue(netdev);
212 netif_carrier_off(netdev);
216 static void ks8842_enable_tx(struct ks8842_adapter *adapter)
218 ks8842_enable_bits(adapter, 16, 0x01, REG_TXCR);
221 static void ks8842_disable_tx(struct ks8842_adapter *adapter)
223 ks8842_clear_bits(adapter, 16, 0x01, REG_TXCR);
226 static void ks8842_enable_rx(struct ks8842_adapter *adapter)
228 ks8842_enable_bits(adapter, 16, 0x01, REG_RXCR);
231 static void ks8842_disable_rx(struct ks8842_adapter *adapter)
233 ks8842_clear_bits(adapter, 16, 0x01, REG_RXCR);
236 static void ks8842_reset_hw(struct ks8842_adapter *adapter)
239 ks8842_reset(adapter);
241 /* Enable QMU Transmit flow control / transmit padding / Transmit CRC */
242 ks8842_write16(adapter, 16, 0x000E, REG_TXCR);
244 /* enable the receiver, uni + multi + broadcast + flow ctrl
246 ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400,
249 /* TX frame pointer autoincrement */
250 ks8842_write16(adapter, 17, 0x4000, REG_TXFDPR);
252 /* RX frame pointer autoincrement */
253 ks8842_write16(adapter, 17, 0x4000, REG_RXFDPR);
255 /* RX 2 kb high watermark */
256 ks8842_write16(adapter, 0, 0x1000, REG_QRFCR);
258 /* aggresive back off in half duplex */
259 ks8842_enable_bits(adapter, 32, 1 << 8, REG_SGCR1);
261 /* enable no excessive collison drop */
262 ks8842_enable_bits(adapter, 32, 1 << 3, REG_SGCR2);
264 /* Enable port 1 force flow control / back pressure / transmit / recv */
265 ks8842_write16(adapter, 48, 0x1E07, REG_P1CR2);
267 /* restart port auto-negotiation */
268 ks8842_enable_bits(adapter, 49, 1 << 13, REG_P1CR4);
269 /* only advertise 10Mbps */
270 ks8842_clear_bits(adapter, 49, 3 << 2, REG_P1CR4);
272 /* Enable the transmitter */
273 ks8842_enable_tx(adapter);
275 /* Enable the receiver */
276 ks8842_enable_rx(adapter);
278 /* clear all interrupts */
279 ks8842_write16(adapter, 18, 0xffff, REG_ISR);
281 /* enable interrupts */
282 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
284 /* enable the switch */
285 ks8842_write16(adapter, 32, 0x1, REG_SW_ID_AND_ENABLE);
288 static void ks8842_read_mac_addr(struct ks8842_adapter *adapter, u8 *dest)
293 for (i = 0; i < ETH_ALEN; i++)
294 dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2, REG_MARL + i);
296 /* make sure the switch port uses the same MAC as the QMU */
297 mac = ks8842_read16(adapter, 2, REG_MARL);
298 ks8842_write16(adapter, 39, mac, REG_MACAR1);
299 mac = ks8842_read16(adapter, 2, REG_MARM);
300 ks8842_write16(adapter, 39, mac, REG_MACAR2);
301 mac = ks8842_read16(adapter, 2, REG_MARH);
302 ks8842_write16(adapter, 39, mac, REG_MACAR3);
305 static inline u16 ks8842_tx_fifo_space(struct ks8842_adapter *adapter)
307 return ks8842_read16(adapter, 16, REG_TXMIR) & 0x1fff;
310 static int ks8842_tx_frame(struct sk_buff *skb, struct net_device *netdev)
312 struct ks8842_adapter *adapter = netdev_priv(netdev);
314 u32 *ptr = (u32 *)skb->data;
317 dev_dbg(&adapter->pdev->dev,
318 "%s: len %u head %p data %p tail %p end %p\n",
319 __func__, skb->len, skb->head, skb->data,
320 skb_tail_pointer(skb), skb_end_pointer(skb));
322 /* check FIFO buffer space, we need space for CRC and command bits */
323 if (ks8842_tx_fifo_space(adapter) < len + 8)
324 return NETDEV_TX_BUSY;
326 /* the control word, enable IRQ, port 1 and the length */
327 ctrl = 0x8000 | 0x100 | (len << 16);
328 ks8842_write32(adapter, 17, ctrl, REG_QMU_DATA_LO);
330 netdev->stats.tx_bytes += len;
334 iowrite32(*ptr, adapter->hw_addr + REG_QMU_DATA_LO);
340 ks8842_write16(adapter, 17, 1, REG_TXQCR);
347 static void ks8842_rx_frame(struct net_device *netdev,
348 struct ks8842_adapter *adapter)
350 u32 status = ks8842_read32(adapter, 17, REG_QMU_DATA_LO);
351 int len = (status >> 16) & 0x7ff;
355 dev_dbg(&adapter->pdev->dev, "%s - rx_data: status: %x\n",
358 /* check the status */
359 if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) {
360 struct sk_buff *skb = netdev_alloc_skb(netdev, len + 2);
362 dev_dbg(&adapter->pdev->dev, "%s, got package, len: %d\n",
367 netdev->stats.rx_packets++;
368 netdev->stats.rx_bytes += len;
369 if (status & RXSR_MULTICAST)
370 netdev->stats.multicast++;
372 /* Align socket buffer in 4-byte boundary for
373 better performance. */
375 data = (u32 *)skb_put(skb, len);
377 ks8842_select_bank(adapter, 17);
379 *data++ = ioread32(adapter->hw_addr +
384 skb->protocol = eth_type_trans(skb, netdev);
387 netdev->stats.rx_dropped++;
389 dev_dbg(&adapter->pdev->dev, "RX error, status: %x\n", status);
390 netdev->stats.rx_errors++;
391 if (status & RXSR_TOO_LONG)
392 netdev->stats.rx_length_errors++;
393 if (status & RXSR_CRC_ERROR)
394 netdev->stats.rx_crc_errors++;
395 if (status & RXSR_RUNT)
396 netdev->stats.rx_frame_errors++;
399 /* set high watermark to 3K */
400 ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR);
402 /* release the frame */
403 ks8842_write16(adapter, 17, 0x01, REG_RXQCR);
405 /* set high watermark to 2K */
406 ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR);
409 void ks8842_handle_rx(struct net_device *netdev, struct ks8842_adapter *adapter)
411 u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
412 dev_dbg(&adapter->pdev->dev, "%s Entry - rx_data: %d\n",
415 ks8842_rx_frame(netdev, adapter);
416 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
420 void ks8842_handle_tx(struct net_device *netdev, struct ks8842_adapter *adapter)
422 u16 sr = ks8842_read16(adapter, 16, REG_TXSR);
423 dev_dbg(&adapter->pdev->dev, "%s - entry, sr: %x\n", __func__, sr);
424 netdev->stats.tx_packets++;
425 if (netif_queue_stopped(netdev))
426 netif_wake_queue(netdev);
429 void ks8842_handle_rx_overrun(struct net_device *netdev,
430 struct ks8842_adapter *adapter)
432 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
433 netdev->stats.rx_errors++;
434 netdev->stats.rx_fifo_errors++;
437 void ks8842_tasklet(unsigned long arg)
439 struct net_device *netdev = (struct net_device *)arg;
440 struct ks8842_adapter *adapter = netdev_priv(netdev);
445 /* read current bank to be able to set it back */
446 spin_lock_irqsave(&adapter->lock, flags);
447 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
448 spin_unlock_irqrestore(&adapter->lock, flags);
450 isr = ks8842_read16(adapter, 18, REG_ISR);
451 dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr);
454 ks8842_write16(adapter, 18, isr, REG_ISR);
456 if (!netif_running(netdev))
459 if (isr & IRQ_LINK_CHANGE)
460 ks8842_update_link_status(netdev, adapter);
462 if (isr & (IRQ_RX | IRQ_RX_ERROR))
463 ks8842_handle_rx(netdev, adapter);
466 ks8842_handle_tx(netdev, adapter);
468 if (isr & IRQ_RX_OVERRUN)
469 ks8842_handle_rx_overrun(netdev, adapter);
471 if (isr & IRQ_TX_STOPPED) {
472 ks8842_disable_tx(adapter);
473 ks8842_enable_tx(adapter);
476 if (isr & IRQ_RX_STOPPED) {
477 ks8842_disable_rx(adapter);
478 ks8842_enable_rx(adapter);
481 /* re-enable interrupts, put back the bank selection register */
482 spin_lock_irqsave(&adapter->lock, flags);
483 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
484 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
485 spin_unlock_irqrestore(&adapter->lock, flags);
488 static irqreturn_t ks8842_irq(int irq, void *devid)
490 struct ks8842_adapter *adapter = devid;
492 u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
493 irqreturn_t ret = IRQ_NONE;
495 isr = ks8842_read16(adapter, 18, REG_ISR);
496 dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr);
500 ks8842_write16(adapter, 18, 0x00, REG_IER);
502 /* schedule tasklet */
503 tasklet_schedule(&adapter->tasklet);
508 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
514 /* Netdevice operations */
516 static int ks8842_open(struct net_device *netdev)
518 struct ks8842_adapter *adapter = netdev_priv(netdev);
521 dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__);
524 ks8842_reset_hw(adapter);
526 ks8842_update_link_status(netdev, adapter);
528 err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME,
531 printk(KERN_ERR "Failed to request IRQ: %d: %d\n",
539 static int ks8842_close(struct net_device *netdev)
541 struct ks8842_adapter *adapter = netdev_priv(netdev);
543 dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__);
546 free_irq(adapter->irq, adapter);
548 /* disable the switch */
549 ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE);
554 static int ks8842_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
557 struct ks8842_adapter *adapter = netdev_priv(netdev);
559 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
561 ret = ks8842_tx_frame(skb, netdev);
563 if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8)
564 netif_stop_queue(netdev);
569 static int ks8842_set_mac(struct net_device *netdev, void *p)
571 struct ks8842_adapter *adapter = netdev_priv(netdev);
573 struct sockaddr *addr = p;
574 char *mac = (u8 *)addr->sa_data;
577 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
579 if (!is_valid_ether_addr(addr->sa_data))
580 return -EADDRNOTAVAIL;
582 memcpy(netdev->dev_addr, mac, netdev->addr_len);
584 spin_lock_irqsave(&adapter->lock, flags);
585 for (i = 0; i < ETH_ALEN; i++) {
586 ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i);
587 ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1],
590 spin_unlock_irqrestore(&adapter->lock, flags);
594 static void ks8842_tx_timeout(struct net_device *netdev)
596 struct ks8842_adapter *adapter = netdev_priv(netdev);
599 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
601 spin_lock_irqsave(&adapter->lock, flags);
602 /* disable interrupts */
603 ks8842_write16(adapter, 18, 0, REG_IER);
604 ks8842_write16(adapter, 18, 0xFFFF, REG_ISR);
605 spin_unlock_irqrestore(&adapter->lock, flags);
607 ks8842_reset_hw(adapter);
609 ks8842_update_link_status(netdev, adapter);
612 static const struct net_device_ops ks8842_netdev_ops = {
613 .ndo_open = ks8842_open,
614 .ndo_stop = ks8842_close,
615 .ndo_start_xmit = ks8842_xmit_frame,
616 .ndo_set_mac_address = ks8842_set_mac,
617 .ndo_tx_timeout = ks8842_tx_timeout,
618 .ndo_validate_addr = eth_validate_addr
621 static struct ethtool_ops ks8842_ethtool_ops = {
622 .get_link = ethtool_op_get_link,
625 static int __devinit ks8842_probe(struct platform_device *pdev)
628 struct resource *iomem;
629 struct net_device *netdev;
630 struct ks8842_adapter *adapter;
633 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
634 if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME))
637 netdev = alloc_etherdev(sizeof(struct ks8842_adapter));
639 goto err_alloc_etherdev;
641 SET_NETDEV_DEV(netdev, &pdev->dev);
643 adapter = netdev_priv(netdev);
644 adapter->hw_addr = ioremap(iomem->start, resource_size(iomem));
645 if (!adapter->hw_addr)
648 adapter->irq = platform_get_irq(pdev, 0);
649 if (adapter->irq < 0) {
654 adapter->pdev = pdev;
656 tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev);
657 spin_lock_init(&adapter->lock);
659 netdev->netdev_ops = &ks8842_netdev_ops;
660 netdev->ethtool_ops = &ks8842_ethtool_ops;
662 ks8842_read_mac_addr(adapter, netdev->dev_addr);
664 id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE);
666 strcpy(netdev->name, "eth%d");
667 err = register_netdev(netdev);
671 platform_set_drvdata(pdev, netdev);
673 printk(KERN_INFO DRV_NAME
674 " Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
675 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
681 iounmap(adapter->hw_addr);
685 release_mem_region(iomem->start, resource_size(iomem));
690 static int __devexit ks8842_remove(struct platform_device *pdev)
692 struct net_device *netdev = platform_get_drvdata(pdev);
693 struct ks8842_adapter *adapter = netdev_priv(netdev);
694 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
696 unregister_netdev(netdev);
697 tasklet_kill(&adapter->tasklet);
698 iounmap(adapter->hw_addr);
700 release_mem_region(iomem->start, resource_size(iomem));
701 platform_set_drvdata(pdev, NULL);
706 static struct platform_driver ks8842_platform_driver = {
709 .owner = THIS_MODULE,
711 .probe = ks8842_probe,
712 .remove = ks8842_remove,
715 static int __init ks8842_init(void)
717 return platform_driver_register(&ks8842_platform_driver);
720 static void __exit ks8842_exit(void)
722 platform_driver_unregister(&ks8842_platform_driver);
725 module_init(ks8842_init);
726 module_exit(ks8842_exit);
728 MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver");
729 MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
730 MODULE_LICENSE("GPL v2");
731 MODULE_ALIAS("platform:ks8842");