summaryrefslogtreecommitdiffstats
path: root/src/drivers/net/natsemi.c
blob: 685b3986ddbfd7a2a372ad73c39289b1e273c48f (plain) (blame)
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* natsemi.c - gPXE driver for the NatSemi DP8381x series. 
 

*/

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <errno.h>
#include <timer.h>
#include <byteswap.h>
#include <gpxe/pci.h>
#include <gpxe/if_ether.h>
#include <gpxe/ethernet.h>
#include <gpxe/iobuf.h>
#include <gpxe/netdevice.h>
#include <gpxe/spi_bit.h>
#include <gpxe/threewire.h>
#include <gpxe/nvo.h>

#define TX_RING_SIZE 4
#define NUM_RX_DESC  4

struct natsemi_tx {
	uint32_t link;
	uint32_t cmdsts;
	uint32_t bufptr;
};

struct natsemi_rx {
	uint32_t link;
	uint32_t cmdsts;
	uint32_t bufptr;
};

struct natsemi_nic {
	unsigned short ioaddr;
	unsigned short tx_cur;
	unsigned short tx_dirty;
	unsigned short rx_cur;
	struct natsemi_tx tx[TX_RING_SIZE];
	struct natsemi_rx rx[NUM_RX_DESC];
	/* need to add iobuf as we cannot free iobuf->data in close without this 
	 * alternatively substracting sizeof(head) and sizeof(list_head) can also 
	 * give the same.*/
	struct io_buffer *iobuf[NUM_RX_DESC];
	struct spi_bit_basher spibit;
	struct spi_device eeprom;
	struct nvo_block nvo;
};

/* Tuning Parameters */
#define TX_FIFO_THRESH	256	/* In bytes, rounded down to 32 byte units. */
#define RX_FIFO_THRESH	4	/* Rx buffer level before first PCI xfer.  */
#define RX_DMA_BURST	4	/* Maximum PCI burst, '4' is 256 bytes */
#define TX_DMA_BURST	4	/* Calculate as 16<<val. */
#define TX_IPG		3	/* This is the only valid value */
//#define RX_BUF_LEN_IDX	0	/*  */
#define RX_BUF_LEN    8192   /*buffer size should be multiple of 32 */ 
#define RX_BUF_PAD 4
#define RX_BUF_SIZE 1536
#define OWN       0x80000000
#define DSIZE     0x00000FFF
#define CRC_SIZE  4

/* NATSEMI: Offsets to the device registers.
   Unlike software-only systems, device drivers interact with complex hardware.
   It's not useful to define symbolic names for every register bit in the
   device.
*/
enum register_offsets {
    ChipCmd      = 0x00, 
    ChipConfig   = 0x04, 
    EECtrl       = 0x08, 
    PCIBusCfg    = 0x0C,
    IntrStatus   = 0x10, 
    IntrMask     = 0x14, 
    IntrEnable   = 0x18,
    TxRingPtr    = 0x20, 
    TxConfig     = 0x24,
    RxRingPtr    = 0x30,
    RxConfig     = 0x34, 
    ClkRun       = 0x3C,
    WOLCmd       = 0x40, 
    PauseCmd     = 0x44,
    RxFilterAddr = 0x48, 
    RxFilterData = 0x4C,
    BootRomAddr  = 0x50, 
    BootRomData  = 0x54, 
    SiliconRev   = 0x58, 
    StatsCtrl    = 0x5C,
    StatsData    = 0x60, 
    RxPktErrs    = 0x60, 
    RxMissed     = 0x68, 
    RxCRCErrs    = 0x64,
    PCIPM        = 0x44,
    PhyStatus    = 0xC0, 
    MIntrCtrl    = 0xC4, 
    MIntrStatus  = 0xC8,

    /* These are from the spec, around page 78... on a separate table. */
    PGSEL        = 0xCC, 
    PMDCSR       = 0xE4, 
    TSTDAT       = 0xFC, 
    DSPCFG       = 0xF4, 
    SDCFG        = 0x8C,
    BasicControl = 0x80,	
    BasicStatus  = 0x84
	    
};




