1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
60
61static const char version[] =
62"cs89x0.c:v1.02 11/26/96 Russell Nelson <nelson@crynwr.com>\n";
63
64#include <linux/module.h>
65
66
67
68
69
70
71
72
73
74
75#include <linux/kernel.h>
76#include <linux/types.h>
77#include <linux/fcntl.h>
78#include <linux/interrupt.h>
79#include <linux/ioport.h>
80#include <linux/in.h>
81#include <linux/string.h>
82#include <linux/nubus.h>
83#include <linux/errno.h>
84#include <linux/init.h>
85#include <linux/netdevice.h>
86#include <linux/platform_device.h>
87#include <linux/etherdevice.h>
88#include <linux/skbuff.h>
89#include <linux/delay.h>
90#include <linux/bitops.h>
91#include <linux/gfp.h>
92
93#include <asm/io.h>
94#include <asm/hwtest.h>
95#include <asm/macints.h>
96
97#include "cs89x0.h"
98
99static int debug = -1;
100module_param(debug, int, 0);
101MODULE_PARM_DESC(debug, "debug message level");
102
103
104struct net_local {
105 int msg_enable;
106 int chip_type;
107 char chip_revision;
108 int send_cmd;
109 int rx_mode;
110 int curr_rx_cfg;
111 int send_underrun;
112};
113
114
115static int net_open(struct net_device *dev);
116static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
117static irqreturn_t net_interrupt(int irq, void *dev_id);
118static void set_multicast_list(struct net_device *dev);
119static void net_rx(struct net_device *dev);
120static int net_close(struct net_device *dev);
121static struct net_device_stats *net_get_stats(struct net_device *dev);
122static int set_mac_address(struct net_device *dev, void *addr);
123
124
125static inline int
126readreg_io(struct net_device *dev, int portno)
127{
128 nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
129 return swab16(nubus_readw(dev->base_addr + DATA_PORT));
130}
131
132static inline void
133writereg_io(struct net_device *dev, int portno, int value)
134{
135 nubus_writew(swab16(portno), dev->base_addr + ADD_PORT);
136 nubus_writew(swab16(value), dev->base_addr + DATA_PORT);
137}
138
139
140static inline int
141readreg(struct net_device *dev, int portno)
142{
143 return swab16(nubus_readw(dev->mem_start + portno));
144}
145
146static inline void
147writereg(struct net_device *dev, int portno, int value)
148{
149 nubus_writew(swab16(value), dev->mem_start + portno);
150}
151
152static const struct net_device_ops mac89x0_netdev_ops = {
153 .ndo_open = net_open,
154 .ndo_stop = net_close,
155 .ndo_start_xmit = net_send_packet,
156 .ndo_get_stats = net_get_stats,
157 .ndo_set_rx_mode = set_multicast_list,
158 .ndo_set_mac_address = set_mac_address,
159 .ndo_validate_addr = eth_validate_addr,
160};
161
162
163
164static int mac89x0_device_probe(struct platform_device *pdev)
165{
166 struct net_device *dev;
167 struct net_local *lp;
168 int i, slot;
169 unsigned rev_type = 0;
170 unsigned long ioaddr;
171 unsigned short sig;
172 int err = -ENODEV;
173 struct nubus_rsrc *fres;
174
175 dev = alloc_etherdev(sizeof(struct net_local));
176 if (!dev)
177 return -ENOMEM;
178
179
180 slot = 0xE;
181
182 for_each_func_rsrc(fres)
183 if (fres->board->slot == slot)
184 goto out;
185
186
187
188 ioaddr = (unsigned long)
189 nubus_slot_addr(slot) | (((slot&0xf) << 20) + DEFAULTIOBASE);
190 {
191 int card_present;
192
193 card_present = (hwreg_present((void *)ioaddr + 4) &&
194 hwreg_present((void *)ioaddr + DATA_PORT));
195 if (!card_present)
196 goto out;
197 }
198
199 nubus_writew(0, ioaddr + ADD_PORT);
200 sig = nubus_readw(ioaddr + DATA_PORT);
201 if (sig != swab16(CHIP_EISA_ID_SIG))
202 goto out;
203
204 SET_NETDEV_DEV(dev, &pdev->dev);
205
206
207 lp = netdev_priv(dev);
208
209 lp->msg_enable = netif_msg_init(debug, 0);
210
211
212 dev->base_addr = ioaddr;
213 dev->mem_start = (unsigned long)
214 nubus_slot_addr(slot) | (((slot&0xf) << 20) + MMIOBASE);
215 dev->mem_end = dev->mem_start + 0x1000;
216
217
218 writereg_io(dev, PP_BusCTL, MEMORY_ON);
219
220
221 rev_type = readreg(dev, PRODUCT_ID_ADD);
222 lp->chip_type = rev_type &~ REVISON_BITS;
223 lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
224
225
226
227 lp->send_cmd = TX_AFTER_381;
228 if (lp->chip_type == CS8900 && lp->chip_revision >= 'F')
229 lp->send_cmd = TX_NOW;
230 if (lp->chip_type != CS8900 && lp->chip_revision >= 'C')
231 lp->send_cmd = TX_NOW;
232
233 netif_dbg(lp, drv, dev, "%s", version);
234
235 pr_info("cs89%c0%s rev %c found at %#8lx\n",
236 lp->chip_type == CS8900 ? '0' : '2',
237 lp->chip_type == CS8920M ? "M" : "",
238 lp->chip_revision, dev->base_addr);
239
240
241 if ((readreg(dev, PP_SelfST) & (EEPROM_PRESENT | EEPROM_OK)) == 0) {
242 pr_info("No EEPROM, giving up now.\n");
243 goto out1;
244 } else {
245 for (i = 0; i < ETH_ALEN; i += 2) {
246
247 unsigned short s = readreg(dev, PP_IA + i);
248 dev->dev_addr[i] = s >> 8;
249 dev->dev_addr[i+1] = s & 0xff;
250 }
251 }
252
253 dev->irq = SLOT2IRQ(slot);
254
255
256
257 pr_info("MAC %pM, IRQ %d\n", dev->dev_addr, dev->irq);
258
259 dev->netdev_ops = &mac89x0_netdev_ops;
260
261 err = register_netdev(dev);
262 if (err)
263 goto out1;
264
265 platform_set_drvdata(pdev, dev);
266 return 0;
267out1:
268 nubus_writew(0, dev->base_addr + ADD_PORT);
269out:
270 free_netdev(dev);
271 return err;
272}
273
274
275
276
277
278
279
280
281static int
282net_open(struct net_device *dev)
283{
284 struct net_local *lp = netdev_priv(dev);
285 int i;
286
287
288 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) & ~ENABLE_IRQ);
289
290
291 if (request_irq(dev->irq, net_interrupt, 0, "cs89x0", dev))
292 return -EAGAIN;
293
294
295 if (lp->chip_type == CS8900)
296 writereg(dev, PP_CS8900_ISAINT, 0);
297 else
298 writereg(dev, PP_CS8920_ISAINT, 0);
299
300
301 for (i=0; i < ETH_ALEN/2; i++)
302 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
303
304
305 writereg(dev, PP_LineCTL, readreg(dev, PP_LineCTL) | SERIAL_RX_ON | SERIAL_TX_ON);
306
307
308 lp->rx_mode = 0;
309 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT);
310
311 lp->curr_rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL;
312
313 writereg(dev, PP_RxCFG, lp->curr_rx_cfg);
314
315 writereg(dev, PP_TxCFG, TX_LOST_CRS_ENBL | TX_SQE_ERROR_ENBL | TX_OK_ENBL |
316 TX_LATE_COL_ENBL | TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
317
318 writereg(dev, PP_BufCFG, READY_FOR_TX_ENBL | RX_MISS_COUNT_OVRFLOW_ENBL |
319 TX_COL_COUNT_OVRFLOW_ENBL | TX_UNDERRUN_ENBL);
320
321
322 writereg(dev, PP_BusCTL, readreg(dev, PP_BusCTL) | ENABLE_IRQ);
323 netif_start_queue(dev);
324 return 0;
325}
326
327static int
328net_send_packet(struct sk_buff *skb, struct net_device *dev)
329{
330 struct net_local *lp = netdev_priv(dev);
331 unsigned long flags;
332
333 netif_dbg(lp, tx_queued, dev, "sent %d byte packet of type %x\n",
334 skb->len, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
335 skb->data[ETH_ALEN + ETH_ALEN + 1]);
336
337
338
339
340 local_irq_save(flags);
341 netif_stop_queue(dev);
342
343
344 writereg(dev, PP_TxCMD, lp->send_cmd);
345 writereg(dev, PP_TxLength, skb->len);
346
347
348 if ((readreg(dev, PP_BusST) & READY_FOR_TX_NOW) == 0) {
349
350
351 local_irq_restore(flags);
352 return NETDEV_TX_BUSY;
353 }
354
355
356 skb_copy_from_linear_data(skb, (void *)(dev->mem_start + PP_TxFrame),
357 skb->len+1);
358
359 local_irq_restore(flags);
360 dev_kfree_skb (skb);
361
362 return NETDEV_TX_OK;
363}
364
365
366
367static irqreturn_t net_interrupt(int irq, void *dev_id)
368{
369 struct net_device *dev = dev_id;
370 struct net_local *lp;
371 int ioaddr, status;
372
373 ioaddr = dev->base_addr;
374 lp = netdev_priv(dev);
375
376
377
378
379
380
381
382
383 while ((status = swab16(nubus_readw(dev->base_addr + ISQ_PORT)))) {
384 netif_dbg(lp, intr, dev, "status=%04x\n", status);
385 switch(status & ISQ_EVENT_MASK) {
386 case ISQ_RECEIVER_EVENT:
387
388 net_rx(dev);
389 break;
390 case ISQ_TRANSMITTER_EVENT:
391 dev->stats.tx_packets++;
392 netif_wake_queue(dev);
393 if ((status & TX_OK) == 0)
394 dev->stats.tx_errors++;
395 if (status & TX_LOST_CRS)
396 dev->stats.tx_carrier_errors++;
397 if (status & TX_SQE_ERROR)
398 dev->stats.tx_heartbeat_errors++;
399 if (status & TX_LATE_COL)
400 dev->stats.tx_window_errors++;
401 if (status & TX_16_COL)
402 dev->stats.tx_aborted_errors++;
403 break;
404 case ISQ_BUFFER_EVENT:
405 if (status & READY_FOR_TX) {
406
407
408
409
410
411 netif_wake_queue(dev);
412 }
413 if (status & TX_UNDERRUN) {
414 netif_dbg(lp, tx_err, dev, "transmit underrun\n");
415 lp->send_underrun++;
416 if (lp->send_underrun == 3) lp->send_cmd = TX_AFTER_381;
417 else if (lp->send_underrun == 6) lp->send_cmd = TX_AFTER_ALL;
418 }
419 break;
420 case ISQ_RX_MISS_EVENT:
421 dev->stats.rx_missed_errors += (status >> 6);
422 break;
423 case ISQ_TX_COL_EVENT:
424 dev->stats.collisions += (status >> 6);
425 break;
426 }
427 }
428 return IRQ_HANDLED;
429}
430
431
432static void
433net_rx(struct net_device *dev)
434{
435 struct net_local *lp = netdev_priv(dev);
436 struct sk_buff *skb;
437 int status, length;
438
439 status = readreg(dev, PP_RxStatus);
440 if ((status & RX_OK) == 0) {
441 dev->stats.rx_errors++;
442 if (status & RX_RUNT)
443 dev->stats.rx_length_errors++;
444 if (status & RX_EXTRA_DATA)
445 dev->stats.rx_length_errors++;
446 if ((status & RX_CRC_ERROR) &&
447 !(status & (RX_EXTRA_DATA|RX_RUNT)))
448
449 dev->stats.rx_crc_errors++;
450 if (status & RX_DRIBBLE)
451 dev->stats.rx_frame_errors++;
452 return;
453 }
454
455 length = readreg(dev, PP_RxLength);
456
457 skb = alloc_skb(length, GFP_ATOMIC);
458 if (skb == NULL) {
459 dev->stats.rx_dropped++;
460 return;
461 }
462 skb_put(skb, length);
463
464 skb_copy_to_linear_data(skb, (void *)(dev->mem_start + PP_RxFrame),
465 length);
466
467 netif_dbg(lp, rx_status, dev, "received %d byte packet of type %x\n",
468 length, skb->data[ETH_ALEN + ETH_ALEN] << 8 |
469 skb->data[ETH_ALEN + ETH_ALEN + 1]);
470
471 skb->protocol=eth_type_trans(skb,dev);
472 netif_rx(skb);
473 dev->stats.rx_packets++;
474 dev->stats.rx_bytes += length;
475}
476
477
478static int
479net_close(struct net_device *dev)
480{
481
482 writereg(dev, PP_RxCFG, 0);
483 writereg(dev, PP_TxCFG, 0);
484 writereg(dev, PP_BufCFG, 0);
485 writereg(dev, PP_BusCTL, 0);
486
487 netif_stop_queue(dev);
488
489 free_irq(dev->irq, dev);
490
491
492
493 return 0;
494
495}
496
497
498
499static struct net_device_stats *
500net_get_stats(struct net_device *dev)
501{
502 unsigned long flags;
503
504 local_irq_save(flags);
505
506 dev->stats.rx_missed_errors += (readreg(dev, PP_RxMiss) >> 6);
507 dev->stats.collisions += (readreg(dev, PP_TxCol) >> 6);
508 local_irq_restore(flags);
509
510 return &dev->stats;
511}
512
513static void set_multicast_list(struct net_device *dev)
514{
515 struct net_local *lp = netdev_priv(dev);
516
517 if(dev->flags&IFF_PROMISC)
518 {
519 lp->rx_mode = RX_ALL_ACCEPT;
520 } else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
521
522
523 lp->rx_mode = RX_MULTCAST_ACCEPT;
524 }
525 else
526 lp->rx_mode = 0;
527
528 writereg(dev, PP_RxCTL, DEF_RX_ACCEPT | lp->rx_mode);
529
530
531 writereg(dev, PP_RxCFG, lp->curr_rx_cfg |
532 (lp->rx_mode == RX_ALL_ACCEPT? (RX_CRC_ERROR_ENBL|RX_RUNT_ENBL|RX_EXTRA_DATA_ENBL) : 0));
533}
534
535
536static int set_mac_address(struct net_device *dev, void *addr)
537{
538 struct sockaddr *saddr = addr;
539 int i;
540
541 if (!is_valid_ether_addr(saddr->sa_data))
542 return -EADDRNOTAVAIL;
543
544 memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
545 netdev_info(dev, "Setting MAC address to %pM\n", dev->dev_addr);
546
547
548 for (i=0; i < ETH_ALEN/2; i++)
549 writereg(dev, PP_IA+i*2, dev->dev_addr[i*2] | (dev->dev_addr[i*2+1] << 8));
550
551 return 0;
552}
553
554MODULE_LICENSE("GPL");
555
556static int mac89x0_device_remove(struct platform_device *pdev)
557{
558 struct net_device *dev = platform_get_drvdata(pdev);
559
560 unregister_netdev(dev);
561 nubus_writew(0, dev->base_addr + ADD_PORT);
562 free_netdev(dev);
563 return 0;
564}
565
566static struct platform_driver mac89x0_platform_driver = {
567 .probe = mac89x0_device_probe,
568 .remove = mac89x0_device_remove,
569 .driver = {
570 .name = "mac89x0",
571 },
572};
573
574module_platform_driver(mac89x0_platform_driver);
575