2 * Acess2 PRO/100 Driver
3 * - By John Hodge (thePowersGang)
8 * Built with reference to the linux e100 driver (drivers/net/ethernet/intel/e100.c)
9 * 82559-fast-ethernet-multifunciton-pci-datasheet.pdf
13 #include <IPStack/include/adapters_api.h>
20 #define NUM_STATIC_CARDS 2
21 static const Uint16 caSupportedCards[][2] = {
22 {0x8086, 0x103D}, // prelude's card
23 {0x8086, 0x1209}, // qemu's i82559 emulation
25 static const int ciNumSupportedCards = sizeof(caSupportedCards)/sizeof(caSupportedCards[0]);
28 int PRO100_Install(char **Arguments);
29 int PRO100_InitCard(tCard *Card);
30 int PRO100_Cleanup(void);
31 tIPStackBuffer *PRO100_WaitForPacket(void *Ptr);
32 int PRO100_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
33 void PRO100_IRQHandler(int Num, void *Ptr);
35 Uint16 PRO100_int_ReadEEPROM(tCard *Card, Uint8 *AddrLen, size_t Ofs);
37 static void _Write8(tCard *Card, int Ofs, Uint8 Val);
38 static void _Write16(tCard *Card, int Ofs, Uint16 Val);
39 static void _Write32(tCard *Card, int Ofs, Uint32 Val);
40 static Uint8 _Read8(tCard *Card, int Ofs);
41 static Uint16 _Read16(tCard *Card, int Ofs);
42 //static Uint32 _Read32(tCard *Card, int Ofs);
43 static void _FlushWait(tCard *Card, int Delay);
46 MODULE_DEFINE(0, 0x100, PRO100, PRO100_Install, PRO100_Cleanup, "IPStack", NULL);
47 tIPStack_AdapterType gPRO100_AdapterType = {
49 .Type = ADAPTERTYPE_ETHERNET_100M,
51 .SendPacket = PRO100_SendPacket,
52 .WaitForPacket = PRO100_WaitForPacket
54 tCard gaPRO100_StaticCards[NUM_STATIC_CARDS];
57 int PRO100_Install(char **Arguments)
60 for( int i = 0; i < ciNumSupportedCards; i ++ )
62 const Uint16 *ven_dev = caSupportedCards[i];
63 LOG("Checking %04x:%04x: %i reported", ven_dev[0], ven_dev[1],
64 PCI_CountDevices(ven_dev[0], ven_dev[1]));
65 for( int idx = 0, pciid = -1; -1 != (pciid = PCI_GetDevice(ven_dev[0], ven_dev[1], idx)); idx++ )
67 Uint8 irq = PCI_GetIRQ(pciid);
68 Uint32 mmiobase = PCI_GetValidBAR(pciid, 0, PCI_BARTYPE_MEM32);
69 Uint16 iobase = PCI_GetValidBAR(pciid, 1, PCI_BARTYPE_IO);
70 LOG("IO Base = %x, MMIOBase = %x", iobase, mmiobase);
72 PCI_SetCommand(pciid, PCI_CMD_IOENABLE|PCI_CMD_BUSMASTER, 0);
75 if( cardidx < NUM_STATIC_CARDS ) {
76 card = &gaPRO100_StaticCards[cardidx++];
79 card = malloc(sizeof(tCard));
82 card->IOBase = iobase;
83 //card->MMIO = MM_MapHWPages(mmiobase, 1);
84 IRQ_AddHandler(irq, PRO100_IRQHandler, card);
87 PRO100_InitCard(card);
89 IPStack_Adapter_Add(&gPRO100_AdapterType, card, card->MAC.Bytes);
95 int PRO100_InitCard(tCard *Card)
97 // Initialise structures
98 Semaphore_Init(&Card->TXCommandSem, NUM_TX, NUM_TX, "PRO100", "Command Buffers");
99 Semaphore_Init(&Card->RXSemaphore, 0, NUM_RX, "PRO100", "Receive");
102 _Write32(Card, REG_Port, PORT_SELECTIVERESET);
103 _FlushWait(Card, 20); // - Write Flush, wait 20us
104 _Write32(Card, REG_Port, PORT_SOFTWARERESET);
105 _FlushWait(Card, 20); // - Write Flush, wait 20us
108 Uint8 addr_len = 8; // default to 8, updated on first read
109 Card->MAC.Words[0] = PRO100_int_ReadEEPROM(Card, &addr_len, 0);
110 Card->MAC.Words[1] = PRO100_int_ReadEEPROM(Card, &addr_len, 1);
111 Card->MAC.Words[2] = PRO100_int_ReadEEPROM(Card, &addr_len, 2);
113 // Set interrupt mask
114 _Write8(Card, REG_IntMask, 0);
116 // Prepare Command Unit
117 Card->TXCommands = MM_AllocDMA(1, 32, NULL);
118 Uint32 txbase = MM_GetPhysAddr(Card->TXCommands);
119 ASSERT(Card->TXCommands);
120 for( int i = 0; i < NUM_TX; i ++ )
122 tCommandUnit *cu = &Card->TXCommands[i].Desc.CU;
124 cu->Command = CMD_Nop|CMD_Suspend;
125 cu->Link = MM_GetPhysAddr(&Card->TXCommands[(i+1)%NUM_TX]) - txbase;
128 _Write32(Card, REG_GenPtr, txbase);
129 _Write16(Card, REG_Command, CU_CMD_BASE);
130 // Ensure CU is in suspend before we attempt sending
131 Card->LastTXIndex = 1;
132 Card->CurTXIndex = 1;
133 _Write32(Card, REG_GenPtr, 0);
134 _Write16(Card, REG_Command, CU_CMD_START);
136 // Create RX Descriptors
137 for( int i = 0; i < NUM_RX; i += 2 )
139 char *base = MM_AllocDMA(1, 32, NULL);
141 Card->RXBufs[i+0] = (void*)base;
142 Card->RXBufs[i+1] = (void*)(base + 0x800);
143 for( int j = 0; j < 2; j ++ )
145 tRXBuffer *rx = Card->RXBufs[i+j];
148 // Link is populated later
149 rx->Size = RX_BUF_SIZE;
150 rx->RXBufAddr = 0; // unused?
154 // NOTE: All `Link` values are relative to the RX base address
155 Uint32 rx_desc_phys = MM_GetPhysAddr(Card->RXBufs[0]);
156 for( int i = 0; i < NUM_RX-1; i ++ )
158 tRXBuffer *rx = Card->RXBufs[i];
159 rx->CU.Link = MM_GetPhysAddr(Card->RXBufs[i+1]) - rx_desc_phys;
161 Card->RXBufs[NUM_RX-1]->CU.Command = CMD_Suspend;
162 Card->RXBufs[NUM_RX-1]->CU.Link = 0; // link = 0, loop back
164 // Set RX Buffer base
165 _Write32(Card, REG_GenPtr, rx_desc_phys);
166 _Write16(Card, REG_Command, RX_CMD_ADDR_LOAD);
168 _Write32(Card, REG_GenPtr, 0);
169 _Write16(Card, REG_Command, RX_CMD_START);
174 int PRO100_Cleanup(void)
179 void PRO100_ReleaseRXBuf(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
182 tRXBuffer *buf = (tRXBuffer*)Data - 1;
185 for( idx = 0; idx < NUM_RX && Card->RXBufs[idx] != buf; idx ++ )
187 ASSERT(idx != NUM_RX);
189 tRXBuffer *prev = Card->RXBufs[ (idx-1+NUM_RX)%NUM_RX ];
192 prev->CU.Command &= ~CMD_Suspend;
195 _Write16(Card, REG_Command, RX_CMD_RESUME);
199 tIPStackBuffer *PRO100_WaitForPacket(void *Ptr)
204 Semaphore_Wait(&Card->RXSemaphore, 1);
205 } while( Card->RXBufs[Card->CurRXIndex]->CU.Status == 0 );
206 // Mark previous buffer as suspend (stops the RX unit running into old packets
207 Card->RXBufs[ (Card->CurRXIndex-1+NUM_RX)%NUM_RX ]->CU.Command |= CMD_Suspend;
208 tRXBuffer *buf = Card->RXBufs[Card->CurRXIndex++];
210 // Return packet (freed in PRO100_ReleaseRXBuf);
211 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(1);
212 // - actual data is just after the descriptor
213 IPStack_Buffer_AppendSubBuffer(ret, buf->Count, 0, buf+1, PRO100_ReleaseRXBuf, Card);
218 void PRO100_int_SetBuf(tTXCommand *TXC, int *IndexPtr, Uint32 Addr, Uint16 Len)
220 ASSERTC(*IndexPtr, <, NUM_LOCAL_TXBUFS);
222 tTXBufDesc *txb = &TXC->LocalBufs[*IndexPtr];
228 int PRO100_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
232 Semaphore_Wait(&Card->TXCommandSem, 1);
234 // Acquire a command buffer
235 Mutex_Acquire(&Card->lTXCommands);
236 int txc_idx = Card->CurTXIndex;
237 Card->CurTXIndex = (Card->CurTXIndex + 1) % NUM_TX;
238 Mutex_Release(&Card->lTXCommands);
239 tTXCommand *txc = &Card->TXCommands[txc_idx];
245 size_t total_size = 0;
247 while( (buf_idx = IPStack_Buffer_GetBuffer(Buffer, buf_idx, &len, &ptr)) != -1 )
250 if( MM_GetPhysAddr(ptr) >> 32 ) {
257 ASSERTC(len, <, PAGE_SIZE);
259 // Check if buffer split is required
260 if( MM_GetPhysAddr((char*)ptr + len-1) != MM_GetPhysAddr(ptr)+len-1 )
262 // Need to split buffer
263 size_t space = PAGE_SIZE - ((tVAddr)ptr % PAGE_SIZE);
264 PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr(ptr), space);
265 PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr((char*)ptr+space), len-space);
270 PRO100_int_SetBuf(txc, &txb_idx, MM_GetPhysAddr(ptr), len);
276 // Set buffer pointer
277 Card->TXBuffers[txc_idx] = Buffer;
279 txc->Desc.TBDArrayAddr = 0xFFFFFFFF;
280 txc->Desc.TCBBytes = total_size;
281 txc->Desc.TXThreshold = 0; // TODO: What does this do on RHW?
282 txc->Desc.TBDCount = 0;
283 txc->Desc.CU.Command = CMD_Suspend|CMD_Tx;
284 // - Mark previous as not suspended
285 Card->TXCommands[ (txc_idx-1+NUM_TX) % NUM_TX ].Desc.CU.Command &= ~CMD_Suspend;
287 IPStack_Buffer_LockBuffer(Buffer);
290 // - If currently running or idle, this should not matter
291 // NOTE: Qemu describes this behavior as 'broken'
292 _Write16(Card, REG_Command, CU_CMD_RESUME);
294 IPStack_Buffer_LockBuffer(Buffer);
295 IPStack_Buffer_UnlockBuffer(Buffer);
300 void PRO100_IRQHandler(int Num, void *Ptr)
303 Uint8 status = _Read8(Card, REG_Ack);
308 _Write8(Card, REG_Ack, status);
309 LOG("status = %x", status);
310 if( status & ISR_FR ) {
312 Semaphore_Signal(&Card->RXSemaphore, 1);
316 if( status & ISR_CNA )
318 // Chase the next command buffer
319 while( Card->LastTXIndex != Card->CurTXIndex )
321 int idx = Card->LastTXIndex++;
322 // Once we hit an incomplete command, stop
323 if( !(Card->TXCommands[idx].Desc.CU.Status & CU_Status_Complete) )
325 IPStack_Buffer_UnlockBuffer( Card->TXBuffers[idx] );
326 Semaphore_Signal(&Card->TXCommandSem, 1);
328 LOG("CU Idle (%i / %i)", Card->LastTXIndex, Card->CurTXIndex);
332 Uint16 PRO100_int_ReadEEPROM(tCard *Card, Uint8 *addr_len, size_t Ofs)
334 ASSERTC( *addr_len, <=, 12 );
336 Uint32 addr_data = ((EEPROM_OP_READ << *addr_len) | Ofs) << 16;
338 // Deslect chip (god knows what state it was left in)
339 _Write16( Card, REG_EEPROMCtrl, 0 );
340 _FlushWait(Card, 4); // Flush + 4us
342 _Write16( Card, REG_EEPROMCtrl, EEPROM_CTRL_CS | EEPROM_CTRL_SK );
343 _FlushWait(Card, 4); // Flush + 4us
347 // 2 preamble (0,1) + 2 command (read=1,0) + n address + 16 data
348 for( int i = (2+2+*addr_len+16); i --; )
350 Uint16 ctrl = EEPROM_CTRL_CS | ((addr_data & (1 << i)) ? EEPROM_CTRL_DI : 0);
351 _Write16( Card, REG_EEPROMCtrl, ctrl );
352 _FlushWait(Card, 4); // Flush + 4us
353 _Write16( Card, REG_EEPROMCtrl, ctrl | EEPROM_CTRL_SK );
354 _FlushWait(Card, 4); // Flush + 4us
356 ctrl = _Read16( Card, REG_EEPROMCtrl );
357 // Once the address is fully recieved, the card emits a zero bit
358 if( !(ctrl & EEPROM_CTRL_DO) && i > 16 )
360 *addr_len = *addr_len - (i - 16);
361 LOG("addr_len = %i", *addr_len);
366 data = (data << 1) | (ctrl & EEPROM_CTRL_DO ? 1 : 0);
370 _Write16( Card, REG_EEPROMCtrl, 0 );
371 _FlushWait(Card, 4); // Flush + 4us
373 LOG("Read %x from EEPROM ofs %i", data&0xFFFF, Ofs);
374 return (data & 0xFFFF);
377 static void _Write8(tCard *Card, int Ofs, Uint8 Val) {
378 //LOG("%p +%i := %02x", Card, Ofs, Val);
379 outb(Card->IOBase + Ofs, Val);
381 static void _Write16(tCard *Card, int Ofs, Uint16 Val) {
382 //LOG("%p +%i := %04x", Card, Ofs, Val);
383 ASSERT( !(Ofs & 1) );
384 outw(Card->IOBase + Ofs, Val);
386 static void _Write32(tCard *Card, int Ofs, Uint32 Val) {
387 //LOG("%p +%i := %08x", Card, Ofs, Val);
388 ASSERT( !(Ofs & 3) );
389 outd(Card->IOBase + Ofs, Val);
391 static Uint8 _Read8(tCard *Card, int Ofs) {
392 Uint8 rv = inb(Card->IOBase + Ofs);
393 //LOG("%p +%i == %02x", Card, Ofs, rv);
396 static Uint16 _Read16(tCard *Card, int Ofs) {
397 ASSERT( !(Ofs & 1) );
398 Uint16 rv = inw(Card->IOBase + Ofs);
399 //LOG("%p +%i == %04x", Card, Ofs, rv);
402 //static Uint32 _Read32(tCard *Card, int Ofs) { return ind(Card->IOBase + Ofs); }
404 static void _FlushWait(tCard *Card, int Delay)
406 _Read16( Card, REG_Status );
409 Time_MicroSleep(Delay);