/* Bit in ChipCmd. */
enum ChipCmdBits {
    ChipReset = 0x100, 
    RxReset   = 0x20, 
    TxReset   = 0x10, 
    RxOff     = 0x08, 
    RxOn      = 0x04,
    TxOff     = 0x02, 
    TxOn      = 0x01
}


/* Bits in the RxMode register. */
enum rx_mode_bits {
    AcceptErr          = 0x20,
    AcceptRunt         = 0x10,
    AcceptBroadcast    = 0xC0000000,
    AcceptMulticast    = 0x00200000, 
    AcceptAllMulticast = 0x20000000,
    AcceptAllPhys      = 0x10000000, 
    AcceptMyPhys       = 0x08000000,
    RxFilterEnable     = 0x80000000
};

/* Bits in network_desc.status */
enum desc_status_bits {
    DescOwn   = 0x80000000, 
    DescMore  = 0x40000000, 
    DescIntr  = 0x20000000,
    DescNoCRC = 0x10000000,
    DescPktOK = 0x08000000, 
    RxTooLong = 0x00400000
};



/*  EEPROM access */
#define EE_M1		0x80	/* Mode select bit 1 */
#define EE_M0		0x40	/* Mode select bit 0 */
#define EE_CS		0x08	/* EEPROM chip select */
#define EE_SK		0x04	/* EEPROM shift clock */
#define EE_DI		0x02	/* Data in */
#define EE_DO		0x01	/* Data out */

/* Offsets within EEPROM (these are word offsets) */
#define EE_MAC 7

static uint32_t SavedClkRun;	



static const uint8_t rtl_ee_bits[] = {
	[SPI_BIT_SCLK]	= EE_SK,
	[SPI_BIT_MOSI]	= EE_DI,
	[SPI_BIT_MISO]	= EE_DO,
	[SPI_BIT_SS(0)]	= ( EE_CS | EE_M1 ),
};

static int rtl_spi_read_bit ( struct bit_basher *basher,
			      unsigned int bit_id ) {
	struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
						 spibit.basher );
	uint8_t mask = rtl_ee_bits[bit_id];
	uint8_t eereg;

	eereg = inb ( rtl->ioaddr + Cfg9346 );
	return ( eereg & mask );
}

static void rtl_spi_write_bit ( struct bit_basher *basher,
				unsigned int bit_id, unsigned long data ) {
	struct rtl8139_nic *rtl = container_of ( basher, struct rtl8139_nic,
						 spibit.basher );
	uint8_t mask = rtl_ee_bits[bit_id];
	uint8_t eereg;

	eereg = inb ( rtl->ioaddr + Cfg9346 );
	eereg &= ~mask;
	eereg |= ( data & mask );
	outb ( eereg, rtl->ioaddr + Cfg9346 );
}

static struct bit_basher_operations rtl_basher_ops = {
	.read = rtl_spi_read_bit,
	.write = rtl_spi_write_bit,
};

/** Portion of EEPROM available for non-volatile stored options
 *
 * We use offset 0x40 (i.e. address 0x20), length 0x40.  This block is
 * marked as VPD in the rtl8139 datasheets, so we use it only if we
 * detect that the card is not supporting VPD.
 */
static struct nvo_fragment rtl_nvo_fragments[] = {
	{ 0x20, 0x40 },
	{ 0, 0 }
};

/**
 * Set up for EEPROM access
 *
 * @v NAT		NATSEMI NIC
 */
 void rtl_init_eeprom ( struct natsemi_nic *rtl ) {
	int ee9356;
	int vpd;

	/* Initialise three-wire bus */
	rtl->spibit.basher.op = &rtl_basher_ops;
	rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
	init_spi_bit_basher ( &rtl->spibit );

	/* Detect EEPROM type and initialise three-wire device */
	ee9356 = ( inw ( rtl->ioaddr + RxConfig ) & Eeprom9356 );
	if ( ee9356 ) {
		DBG ( "EEPROM is an AT93C56\n" );
		init_at93c56 ( &rtl->eeprom, 16 );
	} else {
		DBG ( "EEPROM is an AT93C46\n" );
		init_at93c46 ( &rtl->eeprom, 16 );
	}
	rtl->eeprom.bus = &rtl->spibit.bus;

	/* Initialise space for non-volatile options, if available */
	vpd = ( inw ( rtl->ioaddr + Config1 ) & VPDEnable );
	if ( vpd ) {
		DBG ( "EEPROM in use for VPD; cannot use for options\n" );
	} else {
		rtl->nvo.nvs = &rtl->eeprom.nvs;
		rtl->nvo.fragments = rtl_nvo_fragments;
	}
}

