summaryrefslogtreecommitdiffstats
path: root/drivers/net/gmac.c
blob: b1a431499f944c38364f588881c65c16119f56bb (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * Network device driver for the GMAC ethernet controller on
 * Apple G4 Powermacs.
 *
 * Copyright (C) 2000 Paul Mackerras.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <asm/prom.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include "gmac.h"

#define DEBUG_PHY

#define NTX		32		/* must be power of 2 */
#define NRX		32		/* must be power of 2 */
#define RX_BUFLEN	(ETH_FRAME_LEN + 8)

struct gmac_dma_desc {
	unsigned int	cmd;
	unsigned int	status;
	unsigned int	address;	/* phys addr, low 32 bits */
	unsigned int	hi_addr;
};

/* Bits in cmd */
#define RX_OWN	0x80000000		/* 1 = owned by chip */
#define TX_SOP	0x80000000
#define TX_EOP	0x40000000

struct gmac {
	volatile unsigned int *regs;	/* hardware registers, virtual addr */
	volatile unsigned int *sysregs;
	unsigned long	desc_page;	/* page for DMA descriptors */
	volatile struct gmac_dma_desc *rxring;
	struct sk_buff	*rx_buff[NRX];
	int		next_rx;
	volatile struct gmac_dma_desc *txring;
	struct sk_buff	*tx_buff[NTX];
	int		next_tx;
	int		tx_gone;
	unsigned char	tx_full;
	int		phy_addr;
	int		full_duplex;
	struct net_device_stats stats;
	struct net_device *next_gmac;
};

#define GM_OUT(r, v)	out_le32(gm->regs + (r)/4, (v))
#define GM_IN(r)	in_le32(gm->regs + (r)/4)
#define GM_BIS(r, v)	GM_OUT((r), GM_IN(r) | (v))
#define GM_BIC(r, v)	GM_OUT((r), GM_IN(r) & ~(v))

#define PHY_B5400	0x6040
#define PHY_B5201	0x6212

static unsigned char dummy_buf[RX_BUFLEN+2];
static struct net_device *gmacs = NULL;

/* Prototypes */
static int mii_read(struct gmac *gm, int phy, int r);
static int mii_write(struct gmac *gm, int phy, int r, int v);
static void powerup_transceiver(struct gmac *gm);
static int gmac_reset(struct net_device *dev);
static void gmac_mac_init(struct gmac *gm, unsigned char *mac_addr);
static void gmac_init_rings(struct gmac *gm);
static void gmac_start_dma(struct gmac *gm);
static int gmac_open(struct net_device *dev);
static int gmac_close(struct net_device *dev);
static int gmac_xmit_start(struct sk_buff *skb, struct net_device *dev);
static int gmac_tx_cleanup(struct gmac *gm);
static void gmac_receive(struct net_device *dev);
static void gmac_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static struct net_device_stats *gmac_stats(struct net_device *dev);
static int gmac_probe(void);
static void gmac_probe1(struct device_node *gmac);

/* Stuff for talking to the physical-layer chip */
static int
mii_read(struct gmac *gm, int phy, int r)
{
	int timeout;

	GM_OUT(MIFFRAME, 0x60020000 | (phy << 23) | (r << 18));
	for (timeout = 1000; timeout > 0; --timeout) {
		udelay(20);
		if (GM_IN(MIFFRAME) & 0x10000)
			return GM_IN(MIFFRAME) & 0xffff;
	}
	return -1;
}

static int
mii_write(struct gmac *gm, int phy, int r, int v)
{
	int timeout;

	GM_OUT(MIFFRAME, 0x50020000 | (phy << 23) | (r << 18) | (v & 0xffff));
	for (timeout = 1000; timeout > 0; --timeout) {
		udelay(20);
		if (GM_IN(MIFFRAME) & 0x10000)
			return 0;
	}
	return -1;
}

static void 
mii_poll_start(struct gmac *gm)
{
	unsigned int tmp;
	
	/* Start the MIF polling on the external transceiver. */
	tmp = GM_IN(MIFCONFIG);
	tmp &= ~(GMAC_MIF_CFGPR_MASK | GMAC_MIF_CFGPD_MASK);
	tmp |= ((gm->phy_addr & 0x1f) << GMAC_MIF_CFGPD_SHIFT);
	tmp |= (0x19 << GMAC_MIF_CFGPR_SHIFT);
	tmp |= GMAC_MIF_CFGPE;
	GM_OUT(MIFCONFIG, tmp);

	/* Let the bits set. */
	udelay(GMAC_MIF_POLL_DELAY);

	GM_OUT(MIFINTMASK, 0xffc0);
}

static void 
mii_poll_stop(struct gmac *gm)
{
	GM_OUT(MIFINTMASK, 0xffff);
	GM_BIC(MIFCONFIG, GMAC_MIF_CFGPE);
	udelay(GMAC_MIF_POLL_DELAY);
}

static void
mii_interrupt(struct gmac *gm)
{
	unsigned long	flags;
	int		phy_status;
	
	save_flags(flags);
	cli();

	mii_poll_stop(gm);

	/* May the status change before polling is re-enabled ? */
	mii_poll_start(gm);
	
	/* We read the Auxilliary Status Summary register */
	phy_status = mii_read(gm, gm->phy_addr, 0x19);
#ifdef DEBUG_PHY
	printk("mii_interrupt, phy_status: %x\n", phy_status);
#endif
	/* Auto-neg. complete ? */
	if (phy_status & 0x8000) {
		int full_duplex = 0;
		switch((phy_status >> 8) & 0x7) {
			case 2:
			case 5:
				full_duplex = 1;
				break;
		}
		if (full_duplex != gm->full_duplex) {
			GM_BIC(TXMAC_CONFIG, 1);
			udelay(200);
			if (full_duplex) {
				printk("full duplex active\n");
				GM_OUT(TXMAC_CONFIG, 6);
				GM_OUT(XIF_CONFIG, 1);
			} else {
				printk("half duplex active\n");
				GM_OUT(TXMAC_CONFIG, 0);
				GM_OUT(XIF_CONFIG, 5);
			}
			GM_BIS(TXMAC_CONFIG, 1);
			gm->full_duplex = full_duplex;
		}
	}

	restore_flags(flags);
}

static void
powerup_transceiver(struct gmac *gm)
{
	int phytype = mii_read(gm, 0, 3);
#ifdef DEBUG_PHY
	int i;
#endif	
	switch (phytype) {
	case PHY_B5400:
		mii_write(gm, 0, 0, mii_read(gm, 0, 0) & ~0x800);
		mii_write(gm, 31, 30, mii_read(gm, 31, 30) & ~8);
		break;
	case PHY_B5201:
		mii_write(gm, 0, 30, mii_read(gm, 0, 30) & ~8);
		break;
	default:
		printk(KERN_ERR "GMAC: unknown PHY type %x\n", phytype);
	}
	/* Check this */
	gm->phy_addr = 0;
	gm->full_duplex = 0;

#ifdef DEBUG_PHY
	printk("PHY regs:\n");
	for (i=0; i<0x20; i++) {
		printk("%04x ", mii_read(gm, 0, i)); 
		if ((i % 4) == 3)
			printk("\n");
	}
#endif
}

static int
gmac_reset(struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;
	int timeout;

	/* turn on GB clock */
	out_le32(gm->sysregs + 0x20/4, in_le32(gm->sysregs + 0x20/4) | 2);
	udelay(10);
	GM_OUT(SW_RESET, 3);
	for (timeout = 100; timeout > 0; --timeout) {
		mdelay(10);
		if ((GM_IN(SW_RESET) & 3) == 0)
			return 0;
	}
	printk(KERN_ERR "GMAC: reset failed!\n");
	return -1;
}

static void
gmac_mac_init(struct gmac *gm, unsigned char *mac_addr)
{
	int i;

	GM_OUT(RANSEED, 937);
	GM_OUT(DATAPATHMODE, 4);
	mii_write(gm, 0, 0, 0x1000);
	GM_OUT(TXDMA_CONFIG, 0xffc00);
	GM_OUT(RXDMA_CONFIG, 0);
	GM_OUT(MACPAUSE, 0x1bf0);
	GM_OUT(IPG0, 0);
	GM_OUT(IPG1, 8);
	GM_OUT(IPG2, 4);
	GM_OUT(MINFRAMESIZE, 64);
	GM_OUT(MAXFRAMESIZE, 2000);
	GM_OUT(PASIZE, 7);
	GM_OUT(JAMSIZE, 4);
	GM_OUT(ATTEMPT_LIMIT, 16);
	GM_OUT(SLOTTIME, 64);
	GM_OUT(MACCNTL_TYPE, 0x8808);
	GM_OUT(MAC_ADDR_0, (mac_addr[4] << 8) + mac_addr[5]);
	GM_OUT(MAC_ADDR_1, (mac_addr[2] << 8) + mac_addr[3]);
	GM_OUT(MAC_ADDR_2, (mac_addr[0] << 8) + mac_addr[1]);
	GM_OUT(MAC_ADDR_3, 0);
	GM_OUT(MAC_ADDR_4, 0);
	GM_OUT(MAC_ADDR_5, 0);
	GM_OUT(MAC_ADDR_6, 0x0180);
	GM_OUT(MAC_ADDR_7, 0xc200);
	GM_OUT(MAC_ADDR_8, 0x0001);
	GM_OUT(MAC_ADDR_FILTER_0, 0);
	GM_OUT(MAC_ADDR_FILTER_1, 0);
	GM_OUT(MAC_ADDR_FILTER_2, 0);
	GM_OUT(MAC_ADDR_FILTER_MASK21, 0);
	GM_OUT(MAC_ADDR_FILTER_MASK0, 0);
	for (i = 0; i < 27; ++i)
		GM_OUT(MAC_HASHTABLE + i, 0);
	GM_OUT(MACCNTL_CONFIG, 0);
	/* default to half duplex */
	GM_OUT(TXMAC_CONFIG, 0);
	GM_OUT(XIF_CONFIG, 5);
}

static void
gmac_init_rings(struct gmac *gm)
{
	int i;
	struct sk_buff *skb;
	unsigned char *data;
	struct gmac_dma_desc *ring;

	/* init rx ring */
	ring = (struct gmac_dma_desc *) gm->rxring;
	memset(ring, 0, NRX * sizeof(struct gmac_dma_desc));
	for (i = 0; i < NRX; ++i, ++ring) {
		data = dummy_buf;
		gm->rx_buff[i] = skb = dev_alloc_skb(RX_BUFLEN + 2);
		if (skb != 0) {
			/*skb_reserve(skb, 2);*/
			data = skb->data;
		}
		st_le32(&ring->address, virt_to_bus(data));
		st_le32(&ring->cmd, RX_OWN);
	}

	/* init tx ring */
	ring = (struct gmac_dma_desc *) gm->txring;
	memset(ring, 0, NRX * sizeof(struct gmac_dma_desc));

	/* set pointers in chip */
	mb();
	GM_OUT(RXDMA_BASE_HIGH, 0);
	GM_OUT(RXDMA_BASE_LOW, virt_to_bus(gm->rxring));
	GM_OUT(TXDMA_BASE_HIGH, 0);
	GM_OUT(TXDMA_BASE_LOW, virt_to_bus(gm->txring));
}

static void
gmac_start_dma(struct gmac *gm)
{
	GM_BIS(RXDMA_CONFIG, 1);
	GM_BIS(RXMAC_CONFIG, 1);
	GM_OUT(RXDMA_KICK, NRX);
	GM_BIS(TXDMA_CONFIG, 1);
	GM_BIS(TXMAC_CONFIG, 1);
}

static int gmac_open(struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;

	if (gmac_reset(dev))
		return -EIO;

	MOD_INC_USE_COUNT;

	powerup_transceiver(gm);
	gmac_mac_init(gm, dev->dev_addr);
	gmac_init_rings(gm);
	gmac_start_dma(gm);
	mii_interrupt(gm);

	GM_OUT(INTR_DISABLE, 0xfffdffe8);

	return 0;
}

static int gmac_close(struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;
	int i;

	mii_poll_stop(gm);
	
	GM_BIC(RXDMA_CONFIG, 1);
	GM_BIC(RXMAC_CONFIG, 1);
	GM_BIC(TXDMA_CONFIG, 1);
	GM_BIC(TXMAC_CONFIG, 1);
	GM_OUT(INTR_DISABLE, ~0U);
	for (i = 0; i < NRX; ++i) {
		if (gm->rx_buff[i] != 0) {
			dev_kfree_skb(gm->rx_buff[i]);
			gm->rx_buff[i] = 0;
		}
	}
	for (i = 0; i < NTX; ++i) {
		if (gm->tx_buff[i] != 0) {
			dev_kfree_skb(gm->tx_buff[i]);
			gm->tx_buff[i] = 0;
		}
	}

	MOD_DEC_USE_COUNT;
	return 0;
}

static int gmac_xmit_start(struct sk_buff *skb, struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;
	volatile struct gmac_dma_desc *dp;
	unsigned long flags;
	int i;

	save_flags(flags); cli();
	i = gm->next_tx;
	if (gm->tx_buff[i] != 0) {
		/* buffer is full, can't send this packet at the moment */
		netif_stop_queue(dev);
		gm->tx_full = 1;
		restore_flags(flags);
		return 1;
	}
	gm->next_tx = (i + 1) & (NTX - 1);
	gm->tx_buff[i] = skb;
	restore_flags(flags);

	dp = &gm->txring[i];
	dp->status = 0;
	dp->hi_addr = 0;
	st_le32(&dp->address, virt_to_bus(skb->data));
	mb();
	st_le32(&dp->cmd, TX_SOP | TX_EOP | skb->len);
	mb();

	GM_OUT(TXDMA_KICK, gm->next_tx);

	return 0;
}

static int gmac_tx_cleanup(struct gmac *gm)
{
	int i = gm->tx_gone;
	volatile struct gmac_dma_desc *dp;
	struct sk_buff *skb;
	int ret = 0;
	int gone = GM_IN(TXDMA_COMPLETE);

	while (i != gone) {
		skb = gm->tx_buff[i];
		if (skb == NULL)
			break;
		dp = &gm->txring[i];
		gm->stats.tx_bytes += skb->len;
		++gm->stats.tx_packets;
		gm->tx_buff[i] = NULL;
		dev_kfree_skb_irq(skb);
		if (++i >= NTX)
			i = 0;
	}
	if (i != gm->tx_gone) {
		ret = gm->tx_full;
		gm->tx_gone = i;
		gm->tx_full = 0;
	}
	return ret;
}

static void gmac_receive(struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;
	int i = gm->next_rx;
	volatile struct gmac_dma_desc *dp;
	struct sk_buff *skb;
	int len;
	unsigned char *data;

	for (;;) {
		dp = &gm->rxring[i];
		if (ld_le32(&dp->cmd) & RX_OWN)
			break;
		len = (ld_le32(&dp->cmd) >> 16) & 0x7fff;
		skb = gm->rx_buff[i];
		if (skb == 0) {
			++gm->stats.rx_dropped;
		} else if (ld_le32(&dp->status) & 0x40000000) {
			++gm->stats.rx_errors;
			dev_kfree_skb_irq(skb);
		} else {
			skb_put(skb, len);
			skb->dev = dev;
			skb->protocol = eth_type_trans(skb, dev);
			netif_rx(skb);
			gm->stats.rx_bytes += skb->len;
			++gm->stats.rx_packets;
		}
		data = dummy_buf;
		gm->rx_buff[i] = skb = dev_alloc_skb(RX_BUFLEN + 2);
		if (skb != 0) {
			/*skb_reserve(skb, 2);*/
			data = skb->data;
		}
		st_le32(&dp->address, virt_to_bus(data));
		dp->hi_addr = 0;
		mb();
		st_le32(&dp->cmd, RX_OWN);
		if (++i >= NRX)
			i = 0;
	}
	gm->next_rx = i;
}

static void gmac_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct net_device *dev = (struct net_device *) dev_id;
	struct gmac *gm = (struct gmac *) dev->priv;
	unsigned int status;

	status = GM_IN(INTR_STATUS);
	GM_OUT(INTR_ACK, status);
	
	if (status & GMAC_IRQ_MIF)
		mii_interrupt(gm);
	gmac_receive(dev);
	if (gmac_tx_cleanup(gm))
		netif_wake_queue(dev);
}

