3 * Copyright (c) 2012-2014, ARM Limited. All rights reserved.
5 * This program and the accompanying materials
6 * are licensed and made available under the terms and conditions of the BSD License
7 * which accompanies this distribution. The full text of the license may be found at
8 * http://opensource.org/licenses/bsd-license.php
10 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Lan9118Dxe.h"
17 STATIC EFI_MAC_ADDRESS mZeroMac
= { { 0 } };
20 This internal function reverses bits for 32bit data.
22 @param Value The data to be reversed.
24 @return Data reversed.
36 for (Index
= 0; Index
< 32; Index
++) {
37 if ((Value
& (1 << Index
)) != 0) {
38 NewValue
= NewValue
| (1 << (31 - Index
));
46 ** Create Ethernet CRC
49 ** 1: http://en.wikipedia.org/wiki/Cyclic_redundancy_check
51 ** 2: http://www.erg.abdn.ac.uk/~gorry/eg3567/dl-pages/crc.html
53 ** 3: http://en.wikipedia.org/wiki/Computation_of_CRC
57 IN EFI_MAC_ADDRESS
*Mac
,
66 Remainder
= 0xFFFFFFFF; // 0xFFFFFFFF is standard seed for Ethernet
68 // Convert Mac Address to array of bytes
71 // Generate the Crc bit-by-bit (LSB first)
74 for (Iter
= 0;Iter
< 8;Iter
++) {
75 // Check if exponent is set
77 Remainder
= (Remainder
>> 1) ^ CRC_POLYNOMIAL
;
79 Remainder
= (Remainder
>> 1) ^ 0;
84 // Reverse the bits before returning (to Big Endian)
85 //TODO: Need to be reviewed. Do we want to do a bit reverse or a byte reverse (in this case use SwapBytes32())
86 return ReverseBits (Remainder
);
89 // Function to read from MAC indirect registers
97 // Check index is in the range
100 // Wait until CSR busy bit is cleared
101 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD
) & MAC_CSR_BUSY
) == MAC_CSR_BUSY
);
103 // Set CSR busy bit to ensure read will occur
104 // Set the R/W bit to indicate we are reading
105 // Set the index of CSR Address to access desired register
106 MacCSR
= MAC_CSR_BUSY
| MAC_CSR_READ
| MAC_CSR_ADDR(Index
);
108 // Write to the register
109 Lan9118MmioWrite32 (LAN9118_MAC_CSR_CMD
, MacCSR
);
111 // Wait until CSR busy bit is cleared
112 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD
) & MAC_CSR_BUSY
) == MAC_CSR_BUSY
);
114 // Now read from data register to get read value
115 return Lan9118MmioRead32 (LAN9118_MAC_CSR_DATA
);
119 * LAN9118 chips have special restrictions on some back-to-back Write/Read or
120 * Read/Read pairs of accesses. After a read or write that changes the state of
121 * the device, there is a period in which stale values may be returned in
122 * response to a read. This period is dependent on the registers accessed.
124 * We must delay prior reads by this period. This can either be achieved by
125 * timer-based delays, or by performing dummy reads of the BYTE_TEST register,
126 * for which the recommended number of reads is described in the LAN9118 data
127 * sheet. This is required in addition to any memory barriers.
129 * This function performs a number of dummy reads of the BYTE_TEST register, as
130 * a building block for the above.
138 MmioRead32(LAN9118_BYTE_TEST
);
142 Lan9118RawMmioRead32(
149 Value
= MmioRead32(Address
);
150 WaitDummyReads(Delay
);
155 Lan9118RawMmioWrite32(
161 MmioWrite32(Address
, Value
);
162 WaitDummyReads(Delay
);
166 // Function to write to MAC indirect registers
176 // Check index is in the range
179 // Wait until CSR busy bit is cleared
180 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD
) & MAC_CSR_BUSY
) == MAC_CSR_BUSY
);
182 // Set CSR busy bit to ensure read will occur
183 // Set the R/W bit to indicate we are writing
184 // Set the index of CSR Address to access desired register
185 MacCSR
= MAC_CSR_BUSY
| MAC_CSR_WRITE
| MAC_CSR_ADDR(Index
);
187 // Now write the value to the register before issuing the write command
188 ValueWritten
= Lan9118MmioWrite32 (LAN9118_MAC_CSR_DATA
, Value
);
190 // Write the config to the register
191 Lan9118MmioWrite32 (LAN9118_MAC_CSR_CMD
, MacCSR
);
193 // Wait until CSR busy bit is cleared
194 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD
) & MAC_CSR_BUSY
) == MAC_CSR_BUSY
);
199 // Function to read from MII register (PHY Access)
208 // Check it is a valid index
211 // Wait for busy bit to clear
212 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC
) & MII_ACC_MII_BUSY
) == MII_ACC_MII_BUSY
);
214 // Clear the R/W bit to indicate we are reading
215 // Set the index of the MII register
216 // Set the PHY Address
217 // Set the MII busy bit to allow read
218 MiiAcc
= MII_ACC_MII_READ
| MII_ACC_MII_REG_INDEX(Index
) | MII_ACC_PHY_VALUE
| MII_ACC_MII_BUSY
;
220 // Now write this config to register
221 IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_ACC
, MiiAcc
& 0xFFFF);
223 // Wait for busy bit to clear
224 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC
) & MII_ACC_MII_BUSY
) == MII_ACC_MII_BUSY
);
226 // Now read the value of the register
227 ValueRead
= (IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_DATA
) & 0xFFFF); // only lower 16 bits are valid for any PHY register
233 // Function to write to the MII register (PHY Access)
243 // Check it is a valid index
246 // Wait for busy bit to clear
247 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC
) & MII_ACC_MII_BUSY
) == MII_ACC_MII_BUSY
);
249 // Clear the R/W bit to indicate we are reading
250 // Set the index of the MII register
251 // Set the PHY Address
252 // Set the MII busy bit to allow read
253 MiiAcc
= MII_ACC_MII_WRITE
| MII_ACC_MII_REG_INDEX(Index
) | MII_ACC_PHY_VALUE
| MII_ACC_MII_BUSY
;
255 // Write the desired value to the register first
256 ValueWritten
= IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_DATA
, (Value
& 0xFFFF));
258 // Now write the config to register
259 IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_ACC
, MiiAcc
& 0xFFFF);
261 // Wait for operation to terminate
262 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC
) & MII_ACC_MII_BUSY
) == MII_ACC_MII_BUSY
);
268 /* ---------------- EEPROM Operations ------------------ */
271 // Function to read from EEPROM memory
273 IndirectEEPROMRead32 (
279 // Set the busy bit to ensure read will occur
280 EepromCmd
= E2P_EPC_BUSY
| E2P_EPC_CMD_READ
;
282 // Set the index to access desired EEPROM memory location
283 EepromCmd
|= E2P_EPC_ADDRESS(Index
);
285 // Write to Eeprom command register
286 Lan9118MmioWrite32 (LAN9118_E2P_CMD
, EepromCmd
);
287 gBS
->Stall (LAN9118_STALL
);
289 // Wait until operation has completed
290 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
292 // Check that operation didn't time out
293 if (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_TIMEOUT
) {
294 DEBUG ((EFI_D_ERROR
, "EEPROM Operation Timed out: Read command on index %x\n",Index
));
298 // Wait until operation has completed
299 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
301 // Finally read the value
302 return Lan9118MmioRead32 (LAN9118_E2P_DATA
);
305 // Function to write to EEPROM memory
307 IndirectEEPROMWrite32 (
317 // Read the EEPROM Command register
318 EepromCmd
= Lan9118MmioRead32 (LAN9118_E2P_CMD
);
320 // Set the busy bit to ensure read will occur
321 EepromCmd
|= ((UINT32
)1 << 31);
323 // Set the EEPROM command to write(0b011)
324 EepromCmd
&= ~(7 << 28); // Clear the command first
325 EepromCmd
|= (3 << 28); // Write 011
327 // Set the index to access desired EEPROM memory location
328 EepromCmd
|= (Index
& 0xF);
330 // Write the value to the data register first
331 ValueWritten
= Lan9118MmioWrite32 (LAN9118_E2P_DATA
, Value
);
333 // Write to Eeprom command register
334 Lan9118MmioWrite32 (LAN9118_E2P_CMD
, EepromCmd
);
335 gBS
->Stall (LAN9118_STALL
);
337 // Wait until operation has completed
338 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
340 // Check that operation didn't time out
341 if (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_TIMEOUT
) {
342 DEBUG ((EFI_D_ERROR
, "EEPROM Operation Timed out: Write command at memloc 0x%x, with value 0x%x\n",Index
, Value
));
346 // Wait until operation has completed
347 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
352 /* ---------------- General Operations ----------------- */
355 Lan9118SetMacAddress (
356 EFI_MAC_ADDRESS
*Mac
,
357 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
360 IndirectMACWrite32 (INDIRECT_MAC_INDEX_ADDRL
,
361 (Mac
->Addr
[0] & 0xFF) |
362 ((Mac
->Addr
[1] & 0xFF) << 8) |
363 ((Mac
->Addr
[2] & 0xFF) << 16) |
364 ((Mac
->Addr
[3] & 0xFF) << 24)
367 IndirectMACWrite32 (INDIRECT_MAC_INDEX_ADDRH
,
368 (UINT32
)(Mac
->Addr
[4] & 0xFF) |
369 ((Mac
->Addr
[5] & 0xFF) << 8)
374 Lan9118ReadMacAddress (
375 OUT EFI_MAC_ADDRESS
*MacAddress
378 UINT32 MacAddrHighValue
;
379 UINT32 MacAddrLowValue
;
381 // Read the Mac Addr high register
382 MacAddrHighValue
= (IndirectMACRead32 (INDIRECT_MAC_INDEX_ADDRH
) & 0xFFFF);
383 // Read the Mac Addr low register
384 MacAddrLowValue
= IndirectMACRead32 (INDIRECT_MAC_INDEX_ADDRL
);
386 SetMem (MacAddress
, sizeof(*MacAddress
), 0);
387 MacAddress
->Addr
[0] = (MacAddrLowValue
& 0xFF);
388 MacAddress
->Addr
[1] = (MacAddrLowValue
& 0xFF00) >> 8;
389 MacAddress
->Addr
[2] = (MacAddrLowValue
& 0xFF0000) >> 16;
390 MacAddress
->Addr
[3] = (MacAddrLowValue
& 0xFF000000) >> 24;
391 MacAddress
->Addr
[4] = (MacAddrHighValue
& 0xFF);
392 MacAddress
->Addr
[5] = (MacAddrHighValue
& 0xFF00) >> 8;
396 * Power up the 9118 and find its MAC address.
398 * This operation can be carried out when the LAN9118 is in any power state
403 IN EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
407 UINT64 DefaultMacAddress
;
409 // Attempt to wake-up the device if it is in a lower power state
410 if (((Lan9118MmioRead32 (LAN9118_PMT_CTRL
) & MPTCTRL_PM_MODE_MASK
) >> 12) != 0) {
411 DEBUG ((DEBUG_NET
, "Waking from reduced power state.\n"));
412 Lan9118MmioWrite32 (LAN9118_BYTE_TEST
, 0xFFFFFFFF);
413 gBS
->Stall (LAN9118_STALL
);
416 // Check that device is active
418 while ((Lan9118MmioRead32 (LAN9118_PMT_CTRL
) & MPTCTRL_READY
) == 0 && --Retries
) {
419 gBS
->Stall (LAN9118_STALL
);
425 // Check that EEPROM isn't active
427 while ((Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
) && --Retries
){
428 gBS
->Stall (LAN9118_STALL
);
434 // Check if a MAC address was loaded from EEPROM, and if it was, set it as the
436 if ((Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_MAC_ADDRESS_LOADED
) == 0) {
437 DEBUG ((EFI_D_ERROR
, "Warning: There was an error detecting EEPROM or loading the MAC Address.\n"));
439 // If we had an address before (set by StationAddess), continue to use it
440 if (CompareMem (&Snp
->Mode
->CurrentAddress
, &mZeroMac
, NET_ETHER_ADDR_LEN
)) {
441 Lan9118SetMacAddress (&Snp
->Mode
->CurrentAddress
, Snp
);
443 // If there are no cached addresses, then fall back to a default
444 DEBUG ((EFI_D_WARN
, "Warning: using driver-default MAC address\n"));
445 DefaultMacAddress
= FixedPcdGet64 (PcdLan9118DefaultMacAddress
);
446 Lan9118SetMacAddress((EFI_MAC_ADDRESS
*) &DefaultMacAddress
, Snp
);
447 CopyMem (&Snp
->Mode
->CurrentAddress
, &DefaultMacAddress
, NET_ETHER_ADDR_LEN
);
450 // Store the MAC address that was loaded from EEPROM
451 Lan9118ReadMacAddress (&Snp
->Mode
->CurrentAddress
);
452 CopyMem (&Snp
->Mode
->PermanentAddress
, &Snp
->Mode
->CurrentAddress
, NET_ETHER_ADDR_LEN
);
455 // Clear and acknowledge interrupts
456 Lan9118MmioWrite32 (LAN9118_INT_EN
, 0);
457 Lan9118MmioWrite32 (LAN9118_IRQ_CFG
, 0);
458 Lan9118MmioWrite32 (LAN9118_INT_STS
, 0xFFFFFFFF);
460 // Do self tests here?
466 // Perform software reset on the LAN9118
467 // Return 0 on success, -1 on error
471 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
477 // Initialize variable
481 StopTx (STOP_TX_MAC
| STOP_TX_CFG
| STOP_TX_CLEAR
, Snp
);
482 StopRx (STOP_RX_CLEAR
, Snp
); // Clear receiver FIFO
485 HwConf
= Lan9118MmioRead32 (LAN9118_HW_CFG
);
488 // Set the Must Be One (MBO) bit
489 if (((HwConf
& HWCFG_MBO
) >> 20) == 0) {
493 // Check that EEPROM isn't active
494 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
496 // Write the configuration
497 Lan9118MmioWrite32 (LAN9118_HW_CFG
, HwConf
);
498 gBS
->Stall (LAN9118_STALL
);
500 // Wait for reset to complete
501 while (Lan9118MmioRead32 (LAN9118_HW_CFG
) & HWCFG_SRST
) {
503 gBS
->Stall (LAN9118_STALL
);
506 // If time taken exceeds 100us, then there was an error condition
507 if (ResetTime
> 1000) {
508 Snp
->Mode
->State
= EfiSimpleNetworkStopped
;
513 // Check that EEPROM isn't active
514 while (Lan9118MmioRead32 (LAN9118_E2P_CMD
) & E2P_EPC_BUSY
);
516 // TODO we probably need to re-set the mac address here.
518 // Clear and acknowledge all interrupts
519 if (Flags
& SOFT_RESET_CLEAR_INT
) {
520 Lan9118MmioWrite32 (LAN9118_INT_EN
, 0);
521 Lan9118MmioWrite32 (LAN9118_IRQ_CFG
, 0);
522 Lan9118MmioWrite32 (LAN9118_INT_STS
, 0xFFFFFFFF);
525 // Do self tests here?
526 if (Flags
& SOFT_RESET_SELF_TEST
) {
534 // Perform PHY software reset
538 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
543 // PMT PHY reset takes precedence over BCR
544 if (Flags
& PHY_RESET_PMT
) {
545 PmtCtrl
= Lan9118MmioRead32 (LAN9118_PMT_CTRL
);
546 PmtCtrl
|= MPTCTRL_PHY_RST
;
547 Lan9118MmioWrite32 (LAN9118_PMT_CTRL
,PmtCtrl
);
549 // Wait for completion
550 while (Lan9118MmioRead32 (LAN9118_PMT_CTRL
) & MPTCTRL_PHY_RST
) {
551 gBS
->Stall (LAN9118_STALL
);
553 // PHY Basic Control Register reset
554 } else if (Flags
& PHY_RESET_BCR
) {
555 IndirectPHYWrite32 (PHY_INDEX_BASIC_CTRL
, PHYCR_RESET
);
557 // Wait for completion
558 while (IndirectPHYRead32 (PHY_INDEX_BASIC_CTRL
) & PHYCR_RESET
) {
559 gBS
->Stall (LAN9118_STALL
);
563 // Clear and acknowledge all interrupts
564 if (Flags
& PHY_SOFT_RESET_CLEAR_INT
) {
565 Lan9118MmioWrite32 (LAN9118_INT_EN
, 0);
566 Lan9118MmioWrite32 (LAN9118_IRQ_CFG
, 0);
567 Lan9118MmioWrite32 (LAN9118_INT_STS
, 0xFFFFFFFF);
574 // Configure hardware for LAN9118
578 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
583 // Check if we want to use LEDs on GPIO
584 if (Flags
& HW_CONF_USE_LEDS
) {
585 GpioConf
= Lan9118MmioRead32 (LAN9118_GPIO_CFG
);
587 // Enable GPIO as LEDs and Config as Push-Pull driver
588 GpioConf
|= GPIO_GPIO0_PUSH_PULL
| GPIO_GPIO1_PUSH_PULL
| GPIO_GPIO2_PUSH_PULL
|
589 GPIO_LED1_ENABLE
| GPIO_LED2_ENABLE
| GPIO_LED3_ENABLE
;
591 // Write the configuration
592 Lan9118MmioWrite32 (LAN9118_GPIO_CFG
, GpioConf
);
593 gBS
->Stall (LAN9118_STALL
);
599 // Configure flow control
606 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
612 // Do auto-negotiation
616 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
624 // First check that auto-negotiation is supported
625 PhyStatus
= IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS
);
626 if ((PhyStatus
& PHYSTS_AUTO_CAP
) == 0) {
627 DEBUG ((EFI_D_ERROR
, "Auto-negotiation not supported.\n"));
628 return EFI_DEVICE_ERROR
;
631 // Check that link is up first
632 if ((PhyStatus
& PHYSTS_LINK_STS
) == 0) {
633 // Wait until it is up or until Time Out
634 Retries
= FixedPcdGet32 (PcdLan9118DefaultNegotiationTimeout
) / LAN9118_STALL
;
635 while ((IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS
) & PHYSTS_LINK_STS
) == 0) {
636 gBS
->Stall (LAN9118_STALL
);
639 DEBUG ((EFI_D_ERROR
, "Link timeout in auto-negotiation.\n"));
645 // Configure features to advertise
646 Features
= IndirectPHYRead32 (PHY_INDEX_AUTO_NEG_ADVERT
);
648 if ((Flags
& AUTO_NEGOTIATE_ADVERTISE_ALL
) > 0) {
649 // Link speed capabilities
650 Features
|= (PHYANA_10BASET
| PHYANA_10BASETFD
| PHYANA_100BASETX
| PHYANA_100BASETXFD
);
652 // Pause frame capabilities
653 Features
&= ~(PHYANA_PAUSE_OP_MASK
);
657 // Write the features
658 IndirectPHYWrite32 (PHY_INDEX_AUTO_NEG_ADVERT
, Features
);
660 // Read control register
661 PhyControl
= IndirectPHYRead32 (PHY_INDEX_BASIC_CTRL
);
663 // Enable Auto-Negotiation
664 if ((PhyControl
& PHYCR_AUTO_EN
) == 0) {
665 PhyControl
|= PHYCR_AUTO_EN
;
668 // Restart auto-negotiation
669 PhyControl
|= PHYCR_RST_AUTO
;
671 // Enable collision test if required to do so
672 if (Flags
& AUTO_NEGOTIATE_COLLISION_TEST
) {
673 PhyControl
|= PHYCR_COLL_TEST
;
675 PhyControl
&= ~ PHYCR_COLL_TEST
;
678 // Write this configuration
679 IndirectPHYWrite32 (PHY_INDEX_BASIC_CTRL
, PhyControl
);
681 // Wait until process has completed
682 while ((IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS
) & PHYSTS_AUTO_COMP
) == 0);
687 // Check the Link Status and take appropriate action
691 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
694 // Get the PHY Status
695 UINT32 PhyBStatus
= IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS
);
697 if (PhyBStatus
& PHYSTS_LINK_STS
) {
700 return EFI_DEVICE_ERROR
;
704 // Stop the transmitter
708 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
717 // Check if we want to clear tx
718 if (Flags
& STOP_TX_CLEAR
) {
719 TxCfg
= Lan9118MmioRead32 (LAN9118_TX_CFG
);
720 TxCfg
|= TXCFG_TXS_DUMP
| TXCFG_TXD_DUMP
;
721 Lan9118MmioWrite32 (LAN9118_TX_CFG
, TxCfg
);
722 gBS
->Stall (LAN9118_STALL
);
725 // Check if already stopped
726 if (Flags
& STOP_TX_MAC
) {
727 MacCsr
= IndirectMACRead32 (INDIRECT_MAC_INDEX_CR
);
729 if (MacCsr
& MACCR_TX_EN
) {
730 MacCsr
&= ~MACCR_TX_EN
;
731 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR
, MacCsr
);
735 if (Flags
& STOP_TX_CFG
) {
736 TxCfg
= Lan9118MmioRead32 (LAN9118_TX_CFG
);
738 if (TxCfg
& TXCFG_TX_ON
) {
739 TxCfg
|= TXCFG_STOP_TX
;
740 Lan9118MmioWrite32 (LAN9118_TX_CFG
, TxCfg
);
741 gBS
->Stall (LAN9118_STALL
);
743 // Wait for Tx to finish transmitting
744 while (Lan9118MmioRead32 (LAN9118_TX_CFG
) & TXCFG_STOP_TX
);
755 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
763 // Check if already stopped
764 MacCsr
= IndirectMACRead32 (INDIRECT_MAC_INDEX_CR
);
766 if (MacCsr
& MACCR_RX_EN
) {
767 MacCsr
&= ~ MACCR_RX_EN
;
768 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR
, MacCsr
);
771 // Check if we want to clear receiver FIFOs
772 if (Flags
& STOP_RX_CLEAR
) {
773 RxCfg
= Lan9118MmioRead32 (LAN9118_RX_CFG
);
774 RxCfg
|= RXCFG_RX_DUMP
;
775 Lan9118MmioWrite32 (LAN9118_RX_CFG
, RxCfg
);
776 gBS
->Stall (LAN9118_STALL
);
778 while (Lan9118MmioRead32 (LAN9118_RX_CFG
) & RXCFG_RX_DUMP
);
784 // Start the transmitter
788 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
797 // Check if we want to clear tx
798 if (Flags
& START_TX_CLEAR
) {
799 TxCfg
= Lan9118MmioRead32 (LAN9118_TX_CFG
);
800 TxCfg
|= TXCFG_TXS_DUMP
| TXCFG_TXD_DUMP
;
801 Lan9118MmioWrite32 (LAN9118_TX_CFG
, TxCfg
);
802 gBS
->Stall (LAN9118_STALL
);
805 // Check if tx was started from MAC and enable if not
806 if (Flags
& START_TX_MAC
) {
807 MacCsr
= IndirectMACRead32 (INDIRECT_MAC_INDEX_CR
);
808 gBS
->Stall (LAN9118_STALL
);
809 if ((MacCsr
& MACCR_TX_EN
) == 0) {
810 MacCsr
|= MACCR_TX_EN
;
811 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR
, MacCsr
);
812 gBS
->Stall (LAN9118_STALL
);
816 // Check if tx was started from TX_CFG and enable if not
817 if (Flags
& START_TX_CFG
) {
818 TxCfg
= Lan9118MmioRead32 (LAN9118_TX_CFG
);
819 gBS
->Stall (LAN9118_STALL
);
820 if ((TxCfg
& TXCFG_TX_ON
) == 0) {
821 TxCfg
|= TXCFG_TX_ON
;
822 Lan9118MmioWrite32 (LAN9118_TX_CFG
, TxCfg
);
823 gBS
->Stall (LAN9118_STALL
);
827 // Set the tx data trigger level
832 // Start the receiver
836 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
844 // Check if already started
845 MacCsr
= IndirectMACRead32 (INDIRECT_MAC_INDEX_CR
);
847 if ((MacCsr
& MACCR_RX_EN
) == 0) {
848 // Check if we want to clear receiver FIFOs before starting
849 if (Flags
& START_RX_CLEAR
) {
850 RxCfg
= Lan9118MmioRead32 (LAN9118_RX_CFG
);
851 RxCfg
|= RXCFG_RX_DUMP
;
852 Lan9118MmioWrite32 (LAN9118_RX_CFG
, RxCfg
);
853 gBS
->Stall (LAN9118_STALL
);
855 while (Lan9118MmioRead32 (LAN9118_RX_CFG
) & RXCFG_RX_DUMP
);
858 MacCsr
|= MACCR_RX_EN
;
859 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR
, MacCsr
);
860 gBS
->Stall (LAN9118_STALL
);
866 // Check Tx Data available space
870 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
876 // Get the amount of free space from information register
877 TxInf
= Lan9118MmioRead32 (LAN9118_TX_FIFO_INF
);
878 FreeSpace
= (TxInf
& TXFIFOINF_TDFREE_MASK
);
880 return FreeSpace
; // Value in bytes
883 // Check Tx Status used space
887 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
893 // Get the amount of used space from information register
894 TxInf
= Lan9118MmioRead32 (LAN9118_TX_FIFO_INF
);
895 UsedSpace
= (TxInf
& TXFIFOINF_TXSUSED_MASK
) >> 16;
897 return UsedSpace
<< 2; // Value in bytes
900 // Check Rx Data used space
904 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
910 // Get the amount of used space from information register
911 RxInf
= Lan9118MmioRead32 (LAN9118_RX_FIFO_INF
);
912 UsedSpace
= (RxInf
& RXFIFOINF_RXDUSED_MASK
);
914 return UsedSpace
; // Value in bytes (rounded up to nearest DWORD)
917 // Check Rx Status used space
921 EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
927 // Get the amount of used space from information register
928 RxInf
= Lan9118MmioRead32 (LAN9118_RX_FIFO_INF
);
929 UsedSpace
= (RxInf
& RXFIFOINF_RXSUSED_MASK
) >> 16;
931 return UsedSpace
<< 2; // Value in bytes
935 // Change the allocation of FIFOs
937 ChangeFifoAllocation (
939 IN OUT UINTN
*TxDataSize OPTIONAL
,
940 IN OUT UINTN
*RxDataSize OPTIONAL
,
941 IN OUT UINT32
*TxStatusSize OPTIONAL
,
942 IN OUT UINT32
*RxStatusSize OPTIONAL
,
943 IN OUT EFI_SIMPLE_NETWORK_PROTOCOL
*Snp
949 // Check that desired sizes don't exceed limits
950 if (*TxDataSize
> TX_FIFO_MAX_SIZE
)
951 return EFI_INVALID_PARAMETER
;
953 #if defined(RX_FIFO_MIN_SIZE) && defined(RX_FIFO_MAX_SIZE)
954 if (*RxDataSize
> RX_FIFO_MAX_SIZE
) {
955 return EFI_INVALID_PARAMETER
;
959 if (Flags
& ALLOC_USE_DEFAULT
) {
963 // If we use the FIFOs (always use this first)
964 if (Flags
& ALLOC_USE_FIFOS
) {
965 // Read the current value of allocation
966 HwConf
= Lan9118MmioRead32 (LAN9118_HW_CFG
);
967 TxFifoOption
= (HwConf
>> 16) & 0xF;
969 // Choose the correct size (always use larger than requested if possible)
970 if (*TxDataSize
< TX_FIFO_MIN_SIZE
) {
971 *TxDataSize
= TX_FIFO_MIN_SIZE
;
975 } else if ((*TxDataSize
> TX_FIFO_MIN_SIZE
) && (*TxDataSize
<= 2560)) {
980 } else if ((*TxDataSize
> 2560) && (*TxDataSize
<= 3584)) {
985 } else if ((*TxDataSize
> 3584) && (*TxDataSize
<= 4608)) { // default option
990 } else if ((*TxDataSize
> 4608) && (*TxDataSize
<= 5632)) {
995 } else if ((*TxDataSize
> 5632) && (*TxDataSize
<= 6656)) {
1000 } else if ((*TxDataSize
> 6656) && (*TxDataSize
<= 7680)) {
1003 *RxStatusSize
= 512;
1005 } else if ((*TxDataSize
> 7680) && (*TxDataSize
<= 8704)) {
1008 *RxStatusSize
= 448;
1010 } else if ((*TxDataSize
> 8704) && (*TxDataSize
<= 9728)) {
1013 *RxStatusSize
= 384;
1015 } else if ((*TxDataSize
> 9728) && (*TxDataSize
<= 10752)) {
1016 *TxDataSize
= 10752;
1018 *RxStatusSize
= 320;
1020 } else if ((*TxDataSize
> 10752) && (*TxDataSize
<= 11776)) {
1021 *TxDataSize
= 11776;
1023 *RxStatusSize
= 256;
1025 } else if ((*TxDataSize
> 11776) && (*TxDataSize
<= 12800)) {
1026 *TxDataSize
= 12800;
1028 *RxStatusSize
= 192;
1030 } else if ((*TxDataSize
> 12800) && (*TxDataSize
<= 13824)) {
1031 *TxDataSize
= 13824;
1033 *RxStatusSize
= 128;
1037 ASSERT(0); // Untested code path
1043 if (Flags
& ALLOC_USE_DMA
) {
1044 return EFI_UNSUPPORTED
; // Unsupported as of now
1046 // Clear and assign the new size option
1047 HwConf
&= ~(0xF0000);
1048 HwConf
|= ((TxFifoOption
& 0xF) << 16);
1049 Lan9118MmioWrite32 (LAN9118_HW_CFG
, HwConf
);
1050 gBS
->Stall (LAN9118_STALL
);