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