00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <linux/kernel.h>
00017 #include <linux/netdevice.h>
00018 #include <linux/etherdevice.h>
00019 #include <linux/delay.h>
00020 #include <linux/string.h>
00021 #include <linux/timer.h>
00022 #include <asm/io.h>
00023 #include <asm/pgtable.h>
00024 #include <asm/irq.h>
00025 #include <asm/macintosh.h>
00026 #include <asm/macints.h>
00027 #include <asm/mac_psc.h>
00028 #include "mace.h"
00029
00030 #define N_RX_RING 8
00031 #define N_TX_RING 2
00032 #define MAX_TX_ACTIVE 1
00033 #define NCMDS_TX 1
00034 #define RX_BUFLEN (ETH_FRAME_LEN + 8)
00035 #define TX_TIMEOUT HZ
00036
00037
00038 #define TX_DMA_ERR 0x80
00039
00040
00041
00042 #define MACE_BASE (void *)(0x50F1C000)
00043 #define MACE_PROM (void *)(0x50F08001)
00044
00045 struct mace68k_data
00046 {
00047 volatile struct mace *mace;
00048 volatile unsigned char *tx_ring;
00049 volatile unsigned char *rx_ring;
00050 int dma_intr;
00051 unsigned char maccc;
00052 struct net_device_stats stats;
00053 struct timer_list tx_timeout;
00054 int timeout_active;
00055 int rx_slot, rx_done;
00056 int tx_slot, tx_count;
00057 };
00058
00059 struct mace_frame
00060 {
00061 u16 len;
00062 u16 status;
00063 u16 rntpc;
00064 u16 rcvcc;
00065 u32 pad1;
00066 u32 pad2;
00067 u8 data[1];
00068
00069 };
00070
00071 #define PRIV_BYTES sizeof(struct mace68k_data)
00072
00073 static int mace68k_open(struct device *dev);
00074 static int mace68k_close(struct device *dev);
00075 static int mace68k_xmit_start(struct sk_buff *skb, struct device *dev);
00076 static struct net_device_stats *mace68k_stats(struct device *dev);
00077 static void mace68k_set_multicast(struct device *dev);
00078 static void mace68k_reset(struct device *dev);
00079 static int mace68k_set_address(struct device *dev, void *addr);
00080 static void mace68k_interrupt(int irq, void *dev_id, struct pt_regs *regs);
00081 static void mace68k_dma_intr(int irq, void *dev_id, struct pt_regs *regs);
00082 static void mace68k_set_timeout(struct device *dev);
00083 static void mace68k_tx_timeout(unsigned long data);
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 static void psc_load_rxdma_base(int set, void *base)
00104 {
00105 psc_write_word(PSC_ENETRD_CMD + set, 0x0100);
00106 psc_write_long(PSC_ENETRD_ADDR + set, (u32)base);
00107 psc_write_long(PSC_ENETRD_LEN + set, N_RX_RING);
00108 psc_write_word(PSC_ENETRD_CMD + set, 0x9800);
00109 }
00110
00111
00112
00113
00114
00115 static void mace68k_rxdma_reset(struct device *dev)
00116 {
00117 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00118 volatile struct mace *mace = mp->mace;
00119 u8 mcc = mace->maccc;
00120
00121
00122
00123
00124
00125 mcc&=~ENRCV;
00126 mace->maccc=mcc;
00127
00128
00129
00130
00131
00132 psc_write_word(PSC_ENETRD_CTL, 0x8800);
00133 psc_load_rxdma_base(0x0, (void *)virt_to_bus(mp->rx_ring));
00134 psc_write_word(PSC_ENETRD_CTL, 0x0400);
00135
00136 psc_write_word(PSC_ENETRD_CTL, 0x8800);
00137 psc_load_rxdma_base(0x10, (void *)virt_to_bus(mp->rx_ring));
00138 psc_write_word(PSC_ENETRD_CTL, 0x0400);
00139
00140 mace->maccc=mcc|ENRCV;
00141
00142 psc_write_word(PSC_ENETRD_CTL, 0x9800);
00143 psc_write_word(PSC_ENETRD_CTL+0x10, 0x9800);
00144 }
00145
00146
00147
00148
00149
00150 static void mace68k_txdma_reset(struct device *dev)
00151 {
00152 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00153 volatile struct mace *mace = mp->mace;
00154 u8 mcc = mace->maccc;
00155
00156 psc_write_word(PSC_ENETWR_CTL,0x8800);
00157
00158 mace->maccc = mcc&~ENXMT;
00159 psc_write_word(PSC_ENETWR_CTL,0x400);
00160 mace->maccc = mcc;
00161 }
00162
00163
00164
00165
00166
00167 static void mace68k_dma_off(struct device *dev)
00168 {
00169 psc_write_word(PSC_ENETRD_CTL, 0x8800);
00170 psc_write_word(PSC_ENETRD_CTL, 0x1000);
00171 psc_write_word(PSC_ENETRD_CMD, 0x1100);
00172 psc_write_word(PSC_ENETRD_CMD+0x10, 0x1100);
00173
00174 psc_write_word(PSC_ENETWR_CTL, 0x8800);
00175 psc_write_word(PSC_ENETWR_CTL, 0x1000);
00176 psc_write_word(PSC_ENETWR_CMD, 0x1100);
00177 psc_write_word(PSC_ENETWR_CMD+0x10, 0x1100);
00178 }
00179
00180
00181
00182 static int
00183 bitrev(int b)
00184 {
00185 int d = 0, i;
00186
00187 for (i = 0; i < 8; ++i, b >>= 1)
00188 d = (d << 1) | (b & 1);
00189 return d;
00190 }
00191
00192
00193
00194
00195
00196
00197 int mace68k_probe(struct device *unused)
00198 {
00199 int j;
00200 static int once=0;
00201 struct mace68k_data *mp;
00202 unsigned char *addr;
00203 struct device *dev;
00204 unsigned char checksum = 0;
00205
00206
00207
00208
00209
00210 if (once) return -ENODEV;
00211
00212 once = 1;
00213
00214 if (macintosh_config->ether_type != MAC_ETHER_MACE) return -ENODEV;
00215
00216 printk("MACE ethernet should be present ");
00217
00218 dev = init_etherdev(0, PRIV_BYTES);
00219 if(dev==NULL)
00220 {
00221 printk("no free memory.\n");
00222 return -ENOMEM;
00223 }
00224 mp = (struct mace68k_data *) dev->priv;
00225 dev->base_addr = (u32)MACE_BASE;
00226 mp->mace = (volatile struct mace *) MACE_BASE;
00227
00228 printk("at 0x%p", mp->mace);
00229
00230
00231
00232
00233
00234 mp->rx_ring=(void *)__get_free_pages(GFP_KERNEL, 2);
00235 mp->tx_ring=(void *)__get_free_page(GFP_KERNEL);
00236
00237 printk(".");
00238
00239 if(mp->tx_ring==NULL || mp->rx_ring==NULL)
00240 {
00241 if(mp->tx_ring)
00242 free_page((u32)mp->tx_ring);
00243
00244
00245 printk("\nNo memory for ring buffers.\n");
00246 return -ENOMEM;
00247 }
00248
00249
00250
00251
00252 printk(".");
00253 kernel_set_cachemode((void *)mp->rx_ring, 16384, IOMAP_NOCACHE_NONSER);
00254
00255 printk(".");
00256
00257 kernel_set_cachemode((void *)mp->tx_ring, 4096, IOMAP_WRITETHROUGH);
00258
00259 printk(" Ok\n");
00260 dev->irq = IRQ_MAC_MACE;
00261 printk(KERN_INFO "%s: MACE at", dev->name);
00262
00263
00264
00265
00266
00267
00268
00269
00270 addr = (void *)MACE_PROM;
00271
00272 for (j = 0; j < 6; ++j)
00273 {
00274 u8 v=bitrev(addr[j<<4]);
00275 checksum^=v;
00276 dev->dev_addr[j] = v;
00277 printk("%c%.2x", (j ? ':' : ' '), dev->dev_addr[j]);
00278 }
00279 for (; j < 8; ++j)
00280 {
00281 checksum^=bitrev(addr[j<<4]);
00282 }
00283
00284 if(checksum!=0xFF)
00285 {
00286 printk(" (invalid checksum)\n");
00287 return -ENODEV;
00288 }
00289 printk("\n");
00290
00291 memset(&mp->stats, 0, sizeof(mp->stats));
00292 init_timer(&mp->tx_timeout);
00293 mp->timeout_active = 0;
00294
00295 dev->open = mace68k_open;
00296 dev->stop = mace68k_close;
00297 dev->hard_start_xmit = mace68k_xmit_start;
00298 dev->get_stats = mace68k_stats;
00299 dev->set_multicast_list = mace68k_set_multicast;
00300 dev->set_mac_address = mace68k_set_address;
00301
00302 ether_setup(dev);
00303
00304 mp = (struct mace68k_data *) dev->priv;
00305 mp->maccc = ENXMT | ENRCV;
00306 mp->dma_intr = IRQ_MAC_MACE_DMA;
00307
00308 psc_write_word(PSC_ENETWR_CTL, 0x9000);
00309 psc_write_word(PSC_ENETRD_CTL, 0x9000);
00310 psc_write_word(PSC_ENETWR_CTL, 0x0400);
00311 psc_write_word(PSC_ENETRD_CTL, 0x0400);
00312
00313 mace68k_dma_off(dev);
00314
00315 return 0;
00316 }
00317
00318
00319
00320
00321
00322 static void mace68k_reset(struct device *dev)
00323 {
00324 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00325 volatile struct mace *mb = mp->mace;
00326 int i;
00327
00328
00329 mb->biucc = SWRST;
00330 udelay(100);
00331
00332 mb->biucc = XMTSP_64;
00333 mb->imr = 0xff;
00334 i = mb->ir;
00335 mb->maccc = 0;
00336 mb->utr = RTRD;
00337 mb->fifocc = RCVFW_64;
00338 mb->xmtfc = AUTO_PAD_XMIT;
00339
00340
00341
00342 mb->iac = ADDRCHG | PHYADDR;
00343
00344 while ((mb->iac & ADDRCHG) != 0);
00345
00346 for (i = 0; i < 6; ++i)
00347 mb->padr = dev->dev_addr[i];
00348
00349
00350 mb->iac = ADDRCHG | LOGADDR;
00351
00352 while ((mb->iac & ADDRCHG) != 0);
00353
00354 for (i = 0; i < 8; ++i)
00355 mb->ladrf = 0;
00356
00357 mb->plscc = PORTSEL_GPSI + ENPLSIO;
00358 }
00359
00360
00361
00362
00363
00364 static int mace68k_set_address(struct device *dev, void *addr)
00365 {
00366 unsigned char *p = addr;
00367 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00368 volatile struct mace *mb = mp->mace;
00369 int i;
00370 unsigned long flags;
00371
00372 save_flags(flags);
00373 cli();
00374
00375
00376 mb->iac = ADDRCHG | PHYADDR;
00377 while ((mb->iac & ADDRCHG) != 0);
00378
00379 for (i = 0; i < 6; ++i)
00380 mb->padr = dev->dev_addr[i] = p[i];
00381
00382 mb->maccc = mp->maccc;
00383 restore_flags(flags);
00384 return 0;
00385 }
00386
00387
00388
00389
00390
00391
00392 static int mace68k_open(struct device *dev)
00393 {
00394 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00395 volatile struct mace *mb = mp->mace;
00396
00397
00398 mace68k_reset(dev);
00399
00400 mp->rx_done = 0;
00401 mace68k_rxdma_reset(dev);
00402
00403
00404
00405
00406
00407 if (request_irq(dev->irq, mace68k_interrupt, 0, "68K MACE", dev))
00408 {
00409 printk(KERN_ERR "MACE: can't get irq %d\n", dev->irq);
00410 return -EAGAIN;
00411 }
00412
00413
00414
00415
00416
00417 if (request_irq(IRQ_MAC_MACE_DMA, mace68k_dma_intr, 0, "68K MACE DMA",
00418 dev))
00419 {
00420 printk(KERN_ERR "MACE: can't get irq %d\n", IRQ_MAC_MACE_DMA);
00421 return -EAGAIN;
00422 }
00423
00424
00425
00426 mp->tx_slot = 0;
00427 mp->tx_count = 1;
00428 mace68k_txdma_reset(dev);
00429
00430
00431 mb->maccc = mp->maccc;
00432
00433 mb->imr = RCVINT;
00434 return 0;
00435 }
00436
00437
00438
00439
00440
00441 static int mace68k_close(struct device *dev)
00442 {
00443 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00444 volatile struct mace *mb = mp->mace;
00445
00446
00447 mb->maccc = 0;
00448 mb->imr = 0xff;
00449
00450
00451
00452 mace68k_dma_off(dev);
00453
00454 free_irq(dev->irq, dev);
00455 free_irq(IRQ_MAC_MACE_DMA, dev);
00456 return 0;
00457 }
00458
00459 static inline void mace68k_set_timeout(struct device *dev)
00460 {
00461 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00462 unsigned long flags;
00463
00464 save_flags(flags);
00465 cli();
00466 if (mp->timeout_active)
00467 del_timer(&mp->tx_timeout);
00468 mp->tx_timeout.expires = jiffies + TX_TIMEOUT;
00469 mp->tx_timeout.function = mace68k_tx_timeout;
00470 mp->tx_timeout.data = (unsigned long) dev;
00471 add_timer(&mp->tx_timeout);
00472 mp->timeout_active = 1;
00473 restore_flags(flags);
00474 }
00475
00476
00477
00478
00479
00480 static int mace68k_xmit_start(struct sk_buff *skb, struct device *dev)
00481 {
00482 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00483
00484
00485
00486 if(mp->tx_count == 0)
00487 {
00488 dev->tbusy=1;
00489 return 1;
00490 }
00491 mp->tx_count--;
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 memcpy((void *)mp->tx_ring, skb, skb->len);
00502 psc_write_long(PSC_ENETWR_ADDR + mp->tx_slot, virt_to_bus(mp->tx_ring));
00503 psc_write_long(PSC_ENETWR_LEN + mp->tx_slot, skb->len);
00504 psc_write_word(PSC_ENETWR_CMD + mp->tx_slot, 0x9800);
00505 mp->stats.tx_packets++;
00506 mp->stats.tx_bytes+=skb->len;
00507 dev_kfree_skb(skb);
00508 return 0;
00509 }
00510
00511 static struct net_device_stats *mace68k_stats(struct device *dev)
00512 {
00513 struct mace68k_data *p = (struct mace68k_data *) dev->priv;
00514 return &p->stats;
00515 }
00516
00517
00518
00519
00520 #define CRC_POLY 0xedb88320
00521
00522 static void mace68k_set_multicast(struct device *dev)
00523 {
00524 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00525 volatile struct mace *mb = mp->mace;
00526 int i, j, k, b;
00527 unsigned long crc;
00528
00529 mp->maccc &= ~PROM;
00530 if (dev->flags & IFF_PROMISC)
00531 {
00532 mp->maccc |= PROM;
00533 } else
00534 {
00535 unsigned char multicast_filter[8];
00536 struct dev_mc_list *dmi = dev->mc_list;
00537
00538 if (dev->flags & IFF_ALLMULTI)
00539 {
00540 for (i = 0; i < 8; i++)
00541 multicast_filter[i] = 0xff;
00542 } else
00543 {
00544 for (i = 0; i < 8; i++)
00545 multicast_filter[i] = 0;
00546 for (i = 0; i < dev->mc_count; i++)
00547 {
00548 crc = ~0;
00549 for (j = 0; j < 6; ++j)
00550 {
00551 b = dmi->dmi_addr[j];
00552 for (k = 0; k < 8; ++k)
00553 {
00554 if ((crc ^ b) & 1)
00555 crc = (crc >> 1) ^ CRC_POLY;
00556 else
00557 crc >>= 1;
00558 b >>= 1;
00559 }
00560 }
00561 j = crc >> 26;
00562 multicast_filter[j >> 3] |= 1 << (j & 7);
00563 dmi = dmi->next;
00564 }
00565 }
00566 #if 0
00567 printk("Multicast filter :");
00568 for (i = 0; i < 8; i++)
00569 printk("%02x ", multicast_filter[i]);
00570 printk("\n");
00571 #endif
00572
00573 mb->iac = ADDRCHG | LOGADDR;
00574 while ((mb->iac & ADDRCHG) != 0);
00575
00576 for (i = 0; i < 8; ++i)
00577 mb->ladrf = multicast_filter[i];
00578 }
00579
00580 mb->maccc = mp->maccc;
00581 }
00582
00583
00584
00585
00586
00587
00588 static void mace68k_handle_misc_intrs(struct mace68k_data *mp, int intr)
00589 {
00590 volatile struct mace *mb = mp->mace;
00591 static int mace68k_babbles, mace68k_jabbers;
00592
00593 if (intr & MPCO)
00594 mp->stats.rx_missed_errors += 256;
00595 mp->stats.rx_missed_errors += mb->mpc;
00596 if (intr & RNTPCO)
00597 mp->stats.rx_length_errors += 256;
00598 mp->stats.rx_length_errors += mb->rntpc;
00599 if (intr & CERR)
00600 ++mp->stats.tx_heartbeat_errors;
00601 if (intr & BABBLE)
00602 if (mace68k_babbles++ < 4)
00603 printk(KERN_DEBUG "mace: babbling transmitter\n");
00604 if (intr & JABBER)
00605 if (mace68k_jabbers++ < 4)
00606 printk(KERN_DEBUG "mace: jabbering transceiver\n");
00607 }
00608
00609
00610
00611
00612
00613
00614 static void mace68k_xmit_error(struct device *dev)
00615 {
00616 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00617 volatile struct mace *mb = mp->mace;
00618 u8 xmtfs, xmtrc;
00619
00620 xmtfs = mb->xmtfs;
00621 xmtrc = mb->xmtrc;
00622
00623 if(xmtfs & XMTSV)
00624 {
00625 if(xmtfs & UFLO)
00626 {
00627 printk("%s: DMA underrun.\n", dev->name);
00628 mp->stats.tx_errors++;
00629 mp->stats.tx_fifo_errors++;
00630 mace68k_reset(dev);
00631 }
00632 if(xmtfs & RTRY)
00633 mp->stats.collisions++;
00634 }
00635 mark_bh(NET_BH);
00636 }
00637
00638
00639
00640
00641
00642 static void mace68k_recv_interrupt(struct device *dev)
00643 {
00644
00645
00646 }
00647
00648
00649
00650
00651
00652 static void mace68k_interrupt(int irq, void *dev_id, struct pt_regs *regs)
00653 {
00654 struct device *dev = (struct device *) dev_id;
00655 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00656 volatile struct mace *mb = mp->mace;
00657 u8 ir;
00658
00659 ir = mb->ir;
00660 mace68k_handle_misc_intrs(mp, ir);
00661
00662 if(ir&XMTINT)
00663 mace68k_xmit_error(dev);
00664 if(ir&RCVINT)
00665 mace68k_recv_interrupt(dev);
00666 }
00667
00668 static void mace68k_tx_timeout(unsigned long data)
00669 {
00670
00671
00672
00673 }
00674
00675
00676
00677
00678
00679 static void mace_dma_rx_frame(struct device *dev, struct mace_frame *mf)
00680 {
00681 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00682 struct sk_buff *skb;
00683
00684 if(mf->status&RS_OFLO)
00685 {
00686 printk("%s: fifo overflow.\n", dev->name);
00687 mp->stats.rx_errors++;
00688 mp->stats.rx_fifo_errors++;
00689 }
00690 if(mf->status&(RS_CLSN|RS_FRAMERR|RS_FCSERR))
00691 mp->stats.rx_errors++;
00692
00693 if(mf->status&RS_CLSN)
00694 mp->stats.collisions++;
00695 if(mf->status&RS_FRAMERR)
00696 mp->stats.rx_frame_errors++;
00697 if(mf->status&RS_FCSERR)
00698 mp->stats.rx_crc_errors++;
00699
00700 skb = dev_alloc_skb(mf->len+2);
00701 if(skb==NULL)
00702 {
00703 mp->stats.rx_dropped++;
00704 return;
00705 }
00706 skb_reserve(skb,2);
00707 memcpy(skb_put(skb, mf->len), mf->data, mf->len);
00708
00709 skb->protocol = eth_type_trans(skb, dev);
00710 netif_rx(skb);
00711 mp->stats.rx_packets++;
00712 mp->stats.rx_bytes+=mf->len;
00713 }
00714
00715
00716
00717
00718
00719 static void mace68k_dma_intr(int irq, void *dev_id, struct pt_regs *regs)
00720 {
00721 struct device *dev = (struct device *) dev_id;
00722 struct mace68k_data *mp = (struct mace68k_data *) dev->priv;
00723
00724 u32 psc_status;
00725
00726
00727
00728 while((psc_status=psc_read_long(0x0804))!=psc_read_long(0x0804));
00729
00730
00731
00732
00733
00734 if(psc_status&0x60000000)
00735 {
00736
00737
00738
00739
00740 u16 psc_status = psc_read_word(PSC_ENETRD_CTL);
00741
00742 if(psc_status&0x2000)
00743 {
00744 mace68k_rxdma_reset(dev);
00745 mp->rx_done = 0;
00746 }
00747
00748 else if(psc_status&0x100)
00749 {
00750 int left;
00751
00752 psc_write_word(PSC_ENETRD_CMD+mp->rx_slot, 0x1100);
00753 left=psc_read_long(PSC_ENETRD_LEN+mp->rx_slot);
00754
00755
00756 while(mp->rx_done < left)
00757 {
00758 struct mace_frame *mf=((struct mace_frame *)
00759 mp->rx_ring)+mp->rx_done++;
00760 mace_dma_rx_frame(dev, mf);
00761 }
00762
00763 if(left == 0)
00764 {
00765 psc_load_rxdma_base(mp->rx_slot,
00766 (void *)virt_to_phys(mp->rx_ring));
00767 mp->rx_slot^=16;
00768 mp->rx_done = 0;
00769 }
00770 else
00771 {
00772 psc_write_word(PSC_ENETRD_CMD+mp->rx_slot,
00773 0x9800);
00774 }
00775
00776 }
00777
00778
00779
00780
00781
00782 psc_status = psc_read_word(PSC_ENETWR_CTL);
00783 if(psc_status&0x2000) {
00784 mace68k_txdma_reset(dev);
00785 } else if(psc_status&0x0100) {
00786 psc_write_word(PSC_ENETWR_CMD+mp->tx_slot, 0x100);
00787 mp->tx_slot^=16;
00788 mp->tx_count++;
00789 dev->tbusy = 0;
00790 mark_bh(NET_BH);
00791 }
00792 }
00793 }