/**
 * Reset NIC
 *
 * @v		NATSEMI NIC
 *
 * Issues a hardware reset and waits for the reset to complete.
 */
static void nat_reset ( struct nat_nic *nat ) {

	int i;
	/* Reset chip */
	outb ( ChipReset, nat->ioaddr + ChipCmd );
	mdelay ( 10 );
	nat->tx_dirty=0;
	nat->tx_cur=0;
	for(i=0;i<TX_RING_SIZE;i++)
	{
		nat->tx[i].link=0;
		nat->tx[i].cmdsts=0;
		nat->tx[i].bufptr=0;
	}
	nat->rx_cur = 0;
	outl(virt_to_bus(&nat->tx[0]),nat->ioaddr+TxRingPtr);
	outl(virt_to_bus(&nat->rx[0]), nat->ioaddr + RxRingPtr);

	outl(TxOff|RxOff, nat->ioaddr + ChipCmd);

	/* Restore PME enable bit */
	outl(SavedClkRun, nat->ioaddr + ClkRun);
}

/**
 * Open NIC
 *
 * @v netdev		Net device
 * @ret rc		Return status code
 */
static int nat_open ( struct net_device *netdev ) {
	struct natsemi_nic *nat = netdev->priv;
	//struct io_buffer *iobuf;
	int i;
	
	/* Disable PME:
        * The PME bit is initialized from the EEPROM contents.
        * PCI cards probably have PME disabled, but motherboard
        * implementations may have PME set to enable WakeOnLan. 
        * With PME set the chip will scan incoming packets but
        * nothing will be written to memory. */
        SavedClkRun = inl(nat->ioaddr + ClkRun);
        outl(SavedClkRun & ~0x100, nat->ioaddr + ClkRun);

		


	/* Program the MAC address TODO enable this comment */
	/*
	 for ( i = 0 ; i < ETH_ALEN ; i++ )
		outb ( netdev->ll_addr[i], rtl->ioaddr + MAC0 + i );
        */


	/*Set up the Tx Ring */
	nat->tx_cur=0;
	nat->tx_dirty=0;
	for (i=0;i<TX_RING_SIZE;i++)
	{
		nat->tx[i].link   = virt_to_bus((i+1 < TX_RING_SIZE) ? &nat->tx[i+1] : &nat->tx[0]);
		nat->tx[i].cmdsts = 0;
		nat->tx[i].bufptr = 0;
	}





	/* Set up RX ring */
	nat->rx_cur=0;
	for (i=0;i<NUM_RX_DESC;i++)
	{

		nat->iobuf[i] = alloc_iob ( RX_BUF_SIZE );
		if (!nat->iobuf[i])
		       return -ENOMEM;	
		nat->rx[i].link   = virt_to_bus((i+1 < NUM_RX_DESC) ? &nat->rx[i+1] : &nat->rx[0]);
		nat->rx[i].cmdsts = (uint32_t) RX_BUF_SIZE;
		nat->rx[i].bufptr = virt_to_bus(nat->iobuf[i]->data);
	}


	 /* load Receive Descriptor Register */
	outl(virt_to_bus(&nat->rx[0]), nat->ioaddr + RxRingPtr);
	DBG("Natsemi Rx descriptor loaded with: %X\n",inl(nat->ioaddr+RingPtr));		

	/* setup Tx ring */
	outl(virt_to_bus(&nat->tx[0]),nat->ioaddr+TxRingPtr);
	DBG("Natsemi Tx descriptor loaded with: %X\n",inl(nat->ioaddr+TxRingPtr));

	/* Enables RX */
	outl(RxFilterEnable|AcceptBroadcast|AcceptAllMulticast|AcceptMyPhys, nat->ioaddr+RxFilterAddr);

	/* Initialize other registers. */
	/* Configure the PCI bus bursts and FIFO thresholds. */
	/* Configure for standard, in-spec Ethernet. */
	if (inl(nat->ioaddr + ChipConfig) & 0x20000000) {	/* Full duplex */
		tx_config = 0xD0801002;
		rx_config = 0x10000020;
	} else {
		tx_config = 0x10801002;
		rx_config = 0x0020;
	}
	outl(tx_config, nat->ioaddr + TxConfig);
	outl(rx_config, nat->ioaddr + RxConfig);



	/*start the receiver  */
        outl(RxOn, nat->ioaddr + ChipCmd);


	return 0;
}

