Deleted Added
full compact
1c1
< /******************************************************************************
---
> /*******************************************************************************
21,23c21,23
< *
< * $FreeBSD: head/sys/boot/arm/at91/libat91/emac.c 157873 2006-04-19 17:16:49Z imp $
< *****************************************************************************/
---
> *
> * $FreeBSD: head/sys/boot/arm/at91/libat91/emac.c 161196 2006-08-10 18:11:22Z imp $
> ******************************************************************************/
26,27d25
< #include "emac.h"
< #include "p_string.h"
28a27
> #include "emac.h"
31c30
< /******************************* GLOBALS *************************************/
---
> /* ****************************** GLOBALS *************************************/
33c32
< /*********************** PRIVATE FUNCTIONS/DATA ******************************/
---
> /* ********************** PRIVATE FUNCTIONS/DATA ******************************/
35c34
< static unsigned localMACSet, serverMACSet, MAC_init;
---
> static unsigned localMACSet, serverMACSet;
37c36,38
< static unsigned localIPAddr, serverIPAddr;
---
> static unsigned localMAClow, localMAChigh;
> static unsigned localIPSet, serverIPSet;
> static unsigned char localIPAddr[4], serverIPAddr[4];
40,41c41
<
< static unsigned lastAddress, lastSize;
---
> static unsigned lastSize;
57c57
< IP_checksum(void *cp, int len)
---
> IP_checksum(unsigned short *p, int len)
60d59
< unsigned short *p = (unsigned short *)cp;
62a62
>
64a65
>
66c67
< return (~t);
---
> return (~t);
81a83
>
82a85
>
92a96
>
93a98
>
94a100
>
120a127
>
125c132
< ipHdr = &macHdr->iphdr;
---
> ipHdr = (ip_header_t*)&macHdr->packet_length;
136a144
>
139c147
< ipHdr->ip_sum = SWAP16(IP_checksum(ipHdr, 20));
---
> ipHdr->ip_sum = SWAP16(IP_checksum((unsigned short*)ipHdr, 20));
150c158
< t_checksum = IP_checksum((char *)ipHdr + 12, (16 + tftpLength));
---
> t_checksum = IP_checksum((unsigned short*)ipHdr + 6, (16 + tftpLength));
181c189
< tftpHeader.opcode = SWAP16(TFTP_RRQ_OPCODE);
---
> tftpHeader.opcode = TFTP_RRQ_OPCODE;
206c214
< if (block_num == SWAP16(ackBlock + 1)) {
---
> if (block_num == (ackBlock + 1)) {
210a219,220
> if (ackBlock % 128 == 0)
> printf("tftp: %u kB\r", lastSize / 1024);
212,214c222,223
<
< tftpHeader.opcode = SWAP16(TFTP_ACK_OPCODE);
< tftpHeader.block_num = block_num;
---
> tftpHeader.opcode = TFTP_ACK_OPCODE;
> tftpHeader.block_num = SWAP16(ackBlock);
216,217c225
<
< if (len < 512)
---
> if (len < 512) {
218a227,228
> printf("tftp: %u byte\r\n", lastSize);
> }
229,230c239,240
< static void
< CheckForNewPacket(void)
---
> static int
> CheckForNewPacket(ip_header_t *pHeader)
249c259
< return ;
---
> return (0);
253c263
< pFrameType = (unsigned short *) ((p_rxBD[i].address & 0xFFFFFFFC) + 12);
---
> pFrameType = (unsigned short *)((p_rxBD[i].address & 0xFFFFFFFC) + 12);
256c266
< switch (SWAP16(*pFrameType)) {
---
> switch (*pFrameType) {
258c268
< case PROTOCOL_ARP:
---
> case SWAP16(PROTOCOL_ARP):
260,263c270
<
< i = SWAP16(p_ARP->operation);
< if (i == ARP_REPLY) {
<
---
> if (p_ARP->operation == SWAP16(ARP_REPLY)) {
274,275c281
< } else if (i == ARP_REQUEST) {
<
---
> } else if (p_ARP->operation == SWAP16(ARP_REQUEST)) {
279,286c285,293
< // Swap the src/dst MAC addr
< p_memcpy(p_ARP->dest_mac, p_ARP->src_mac, 6);
< p_memcpy(p_ARP->src_mac, localMACAddr, 6);
<
< // Do IP and MAC addr at same time.
< p_memcpy(p_ARP->target_mac, p_ARP->sender_mac, 10);
< p_memcpy(p_ARP->sender_mac, localMACAddr, 6);
< p_memcpy(p_ARP->sender_ip, (char *)&localIPAddr, 4);
---
> // Fill the dest address and src address
> for (i = 0; i <6; i++) {
> // swap ethernet dest address and ethernet src address
> pData[i] = pData[i+6];
> pData[i+6] = localMACAddr[i];
> // swap sender ethernet address and target ethernet address
> pData[i+22] = localMACAddr[i];
> pData[i+32] = pData[i+6];
> }
288,289c295,299
< if (!(*AT91C_EMAC_TSR & AT91C_EMAC_BNQ))
< break;
---
> // swap sender IP address and target IP address
> for (i = 0; i<4; i++) {
> pData[i+38] = pData[i+28];
> pData[i+28] = localIPAddr[i];
> }
290a301,302
> if (!(*AT91C_EMAC_TSR & AT91C_EMAC_BNQ)) break;
>
295c307,310
< break;
---
> break;
> case SWAP16(PROTOCOL_IP):
> pIpHeader = (ip_header_t*)(pData + 14);
> p_memcpy((char*)pHeader, (char*)pIpHeader,sizeof(ip_header_t));
297,301c312
< case PROTOCOL_IP:
< pIpHeader = (ip_header_t*)(pData + 14);
< switch(pIpHeader->ip_p) {
< case PROTOCOL_UDP:
< {
---
> if (pIpHeader->ip_p == PROTOCOL_UDP) {
311c322
< if (tftpHdr->opcode != SWAP16(TFTP_DATA_OPCODE))
---
> if (tftpHdr->opcode != TFTP_DATA_OPCODE)
316,319c327,330
< break;
< serverPort = udpHdr->src_port;
< ackBlock = 0;
< }
---
> break;
> serverPort = udpHdr->src_port;
> ackBlock = 0;
> }
325,326c336,337
< tftpHdr->block_num,
< SWAP16(udpHdr->udp_len) - 12);
---
> SWAP16(tftpHdr->block_num),
> SWAP16(udpHdr->udp_len) - 12);
328,336d338
< break;
<
< default:
< break;
< }
< break;
<
< default:
< break;
338a341
> return (1);
359a363,373
> /*
> * .KB_C_FN_DEFINITION_START
> * unsigned short AT91F_MII_ReadPhy (AT91PS_EMAC pEmac, unsigned char addr)
> * This private function reads the PHY device.
> * .KB_C_FN_DEFINITION_END
> */
> #ifdef BOOT_TSC
> static unsigned short
> AT91F_MII_WritePhy (AT91PS_EMAC pEmac, unsigned char addr, unsigned short s)
> {
> unsigned value = 0x50020000 | (addr << 18) | s;
360a375,382
> pEmac->EMAC_CTL |= AT91C_EMAC_MPE;
> pEmac->EMAC_MAN = value;
> while(!((pEmac->EMAC_SR) & AT91C_EMAC_IDLE));
> pEmac->EMAC_CTL &= ~AT91C_EMAC_MPE;
> return (pEmac->EMAC_MAN & 0x0000ffff);
> }
> #endif
>
370,372c392,398
< unsigned short stat2;
< unsigned update = 0;
<
---
> unsigned short stat2;
> unsigned update;
> #ifdef BOOT_TSC
> unsigned sec;
> int i;
> #endif
> #ifdef BOOT_KB9202
374,375c400
<
< if (!(stat2 & 0x400)) {
---
> if (!(stat2 & MII_STS2_LINK))
377,379c402,403
<
< } else if (stat2 & 0x4000) {
<
---
> update = pEmac->EMAC_CFG & ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
> if (stat2 & MII_STS2_100TX)
381,383c405,416
<
< if (stat2 & 0x200) {
< update |= AT91C_EMAC_FD;
---
> if (stat2 & MII_STS2_FDX)
> update |= AT91C_EMAC_FD;
> #endif
> #ifdef BOOT_TSC
> while (1) {
> for (i = 0; i < 10; i++) {
> stat2 = AT91F_MII_ReadPhy(pEmac, MII_STS_REG);
> if (stat2 & MII_STS_LINK_STAT)
> break;
> printf(".");
> sec = GetSeconds();
> while (GetSeconds() <= sec) continue;
385,386c418,432
<
< } else if (stat2 & 0x200) {
---
> if (stat2 & MII_STS_LINK_STAT)
> break;
> printf("Resetting MII...");
> AT91F_MII_WritePhy(pEmac, 0x0, 0x8000);
> while (AT91F_MII_ReadPhy(pEmac, 0x0) & 0x8000) continue;
> }
> printf("emac: link");
> stat2 = AT91F_MII_ReadPhy(pEmac, MII_SPEC_STS_REG);
> update = pEmac->EMAC_CFG & ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
> if (stat2 & (MII_SSTS_100FDX | MII_SSTS_100HDX)) {
> printf(" 100TX");
> update |= AT91C_EMAC_SPD;
> }
> if (stat2 & (MII_SSTS_100FDX | MII_SSTS_10FDX)) {
> printf(" FDX");
389,391c435,437
<
< pEmac->EMAC_CFG =
< (pEmac->EMAC_CFG & ~(AT91C_EMAC_SPD | AT91C_EMAC_FD)) | update;
---
> printf("\r\n");
> #endif
> pEmac->EMAC_CFG = update;
408a455
>
417,419c464
< pEmac->EMAC_CTL = 0;
<
< if(!(pEmac->EMAC_SR & AT91C_EMAC_LINK))
---
> if (!(pEmac->EMAC_SR & AT91C_EMAC_LINK))
422,427d466
< // the sequence write EMAC_SA1L and write EMAC_SA1H must be respected
< pEmac->EMAC_SA1L = ((unsigned)localMACAddr[2] << 24) |
< ((unsigned)localMACAddr[3] << 16) | ((int)localMACAddr[4] << 8) |
< localMACAddr[5];
< pEmac->EMAC_SA1H = ((unsigned)localMACAddr[0] << 8) | localMACAddr[1];
<
430,433c469
< pEmac->EMAC_CFG |= AT91C_EMAC_CAF;
< pEmac->EMAC_CFG = (pEmac->EMAC_CFG & ~(AT91C_EMAC_CLK)) |
< AT91C_EMAC_CLK_HCLK_32;
< pEmac->EMAC_CTL |= (AT91C_EMAC_TE | AT91C_EMAC_RE);
---
> pEmac->EMAC_CTL = AT91C_EMAC_TE | AT91C_EMAC_RE;
450c486
< SetMACAddress(unsigned low_address, unsigned high_address)
---
> SetMACAddress(unsigned char mac[6])
452,453d487
<
< AT91PS_EMAC pEmac = AT91C_BASE_EMAC;
454a489
> AT91PS_EMAC pEmac = AT91C_BASE_EMAC;
459,468c494,496
< pEmac->EMAC_SA1L = low_address;
< pEmac->EMAC_SA1H = (high_address & 0x0000ffff);
<
< localMACAddr[0] = (low_address >> 0) & 0xFF;
< localMACAddr[1] = (low_address >> 8) & 0xFF;
< localMACAddr[2] = (low_address >> 16) & 0xFF;
< localMACAddr[3] = (low_address >> 24) & 0xFF;
< localMACAddr[4] = (high_address >> 0) & 0xFF;
< localMACAddr[5] = (high_address >> 8) & 0xFF;
<
---
> p_memcpy(localMACAddr, mac, 6);
> localMAClow = (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
> localMAChigh = (mac[0] << 8) | mac[1];
471,476c499,529
< // low_address & 0x000000ff = first byte in address
< // low_address & 0x0000ff00 = next
< // low_address & 0x00ff0000 = next
< // low_address & 0xff000000 = next
< // high_address & 0x000000ff = next
< // high_address & 0x0000ff00 = last byte in address
---
> AT91C_BASE_PMC->PMC_PCER = 1u << AT91C_ID_EMAC;
> AT91C_BASE_PIOA->PIO_ASR =
> AT91C_PA14_ERXER | AT91C_PA12_ERX0 | AT91C_PA13_ERX1 |
> AT91C_PA8_ETXEN | AT91C_PA16_EMDIO | AT91C_PA9_ETX0 |
> AT91C_PA10_ETX1 | AT91C_PA11_ECRS_ECRSDV | AT91C_PA15_EMDC |
> AT91C_PA7_ETXCK_EREFCK;
> AT91C_BASE_PIOA->PIO_PDR =
> AT91C_PA14_ERXER | AT91C_PA12_ERX0 | AT91C_PA13_ERX1 |
> AT91C_PA8_ETXEN | AT91C_PA16_EMDIO | AT91C_PA9_ETX0 |
> AT91C_PA10_ETX1 | AT91C_PA11_ECRS_ECRSDV | AT91C_PA15_EMDC |
> AT91C_PA7_ETXCK_EREFCK;
> #ifdef BOOT_KB9202 /* Really !RMII */
> AT91C_BASE_PIOB->PIO_BSR =
> AT91C_PB12_ETX2 | AT91C_PB13_ETX3 | AT91C_PB14_ETXER |
> AT91C_PB15_ERX2 | AT91C_PB16_ERX3 | AT91C_PB17_ERXDV |
> AT91C_PB18_ECOL | AT91C_PB19_ERXCK;
> AT91C_BASE_PIOB->PIO_PDR =
> AT91C_PB12_ETX2 | AT91C_PB13_ETX3 | AT91C_PB14_ETXER |
> AT91C_PB15_ERX2 | AT91C_PB16_ERX3 | AT91C_PB17_ERXDV |
> AT91C_PB18_ECOL | AT91C_PB19_ERXCK;
> #endif
> pEmac->EMAC_CTL = 0;
>
> pEmac->EMAC_CFG = (pEmac->EMAC_CFG & ~(AT91C_EMAC_CLK)) |
> #ifdef BOOT_TSC
> AT91C_EMAC_RMII |
> #endif
> AT91C_EMAC_CLK_HCLK_32 | AT91C_EMAC_CAF;
> // the sequence write EMAC_SA1L and write EMAC_SA1H must be respected
> pEmac->EMAC_SA1L = localMAClow;
> pEmac->EMAC_SA1H = localMAChigh;
491c544,550
< serverIPAddr = address;
---
>
> serverIPAddr[0] = (address >> 24) & 0xFF;
> serverIPAddr[1] = (address >> 16) & 0xFF;
> serverIPAddr[2] = (address >> 8) & 0xFF;
> serverIPAddr[3] = (address >> 0) & 0xFF;
>
> serverIPSet = 1;
506c565,571
< localIPAddr = address;
---
>
> localIPAddr[0] = (address >> 24) & 0xFF;
> localIPAddr[1] = (address >> 16) & 0xFF;
> localIPAddr[2] = (address >> 8) & 0xFF;
> localIPAddr[3] = (address >> 0) & 0xFF;
>
> localIPSet = 1;
521,522c586,588
< unsigned thisSeconds, running, state;
< int timeout, tickUpdate;
---
> ip_header_t IpHeader;
> unsigned thisSeconds;
> int timeout;
524,528c590
< if (!address) {
< // report last transfer information
< printf("Last tftp transfer info --\r\n"
< "address: 0x%x\r\n"
< " size: 0x%x\r\n", lastAddress, lastSize);
---
> if ((!localMACSet) || (!localIPSet) || (!serverIPSet))
530d591
< }
532,582d592
< if ((!localMACSet) || (!localIPAddr) || (!serverIPAddr))
< return ;
<
< if (!MAC_init) {
< AT91C_BASE_PMC->PMC_PCER = 1u << AT91C_ID_EMAC;
<
< AT91C_BASE_PIOA->PIO_ASR =
< AT91C_PA14_ERXER |
< AT91C_PA12_ERX0 |
< AT91C_PA13_ERX1 |
< AT91C_PA8_ETXEN |
< AT91C_PA16_EMDIO |
< AT91C_PA9_ETX0 |
< AT91C_PA10_ETX1 |
< AT91C_PA11_ECRS_ECRSDV |
< AT91C_PA15_EMDC |
< AT91C_PA7_ETXCK_EREFCK;
< AT91C_BASE_PIOA->PIO_BSR = 0;
< AT91C_BASE_PIOA->PIO_PDR =
< AT91C_PA14_ERXER |
< AT91C_PA12_ERX0 |
< AT91C_PA13_ERX1 |
< AT91C_PA8_ETXEN |
< AT91C_PA16_EMDIO |
< AT91C_PA9_ETX0 |
< AT91C_PA10_ETX1 |
< AT91C_PA11_ECRS_ECRSDV |
< AT91C_PA15_EMDC |
< AT91C_PA7_ETXCK_EREFCK;
< AT91C_BASE_PIOB->PIO_ASR = 0;
< AT91C_BASE_PIOB->PIO_BSR =
< AT91C_PB12_ETX2 |
< AT91C_PB13_ETX3 |
< AT91C_PB14_ETXER |
< AT91C_PB15_ERX2 |
< AT91C_PB16_ERX3 |
< AT91C_PB17_ERXDV |
< AT91C_PB18_ECOL |
< AT91C_PB19_ERXCK;
< AT91C_BASE_PIOB->PIO_PDR =
< AT91C_PB12_ETX2 |
< AT91C_PB13_ETX3 |
< AT91C_PB14_ETXER |
< AT91C_PB15_ERX2 |
< AT91C_PB16_ERX3 |
< AT91C_PB17_ERXDV |
< AT91C_PB18_ECOL |
< AT91C_PB19_ERXCK;
< MAC_init = 1;
< }
<
584d593
<
586d594
< lastAddress = address;
589,590d596
< running = 1;
< state = TFTP_WAITING_SERVER_MAC;
592c598
< thisSeconds = GetSeconds();
---
> thisSeconds = GetSeconds() + 1;
594c600
< localPort++; /* In network byte order, but who cares */
---
> ++localPort;
597,604c603,609
< while (running && timeout) {
<
< CheckForNewPacket();
<
< tickUpdate = 0;
<
< if (thisSeconds != GetSeconds()) {
< tickUpdate = 1;
---
> while (timeout) {
> if (CheckForNewPacket(&IpHeader)) {
> if (ackBlock == -2)
> break;
> timeout = 10;
> thisSeconds = GetSeconds() + 1;
> } else if (GetSeconds() > thisSeconds) {
606c611,621
< thisSeconds = GetSeconds();
---
> thisSeconds = GetSeconds() + 1;
> if (!serverMACSet)
> GetServerAddress();
> else if (ackBlock == -1)
> TFTP_RequestFile(filename);
> else {
> // Be sure to send a NAK, which is done by
> // ACKing the last block we got.
> TFTP_ACK_Data(0, ackBlock, 512);
> printf("\nNAK %u\r\n", ackBlock);
> }
608,644d622
<
< switch (state) {
<
< case TFTP_WAITING_SERVER_MAC:
< if (serverMACSet) {
< state = TFTP_SEND_REQUEST;
< break;
< }
<
< if (tickUpdate)
< GetServerAddress();
< break;
<
< case TFTP_SEND_REQUEST:
< // send request for file
< if (ackBlock != -1) {
< state = TFTP_GET_DATA;
< break;
< }
<
< if (tickUpdate)
< TFTP_RequestFile(filename);
< break;
<
< case TFTP_GET_DATA:
< // receiving data
< if (ackBlock == -2) {
< state = TFTP_COMPLETE;
< break;
< }
< break;
<
< case TFTP_COMPLETE:
< default:
< running = 0;
< break;
< }
645a624,625
> if (timeout == 0)
> printf("TFTP TIMEOUT!\r\n");
660a641,642
> localIPSet = 0;
> serverIPSet = 0;
662d643
< lastAddress = 0;
664d644
< MAC_init = 0;