static struct net_device_stats *gmac_stats(struct net_device *dev)
{
	struct gmac *gm = (struct gmac *) dev->priv;

	return &gm->stats;
}

static int __init gmac_probe(void)
{
	struct device_node *gmac;

	/*
	 * We could (and maybe should) do this using PCI scanning
	 * for vendor/net_device ID 0x106b/0x21.
	 */
	for (gmac = find_compatible_devices("network", "gmac"); gmac != 0;
	     gmac = gmac->next)
		gmac_probe1(gmac);

	return 0;
}

static void gmac_probe1(struct device_node *gmac)
{
	struct gmac *gm;
	unsigned long descpage;
	unsigned char *addr;
	struct net_device *dev;
	int i;

	if (gmac->n_addrs < 1 || gmac->n_intrs < 1) {
		printk(KERN_ERR "can't use GMAC %s: %d addrs and %d intrs\n",
		       gmac->full_name, gmac->n_addrs, gmac->n_intrs);
		return;
	}

	addr = get_property(gmac, "local-mac-address", NULL);
	if (addr == NULL) {
		printk(KERN_ERR "Can't get mac-address for GMAC %s\n",
		       gmac->full_name);
		return;
	}

	descpage = get_free_page(GFP_KERNEL);
	if (descpage == 0) {
		printk(KERN_ERR "GMAC: can't get a page for descriptors\n");
		return;
	}

	dev = init_etherdev(0, sizeof(struct gmac));
	memset(dev->priv, 0, sizeof(struct gmac));

	gm = (struct gmac *) dev->priv;
	dev->base_addr = gmac->addrs[0].address;
	gm->regs = (volatile unsigned int *)
		ioremap(gmac->addrs[0].address, 0x10000);
	gm->sysregs = (volatile unsigned int *) ioremap(0xf8000000, 0x1000);
	dev->irq = gmac->intrs[0].line;

	printk(KERN_INFO "%s: GMAC at", dev->name);
	for (i = 0; i < 6; ++i) {
		dev->dev_addr[i] = addr[i];
		printk("%c%.2x", (i? ':': ' '), addr[i]);
	}
	printk("\n");

	gm->desc_page = descpage;
	gm->rxring = (volatile struct gmac_dma_desc *) descpage;
	gm->txring = (volatile struct gmac_dma_desc *) (descpage + 0x800);

	gm->phy_addr = 0;
	
	dev->open = gmac_open;
	dev->stop = gmac_close;
	dev->hard_start_xmit = gmac_xmit_start;
	dev->get_stats = gmac_stats;

	ether_setup(dev);

	if (request_irq(dev->irq, gmac_interrupt, 0, "GMAC", dev))
		printk(KERN_ERR "GMAC: can't get irq %d\n", dev->irq);

	gm->next_gmac = gmacs;
	gmacs = dev;
}

MODULE_AUTHOR("Paul Mackerras/Ben Herrenschmidt");
MODULE_DESCRIPTION("PowerMac GMAC driver.");

static void __exit gmac_cleanup_module(void)
{
	struct gmac *gm;
	struct net_device *dev;

	while ((dev = gmacs) != NULL) {
		gm = (struct gmac *) dev->priv;
		gmacs = gm->next_gmac;
		free_irq(dev->irq, dev);
		free_page(gm->desc_page);
		unregister_netdev(dev);
		kfree(dev);
	}
}

module_init(gmac_probe);
module_exit(gmac_cleanup_module);