/**
 * Close NIC
 *
 * @v netdev		Net device
 */
static void nat_close ( struct net_device *netdev ) {
	struct natsemi_nic *nat = netdev->priv;

	/* Reset the hardware to disable everything in one go */
	nat_reset ( nat );

	/* Free RX ring */
	for (i=0;i<NUM_RX_DESC;i++)
	{
		
		free_iob( nat->iobuf[i] );
	}
}

/** 
 * Transmit packet
 *
 * @v netdev	Network device
 * @v iobuf	I/O buffer
 * @ret rc	Return status code
 */
static int natsemi_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
	struct natsemi_nic *nat = netdev->priv;

       /* check for space in TX ring */

	if (nat->tx[nat->tx_cur].cmdsts !=0)
	{
		printf ( "TX overflow\n" );
		return -ENOBUFS;
	}


	/* Pad and align packet */
	iob_pad ( iobuf, ETH_ZLEN );

	/* Add to TX ring */
	DBG ( "TX id %d at %lx+%x\n", nat->tx_cur,
	      virt_to_bus ( iobuf->data ), iob_len ( iobuf ) );

	nat->tx[nat->tx_cur].bufptr = virt_to_bus(iobuf->data);
	nat->tx[nat->tx_cur].cmdsts= (uint32_t) iob_len(iobuf)|OWN;

	nat->tx_cur=(nat->tx_cur+1) % TX_RING_SIZE;

	/*start the transmitter  */
        outl(TxOn, nat->ioaddr + ChipCmd);

	return 0;
}

/** 
 * Poll for received packets
 *
 * @v netdev	Network device
 * @v rx_quota	Maximum number of packets to receive
 */
