]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Drivers/Lan9118Dxe/Lan9118DxeUtil.c
EmbeddedPkg/Lan9118Dxe: add mask PCD to disable auto-negotiation features
[mirror_edk2.git] / EmbeddedPkg / Drivers / Lan9118Dxe / Lan9118DxeUtil.c
CommitLineData
46f2c53b
OM
1/** @file\r
2*\r
3* Copyright (c) 2012-2014, ARM Limited. All rights reserved.\r
4*\r
5* This program and the accompanying materials\r
6* are licensed and made available under the terms and conditions of the BSD License\r
7* which accompanies this distribution. The full text of the license may be found at\r
8* http://opensource.org/licenses/bsd-license.php\r
9*\r
10* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12*\r
13**/\r
14\r
15#include "Lan9118Dxe.h"\r
16\r
b0fdce95 17STATIC EFI_MAC_ADDRESS mZeroMac = { { 0 } };\r
46f2c53b
OM
18\r
19/**\r
20 This internal function reverses bits for 32bit data.\r
21\r
22 @param Value The data to be reversed.\r
23\r
24 @return Data reversed.\r
25\r
26**/\r
27UINT32\r
28ReverseBits (\r
29 UINT32 Value\r
30 )\r
31{\r
32 UINTN Index;\r
33 UINT32 NewValue;\r
34\r
35 NewValue = 0;\r
36 for (Index = 0; Index < 32; Index++) {\r
37 if ((Value & (1 << Index)) != 0) {\r
38 NewValue = NewValue | (1 << (31 - Index));\r
39 }\r
40 }\r
41\r
42 return NewValue;\r
43}\r
44\r
45/*\r
46** Create Ethernet CRC\r
47**\r
48** INFO USED:\r
49** 1: http://en.wikipedia.org/wiki/Cyclic_redundancy_check\r
50**\r
51** 2: http://www.erg.abdn.ac.uk/~gorry/eg3567/dl-pages/crc.html\r
52**\r
53** 3: http://en.wikipedia.org/wiki/Computation_of_CRC\r
54*/\r
55UINT32\r
56GenEtherCrc32 (\r
57 IN EFI_MAC_ADDRESS *Mac,\r
58 IN UINT32 AddrLen\r
59 )\r
60{\r
61 INT32 Iter;\r
62 UINT32 Remainder;\r
63 UINT8 *Ptr;\r
64\r
65 Iter = 0;\r
66 Remainder = 0xFFFFFFFF; // 0xFFFFFFFF is standard seed for Ethernet\r
67\r
68 // Convert Mac Address to array of bytes\r
69 Ptr = (UINT8*)Mac;\r
70\r
71 // Generate the Crc bit-by-bit (LSB first)\r
72 while (AddrLen--) {\r
73 Remainder ^= *Ptr++;\r
74 for (Iter = 0;Iter < 8;Iter++) {\r
75 // Check if exponent is set\r
76 if (Remainder & 1) {\r
77 Remainder = (Remainder >> 1) ^ CRC_POLYNOMIAL;\r
78 } else {\r
79 Remainder = (Remainder >> 1) ^ 0;\r
80 }\r
81 }\r
82 }\r
83\r
84 // Reverse the bits before returning (to Big Endian)\r
85 //TODO: Need to be reviewed. Do we want to do a bit reverse or a byte reverse (in this case use SwapBytes32())\r
86 return ReverseBits (Remainder);\r
87}\r
88\r
89// Function to read from MAC indirect registers\r
90UINT32\r
91IndirectMACRead32 (\r
92 UINT32 Index\r
93 )\r
94{\r
95 UINT32 MacCSR;\r
96\r
97 // Check index is in the range\r
98 ASSERT(Index <= 12);\r
99\r
100 // Wait until CSR busy bit is cleared\r
e68449c9 101 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD) & MAC_CSR_BUSY) == MAC_CSR_BUSY);\r
46f2c53b
OM
102\r
103 // Set CSR busy bit to ensure read will occur\r
104 // Set the R/W bit to indicate we are reading\r
105 // Set the index of CSR Address to access desired register\r
106 MacCSR = MAC_CSR_BUSY | MAC_CSR_READ | MAC_CSR_ADDR(Index);\r
107\r
108 // Write to the register\r
e68449c9 109 Lan9118MmioWrite32 (LAN9118_MAC_CSR_CMD, MacCSR);\r
46f2c53b
OM
110\r
111 // Wait until CSR busy bit is cleared\r
e68449c9 112 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD) & MAC_CSR_BUSY) == MAC_CSR_BUSY);\r
46f2c53b
OM
113\r
114 // Now read from data register to get read value\r
e68449c9 115 return Lan9118MmioRead32 (LAN9118_MAC_CSR_DATA);\r
46f2c53b
OM
116}\r
117\r
73683a24
MR
118/*\r
119 * LAN9118 chips have special restrictions on some back-to-back Write/Read or\r
120 * Read/Read pairs of accesses. After a read or write that changes the state of\r
121 * the device, there is a period in which stale values may be returned in\r
122 * response to a read. This period is dependent on the registers accessed.\r
123 *\r
124 * We must delay prior reads by this period. This can either be achieved by\r
125 * timer-based delays, or by performing dummy reads of the BYTE_TEST register,\r
126 * for which the recommended number of reads is described in the LAN9118 data\r
127 * sheet. This is required in addition to any memory barriers.\r
128 *\r
129 * This function performs a number of dummy reads of the BYTE_TEST register, as\r
130 * a building block for the above.\r
131 */\r
132VOID\r
133WaitDummyReads (\r
134 UINTN Count\r
135 )\r
136{\r
e68449c9
MR
137 while (Count--)\r
138 MmioRead32(LAN9118_BYTE_TEST);\r
73683a24
MR
139}\r
140\r
141UINT32\r
142Lan9118RawMmioRead32(\r
143 UINTN Address,\r
144 UINTN Delay\r
145 )\r
146{\r
147 UINT32 Value;\r
148\r
149 Value = MmioRead32(Address);\r
150 WaitDummyReads(Delay);\r
151 return Value;\r
152}\r
153\r
154UINT32\r
155Lan9118RawMmioWrite32(\r
156 UINTN Address,\r
157 UINT32 Value,\r
158 UINTN Delay\r
159 )\r
160{\r
161 MmioWrite32(Address, Value);\r
162 WaitDummyReads(Delay);\r
163 return Value;\r
164}\r
165\r
46f2c53b
OM
166// Function to write to MAC indirect registers\r
167UINT32\r
168IndirectMACWrite32 (\r
169 UINT32 Index,\r
170 UINT32 Value\r
171 )\r
172{\r
173 UINT32 ValueWritten;\r
174 UINT32 MacCSR;\r
175\r
176 // Check index is in the range\r
177 ASSERT(Index <= 12);\r
178\r
179 // Wait until CSR busy bit is cleared\r
e68449c9 180 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD) & MAC_CSR_BUSY) == MAC_CSR_BUSY);\r
46f2c53b
OM
181\r
182 // Set CSR busy bit to ensure read will occur\r
183 // Set the R/W bit to indicate we are writing\r
184 // Set the index of CSR Address to access desired register\r
185 MacCSR = MAC_CSR_BUSY | MAC_CSR_WRITE | MAC_CSR_ADDR(Index);\r
186\r
187 // Now write the value to the register before issuing the write command\r
e68449c9 188 ValueWritten = Lan9118MmioWrite32 (LAN9118_MAC_CSR_DATA, Value);\r
46f2c53b
OM
189\r
190 // Write the config to the register\r
e68449c9 191 Lan9118MmioWrite32 (LAN9118_MAC_CSR_CMD, MacCSR);\r
46f2c53b
OM
192\r
193 // Wait until CSR busy bit is cleared\r
e68449c9 194 while ((Lan9118MmioRead32 (LAN9118_MAC_CSR_CMD) & MAC_CSR_BUSY) == MAC_CSR_BUSY);\r
46f2c53b
OM
195\r
196 return ValueWritten;\r
197}\r
198\r
199// Function to read from MII register (PHY Access)\r
200UINT32\r
201IndirectPHYRead32 (\r
202 UINT32 Index\r
203 )\r
204{\r
205 UINT32 ValueRead;\r
206 UINT32 MiiAcc;\r
207\r
208 // Check it is a valid index\r
209 ASSERT(Index < 31);\r
210\r
211 // Wait for busy bit to clear\r
212 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC) & MII_ACC_MII_BUSY) == MII_ACC_MII_BUSY);\r
213\r
214 // Clear the R/W bit to indicate we are reading\r
215 // Set the index of the MII register\r
216 // Set the PHY Address\r
217 // Set the MII busy bit to allow read\r
218 MiiAcc = MII_ACC_MII_READ | MII_ACC_MII_REG_INDEX(Index) | MII_ACC_PHY_VALUE | MII_ACC_MII_BUSY;\r
219\r
220 // Now write this config to register\r
221 IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_ACC, MiiAcc & 0xFFFF);\r
222\r
223 // Wait for busy bit to clear\r
224 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC) & MII_ACC_MII_BUSY) == MII_ACC_MII_BUSY);\r
225\r
226 // Now read the value of the register\r
227 ValueRead = (IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_DATA) & 0xFFFF); // only lower 16 bits are valid for any PHY register\r
228\r
229 return ValueRead;\r
230}\r
231\r
232\r
233// Function to write to the MII register (PHY Access)\r
234UINT32\r
235IndirectPHYWrite32 (\r
236 UINT32 Index,\r
237 UINT32 Value\r
238 )\r
239{\r
240 UINT32 MiiAcc;\r
241 UINT32 ValueWritten;\r
242\r
243 // Check it is a valid index\r
244 ASSERT(Index < 31);\r
245\r
246 // Wait for busy bit to clear\r
247 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC) & MII_ACC_MII_BUSY) == MII_ACC_MII_BUSY);\r
248\r
249 // Clear the R/W bit to indicate we are reading\r
250 // Set the index of the MII register\r
251 // Set the PHY Address\r
252 // Set the MII busy bit to allow read\r
253 MiiAcc = MII_ACC_MII_WRITE | MII_ACC_MII_REG_INDEX(Index) | MII_ACC_PHY_VALUE | MII_ACC_MII_BUSY;\r
254\r
255 // Write the desired value to the register first\r
256 ValueWritten = IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_DATA, (Value & 0xFFFF));\r
257\r
258 // Now write the config to register\r
259 IndirectMACWrite32 (INDIRECT_MAC_INDEX_MII_ACC, MiiAcc & 0xFFFF);\r
260\r
261 // Wait for operation to terminate\r
262 while ((IndirectMACRead32 (INDIRECT_MAC_INDEX_MII_ACC) & MII_ACC_MII_BUSY) == MII_ACC_MII_BUSY);\r
263\r
264 return ValueWritten;\r
265}\r
266\r
267\r
268/* ---------------- EEPROM Operations ------------------ */\r
269\r
270\r
271// Function to read from EEPROM memory\r
272UINT32\r
273IndirectEEPROMRead32 (\r
274 UINT32 Index\r
275 )\r
276{\r
277 UINT32 EepromCmd;\r
278\r
279 // Set the busy bit to ensure read will occur\r
280 EepromCmd = E2P_EPC_BUSY | E2P_EPC_CMD_READ;\r
281\r
282 // Set the index to access desired EEPROM memory location\r
283 EepromCmd |= E2P_EPC_ADDRESS(Index);\r
284\r
285 // Write to Eeprom command register\r
e68449c9 286 Lan9118MmioWrite32 (LAN9118_E2P_CMD, EepromCmd);\r
46f2c53b
OM
287\r
288 // Wait until operation has completed\r
e68449c9 289 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
290\r
291 // Check that operation didn't time out\r
e68449c9 292 if (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_TIMEOUT) {\r
46f2c53b
OM
293 DEBUG ((EFI_D_ERROR, "EEPROM Operation Timed out: Read command on index %x\n",Index));\r
294 return 0;\r
295 }\r
296\r
297 // Wait until operation has completed\r
e68449c9 298 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
299\r
300 // Finally read the value\r
e68449c9 301 return Lan9118MmioRead32 (LAN9118_E2P_DATA);\r
46f2c53b
OM
302}\r
303\r
304// Function to write to EEPROM memory\r
305UINT32\r
306IndirectEEPROMWrite32 (\r
307 UINT32 Index,\r
308 UINT32 Value\r
309 )\r
310{\r
311 UINT32 ValueWritten;\r
312 UINT32 EepromCmd;\r
313\r
314 ValueWritten = 0;\r
315\r
316 // Read the EEPROM Command register\r
e68449c9 317 EepromCmd = Lan9118MmioRead32 (LAN9118_E2P_CMD);\r
46f2c53b
OM
318\r
319 // Set the busy bit to ensure read will occur\r
320 EepromCmd |= ((UINT32)1 << 31);\r
321\r
322 // Set the EEPROM command to write(0b011)\r
323 EepromCmd &= ~(7 << 28); // Clear the command first\r
324 EepromCmd |= (3 << 28); // Write 011\r
325\r
326 // Set the index to access desired EEPROM memory location\r
327 EepromCmd |= (Index & 0xF);\r
328\r
329 // Write the value to the data register first\r
e68449c9 330 ValueWritten = Lan9118MmioWrite32 (LAN9118_E2P_DATA, Value);\r
46f2c53b
OM
331\r
332 // Write to Eeprom command register\r
e68449c9 333 Lan9118MmioWrite32 (LAN9118_E2P_CMD, EepromCmd);\r
46f2c53b
OM
334\r
335 // Wait until operation has completed\r
e68449c9 336 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
337\r
338 // Check that operation didn't time out\r
e68449c9 339 if (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_TIMEOUT) {\r
46f2c53b
OM
340 DEBUG ((EFI_D_ERROR, "EEPROM Operation Timed out: Write command at memloc 0x%x, with value 0x%x\n",Index, Value));\r
341 return 0;\r
342 }\r
343\r
344 // Wait until operation has completed\r
e68449c9 345 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
346\r
347 return ValueWritten;\r
348}\r
349\r
350/* ---------------- General Operations ----------------- */\r
351\r
352VOID\r
353Lan9118SetMacAddress (\r
354 EFI_MAC_ADDRESS *Mac,\r
355 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
356 )\r
357{\r
358 IndirectMACWrite32 (INDIRECT_MAC_INDEX_ADDRL,\r
359 (Mac->Addr[0] & 0xFF) |\r
360 ((Mac->Addr[1] & 0xFF) << 8) |\r
361 ((Mac->Addr[2] & 0xFF) << 16) |\r
362 ((Mac->Addr[3] & 0xFF) << 24)\r
363 );\r
364\r
365 IndirectMACWrite32 (INDIRECT_MAC_INDEX_ADDRH,\r
366 (UINT32)(Mac->Addr[4] & 0xFF) |\r
367 ((Mac->Addr[5] & 0xFF) << 8)\r
368 );\r
46f2c53b
OM
369}\r
370\r
371VOID\r
372Lan9118ReadMacAddress (\r
373 OUT EFI_MAC_ADDRESS *MacAddress\r
374 )\r
375{\r
376 UINT32 MacAddrHighValue;\r
377 UINT32 MacAddrLowValue;\r
378\r
379 // Read the Mac Addr high register\r
380 MacAddrHighValue = (IndirectMACRead32 (INDIRECT_MAC_INDEX_ADDRH) & 0xFFFF);\r
381 // Read the Mac Addr low register\r
382 MacAddrLowValue = IndirectMACRead32 (INDIRECT_MAC_INDEX_ADDRL);\r
383\r
384 SetMem (MacAddress, sizeof(*MacAddress), 0);\r
385 MacAddress->Addr[0] = (MacAddrLowValue & 0xFF);\r
386 MacAddress->Addr[1] = (MacAddrLowValue & 0xFF00) >> 8;\r
387 MacAddress->Addr[2] = (MacAddrLowValue & 0xFF0000) >> 16;\r
388 MacAddress->Addr[3] = (MacAddrLowValue & 0xFF000000) >> 24;\r
389 MacAddress->Addr[4] = (MacAddrHighValue & 0xFF);\r
390 MacAddress->Addr[5] = (MacAddrHighValue & 0xFF00) >> 8;\r
391}\r
392\r
393/*\r
394 * Power up the 9118 and find its MAC address.\r
395 *\r
396 * This operation can be carried out when the LAN9118 is in any power state\r
397 *\r
398 */\r
399EFI_STATUS\r
400Lan9118Initialize (\r
401 IN EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
402 )\r
403{\r
bbff41c1 404 UINTN Retries;\r
46f2c53b
OM
405 UINT64 DefaultMacAddress;\r
406\r
407 // Attempt to wake-up the device if it is in a lower power state\r
e68449c9 408 if (((Lan9118MmioRead32 (LAN9118_PMT_CTRL) & MPTCTRL_PM_MODE_MASK) >> 12) != 0) {\r
46f2c53b 409 DEBUG ((DEBUG_NET, "Waking from reduced power state.\n"));\r
e68449c9 410 Lan9118MmioWrite32 (LAN9118_BYTE_TEST, 0xFFFFFFFF);\r
46f2c53b
OM
411 }\r
412\r
413 // Check that device is active\r
bbff41c1 414 Retries = 20;\r
e68449c9 415 while ((Lan9118MmioRead32 (LAN9118_PMT_CTRL) & MPTCTRL_READY) == 0 && --Retries) {\r
46f2c53b
OM
416 gBS->Stall (LAN9118_STALL);\r
417 }\r
bbff41c1 418 if (!Retries) {\r
46f2c53b
OM
419 return EFI_TIMEOUT;\r
420 }\r
421\r
422 // Check that EEPROM isn't active\r
bbff41c1 423 Retries = 20;\r
e68449c9 424 while ((Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY) && --Retries){\r
46f2c53b
OM
425 gBS->Stall (LAN9118_STALL);\r
426 }\r
bbff41c1 427 if (!Retries) {\r
46f2c53b
OM
428 return EFI_TIMEOUT;\r
429 }\r
430\r
431 // Check if a MAC address was loaded from EEPROM, and if it was, set it as the\r
432 // current address.\r
e68449c9 433 if ((Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_MAC_ADDRESS_LOADED) == 0) {\r
46f2c53b
OM
434 DEBUG ((EFI_D_ERROR, "Warning: There was an error detecting EEPROM or loading the MAC Address.\n"));\r
435\r
436 // If we had an address before (set by StationAddess), continue to use it\r
437 if (CompareMem (&Snp->Mode->CurrentAddress, &mZeroMac, NET_ETHER_ADDR_LEN)) {\r
438 Lan9118SetMacAddress (&Snp->Mode->CurrentAddress, Snp);\r
439 } else {\r
440 // If there are no cached addresses, then fall back to a default\r
441 DEBUG ((EFI_D_WARN, "Warning: using driver-default MAC address\n"));\r
442 DefaultMacAddress = FixedPcdGet64 (PcdLan9118DefaultMacAddress);\r
443 Lan9118SetMacAddress((EFI_MAC_ADDRESS *) &DefaultMacAddress, Snp);\r
11bbc257 444 CopyMem (&Snp->Mode->CurrentAddress, &DefaultMacAddress, NET_ETHER_ADDR_LEN);\r
46f2c53b
OM
445 }\r
446 } else {\r
447 // Store the MAC address that was loaded from EEPROM\r
448 Lan9118ReadMacAddress (&Snp->Mode->CurrentAddress);\r
449 CopyMem (&Snp->Mode->PermanentAddress, &Snp->Mode->CurrentAddress, NET_ETHER_ADDR_LEN);\r
450 }\r
451\r
452 // Clear and acknowledge interrupts\r
e68449c9
MR
453 Lan9118MmioWrite32 (LAN9118_INT_EN, 0);\r
454 Lan9118MmioWrite32 (LAN9118_IRQ_CFG, 0);\r
455 Lan9118MmioWrite32 (LAN9118_INT_STS, 0xFFFFFFFF);\r
46f2c53b
OM
456\r
457 // Do self tests here?\r
458\r
459 return EFI_SUCCESS;\r
460}\r
461\r
462\r
463// Perform software reset on the LAN9118\r
464// Return 0 on success, -1 on error\r
465EFI_STATUS\r
466SoftReset (\r
467 UINT32 Flags,\r
468 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
469 )\r
470{\r
471 UINT32 HwConf;\r
472 UINT32 ResetTime;\r
473\r
474 // Initialize variable\r
475 ResetTime = 0;\r
476\r
477 // Stop Rx and Tx\r
478 StopTx (STOP_TX_MAC | STOP_TX_CFG | STOP_TX_CLEAR, Snp);\r
479 StopRx (STOP_RX_CLEAR, Snp); // Clear receiver FIFO\r
480\r
481 // Issue the reset\r
e68449c9 482 HwConf = Lan9118MmioRead32 (LAN9118_HW_CFG);\r
46f2c53b
OM
483 HwConf |= 1;\r
484\r
485 // Set the Must Be One (MBO) bit\r
486 if (((HwConf & HWCFG_MBO) >> 20) == 0) {\r
487 HwConf |= HWCFG_MBO;\r
488 }\r
489\r
490 // Check that EEPROM isn't active\r
e68449c9 491 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
492\r
493 // Write the configuration\r
e68449c9 494 Lan9118MmioWrite32 (LAN9118_HW_CFG, HwConf);\r
46f2c53b
OM
495\r
496 // Wait for reset to complete\r
e68449c9 497 while (Lan9118MmioRead32 (LAN9118_HW_CFG) & HWCFG_SRST) {\r
46f2c53b
OM
498\r
499 gBS->Stall (LAN9118_STALL);\r
500 ResetTime += 1;\r
501\r
502 // If time taken exceeds 100us, then there was an error condition\r
503 if (ResetTime > 1000) {\r
504 Snp->Mode->State = EfiSimpleNetworkStopped;\r
505 return EFI_TIMEOUT;\r
506 }\r
507 }\r
508\r
509 // Check that EEPROM isn't active\r
e68449c9 510 while (Lan9118MmioRead32 (LAN9118_E2P_CMD) & E2P_EPC_BUSY);\r
46f2c53b
OM
511\r
512 // TODO we probably need to re-set the mac address here.\r
513\r
514 // Clear and acknowledge all interrupts\r
515 if (Flags & SOFT_RESET_CLEAR_INT) {\r
e68449c9
MR
516 Lan9118MmioWrite32 (LAN9118_INT_EN, 0);\r
517 Lan9118MmioWrite32 (LAN9118_IRQ_CFG, 0);\r
518 Lan9118MmioWrite32 (LAN9118_INT_STS, 0xFFFFFFFF);\r
46f2c53b
OM
519 }\r
520\r
521 // Do self tests here?\r
522 if (Flags & SOFT_RESET_SELF_TEST) {\r
523\r
524 }\r
525\r
526 return EFI_SUCCESS;\r
527}\r
528\r
529\r
530// Perform PHY software reset\r
42589b9a 531EFI_STATUS\r
46f2c53b
OM
532PhySoftReset (\r
533 UINT32 Flags,\r
534 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
535 )\r
536{\r
537 UINT32 PmtCtrl = 0;\r
46f2c53b
OM
538\r
539 // PMT PHY reset takes precedence over BCR\r
540 if (Flags & PHY_RESET_PMT) {\r
e68449c9 541 PmtCtrl = Lan9118MmioRead32 (LAN9118_PMT_CTRL);\r
46f2c53b 542 PmtCtrl |= MPTCTRL_PHY_RST;\r
e68449c9 543 Lan9118MmioWrite32 (LAN9118_PMT_CTRL,PmtCtrl);\r
46f2c53b
OM
544\r
545 // Wait for completion\r
e68449c9 546 while (Lan9118MmioRead32 (LAN9118_PMT_CTRL) & MPTCTRL_PHY_RST) {\r
28f52b9f 547 gBS->Stall (LAN9118_STALL);\r
46f2c53b
OM
548 }\r
549 // PHY Basic Control Register reset\r
fffa8522 550 } else if (Flags & PHY_RESET_BCR) {\r
46f2c53b
OM
551 IndirectPHYWrite32 (PHY_INDEX_BASIC_CTRL, PHYCR_RESET);\r
552\r
553 // Wait for completion\r
554 while (IndirectPHYRead32 (PHY_INDEX_BASIC_CTRL) & PHYCR_RESET) {\r
28f52b9f 555 gBS->Stall (LAN9118_STALL);\r
46f2c53b
OM
556 }\r
557 }\r
558\r
46f2c53b
OM
559 // Clear and acknowledge all interrupts\r
560 if (Flags & PHY_SOFT_RESET_CLEAR_INT) {\r
e68449c9
MR
561 Lan9118MmioWrite32 (LAN9118_INT_EN, 0);\r
562 Lan9118MmioWrite32 (LAN9118_IRQ_CFG, 0);\r
563 Lan9118MmioWrite32 (LAN9118_INT_STS, 0xFFFFFFFF);\r
46f2c53b
OM
564 }\r
565\r
42589b9a 566 return EFI_SUCCESS;\r
46f2c53b
OM
567}\r
568\r
569\r
570// Configure hardware for LAN9118\r
571EFI_STATUS\r
572ConfigureHardware (\r
573 UINT32 Flags,\r
574 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
575 )\r
576{\r
577 UINT32 GpioConf;\r
578\r
579 // Check if we want to use LEDs on GPIO\r
580 if (Flags & HW_CONF_USE_LEDS) {\r
e68449c9 581 GpioConf = Lan9118MmioRead32 (LAN9118_GPIO_CFG);\r
46f2c53b
OM
582\r
583 // Enable GPIO as LEDs and Config as Push-Pull driver\r
584 GpioConf |= GPIO_GPIO0_PUSH_PULL | GPIO_GPIO1_PUSH_PULL | GPIO_GPIO2_PUSH_PULL |\r
585 GPIO_LED1_ENABLE | GPIO_LED2_ENABLE | GPIO_LED3_ENABLE;\r
586\r
587 // Write the configuration\r
e68449c9 588 Lan9118MmioWrite32 (LAN9118_GPIO_CFG, GpioConf);\r
46f2c53b
OM
589 }\r
590\r
591 return EFI_SUCCESS;\r
592}\r
593\r
594// Configure flow control\r
595EFI_STATUS\r
596ConfigureFlow (\r
597 UINT32 Flags,\r
598 UINT32 HighTrig,\r
599 UINT32 LowTrig,\r
600 UINT32 BPDuration,\r
601 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
602 )\r
603{\r
604 return EFI_SUCCESS;\r
605}\r
606\r
607// Do auto-negotiation\r
608EFI_STATUS\r
609AutoNegotiate (\r
610 UINT32 Flags,\r
611 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
612 )\r
613{\r
614 UINT32 PhyControl;\r
615 UINT32 PhyStatus;\r
616 UINT32 Features;\r
bbff41c1 617 UINT32 Retries;\r
46f2c53b
OM
618\r
619 // First check that auto-negotiation is supported\r
620 PhyStatus = IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS);\r
621 if ((PhyStatus & PHYSTS_AUTO_CAP) == 0) {\r
622 DEBUG ((EFI_D_ERROR, "Auto-negotiation not supported.\n"));\r
623 return EFI_DEVICE_ERROR;\r
624 }\r
625\r
626 // Check that link is up first\r
627 if ((PhyStatus & PHYSTS_LINK_STS) == 0) {\r
628 // Wait until it is up or until Time Out\r
bbff41c1 629 Retries = FixedPcdGet32 (PcdLan9118DefaultNegotiationTimeout) / LAN9118_STALL;\r
46f2c53b
OM
630 while ((IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS) & PHYSTS_LINK_STS) == 0) {\r
631 gBS->Stall (LAN9118_STALL);\r
bbff41c1
RH
632 Retries--;\r
633 if (!Retries) {\r
46f2c53b
OM
634 DEBUG ((EFI_D_ERROR, "Link timeout in auto-negotiation.\n"));\r
635 return EFI_TIMEOUT;\r
636 }\r
637 }\r
638 }\r
639\r
640 // Configure features to advertise\r
641 Features = IndirectPHYRead32 (PHY_INDEX_AUTO_NEG_ADVERT);\r
642\r
643 if ((Flags & AUTO_NEGOTIATE_ADVERTISE_ALL) > 0) {\r
644 // Link speed capabilities\r
645 Features |= (PHYANA_10BASET | PHYANA_10BASETFD | PHYANA_100BASETX | PHYANA_100BASETXFD);\r
646\r
647 // Pause frame capabilities\r
648 Features &= ~(PHYANA_PAUSE_OP_MASK);\r
649 Features |= 3 << 10;\r
650 }\r
6336850c 651 Features &= FixedPcdGet32 (PcdLan9118NegotiationFeatureMask);\r
46f2c53b
OM
652\r
653 // Write the features\r
654 IndirectPHYWrite32 (PHY_INDEX_AUTO_NEG_ADVERT, Features);\r
655\r
656 // Read control register\r
657 PhyControl = IndirectPHYRead32 (PHY_INDEX_BASIC_CTRL);\r
658\r
659 // Enable Auto-Negotiation\r
660 if ((PhyControl & PHYCR_AUTO_EN) == 0) {\r
661 PhyControl |= PHYCR_AUTO_EN;\r
662 }\r
663\r
664 // Restart auto-negotiation\r
665 PhyControl |= PHYCR_RST_AUTO;\r
666\r
667 // Enable collision test if required to do so\r
668 if (Flags & AUTO_NEGOTIATE_COLLISION_TEST) {\r
669 PhyControl |= PHYCR_COLL_TEST;\r
670 } else {\r
671 PhyControl &= ~ PHYCR_COLL_TEST;\r
672 }\r
673\r
674 // Write this configuration\r
675 IndirectPHYWrite32 (PHY_INDEX_BASIC_CTRL, PhyControl);\r
676\r
677 // Wait until process has completed\r
678 while ((IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS) & PHYSTS_AUTO_COMP) == 0);\r
679\r
680 return EFI_SUCCESS;\r
681}\r
682\r
683// Check the Link Status and take appropriate action\r
684EFI_STATUS\r
685CheckLinkStatus (\r
686 UINT32 Flags,\r
687 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
688 )\r
689{\r
690 // Get the PHY Status\r
691 UINT32 PhyBStatus = IndirectPHYRead32 (PHY_INDEX_BASIC_STATUS);\r
692\r
693 if (PhyBStatus & PHYSTS_LINK_STS) {\r
694 return EFI_SUCCESS;\r
695 } else {\r
696 return EFI_DEVICE_ERROR;\r
697 }\r
698}\r
699\r
700// Stop the transmitter\r
701EFI_STATUS\r
702StopTx (\r
703 UINT32 Flags,\r
704 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
705 )\r
706{\r
707 UINT32 MacCsr;\r
708 UINT32 TxCfg;\r
709\r
710 MacCsr = 0;\r
711 TxCfg = 0;\r
712\r
713 // Check if we want to clear tx\r
714 if (Flags & STOP_TX_CLEAR) {\r
e68449c9 715 TxCfg = Lan9118MmioRead32 (LAN9118_TX_CFG);\r
46f2c53b 716 TxCfg |= TXCFG_TXS_DUMP | TXCFG_TXD_DUMP;\r
e68449c9 717 Lan9118MmioWrite32 (LAN9118_TX_CFG, TxCfg);\r
46f2c53b
OM
718 }\r
719\r
720 // Check if already stopped\r
721 if (Flags & STOP_TX_MAC) {\r
722 MacCsr = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);\r
723\r
724 if (MacCsr & MACCR_TX_EN) {\r
725 MacCsr &= ~MACCR_TX_EN;\r
726 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCsr);\r
727 }\r
728 }\r
729\r
730 if (Flags & STOP_TX_CFG) {\r
e68449c9 731 TxCfg = Lan9118MmioRead32 (LAN9118_TX_CFG);\r
46f2c53b
OM
732\r
733 if (TxCfg & TXCFG_TX_ON) {\r
734 TxCfg |= TXCFG_STOP_TX;\r
e68449c9 735 Lan9118MmioWrite32 (LAN9118_TX_CFG, TxCfg);\r
46f2c53b
OM
736\r
737 // Wait for Tx to finish transmitting\r
e68449c9 738 while (Lan9118MmioRead32 (LAN9118_TX_CFG) & TXCFG_STOP_TX);\r
46f2c53b
OM
739 }\r
740 }\r
741\r
742 return EFI_SUCCESS;\r
743}\r
744\r
745// Stop the receiver\r
746EFI_STATUS\r
747StopRx (\r
748 UINT32 Flags,\r
749 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
750 )\r
751{\r
752 UINT32 MacCsr;\r
753 UINT32 RxCfg;\r
754\r
755 RxCfg = 0;\r
756\r
757 // Check if already stopped\r
758 MacCsr = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);\r
759\r
760 if (MacCsr & MACCR_RX_EN) {\r
761 MacCsr &= ~ MACCR_RX_EN;\r
762 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCsr);\r
763 }\r
764\r
765 // Check if we want to clear receiver FIFOs\r
766 if (Flags & STOP_RX_CLEAR) {\r
e68449c9 767 RxCfg = Lan9118MmioRead32 (LAN9118_RX_CFG);\r
46f2c53b 768 RxCfg |= RXCFG_RX_DUMP;\r
e68449c9 769 Lan9118MmioWrite32 (LAN9118_RX_CFG, RxCfg);\r
46f2c53b 770\r
e68449c9 771 while (Lan9118MmioRead32 (LAN9118_RX_CFG) & RXCFG_RX_DUMP);\r
46f2c53b
OM
772 }\r
773\r
774 return EFI_SUCCESS;\r
775}\r
776\r
777// Start the transmitter\r
778EFI_STATUS\r
779StartTx (\r
780 UINT32 Flags,\r
781 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
782 )\r
783{\r
784 UINT32 MacCsr;\r
785 UINT32 TxCfg;\r
786\r
787 MacCsr = 0;\r
788 TxCfg = 0;\r
789\r
790 // Check if we want to clear tx\r
791 if (Flags & START_TX_CLEAR) {\r
e68449c9 792 TxCfg = Lan9118MmioRead32 (LAN9118_TX_CFG);\r
46f2c53b 793 TxCfg |= TXCFG_TXS_DUMP | TXCFG_TXD_DUMP;\r
e68449c9 794 Lan9118MmioWrite32 (LAN9118_TX_CFG, TxCfg);\r
46f2c53b
OM
795 }\r
796\r
797 // Check if tx was started from MAC and enable if not\r
798 if (Flags & START_TX_MAC) {\r
799 MacCsr = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);\r
46f2c53b
OM
800 if ((MacCsr & MACCR_TX_EN) == 0) {\r
801 MacCsr |= MACCR_TX_EN;\r
802 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCsr);\r
46f2c53b
OM
803 }\r
804 }\r
805\r
806 // Check if tx was started from TX_CFG and enable if not\r
807 if (Flags & START_TX_CFG) {\r
e68449c9 808 TxCfg = Lan9118MmioRead32 (LAN9118_TX_CFG);\r
46f2c53b
OM
809 if ((TxCfg & TXCFG_TX_ON) == 0) {\r
810 TxCfg |= TXCFG_TX_ON;\r
e68449c9 811 Lan9118MmioWrite32 (LAN9118_TX_CFG, TxCfg);\r
46f2c53b
OM
812 }\r
813 }\r
814\r
815 // Set the tx data trigger level\r
816\r
817 return EFI_SUCCESS;\r
818}\r
819\r
820// Start the receiver\r
821EFI_STATUS\r
822StartRx (\r
823 UINT32 Flags,\r
824 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
825 )\r
826{\r
827 UINT32 MacCsr;\r
828 UINT32 RxCfg;\r
829\r
830 RxCfg = 0;\r
831\r
832 // Check if already started\r
833 MacCsr = IndirectMACRead32 (INDIRECT_MAC_INDEX_CR);\r
834\r
835 if ((MacCsr & MACCR_RX_EN) == 0) {\r
836 // Check if we want to clear receiver FIFOs before starting\r
837 if (Flags & START_RX_CLEAR) {\r
e68449c9 838 RxCfg = Lan9118MmioRead32 (LAN9118_RX_CFG);\r
46f2c53b 839 RxCfg |= RXCFG_RX_DUMP;\r
e68449c9 840 Lan9118MmioWrite32 (LAN9118_RX_CFG, RxCfg);\r
46f2c53b 841\r
e68449c9 842 while (Lan9118MmioRead32 (LAN9118_RX_CFG) & RXCFG_RX_DUMP);\r
46f2c53b
OM
843 }\r
844\r
845 MacCsr |= MACCR_RX_EN;\r
846 IndirectMACWrite32 (INDIRECT_MAC_INDEX_CR, MacCsr);\r
46f2c53b
OM
847 }\r
848\r
849 return EFI_SUCCESS;\r
850}\r
851\r
852// Check Tx Data available space\r
853UINT32\r
854TxDataFreeSpace (\r
855 UINT32 Flags,\r
856 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
857 )\r
858{\r
859 UINT32 TxInf;\r
860 UINT32 FreeSpace;\r
861\r
862 // Get the amount of free space from information register\r
e68449c9 863 TxInf = Lan9118MmioRead32 (LAN9118_TX_FIFO_INF);\r
46f2c53b
OM
864 FreeSpace = (TxInf & TXFIFOINF_TDFREE_MASK);\r
865\r
866 return FreeSpace; // Value in bytes\r
867}\r
868\r
869// Check Tx Status used space\r
870UINT32\r
871TxStatusUsedSpace (\r
872 UINT32 Flags,\r
873 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
874 )\r
875{\r
876 UINT32 TxInf;\r
877 UINT32 UsedSpace;\r
878\r
879 // Get the amount of used space from information register\r
e68449c9 880 TxInf = Lan9118MmioRead32 (LAN9118_TX_FIFO_INF);\r
46f2c53b
OM
881 UsedSpace = (TxInf & TXFIFOINF_TXSUSED_MASK) >> 16;\r
882\r
883 return UsedSpace << 2; // Value in bytes\r
884}\r
885\r
886// Check Rx Data used space\r
887UINT32\r
888RxDataUsedSpace (\r
889 UINT32 Flags,\r
890 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
891 )\r
892{\r
893 UINT32 RxInf;\r
894 UINT32 UsedSpace;\r
895\r
896 // Get the amount of used space from information register\r
e68449c9 897 RxInf = Lan9118MmioRead32 (LAN9118_RX_FIFO_INF);\r
46f2c53b
OM
898 UsedSpace = (RxInf & RXFIFOINF_RXDUSED_MASK);\r
899\r
900 return UsedSpace; // Value in bytes (rounded up to nearest DWORD)\r
901}\r
902\r
903// Check Rx Status used space\r
904UINT32\r
905RxStatusUsedSpace (\r
906 UINT32 Flags,\r
907 EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
908 )\r
909{\r
910 UINT32 RxInf;\r
911 UINT32 UsedSpace;\r
912\r
913 // Get the amount of used space from information register\r
e68449c9 914 RxInf = Lan9118MmioRead32 (LAN9118_RX_FIFO_INF);\r
46f2c53b
OM
915 UsedSpace = (RxInf & RXFIFOINF_RXSUSED_MASK) >> 16;\r
916\r
917 return UsedSpace << 2; // Value in bytes\r
918}\r
919\r
920\r
921// Change the allocation of FIFOs\r
922EFI_STATUS\r
923ChangeFifoAllocation (\r
924 IN UINT32 Flags,\r
925 IN OUT UINTN *TxDataSize OPTIONAL,\r
926 IN OUT UINTN *RxDataSize OPTIONAL,\r
927 IN OUT UINT32 *TxStatusSize OPTIONAL,\r
928 IN OUT UINT32 *RxStatusSize OPTIONAL,\r
929 IN OUT EFI_SIMPLE_NETWORK_PROTOCOL *Snp\r
930 )\r
931{\r
932 UINT32 HwConf;\r
933 UINT32 TxFifoOption;\r
934\r
935 // Check that desired sizes don't exceed limits\r
936 if (*TxDataSize > TX_FIFO_MAX_SIZE)\r
937 return EFI_INVALID_PARAMETER;\r
938\r
939#if defined(RX_FIFO_MIN_SIZE) && defined(RX_FIFO_MAX_SIZE)\r
940 if (*RxDataSize > RX_FIFO_MAX_SIZE) {\r
941 return EFI_INVALID_PARAMETER;\r
942 }\r
943#endif\r
944\r
945 if (Flags & ALLOC_USE_DEFAULT) {\r
946 return EFI_SUCCESS;\r
947 }\r
948\r
949 // If we use the FIFOs (always use this first)\r
950 if (Flags & ALLOC_USE_FIFOS) {\r
951 // Read the current value of allocation\r
e68449c9 952 HwConf = Lan9118MmioRead32 (LAN9118_HW_CFG);\r
46f2c53b
OM
953 TxFifoOption = (HwConf >> 16) & 0xF;\r
954\r
955 // Choose the correct size (always use larger than requested if possible)\r
956 if (*TxDataSize < TX_FIFO_MIN_SIZE) {\r
957 *TxDataSize = TX_FIFO_MIN_SIZE;\r
958 *RxDataSize = 13440;\r
959 *RxStatusSize = 896;\r
960 TxFifoOption = 2;\r
961 } else if ((*TxDataSize > TX_FIFO_MIN_SIZE) && (*TxDataSize <= 2560)) {\r
962 *TxDataSize = 2560;\r
963 *RxDataSize = 12480;\r
964 *RxStatusSize = 832;\r
965 TxFifoOption = 3;\r
966 } else if ((*TxDataSize > 2560) && (*TxDataSize <= 3584)) {\r
967 *TxDataSize = 3584;\r
968 *RxDataSize = 11520;\r
969 *RxStatusSize = 768;\r
970 TxFifoOption = 4;\r
971 } else if ((*TxDataSize > 3584) && (*TxDataSize <= 4608)) { // default option\r
972 *TxDataSize = 4608;\r
973 *RxDataSize = 10560;\r
974 *RxStatusSize = 704;\r
975 TxFifoOption = 5;\r
976 } else if ((*TxDataSize > 4608) && (*TxDataSize <= 5632)) {\r
977 *TxDataSize = 5632;\r
978 *RxDataSize = 9600;\r
979 *RxStatusSize = 640;\r
980 TxFifoOption = 6;\r
981 } else if ((*TxDataSize > 5632) && (*TxDataSize <= 6656)) {\r
982 *TxDataSize = 6656;\r
983 *RxDataSize = 8640;\r
984 *RxStatusSize = 576;\r
985 TxFifoOption = 7;\r
986 } else if ((*TxDataSize > 6656) && (*TxDataSize <= 7680)) {\r
987 *TxDataSize = 7680;\r
988 *RxDataSize = 7680;\r
989 *RxStatusSize = 512;\r
990 TxFifoOption = 8;\r
991 } else if ((*TxDataSize > 7680) && (*TxDataSize <= 8704)) {\r
992 *TxDataSize = 8704;\r
993 *RxDataSize = 6720;\r
994 *RxStatusSize = 448;\r
995 TxFifoOption = 9;\r
996 } else if ((*TxDataSize > 8704) && (*TxDataSize <= 9728)) {\r
997 *TxDataSize = 9728;\r
998 *RxDataSize = 5760;\r
999 *RxStatusSize = 384;\r
1000 TxFifoOption = 10;\r
1001 } else if ((*TxDataSize > 9728) && (*TxDataSize <= 10752)) {\r
1002 *TxDataSize = 10752;\r
1003 *RxDataSize = 4800;\r
1004 *RxStatusSize = 320;\r
1005 TxFifoOption = 11;\r
1006 } else if ((*TxDataSize > 10752) && (*TxDataSize <= 11776)) {\r
1007 *TxDataSize = 11776;\r
1008 *RxDataSize = 3840;\r
1009 *RxStatusSize = 256;\r
1010 TxFifoOption = 12;\r
1011 } else if ((*TxDataSize > 11776) && (*TxDataSize <= 12800)) {\r
1012 *TxDataSize = 12800;\r
1013 *RxDataSize = 2880;\r
1014 *RxStatusSize = 192;\r
1015 TxFifoOption = 13;\r
1016 } else if ((*TxDataSize > 12800) && (*TxDataSize <= 13824)) {\r
1017 *TxDataSize = 13824;\r
1018 *RxDataSize = 1920;\r
1019 *RxStatusSize = 128;\r
1020 TxFifoOption = 14;\r
1021 }\r
1022 } else {\r
1023 ASSERT(0); // Untested code path\r
1024 HwConf = 0;\r
1025 TxFifoOption = 0;\r
1026 }\r
1027\r
1028 // Do we need DMA?\r
1029 if (Flags & ALLOC_USE_DMA) {\r
1030 return EFI_UNSUPPORTED; // Unsupported as of now\r
1031 }\r
1032 // Clear and assign the new size option\r
1033 HwConf &= ~(0xF0000);\r
1034 HwConf |= ((TxFifoOption & 0xF) << 16);\r
e68449c9 1035 Lan9118MmioWrite32 (LAN9118_HW_CFG, HwConf);\r
46f2c53b
OM
1036\r
1037 return EFI_SUCCESS;\r
1038}\r