2014-03-28 15:20:26 -06:00
|
|
|
|
/************************************************************************************
|
|
|
|
|
* configs/sama5d3-xplained/src/sam_ethernet.c
|
|
|
|
|
*
|
2017-03-02 08:42:13 -06:00
|
|
|
|
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
2014-03-28 15:20:26 -06:00
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
|
* distribution.
|
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
|
* without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2014-08-17 14:07:53 -06:00
|
|
|
|
/* Force verbose debug on in this file only to support unit-level testing. */
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_PHY_DEBUG
|
2016-06-11 11:50:18 -06:00
|
|
|
|
# undef CONFIG_DEBUG_INFO
|
|
|
|
|
# define CONFIG_DEBUG_INFO 1
|
2014-08-17 14:07:53 -06:00
|
|
|
|
# undef CONFIG_DEBUG_NET
|
|
|
|
|
# define CONFIG_DEBUG_NET 1
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-16 08:35:31 -06:00
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
2014-08-16 15:04:09 -06:00
|
|
|
|
#include <debug.h>
|
2014-08-16 08:35:31 -06:00
|
|
|
|
|
2014-03-28 15:20:26 -06:00
|
|
|
|
#include <nuttx/irq.h>
|
2014-08-16 08:35:31 -06:00
|
|
|
|
#include <nuttx/arch.h>
|
2014-03-28 15:20:26 -06:00
|
|
|
|
|
|
|
|
|
#include "sam_pio.h"
|
|
|
|
|
#include "sam_ethernet.h"
|
|
|
|
|
|
2014-08-16 15:04:09 -06:00
|
|
|
|
#include "sama5d3-xplained.h"
|
|
|
|
|
|
2014-03-28 15:20:26 -06:00
|
|
|
|
#ifdef HAVE_NETWORK
|
|
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
2014-08-16 08:35:31 -06:00
|
|
|
|
#ifndef CONFIG_SAMA5_EMACA
|
|
|
|
|
# undef CONFIG_SAMA5_EMAC_ISETH0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_EMAC_ISETH0
|
2014-08-16 15:04:09 -06:00
|
|
|
|
# define SAMA5_EMAC_DEVNAME "eth0"
|
|
|
|
|
# define SAMA5_GMAC_DEVNAME "eth1"
|
2014-08-16 08:35:31 -06:00
|
|
|
|
#else
|
2014-08-16 15:04:09 -06:00
|
|
|
|
# define SAMA5_GMAC_DEVNAME "eth0"
|
|
|
|
|
# define SAMA5_EMAC_DEVNAME "eth1"
|
2014-08-16 08:35:31 -06:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-17 14:07:53 -06:00
|
|
|
|
/* Debug ********************************************************************/
|
|
|
|
|
/* Extra, in-depth debug output that is only available if
|
|
|
|
|
* CONFIG_NETDEV_PHY_DEBUG us defined.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_PHY_DEBUG
|
2016-06-16 12:33:32 -06:00
|
|
|
|
# define phyerr _err
|
|
|
|
|
# define phywarn _warn
|
|
|
|
|
# define phyinfo _info
|
2014-08-17 14:07:53 -06:00
|
|
|
|
#else
|
2016-06-11 15:50:49 -06:00
|
|
|
|
# define phyerr(x...)
|
2016-06-13 07:43:55 -06:00
|
|
|
|
# define phywarn(x...)
|
|
|
|
|
# define phyinfo(x...)
|
2014-08-17 14:07:53 -06:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-03-28 15:20:26 -06:00
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
2014-08-17 16:51:56 -06:00
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Name: sam_emac_phy_enable and sam_gmac_enable
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_PIOE_IRQ
|
|
|
|
|
#ifdef CONFIG_SAMA5_EMACA
|
|
|
|
|
static void sam_emac_phy_enable(bool enable)
|
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("IRQ%d: enable=%d\n", IRQ_INT_ETH1, enable);
|
2014-08-17 16:51:56 -06:00
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
sam_pioirqenable(IRQ_INT_ETH1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sam_pioirqdisable(IRQ_INT_ETH1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_GMAC
|
|
|
|
|
static void sam_gmac_phy_enable(bool enable)
|
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("IRQ%d: enable=%d\n", IRQ_INT_ETH0, enable);
|
2014-08-17 16:51:56 -06:00
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
sam_pioirqenable(IRQ_INT_ETH0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sam_pioirqdisable(IRQ_INT_ETH0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-03-28 15:20:26 -06:00
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
|
|
/************************************************************************************
|
|
|
|
|
* Name: sam_netinitialize
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Configure board resources to support networking.
|
|
|
|
|
*
|
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
|
|
void weak_function sam_netinitialize(void)
|
|
|
|
|
{
|
2014-08-16 15:04:09 -06:00
|
|
|
|
#ifdef CONFIG_SAMA5_EMACA
|
2014-06-10 17:40:25 -06:00
|
|
|
|
/* Ethernet 10/100 (EMAC A) Port
|
2014-03-28 15:20:26 -06:00
|
|
|
|
*
|
|
|
|
|
* The main board contains a MICREL PHY device (KSZ8051) operating at 10/100 Mbps.
|
|
|
|
|
* The board supports MII and RMII interface modes.
|
|
|
|
|
*
|
|
|
|
|
* The two independent PHY devices embedded on CM and MB boards are connected to
|
|
|
|
|
* independent RJ-45 connectors with built-in magnetic and status LEDs.
|
|
|
|
|
*
|
|
|
|
|
* At the De-Assertion of Reset:
|
|
|
|
|
* PHY ADD[2:0]:001
|
|
|
|
|
* CONFIG[2:0]:001,Mode:RMII
|
|
|
|
|
* Duplex Mode:Half Duplex
|
|
|
|
|
* Isolate Mode:Disable
|
|
|
|
|
* Speed Mode:100Mbps
|
|
|
|
|
* Nway Auto-Negotiation:Enable
|
|
|
|
|
*
|
2014-08-17 14:07:53 -06:00
|
|
|
|
* The KSZ8051 PHY interrupt is available on PE30 INT_ETH1
|
2014-03-28 15:20:26 -06:00
|
|
|
|
*/
|
|
|
|
|
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Configuring %08x\n", PIO_INT_ETH1);
|
2014-03-28 15:20:26 -06:00
|
|
|
|
sam_configpio(PIO_INT_ETH1);
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-16 15:04:09 -06:00
|
|
|
|
#ifdef CONFIG_SAMA5_GMAC
|
2014-03-28 15:20:26 -06:00
|
|
|
|
/* Tri-Speed Ethernet PHY
|
|
|
|
|
*
|
|
|
|
|
* The SAMA5D3 series-CM board is equipped with a MICREL PHY devices (MICREL
|
|
|
|
|
* KSZ9021/31) operating at 10/100/1000 Mbps. The board supports RGMII interface
|
|
|
|
|
* mode. The Ethernet interface consists of 4 pairs of low voltage differential
|
|
|
|
|
* pair signals designated from GRX<EFBFBD> and GTx<EFBFBD> plus control signals for link
|
|
|
|
|
* activity indicators. These signals can be used to connect to a 10/100/1000
|
|
|
|
|
* BaseT RJ45 connector integrated on the main board.
|
|
|
|
|
*
|
|
|
|
|
* The KSZ9021/31 interrupt is available on PB35 INT_GETH0
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Configuring %08x\n", PIO_INT_ETH0);
|
2014-03-28 15:20:26 -06:00
|
|
|
|
sam_configpio(PIO_INT_ETH0);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 08:35:31 -06:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: arch_phy_irq
|
2014-03-28 15:20:26 -06:00
|
|
|
|
*
|
|
|
|
|
* Description:
|
2014-08-16 08:35:31 -06:00
|
|
|
|
* This function may be called to register an interrupt handler that will
|
|
|
|
|
* be called when a PHY interrupt occurs. This function both attaches
|
|
|
|
|
* the interrupt handler and enables the interrupt if 'handler' is non-
|
|
|
|
|
* NULL. If handler is NULL, then the interrupt is detached and disabled
|
|
|
|
|
* instead.
|
2014-03-28 15:20:26 -06:00
|
|
|
|
*
|
2014-08-17 16:51:56 -06:00
|
|
|
|
* The PHY interrupt is always disabled upon return. The caller must
|
|
|
|
|
* call back through the enable function point to control the state of
|
|
|
|
|
* the interrupt.
|
|
|
|
|
*
|
2014-08-16 08:35:31 -06:00
|
|
|
|
* This interrupt may or may not be available on a given platform depending
|
|
|
|
|
* on how the network hardware architecture is implemented. In a typical
|
|
|
|
|
* case, the PHY interrupt is provided to board-level logic as a GPIO
|
|
|
|
|
* interrupt (in which case this is a board-specific interface and really
|
|
|
|
|
* should be called board_phy_irq()); In other cases, the PHY interrupt
|
|
|
|
|
* may be cause by the chip's MAC logic (in which case arch_phy_irq()) is
|
|
|
|
|
* an appropriate name. Other other boards, there may be no PHY interrupts
|
|
|
|
|
* available at all. If client attachable PHY interrupts are available
|
|
|
|
|
* from the board or from the chip, then CONFIG_ARCH_PHY_INTERRUPT should
|
|
|
|
|
* be defined to indicate that fact.
|
|
|
|
|
*
|
|
|
|
|
* Typical usage:
|
|
|
|
|
* a. OS service logic (not application logic*) attaches to the PHY
|
2014-08-17 16:51:56 -06:00
|
|
|
|
* PHY interrupt and enables the PHY interrupt.
|
|
|
|
|
* b. When the PHY interrupt occurs: (1) the interrupt should be
|
|
|
|
|
* disabled and () work should be scheduled on the worker thread (or
|
|
|
|
|
* perhaps a dedicated application thread).
|
2014-08-16 08:35:31 -06:00
|
|
|
|
* c. That worker thread should use the SIOCGMIIPHY, SIOCGMIIREG,
|
|
|
|
|
* and SIOCSMIIREG ioctl calls** to communicate with the PHY,
|
|
|
|
|
* determine what network event took place (Link Up/Down?), and
|
|
|
|
|
* take the appropriate actions.
|
2017-05-11 13:35:56 -06:00
|
|
|
|
* d. It should then interact the PHY to clear any pending
|
2014-08-17 16:51:56 -06:00
|
|
|
|
* interrupts, then re-enable the PHY interrupt.
|
2014-08-16 08:35:31 -06:00
|
|
|
|
*
|
|
|
|
|
* * This is an OS internal interface and should not be used from
|
2014-08-17 16:51:56 -06:00
|
|
|
|
* application space. Rather applications should use the SIOCMIISIG
|
|
|
|
|
* ioctl to receive a signal when a PHY event occurs.
|
2014-08-16 08:35:31 -06:00
|
|
|
|
* ** This interrupt is really of no use if the Ethernet MAC driver
|
|
|
|
|
* does not support these ioctl calls.
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* intf - Identifies the network interface. For example "eth0". Only
|
|
|
|
|
* useful on platforms that support multiple Ethernet interfaces
|
|
|
|
|
* and, hence, multiple PHYs and PHY interrupts.
|
|
|
|
|
* handler - The client interrupt handler to be invoked when the PHY
|
|
|
|
|
* asserts an interrupt. Must reside in OS space, but can
|
|
|
|
|
* signal tasks in user space. A value of NULL can be passed
|
|
|
|
|
* in order to detach and disable the PHY interrupt.
|
2017-03-02 08:42:13 -06:00
|
|
|
|
* arg - The argument that will accompany the interrupt
|
2014-08-17 16:51:56 -06:00
|
|
|
|
* enable - A function pointer that be unsed to enable or disable the
|
|
|
|
|
* PHY interrupt.
|
2014-08-16 08:35:31 -06:00
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
2017-03-02 08:42:13 -06:00
|
|
|
|
* Zero (OK) returned on success; a negated errno value is returned on
|
|
|
|
|
* failure.
|
2014-08-16 08:35:31 -06:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2014-03-28 15:20:26 -06:00
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMA5_PIOE_IRQ
|
2017-03-02 08:42:13 -06:00
|
|
|
|
int arch_phy_irq(FAR const char *intf, xcpt_t handler, void *arg,
|
|
|
|
|
phy_enable_t *enable)
|
2014-03-28 15:20:26 -06:00
|
|
|
|
{
|
|
|
|
|
irqstate_t flags;
|
2014-07-07 15:54:37 -06:00
|
|
|
|
pio_pinset_t pinset;
|
2014-08-17 16:51:56 -06:00
|
|
|
|
phy_enable_t enabler;
|
2014-03-28 15:20:26 -06:00
|
|
|
|
int irq;
|
|
|
|
|
|
2014-08-16 08:35:31 -06:00
|
|
|
|
DEBUGASSERT(intf);
|
|
|
|
|
|
2016-06-11 11:59:51 -06:00
|
|
|
|
ninfo("%s: handler=%p\n", intf, handler);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
#ifdef CONFIG_SAMA5_EMACA
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("EMAC: devname=%s\n", SAMA5_EMAC_DEVNAME);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef CONFIG_SAMA5_GMAC
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("GMAC: devname=%s\n", SAMA5_GMAC_DEVNAME);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-06-10 17:40:25 -06:00
|
|
|
|
#ifdef CONFIG_SAMA5_EMACA
|
2014-08-16 08:35:31 -06:00
|
|
|
|
if (strcmp(intf, SAMA5_EMAC_DEVNAME) == 0)
|
2014-03-28 15:20:26 -06:00
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Select EMAC\n");
|
2014-08-16 15:04:09 -06:00
|
|
|
|
pinset = PIO_INT_ETH1;
|
|
|
|
|
irq = IRQ_INT_ETH1;
|
2014-08-17 16:51:56 -06:00
|
|
|
|
enabler = sam_emac_phy_enable;
|
2014-03-28 15:20:26 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef CONFIG_SAMA5_GMAC
|
2014-08-16 08:35:31 -06:00
|
|
|
|
if (strcmp(intf, SAMA5_GMAC_DEVNAME) == 0)
|
2014-03-28 15:20:26 -06:00
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Select GMAC\n");
|
2014-08-16 15:04:09 -06:00
|
|
|
|
pinset = PIO_INT_ETH0;
|
|
|
|
|
irq = IRQ_INT_ETH0;
|
2014-08-17 16:51:56 -06:00
|
|
|
|
enabler = sam_gmac_phy_enable;
|
2014-03-28 15:20:26 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
nerr("ERROR: Unsupported interface: %s\n", intf);
|
2014-03-28 15:20:26 -06:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Disable interrupts until we are done. This guarantees that the
|
|
|
|
|
* following operations are atomic.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-13 12:37:46 -06:00
|
|
|
|
flags = enter_critical_section();
|
2014-03-28 15:20:26 -06:00
|
|
|
|
|
|
|
|
|
/* Configure the interrupt */
|
|
|
|
|
|
2014-08-17 14:07:53 -06:00
|
|
|
|
if (handler)
|
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Configure pin: %08x\n", pinset);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
sam_pioirq(pinset);
|
|
|
|
|
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Attach IRQ%d\n", irq);
|
2017-03-02 08:42:13 -06:00
|
|
|
|
(void)irq_attach(irq, handler, arg);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-13 07:43:55 -06:00
|
|
|
|
phyinfo("Detach IRQ%d\n", irq);
|
2014-08-17 14:07:53 -06:00
|
|
|
|
(void)irq_detach(irq);
|
2014-08-17 16:51:56 -06:00
|
|
|
|
enabler = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return with the interrupt disabled in either case */
|
|
|
|
|
|
|
|
|
|
sam_pioirqdisable(irq);
|
|
|
|
|
|
|
|
|
|
/* Return the enabling function pointer */
|
|
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
*enable = enabler;
|
2014-08-17 14:07:53 -06:00
|
|
|
|
}
|
2014-03-28 15:20:26 -06:00
|
|
|
|
|
2014-09-29 12:22:31 -06:00
|
|
|
|
/* Return the old handler (so that it can be restored) */
|
2014-03-28 15:20:26 -06:00
|
|
|
|
|
2016-02-13 12:37:46 -06:00
|
|
|
|
leave_critical_section(flags);
|
2017-03-02 08:42:13 -06:00
|
|
|
|
return OK;
|
2014-03-28 15:20:26 -06:00
|
|
|
|
}
|
|
|
|
|
#endif /* CONFIG_SAMA5_PIOE_IRQ */
|
|
|
|
|
|
|
|
|
|
#endif /* HAVE_NETWORK */
|