static void nat_poll ( struct net_device *netdev, unsigned int rx_quota ) {
	struct natsemi_nic *nat = netdev->priv;
	unsigned int status;
	unsigned int rx_status;
	unsigned int rx_len;
	struct io_buffer *rx_iob;
	int i;

	
	/* check the status of packets given to card for transmission */	
	for ( i = 0 ; i < TX_RING_SIZE ; i++ ) 
	{
		status=bus_to_virt(nat->tx[nat->tx_dirty].cmdsts);
		/* check if current packet has been transmitted or not */
		if(status & own) 
			break;
		/* Check if any errors in transmission */
		if (! (status & DescPktOK))
		{
			printf("Error in sending Packet with data: %s\n and status:%X\n",
					bus_to_virt(nat->tx[nat->tx_dirty].bufptr),
					status);
		}
		else
		{
			DBG("Success in transmitting Packet with data: %s",
					bus_to_virt(nat->tx[nat->tx_dirty].bufptr));
		}
		/* setting cmdsts zero, indicating that it can be reused */
		nat->tx[nat->tx_dirty].cmdsts=0;
		nat->tx_dirty=(nat->tx_dirty +1) % TX_RING_SIZE;
	}
			
	
	rx_status=bus_to_virt(nat->rx[nat->rx_cur].cmdsts); 
	/* Handle received packets */
	while (rx_quota && (rx_status & OWN))
	{
		rx_len= (rx_status & DSIZE) - CRC_SIZE;

		/*check for the corrupt packet */
		if((rx_status & (DescMore|DescPktOK|RxTooLong)) != DescPktOK)
		{
			 printf("natsemi_poll: Corrupted packet received, 
					 buffer status = %X\n",rx_status);
		}
		else
		{
			rx_iob = alloc_iob(rx_len);
			if(!rx_iob) 
				/* leave packet for next call to poll*/
				return;
			memcpy(iob_put(rx_iob,rx_len),
					nat->rx[nat->rx_cur].bufptr,rxlen);
			/* add to the receive queue. */
			netdev_rx(netdev,rx_iob);
			rx_quota--;
		}
		nat->rx[nat->rx_cur].cmdsts = RX_BUF_SIZE;
		nat->rx_cur=(nat->rx_cur+1) % NUM_RX_DESC;
	}


	 /* re-enable the potentially idle receive state machine */
	    outl(RxOn, ioaddr + ChipCmd);	
}				






/**
 * Probe PCI device
 *
 * @v pci	PCI device
 * @v id	PCI ID
 * @ret rc	Return status code
 */
static int nat_probe ( struct pci_device *pci,
		       const struct pci_device_id *id __unused ) {
	struct net_device *netdev;
	struct natsemi_nic *nat = NULL;
	int registered_netdev = 0;
	int rc;
	uint32_t advertising;

	/* Fix up PCI device */
	adjust_pci_device ( pci );

	/* Allocate net device */
	netdev = alloc_etherdev ( sizeof ( *nat ) );
	if ( ! netdev ) {
		rc = -ENOMEM;
		goto err;
	}
	nat = netdev->priv;
	pci_set_drvdata ( pci, netdev );
	netdev->dev = &pci->dev;
	memset ( nat, 0, sizeof ( *nat ) );
	nat->ioaddr = pci->ioaddr;

	/* Reset the NIC, set up EEPROM access and read MAC address */
	nat_reset ( nat );
	/* commenitng two line below. Have to be included in final natsemi.c TODO*/
	/*
	nat_init_eeprom ( rtl );
	nvs_read ( &nat->eeprom.nvs, EE_MAC, netdev->ll_addr, ETH_ALEN );
	
	*/
	

	/* mdio routine of etherboot-5.4.0 natsemi driver has been removed and 
	 * statement to read from MII transceiver control section is used directly
	 */

        advertising = inl(nat->ioaddr + 0x80 + (4<<2)) & 0xffff; 
        {
	   	uint32_t chip_config = inl(ioaddr + ChipConfig);
		DBG("%s: Transceiver default autoneg. %s 10 %s %s duplex.\n",
	      	pci->driver_name,
	        chip_config & 0x2000 ? "enabled, advertise" : "disabled, force",
	        chip_config & 0x4000 ? "0" : "",
	        chip_config & 0x8000 ? "full" : "half");
    	}
	DBG("%s: Transceiver status %hX advertising %hX\n",pci->driver_name, (int)inl(nat->ioaddr + 0x84), advertising);





	/* Point to NIC specific routines */
	netdev->open	 = nat_open;
	netdev->close	 = nat_close;
	netdev->transmit = nat_transmit;
	netdev->poll	 = nat_poll;

	/* Register network device */
	if ( ( rc = register_netdev ( netdev ) ) != 0 )
		goto err;
	registered_netdev = 1;

	/* Register non-volatile storagei
	 * uncomment lines below in final version*/
	/*
	 if ( rtl->nvo.nvs ) {
		if ( ( rc = nvo_register ( &rtl->nvo ) ) != 0 )
			goto err;
	}
	*/

	return 0;

 err:
	/* Disable NIC */
	if ( nat )
		nat_reset ( nat );
	if ( registered_netdev )
		unregister_netdev ( netdev );
	/* Free net device */
	free_netdev ( netdev );
	return rc;
}

/**
 * Remove PCI device
 *
 * @v pci	PCI device
 */
static void nat_remove ( struct pci_device *pci ) {
	struct net_device *netdev = pci_get_drvdata ( pci );
	struct natsemi_nic *nat = netdev->priv;
/* TODO 
	if ( rtl->nvo.nvs )
		nvo_unregister ( &rtl->nvo );
		*/
	unregister_netdev ( netdev );
	nat_reset ( nat );
	free_netdev ( netdev );
}

static struct pci_device_id natsemi_nics[] = {
	PCI_ROM(0x100b, 0x0020, "dp83815", "DP83815"),

};

struct pci_driver natsemi_driver __pci_driver = {
	.ids = natsemi_nics,
	.id_count = ( sizeof ( natsemi_nics ) / sizeof ( natsemi_nics[0] ) ),
	.probe = nat_probe,
	.remove = nat_remove,
};