blob: f852ab3ae9cf2aaa86ff6ac9718d8b839bea6bdb [file] [log] [blame]
Richard Ršöjforsb07878e2009-06-04 03:35:55 +00001/*
Richard Röjforsa1aa8822010-04-21 16:33:29 -07002 * ks8842.c timberdale KS8842 ethernet driver
Richard Ršöjforsb07878e2009-06-04 03:35:55 +00003 * Copyright (c) 2009 Intel Corporation
4 *
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.
8 *
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.
13 *
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.
17 */
18
19/* Supports:
20 * The Micrel KS8842 behind the timberdale FPGA
21 */
22
Joe Perches0dc7d2b2010-02-27 14:43:51 +000023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Richard Ršöjforsb07878e2009-06-04 03:35:55 +000025#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/platform_device.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
Richard Röjforsa1aa8822010-04-21 16:33:29 -070031#include <linux/ks8842.h>
Richard Ršöjforsb07878e2009-06-04 03:35:55 +000032
33#define DRV_NAME "ks8842"
34
35/* Timberdale specific Registers */
36#define REG_TIMB_RST 0x1c
37
38/* KS8842 registers */
39
40#define REG_SELECT_BANK 0x0e
41
42/* bank 0 registers */
43#define REG_QRFCR 0x04
44
45/* bank 2 registers */
46#define REG_MARL 0x00
47#define REG_MARM 0x02
48#define REG_MARH 0x04
49
50/* bank 3 registers */
51#define REG_GRR 0x06
52
53/* bank 16 registers */
54#define REG_TXCR 0x00
55#define REG_TXSR 0x02
56#define REG_RXCR 0x04
57#define REG_TXMIR 0x08
58#define REG_RXMIR 0x0A
59
60/* bank 17 registers */
61#define REG_TXQCR 0x00
62#define REG_RXQCR 0x02
63#define REG_TXFDPR 0x04
64#define REG_RXFDPR 0x06
65#define REG_QMU_DATA_LO 0x08
66#define REG_QMU_DATA_HI 0x0A
67
68/* bank 18 registers */
69#define REG_IER 0x00
70#define IRQ_LINK_CHANGE 0x8000
71#define IRQ_TX 0x4000
72#define IRQ_RX 0x2000
73#define IRQ_RX_OVERRUN 0x0800
74#define IRQ_TX_STOPPED 0x0200
75#define IRQ_RX_STOPPED 0x0100
76#define IRQ_RX_ERROR 0x0080
77#define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \
78 IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
79#define REG_ISR 0x02
80#define REG_RXSR 0x04
81#define RXSR_VALID 0x8000
82#define RXSR_BROADCAST 0x80
83#define RXSR_MULTICAST 0x40
84#define RXSR_UNICAST 0x20
85#define RXSR_FRAMETYPE 0x08
86#define RXSR_TOO_LONG 0x04
87#define RXSR_RUNT 0x02
88#define RXSR_CRC_ERROR 0x01
89#define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR)
90
91/* bank 32 registers */
92#define REG_SW_ID_AND_ENABLE 0x00
93#define REG_SGCR1 0x02
94#define REG_SGCR2 0x04
95#define REG_SGCR3 0x06
96
97/* bank 39 registers */
98#define REG_MACAR1 0x00
99#define REG_MACAR2 0x02
100#define REG_MACAR3 0x04
101
102/* bank 45 registers */
103#define REG_P1MBCR 0x00
104#define REG_P1MBSR 0x02
105
106/* bank 46 registers */
107#define REG_P2MBCR 0x00
108#define REG_P2MBSR 0x02
109
110/* bank 48 registers */
111#define REG_P1CR2 0x02
112
113/* bank 49 registers */
114#define REG_P1CR4 0x02
115#define REG_P1SR 0x04
116
117struct ks8842_adapter {
118 void __iomem *hw_addr;
119 int irq;
120 struct tasklet_struct tasklet;
121 spinlock_t lock; /* spinlock to be interrupt safe */
122 struct platform_device *pdev;
123};
124
125static inline void ks8842_select_bank(struct ks8842_adapter *adapter, u16 bank)
126{
127 iowrite16(bank, adapter->hw_addr + REG_SELECT_BANK);
128}
129
130static inline void ks8842_write8(struct ks8842_adapter *adapter, u16 bank,
131 u8 value, int offset)
132{
133 ks8842_select_bank(adapter, bank);
134 iowrite8(value, adapter->hw_addr + offset);
135}
136
137static inline void ks8842_write16(struct ks8842_adapter *adapter, u16 bank,
138 u16 value, int offset)
139{
140 ks8842_select_bank(adapter, bank);
141 iowrite16(value, adapter->hw_addr + offset);
142}
143
144static inline void ks8842_enable_bits(struct ks8842_adapter *adapter, u16 bank,
145 u16 bits, int offset)
146{
147 u16 reg;
148 ks8842_select_bank(adapter, bank);
149 reg = ioread16(adapter->hw_addr + offset);
150 reg |= bits;
151 iowrite16(reg, adapter->hw_addr + offset);
152}
153
154static inline void ks8842_clear_bits(struct ks8842_adapter *adapter, u16 bank,
155 u16 bits, int offset)
156{
157 u16 reg;
158 ks8842_select_bank(adapter, bank);
159 reg = ioread16(adapter->hw_addr + offset);
160 reg &= ~bits;
161 iowrite16(reg, adapter->hw_addr + offset);
162}
163
164static inline void ks8842_write32(struct ks8842_adapter *adapter, u16 bank,
165 u32 value, int offset)
166{
167 ks8842_select_bank(adapter, bank);
168 iowrite32(value, adapter->hw_addr + offset);
169}
170
171static inline u8 ks8842_read8(struct ks8842_adapter *adapter, u16 bank,
172 int offset)
173{
174 ks8842_select_bank(adapter, bank);
175 return ioread8(adapter->hw_addr + offset);
176}
177
178static inline u16 ks8842_read16(struct ks8842_adapter *adapter, u16 bank,
179 int offset)
180{
181 ks8842_select_bank(adapter, bank);
182 return ioread16(adapter->hw_addr + offset);
183}
184
185static inline u32 ks8842_read32(struct ks8842_adapter *adapter, u16 bank,
186 int offset)
187{
188 ks8842_select_bank(adapter, bank);
189 return ioread32(adapter->hw_addr + offset);
190}
191
192static void ks8842_reset(struct ks8842_adapter *adapter)
193{
194 /* The KS8842 goes haywire when doing softare reset
195 * a work around in the timberdale IP is implemented to
196 * do a hardware reset instead
197 ks8842_write16(adapter, 3, 1, REG_GRR);
198 msleep(10);
199 iowrite16(0, adapter->hw_addr + REG_GRR);
200 */
201 iowrite16(32, adapter->hw_addr + REG_SELECT_BANK);
202 iowrite32(0x1, adapter->hw_addr + REG_TIMB_RST);
203 msleep(20);
204}
205
206static void ks8842_update_link_status(struct net_device *netdev,
207 struct ks8842_adapter *adapter)
208{
209 /* check the status of the link */
210 if (ks8842_read16(adapter, 45, REG_P1MBSR) & 0x4) {
211 netif_carrier_on(netdev);
212 netif_wake_queue(netdev);
213 } else {
214 netif_stop_queue(netdev);
215 netif_carrier_off(netdev);
216 }
217}
218
219static void ks8842_enable_tx(struct ks8842_adapter *adapter)
220{
221 ks8842_enable_bits(adapter, 16, 0x01, REG_TXCR);
222}
223
224static void ks8842_disable_tx(struct ks8842_adapter *adapter)
225{
226 ks8842_clear_bits(adapter, 16, 0x01, REG_TXCR);
227}
228
229static void ks8842_enable_rx(struct ks8842_adapter *adapter)
230{
231 ks8842_enable_bits(adapter, 16, 0x01, REG_RXCR);
232}
233
234static void ks8842_disable_rx(struct ks8842_adapter *adapter)
235{
236 ks8842_clear_bits(adapter, 16, 0x01, REG_RXCR);
237}
238
239static void ks8842_reset_hw(struct ks8842_adapter *adapter)
240{
241 /* reset the HW */
242 ks8842_reset(adapter);
243
244 /* Enable QMU Transmit flow control / transmit padding / Transmit CRC */
245 ks8842_write16(adapter, 16, 0x000E, REG_TXCR);
246
247 /* enable the receiver, uni + multi + broadcast + flow ctrl
248 + crc strip */
249 ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400,
250 REG_RXCR);
251
252 /* TX frame pointer autoincrement */
253 ks8842_write16(adapter, 17, 0x4000, REG_TXFDPR);
254
255 /* RX frame pointer autoincrement */
256 ks8842_write16(adapter, 17, 0x4000, REG_RXFDPR);
257
258 /* RX 2 kb high watermark */
259 ks8842_write16(adapter, 0, 0x1000, REG_QRFCR);
260
261 /* aggresive back off in half duplex */
262 ks8842_enable_bits(adapter, 32, 1 << 8, REG_SGCR1);
263
264 /* enable no excessive collison drop */
265 ks8842_enable_bits(adapter, 32, 1 << 3, REG_SGCR2);
266
267 /* Enable port 1 force flow control / back pressure / transmit / recv */
268 ks8842_write16(adapter, 48, 0x1E07, REG_P1CR2);
269
270 /* restart port auto-negotiation */
271 ks8842_enable_bits(adapter, 49, 1 << 13, REG_P1CR4);
272 /* only advertise 10Mbps */
273 ks8842_clear_bits(adapter, 49, 3 << 2, REG_P1CR4);
274
275 /* Enable the transmitter */
276 ks8842_enable_tx(adapter);
277
278 /* Enable the receiver */
279 ks8842_enable_rx(adapter);
280
281 /* clear all interrupts */
282 ks8842_write16(adapter, 18, 0xffff, REG_ISR);
283
284 /* enable interrupts */
285 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
286
287 /* enable the switch */
288 ks8842_write16(adapter, 32, 0x1, REG_SW_ID_AND_ENABLE);
289}
290
291static void ks8842_read_mac_addr(struct ks8842_adapter *adapter, u8 *dest)
292{
293 int i;
294 u16 mac;
295
296 for (i = 0; i < ETH_ALEN; i++)
297 dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2, REG_MARL + i);
298
299 /* make sure the switch port uses the same MAC as the QMU */
300 mac = ks8842_read16(adapter, 2, REG_MARL);
301 ks8842_write16(adapter, 39, mac, REG_MACAR1);
302 mac = ks8842_read16(adapter, 2, REG_MARM);
303 ks8842_write16(adapter, 39, mac, REG_MACAR2);
304 mac = ks8842_read16(adapter, 2, REG_MARH);
305 ks8842_write16(adapter, 39, mac, REG_MACAR3);
306}
307
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700308static void ks8842_write_mac_addr(struct ks8842_adapter *adapter, u8 *mac)
309{
310 unsigned long flags;
311 unsigned i;
312
313 spin_lock_irqsave(&adapter->lock, flags);
314 for (i = 0; i < ETH_ALEN; i++) {
315 ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i);
316 ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1],
317 REG_MACAR1 + i);
318 }
319 spin_unlock_irqrestore(&adapter->lock, flags);
320}
321
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000322static inline u16 ks8842_tx_fifo_space(struct ks8842_adapter *adapter)
323{
324 return ks8842_read16(adapter, 16, REG_TXMIR) & 0x1fff;
325}
326
327static int ks8842_tx_frame(struct sk_buff *skb, struct net_device *netdev)
328{
329 struct ks8842_adapter *adapter = netdev_priv(netdev);
330 int len = skb->len;
331 u32 *ptr = (u32 *)skb->data;
332 u32 ctrl;
333
334 dev_dbg(&adapter->pdev->dev,
335 "%s: len %u head %p data %p tail %p end %p\n",
336 __func__, skb->len, skb->head, skb->data,
337 skb_tail_pointer(skb), skb_end_pointer(skb));
338
339 /* check FIFO buffer space, we need space for CRC and command bits */
340 if (ks8842_tx_fifo_space(adapter) < len + 8)
341 return NETDEV_TX_BUSY;
342
343 /* the control word, enable IRQ, port 1 and the length */
344 ctrl = 0x8000 | 0x100 | (len << 16);
345 ks8842_write32(adapter, 17, ctrl, REG_QMU_DATA_LO);
346
347 netdev->stats.tx_bytes += len;
348
349 /* copy buffer */
350 while (len > 0) {
351 iowrite32(*ptr, adapter->hw_addr + REG_QMU_DATA_LO);
352 len -= sizeof(u32);
353 ptr++;
354 }
355
356 /* enqueue packet */
357 ks8842_write16(adapter, 17, 1, REG_TXQCR);
358
359 dev_kfree_skb(skb);
360
361 return NETDEV_TX_OK;
362}
363
364static void ks8842_rx_frame(struct net_device *netdev,
365 struct ks8842_adapter *adapter)
366{
367 u32 status = ks8842_read32(adapter, 17, REG_QMU_DATA_LO);
368 int len = (status >> 16) & 0x7ff;
369
370 status &= 0xffff;
371
372 dev_dbg(&adapter->pdev->dev, "%s - rx_data: status: %x\n",
373 __func__, status);
374
375 /* check the status */
376 if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) {
Eric Dumazet89d71a62009-10-13 05:34:20 +0000377 struct sk_buff *skb = netdev_alloc_skb_ip_align(netdev, len);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000378
379 dev_dbg(&adapter->pdev->dev, "%s, got package, len: %d\n",
380 __func__, len);
381 if (skb) {
382 u32 *data;
383
384 netdev->stats.rx_packets++;
385 netdev->stats.rx_bytes += len;
386 if (status & RXSR_MULTICAST)
387 netdev->stats.multicast++;
388
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000389 data = (u32 *)skb_put(skb, len);
390
391 ks8842_select_bank(adapter, 17);
392 while (len > 0) {
393 *data++ = ioread32(adapter->hw_addr +
394 REG_QMU_DATA_LO);
395 len -= sizeof(u32);
396 }
397
398 skb->protocol = eth_type_trans(skb, netdev);
399 netif_rx(skb);
400 } else
401 netdev->stats.rx_dropped++;
402 } else {
403 dev_dbg(&adapter->pdev->dev, "RX error, status: %x\n", status);
404 netdev->stats.rx_errors++;
405 if (status & RXSR_TOO_LONG)
406 netdev->stats.rx_length_errors++;
407 if (status & RXSR_CRC_ERROR)
408 netdev->stats.rx_crc_errors++;
409 if (status & RXSR_RUNT)
410 netdev->stats.rx_frame_errors++;
411 }
412
413 /* set high watermark to 3K */
414 ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR);
415
416 /* release the frame */
417 ks8842_write16(adapter, 17, 0x01, REG_RXQCR);
418
419 /* set high watermark to 2K */
420 ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR);
421}
422
423void ks8842_handle_rx(struct net_device *netdev, struct ks8842_adapter *adapter)
424{
425 u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
426 dev_dbg(&adapter->pdev->dev, "%s Entry - rx_data: %d\n",
427 __func__, rx_data);
428 while (rx_data) {
429 ks8842_rx_frame(netdev, adapter);
430 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
431 }
432}
433
434void ks8842_handle_tx(struct net_device *netdev, struct ks8842_adapter *adapter)
435{
436 u16 sr = ks8842_read16(adapter, 16, REG_TXSR);
437 dev_dbg(&adapter->pdev->dev, "%s - entry, sr: %x\n", __func__, sr);
438 netdev->stats.tx_packets++;
439 if (netif_queue_stopped(netdev))
440 netif_wake_queue(netdev);
441}
442
443void ks8842_handle_rx_overrun(struct net_device *netdev,
444 struct ks8842_adapter *adapter)
445{
446 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
447 netdev->stats.rx_errors++;
448 netdev->stats.rx_fifo_errors++;
449}
450
451void ks8842_tasklet(unsigned long arg)
452{
453 struct net_device *netdev = (struct net_device *)arg;
454 struct ks8842_adapter *adapter = netdev_priv(netdev);
455 u16 isr;
456 unsigned long flags;
457 u16 entry_bank;
458
459 /* read current bank to be able to set it back */
460 spin_lock_irqsave(&adapter->lock, flags);
461 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
462 spin_unlock_irqrestore(&adapter->lock, flags);
463
464 isr = ks8842_read16(adapter, 18, REG_ISR);
465 dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr);
466
467 /* Ack */
468 ks8842_write16(adapter, 18, isr, REG_ISR);
469
470 if (!netif_running(netdev))
471 return;
472
473 if (isr & IRQ_LINK_CHANGE)
474 ks8842_update_link_status(netdev, adapter);
475
476 if (isr & (IRQ_RX | IRQ_RX_ERROR))
477 ks8842_handle_rx(netdev, adapter);
478
479 if (isr & IRQ_TX)
480 ks8842_handle_tx(netdev, adapter);
481
482 if (isr & IRQ_RX_OVERRUN)
483 ks8842_handle_rx_overrun(netdev, adapter);
484
485 if (isr & IRQ_TX_STOPPED) {
486 ks8842_disable_tx(adapter);
487 ks8842_enable_tx(adapter);
488 }
489
490 if (isr & IRQ_RX_STOPPED) {
491 ks8842_disable_rx(adapter);
492 ks8842_enable_rx(adapter);
493 }
494
495 /* re-enable interrupts, put back the bank selection register */
496 spin_lock_irqsave(&adapter->lock, flags);
497 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
498 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
499 spin_unlock_irqrestore(&adapter->lock, flags);
500}
501
502static irqreturn_t ks8842_irq(int irq, void *devid)
503{
504 struct ks8842_adapter *adapter = devid;
505 u16 isr;
506 u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
507 irqreturn_t ret = IRQ_NONE;
508
509 isr = ks8842_read16(adapter, 18, REG_ISR);
510 dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr);
511
512 if (isr) {
513 /* disable IRQ */
514 ks8842_write16(adapter, 18, 0x00, REG_IER);
515
516 /* schedule tasklet */
517 tasklet_schedule(&adapter->tasklet);
518
519 ret = IRQ_HANDLED;
520 }
521
522 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
523
524 return ret;
525}
526
527
528/* Netdevice operations */
529
530static int ks8842_open(struct net_device *netdev)
531{
532 struct ks8842_adapter *adapter = netdev_priv(netdev);
533 int err;
534
535 dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__);
536
537 /* reset the HW */
538 ks8842_reset_hw(adapter);
539
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700540 ks8842_write_mac_addr(adapter, netdev->dev_addr);
541
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000542 ks8842_update_link_status(netdev, adapter);
543
544 err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME,
545 adapter);
546 if (err) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000547 pr_err("Failed to request IRQ: %d: %d\n", adapter->irq, err);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000548 return err;
549 }
550
551 return 0;
552}
553
554static int ks8842_close(struct net_device *netdev)
555{
556 struct ks8842_adapter *adapter = netdev_priv(netdev);
557
558 dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__);
559
560 /* free the irq */
561 free_irq(adapter->irq, adapter);
562
563 /* disable the switch */
564 ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE);
565
566 return 0;
567}
568
Stephen Hemminger613573252009-08-31 19:50:58 +0000569static netdev_tx_t ks8842_xmit_frame(struct sk_buff *skb,
570 struct net_device *netdev)
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000571{
572 int ret;
573 struct ks8842_adapter *adapter = netdev_priv(netdev);
574
575 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
576
577 ret = ks8842_tx_frame(skb, netdev);
578
579 if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8)
580 netif_stop_queue(netdev);
581
582 return ret;
583}
584
585static int ks8842_set_mac(struct net_device *netdev, void *p)
586{
587 struct ks8842_adapter *adapter = netdev_priv(netdev);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000588 struct sockaddr *addr = p;
589 char *mac = (u8 *)addr->sa_data;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000590
591 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
592
593 if (!is_valid_ether_addr(addr->sa_data))
594 return -EADDRNOTAVAIL;
595
596 memcpy(netdev->dev_addr, mac, netdev->addr_len);
597
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700598 ks8842_write_mac_addr(adapter, mac);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000599 return 0;
600}
601
602static void ks8842_tx_timeout(struct net_device *netdev)
603{
604 struct ks8842_adapter *adapter = netdev_priv(netdev);
605 unsigned long flags;
606
607 dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__);
608
609 spin_lock_irqsave(&adapter->lock, flags);
610 /* disable interrupts */
611 ks8842_write16(adapter, 18, 0, REG_IER);
612 ks8842_write16(adapter, 18, 0xFFFF, REG_ISR);
613 spin_unlock_irqrestore(&adapter->lock, flags);
614
615 ks8842_reset_hw(adapter);
616
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700617 ks8842_write_mac_addr(adapter, netdev->dev_addr);
618
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000619 ks8842_update_link_status(netdev, adapter);
620}
621
622static const struct net_device_ops ks8842_netdev_ops = {
623 .ndo_open = ks8842_open,
624 .ndo_stop = ks8842_close,
625 .ndo_start_xmit = ks8842_xmit_frame,
626 .ndo_set_mac_address = ks8842_set_mac,
627 .ndo_tx_timeout = ks8842_tx_timeout,
628 .ndo_validate_addr = eth_validate_addr
629};
630
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700631static const struct ethtool_ops ks8842_ethtool_ops = {
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000632 .get_link = ethtool_op_get_link,
633};
634
635static int __devinit ks8842_probe(struct platform_device *pdev)
636{
637 int err = -ENOMEM;
638 struct resource *iomem;
639 struct net_device *netdev;
640 struct ks8842_adapter *adapter;
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700641 struct ks8842_platform_data *pdata = pdev->dev.platform_data;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000642 u16 id;
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700643 unsigned i;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000644
645 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
646 if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME))
647 goto err_mem_region;
648
649 netdev = alloc_etherdev(sizeof(struct ks8842_adapter));
650 if (!netdev)
651 goto err_alloc_etherdev;
652
653 SET_NETDEV_DEV(netdev, &pdev->dev);
654
655 adapter = netdev_priv(netdev);
656 adapter->hw_addr = ioremap(iomem->start, resource_size(iomem));
657 if (!adapter->hw_addr)
658 goto err_ioremap;
659
660 adapter->irq = platform_get_irq(pdev, 0);
661 if (adapter->irq < 0) {
662 err = adapter->irq;
663 goto err_get_irq;
664 }
665
666 adapter->pdev = pdev;
667
668 tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev);
669 spin_lock_init(&adapter->lock);
670
671 netdev->netdev_ops = &ks8842_netdev_ops;
672 netdev->ethtool_ops = &ks8842_ethtool_ops;
673
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700674 /* Check if a mac address was given */
675 i = netdev->addr_len;
676 if (pdata) {
677 for (i = 0; i < netdev->addr_len; i++)
678 if (pdata->macaddr[i] != 0)
679 break;
680
681 if (i < netdev->addr_len)
682 /* an address was passed, use it */
683 memcpy(netdev->dev_addr, pdata->macaddr,
684 netdev->addr_len);
685 }
686
687 if (i == netdev->addr_len) {
688 ks8842_read_mac_addr(adapter, netdev->dev_addr);
689
690 if (!is_valid_ether_addr(netdev->dev_addr))
691 random_ether_addr(netdev->dev_addr);
692 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000693
694 id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE);
695
696 strcpy(netdev->name, "eth%d");
697 err = register_netdev(netdev);
698 if (err)
699 goto err_register;
700
701 platform_set_drvdata(pdev, netdev);
702
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000703 pr_info("Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000704 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
705
706 return 0;
707
708err_register:
709err_get_irq:
710 iounmap(adapter->hw_addr);
711err_ioremap:
712 free_netdev(netdev);
713err_alloc_etherdev:
714 release_mem_region(iomem->start, resource_size(iomem));
715err_mem_region:
716 return err;
717}
718
719static int __devexit ks8842_remove(struct platform_device *pdev)
720{
721 struct net_device *netdev = platform_get_drvdata(pdev);
722 struct ks8842_adapter *adapter = netdev_priv(netdev);
723 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
724
725 unregister_netdev(netdev);
726 tasklet_kill(&adapter->tasklet);
727 iounmap(adapter->hw_addr);
728 free_netdev(netdev);
729 release_mem_region(iomem->start, resource_size(iomem));
730 platform_set_drvdata(pdev, NULL);
731 return 0;
732}
733
734
735static struct platform_driver ks8842_platform_driver = {
736 .driver = {
737 .name = DRV_NAME,
738 .owner = THIS_MODULE,
739 },
740 .probe = ks8842_probe,
741 .remove = ks8842_remove,
742};
743
744static int __init ks8842_init(void)
745{
746 return platform_driver_register(&ks8842_platform_driver);
747}
748
749static void __exit ks8842_exit(void)
750{
751 platform_driver_unregister(&ks8842_platform_driver);
752}
753
754module_init(ks8842_init);
755module_exit(ks8842_exit);
756
757MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver");
758MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
759MODULE_LICENSE("GPL v2");
760MODULE_ALIAS("platform:ks8842");
761