199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2006 M. Warner Losh. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software is derived from software provide by Kwikbyte who specifically
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disclaimed copyright on the code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/******************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Filename: emac.c
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Instantiation of routines for MAC/ethernet functions supporting tftp.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Revision information:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 28AUG2004 kb_admin initial creation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 08JAN2005 kb_admin added tftp download
199767f8919635c4928607450d9e0abb932109ceToomas Soome * also adapted from external sources
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BEGIN_KBDD_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome * No warranty, expressed or implied, is included with this software. It is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provided "AS IS" and no warranty of any kind including statutory or aspects
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relating to merchantability or fitness for any purpose is provided. All
199767f8919635c4928607450d9e0abb932109ceToomas Soome * intellectual property rights of others is maintained with the respective
199767f8919635c4928607450d9e0abb932109ceToomas Soome * owners. This software is not copyrighted and is intended for reference
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * END_BLOCK
199767f8919635c4928607450d9e0abb932109ceToomas Soome ******************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "at91rm9200.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "at91rm9200_lowlevel.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "emac.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "lib.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ****************************** GLOBALS *************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunsigned localMACSet;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunsigned char localMACAddr[6];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunsigned localMAClow, localMAChigh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ********************** PRIVATE FUNCTIONS/DATA ******************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_START
199767f8919635c4928607450d9e0abb932109ceToomas Soome * void EMAC_SetMACAddress(unsigned low_address, unsigned high_address)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This global function sets the MAC address. low_address is the first
199767f8919635c4928607450d9e0abb932109ceToomas Soome * four bytes while high_address is the last 2 bytes of the 48-bit value.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * .KB_C_FN_DEFINITION_END
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEMAC_SetMACAddress(unsigned char mac[6])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91PS_PMC pPMC = AT91C_BASE_PMC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91PS_EMAC pEmac = AT91C_BASE_EMAC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* enable the peripheral clock before using EMAC */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pPMC->PMC_PCER = ((unsigned) 1 << AT91C_ID_EMAC);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(localMACAddr, mac, 6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome localMAClow = (mac[3] << 24) | (mac[2] << 16) | (mac[1] << 8) | mac[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome localMAChigh = (mac[5] << 8) | mac[4];
199767f8919635c4928607450d9e0abb932109ceToomas Soome localMACSet = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PMC->PMC_PCER = 1u << AT91C_ID_EMAC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PIOA->PIO_ASR =
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA14 | AT91C_PIO_PA12 | AT91C_PIO_PA13 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA8 | AT91C_PIO_PA16 | AT91C_PIO_PA9 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA10 | AT91C_PIO_PA11 | AT91C_PIO_PA15 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA7;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PIOA->PIO_PDR =
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA14 | AT91C_PIO_PA12 | AT91C_PIO_PA13 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA8 | AT91C_PIO_PA16 | AT91C_PIO_PA9 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA10 | AT91C_PIO_PA11 | AT91C_PIO_PA15 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PA7;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(BOOT_KB920X) | defined(BOOT_BWCT) /* Really !RMII */
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PIOB->PIO_BSR =
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB12 | AT91C_PIO_PB13 | AT91C_PIO_PB14 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB15 | AT91C_PIO_PB16 | AT91C_PIO_PB17 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB18 | AT91C_PIO_PB19;
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_BASE_PIOB->PIO_PDR =
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB12 | AT91C_PIO_PB13 | AT91C_PIO_PB14 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB15 | AT91C_PIO_PB16 | AT91C_PIO_PB17 |
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_PIO_PB18 | AT91C_PIO_PB19;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome pEmac->EMAC_CTL = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pEmac->EMAC_CFG = (pEmac->EMAC_CFG & ~(AT91C_EMAC_CLK)) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef BOOT_TSC
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_EMAC_RMII |
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome AT91C_EMAC_CLK_HCLK_32 | AT91C_EMAC_CAF;
199767f8919635c4928607450d9e0abb932109ceToomas Soome // the sequence write EMAC_SA1L and write EMAC_SA1H must be respected
199767f8919635c4928607450d9e0abb932109ceToomas Soome pEmac->EMAC_SA1L = localMAClow;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pEmac->EMAC_SA1H = localMAChigh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}