2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/net/enc28j60.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* References:
|
|
|
|
* - ENC28J60 Data Sheet, Stand-Alone Ethernet Controller with SPI Interface,
|
|
|
|
* DS39662C, 2008 Microchip Technology Inc.
|
|
|
|
*/
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2012-09-19 01:06:22 +02:00
|
|
|
|
2012-09-11 18:50:16 +02:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_ENC28J60)
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2010-04-26 01:05:35 +02:00
|
|
|
#include <stdint.h>
|
2010-04-25 23:24:12 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2010-04-25 23:24:12 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/irq.h>
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/arch.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/wdog.h>
|
2013-07-01 16:11:54 +02:00
|
|
|
#include <nuttx/spi/spi.h>
|
2010-04-26 14:46:08 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2010-04-29 03:55:16 +02:00
|
|
|
#include <nuttx/clock.h>
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/net/enc28j60.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2023-08-18 22:23:01 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2015-01-20 22:14:29 +01:00
|
|
|
#ifdef CONFIG_NET_PKT
|
|
|
|
# include <nuttx/net/pkt.h>
|
|
|
|
#endif
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
#include "enc28j60.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 15:15:32 +02:00
|
|
|
* Pre-processor Definitions
|
2010-04-25 23:24:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2010-05-01 21:52:50 +02:00
|
|
|
/* ENC28J60 Configuration Settings:
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2012-09-11 18:50:16 +02:00
|
|
|
* CONFIG_ENC28J60 - Enabled ENC28J60 support
|
2010-04-25 23:24:12 +02:00
|
|
|
* CONFIG_ENC28J60_SPIMODE - Controls the SPI mode
|
|
|
|
* CONFIG_ENC28J60_FREQUENCY - Define to use a different bus frequency
|
|
|
|
* CONFIG_ENC28J60_NINTERFACES - Specifies the number of physical ENC28J60
|
|
|
|
* devices that will be supported.
|
2010-05-01 21:52:50 +02:00
|
|
|
* CONFIG_ENC28J60_HALFDUPPLEX - Default is full duplex
|
2010-05-01 21:21:06 +02:00
|
|
|
*/
|
|
|
|
|
2012-09-12 16:07:13 +02:00
|
|
|
/* The ENC28J60 spec says that it supports SPI mode 0,0 only: "The
|
|
|
|
* implementation used on this device supports SPI mode 0,0 only. In
|
|
|
|
* addition, the SPI port requires that SCK be at Idle in a low state;
|
|
|
|
* selectable clock polarity is not supported." However, sometimes you
|
|
|
|
* need to tinker with these things.
|
2010-04-25 23:24:12 +02:00
|
|
|
*/
|
|
|
|
|
2010-04-26 01:05:35 +02:00
|
|
|
#ifndef CONFIG_ENC28J60_SPIMODE
|
2012-09-12 16:07:13 +02:00
|
|
|
# define CONFIG_ENC28J60_SPIMODE SPIDEV_MODE0
|
2010-04-25 23:24:12 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* CONFIG_ENC28J60_NINTERFACES determines the number of physical interfaces
|
|
|
|
* that will be supported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_ENC28J60_NINTERFACES
|
2010-05-01 04:14:21 +02:00
|
|
|
# define CONFIG_ENC28J60_NINTERFACES 1
|
|
|
|
#endif
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
/* CONFIG_NET_ETH_PKTSIZE must always be defined */
|
2010-05-01 04:14:21 +02:00
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
#if !defined(CONFIG_NET_ETH_PKTSIZE) && (CONFIG_NET_ETH_PKTSIZE <= MAX_FRAMELEN)
|
|
|
|
# error "CONFIG_NET_ETH_PKTSIZE is not valid for the ENC28J60"
|
2010-04-25 23:24:12 +02:00
|
|
|
#endif
|
|
|
|
|
2010-04-26 14:46:08 +02:00
|
|
|
/* We need to have the work queue to handle SPI interrupts */
|
|
|
|
|
2016-11-18 15:41:27 +01:00
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
2010-04-26 14:46:08 +02:00
|
|
|
# error "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)"
|
|
|
|
#endif
|
|
|
|
|
2018-11-21 14:57:26 +01:00
|
|
|
/* The low priority work queue is preferred. If it is not enabled, LPWORK
|
|
|
|
* will be the same as HPWORK.
|
|
|
|
*
|
|
|
|
* NOTE: However, the network should NEVER run on the high priority work
|
|
|
|
* queue! That queue is intended only to service short back end interrupt
|
|
|
|
* processing that never suspends. Suspending the high priority work queue
|
|
|
|
* may bring the system to its knees!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ENCWORK LPWORK
|
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* CONFIG_ENC28J60_DUMPPACKET will dump the contents of each packet. */
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_ENC28J60_DUMPPACKET
|
|
|
|
# define enc_dumppacket(m,a,n) lib_dumpbuffer(m,a,n)
|
|
|
|
#else
|
|
|
|
# define enc_dumppacket(m,a,n)
|
|
|
|
#endif
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Low-level register debug */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#if !defined(CONFIG_DEBUG_FEATURES) || !defined(CONFIG_DEBUG_NET)
|
2012-09-18 01:02:58 +02:00
|
|
|
# undef CONFIG_ENC28J60_REGDEBUG
|
|
|
|
#endif
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Timing *******************************************************************/
|
|
|
|
|
|
|
|
/* TX timeout = 1 minute */
|
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
#define ENC_TXTIMEOUT (60*CLK_TCK)
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Poll timeout */
|
|
|
|
|
|
|
|
#define ENC_POLLTIMEOUT MSEC2TICK(50)
|
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
/* Packet Memory ************************************************************/
|
|
|
|
|
|
|
|
/* Packet memory layout */
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
#define ALIGNED_BUFSIZE ((CONFIG_NET_ETH_PKTSIZE + 255) & ~255)
|
2010-05-01 04:14:21 +02:00
|
|
|
|
2013-06-23 01:01:44 +02:00
|
|
|
/* Work around Errata #5 (spurious reset of ERXWRPT to 0) by placing the RX
|
|
|
|
* FIFO at the beginning of packet memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ERRATA5 1
|
|
|
|
#if ERRATA5
|
|
|
|
# define PKTMEM_RX_START 0x0000 /* RX buffer must be at addr 0 for errata 5 */
|
|
|
|
# define PKTMEM_RX_END (PKTMEM_END-ALIGNED_BUFSIZE) /* RX buffer length is total SRAM minus TX buffer */
|
|
|
|
# define PKTMEM_TX_START (PKTMEM_RX_END+1) /* Start TX buffer after */
|
|
|
|
# define PKTMEM_TX_ENDP1 (PKTMEM_TX_START+ALIGNED_BUFSIZE) /* Allow TX buffer for one frame */
|
2013-06-22 16:55:16 +02:00
|
|
|
#else
|
2013-06-23 01:01:44 +02:00
|
|
|
# define PKTMEM_TX_START 0x0000 /* Start TX buffer at 0 */
|
|
|
|
# define PKTMEM_TX_ENDP1 ALIGNED_BUFSIZE /* Allow TX buffer for one frame */
|
|
|
|
# define PKTMEM_RX_START PKTMEM_TX_ENDP1 /* Followed by RX buffer */
|
|
|
|
# define PKTMEM_RX_END PKTMEM_END /* RX buffer goes to the end of SRAM */
|
2013-06-22 16:55:16 +02:00
|
|
|
#endif
|
2010-04-29 03:55:16 +02:00
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Misc. Helper Macros ******************************************************/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
#define enc_rdgreg(priv,ctrlreg) \
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_rdgreg2(priv, ENC_RCR | GETADDR(ctrlreg))
|
2010-04-30 03:43:46 +02:00
|
|
|
#define enc_wrgreg(priv,ctrlreg,wrdata) \
|
|
|
|
enc_wrgreg2(priv, ENC_WCR | GETADDR(ctrlreg), wrdata)
|
2010-04-27 05:52:56 +02:00
|
|
|
#define enc_bfcgreg(priv,ctrlreg,clrbits) \
|
2010-04-30 03:43:46 +02:00
|
|
|
enc_wrgreg2(priv, ENC_BFC | GETADDR(ctrlreg), clrbits)
|
2010-04-27 05:52:56 +02:00
|
|
|
#define enc_bfsgreg(priv,ctrlreg,setbits) \
|
2010-04-30 03:43:46 +02:00
|
|
|
enc_wrgreg2(priv, ENC_BFS | GETADDR(ctrlreg), setbits)
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2022-08-04 19:47:53 +02:00
|
|
|
/* Packet buffer size */
|
|
|
|
|
|
|
|
#define PKTBUF_SIZE (MAX_NETDEV_PKTSIZE + CONFIG_NET_GUARDSIZE)
|
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* This is a helper pointer for accessing the contents of Ethernet header */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-01-17 23:09:14 +01:00
|
|
|
#define BUF ((FAR struct eth_hdr_s *)priv->dev.d_buf)
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Debug ********************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENC28J60_REGDEBUG
|
2014-10-08 18:18:58 +02:00
|
|
|
# define enc_wrdump(a,v) \
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "ENC28J60: %02x<-%02x\n", a, v);
|
2014-10-08 18:18:58 +02:00
|
|
|
# define enc_rddump(a,v) \
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "ENC28J60: %02x->%02x\n", a, v);
|
2014-10-08 18:18:58 +02:00
|
|
|
# define enc_cmddump(c) \
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "ENC28J60: CMD: %02x\n", c);
|
2014-10-08 18:18:58 +02:00
|
|
|
# define enc_bmdump(c,b,s) \
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "ENC28J60: CMD: %02x buffer: %p length: %d\n", c, b, s);
|
2012-09-18 01:02:58 +02:00
|
|
|
#else
|
|
|
|
# define enc_wrdump(a,v)
|
|
|
|
# define enc_rddump(a,v)
|
|
|
|
# define enc_cmddump(c)
|
|
|
|
# define enc_bmdump(c,b,s)
|
|
|
|
#endif
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* The state of the interface */
|
|
|
|
|
|
|
|
enum enc_state_e
|
|
|
|
{
|
2012-09-17 20:35:37 +02:00
|
|
|
ENCSTATE_UNINIT = 0, /* The interface is in an uninitialized state */
|
2012-09-11 22:33:58 +02:00
|
|
|
ENCSTATE_DOWN, /* The interface is down */
|
|
|
|
ENCSTATE_UP /* The interface is up */
|
|
|
|
};
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* The enc_driver_s encapsulates all state information for a single hardware
|
2010-04-25 23:24:12 +02:00
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
struct enc_driver_s
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-26 14:46:08 +02:00
|
|
|
/* Device control */
|
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
uint8_t ifstate; /* Interface state: See ENCSTATE_* */
|
2012-09-11 18:50:16 +02:00
|
|
|
uint8_t bank; /* Currently selected bank */
|
|
|
|
uint16_t nextpkt; /* Next packet address */
|
|
|
|
FAR const struct enc_lower_s *lower; /* Low-level MCU-specific support */
|
2010-04-26 14:46:08 +02:00
|
|
|
|
|
|
|
/* Timing */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
struct wdog_s txtimeout; /* TX timeout timer */
|
2010-04-26 14:46:08 +02:00
|
|
|
|
2012-09-12 02:12:18 +02:00
|
|
|
/* If we don't own the SPI bus, then we cannot do SPI accesses from the
|
2010-04-26 14:46:08 +02:00
|
|
|
* interrupt handler.
|
|
|
|
*/
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
struct work_s irqwork; /* Interrupt continuation work queue support */
|
|
|
|
struct work_s towork; /* Tx timeout work queue support */
|
|
|
|
struct work_s pollwork; /* Poll timeout work queue support */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2021-12-14 12:11:41 +01:00
|
|
|
/* This is the contained SPI driver instance */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-11 18:50:16 +02:00
|
|
|
FAR struct spi_dev_s *spi;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* This holds the information visible to the NuttX network */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
struct net_driver_s dev; /* Interface understood by the network */
|
2010-04-25 23:24:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-11-29 23:44:23 +01:00
|
|
|
/* A single packet buffer is used */
|
|
|
|
|
2022-08-04 19:47:53 +02:00
|
|
|
static uint16_t g_pktbuf[CONFIG_ENC28J60_NINTERFACES][(PKTBUF_SIZE + 1) / 2];
|
2016-11-29 23:44:23 +01:00
|
|
|
|
|
|
|
/* Driver status structure */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static struct enc_driver_s g_enc28j60[CONFIG_ENC28J60_NINTERFACES];
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Low-level SPI helpers */
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
static inline void enc_configspi(FAR struct spi_dev_s *spi);
|
|
|
|
static void enc_lock(FAR struct enc_driver_s *priv);
|
|
|
|
static inline void enc_unlock(FAR struct enc_driver_s *priv);
|
2010-04-26 01:26:16 +02:00
|
|
|
|
|
|
|
/* SPI control register access */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static uint8_t enc_rdgreg2(FAR struct enc_driver_s *priv, uint8_t cmd);
|
2010-04-30 03:43:46 +02:00
|
|
|
static void enc_wrgreg2(FAR struct enc_driver_s *priv, uint8_t cmd,
|
2010-04-27 05:03:40 +02:00
|
|
|
uint8_t wrdata);
|
2012-09-12 02:12:18 +02:00
|
|
|
static inline void enc_src(FAR struct enc_driver_s *priv);
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_setbank(FAR struct enc_driver_s *priv, uint8_t bank);
|
|
|
|
static uint8_t enc_rdbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg);
|
2010-04-28 03:25:38 +02:00
|
|
|
static void enc_wrbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
|
2010-04-27 05:03:40 +02:00
|
|
|
uint8_t wrdata);
|
2012-09-12 02:12:18 +02:00
|
|
|
static int enc_waitbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
|
2010-04-30 03:43:46 +02:00
|
|
|
uint8_t bits, uint8_t value);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-19 01:06:22 +02:00
|
|
|
#if 0 /* Sometimes useful */
|
|
|
|
static void enc_rxdump(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_txdump(FAR struct enc_driver_s *priv);
|
|
|
|
#endif
|
|
|
|
|
2010-04-26 01:26:16 +02:00
|
|
|
/* SPI buffer transfers */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_rdbuffer(FAR struct enc_driver_s *priv, FAR uint8_t *buffer,
|
|
|
|
size_t buflen);
|
2012-09-18 01:02:58 +02:00
|
|
|
static inline void enc_wrbuffer(FAR struct enc_driver_s *priv,
|
2010-04-26 01:26:16 +02:00
|
|
|
FAR const uint8_t *buffer, size_t buflen);
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* PHY register access */
|
|
|
|
|
|
|
|
static uint16_t enc_rdphy(FAR struct enc_driver_s *priv, uint8_t phyaddr);
|
|
|
|
static void enc_wrphy(FAR struct enc_driver_s *priv, uint8_t phyaddr,
|
|
|
|
uint16_t phydata);
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Common TX logic */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static int enc_transmit(FAR struct enc_driver_s *priv);
|
2014-07-02 02:41:08 +02:00
|
|
|
static int enc_txpoll(struct net_driver_s *dev);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/* Interrupt handling */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_linkstatus(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_txif(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_txerif(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_txerif(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_rxerif(FAR struct enc_driver_s *priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_rxdispatch(FAR struct enc_driver_s *priv);
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_pktif(FAR struct enc_driver_s *priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_irqworker(FAR void *arg);
|
2017-02-28 02:28:24 +01:00
|
|
|
static int enc_interrupt(int irq, FAR void *context, FAR void *arg);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/* Watchdog timer expirations */
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_toworker(FAR void *arg);
|
2020-08-09 20:29:35 +02:00
|
|
|
static void enc_txtimeout(wdparm_t arg);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/* NuttX callback functions */
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_ifup(struct net_driver_s *dev);
|
|
|
|
static int enc_ifdown(struct net_driver_s *dev);
|
|
|
|
static int enc_txavail(struct net_driver_s *dev);
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_addmac(struct net_driver_s *dev, FAR const uint8_t *mac);
|
|
|
|
static int enc_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac);
|
2010-07-11 17:17:11 +02:00
|
|
|
#endif
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-28 04:29:13 +02:00
|
|
|
/* Initialization */
|
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
static void enc_pwrsave(FAR struct enc_driver_s *priv);
|
|
|
|
static void enc_pwrfull(FAR struct enc_driver_s *priv);
|
2010-04-28 04:29:13 +02:00
|
|
|
static void enc_setmacaddr(FAR struct enc_driver_s *priv);
|
2010-04-30 03:43:46 +02:00
|
|
|
static int enc_reset(FAR struct enc_driver_s *priv);
|
2010-04-28 04:29:13 +02:00
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_configspi
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the SPI for use with the ENC28J60
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-25 23:24:12 +02:00
|
|
|
****************************************************************************/
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static inline void enc_configspi(FAR struct spi_dev_s *spi)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2016-01-24 01:54:36 +01:00
|
|
|
/* Configure SPI for the ENC28J60. */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
SPI_SETMODE(spi, CONFIG_ENC28J60_SPIMODE);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_ENC28J60_FREQUENCY);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_lock
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select the SPI, locking and re-configuring if necessary
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-25 23:24:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
static void enc_lock(FAR struct enc_driver_s *priv)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2020-05-04 19:34:04 +02:00
|
|
|
/* Lock the SPI bus in case there are multiple devices competing for the
|
|
|
|
* SPI bus.
|
2010-04-25 23:24:12 +02:00
|
|
|
*/
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
SPI_LOCK(priv->spi, true);
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Now make sure that the SPI bus is configured for the ENC28J60 (it
|
|
|
|
* might have gotten configured for a different device while unlocked)
|
|
|
|
*/
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
SPI_SETMODE(priv->spi, CONFIG_ENC28J60_SPIMODE);
|
|
|
|
SPI_SETBITS(priv->spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(priv->spi, 0);
|
|
|
|
SPI_SETFREQUENCY(priv->spi, CONFIG_ENC28J60_FREQUENCY);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_unlock
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* De-select the SPI
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* spi - Reference to the SPI driver structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-25 23:24:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
static inline void enc_unlock(FAR struct enc_driver_s *priv)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Relinquish the lock on the bus. */
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
SPI_LOCK(priv->spi, false);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 01:05:35 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rdgreg2
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
|
2013-08-27 17:40:19 +02:00
|
|
|
* include the CMD 'OR'd with the global address register.
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* cmd - The full command to received (cmd | address)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The value read from the register
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static uint8_t enc_rdgreg2(FAR struct enc_driver_s *priv, uint8_t cmd)
|
2010-04-26 01:05:35 +02:00
|
|
|
{
|
|
|
|
uint8_t rddata;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
2010-04-26 01:26:16 +02:00
|
|
|
/* Select ENC28J60 chip */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
/* Send the read command and collect the data. The sequence requires
|
|
|
|
* 16-clocks: 8 to clock out the cmd + 8 to clock in the data.
|
|
|
|
*/
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
SPI_SEND(priv->spi, cmd); /* Clock out the command */
|
2012-09-12 02:12:18 +02:00
|
|
|
rddata = SPI_SEND(priv->spi, 0); /* Clock in the data */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
|
|
|
/* De-select ENC28J60 chip */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
|
|
|
|
enc_rddump(cmd, rddata);
|
2010-04-26 01:05:35 +02:00
|
|
|
return rddata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_wrgreg2
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to a global register (EIE, EIR, ESTAT, ECON2, or ECON1). The cmd
|
2013-08-27 17:40:19 +02:00
|
|
|
* include the CMD 'OR'd with the global address register.
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* cmd - The full command to received (cmd | address)
|
|
|
|
* wrdata - The data to send
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
static void enc_wrgreg2(FAR struct enc_driver_s *priv, uint8_t cmd,
|
2010-04-27 05:03:40 +02:00
|
|
|
uint8_t wrdata)
|
2010-04-26 01:05:35 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
2010-04-26 01:26:16 +02:00
|
|
|
/* Select ENC28J60 chip */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
/* Send the write command and data. The sequence requires 16-clocks:
|
|
|
|
* 8 to clock out the cmd + 8 to clock out the data.
|
|
|
|
*/
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, cmd); /* Clock out the command */
|
|
|
|
SPI_SEND(priv->spi, wrdata); /* Clock out the data */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
|
|
|
/* De-select ENC28J60 chip. */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
enc_wrdump(cmd, wrdata);
|
2012-09-12 02:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_src
|
2012-09-12 02:12:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the single byte system reset command (SRC).
|
|
|
|
*
|
|
|
|
* "The System Reset Command (SRC) allows the host controller to issue a
|
|
|
|
* System Soft Reset command. Unlike other SPI commands, the SRC is
|
|
|
|
* only a single byte command and does not operate on any register. The
|
|
|
|
* command is started by pulling the CS pin low. The SRC opcode is the
|
|
|
|
* sent, followed by a 5-bit Soft Reset command constant of 1Fh. The
|
|
|
|
* SRC operation is terminated by raising the CS pin."
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2012-09-12 02:12:18 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void enc_src(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
|
|
|
/* Select ENC28J60 chip */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2012-09-12 02:12:18 +02:00
|
|
|
|
|
|
|
/* Send the system reset command. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, ENC_SRC);
|
2012-09-12 02:12:18 +02:00
|
|
|
|
|
|
|
/* Check CLKRDY bit to see when the reset is complete. There is an errata
|
|
|
|
* that says the CLKRDY may be invalid. We'll wait a couple of msec to
|
|
|
|
* workaround this condition.
|
|
|
|
*
|
|
|
|
* Also, "After a System Reset, all PHY registers should not be read or
|
2020-12-01 22:28:22 +01:00
|
|
|
* written to until at least 50 us have passed since the Reset has ended.
|
2012-09-12 02:12:18 +02:00
|
|
|
* All registers will revert to their Reset default values. The dual
|
|
|
|
* port buffer memory will maintain state throughout the System Reset."
|
|
|
|
*/
|
|
|
|
|
|
|
|
up_mdelay(2);
|
2019-02-24 18:51:25 +01:00
|
|
|
#if 0
|
|
|
|
while ((enc_rdgreg(priv, ENC_ESTAT) & ESTAT_CLKRDY) != 0);
|
|
|
|
#endif
|
2012-09-12 02:12:18 +02:00
|
|
|
|
|
|
|
/* De-select ENC28J60 chip. */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
enc_cmddump(ENC_SRC);
|
2010-04-26 01:26:16 +02:00
|
|
|
}
|
|
|
|
|
2010-04-26 01:05:35 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_setbank
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the bank for these next control register access.
|
|
|
|
*
|
|
|
|
* Assumption:
|
|
|
|
* The caller has exclusive access to the SPI bus
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* bank - The bank to select (0-3)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_setbank(FAR struct enc_driver_s *priv, uint8_t bank)
|
2010-04-26 01:05:35 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
/* Check if the bank setting has changed */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
|
|
|
if (bank != priv->bank)
|
|
|
|
{
|
|
|
|
/* Select bank 0 (just so that all of the bits are cleared) */
|
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfcgreg(priv, ENC_ECON1, ECON1_BSEL_MASK);
|
2010-04-26 01:05:35 +02:00
|
|
|
|
|
|
|
/* Then OR in bits to get the correct bank */
|
|
|
|
|
|
|
|
if (bank != 0)
|
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfsgreg(priv, ENC_ECON1, (bank << ECON1_BSEL_SHIFT));
|
2010-04-26 01:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then remember the bank setting */
|
|
|
|
|
|
|
|
priv->bank = bank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rdbreg
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-28 03:25:38 +02:00
|
|
|
* Read from a banked control register using the RCR command.
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* ctrlreg - Bit encoded address of banked register to read
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The byte read from the banked register
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static uint8_t enc_rdbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg)
|
2010-04-26 01:05:35 +02:00
|
|
|
{
|
|
|
|
uint8_t rddata;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
/* Set the bank */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
enc_setbank(priv, GETBANK(ctrlreg));
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2012-09-19 01:06:22 +02:00
|
|
|
/* Re-select ENC28J60 chip */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Send the RCR command and collect the data. How we collect the data
|
|
|
|
* depends on if this is a PHY/CAN or not. The normal sequence requires
|
|
|
|
* 16-clocks: 8 to clock out the cmd and 8 to clock in the data.
|
2010-04-28 03:25:38 +02:00
|
|
|
*/
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, ENC_RCR | GETADDR(ctrlreg)); /* Clock out the command */
|
2010-04-29 03:55:16 +02:00
|
|
|
if (ISPHYMAC(ctrlreg))
|
|
|
|
{
|
|
|
|
/* The PHY/MAC sequence requires 24-clocks: 8 to clock out the cmd,
|
|
|
|
* 8 dummy bits, and 8 to clock in the PHY/MAC data.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, 0); /* Clock in the dummy byte */
|
2010-04-29 03:55:16 +02:00
|
|
|
}
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
rddata = SPI_SEND(priv->spi, 0); /* Clock in the data */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
|
|
|
/* De-select ENC28J60 chip */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
enc_rddump(ENC_RCR | GETADDR(ctrlreg), rddata);
|
2010-04-26 01:05:35 +02:00
|
|
|
return rddata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_wrbreg
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-29 03:55:16 +02:00
|
|
|
* Write to a banked control register using the WCR command. Unlike
|
|
|
|
* reading, this same SPI sequence works for normal, MAC, and PHY
|
|
|
|
* registers.
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* ctrlreg - Bit encoded address of banked register to write
|
|
|
|
* wrdata - The data to send
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
static void enc_wrbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
|
|
|
|
uint8_t wrdata)
|
2010-04-26 01:05:35 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
|
|
|
/* Set the bank */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
enc_setbank(priv, GETBANK(ctrlreg));
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2012-09-19 01:06:22 +02:00
|
|
|
/* Re-select ENC28J60 chip */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2012-09-19 01:06:22 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Send the WCR command and data. The sequence requires 16-clocks:
|
|
|
|
* 8 to clock out the cmd + 8 to clock out the data.
|
2010-04-28 03:25:38 +02:00
|
|
|
*/
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, ENC_WCR | GETADDR(ctrlreg)); /* Clock out the command */
|
|
|
|
SPI_SEND(priv->spi, wrdata); /* Clock out the data */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* De-select ENC28J60 chip. */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
enc_wrdump(ENC_WCR | GETADDR(ctrlreg), wrdata);
|
2010-04-26 01:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_waitbreg
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-29 03:55:16 +02:00
|
|
|
* Wait until banked register bit(s) take a specific value (or a timeout
|
|
|
|
* occurs).
|
2010-04-26 01:05:35 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* ctrlreg - Bit encoded address of banked register to check
|
|
|
|
* bits - The bits to check (a mask)
|
|
|
|
* value - The value of the bits to return (value under mask)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success, negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-26 01:05:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
static int enc_waitbreg(FAR struct enc_driver_s *priv, uint8_t ctrlreg,
|
|
|
|
uint8_t bits, uint8_t value)
|
|
|
|
{
|
2020-05-04 16:15:10 +02:00
|
|
|
clock_t start = clock_systime_ticks();
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t elapsed;
|
|
|
|
uint8_t rddata;
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Loop until the exit condition is met */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Read the byte from the requested banked register */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
rddata = enc_rdbreg(priv, ctrlreg);
|
2020-05-04 16:15:10 +02:00
|
|
|
elapsed = clock_systime_ticks() - start;
|
2010-04-29 03:55:16 +02:00
|
|
|
}
|
2013-09-06 21:17:12 +02:00
|
|
|
while ((rddata & bits) != value && elapsed < ENC_POLLTIMEOUT);
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2013-08-21 16:10:32 +02:00
|
|
|
return (rddata & bits) == value ? OK : -ETIMEDOUT;
|
2010-04-26 01:05:35 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 01:06:22 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txdump enc_rxdump
|
2012-09-19 01:06:22 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump registers associated with receiving or sending packets.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2012-09-19 01:06:22 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if 0 /* Sometimes useful */
|
|
|
|
static void enc_rxdump(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "Rx Registers:\n");
|
|
|
|
syslog(LOG_DEBUG, " EIE: %02x EIR: %02x\n",
|
|
|
|
enc_rdgreg(priv, ENC_EIE), enc_rdgreg(priv, ENC_EIR));
|
|
|
|
syslog(LOG_DEBUG, " ESTAT: %02x ECON1: %02x ECON2: %02x\n",
|
|
|
|
enc_rdgreg(priv, ENC_ESTAT), enc_rdgreg(priv, ENC_ECON1),
|
|
|
|
enc_rdgreg(priv, ENC_ECON2));
|
|
|
|
syslog(LOG_DEBUG, " ERXST: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ERXSTH), enc_rdbreg(priv, ENC_ERXSTL));
|
|
|
|
syslog(LOG_DEBUG, " ERXND: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ERXNDH), enc_rdbreg(priv, ENC_ERXNDL));
|
|
|
|
syslog(LOG_DEBUG, " ERXRDPT: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ERXRDPTH), enc_rdbreg(priv, ENC_ERXRDPTL));
|
|
|
|
syslog(LOG_DEBUG, " ERXFCON: %02x EPKTCNT: %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ERXFCON), enc_rdbreg(priv, ENC_EPKTCNT));
|
|
|
|
syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3));
|
|
|
|
syslog(LOG_DEBUG, " MAMXFL: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MAMXFLH), enc_rdbreg(priv, ENC_MAMXFLL));
|
|
|
|
syslog(LOG_DEBUG, " MAADR: %02x:%02x:%02x:%02x:%02x:%02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MAADR1), enc_rdbreg(priv, ENC_MAADR2),
|
|
|
|
enc_rdbreg(priv, ENC_MAADR3), enc_rdbreg(priv, ENC_MAADR4),
|
|
|
|
enc_rdbreg(priv, ENC_MAADR5), enc_rdbreg(priv, ENC_MAADR6));
|
2012-09-19 01:06:22 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0 /* Sometimes useful */
|
|
|
|
static void enc_txdump(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "Tx Registers:\n");
|
|
|
|
syslog(LOG_DEBUG, " EIE: %02x EIR: %02x\n",
|
|
|
|
enc_rdgreg(priv, ENC_EIE), enc_rdgreg(priv, ENC_EIR));
|
|
|
|
syslog(LOG_DEBUG, " ESTAT: %02x ECON1: %02x\n",
|
|
|
|
enc_rdgreg(priv, ENC_ESTAT), enc_rdgreg(priv, ENC_ECON1));
|
|
|
|
syslog(LOG_DEBUG, " ETXST: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ETXSTH), enc_rdbreg(priv, ENC_ETXSTL));
|
|
|
|
syslog(LOG_DEBUG, " ETXND: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_ETXNDH), enc_rdbreg(priv, ENC_ETXNDL));
|
|
|
|
syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x MACON4: %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3),
|
|
|
|
enc_rdbreg(priv, ENC_MACON4));
|
|
|
|
syslog(LOG_DEBUG, " MACON1: %02x MACON3: %02x MACON4: %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MACON1), enc_rdbreg(priv, ENC_MACON3),
|
|
|
|
enc_rdbreg(priv, ENC_MACON4));
|
|
|
|
syslog(LOG_DEBUG, " MABBIPG: %02x MAIPG %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MABBIPG), enc_rdbreg(priv, ENC_MAIPGH),
|
|
|
|
enc_rdbreg(priv, ENC_MAIPGL));
|
|
|
|
syslog(LOG_DEBUG, " MACLCON1: %02x MACLCON2: %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MACLCON1), enc_rdbreg(priv, ENC_MACLCON2));
|
|
|
|
syslog(LOG_DEBUG, " MAMXFL: %02x %02x\n",
|
|
|
|
enc_rdbreg(priv, ENC_MAMXFLH), enc_rdbreg(priv, ENC_MAMXFLL));
|
2012-09-19 01:06:22 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rdbuffer
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-27 05:03:40 +02:00
|
|
|
* Read a buffer of data.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* buffer - A pointer to the buffer to read into
|
|
|
|
* buflen - The number of bytes to read
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Read pointer is set to the correct address
|
|
|
|
*
|
2010-04-27 05:03:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_rdbuffer(FAR struct enc_driver_s *priv, FAR uint8_t *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
|
|
|
/* Select ENC28J60 chip */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
|
|
|
/* Send the read buffer memory command (ignoring the response) */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, ENC_RBM);
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Then read the buffer data */
|
|
|
|
|
2012-09-12 02:12:18 +02:00
|
|
|
SPI_RECVBLOCK(priv->spi, buffer, buflen);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
|
|
|
/* De-select ENC28J60 chip. */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2012-09-18 01:02:58 +02:00
|
|
|
enc_bmdump(ENC_WBM, buffer, buflen);
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_wrbuffer
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write a buffer of data.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* buffer - A pointer to the buffer to write from
|
|
|
|
* buflen - The number of bytes to write
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Read pointer is set to the correct address
|
|
|
|
*
|
2010-04-27 05:03:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
static inline void enc_wrbuffer(FAR struct enc_driver_s *priv,
|
|
|
|
FAR const uint8_t *buffer, size_t buflen)
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
|
|
|
DEBUGASSERT(priv && priv->spi);
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Select ENC28J60 chip
|
|
|
|
*
|
|
|
|
* "The WBM command is started by lowering the CS pin. ..."
|
|
|
|
*/
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), true);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Send the write buffer memory command (ignoring the response)
|
|
|
|
*
|
|
|
|
* "...The [3-bit]WBM opcode should then be sent to the ENC28J60,
|
|
|
|
* followed by the 5-bit constant, 1Ah."
|
|
|
|
*/
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi, ENC_WBM);
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* "...the ENC28J60 requires a single per packet control byte to
|
|
|
|
* precede the packet for transmission."
|
|
|
|
*
|
|
|
|
* POVERRIDE: Per Packet Override bit (Not set):
|
|
|
|
* 1 = The values of PCRCEN, PPADEN and PHUGEEN will override the
|
|
|
|
* configuration defined by MACON3.
|
|
|
|
* 0 = The values in MACON3 will be used to determine how the packet
|
|
|
|
* will be transmitted
|
|
|
|
* PCRCEN: Per Packet CRC Enable bit (Set, but won't be used because
|
|
|
|
* POVERRIDE is zero).
|
|
|
|
* PPADEN: Per Packet Padding Enable bit (Set, but won't be used because
|
|
|
|
* POVERRIDE is zero).
|
|
|
|
* PHUGEEN: Per Packet Huge Frame Enable bit (Set, but won't be used
|
|
|
|
* because POVERRIDE is zero).
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SEND(priv->spi,
|
|
|
|
(PKTCTRL_PCRCEN | PKTCTRL_PPADEN | PKTCTRL_PHUGEEN));
|
2012-09-18 01:02:58 +02:00
|
|
|
|
|
|
|
/* Then send the buffer
|
|
|
|
*
|
|
|
|
* "... After the WBM command and constant are sent, the data to
|
|
|
|
* be stored in the memory pointed to by EWRPT should be shifted
|
|
|
|
* out MSb first to the ENC28J60. After 8 data bits are received,
|
|
|
|
* the Write Pointer will automatically increment if AUTOINC is
|
|
|
|
* set. The host controller can continue to provide clocks on the
|
|
|
|
* SCK pin and send data on the SI pin, without raising CS, to
|
|
|
|
* keep writing to the memory. In this manner, with AUTOINC
|
|
|
|
* enabled, it is possible to continuously write sequential bytes
|
|
|
|
* to the buffer memory without any extra SPI command
|
|
|
|
* overhead.
|
|
|
|
*/
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2012-09-12 02:12:18 +02:00
|
|
|
SPI_SNDBLOCK(priv->spi, buffer, buflen);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* De-select ENC28J60 chip
|
|
|
|
*
|
|
|
|
* "The WBM command is terminated by bringing up the CS pin. ..."
|
|
|
|
*/
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(priv->spi, SPIDEV_ETHERNET(0), false);
|
2019-12-05 21:54:50 +01:00
|
|
|
enc_bmdump(ENC_WBM, buffer, buflen + 1);
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rdphy
|
2010-04-28 03:25:38 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read 16-bits of PHY data.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* phyaddr - The PHY register address
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 16-bit value read from the PHY
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-28 03:25:38 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint16_t enc_rdphy(FAR struct enc_driver_s *priv, uint8_t phyaddr)
|
|
|
|
{
|
2010-04-29 03:55:16 +02:00
|
|
|
uint16_t data = 0;
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
/* "To read from a PHY register:
|
|
|
|
*
|
2020-05-04 19:34:04 +02:00
|
|
|
* 1. Write the address of the PHY register to read from into MIREGADR
|
2012-09-18 20:32:31 +02:00
|
|
|
* register.
|
|
|
|
*/
|
2010-04-28 03:25:38 +02:00
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MIREGADR, phyaddr);
|
2012-09-18 20:32:31 +02:00
|
|
|
|
|
|
|
/* 2. Set the MICMD.MIIRD bit. The read operation begins and the
|
|
|
|
* MISTAT.BUSY bit is set.
|
|
|
|
*/
|
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
enc_wrbreg(priv, ENC_MICMD, MICMD_MIIRD);
|
|
|
|
|
2020-12-01 22:28:22 +01:00
|
|
|
/* 3. Wait 10.24 us. Poll the MISTAT.BUSY bit to be certain that the
|
2012-09-18 20:32:31 +02:00
|
|
|
* operation is complete. While busy, the host controller should not
|
|
|
|
* start any MIISCAN operations or write to the MIWRH register.
|
|
|
|
*
|
|
|
|
* When the MAC has obtained the register contents, the BUSY bit will
|
|
|
|
* clear itself.
|
|
|
|
*/
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
up_udelay(12);
|
2013-08-21 16:10:32 +02:00
|
|
|
if (enc_waitbreg(priv, ENC_MISTAT, MISTAT_BUSY, 0x00) == OK)
|
2010-04-29 03:55:16 +02:00
|
|
|
{
|
2012-09-18 20:32:31 +02:00
|
|
|
/* 4. Clear the MICMD.MIIRD bit. */
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
enc_wrbreg(priv, ENC_MICMD, 0x00);
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
/* 5. Read the desired data from the MIRDL and MIRDH registers. The
|
|
|
|
* order that these bytes are accessed is unimportant."
|
|
|
|
*/
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
data = (uint16_t)enc_rdbreg(priv, ENC_MIRDL);
|
|
|
|
data |= (uint16_t)enc_rdbreg(priv, ENC_MIRDH) << 8;
|
|
|
|
}
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_wrphy
|
2010-04-28 03:25:38 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* write 16-bits of PHY data.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
* phyaddr - The PHY register address
|
|
|
|
* phydata - 16-bit data to write to the PHY
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-28 03:25:38 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_wrphy(FAR struct enc_driver_s *priv, uint8_t phyaddr,
|
|
|
|
uint16_t phydata)
|
|
|
|
{
|
2012-09-18 20:32:31 +02:00
|
|
|
/* "To write to a PHY register:
|
|
|
|
*
|
|
|
|
* 1. Write the address of the PHY register to write to into the
|
|
|
|
* MIREGADR register.
|
|
|
|
*/
|
2010-04-28 03:25:38 +02:00
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MIREGADR, phyaddr);
|
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
/* 2. Write the lower 8 bits of data to write into the MIWRL register. */
|
2010-04-28 03:25:38 +02:00
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MIWRL, phydata);
|
2012-09-18 20:32:31 +02:00
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* 3. Write the upper 8 bits of data to write into MIWRH register.
|
|
|
|
* Writing to this register automatically begins MIIM transaction,
|
2012-09-18 20:32:31 +02:00
|
|
|
* so it must be written to after MIWRL. The MISTAT.BUSY bit becomes
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
enc_wrbreg(priv, ENC_MIWRH, phydata >> 8);
|
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
/* The PHY register will be written after the MIIM operation completes,
|
2020-12-01 22:28:22 +01:00
|
|
|
* which takes 10.24 us. When the write operation has completed, BUSY
|
2012-09-18 20:32:31 +02:00
|
|
|
* bit will clear itself.
|
|
|
|
*
|
|
|
|
* The host controller should not start any MIISCAN or MIIRD operations
|
|
|
|
* while busy."
|
|
|
|
*/
|
2010-04-28 03:25:38 +02:00
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
up_udelay(12);
|
2010-04-29 03:55:16 +02:00
|
|
|
enc_waitbreg(priv, ENC_MISTAT, MISTAT_BUSY, 0x00);
|
2010-04-28 03:25:38 +02:00
|
|
|
}
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_transmit
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-05-01 04:14:21 +02:00
|
|
|
* Start hardware transmission. Called either from:
|
|
|
|
*
|
|
|
|
* - pkif interrupt when an application responds to the receipt of data
|
|
|
|
* by trying to send something, or
|
|
|
|
* - From watchdog based polling.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static int enc_transmit(FAR struct enc_driver_s *priv)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-05-01 04:14:21 +02:00
|
|
|
uint16_t txend;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/* Increment statistics */
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Sending packet, pktlen: %d\n", priv->dev.d_len);
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXPACKETS(&priv->dev);
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
/* Verify that the hardware is ready to send another packet. The driver
|
2010-05-04 03:28:47 +02:00
|
|
|
* starts a transmission process by setting ECON1.TXRTS. When the packet is
|
2010-05-01 04:14:21 +02:00
|
|
|
* finished transmitting or is aborted due to an error/cancellation, the
|
|
|
|
* ECON1.TXRTS bit will be cleared.
|
|
|
|
*
|
|
|
|
* NOTE: If we got here, then we have committed to sending a packet.
|
|
|
|
* higher level logic must have assured that (1) there is no transmission
|
|
|
|
* in progress, and that (2) TX-related interrupts are disabled.
|
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
DEBUGASSERT((enc_rdgreg(priv, ENC_ECON1) & ECON1_TXRTS) == 0);
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Send the packet: address=priv->dev.d_buf, length=priv->dev.d_len */
|
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
enc_dumppacket("Transmit Packet", priv->dev.d_buf, priv->dev.d_len);
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Set transmit buffer start (is this necessary?). */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_ETXSTL, PKTMEM_TX_START & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ETXSTH, PKTMEM_TX_START >> 8);
|
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Reset the write pointer to start of transmit buffer */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_EWRPTL, PKTMEM_TX_START & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_EWRPTH, PKTMEM_TX_START >> 8);
|
|
|
|
|
2012-09-18 20:32:31 +02:00
|
|
|
/* Set the TX End pointer based on the size of the packet to send. Note
|
|
|
|
* that the offset accounts for the control byte at the beginning the
|
|
|
|
* buffer plus the size of the packet data.
|
|
|
|
*/
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
txend = PKTMEM_TX_START + priv->dev.d_len;
|
|
|
|
enc_wrbreg(priv, ENC_ETXNDL, txend & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ETXNDH, txend >> 8);
|
|
|
|
|
2012-09-18 01:02:58 +02:00
|
|
|
/* Send the WBM command and copy the packet itself into the transmit
|
|
|
|
* buffer at the position of the EWRPT register.
|
|
|
|
*/
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
enc_wrbuffer(priv, priv->dev.d_buf, priv->dev.d_len);
|
|
|
|
|
|
|
|
/* Set TXRTS to send the packet in the transmit buffer */
|
|
|
|
|
|
|
|
enc_bfsgreg(priv, ENC_ECON1, ECON1_TXRTS);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Setup the TX timeout watchdog (perhaps restarting the timer). Note:
|
|
|
|
* Is there a race condition. Could the TXIF interrupt occur before
|
|
|
|
* the timer is started?
|
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->txtimeout, ENC_TXTIMEOUT,
|
2020-08-09 20:29:35 +02:00
|
|
|
enc_txtimeout, (wdparm_t)priv);
|
2010-04-25 23:24:12 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txpoll
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 18:51:25 +01:00
|
|
|
* The transmitter is available, check if the network has any outgoing
|
|
|
|
* packets ready to send. This is a callback from devif_poll().
|
|
|
|
* devif_poll() may be called:
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* 1. When the preceding TX packet send is complete,
|
2019-02-24 18:51:25 +01:00
|
|
|
* 2. When the preceding TX packet send timesout and the interface is
|
|
|
|
* reset
|
2010-04-25 23:24:12 +02:00
|
|
|
* 3. During normal TX polling
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2016-05-30 17:37:34 +02:00
|
|
|
* Interrupts are enabled but the caller holds the network lock.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-02 02:41:08 +02:00
|
|
|
static int enc_txpoll(struct net_driver_s *dev)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
/* Send the packet */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
enc_transmit(priv);
|
2010-05-01 04:14:21 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
/* Stop the poll now because we can queue only one packet */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-11-26 20:31:07 +01:00
|
|
|
return -EBUSY;
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_linkstatus
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-27 05:03:40 +02:00
|
|
|
* The current link status can be obtained from the PHSTAT1.LLSTAT or
|
|
|
|
* PHSTAT2.LSTAT.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-27 05:03:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_linkstatus(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2010-05-01 04:14:21 +02:00
|
|
|
#if 0
|
2010-05-01 14:22:44 +02:00
|
|
|
uint16_t regval = enc_rdphy(priv, ENC_PHSTAT2);
|
2010-05-01 04:14:21 +02:00
|
|
|
priv->duplex = ((regval & PHSTAT2_DPXSTAT) != 0);
|
|
|
|
priv->carrier = ((regval & PHSTAT2_LSTAT) != 0);
|
|
|
|
#endif
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txif
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An TXIF interrupt was received indicating that the last TX packet(s) is
|
|
|
|
* done
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2016-05-30 17:37:34 +02:00
|
|
|
* Interrupts are enabled but the caller holds the network lock.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
static void enc_txif(FAR struct enc_driver_s *priv)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Update statistics */
|
|
|
|
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXDONE(&priv->dev);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Clear the request to send bit */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfcgreg(priv, ENC_ECON1, ECON1_TXRTS);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* If no further xmits are pending, then cancel the TX timeout */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->txtimeout);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Then poll the network for new XMIT data */
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_poll(&priv->dev, enc_txpoll);
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txerif
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-04 19:34:04 +02:00
|
|
|
* An TXERIF interrupt was received indicating that TX abort has occurred.
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-27 05:03:40 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_txerif(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
|
|
|
/* Update statistics */
|
|
|
|
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXERRORS(&priv->dev);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Reset TX */
|
2010-04-26 01:05:35 +02:00
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfsgreg(priv, ENC_ECON1, ECON1_TXRST);
|
|
|
|
enc_bfcgreg(priv, ENC_ECON1, ECON1_TXRST | ECON1_TXRTS);
|
|
|
|
|
2010-04-28 03:25:38 +02:00
|
|
|
/* Here we really should re-transmit (I fact, if we want half duplex to
|
|
|
|
* work right, then it is necessary to do this!):
|
2010-04-27 05:52:56 +02:00
|
|
|
*
|
|
|
|
* 1. Read the TSV:
|
|
|
|
* - Read ETXNDL to get the end pointer
|
|
|
|
* - Read 7 bytes from that pointer + 1 using ENC_RMB.
|
2013-09-06 21:17:12 +02:00
|
|
|
* 2. Determine if we need to retransmit. Check the LATE COLLISION bit, if
|
2010-04-27 05:52:56 +02:00
|
|
|
* set, then we need to transmit.
|
|
|
|
* 3. Retranmit by resetting ECON1_TXRTS.
|
|
|
|
*/
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_ENC28J60_HALFDUPLEX
|
|
|
|
# error "Missing logic for half duplex"
|
|
|
|
#endif
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rxerif
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-04 19:34:04 +02:00
|
|
|
* An RXERIF interrupt was received indicating that the last TX packet(s)
|
|
|
|
* is done
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-27 05:03:40 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_rxerif(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2015-11-26 19:08:09 +01:00
|
|
|
/* REVISIT: Update statistics */
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rxdispatch
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-05-30 17:37:34 +02:00
|
|
|
* Give the newly received packet to the network.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2016-05-30 17:37:34 +02:00
|
|
|
* Interrupts are enabled but the caller holds the network lock.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_rxdispatch(FAR struct enc_driver_s *priv)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2015-01-20 22:14:29 +01:00
|
|
|
#ifdef CONFIG_NET_PKT
|
2020-08-06 19:41:45 +02:00
|
|
|
/* When packet sockets are enabled, feed the frame into the tap */
|
2015-01-20 22:14:29 +01:00
|
|
|
|
2019-12-05 21:54:50 +01:00
|
|
|
pkt_input(&priv->dev);
|
2015-01-20 22:14:29 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-15 17:25:53 +01:00
|
|
|
/* We only accept IP packets of the configured type and ARP packets */
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2015-01-15 17:25:53 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2014-07-04 23:40:49 +02:00
|
|
|
if (BUF->type == HTONS(ETHTYPE_IP))
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("IPv4 frame\n");
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXIPV4(&priv->dev);
|
2015-01-15 17:25:53 +01:00
|
|
|
|
2022-11-26 19:13:21 +01:00
|
|
|
/* Receive an IPv4 packet from the network device */
|
2015-01-15 17:25:53 +01:00
|
|
|
|
2015-01-15 15:03:56 +01:00
|
|
|
ipv4_input(&priv->dev);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
|
|
|
/* If the above function invocation resulted in data that should be
|
2020-08-06 19:41:45 +02:00
|
|
|
* sent out on the network, d_len field will set to a value > 0.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
2015-01-15 17:25:53 +01:00
|
|
|
/* And send the packet */
|
|
|
|
|
|
|
|
enc_transmit(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (BUF->type == HTONS(ETHTYPE_IP6))
|
|
|
|
{
|
2020-03-03 16:11:57 +01:00
|
|
|
ninfo("IPv6 frame\n");
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXIPV6(&priv->dev);
|
2015-01-15 17:25:53 +01:00
|
|
|
|
|
|
|
/* Give the IPv6 packet to the network layer */
|
|
|
|
|
|
|
|
ipv6_input(&priv->dev);
|
|
|
|
|
|
|
|
/* If the above function invocation resulted in data that should be
|
2020-08-06 19:41:45 +02:00
|
|
|
* sent out on the network, d_len field will set to a value > 0.
|
2015-01-15 17:25:53 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
/* And send the packet */
|
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
enc_transmit(priv);
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 17:25:53 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ARP
|
2022-01-18 08:38:00 +01:00
|
|
|
if (BUF->type == HTONS(ETHTYPE_ARP))
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("ARP packet received (%02x)\n", BUF->type);
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXARP(&priv->dev);
|
|
|
|
|
2022-12-04 08:01:53 +01:00
|
|
|
arp_input(&priv->dev);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* If the above function invocation resulted in data that should be
|
2020-08-06 19:41:45 +02:00
|
|
|
* sent out on the network, d_len field will set to a value > 0.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
|
|
|
|
2019-12-05 21:54:50 +01:00
|
|
|
if (priv->dev.d_len > 0)
|
|
|
|
{
|
|
|
|
enc_transmit(priv);
|
|
|
|
}
|
|
|
|
}
|
2010-05-01 04:14:21 +02:00
|
|
|
else
|
2015-01-15 17:25:53 +01:00
|
|
|
#endif
|
2010-05-01 04:14:21 +02:00
|
|
|
{
|
2018-09-25 15:07:04 +02:00
|
|
|
nwarn("WARNING: Unsupported packet type dropped (%02x)\n",
|
2022-01-18 08:38:00 +01:00
|
|
|
HTONS(BUF->type));
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXDROPPED(&priv->dev);
|
2010-05-01 04:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_pktif
|
2010-05-01 04:14:21 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* An interrupt was received indicating the availability of a new RX packet
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2016-05-30 17:37:34 +02:00
|
|
|
* Interrupts are enabled but the caller holds the network lock.
|
2010-05-01 04:14:21 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_pktif(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
|
|
|
uint8_t rsv[6];
|
|
|
|
uint16_t pktlen;
|
|
|
|
uint16_t rxstat;
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Set the read pointer to the start of the received packet (ERDPT) */
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv->nextpkt <= PKTMEM_RX_END);
|
|
|
|
enc_wrbreg(priv, ENC_ERDPTL, (priv->nextpkt));
|
|
|
|
enc_wrbreg(priv, ENC_ERDPTH, (priv->nextpkt) >> 8);
|
|
|
|
|
|
|
|
/* Read the next packet pointer and the 4 byte read status vector (RSV)
|
2012-09-19 16:22:18 +02:00
|
|
|
* at the beginning of the received packet. (ERDPT should auto-increment
|
|
|
|
* and wrap to the beginning of the read buffer as necessary)
|
2010-05-01 04:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
enc_rdbuffer(priv, rsv, 6);
|
|
|
|
|
|
|
|
/* Decode the new next packet pointer, and the RSV. The
|
|
|
|
* RSV is encoded as:
|
|
|
|
*
|
|
|
|
* Bits 0-15: Indicates length of the received frame. This includes the
|
|
|
|
* destination address, source address, type/length, data,
|
|
|
|
* padding and CRC fields. This field is stored in little-
|
|
|
|
* endian format.
|
|
|
|
* Bits 16-31: Bit encoded RX status.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->nextpkt = (uint16_t)rsv[1] << 8 | (uint16_t)rsv[0];
|
|
|
|
pktlen = (uint16_t)rsv[3] << 8 | (uint16_t)rsv[2];
|
|
|
|
rxstat = (uint16_t)rsv[5] << 8 | (uint16_t)rsv[4];
|
2012-09-19 01:06:22 +02:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Receiving packet, nextpkt: %04x pktlen: %d rxstat: %04x\n",
|
|
|
|
priv->nextpkt, pktlen, rxstat);
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
/* Check if the packet was received OK */
|
|
|
|
|
|
|
|
if ((rxstat & RXSTAT_OK) == 0)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: RXSTAT: %04x\n", rxstat);
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXERRORS(&priv->dev);
|
2010-05-01 04:14:21 +02:00
|
|
|
}
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Check for a usable packet length (4 added for the CRC) */
|
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
else if (pktlen > (CONFIG_NET_ETH_PKTSIZE + 4) ||
|
|
|
|
pktlen <= (ETH_HDRLEN + 4))
|
2010-05-01 04:14:21 +02:00
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Bad packet size dropped (%d)\n", pktlen);
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_RXERRORS(&priv->dev);
|
2010-05-01 04:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, read and process the packet */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
/* Save the packet length (without the 4 byte CRC) in priv->dev.d_len */
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
priv->dev.d_len = pktlen - 4;
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Copy the data data from the receive buffer to priv->dev.d_buf.
|
2019-09-29 20:52:20 +02:00
|
|
|
* ERDPT should be correctly positioned from the last call to
|
2012-09-19 16:22:18 +02:00
|
|
|
* end_rdbuffer (above).
|
|
|
|
*/
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
enc_rdbuffer(priv, priv->dev.d_buf, priv->dev.d_len);
|
2012-09-17 20:35:37 +02:00
|
|
|
enc_dumppacket("Received Packet", priv->dev.d_buf, priv->dev.d_len);
|
2010-05-01 04:14:21 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Dispatch the packet to the network */
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
enc_rxdispatch(priv);
|
2010-05-01 04:14:21 +02:00
|
|
|
}
|
|
|
|
|
2024-09-02 09:51:53 +02:00
|
|
|
/* Update statistics */
|
|
|
|
|
|
|
|
NETDEV_RXPACKETS(&priv->dev);
|
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Move the RX read pointer to the start of the next received packet.
|
|
|
|
* This frees the memory we just read.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_ERXRDPTL, (priv->nextpkt));
|
|
|
|
enc_wrbreg(priv, ENC_ERXRDPTH, (priv->nextpkt) >> 8);
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Decrement the packet counter indicate we are done with this packet */
|
2010-05-01 04:14:21 +02:00
|
|
|
|
|
|
|
enc_bfsgreg(priv, ENC_ECON2, ECON2_PKTDEC);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_irqworker
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-26 14:46:08 +02:00
|
|
|
* Perform interrupt handling logic outside of the interrupt handler (on
|
|
|
|
* the work queue thread).
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-26 14:46:08 +02:00
|
|
|
* arg - The reference to the driver structure (case to void*)
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2010-04-26 14:46:08 +02:00
|
|
|
* None
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_irqworker(FAR void *arg)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
|
|
|
|
uint8_t eir;
|
2010-04-26 14:46:08 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Get exclusive access to both the network and the SPI bus. */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2012-09-19 16:22:18 +02:00
|
|
|
enc_lock(priv);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* Disable further interrupts by clearing the global interrupt enable bit.
|
|
|
|
* "After an interrupt occurs, the host controller should clear the global
|
2020-05-04 19:34:04 +02:00
|
|
|
* enable bit for the interrupt pin before servicing the interrupt.
|
|
|
|
* Clearing the enable bit will cause the interrupt pin to return to the
|
|
|
|
* non-asserted state (high). Doing so will prevent the host controller
|
|
|
|
* from missing a falling edge should another interrupt occur while the
|
|
|
|
* immediate interrupt is being serviced."
|
2012-09-17 20:35:37 +02:00
|
|
|
*/
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfcgreg(priv, ENC_EIE, EIE_INTIE);
|
2010-04-27 05:03:40 +02:00
|
|
|
|
|
|
|
/* Loop until all interrupts have been processed (EIR==0). Note that
|
2019-02-24 18:51:25 +01:00
|
|
|
* there is no infinite loop check... if there are always pending
|
|
|
|
* interrupts, we are just broken.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
while ((eir = enc_rdgreg(priv, ENC_EIR) & EIR_ALLINTS) != 0)
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
|
|
|
/* Handle interrupts according to interrupt register register bit
|
2012-09-17 20:35:37 +02:00
|
|
|
* settings.
|
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("EIR: %02x\n", eir);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
|
|
|
/* DMAIF: The DMA interrupt indicates that the DMA module has completed
|
2020-05-04 19:34:04 +02:00
|
|
|
* its memory copy or checksum calculation. Additionally, this
|
|
|
|
* interrupt will be caused if the host controller cancels a DMA
|
|
|
|
* operation by manually clearing the DMAST bit. Once set, DMAIF can
|
|
|
|
* only be cleared by the host controller or by a Reset condition.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
if ((eir & EIR_DMAIF) != 0) /* DMA interrupt */
|
|
|
|
{
|
|
|
|
/* Not used by this driver. Just clear the interrupt request. */
|
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfcgreg(priv, ENC_EIR, EIR_DMAIF);
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* LINKIF: The LINKIF indicates that the link status has changed.
|
|
|
|
* The actual current link status can be obtained from the
|
|
|
|
* PHSTAT1.LLSTAT or PHSTAT2.LSTAT. Unlike other interrupt sources, the
|
|
|
|
* link status change interrupt is created in the integrated PHY
|
|
|
|
* module.
|
|
|
|
*
|
|
|
|
* To receive it, the host controller must set the PHIE.PLNKIE and
|
|
|
|
* PGEIE bits. After setting the two PHY interrupt enable bits, the
|
|
|
|
* LINKIF bit will then shadow the contents of the PHIR.PGIF bit.
|
|
|
|
*
|
|
|
|
* Once LINKIF is set, it can only be cleared by the host controller or
|
|
|
|
* by a Reset. The LINKIF bit is read-only. Performing an MII read on
|
|
|
|
* the PHIR register will clear the LINKIF, PGIF and PLNKIF bits
|
|
|
|
* automatically and allow for future link status change interrupts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((eir & EIR_LINKIF) != 0) /* Link change interrupt */
|
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_linkstatus(priv); /* Get current link status */
|
|
|
|
enc_rdphy(priv, ENC_PHIR); /* Clear the LINKIF interrupt */
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TXIF: The Transmit Interrupt Flag (TXIF) is used to indicate that
|
|
|
|
* the requested packet transmission has ended. Upon transmission
|
|
|
|
* completion, abort or transmission cancellation by the host
|
|
|
|
* controller, the EIR.TXIF flag will be set to 1.
|
|
|
|
*
|
|
|
|
* Once TXIF is set, it can only be cleared by the host controller
|
|
|
|
* or by a Reset condition. Once processed, the host controller should
|
|
|
|
* use the BFC command to clear the EIR.TXIF bit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((eir & EIR_TXIF) != 0) /* Transmit interrupt */
|
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_txif(priv); /* Handle TX completion */
|
|
|
|
enc_bfcgreg(priv, ENC_EIR, EIR_TXIF); /* Clear the TXIF interrupt */
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TXERIF: The Transmit Error Interrupt Flag (TXERIF) is used to
|
|
|
|
* indicate that a transmit abort has occurred. An abort can occur
|
|
|
|
* because of any of the following:
|
|
|
|
*
|
|
|
|
* 1. Excessive collisions occurred as defined by the Retransmission
|
|
|
|
* Maximum (RETMAX) bits in the MACLCON1 register.
|
|
|
|
* 2. A late collision occurred as defined by the Collision Window
|
|
|
|
* (COLWIN) bits in the MACLCON2 register.
|
|
|
|
* 3. A collision after transmitting 64 bytes occurred (ESTAT.LATECOL
|
|
|
|
* set).
|
|
|
|
* 4. The transmission was unable to gain an opportunity to transmit
|
2019-02-24 18:51:25 +01:00
|
|
|
* the packet because the medium was constantly occupied for too
|
|
|
|
* long. The deferral limit (2.4287 ms) was reached and the
|
|
|
|
* MACON4.DEFER bit was clear.
|
2010-04-27 05:03:40 +02:00
|
|
|
* 5. An attempt to transmit a packet larger than the maximum frame
|
|
|
|
* length defined by the MAMXFL registers was made without setting
|
|
|
|
* the MACON3.HFRMEN bit or per packet POVERRIDE and PHUGEEN bits.
|
|
|
|
*
|
|
|
|
* Upon any of these conditions, the EIR.TXERIF flag is set to 1. Once
|
|
|
|
* set, it can only be cleared by the host controller or by a Reset
|
|
|
|
* condition.
|
|
|
|
*
|
|
|
|
* After a transmit abort, the TXRTS bit will be cleared, the
|
|
|
|
* ESTAT.TXABRT bit will be set and the transmit status vector will be
|
|
|
|
* written at ETXND + 1. The MAC will not automatically attempt to
|
|
|
|
* retransmit the packet. The host controller may wish to read the
|
|
|
|
* transmit status vector and LATECOL bit to determine the cause of
|
|
|
|
* the abort. After determining the problem and solution, the host
|
|
|
|
* controller should clear the LATECOL (if set) and TXABRT bits so
|
|
|
|
* that future aborts can be detected accurately.
|
|
|
|
*
|
|
|
|
* In Full-Duplex mode, condition 5 is the only one that should cause
|
|
|
|
* this interrupt. Collisions and other problems related to sharing
|
|
|
|
* the network are not possible on full-duplex networks. The conditions
|
2020-05-04 19:34:04 +02:00
|
|
|
* which cause the transmit error interrupt meet the requirements of
|
|
|
|
* the transmit interrupt. As a result, when this interrupt occurs,
|
|
|
|
* TXIF will also be simultaneously set.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
if ((eir & EIR_TXERIF) != 0) /* Transmit Error Interrupts */
|
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_txerif(priv); /* Handle the TX error */
|
|
|
|
enc_bfcgreg(priv, ENC_EIR, EIR_TXERIF); /* Clear the TXERIF interrupt */
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* PKTIF The Receive Packet Pending Interrupt Flag (PKTIF) is used to
|
|
|
|
* indicate the presence of one or more data packets in the receive
|
|
|
|
* buffer and to provide a notification means for the arrival of new
|
|
|
|
* packets. When the receive buffer has at least one packet in it,
|
|
|
|
* EIR.PKTIF will be set. In other words, this interrupt flag will be
|
2020-05-04 19:34:04 +02:00
|
|
|
* set anytime the Ethernet Packet Count register (EPKTCNT) is
|
|
|
|
* non-zero.
|
2010-04-27 05:03:40 +02:00
|
|
|
*
|
2019-02-24 18:51:25 +01:00
|
|
|
* The PKTIF bit can only be cleared by the host controller or by a
|
|
|
|
* Reset condition. In order to clear PKTIF, the EPKTCNT register must
|
2020-05-04 19:34:04 +02:00
|
|
|
* be decremented to 0. If the last data packet in the receive buffer
|
|
|
|
* is processed, EPKTCNT will become zero and the PKTIF bit will
|
2019-02-24 18:51:25 +01:00
|
|
|
* automatically be cleared.
|
2010-04-27 05:03:40 +02:00
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
#if 0
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Ignore PKTIF because is unreliable. Use EPKTCNT instead */
|
2015-11-26 19:08:09 +01:00
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
if ((eir & EIR_PKTIF) != 0)
|
|
|
|
#endif
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
uint8_t pktcnt = enc_rdbreg(priv, ENC_EPKTCNT);
|
2010-04-27 05:03:40 +02:00
|
|
|
if (pktcnt > 0)
|
|
|
|
{
|
2018-09-25 15:07:04 +02:00
|
|
|
ninfo("EPKTCNT: %02x\n", pktcnt);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* Handle packet receipt */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
enc_pktif(priv);
|
|
|
|
}
|
|
|
|
}
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-27 05:03:40 +02:00
|
|
|
/* RXERIF: The Receive Error Interrupt Flag (RXERIF) is used to
|
|
|
|
* indicate a receive buffer overflow condition. Alternately, this
|
|
|
|
* interrupt may indicate that too many packets are in the receive
|
|
|
|
* buffer and more cannot be stored without overflowing the EPKTCNT
|
|
|
|
* register. When a packet is being received and the receive buffer
|
|
|
|
* runs completely out of space, or EPKTCNT is 255 and cannot be
|
|
|
|
* incremented, the packet being received will be aborted (permanently
|
|
|
|
* lost) and the EIR.RXERIF bit will be set to 1.
|
|
|
|
*
|
|
|
|
* Once set, RXERIF can only be cleared by the host controller or by a
|
|
|
|
* Reset condition. Normally, upon the receive error condition, the
|
|
|
|
* host controller would process any packets pending from the receive
|
|
|
|
* buffer and then make additional room for future packets by
|
|
|
|
* advancing the ERXRDPT registers (low byte first) and decrementing
|
|
|
|
* the EPKTCNT register.
|
|
|
|
*
|
|
|
|
* Once processed, the host controller should use the BFC command to
|
|
|
|
* clear the EIR.RXERIF bit.
|
|
|
|
*/
|
|
|
|
|
2018-09-25 15:07:04 +02:00
|
|
|
if ((eir & EIR_RXERIF) != 0) /* Receive Error Interrupts */
|
2010-04-27 05:03:40 +02:00
|
|
|
{
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_rxerif(priv); /* Handle the RX error */
|
|
|
|
enc_bfcgreg(priv, ENC_EIR, EIR_RXERIF); /* Clear the RXERIF interrupt */
|
2010-04-27 05:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2013-09-06 21:17:12 +02:00
|
|
|
/* Enable GPIO interrupts */
|
|
|
|
|
|
|
|
priv->lower->enable(priv->lower);
|
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Enable Ethernet interrupts */
|
2010-04-27 05:03:40 +02:00
|
|
|
|
2010-04-27 05:52:56 +02:00
|
|
|
enc_bfsgreg(priv, ENC_EIE, EIE_INTIE);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Release lock on the SPI bus and the network */
|
2012-09-19 16:22:18 +02:00
|
|
|
|
|
|
|
enc_unlock(priv);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2010-04-26 14:46:08 +02:00
|
|
|
}
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-26 14:46:08 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_interrupt
|
2010-04-26 14:46:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Hardware interrupt handler
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-26 14:46:08 +02:00
|
|
|
* irq - Number of the IRQ that generated the interrupt
|
|
|
|
* context - Interrupt register state save info (architecture-specific)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-28 02:28:24 +01:00
|
|
|
static int enc_interrupt(int irq, FAR void *context, FAR void *arg)
|
2010-04-26 14:46:08 +02:00
|
|
|
{
|
2017-08-22 00:49:43 +02:00
|
|
|
FAR struct enc_driver_s *priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(arg != NULL);
|
|
|
|
priv = (FAR struct enc_driver_s *)arg;
|
2010-04-26 14:46:08 +02:00
|
|
|
|
|
|
|
/* In complex environments, we cannot do SPI transfers from the interrupt
|
|
|
|
* handler because semaphores are probably used to lock the SPI bus. In
|
|
|
|
* this case, we will defer processing to the worker thread. This is also
|
|
|
|
* much kinder in the use of system resources and is, therefore, probably
|
|
|
|
* a good thing to do in any event.
|
|
|
|
*/
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
DEBUGASSERT(work_available(&priv->irqwork));
|
|
|
|
|
|
|
|
/* Notice that further GPIO interrupts are disabled until the work is
|
|
|
|
* actually performed. This is to prevent overrun of the worker thread.
|
|
|
|
* Interrupts are re-enabled in enc_irqworker() when the work is completed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->lower->disable(priv->lower);
|
2019-02-24 18:51:25 +01:00
|
|
|
return work_queue(ENCWORK, &priv->irqwork, enc_irqworker,
|
|
|
|
(FAR void *)priv, 0);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_toworker
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-09-17 20:35:37 +02:00
|
|
|
* Our TX watchdog timed out. This is the worker thread continuation of
|
|
|
|
* the watchdog timer interrupt. Reset the hardware and start again.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2012-09-17 20:35:37 +02:00
|
|
|
* arg - The reference to the driver structure (case to void*)
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
static void enc_toworker(FAR void *arg)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
|
2010-04-30 03:43:46 +02:00
|
|
|
int ret;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Tx timeout\n");
|
2012-09-17 20:35:37 +02:00
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Get exclusive access to the network */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Increment statistics and dump debug info */
|
|
|
|
|
2015-11-26 19:08:09 +01:00
|
|
|
NETDEV_TXTIMEOUTS(&priv->dev);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Then reset the hardware: Take the interface down, then bring it
|
2010-04-30 03:43:46 +02:00
|
|
|
* back up
|
|
|
|
*/
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
ret = enc_ifdown(&priv->dev);
|
|
|
|
DEBUGASSERT(ret == OK);
|
|
|
|
ret = enc_ifup(&priv->dev);
|
|
|
|
DEBUGASSERT(ret == OK);
|
2015-07-01 22:33:37 +02:00
|
|
|
UNUSED(ret);
|
2010-04-28 04:29:13 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Then poll the network for new XMIT data */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
devif_poll(&priv->dev, enc_txpoll);
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2016-05-30 17:37:34 +02:00
|
|
|
/* Release lock on the network */
|
2012-09-17 20:35:37 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txtimeout
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-09-17 20:35:37 +02:00
|
|
|
* Our TX watchdog timed out. Called from the timer interrupt handler.
|
|
|
|
* The last TX never completed. Perform work on the worker thread.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2020-08-09 20:29:35 +02:00
|
|
|
* arg - The argument
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void enc_txtimeout(wdparm_t arg)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)arg;
|
2012-09-17 20:35:37 +02:00
|
|
|
int ret;
|
|
|
|
|
2018-09-25 15:07:04 +02:00
|
|
|
/* In complex environments, we cannot do SPI transfers from the timeout
|
2012-09-17 20:35:37 +02:00
|
|
|
* handler because semaphores are probably used to lock the SPI bus. In
|
|
|
|
* this case, we will defer processing to the worker thread. This is also
|
|
|
|
* much kinder in the use of system resources and is, therefore, probably
|
|
|
|
* a good thing to do in any event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && work_available(&priv->towork));
|
|
|
|
|
|
|
|
/* Notice that Tx timeout watchdog is not active so further Tx timeouts
|
|
|
|
* can occur until we restart the Tx timeout watchdog.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
ret = work_queue(ENCWORK, &priv->towork, enc_toworker, priv, 0);
|
2012-09-17 20:35:37 +02:00
|
|
|
DEBUGASSERT(ret == OK);
|
2015-07-01 22:33:37 +02:00
|
|
|
UNUSED(ret);
|
2012-09-17 20:35:37 +02:00
|
|
|
}
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_ifup
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Bring up the Ethernet interface when an IP address is
|
2013-09-06 21:17:12 +02:00
|
|
|
* provided
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_ifup(struct net_driver_s *dev)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
2010-04-30 03:43:46 +02:00
|
|
|
int ret;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2023-08-18 22:23:01 +02:00
|
|
|
ninfo("Bringing up: %u.%u.%u.%u\n",
|
|
|
|
ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr),
|
|
|
|
ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr));
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
enc_lock(priv);
|
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Initialize Ethernet interface, set the MAC address, and make sure that
|
|
|
|
* the ENC28J80 is not in power save mode.
|
|
|
|
*/
|
2010-04-28 04:29:13 +02:00
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
ret = enc_reset(priv);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
enc_setmacaddr(priv);
|
|
|
|
enc_pwrfull(priv);
|
2010-04-29 03:55:16 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Enable interrupts at the ENC28J60. Interrupts are still disabled
|
|
|
|
* at the interrupt controller.
|
|
|
|
*/
|
2010-04-29 03:55:16 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
enc_wrphy(priv, ENC_PHIE, PHIE_PGEIE | PHIE_PLNKIE);
|
2012-09-17 20:35:37 +02:00
|
|
|
enc_bfcgreg(priv, ENC_EIR, EIR_ALLINTS);
|
|
|
|
enc_wrgreg(priv, ENC_EIE, EIE_INTIE | EIE_PKTIE | EIE_LINKIE |
|
|
|
|
EIE_TXIE | EIE_TXERIE | EIE_RXERIE);
|
2010-04-29 03:55:16 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Enable the receiver */
|
2010-04-29 03:55:16 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
enc_bfsgreg(priv, ENC_ECON1, ECON1_RXEN);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-05-01 04:14:21 +02:00
|
|
|
/* Mark the interface up and enable the Ethernet interrupt at the
|
|
|
|
* controller
|
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
priv->ifstate = ENCSTATE_UP;
|
2012-09-10 18:48:45 +02:00
|
|
|
priv->lower->enable(priv->lower);
|
2010-04-30 03:43:46 +02:00
|
|
|
}
|
2012-09-10 18:48:45 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Un-lock the SPI bus */
|
|
|
|
|
|
|
|
enc_unlock(priv);
|
2010-04-30 03:43:46 +02:00
|
|
|
return ret;
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_ifdown
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Stop the interface.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_ifdown(struct net_driver_s *dev)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
2010-04-25 23:24:12 +02:00
|
|
|
irqstate_t flags;
|
2010-04-30 03:43:46 +02:00
|
|
|
int ret;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2023-08-18 22:23:01 +02:00
|
|
|
ninfo("Taking down: %u.%u.%u.%u\n",
|
|
|
|
ip4_addr1(dev->d_ipaddr), ip4_addr2(dev->d_ipaddr),
|
|
|
|
ip4_addr3(dev->d_ipaddr), ip4_addr4(dev->d_ipaddr));
|
2010-05-01 04:14:21 +02:00
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
enc_lock(priv);
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Disable the Ethernet interrupt */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-09-10 18:48:45 +02:00
|
|
|
priv->lower->disable(priv->lower);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-05-29 15:47:28 +02:00
|
|
|
/* Cancel the TX timeout timers */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->txtimeout);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* Reset the device and leave in the power save state */
|
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
ret = enc_reset(priv);
|
2010-04-29 03:55:16 +02:00
|
|
|
enc_pwrsave(priv);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
priv->ifstate = ENCSTATE_DOWN;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-09-19 16:22:18 +02:00
|
|
|
|
|
|
|
/* Un-lock the SPI bus */
|
|
|
|
|
|
|
|
enc_unlock(priv);
|
2010-04-30 03:43:46 +02:00
|
|
|
return ret;
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_txavail
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-09-06 21:17:12 +02:00
|
|
|
* Driver callback invoked when new TX data is available. This is a
|
2010-04-25 23:24:12 +02:00
|
|
|
* stimulus perform an out-of-cycle poll and, thereby, reduce the TX
|
|
|
|
* latency.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-25 23:24:12 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called in normal user mode
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_txavail(struct net_driver_s *dev)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-04-27 05:03:40 +02:00
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
2010-04-25 23:24:12 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
enc_lock(priv);
|
2010-04-25 23:24:12 +02:00
|
|
|
|
|
|
|
/* Ignore the notification if the interface is not yet up */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-09-11 22:33:58 +02:00
|
|
|
if (priv->ifstate == ENCSTATE_UP)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2010-05-04 03:28:47 +02:00
|
|
|
/* Check if the hardware is ready to send another packet. The driver
|
2020-05-04 19:34:04 +02:00
|
|
|
* starts a transmission process by setting ECON1.TXRTS. When the
|
|
|
|
* packet is finished transmitting or is aborted due to an error/
|
|
|
|
* cancellation, the ECON1.TXRTS bit will be cleared.
|
2010-05-04 03:28:47 +02:00
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2010-05-04 03:28:47 +02:00
|
|
|
if ((enc_rdgreg(priv, ENC_ECON1) & ECON1_TXRTS) == 0)
|
|
|
|
{
|
2020-05-04 19:34:04 +02:00
|
|
|
/* The interface is up and TX is idle;
|
|
|
|
* poll the network for new XMIT data
|
|
|
|
*/
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2022-05-29 15:47:28 +02:00
|
|
|
devif_poll(&priv->dev, enc_txpoll);
|
2010-05-04 03:28:47 +02:00
|
|
|
}
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Un-lock the SPI bus */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-09-19 16:22:18 +02:00
|
|
|
enc_unlock(priv);
|
2010-04-25 23:24:12 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2010-07-11 17:17:11 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_addmac
|
2010-07-11 17:17:11 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NuttX Callback: Add the specified MAC address to the hardware multicast
|
|
|
|
* address filtering
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-07-11 17:17:11 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2013-09-06 21:17:12 +02:00
|
|
|
* mac - The MAC address to be added
|
2010-07-11 17:17:11 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_addmac(struct net_driver_s *dev, FAR const uint8_t *mac)
|
2010-07-11 17:17:11 +02:00
|
|
|
{
|
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
enc_lock(priv);
|
|
|
|
|
2010-07-11 17:17:11 +02:00
|
|
|
/* Add the MAC address to the hardware multicast routing table */
|
|
|
|
|
2023-05-01 15:55:35 +02:00
|
|
|
/* #warning "Multicast MAC support not implemented" */
|
2012-09-19 16:22:18 +02:00
|
|
|
|
|
|
|
/* Un-lock the SPI bus */
|
|
|
|
|
|
|
|
enc_unlock(priv);
|
2023-05-01 15:55:35 +02:00
|
|
|
return -ENOSYS;
|
2010-07-11 17:17:11 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_rmmac
|
2010-07-11 17:17:11 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2019-02-24 18:51:25 +01:00
|
|
|
* NuttX Callback: Remove the specified MAC address from the hardware
|
|
|
|
* multicast address filtering
|
2010-07-11 17:17:11 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-07-11 17:17:11 +02:00
|
|
|
* dev - Reference to the NuttX driver state structure
|
2013-09-06 21:17:12 +02:00
|
|
|
* mac - The MAC address to be removed
|
2010-07-11 17:17:11 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2014-06-28 00:48:12 +02:00
|
|
|
static int enc_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac)
|
2010-07-11 17:17:11 +02:00
|
|
|
{
|
|
|
|
FAR struct enc_driver_s *priv = (FAR struct enc_driver_s *)dev->d_private;
|
|
|
|
|
2012-09-19 16:22:18 +02:00
|
|
|
/* Lock the SPI bus so that we have exclusive access */
|
|
|
|
|
|
|
|
enc_lock(priv);
|
|
|
|
|
2010-07-11 17:17:11 +02:00
|
|
|
/* Add the MAC address to the hardware multicast routing table */
|
|
|
|
|
2023-05-01 15:55:35 +02:00
|
|
|
/* #warning "Multicast MAC support not implemented" */
|
2012-09-19 16:22:18 +02:00
|
|
|
|
|
|
|
/* Un-lock the SPI bus */
|
|
|
|
|
|
|
|
enc_unlock(priv);
|
2023-05-01 15:55:35 +02:00
|
|
|
return -ENOSYS;
|
2010-07-11 17:17:11 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_pwrsave
|
2010-04-29 03:55:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The ENC28J60 may be commanded to power-down via the SPI interface.
|
|
|
|
* When powered down, it will no longer be able to transmit and receive
|
|
|
|
* any packets. To maximize power savings:
|
|
|
|
*
|
|
|
|
* 1. Turn off packet reception by clearing ECON1.RXEN.
|
|
|
|
* 2. Wait for any in-progress packets to finish being received by
|
|
|
|
* polling ESTAT.RXBUSY. This bit should be clear before proceeding.
|
|
|
|
* 3. Wait for any current transmissions to end by confirming ECON1.TXRTS
|
|
|
|
* is clear.
|
|
|
|
* 4. Set ECON2.VRPS (if not already set).
|
|
|
|
* 5. Enter Sleep by setting ECON2.PWRSV. All MAC, MII and PHY registers
|
|
|
|
* become inaccessible as a result. Setting PWRSV also clears
|
|
|
|
* ESTAT.CLKRDY automatically.
|
|
|
|
*
|
|
|
|
* In Sleep mode, all registers and buffer memory will maintain their
|
|
|
|
* states. The ETH registers and buffer memory will still be accessible
|
|
|
|
* by the host controller. Additionally, the clock driver will continue
|
|
|
|
* to operate. The CLKOUT function will be unaffected.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-29 03:55:16 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_pwrsave(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Set PWRSV\n");
|
2010-05-04 03:28:47 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* 1. Turn off packet reception by clearing ECON1.RXEN. */
|
|
|
|
|
|
|
|
enc_bfcgreg(priv, ENC_ECON1, ECON1_RXEN);
|
|
|
|
|
|
|
|
/* 2. Wait for any in-progress packets to finish being received by
|
|
|
|
* polling ESTAT.RXBUSY. This bit should be clear before proceeding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (enc_waitbreg(priv, ENC_ESTAT, ESTAT_RXBUSY, 0) == OK)
|
|
|
|
{
|
|
|
|
/* 3. Wait for any current transmissions to end by confirming
|
|
|
|
* ECON1.TXRTS is clear.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enc_waitbreg(priv, ENC_ECON1, ECON1_TXRTS, 0);
|
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
/* 4. Set ECON2.VRPS (if not already set).
|
|
|
|
* (Set in enc_reset()
|
|
|
|
*
|
|
|
|
* 5. Enter Sleep by setting ECON2.PWRSV.
|
|
|
|
*/
|
2010-04-29 03:55:16 +02:00
|
|
|
|
|
|
|
enc_bfsgreg(priv, ENC_ECON2, ECON2_PWRSV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_pwrfull
|
2010-04-29 03:55:16 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* When normal operation is desired, the host controller must perform
|
|
|
|
* a slightly modified procedure:
|
|
|
|
*
|
|
|
|
* 1. Wake-up by clearing ECON2.PWRSV.
|
2020-12-01 22:28:22 +01:00
|
|
|
* 2. Wait at least 300 us for the PHY to stabilize. To accomplish the
|
2010-04-29 03:55:16 +02:00
|
|
|
* delay, the host controller may poll ESTAT.CLKRDY and wait for it
|
|
|
|
* to become set.
|
|
|
|
* 3. Restore receive capability by setting ECON1.RXEN.
|
|
|
|
*
|
|
|
|
* After leaving Sleep mode, there is a delay of many milliseconds
|
|
|
|
* before a new link is established (assuming an appropriate link
|
|
|
|
* partner is present). The host controller may wish to wait until
|
|
|
|
* the link is established before attempting to transmit any packets.
|
|
|
|
* The link status can be determined by polling the PHSTAT2.LSTAT bit.
|
|
|
|
* Alternatively, the link change interrupt may be used if it is
|
|
|
|
* enabled.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-05-01 04:14:21 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2010-04-29 03:55:16 +02:00
|
|
|
****************************************************************************/
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
static void enc_pwrfull(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Clear PWRSV\n");
|
2010-05-04 03:28:47 +02:00
|
|
|
|
2010-04-29 03:55:16 +02:00
|
|
|
/* 1. Wake-up by clearing ECON2.PWRSV. */
|
|
|
|
|
|
|
|
enc_bfcgreg(priv, ENC_ECON2, ECON2_PWRSV);
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2020-12-01 22:28:22 +01:00
|
|
|
/* 2. Wait at least 300 us for the PHY to stabilize. To accomplish the
|
2010-04-29 03:55:16 +02:00
|
|
|
* delay, the host controller may poll ESTAT.CLKRDY and wait for it to
|
|
|
|
* become set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enc_waitbreg(priv, ENC_ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
|
|
|
|
|
|
|
|
/* 3. Restore receive capability by setting ECON1.RXEN.
|
|
|
|
*
|
|
|
|
* The caller will do this when it is read to receive packets
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2010-04-28 04:29:13 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_setmacaddr
|
2010-04-28 04:29:13 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the MAC address to the configured value. This is done after ifup
|
|
|
|
* or after a TX timeout. Note that this means that the interface must
|
|
|
|
* be down before configuring the MAC addr.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-28 04:29:13 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void enc_setmacaddr(FAR struct enc_driver_s *priv)
|
|
|
|
{
|
2012-09-18 14:34:43 +02:00
|
|
|
/* Program the hardware with it's MAC address (for filtering).
|
|
|
|
* MAADR1 MAC Address Byte 1 (MAADR<47:40>), OUI Byte 1
|
|
|
|
* MAADR2 MAC Address Byte 2 (MAADR<39:32>), OUI Byte 2
|
|
|
|
* MAADR3 MAC Address Byte 3 (MAADR<31:24>), OUI Byte 3
|
|
|
|
* MAADR4 MAC Address Byte 4 (MAADR<23:16>)
|
|
|
|
* MAADR5 MAC Address Byte 5 (MAADR<15:8>)
|
|
|
|
* MAADR6 MAC Address Byte 6 (MAADR<7:0>)
|
|
|
|
*/
|
|
|
|
|
2017-04-22 19:10:30 +02:00
|
|
|
enc_wrbreg(priv, ENC_MAADR1, priv->dev.d_mac.ether.ether_addr_octet[0]);
|
|
|
|
enc_wrbreg(priv, ENC_MAADR2, priv->dev.d_mac.ether.ether_addr_octet[1]);
|
|
|
|
enc_wrbreg(priv, ENC_MAADR3, priv->dev.d_mac.ether.ether_addr_octet[2]);
|
|
|
|
enc_wrbreg(priv, ENC_MAADR4, priv->dev.d_mac.ether.ether_addr_octet[3]);
|
|
|
|
enc_wrbreg(priv, ENC_MAADR5, priv->dev.d_mac.ether.ether_addr_octet[4]);
|
|
|
|
enc_wrbreg(priv, ENC_MAADR6, priv->dev.d_mac.ether.ether_addr_octet[5]);
|
2010-04-28 04:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_reset
|
2010-04-28 04:29:13 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop, reset, re-initialize, and restart the ENC28J60. This is done
|
|
|
|
* initially, on ifup, and after a TX timeout.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-28 04:29:13 +02:00
|
|
|
* priv - Reference to the driver state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
static int enc_reset(FAR struct enc_driver_s *priv)
|
2010-04-28 04:29:13 +02:00
|
|
|
{
|
2010-04-30 03:43:46 +02:00
|
|
|
uint8_t regval;
|
|
|
|
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Reset\n");
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Configure SPI for the ENC28J60 */
|
|
|
|
|
|
|
|
enc_configspi(priv->spi);
|
|
|
|
|
|
|
|
/* Reset the ENC28J60 */
|
|
|
|
|
2012-09-12 02:12:18 +02:00
|
|
|
enc_src(priv);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Initialize ECON1: Clear ECON1 */
|
|
|
|
|
|
|
|
enc_wrgreg(priv, ENC_ECON1, 0x00);
|
|
|
|
|
|
|
|
/* Initialize ECON2: Enable address auto increment and voltage
|
|
|
|
* regulator powersave.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enc_wrgreg(priv, ENC_ECON2, ECON2_AUTOINC | ECON2_VRPS);
|
|
|
|
|
|
|
|
/* Initialize receive buffer.
|
|
|
|
* First, set the receive buffer start address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->nextpkt = PKTMEM_RX_START;
|
|
|
|
enc_wrbreg(priv, ENC_ERXSTL, PKTMEM_RX_START & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ERXSTH, PKTMEM_RX_START >> 8);
|
|
|
|
|
|
|
|
/* Set the receive data pointer */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_ERXRDPTL, PKTMEM_RX_START & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ERXRDPTH, PKTMEM_RX_START >> 8);
|
|
|
|
|
|
|
|
/* Set the receive buffer end. */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_ERXNDL, PKTMEM_RX_END & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ERXNDH, PKTMEM_RX_END >> 8);
|
|
|
|
|
|
|
|
/* Set transmit buffer start. */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_ETXSTL, PKTMEM_TX_START & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_ETXSTH, PKTMEM_TX_START >> 8);
|
|
|
|
|
|
|
|
/* Check if we are actually communicating with the ENC28J60. If its
|
|
|
|
* 0x00 or 0xff, then we are probably not communicating correctly
|
|
|
|
* via SPI.
|
|
|
|
*/
|
|
|
|
|
|
|
|
regval = enc_rdbreg(priv, ENC_EREVID);
|
|
|
|
if (regval == 0x00 || regval == 0xff)
|
|
|
|
{
|
2016-06-20 19:59:15 +02:00
|
|
|
nerr("ERROR: Bad Rev ID: %02x\n", regval);
|
2010-04-30 03:43:46 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
2012-09-12 02:12:18 +02:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Rev ID: %02x\n", regval);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Set filter mode: unicast OR broadcast AND crc valid */
|
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
enc_wrbreg(priv, ENC_ERXFCON, ERXFCON_UCEN | ERXFCON_CRCEN |
|
|
|
|
ERXFCON_BCEN);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Enable MAC receive */
|
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
enc_wrbreg(priv, ENC_MACON1, MACON1_MARXEN | MACON1_TXPAUS |
|
|
|
|
MACON1_RXPAUS);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Enable automatic padding and CRC operations */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENC28J60_HALFDUPLEX
|
2019-02-24 18:51:25 +01:00
|
|
|
enc_wrbreg(priv, ENC_MACON3, MACON3_PADCFG0 | MACON3_TXCRCEN |
|
|
|
|
MACON3_FRMLNEN);
|
2010-04-30 03:43:46 +02:00
|
|
|
enc_wrbreg(priv, ENC_MACON4, MACON4_DEFER); /* Defer transmission enable */
|
|
|
|
|
|
|
|
/* Set Non-Back-to-Back Inter-Packet Gap */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MAIPGL, 0x12);
|
|
|
|
enc_wrbreg(priv, ENC_MAIPGH, 0x0c);
|
|
|
|
|
|
|
|
/* Set Back-to-Back Inter-Packet Gap */
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
enc_wrbreg(priv, ENC_MABBIPG, 0x12);
|
|
|
|
#else
|
|
|
|
/* Set filter mode: unicast OR broadcast AND crc valid AND Full Duplex */
|
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MACON3,
|
2019-02-24 18:51:25 +01:00
|
|
|
MACON3_PADCFG0 | MACON3_TXCRCEN | MACON3_FRMLNEN |
|
|
|
|
MACON3_FULDPX);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
2010-05-04 03:28:47 +02:00
|
|
|
/* Set Non-Back-to-Back Inter-Packet Gap */
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MAIPGL, 0x12);
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
/* Set Back-to-Back Inter-Packet Gap */
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
enc_wrbreg(priv, ENC_MABBIPG, 0x15);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the maximum packet size which the controller will accept */
|
|
|
|
|
2018-07-04 22:10:40 +02:00
|
|
|
enc_wrbreg(priv, ENC_MAMXFLL, CONFIG_NET_ETH_PKTSIZE & 0xff);
|
|
|
|
enc_wrbreg(priv, ENC_MAMXFLH, CONFIG_NET_ETH_PKTSIZE >> 8);
|
2010-04-30 03:43:46 +02:00
|
|
|
|
|
|
|
/* Configure LEDs (No, just use the defaults for now) */
|
|
|
|
|
|
|
|
/* Setup up PHCON1 & 2 */
|
2013-09-06 21:17:12 +02:00
|
|
|
|
2010-04-30 03:43:46 +02:00
|
|
|
#ifdef CONFIG_ENC28J60_HALFDUPLEX
|
|
|
|
enc_wrphy(priv, ENC_PHCON1, 0x00);
|
|
|
|
enc_wrphy(priv, ENC_PHCON2, PHCON2_HDLDIS);
|
|
|
|
#else
|
|
|
|
enc_wrphy(priv, ENC_PHCON1, PHCON1_PDPXMD);
|
|
|
|
enc_wrphy(priv, ENC_PHCON2, 0x00);
|
|
|
|
#endif
|
|
|
|
return OK;
|
2010-04-28 04:29:13 +02:00
|
|
|
}
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: enc_initialize
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2010-04-27 05:03:40 +02:00
|
|
|
* Initialize the Ethernet driver. The ENC28J60 device is assumed to be
|
|
|
|
* in the post-reset state upon entry to this function.
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2010-04-28 04:29:13 +02:00
|
|
|
* spi - A reference to the platform's SPI driver for the ENC28J60
|
2012-09-10 18:48:45 +02:00
|
|
|
* lower - The MCU-specific interrupt used to control low-level MCU
|
|
|
|
* functions (i.e., ENC28J60 GPIO interrupts).
|
2010-04-28 04:29:13 +02:00
|
|
|
* devno - If more than one ENC28J60 is supported, then this is the
|
|
|
|
* zero based number that identifies the ENC28J60;
|
2010-04-25 23:24:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK on success; Negated errno on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-10 18:48:45 +02:00
|
|
|
int enc_initialize(FAR struct spi_dev_s *spi,
|
|
|
|
FAR const struct enc_lower_s *lower, unsigned int devno)
|
2010-04-25 23:24:12 +02:00
|
|
|
{
|
2012-09-11 22:33:58 +02:00
|
|
|
FAR struct enc_driver_s *priv;
|
2010-04-28 04:29:13 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(devno < CONFIG_ENC28J60_NINTERFACES);
|
|
|
|
priv = &g_enc28j60[devno];
|
|
|
|
|
2010-04-25 23:24:12 +02:00
|
|
|
/* Initialize the driver structure */
|
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
memset(g_enc28j60, 0,
|
|
|
|
CONFIG_ENC28J60_NINTERFACES * sizeof(struct enc_driver_s));
|
|
|
|
|
2022-08-04 19:47:53 +02:00
|
|
|
priv->dev.d_buf = (FAR uint8_t *)g_pktbuf[devno]; /* Single packet buffer */
|
|
|
|
priv->dev.d_ifup = enc_ifup; /* I/F down callback */
|
|
|
|
priv->dev.d_ifdown = enc_ifdown; /* I/F up (new IP address) callback */
|
|
|
|
priv->dev.d_txavail = enc_txavail; /* New TX data callback */
|
2018-10-31 22:03:51 +01:00
|
|
|
#ifdef CONFIG_NET_MCASTGROUP
|
2022-08-04 19:47:53 +02:00
|
|
|
priv->dev.d_addmac = enc_addmac; /* Add multicast MAC address */
|
|
|
|
priv->dev.d_rmmac = enc_rmmac; /* Remove multicast MAC address */
|
2010-07-11 17:17:11 +02:00
|
|
|
#endif
|
2022-08-04 19:47:53 +02:00
|
|
|
priv->dev.d_private = priv; /* Used to recover private state from dev */
|
|
|
|
priv->spi = spi; /* Save the SPI instance */
|
|
|
|
priv->lower = lower; /* Save the low-level MCU interface */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2019-02-24 18:51:25 +01:00
|
|
|
/* The interface should be in the down state. However, this function is
|
|
|
|
* called too early in initialization to perform the ENC28J60 reset in
|
|
|
|
* enc_ifdown. We are depending upon the fact that the application level
|
|
|
|
* logic will call enc_ifdown later to reset the ENC28J60. NOTE: The MAC
|
|
|
|
* address will not be set up until enc_ifup() is called. That gives the
|
|
|
|
* app time to set the MAC address before bringing the interface up.
|
2010-04-29 03:55:16 +02:00
|
|
|
*/
|
|
|
|
|
2012-09-17 20:35:37 +02:00
|
|
|
priv->ifstate = ENCSTATE_UNINIT;
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* Attach the interrupt to the driver (but don't enable it yet) */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2017-08-22 00:49:43 +02:00
|
|
|
if (lower->attach(lower, enc_interrupt, priv) < 0)
|
2012-09-11 22:33:58 +02:00
|
|
|
{
|
|
|
|
/* We could not attach the ISR to the interrupt */
|
2010-04-25 23:24:12 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
return -EAGAIN;
|
2010-04-30 03:43:46 +02:00
|
|
|
}
|
2012-09-10 18:48:45 +02:00
|
|
|
|
2012-09-11 22:33:58 +02:00
|
|
|
/* Register the device with the OS so that socket IOCTLs can be performed */
|
|
|
|
|
2014-11-15 15:22:51 +01:00
|
|
|
return netdev_register(&priv->dev, NET_LL_ETHERNET);
|
2010-04-25 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_ENC28J60_NET */
|