blob: 9a650d1c1bdd1da50a99d940a269fc30a48155dc [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Network device driver for the BMAC ethernet controller on
4 * Apple Powermacs. Assumes it's under a DBDMA controller.
5 *
6 * Copyright (C) 1998 Randy Gobbel.
7 *
8 * May 1999, Al Viro: proper release of /proc/net/bmac entry, switched to
9 * dynamic procfs inode.
10 */
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000011#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/string.h>
18#include <linux/timer.h>
19#include <linux/proc_fs.h>
20#include <linux/init.h>
21#include <linux/spinlock.h>
22#include <linux/crc32.h>
Krzysztof Kozlowski5d258b42018-07-17 18:05:39 +020023#include <linux/crc32poly.h>
Akinobu Mitabc63eb92006-12-19 13:09:08 -080024#include <linux/bitrev.h>
Olaf Heringced13332007-08-25 20:32:59 +020025#include <linux/ethtool.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070027#include <linux/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/prom.h>
29#include <asm/dbdma.h>
30#include <asm/io.h>
31#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/machdep.h>
33#include <asm/pmac_feature.h>
34#include <asm/macio.h>
35#include <asm/irq.h>
36
37#include "bmac.h"
38
39#define trunc_page(x) ((void *)(((unsigned long)(x)) & ~((unsigned long)(PAGE_SIZE - 1))))
40#define round_page(x) trunc_page(((unsigned long)(x)) + ((unsigned long)(PAGE_SIZE - 1)))
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* switch to use multicast code lifted from sunhme driver */
43#define SUNHME_MULTICAST
44
45#define N_RX_RING 64
46#define N_TX_RING 32
47#define MAX_TX_ACTIVE 1
48#define ETHERCRC 4
49#define ETHERMINPACKET 64
50#define ETHERMTU 1500
51#define RX_BUFLEN (ETHERMTU + 14 + ETHERCRC + 2)
52#define TX_TIMEOUT HZ /* 1 second */
53
54/* Bits in transmit DMA status */
55#define TX_DMA_ERR 0x80
56
57#define XXDEBUG(args)
58
59struct bmac_data {
60 /* volatile struct bmac *bmac; */
61 struct sk_buff_head *queue;
62 volatile struct dbdma_regs __iomem *tx_dma;
63 int tx_dma_intr;
64 volatile struct dbdma_regs __iomem *rx_dma;
65 int rx_dma_intr;
66 volatile struct dbdma_cmd *tx_cmds; /* xmit dma command list */
67 volatile struct dbdma_cmd *rx_cmds; /* recv dma command list */
68 struct macio_dev *mdev;
69 int is_bmac_plus;
70 struct sk_buff *rx_bufs[N_RX_RING];
71 int rx_fill;
72 int rx_empty;
73 struct sk_buff *tx_bufs[N_TX_RING];
74 int tx_fill;
75 int tx_empty;
76 unsigned char tx_fullup;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct timer_list tx_timeout;
78 int timeout_active;
79 int sleeping;
80 int opened;
81 unsigned short hash_use_count[64];
82 unsigned short hash_table_mask[4];
83 spinlock_t lock;
84};
85
86#if 0 /* Move that to ethtool */
87
88typedef struct bmac_reg_entry {
89 char *name;
90 unsigned short reg_offset;
91} bmac_reg_entry_t;
92
93#define N_REG_ENTRIES 31
94
95static bmac_reg_entry_t reg_entries[N_REG_ENTRIES] = {
96 {"MEMADD", MEMADD},
97 {"MEMDATAHI", MEMDATAHI},
98 {"MEMDATALO", MEMDATALO},
99 {"TXPNTR", TXPNTR},
100 {"RXPNTR", RXPNTR},
101 {"IPG1", IPG1},
102 {"IPG2", IPG2},
103 {"ALIMIT", ALIMIT},
104 {"SLOT", SLOT},
105 {"PALEN", PALEN},
106 {"PAPAT", PAPAT},
107 {"TXSFD", TXSFD},
108 {"JAM", JAM},
109 {"TXCFG", TXCFG},
110 {"TXMAX", TXMAX},
111 {"TXMIN", TXMIN},
112 {"PAREG", PAREG},
113 {"DCNT", DCNT},
114 {"NCCNT", NCCNT},
115 {"NTCNT", NTCNT},
116 {"EXCNT", EXCNT},
117 {"LTCNT", LTCNT},
118 {"TXSM", TXSM},
119 {"RXCFG", RXCFG},
120 {"RXMAX", RXMAX},
121 {"RXMIN", RXMIN},
122 {"FRCNT", FRCNT},
123 {"AECNT", AECNT},
124 {"FECNT", FECNT},
125 {"RXSM", RXSM},
126 {"RXCV", RXCV}
127};
128
129#endif
130
131static unsigned char *bmac_emergency_rxbuf;
132
133/*
134 * Number of bytes of private data per BMAC: allow enough for
135 * the rx and tx dma commands plus a branch dma command each,
136 * and another 16 bytes to allow us to align the dma command
137 * buffers on a 16 byte boundary.
138 */
139#define PRIV_BYTES (sizeof(struct bmac_data) \
140 + (N_RX_RING + N_TX_RING + 4) * sizeof(struct dbdma_cmd) \
141 + sizeof(struct sk_buff_head))
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int bmac_open(struct net_device *dev);
144static int bmac_close(struct net_device *dev);
145static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static void bmac_set_multicast(struct net_device *dev);
147static void bmac_reset_and_enable(struct net_device *dev);
148static void bmac_start_chip(struct net_device *dev);
149static void bmac_init_chip(struct net_device *dev);
150static void bmac_init_registers(struct net_device *dev);
151static void bmac_enable_and_reset_chip(struct net_device *dev);
152static int bmac_set_address(struct net_device *dev, void *addr);
David Howells7d12e782006-10-05 14:55:46 +0100153static irqreturn_t bmac_misc_intr(int irq, void *dev_id);
154static irqreturn_t bmac_txdma_intr(int irq, void *dev_id);
155static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void bmac_set_timeout(struct net_device *dev);
Kees Cookde892f82017-10-16 17:29:27 -0700157static void bmac_tx_timeout(struct timer_list *t);
YueHaibinge6ce38222018-09-21 11:46:37 +0800158static netdev_tx_t bmac_output(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void bmac_start(struct net_device *dev);
160
161#define DBDMA_SET(x) ( ((x) | (x) << 16) )
162#define DBDMA_CLEAR(x) ( (x) << 16)
163
164static inline void
165dbdma_st32(volatile __u32 __iomem *a, unsigned long x)
166{
167 __asm__ volatile( "stwbrx %0,0,%1" : : "r" (x), "r" (a) : "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static inline unsigned long
171dbdma_ld32(volatile __u32 __iomem *a)
172{
173 __u32 swap;
174 __asm__ volatile ("lwbrx %0,0,%1" : "=r" (swap) : "r" (a));
175 return swap;
176}
177
178static void
179dbdma_continue(volatile struct dbdma_regs __iomem *dmap)
180{
181 dbdma_st32(&dmap->control,
182 DBDMA_SET(RUN|WAKE) | DBDMA_CLEAR(PAUSE|DEAD));
183 eieio();
184}
185
186static void
187dbdma_reset(volatile struct dbdma_regs __iomem *dmap)
188{
189 dbdma_st32(&dmap->control,
190 DBDMA_CLEAR(ACTIVE|DEAD|WAKE|FLUSH|PAUSE|RUN));
191 eieio();
192 while (dbdma_ld32(&dmap->status) & RUN)
193 eieio();
194}
195
196static void
197dbdma_setcmd(volatile struct dbdma_cmd *cp,
198 unsigned short cmd, unsigned count, unsigned long addr,
199 unsigned long cmd_dep)
200{
201 out_le16(&cp->command, cmd);
202 out_le16(&cp->req_count, count);
203 out_le32(&cp->phy_addr, addr);
204 out_le32(&cp->cmd_dep, cmd_dep);
205 out_le16(&cp->xfer_status, 0);
206 out_le16(&cp->res_count, 0);
207}
208
209static inline
210void bmwrite(struct net_device *dev, unsigned long reg_offset, unsigned data )
211{
212 out_le16((void __iomem *)dev->base_addr + reg_offset, data);
213}
214
215
216static inline
Al Viro66df3bb2005-09-30 04:19:43 +0100217unsigned short bmread(struct net_device *dev, unsigned long reg_offset )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 return in_le16((void __iomem *)dev->base_addr + reg_offset);
220}
221
222static void
223bmac_enable_and_reset_chip(struct net_device *dev)
224{
225 struct bmac_data *bp = netdev_priv(dev);
226 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
227 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
228
229 if (rd)
230 dbdma_reset(rd);
231 if (td)
232 dbdma_reset(td);
233
234 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 1);
235}
236
237#define MIFDELAY udelay(10)
238
239static unsigned int
240bmac_mif_readbits(struct net_device *dev, int nb)
241{
242 unsigned int val = 0;
243
244 while (--nb >= 0) {
245 bmwrite(dev, MIFCSR, 0);
246 MIFDELAY;
247 if (bmread(dev, MIFCSR) & 8)
248 val |= 1 << nb;
249 bmwrite(dev, MIFCSR, 1);
250 MIFDELAY;
251 }
252 bmwrite(dev, MIFCSR, 0);
253 MIFDELAY;
254 bmwrite(dev, MIFCSR, 1);
255 MIFDELAY;
256 return val;
257}
258
259static void
260bmac_mif_writebits(struct net_device *dev, unsigned int val, int nb)
261{
262 int b;
263
264 while (--nb >= 0) {
265 b = (val & (1 << nb))? 6: 4;
266 bmwrite(dev, MIFCSR, b);
267 MIFDELAY;
268 bmwrite(dev, MIFCSR, b|1);
269 MIFDELAY;
270 }
271}
272
273static unsigned int
274bmac_mif_read(struct net_device *dev, unsigned int addr)
275{
276 unsigned int val;
277
278 bmwrite(dev, MIFCSR, 4);
279 MIFDELAY;
280 bmac_mif_writebits(dev, ~0U, 32);
281 bmac_mif_writebits(dev, 6, 4);
282 bmac_mif_writebits(dev, addr, 10);
283 bmwrite(dev, MIFCSR, 2);
284 MIFDELAY;
285 bmwrite(dev, MIFCSR, 1);
286 MIFDELAY;
287 val = bmac_mif_readbits(dev, 17);
288 bmwrite(dev, MIFCSR, 4);
289 MIFDELAY;
290 return val;
291}
292
293static void
294bmac_mif_write(struct net_device *dev, unsigned int addr, unsigned int val)
295{
296 bmwrite(dev, MIFCSR, 4);
297 MIFDELAY;
298 bmac_mif_writebits(dev, ~0U, 32);
299 bmac_mif_writebits(dev, 5, 4);
300 bmac_mif_writebits(dev, addr, 10);
301 bmac_mif_writebits(dev, 2, 2);
302 bmac_mif_writebits(dev, val, 16);
303 bmac_mif_writebits(dev, 3, 2);
304}
305
306static void
307bmac_init_registers(struct net_device *dev)
308{
309 struct bmac_data *bp = netdev_priv(dev);
310 volatile unsigned short regValue;
Jakub Kicinski76660752021-10-14 07:24:31 -0700311 const unsigned short *pWord16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int i;
313
314 /* XXDEBUG(("bmac: enter init_registers\n")); */
315
316 bmwrite(dev, RXRST, RxResetValue);
317 bmwrite(dev, TXRST, TxResetBit);
318
319 i = 100;
320 do {
321 --i;
322 udelay(10000);
323 regValue = bmread(dev, TXRST); /* wait for reset to clear..acknowledge */
324 } while ((regValue & TxResetBit) && i > 0);
325
326 if (!bp->is_bmac_plus) {
327 regValue = bmread(dev, XCVRIF);
328 regValue |= ClkBit | SerialMode | COLActiveLow;
329 bmwrite(dev, XCVRIF, regValue);
330 udelay(10000);
331 }
332
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400333 bmwrite(dev, RSEED, (unsigned short)0x1968);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 regValue = bmread(dev, XIFC);
336 regValue |= TxOutputEnable;
337 bmwrite(dev, XIFC, regValue);
338
339 bmread(dev, PAREG);
340
341 /* set collision counters to 0 */
342 bmwrite(dev, NCCNT, 0);
343 bmwrite(dev, NTCNT, 0);
344 bmwrite(dev, EXCNT, 0);
345 bmwrite(dev, LTCNT, 0);
346
347 /* set rx counters to 0 */
348 bmwrite(dev, FRCNT, 0);
349 bmwrite(dev, LECNT, 0);
350 bmwrite(dev, AECNT, 0);
351 bmwrite(dev, FECNT, 0);
352 bmwrite(dev, RXCV, 0);
353
354 /* set tx fifo information */
355 bmwrite(dev, TXTH, 4); /* 4 octets before tx starts */
356
357 bmwrite(dev, TXFIFOCSR, 0); /* first disable txFIFO */
358 bmwrite(dev, TXFIFOCSR, TxFIFOEnable );
359
360 /* set rx fifo information */
361 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
362 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
363
364 //bmwrite(dev, TXCFG, TxMACEnable); /* TxNeverGiveUp maybe later */
365 bmread(dev, STATUS); /* read it just to clear it */
366
367 /* zero out the chip Hash Filter registers */
368 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
369 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
370 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
371 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
372 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400373
Jakub Kicinski76660752021-10-14 07:24:31 -0700374 pWord16 = (const unsigned short *)dev->dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 bmwrite(dev, MADD0, *pWord16++);
376 bmwrite(dev, MADD1, *pWord16++);
377 bmwrite(dev, MADD2, *pWord16);
378
379 bmwrite(dev, RXCFG, RxCRCNoStrip | RxHashFilterEnable | RxRejectOwnPackets);
380
381 bmwrite(dev, INTDISABLE, EnableNormal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384#if 0
385static void
386bmac_disable_interrupts(struct net_device *dev)
387{
388 bmwrite(dev, INTDISABLE, DisableAll);
389}
390
391static void
392bmac_enable_interrupts(struct net_device *dev)
393{
394 bmwrite(dev, INTDISABLE, EnableNormal);
395}
396#endif
397
398
399static void
400bmac_start_chip(struct net_device *dev)
401{
402 struct bmac_data *bp = netdev_priv(dev);
403 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
404 unsigned short oldConfig;
405
406 /* enable rx dma channel */
407 dbdma_continue(rd);
408
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400409 oldConfig = bmread(dev, TXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
411
412 /* turn on rx plus any other bits already on (promiscuous possibly) */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400413 oldConfig = bmread(dev, RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
415 udelay(20000);
416}
417
418static void
419bmac_init_phy(struct net_device *dev)
420{
421 unsigned int addr;
422 struct bmac_data *bp = netdev_priv(dev);
423
424 printk(KERN_DEBUG "phy registers:");
425 for (addr = 0; addr < 32; ++addr) {
426 if ((addr & 7) == 0)
Joe Perchesad361c92009-07-06 13:05:40 -0700427 printk(KERN_DEBUG);
428 printk(KERN_CONT " %.4x", bmac_mif_read(dev, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Dave Jones42359da2009-07-09 21:54:39 -0400430 printk(KERN_CONT "\n");
Joe Perchesad361c92009-07-06 13:05:40 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (bp->is_bmac_plus) {
433 unsigned int capable, ctrl;
434
435 ctrl = bmac_mif_read(dev, 0);
436 capable = ((bmac_mif_read(dev, 1) & 0xf800) >> 6) | 1;
Joe Perches8e95a202009-12-03 07:58:21 +0000437 if (bmac_mif_read(dev, 4) != capable ||
438 (ctrl & 0x1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 bmac_mif_write(dev, 4, capable);
440 bmac_mif_write(dev, 0, 0x1200);
441 } else
442 bmac_mif_write(dev, 0, 0x1000);
443 }
444}
445
446static void bmac_init_chip(struct net_device *dev)
447{
448 bmac_init_phy(dev);
449 bmac_init_registers(dev);
450}
451
452#ifdef CONFIG_PM
Pavel Machek05adc3b2005-04-16 15:25:25 -0700453static int bmac_suspend(struct macio_dev *mdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400455 struct net_device* dev = macio_get_drvdata(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct bmac_data *bp = netdev_priv(dev);
457 unsigned long flags;
458 unsigned short config;
459 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 netif_device_detach(dev);
462 /* prolly should wait for dma to finish & turn off the chip */
463 spin_lock_irqsave(&bp->lock, flags);
464 if (bp->timeout_active) {
465 del_timer(&bp->tx_timeout);
466 bp->timeout_active = 0;
467 }
468 disable_irq(dev->irq);
469 disable_irq(bp->tx_dma_intr);
470 disable_irq(bp->rx_dma_intr);
471 bp->sleeping = 1;
472 spin_unlock_irqrestore(&bp->lock, flags);
473 if (bp->opened) {
474 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
475 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 config = bmread(dev, RXCFG);
478 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
479 config = bmread(dev, TXCFG);
Hui Tanga22cf812021-05-19 13:30:37 +0800480 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
Hui Tanga22cf812021-05-19 13:30:37 +0800482 /* disable rx and tx dma */
David Gibsonf5718722015-02-03 16:36:21 +1100483 rd->control = cpu_to_le32(DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
484 td->control = cpu_to_le32(DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
Hui Tanga22cf812021-05-19 13:30:37 +0800485 /* free some skb's */
486 for (i=0; i<N_RX_RING; i++) {
487 if (bp->rx_bufs[i] != NULL) {
488 dev_kfree_skb(bp->rx_bufs[i]);
489 bp->rx_bufs[i] = NULL;
490 }
491 }
492 for (i = 0; i<N_TX_RING; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (bp->tx_bufs[i] != NULL) {
494 dev_kfree_skb(bp->tx_bufs[i]);
495 bp->tx_bufs[i] = NULL;
496 }
497 }
498 }
Hui Tanga22cf812021-05-19 13:30:37 +0800499 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
503static int bmac_resume(struct macio_dev *mdev)
504{
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400505 struct net_device* dev = macio_get_drvdata(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct bmac_data *bp = netdev_priv(dev);
507
508 /* see if this is enough */
509 if (bp->opened)
510 bmac_reset_and_enable(dev);
511
512 enable_irq(dev->irq);
Hui Tanga22cf812021-05-19 13:30:37 +0800513 enable_irq(bp->tx_dma_intr);
514 enable_irq(bp->rx_dma_intr);
515 netif_device_attach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return 0;
518}
519#endif /* CONFIG_PM */
520
521static int bmac_set_address(struct net_device *dev, void *addr)
522{
523 struct bmac_data *bp = netdev_priv(dev);
Jakub Kicinski76660752021-10-14 07:24:31 -0700524 const unsigned short *pWord16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 XXDEBUG(("bmac: enter set_address\n"));
528 spin_lock_irqsave(&bp->lock, flags);
529
Jakub Kicinski562ef982021-10-13 13:44:34 -0700530 eth_hw_addr_set(dev, addr);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* load up the hardware address */
Jakub Kicinski76660752021-10-14 07:24:31 -0700533 pWord16 = (const unsigned short *)dev->dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 bmwrite(dev, MADD0, *pWord16++);
535 bmwrite(dev, MADD1, *pWord16++);
536 bmwrite(dev, MADD2, *pWord16);
537
538 spin_unlock_irqrestore(&bp->lock, flags);
539 XXDEBUG(("bmac: exit set_address\n"));
540 return 0;
541}
542
543static inline void bmac_set_timeout(struct net_device *dev)
544{
545 struct bmac_data *bp = netdev_priv(dev);
546 unsigned long flags;
547
548 spin_lock_irqsave(&bp->lock, flags);
549 if (bp->timeout_active)
550 del_timer(&bp->tx_timeout);
551 bp->tx_timeout.expires = jiffies + TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 add_timer(&bp->tx_timeout);
553 bp->timeout_active = 1;
554 spin_unlock_irqrestore(&bp->lock, flags);
555}
556
557static void
558bmac_construct_xmt(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
559{
560 void *vaddr;
561 unsigned long baddr;
562 unsigned long len;
563
564 len = skb->len;
565 vaddr = skb->data;
566 baddr = virt_to_bus(vaddr);
567
568 dbdma_setcmd(cp, (OUTPUT_LAST | INTR_ALWAYS | WAIT_IFCLR), len, baddr, 0);
569}
570
571static void
572bmac_construct_rxbuff(struct sk_buff *skb, volatile struct dbdma_cmd *cp)
573{
574 unsigned char *addr = skb? skb->data: bmac_emergency_rxbuf;
575
576 dbdma_setcmd(cp, (INPUT_LAST | INTR_ALWAYS), RX_BUFLEN,
577 virt_to_bus(addr), 0);
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static void
581bmac_init_tx_ring(struct bmac_data *bp)
582{
583 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
584
585 memset((char *)bp->tx_cmds, 0, (N_TX_RING+1) * sizeof(struct dbdma_cmd));
586
587 bp->tx_empty = 0;
588 bp->tx_fill = 0;
589 bp->tx_fullup = 0;
590
591 /* put a branch at the end of the tx command list */
592 dbdma_setcmd(&bp->tx_cmds[N_TX_RING],
593 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->tx_cmds));
594
595 /* reset tx dma */
596 dbdma_reset(td);
597 out_le32(&td->wait_sel, 0x00200020);
598 out_le32(&td->cmdptr, virt_to_bus(bp->tx_cmds));
599}
600
601static int
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +0000602bmac_init_rx_ring(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +0000604 struct bmac_data *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
606 int i;
607 struct sk_buff *skb;
608
609 /* initialize list of sk_buffs for receiving and set up recv dma */
610 memset((char *)bp->rx_cmds, 0,
611 (N_RX_RING + 1) * sizeof(struct dbdma_cmd));
612 for (i = 0; i < N_RX_RING; i++) {
613 if ((skb = bp->rx_bufs[i]) == NULL) {
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +0000614 bp->rx_bufs[i] = skb = netdev_alloc_skb(dev, RX_BUFLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (skb != NULL)
616 skb_reserve(skb, 2);
617 }
618 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
619 }
620
621 bp->rx_empty = 0;
622 bp->rx_fill = i;
623
624 /* Put a branch back to the beginning of the receive command list */
625 dbdma_setcmd(&bp->rx_cmds[N_RX_RING],
626 (DBDMA_NOP | BR_ALWAYS), 0, 0, virt_to_bus(bp->rx_cmds));
627
628 /* start rx dma */
629 dbdma_reset(rd);
630 out_le32(&rd->cmdptr, virt_to_bus(bp->rx_cmds));
631
632 return 1;
633}
634
635
636static int bmac_transmit_packet(struct sk_buff *skb, struct net_device *dev)
637{
638 struct bmac_data *bp = netdev_priv(dev);
639 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
640 int i;
641
642 /* see if there's a free slot in the tx ring */
643 /* XXDEBUG(("bmac_xmit_start: empty=%d fill=%d\n", */
644 /* bp->tx_empty, bp->tx_fill)); */
645 i = bp->tx_fill + 1;
646 if (i >= N_TX_RING)
647 i = 0;
648 if (i == bp->tx_empty) {
649 netif_stop_queue(dev);
650 bp->tx_fullup = 1;
651 XXDEBUG(("bmac_transmit_packet: tx ring full\n"));
652 return -1; /* can't take it at the moment */
653 }
654
655 dbdma_setcmd(&bp->tx_cmds[i], DBDMA_STOP, 0, 0, 0);
656
657 bmac_construct_xmt(skb, &bp->tx_cmds[bp->tx_fill]);
658
659 bp->tx_bufs[bp->tx_fill] = skb;
660 bp->tx_fill = i;
661
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700662 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 dbdma_continue(td);
665
666 return 0;
667}
668
669static int rxintcount;
670
David Howells7d12e782006-10-05 14:55:46 +0100671static irqreturn_t bmac_rxdma_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct net_device *dev = (struct net_device *) dev_id;
674 struct bmac_data *bp = netdev_priv(dev);
675 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
676 volatile struct dbdma_cmd *cp;
677 int i, nb, stat;
678 struct sk_buff *skb;
679 unsigned int residual;
680 int last;
681 unsigned long flags;
682
683 spin_lock_irqsave(&bp->lock, flags);
684
685 if (++rxintcount < 10) {
686 XXDEBUG(("bmac_rxdma_intr\n"));
687 }
688
689 last = -1;
690 i = bp->rx_empty;
691
692 while (1) {
693 cp = &bp->rx_cmds[i];
David Gibsonf5718722015-02-03 16:36:21 +1100694 stat = le16_to_cpu(cp->xfer_status);
695 residual = le16_to_cpu(cp->res_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if ((stat & ACTIVE) == 0)
697 break;
698 nb = RX_BUFLEN - residual - 2;
699 if (nb < (ETHERMINPACKET - ETHERCRC)) {
700 skb = NULL;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700701 dev->stats.rx_length_errors++;
702 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 } else {
704 skb = bp->rx_bufs[i];
705 bp->rx_bufs[i] = NULL;
706 }
707 if (skb != NULL) {
708 nb -= ETHERCRC;
709 skb_put(skb, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 skb->protocol = eth_type_trans(skb, dev);
711 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700712 ++dev->stats.rx_packets;
713 dev->stats.rx_bytes += nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700715 ++dev->stats.rx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if ((skb = bp->rx_bufs[i]) == NULL) {
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +0000718 bp->rx_bufs[i] = skb = netdev_alloc_skb(dev, RX_BUFLEN + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (skb != NULL)
720 skb_reserve(bp->rx_bufs[i], 2);
721 }
722 bmac_construct_rxbuff(skb, &bp->rx_cmds[i]);
David Gibsonf5718722015-02-03 16:36:21 +1100723 cp->res_count = cpu_to_le16(0);
724 cp->xfer_status = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 last = i;
726 if (++i >= N_RX_RING) i = 0;
727 }
728
729 if (last != -1) {
730 bp->rx_fill = last;
731 bp->rx_empty = i;
732 }
733
734 dbdma_continue(rd);
735 spin_unlock_irqrestore(&bp->lock, flags);
736
737 if (rxintcount < 10) {
738 XXDEBUG(("bmac_rxdma_intr done\n"));
739 }
740 return IRQ_HANDLED;
741}
742
743static int txintcount;
744
David Howells7d12e782006-10-05 14:55:46 +0100745static irqreturn_t bmac_txdma_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 struct net_device *dev = (struct net_device *) dev_id;
748 struct bmac_data *bp = netdev_priv(dev);
749 volatile struct dbdma_cmd *cp;
750 int stat;
751 unsigned long flags;
752
753 spin_lock_irqsave(&bp->lock, flags);
754
755 if (txintcount++ < 10) {
756 XXDEBUG(("bmac_txdma_intr\n"));
757 }
758
759 /* del_timer(&bp->tx_timeout); */
760 /* bp->timeout_active = 0; */
761
762 while (1) {
763 cp = &bp->tx_cmds[bp->tx_empty];
David Gibsonf5718722015-02-03 16:36:21 +1100764 stat = le16_to_cpu(cp->xfer_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (txintcount < 10) {
766 XXDEBUG(("bmac_txdma_xfer_stat=%#0x\n", stat));
767 }
768 if (!(stat & ACTIVE)) {
769 /*
770 * status field might not have been filled by DBDMA
771 */
772 if (cp == bus_to_virt(in_le32(&bp->tx_dma->cmdptr)))
773 break;
774 }
775
776 if (bp->tx_bufs[bp->tx_empty]) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700777 ++dev->stats.tx_packets;
Yang Wei10009112019-01-29 07:39:13 +0800778 dev_consume_skb_irq(bp->tx_bufs[bp->tx_empty]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 bp->tx_bufs[bp->tx_empty] = NULL;
781 bp->tx_fullup = 0;
782 netif_wake_queue(dev);
783 if (++bp->tx_empty >= N_TX_RING)
784 bp->tx_empty = 0;
785 if (bp->tx_empty == bp->tx_fill)
786 break;
787 }
788
789 spin_unlock_irqrestore(&bp->lock, flags);
790
791 if (txintcount < 10) {
792 XXDEBUG(("bmac_txdma_intr done->bmac_start\n"));
793 }
794
795 bmac_start(dev);
796 return IRQ_HANDLED;
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799#ifndef SUNHME_MULTICAST
800/* Real fast bit-reversal algorithm, 6-bit values */
801static int reverse6[64] = {
802 0x0,0x20,0x10,0x30,0x8,0x28,0x18,0x38,
803 0x4,0x24,0x14,0x34,0xc,0x2c,0x1c,0x3c,
804 0x2,0x22,0x12,0x32,0xa,0x2a,0x1a,0x3a,
805 0x6,0x26,0x16,0x36,0xe,0x2e,0x1e,0x3e,
806 0x1,0x21,0x11,0x31,0x9,0x29,0x19,0x39,
807 0x5,0x25,0x15,0x35,0xd,0x2d,0x1d,0x3d,
808 0x3,0x23,0x13,0x33,0xb,0x2b,0x1b,0x3b,
809 0x7,0x27,0x17,0x37,0xf,0x2f,0x1f,0x3f
810};
811
812static unsigned int
813crc416(unsigned int curval, unsigned short nxtval)
814{
Alexey Dobriyan9d2f1122019-08-05 23:34:41 +0300815 unsigned int counter, cur = curval, next = nxtval;
816 int high_crc_set, low_data_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Swap bytes */
819 next = ((next & 0x00FF) << 8) | (next >> 8);
820
821 /* Compute bit-by-bit */
822 for (counter = 0; counter < 16; ++counter) {
823 /* is high CRC bit set? */
824 if ((cur & 0x80000000) == 0) high_crc_set = 0;
825 else high_crc_set = 1;
826
827 cur = cur << 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if ((next & 0x0001) == 0) low_data_set = 0;
830 else low_data_set = 1;
831
832 next = next >> 1;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* do the XOR */
Krzysztof Kozlowski5d258b42018-07-17 18:05:39 +0200835 if (high_crc_set ^ low_data_set) cur = cur ^ CRC32_POLY_BE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 return cur;
838}
839
840static unsigned int
841bmac_crc(unsigned short *address)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 unsigned int newcrc;
844
845 XXDEBUG(("bmac_crc: addr=%#04x, %#04x, %#04x\n", *address, address[1], address[2]));
846 newcrc = crc416(0xffffffff, *address); /* address bits 47 - 32 */
847 newcrc = crc416(newcrc, address[1]); /* address bits 31 - 16 */
848 newcrc = crc416(newcrc, address[2]); /* address bits 15 - 0 */
849
850 return(newcrc);
851}
852
853/*
854 * Add requested mcast addr to BMac's hash table filter.
855 *
856 */
857
858static void
859bmac_addhash(struct bmac_data *bp, unsigned char *addr)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int crc;
862 unsigned short mask;
863
864 if (!(*addr)) return;
865 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
866 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
867 if (bp->hash_use_count[crc]++) return; /* This bit is already set */
868 mask = crc % 16;
869 mask = (unsigned char)1 << mask;
870 bp->hash_use_count[crc/16] |= mask;
871}
872
873static void
874bmac_removehash(struct bmac_data *bp, unsigned char *addr)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned int crc;
877 unsigned char mask;
878
879 /* Now, delete the address from the filter copy, as indicated */
880 crc = bmac_crc((unsigned short *)addr) & 0x3f; /* Big-endian alert! */
881 crc = reverse6[crc]; /* Hyperfast bit-reversing algorithm */
882 if (bp->hash_use_count[crc] == 0) return; /* That bit wasn't in use! */
883 if (--bp->hash_use_count[crc]) return; /* That bit is still in use */
884 mask = crc % 16;
885 mask = ((unsigned char)1 << mask) ^ 0xffff; /* To turn off bit */
886 bp->hash_table_mask[crc/16] &= mask;
887}
888
889/*
890 * Sync the adapter with the software copy of the multicast mask
891 * (logical address filter).
892 */
893
894static void
895bmac_rx_off(struct net_device *dev)
896{
897 unsigned short rx_cfg;
898
899 rx_cfg = bmread(dev, RXCFG);
900 rx_cfg &= ~RxMACEnable;
901 bmwrite(dev, RXCFG, rx_cfg);
902 do {
903 rx_cfg = bmread(dev, RXCFG);
904 } while (rx_cfg & RxMACEnable);
905}
906
907unsigned short
908bmac_rx_on(struct net_device *dev, int hash_enable, int promisc_enable)
909{
910 unsigned short rx_cfg;
911
912 rx_cfg = bmread(dev, RXCFG);
913 rx_cfg |= RxMACEnable;
914 if (hash_enable) rx_cfg |= RxHashFilterEnable;
915 else rx_cfg &= ~RxHashFilterEnable;
916 if (promisc_enable) rx_cfg |= RxPromiscEnable;
917 else rx_cfg &= ~RxPromiscEnable;
918 bmwrite(dev, RXRST, RxResetValue);
919 bmwrite(dev, RXFIFOCSR, 0); /* first disable rxFIFO */
920 bmwrite(dev, RXFIFOCSR, RxFIFOEnable );
921 bmwrite(dev, RXCFG, rx_cfg );
922 return rx_cfg;
923}
924
925static void
926bmac_update_hash_table_mask(struct net_device *dev, struct bmac_data *bp)
927{
928 bmwrite(dev, BHASH3, bp->hash_table_mask[0]); /* bits 15 - 0 */
929 bmwrite(dev, BHASH2, bp->hash_table_mask[1]); /* bits 31 - 16 */
930 bmwrite(dev, BHASH1, bp->hash_table_mask[2]); /* bits 47 - 32 */
931 bmwrite(dev, BHASH0, bp->hash_table_mask[3]); /* bits 63 - 48 */
932}
933
934#if 0
935static void
936bmac_add_multi(struct net_device *dev,
937 struct bmac_data *bp, unsigned char *addr)
938{
939 /* XXDEBUG(("bmac: enter bmac_add_multi\n")); */
940 bmac_addhash(bp, addr);
941 bmac_rx_off(dev);
942 bmac_update_hash_table_mask(dev, bp);
943 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
944 /* XXDEBUG(("bmac: exit bmac_add_multi\n")); */
945}
946
947static void
948bmac_remove_multi(struct net_device *dev,
949 struct bmac_data *bp, unsigned char *addr)
950{
951 bmac_removehash(bp, addr);
952 bmac_rx_off(dev);
953 bmac_update_hash_table_mask(dev, bp);
954 bmac_rx_on(dev, 1, (dev->flags & IFF_PROMISC)? 1 : 0);
955}
956#endif
957
958/* Set or clear the multicast filter for this adaptor.
959 num_addrs == -1 Promiscuous mode, receive all packets
960 num_addrs == 0 Normal mode, clear multicast list
961 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
962 best-effort filtering.
963 */
964static void bmac_set_multicast(struct net_device *dev)
965{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000966 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct bmac_data *bp = netdev_priv(dev);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000968 int num_addrs = netdev_mc_count(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned short rx_cfg;
970 int i;
971
972 if (bp->sleeping)
973 return;
974
975 XXDEBUG(("bmac: enter bmac_set_multicast, n_addrs=%d\n", num_addrs));
976
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000977 if((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0xffff;
979 bmac_update_hash_table_mask(dev, bp);
980 rx_cfg = bmac_rx_on(dev, 1, 0);
981 XXDEBUG(("bmac: all multi, rx_cfg=%#08x\n"));
982 } else if ((dev->flags & IFF_PROMISC) || (num_addrs < 0)) {
983 rx_cfg = bmread(dev, RXCFG);
984 rx_cfg |= RxPromiscEnable;
985 bmwrite(dev, RXCFG, rx_cfg);
986 rx_cfg = bmac_rx_on(dev, 0, 1);
987 XXDEBUG(("bmac: promisc mode enabled, rx_cfg=%#08x\n", rx_cfg));
988 } else {
989 for (i=0; i<4; i++) bp->hash_table_mask[i] = 0;
990 for (i=0; i<64; i++) bp->hash_use_count[i] = 0;
991 if (num_addrs == 0) {
992 rx_cfg = bmac_rx_on(dev, 0, 0);
993 XXDEBUG(("bmac: multi disabled, rx_cfg=%#08x\n", rx_cfg));
994 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000995 netdev_for_each_mc_addr(ha, dev)
996 bmac_addhash(bp, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 bmac_update_hash_table_mask(dev, bp);
998 rx_cfg = bmac_rx_on(dev, 1, 0);
999 XXDEBUG(("bmac: multi enabled, rx_cfg=%#08x\n", rx_cfg));
1000 }
1001 }
1002 /* XXDEBUG(("bmac: exit bmac_set_multicast\n")); */
1003}
1004#else /* ifdef SUNHME_MULTICAST */
1005
1006/* The version of set_multicast below was lifted from sunhme.c */
1007
1008static void bmac_set_multicast(struct net_device *dev)
1009{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001010 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 unsigned short rx_cfg;
1012 u32 crc;
1013
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001014 if((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 bmwrite(dev, BHASH0, 0xffff);
1016 bmwrite(dev, BHASH1, 0xffff);
1017 bmwrite(dev, BHASH2, 0xffff);
1018 bmwrite(dev, BHASH3, 0xffff);
1019 } else if(dev->flags & IFF_PROMISC) {
1020 rx_cfg = bmread(dev, RXCFG);
1021 rx_cfg |= RxPromiscEnable;
1022 bmwrite(dev, RXCFG, rx_cfg);
1023 } else {
Emilio López3b0aaef2013-05-17 10:42:54 +00001024 u16 hash_table[4] = { 0 };
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 rx_cfg = bmread(dev, RXCFG);
1027 rx_cfg &= ~RxPromiscEnable;
1028 bmwrite(dev, RXCFG, rx_cfg);
1029
Jiri Pirko22bedad32010-04-01 21:22:57 +00001030 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +00001031 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 crc >>= 26;
1033 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1034 }
1035 bmwrite(dev, BHASH0, hash_table[0]);
1036 bmwrite(dev, BHASH1, hash_table[1]);
1037 bmwrite(dev, BHASH2, hash_table[2]);
1038 bmwrite(dev, BHASH3, hash_table[3]);
1039 }
1040}
1041#endif /* SUNHME_MULTICAST */
1042
1043static int miscintcount;
1044
David Howells7d12e782006-10-05 14:55:46 +01001045static irqreturn_t bmac_misc_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct net_device *dev = (struct net_device *) dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 unsigned int status = bmread(dev, STATUS);
1049 if (miscintcount++ < 10) {
1050 XXDEBUG(("bmac_misc_intr\n"));
1051 }
1052 /* XXDEBUG(("bmac_misc_intr, status=%#08x\n", status)); */
David Howells7d12e782006-10-05 14:55:46 +01001053 /* bmac_txdma_intr_inner(irq, dev_id); */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001054 /* if (status & FrameReceived) dev->stats.rx_dropped++; */
1055 if (status & RxErrorMask) dev->stats.rx_errors++;
1056 if (status & RxCRCCntExp) dev->stats.rx_crc_errors++;
1057 if (status & RxLenCntExp) dev->stats.rx_length_errors++;
1058 if (status & RxOverFlow) dev->stats.rx_over_errors++;
1059 if (status & RxAlignCntExp) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001061 /* if (status & FrameSent) dev->stats.tx_dropped++; */
1062 if (status & TxErrorMask) dev->stats.tx_errors++;
1063 if (status & TxUnderrun) dev->stats.tx_fifo_errors++;
1064 if (status & TxNormalCollExp) dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return IRQ_HANDLED;
1066}
1067
1068/*
1069 * Procedure for reading EEPROM
1070 */
1071#define SROMAddressLength 5
1072#define DataInOn 0x0008
1073#define DataInOff 0x0000
1074#define Clk 0x0002
1075#define ChipSelect 0x0001
1076#define SDIShiftCount 3
1077#define SD0ShiftCount 2
1078#define DelayValue 1000 /* number of microseconds */
1079#define SROMStartOffset 10 /* this is in words */
1080#define SROMReadCount 3 /* number of words to read from SROM */
1081#define SROMAddressBits 6
1082#define EnetAddressOffset 20
1083
1084static unsigned char
1085bmac_clock_out_bit(struct net_device *dev)
1086{
1087 unsigned short data;
1088 unsigned short val;
1089
1090 bmwrite(dev, SROMCSR, ChipSelect | Clk);
1091 udelay(DelayValue);
1092
1093 data = bmread(dev, SROMCSR);
1094 udelay(DelayValue);
1095 val = (data >> SD0ShiftCount) & 1;
1096
1097 bmwrite(dev, SROMCSR, ChipSelect);
1098 udelay(DelayValue);
1099
1100 return val;
1101}
1102
1103static void
1104bmac_clock_in_bit(struct net_device *dev, unsigned int val)
1105{
1106 unsigned short data;
1107
1108 if (val != 0 && val != 1) return;
1109
1110 data = (val << SDIShiftCount);
1111 bmwrite(dev, SROMCSR, data | ChipSelect );
1112 udelay(DelayValue);
1113
1114 bmwrite(dev, SROMCSR, data | ChipSelect | Clk );
1115 udelay(DelayValue);
1116
1117 bmwrite(dev, SROMCSR, data | ChipSelect);
1118 udelay(DelayValue);
1119}
1120
1121static void
1122reset_and_select_srom(struct net_device *dev)
1123{
1124 /* first reset */
1125 bmwrite(dev, SROMCSR, 0);
1126 udelay(DelayValue);
1127
1128 /* send it the read command (110) */
1129 bmac_clock_in_bit(dev, 1);
1130 bmac_clock_in_bit(dev, 1);
1131 bmac_clock_in_bit(dev, 0);
1132}
1133
1134static unsigned short
1135read_srom(struct net_device *dev, unsigned int addr, unsigned int addr_len)
1136{
1137 unsigned short data, val;
1138 int i;
1139
1140 /* send out the address we want to read from */
1141 for (i = 0; i < addr_len; i++) {
1142 val = addr >> (addr_len-i-1);
1143 bmac_clock_in_bit(dev, val & 1);
1144 }
1145
1146 /* Now read in the 16-bit data */
1147 data = 0;
1148 for (i = 0; i < 16; i++) {
1149 val = bmac_clock_out_bit(dev);
1150 data <<= 1;
1151 data |= val;
1152 }
1153 bmwrite(dev, SROMCSR, 0);
1154
1155 return data;
1156}
1157
1158/*
1159 * It looks like Cogent and SMC use different methods for calculating
1160 * checksums. What a pain..
1161 */
1162
1163static int
1164bmac_verify_checksum(struct net_device *dev)
1165{
1166 unsigned short data, storedCS;
1167
1168 reset_and_select_srom(dev);
1169 data = read_srom(dev, 3, SROMAddressBits);
1170 storedCS = ((data >> 8) & 0x0ff) | ((data << 8) & 0xff00);
1171
1172 return 0;
1173}
1174
1175
1176static void
1177bmac_get_station_address(struct net_device *dev, unsigned char *ea)
1178{
1179 int i;
1180 unsigned short data;
1181
Jeremy Kerref01cee2020-05-19 09:05:58 +08001182 for (i = 0; i < 3; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 {
1184 reset_and_select_srom(dev);
1185 data = read_srom(dev, i + EnetAddressOffset/2, SROMAddressBits);
Akinobu Mitabc63eb92006-12-19 13:09:08 -08001186 ea[2*i] = bitrev8(data & 0x0ff);
1187 ea[2*i+1] = bitrev8((data >> 8) & 0x0ff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189}
1190
1191static void bmac_reset_and_enable(struct net_device *dev)
1192{
1193 struct bmac_data *bp = netdev_priv(dev);
1194 unsigned long flags;
1195 struct sk_buff *skb;
1196 unsigned char *data;
1197
1198 spin_lock_irqsave(&bp->lock, flags);
1199 bmac_enable_and_reset_chip(dev);
1200 bmac_init_tx_ring(bp);
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +00001201 bmac_init_rx_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 bmac_init_chip(dev);
1203 bmac_start_chip(dev);
1204 bmwrite(dev, INTDISABLE, EnableNormal);
1205 bp->sleeping = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * It seems that the bmac can't receive until it's transmitted
1209 * a packet. So we give it a dummy packet to transmit.
1210 */
Pradeep A. Dalvi1ab0d2e2012-02-06 11:16:48 +00001211 skb = netdev_alloc_skb(dev, ETHERMINPACKET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (skb != NULL) {
Johannes Bergaa9f9792017-06-13 14:28:18 +02001213 data = skb_put_zero(skb, ETHERMINPACKET);
Joe Perchesd458cdf2013-10-01 19:04:40 -07001214 memcpy(data, dev->dev_addr, ETH_ALEN);
1215 memcpy(data + ETH_ALEN, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 bmac_transmit_packet(skb, dev);
1217 }
1218 spin_unlock_irqrestore(&bp->lock, flags);
1219}
Olaf Heringced13332007-08-25 20:32:59 +02001220
1221static const struct ethtool_ops bmac_ethtool_ops = {
Olaf Heringced13332007-08-25 20:32:59 +02001222 .get_link = ethtool_op_get_link,
1223};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Alexander Beregalov7a4762a2009-04-15 12:52:35 +00001225static const struct net_device_ops bmac_netdev_ops = {
1226 .ndo_open = bmac_open,
1227 .ndo_stop = bmac_close,
1228 .ndo_start_xmit = bmac_output,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001229 .ndo_set_rx_mode = bmac_set_multicast,
Alexander Beregalov7a4762a2009-04-15 12:52:35 +00001230 .ndo_set_mac_address = bmac_set_address,
Alexander Beregalov7a4762a2009-04-15 12:52:35 +00001231 .ndo_validate_addr = eth_validate_addr,
1232};
1233
Bill Pemberton97c71ad2012-12-03 09:23:55 -05001234static int bmac_probe(struct macio_dev *mdev, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 int j, rev, ret;
1237 struct bmac_data *bp;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001238 const unsigned char *prop_addr;
1239 unsigned char addr[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct net_device *dev;
1241 int is_bmac_plus = ((int)match->data) != 0;
1242
1243 if (macio_resource_count(mdev) != 3 || macio_irq_count(mdev) != 3) {
1244 printk(KERN_ERR "BMAC: can't use, need 3 addrs and 3 intrs\n");
1245 return -ENODEV;
1246 }
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001247 prop_addr = of_get_property(macio_get_of_node(mdev),
1248 "mac-address", NULL);
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001249 if (prop_addr == NULL) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10001250 prop_addr = of_get_property(macio_get_of_node(mdev),
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001251 "local-mac-address", NULL);
1252 if (prop_addr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 printk(KERN_ERR "BMAC: Can't get mac-address\n");
1254 return -ENODEV;
1255 }
1256 }
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10001257 memcpy(addr, prop_addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 dev = alloc_etherdev(PRIV_BYTES);
Joe Perches41de8d42012-01-29 13:47:52 +00001260 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return -ENOMEM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
1265 macio_set_drvdata(mdev, dev);
1266
1267 bp->mdev = mdev;
1268 spin_lock_init(&bp->lock);
1269
1270 if (macio_request_resources(mdev, "bmac")) {
1271 printk(KERN_ERR "BMAC: can't request IO resource !\n");
1272 goto out_free;
1273 }
1274
1275 dev->base_addr = (unsigned long)
1276 ioremap(macio_resource_start(mdev, 0), macio_resource_len(mdev, 0));
1277 if (dev->base_addr == 0)
1278 goto out_release;
1279
1280 dev->irq = macio_irq(mdev, 0);
1281
1282 bmac_enable_and_reset_chip(dev);
1283 bmwrite(dev, INTDISABLE, DisableAll);
1284
1285 rev = addr[0] == 0 && addr[1] == 0xA0;
1286 for (j = 0; j < 6; ++j)
Akinobu Mitabc63eb92006-12-19 13:09:08 -08001287 dev->dev_addr[j] = rev ? bitrev8(addr[j]): addr[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* Enable chip without interrupts for now */
1290 bmac_enable_and_reset_chip(dev);
1291 bmwrite(dev, INTDISABLE, DisableAll);
1292
Alexander Beregalov7a4762a2009-04-15 12:52:35 +00001293 dev->netdev_ops = &bmac_netdev_ops;
Olaf Heringced13332007-08-25 20:32:59 +02001294 dev->ethtool_ops = &bmac_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 bmac_get_station_address(dev, addr);
1297 if (bmac_verify_checksum(dev) != 0)
1298 goto err_out_iounmap;
1299
1300 bp->is_bmac_plus = is_bmac_plus;
1301 bp->tx_dma = ioremap(macio_resource_start(mdev, 1), macio_resource_len(mdev, 1));
1302 if (!bp->tx_dma)
1303 goto err_out_iounmap;
1304 bp->tx_dma_intr = macio_irq(mdev, 1);
1305 bp->rx_dma = ioremap(macio_resource_start(mdev, 2), macio_resource_len(mdev, 2));
1306 if (!bp->rx_dma)
1307 goto err_out_iounmap_tx;
1308 bp->rx_dma_intr = macio_irq(mdev, 2);
1309
1310 bp->tx_cmds = (volatile struct dbdma_cmd *) DBDMA_ALIGN(bp + 1);
1311 bp->rx_cmds = bp->tx_cmds + N_TX_RING + 1;
1312
1313 bp->queue = (struct sk_buff_head *)(bp->rx_cmds + N_RX_RING + 1);
1314 skb_queue_head_init(bp->queue);
1315
Kees Cookde892f82017-10-16 17:29:27 -07001316 timer_setup(&bp->tx_timeout, bmac_tx_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 ret = request_irq(dev->irq, bmac_misc_intr, 0, "BMAC-misc", dev);
1319 if (ret) {
1320 printk(KERN_ERR "BMAC: can't get irq %d\n", dev->irq);
1321 goto err_out_iounmap_rx;
1322 }
1323 ret = request_irq(bp->tx_dma_intr, bmac_txdma_intr, 0, "BMAC-txdma", dev);
1324 if (ret) {
1325 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->tx_dma_intr);
1326 goto err_out_irq0;
1327 }
1328 ret = request_irq(bp->rx_dma_intr, bmac_rxdma_intr, 0, "BMAC-rxdma", dev);
1329 if (ret) {
1330 printk(KERN_ERR "BMAC: can't get irq %d\n", bp->rx_dma_intr);
1331 goto err_out_irq1;
1332 }
1333
1334 /* Mask chip interrupts and disable chip, will be
1335 * re-enabled on open()
1336 */
1337 disable_irq(dev->irq);
1338 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1339
1340 if (register_netdev(dev) != 0) {
1341 printk(KERN_ERR "BMAC: Ethernet registration failed\n");
1342 goto err_out_irq2;
1343 }
1344
Johannes Berge1749612008-10-27 15:59:26 -07001345 printk(KERN_INFO "%s: BMAC%s at %pM",
1346 dev->name, (is_bmac_plus ? "+" : ""), dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 XXDEBUG((", base_addr=%#0lx", dev->base_addr));
1348 printk("\n");
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return 0;
1351
1352err_out_irq2:
1353 free_irq(bp->rx_dma_intr, dev);
1354err_out_irq1:
1355 free_irq(bp->tx_dma_intr, dev);
1356err_out_irq0:
1357 free_irq(dev->irq, dev);
1358err_out_iounmap_rx:
1359 iounmap(bp->rx_dma);
1360err_out_iounmap_tx:
1361 iounmap(bp->tx_dma);
1362err_out_iounmap:
1363 iounmap((void __iomem *)dev->base_addr);
1364out_release:
1365 macio_release_resources(mdev);
1366out_free:
1367 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1368 free_netdev(dev);
1369
1370 return -ENODEV;
1371}
1372
1373static int bmac_open(struct net_device *dev)
1374{
1375 struct bmac_data *bp = netdev_priv(dev);
1376 /* XXDEBUG(("bmac: enter open\n")); */
1377 /* reset the chip */
1378 bp->opened = 1;
1379 bmac_reset_and_enable(dev);
1380 enable_irq(dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return 0;
1382}
1383
1384static int bmac_close(struct net_device *dev)
1385{
1386 struct bmac_data *bp = netdev_priv(dev);
1387 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1388 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1389 unsigned short config;
1390 int i;
1391
1392 bp->sleeping = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* disable rx and tx */
1395 config = bmread(dev, RXCFG);
1396 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1397
1398 config = bmread(dev, TXCFG);
1399 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1400
1401 bmwrite(dev, INTDISABLE, DisableAll); /* disable all intrs */
1402
1403 /* disable rx and tx dma */
David Gibsonf5718722015-02-03 16:36:21 +11001404 rd->control = cpu_to_le32(DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
1405 td->control = cpu_to_le32(DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE)); /* clear run bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* free some skb's */
1408 XXDEBUG(("bmac: free rx bufs\n"));
1409 for (i=0; i<N_RX_RING; i++) {
1410 if (bp->rx_bufs[i] != NULL) {
1411 dev_kfree_skb(bp->rx_bufs[i]);
1412 bp->rx_bufs[i] = NULL;
1413 }
1414 }
1415 XXDEBUG(("bmac: free tx bufs\n"));
1416 for (i = 0; i<N_TX_RING; i++) {
1417 if (bp->tx_bufs[i] != NULL) {
1418 dev_kfree_skb(bp->tx_bufs[i]);
1419 bp->tx_bufs[i] = NULL;
1420 }
1421 }
1422 XXDEBUG(("bmac: all bufs freed\n"));
1423
1424 bp->opened = 0;
1425 disable_irq(dev->irq);
1426 pmac_call_feature(PMAC_FTR_BMAC_ENABLE, macio_get_of_node(bp->mdev), 0, 0);
1427
1428 return 0;
1429}
1430
1431static void
1432bmac_start(struct net_device *dev)
1433{
1434 struct bmac_data *bp = netdev_priv(dev);
1435 int i;
1436 struct sk_buff *skb;
1437 unsigned long flags;
1438
1439 if (bp->sleeping)
1440 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 spin_lock_irqsave(&bp->lock, flags);
1443 while (1) {
1444 i = bp->tx_fill + 1;
1445 if (i >= N_TX_RING)
1446 i = 0;
1447 if (i == bp->tx_empty)
1448 break;
1449 skb = skb_dequeue(bp->queue);
1450 if (skb == NULL)
1451 break;
1452 bmac_transmit_packet(skb, dev);
1453 }
1454 spin_unlock_irqrestore(&bp->lock, flags);
1455}
1456
YueHaibinge6ce38222018-09-21 11:46:37 +08001457static netdev_tx_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458bmac_output(struct sk_buff *skb, struct net_device *dev)
1459{
1460 struct bmac_data *bp = netdev_priv(dev);
1461 skb_queue_tail(bp->queue, skb);
1462 bmac_start(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001463 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Kees Cookde892f82017-10-16 17:29:27 -07001466static void bmac_tx_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Kees Cookde892f82017-10-16 17:29:27 -07001468 struct bmac_data *bp = from_timer(bp, t, tx_timeout);
1469 struct net_device *dev = macio_get_drvdata(bp->mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 volatile struct dbdma_regs __iomem *td = bp->tx_dma;
1471 volatile struct dbdma_regs __iomem *rd = bp->rx_dma;
1472 volatile struct dbdma_cmd *cp;
1473 unsigned long flags;
1474 unsigned short config, oldConfig;
1475 int i;
1476
1477 XXDEBUG(("bmac: tx_timeout called\n"));
1478 spin_lock_irqsave(&bp->lock, flags);
1479 bp->timeout_active = 0;
1480
1481 /* update various counters */
1482/* bmac_handle_misc_intrs(bp, 0); */
1483
1484 cp = &bp->tx_cmds[bp->tx_empty];
1485/* XXDEBUG((KERN_DEBUG "bmac: tx dmastat=%x %x runt=%d pr=%x fs=%x fc=%x\n", */
David Gibsonf5718722015-02-03 16:36:21 +11001486/* le32_to_cpu(td->status), le16_to_cpu(cp->xfer_status), bp->tx_bad_runt, */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/* mb->pr, mb->xmtfs, mb->fifofc)); */
1488
1489 /* turn off both tx and rx and reset the chip */
1490 config = bmread(dev, RXCFG);
1491 bmwrite(dev, RXCFG, (config & ~RxMACEnable));
1492 config = bmread(dev, TXCFG);
1493 bmwrite(dev, TXCFG, (config & ~TxMACEnable));
1494 out_le32(&td->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1495 printk(KERN_ERR "bmac: transmit timeout - resetting\n");
1496 bmac_enable_and_reset_chip(dev);
1497
1498 /* restart rx dma */
David Gibsonf5718722015-02-03 16:36:21 +11001499 cp = bus_to_virt(le32_to_cpu(rd->cmdptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 out_le32(&rd->control, DBDMA_CLEAR(RUN|PAUSE|FLUSH|WAKE|ACTIVE|DEAD));
1501 out_le16(&cp->xfer_status, 0);
1502 out_le32(&rd->cmdptr, virt_to_bus(cp));
1503 out_le32(&rd->control, DBDMA_SET(RUN|WAKE));
1504
1505 /* fix up the transmit side */
1506 XXDEBUG((KERN_DEBUG "bmac: tx empty=%d fill=%d fullup=%d\n",
1507 bp->tx_empty, bp->tx_fill, bp->tx_fullup));
1508 i = bp->tx_empty;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001509 ++dev->stats.tx_errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (i != bp->tx_fill) {
1511 dev_kfree_skb(bp->tx_bufs[i]);
1512 bp->tx_bufs[i] = NULL;
1513 if (++i >= N_TX_RING) i = 0;
1514 bp->tx_empty = i;
1515 }
1516 bp->tx_fullup = 0;
1517 netif_wake_queue(dev);
1518 if (i != bp->tx_fill) {
1519 cp = &bp->tx_cmds[i];
1520 out_le16(&cp->xfer_status, 0);
1521 out_le16(&cp->command, OUTPUT_LAST);
1522 out_le32(&td->cmdptr, virt_to_bus(cp));
1523 out_le32(&td->control, DBDMA_SET(RUN));
1524 /* bmac_set_timeout(dev); */
1525 XXDEBUG((KERN_DEBUG "bmac: starting %d\n", i));
1526 }
1527
1528 /* turn it back on */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001529 oldConfig = bmread(dev, RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 bmwrite(dev, RXCFG, oldConfig | RxMACEnable );
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001531 oldConfig = bmread(dev, TXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 bmwrite(dev, TXCFG, oldConfig | TxMACEnable );
1533
1534 spin_unlock_irqrestore(&bp->lock, flags);
1535}
1536
1537#if 0
1538static void dump_dbdma(volatile struct dbdma_cmd *cp,int count)
1539{
1540 int i,*ip;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 for (i=0;i< count;i++) {
1543 ip = (int*)(cp+i);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 printk("dbdma req 0x%x addr 0x%x baddr 0x%x xfer/res 0x%x\n",
David Gibsonf5718722015-02-03 16:36:21 +11001546 le32_to_cpup(ip+0),
1547 le32_to_cpup(ip+1),
1548 le32_to_cpup(ip+2),
1549 le32_to_cpup(ip+3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
1552}
1553#endif
1554
1555#if 0
1556static int
1557bmac_proc_info(char *buffer, char **start, off_t offset, int length)
1558{
1559 int len = 0;
1560 off_t pos = 0;
1561 off_t begin = 0;
1562 int i;
1563
1564 if (bmac_devs == NULL)
Eric Dumazet807540b2010-09-23 05:40:09 +00001565 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 len += sprintf(buffer, "BMAC counters & registers\n");
1568
1569 for (i = 0; i<N_REG_ENTRIES; i++) {
1570 len += sprintf(buffer + len, "%s: %#08x\n",
1571 reg_entries[i].name,
1572 bmread(bmac_devs, reg_entries[i].reg_offset));
1573 pos = begin + len;
1574
1575 if (pos < offset) {
1576 len = 0;
1577 begin = pos;
1578 }
1579
1580 if (pos > offset+length) break;
1581 }
1582
1583 *start = buffer + (offset - begin);
1584 len -= (offset - begin);
1585
1586 if (len > length) len = length;
1587
1588 return len;
1589}
1590#endif
1591
Bill Pemberton97c71ad2012-12-03 09:23:55 -05001592static int bmac_remove(struct macio_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct net_device *dev = macio_get_drvdata(mdev);
1595 struct bmac_data *bp = netdev_priv(dev);
1596
1597 unregister_netdev(dev);
1598
Hui Tanga22cf812021-05-19 13:30:37 +08001599 free_irq(dev->irq, dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001600 free_irq(bp->tx_dma_intr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 free_irq(bp->rx_dma_intr, dev);
1602
1603 iounmap((void __iomem *)dev->base_addr);
1604 iounmap(bp->tx_dma);
1605 iounmap(bp->rx_dma);
1606
1607 macio_release_resources(mdev);
1608
1609 free_netdev(dev);
1610
1611 return 0;
1612}
1613
Fabian Frederick14448e22015-03-17 19:37:37 +01001614static const struct of_device_id bmac_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 {
1617 .name = "bmac",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .data = (void *)0,
1619 },
1620 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .type = "network",
1622 .compatible = "bmac+",
1623 .data = (void *)1,
1624 },
1625 {},
1626};
Olaf Hering8c9795b2005-10-28 17:46:21 -07001627MODULE_DEVICE_TABLE (of, bmac_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001629static struct macio_driver bmac_driver =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Benjamin Herrenschmidtc2cdf6a2010-06-02 17:09:18 +10001631 .driver = {
1632 .name = "bmac",
1633 .owner = THIS_MODULE,
1634 .of_match_table = bmac_match,
1635 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 .probe = bmac_probe,
1637 .remove = bmac_remove,
1638#ifdef CONFIG_PM
1639 .suspend = bmac_suspend,
1640 .resume = bmac_resume,
1641#endif
1642};
1643
1644
1645static int __init bmac_init(void)
1646{
1647 if (bmac_emergency_rxbuf == NULL) {
1648 bmac_emergency_rxbuf = kmalloc(RX_BUFLEN, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001649 if (bmac_emergency_rxbuf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
1653 return macio_register_driver(&bmac_driver);
1654}
1655
1656static void __exit bmac_exit(void)
1657{
1658 macio_unregister_driver(&bmac_driver);
1659
Jesper Juhlb4558ea2005-10-28 16:53:13 -04001660 kfree(bmac_emergency_rxbuf);
1661 bmac_emergency_rxbuf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664MODULE_AUTHOR("Randy Gobbel/Paul Mackerras");
1665MODULE_DESCRIPTION("PowerMac BMAC ethernet driver.");
1666MODULE_LICENSE("GPL");
1667
1668module_init(bmac_init);
1669module_exit(bmac_exit);