KEIS

Dependents:   EthernetInterface_ccx

Fork of lwip-eth by mbed official

Committer:
emilmont
Date:
Fri Mar 01 15:30:33 2013 +0000
Revision:
4:d827a085afd9
Parent:
3:dd8b8f5b449a
Child:
5:698d868a5285
Apply latest NXP patches

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 4:d827a085afd9 1 /**********************************************************************
emilmont 4:d827a085afd9 2 * $Id$ lpc_phy_dp83848.c 2011-11-20
emilmont 4:d827a085afd9 3 *//**
emilmont 4:d827a085afd9 4 * @file lpc_phy_dp83848.c
emilmont 4:d827a085afd9 5 * @brief DP83848C PHY status and control.
emilmont 4:d827a085afd9 6 * @version 1.0
emilmont 4:d827a085afd9 7 * @date 20 Nov. 2011
emilmont 4:d827a085afd9 8 * @author NXP MCU SW Application Team
emilmont 4:d827a085afd9 9 *
emilmont 4:d827a085afd9 10 * Copyright(C) 2011, NXP Semiconductor
emilmont 4:d827a085afd9 11 * All rights reserved.
emilmont 4:d827a085afd9 12 *
emilmont 4:d827a085afd9 13 ***********************************************************************
emilmont 4:d827a085afd9 14 * Software that is described herein is for illustrative purposes only
emilmont 4:d827a085afd9 15 * which provides customers with programming information regarding the
emilmont 4:d827a085afd9 16 * products. This software is supplied "AS IS" without any warranties.
emilmont 4:d827a085afd9 17 * NXP Semiconductors assumes no responsibility or liability for the
emilmont 4:d827a085afd9 18 * use of the software, conveys no license or title under any patent,
emilmont 4:d827a085afd9 19 * copyright, or mask work right to the product. NXP Semiconductors
emilmont 4:d827a085afd9 20 * reserves the right to make changes in the software without
emilmont 4:d827a085afd9 21 * notification. NXP Semiconductors also make no representation or
emilmont 4:d827a085afd9 22 * warranty that such application will be suitable for the specified
emilmont 4:d827a085afd9 23 * use without further testing or modification.
emilmont 4:d827a085afd9 24 **********************************************************************/
emilmont 4:d827a085afd9 25
emilmont 4:d827a085afd9 26 #include "lwip/opt.h"
emilmont 4:d827a085afd9 27 #include "lwip/err.h"
emilmont 4:d827a085afd9 28 #include "lwip/tcpip.h"
emilmont 4:d827a085afd9 29 #include "lwip/snmp.h"
emilmont 4:d827a085afd9 30 #include "lpc_emac_config.h"
emilmont 4:d827a085afd9 31 #include "lpc_phy.h"
emilmont 4:d827a085afd9 32
emilmont 4:d827a085afd9 33 /** @defgroup dp83848_phy PHY status and control for the DP83848.
emilmont 4:d827a085afd9 34 * @ingroup lwip_phy
emilmont 4:d827a085afd9 35 *
emilmont 4:d827a085afd9 36 * Various functions for controlling and monitoring the status of the
emilmont 4:d827a085afd9 37 * DP83848 PHY. In polled (standalone) systems, the PHY state must be
emilmont 4:d827a085afd9 38 * monitored as part of the application. In a threaded (RTOS) system,
emilmont 4:d827a085afd9 39 * the PHY state is monitored by the PHY handler thread. The MAC
emilmont 4:d827a085afd9 40 * driver will not transmit unless the PHY link is active.
emilmont 4:d827a085afd9 41 * @{
emilmont 4:d827a085afd9 42 */
emilmont 4:d827a085afd9 43
emilmont 4:d827a085afd9 44 /** \brief DP83848 PHY register offsets */
emilmont 4:d827a085afd9 45 #define DP8_BMCR_REG 0x0 /**< Basic Mode Control Register */
emilmont 4:d827a085afd9 46 #define DP8_BMSR_REG 0x1 /**< Basic Mode Status Reg */
emilmont 4:d827a085afd9 47 #define DP8_ANADV_REG 0x4 /**< Auto_Neg Advt Reg */
emilmont 4:d827a085afd9 48 #define DP8_ANLPA_REG 0x5 /**< Auto_neg Link Partner Ability Reg */
emilmont 4:d827a085afd9 49 #define DP8_ANEEXP_REG 0x6 /**< Auto-neg Expansion Reg */
emilmont 4:d827a085afd9 50 #define DP8_PHY_STAT_REG 0x10 /**< PHY Status Register */
emilmont 4:d827a085afd9 51 #define DP8_PHY_INT_CTL_REG 0x11 /**< PHY Interrupt Control Register */
emilmont 4:d827a085afd9 52 #define DP8_PHY_RBR_REG 0x17 /**< PHY RMII and Bypass Register */
emilmont 4:d827a085afd9 53 #define DP8_PHY_STS_REG 0x19 /**< PHY Status Register */
emilmont 4:d827a085afd9 54
emilmont 4:d827a085afd9 55 /** \brief DP83848 Control register definitions */
emilmont 4:d827a085afd9 56 #define DP8_RESET (1 << 15) /**< 1= S/W Reset */
emilmont 4:d827a085afd9 57 #define DP8_LOOPBACK (1 << 14) /**< 1=loopback Enabled */
emilmont 4:d827a085afd9 58 #define DP8_SPEED_SELECT (1 << 13) /**< 1=Select 100MBps */
emilmont 4:d827a085afd9 59 #define DP8_AUTONEG (1 << 12) /**< 1=Enable auto-negotiation */
emilmont 4:d827a085afd9 60 #define DP8_POWER_DOWN (1 << 11) /**< 1=Power down PHY */
emilmont 4:d827a085afd9 61 #define DP8_ISOLATE (1 << 10) /**< 1=Isolate PHY */
emilmont 4:d827a085afd9 62 #define DP8_RESTART_AUTONEG (1 << 9) /**< 1=Restart auto-negoatiation */
emilmont 4:d827a085afd9 63 #define DP8_DUPLEX_MODE (1 << 8) /**< 1=Full duplex mode */
emilmont 4:d827a085afd9 64 #define DP8_COLLISION_TEST (1 << 7) /**< 1=Perform collsion test */
emilmont 4:d827a085afd9 65
emilmont 4:d827a085afd9 66 /** \brief DP83848 Status register definitions */
emilmont 4:d827a085afd9 67 #define DP8_100BASE_T4 (1 << 15) /**< T4 mode */
emilmont 4:d827a085afd9 68 #define DP8_100BASE_TX_FD (1 << 14) /**< 100MBps full duplex */
emilmont 4:d827a085afd9 69 #define DP8_100BASE_TX_HD (1 << 13) /**< 100MBps half duplex */
emilmont 4:d827a085afd9 70 #define DP8_10BASE_T_FD (1 << 12) /**< 100Bps full duplex */
emilmont 4:d827a085afd9 71 #define DP8_10BASE_T_HD (1 << 11) /**< 10MBps half duplex */
emilmont 4:d827a085afd9 72 #define DP8_MF_PREAMB_SUPPR (1 << 6) /**< Preamble suppress */
emilmont 4:d827a085afd9 73 #define DP8_AUTONEG_COMP (1 << 5) /**< Auto-negotation complete */
emilmont 4:d827a085afd9 74 #define DP8_RMT_FAULT (1 << 4) /**< Fault */
emilmont 4:d827a085afd9 75 #define DP8_AUTONEG_ABILITY (1 << 3) /**< Auto-negotation supported */
emilmont 4:d827a085afd9 76 #define DP8_LINK_STATUS (1 << 2) /**< 1=Link active */
emilmont 4:d827a085afd9 77 #define DP8_JABBER_DETECT (1 << 1) /**< Jabber detect */
emilmont 4:d827a085afd9 78 #define DP8_EXTEND_CAPAB (1 << 0) /**< Supports extended capabilities */
emilmont 4:d827a085afd9 79
emilmont 4:d827a085afd9 80 /** \brief DP83848 PHY RBR MII dode definitions */
emilmont 4:d827a085afd9 81 #define DP8_RBR_RMII_MODE (1 << 5) /**< Use RMII mode */
emilmont 4:d827a085afd9 82
emilmont 4:d827a085afd9 83 /** \brief DP83848 PHY status definitions */
emilmont 4:d827a085afd9 84 #define DP8_REMOTEFAULT (1 << 6) /**< Remote fault */
emilmont 4:d827a085afd9 85 #define DP8_FULLDUPLEX (1 << 2) /**< 1=full duplex */
emilmont 4:d827a085afd9 86 #define DP8_SPEED10MBPS (1 << 1) /**< 1=10MBps speed */
emilmont 4:d827a085afd9 87 #define DP8_VALID_LINK (1 << 0) /**< 1=Link active */
emilmont 4:d827a085afd9 88
emilmont 4:d827a085afd9 89 /** \brief DP83848 PHY ID register definitions */
emilmont 4:d827a085afd9 90 #define DP8_PHYID1_OUI 0x2000 /**< Expected PHY ID1 */
emilmont 4:d827a085afd9 91 #define DP8_PHYID2_OUI 0x5c90 /**< Expected PHY ID2 */
emilmont 4:d827a085afd9 92
emilmont 4:d827a085afd9 93 /** \brief PHY status structure used to indicate current status of PHY.
emilmont 4:d827a085afd9 94 */
emilmont 4:d827a085afd9 95 typedef struct {
emilmont 4:d827a085afd9 96 u32_t phy_speed_100mbs:1; /**< 10/100 MBS connection speed flag. */
emilmont 4:d827a085afd9 97 u32_t phy_full_duplex:1; /**< Half/full duplex connection speed flag. */
emilmont 4:d827a085afd9 98 u32_t phy_link_active:1; /**< Phy link active flag. */
emilmont 4:d827a085afd9 99 } PHY_STATUS_TYPE;
emilmont 4:d827a085afd9 100
emilmont 4:d827a085afd9 101 /** \brief PHY update flags */
emilmont 4:d827a085afd9 102 static PHY_STATUS_TYPE physts;
emilmont 4:d827a085afd9 103
emilmont 4:d827a085afd9 104 /** \brief Last PHY update flags, used for determing if something has changed */
emilmont 4:d827a085afd9 105 static PHY_STATUS_TYPE olddphysts;
emilmont 4:d827a085afd9 106
emilmont 4:d827a085afd9 107 /** \brief PHY update counter for state machine */
emilmont 4:d827a085afd9 108 static s32_t phyustate;
emilmont 4:d827a085afd9 109
emilmont 4:d827a085afd9 110 /** \brief Update PHY status from passed value
emilmont 4:d827a085afd9 111 *
emilmont 4:d827a085afd9 112 * This function updates the current PHY status based on the
emilmont 4:d827a085afd9 113 * passed PHY status word. The PHY status indicate if the link
emilmont 4:d827a085afd9 114 * is active, the connection speed, and duplex.
emilmont 4:d827a085afd9 115 *
emilmont 4:d827a085afd9 116 * \param[in] netif NETIF structure
emilmont 4:d827a085afd9 117 * \param[in] linksts Status word from PHY
emilmont 4:d827a085afd9 118 * \return 1 if the status has changed, otherwise 0
emilmont 4:d827a085afd9 119 */
emilmont 4:d827a085afd9 120 static s32_t lpc_update_phy_sts(struct netif *netif, u32_t linksts)
emilmont 4:d827a085afd9 121 {
emilmont 4:d827a085afd9 122 s32_t changed = 0;
emilmont 4:d827a085afd9 123
emilmont 4:d827a085afd9 124 /* Update link active status */
emilmont 4:d827a085afd9 125 if (linksts & DP8_VALID_LINK)
emilmont 4:d827a085afd9 126 physts.phy_link_active = 1;
emilmont 4:d827a085afd9 127 else
emilmont 4:d827a085afd9 128 physts.phy_link_active = 0;
emilmont 4:d827a085afd9 129
emilmont 4:d827a085afd9 130 /* Full or half duplex */
emilmont 4:d827a085afd9 131 if (linksts & DP8_FULLDUPLEX)
emilmont 4:d827a085afd9 132 physts.phy_full_duplex = 1;
emilmont 4:d827a085afd9 133 else
emilmont 4:d827a085afd9 134 physts.phy_full_duplex = 0;
emilmont 4:d827a085afd9 135
emilmont 4:d827a085afd9 136 /* Configure 100MBit/10MBit mode. */
emilmont 4:d827a085afd9 137 if (linksts & DP8_SPEED10MBPS)
emilmont 4:d827a085afd9 138 physts.phy_speed_100mbs = 0;
emilmont 4:d827a085afd9 139 else
emilmont 4:d827a085afd9 140 physts.phy_speed_100mbs = 1;
emilmont 4:d827a085afd9 141
emilmont 4:d827a085afd9 142 if (physts.phy_speed_100mbs != olddphysts.phy_speed_100mbs) {
emilmont 4:d827a085afd9 143 changed = 1;
emilmont 4:d827a085afd9 144 if (physts.phy_speed_100mbs) {
emilmont 4:d827a085afd9 145 /* 100MBit mode. */
emilmont 4:d827a085afd9 146 lpc_emac_set_speed(1);
emilmont 4:d827a085afd9 147
emilmont 4:d827a085afd9 148 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
emilmont 4:d827a085afd9 149 }
emilmont 4:d827a085afd9 150 else {
emilmont 4:d827a085afd9 151 /* 10MBit mode. */
emilmont 4:d827a085afd9 152 lpc_emac_set_speed(0);
emilmont 4:d827a085afd9 153
emilmont 4:d827a085afd9 154 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000);
emilmont 4:d827a085afd9 155 }
emilmont 4:d827a085afd9 156
emilmont 4:d827a085afd9 157 olddphysts.phy_speed_100mbs = physts.phy_speed_100mbs;
emilmont 4:d827a085afd9 158 }
emilmont 4:d827a085afd9 159
emilmont 4:d827a085afd9 160 if (physts.phy_full_duplex != olddphysts.phy_full_duplex) {
emilmont 4:d827a085afd9 161 changed = 1;
emilmont 4:d827a085afd9 162 if (physts.phy_full_duplex)
emilmont 4:d827a085afd9 163 lpc_emac_set_duplex(1);
emilmont 4:d827a085afd9 164 else
emilmont 4:d827a085afd9 165 lpc_emac_set_duplex(0);
emilmont 4:d827a085afd9 166
emilmont 4:d827a085afd9 167 olddphysts.phy_full_duplex = physts.phy_full_duplex;
emilmont 4:d827a085afd9 168 }
emilmont 4:d827a085afd9 169
emilmont 4:d827a085afd9 170 if (physts.phy_link_active != olddphysts.phy_link_active) {
emilmont 4:d827a085afd9 171 changed = 1;
emilmont 4:d827a085afd9 172 #if NO_SYS == 1
emilmont 4:d827a085afd9 173 if (physts.phy_link_active)
emilmont 4:d827a085afd9 174 netif_set_link_up(netif);
emilmont 4:d827a085afd9 175 else
emilmont 4:d827a085afd9 176 netif_set_link_down(netif);
emilmont 4:d827a085afd9 177 #else
emilmont 4:d827a085afd9 178 if (physts.phy_link_active)
emilmont 4:d827a085afd9 179 tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up,
emilmont 4:d827a085afd9 180 (void*) netif, 1);
emilmont 4:d827a085afd9 181 else
emilmont 4:d827a085afd9 182 tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down,
emilmont 4:d827a085afd9 183 (void*) netif, 1);
emilmont 4:d827a085afd9 184 #endif
emilmont 4:d827a085afd9 185
emilmont 4:d827a085afd9 186 olddphysts.phy_link_active = physts.phy_link_active;
emilmont 4:d827a085afd9 187 }
emilmont 4:d827a085afd9 188
emilmont 4:d827a085afd9 189 return changed;
emilmont 4:d827a085afd9 190 }
emilmont 4:d827a085afd9 191
emilmont 4:d827a085afd9 192 /** \brief Initialize the DP83848 PHY.
emilmont 4:d827a085afd9 193 *
emilmont 4:d827a085afd9 194 * This function initializes the DP83848 PHY. It will block until
emilmont 4:d827a085afd9 195 * complete. This function is called as part of the EMAC driver
emilmont 4:d827a085afd9 196 * initialization. Configuration of the PHY at startup is
emilmont 4:d827a085afd9 197 * controlled by setting up configuration defines in lpc_phy.h.
emilmont 4:d827a085afd9 198 *
emilmont 4:d827a085afd9 199 * \param[in] netif NETIF structure
emilmont 4:d827a085afd9 200 * \param[in] rmii If set, configures the PHY for RMII mode
emilmont 4:d827a085afd9 201 * \return ERR_OK if the setup was successful, otherwise ERR_TIMEOUT
emilmont 4:d827a085afd9 202 */
emilmont 4:d827a085afd9 203 err_t lpc_phy_init(struct netif *netif, int rmii)
emilmont 4:d827a085afd9 204 {
emilmont 4:d827a085afd9 205 u32_t tmp;
emilmont 4:d827a085afd9 206 s32_t i;
emilmont 4:d827a085afd9 207
emilmont 4:d827a085afd9 208 physts.phy_speed_100mbs = olddphysts.phy_speed_100mbs = 2;
emilmont 4:d827a085afd9 209 physts.phy_full_duplex = olddphysts.phy_full_duplex = 2;
emilmont 4:d827a085afd9 210 physts.phy_link_active = olddphysts.phy_link_active = 2;
emilmont 4:d827a085afd9 211 phyustate = 0;
emilmont 4:d827a085afd9 212
emilmont 4:d827a085afd9 213 /* Only first read and write are checked for failure */
emilmont 4:d827a085afd9 214 /* Put the DP83848C in reset mode and wait for completion */
emilmont 4:d827a085afd9 215 if (lpc_mii_write(DP8_BMCR_REG, DP8_RESET) != 0)
emilmont 4:d827a085afd9 216 return ERR_TIMEOUT;
emilmont 4:d827a085afd9 217 i = 400;
emilmont 4:d827a085afd9 218 while (i > 0) {
emilmont 4:d827a085afd9 219 osDelay(1); /* 1 ms */
emilmont 4:d827a085afd9 220 if (lpc_mii_read(DP8_BMCR_REG, &tmp) != 0)
emilmont 4:d827a085afd9 221 return ERR_TIMEOUT;
emilmont 4:d827a085afd9 222
emilmont 4:d827a085afd9 223 if (!(tmp & (DP8_RESET | DP8_POWER_DOWN)))
emilmont 4:d827a085afd9 224 i = -1;
emilmont 4:d827a085afd9 225 else
emilmont 4:d827a085afd9 226 i--;
emilmont 4:d827a085afd9 227 }
emilmont 4:d827a085afd9 228 /* Timeout? */
emilmont 4:d827a085afd9 229 if (i == 0)
emilmont 4:d827a085afd9 230 return ERR_TIMEOUT;
emilmont 4:d827a085afd9 231
emilmont 4:d827a085afd9 232 /* Setup link based on configuration options */
emilmont 4:d827a085afd9 233 #if PHY_USE_AUTONEG==1
emilmont 4:d827a085afd9 234 tmp = DP8_AUTONEG;
emilmont 4:d827a085afd9 235 #else
emilmont 4:d827a085afd9 236 tmp = 0;
emilmont 4:d827a085afd9 237 #endif
emilmont 4:d827a085afd9 238 #if PHY_USE_100MBS==1
emilmont 4:d827a085afd9 239 tmp |= DP8_SPEED_SELECT;
emilmont 4:d827a085afd9 240 #endif
emilmont 4:d827a085afd9 241 #if PHY_USE_FULL_DUPLEX==1
emilmont 4:d827a085afd9 242 tmp |= DP8_DUPLEX_MODE;
emilmont 4:d827a085afd9 243 #endif
emilmont 4:d827a085afd9 244 lpc_mii_write(DP8_BMCR_REG, tmp);
emilmont 4:d827a085afd9 245
emilmont 4:d827a085afd9 246 /* Enable RMII mode for PHY */
emilmont 4:d827a085afd9 247 if (rmii)
emilmont 4:d827a085afd9 248 lpc_mii_write(DP8_PHY_RBR_REG, DP8_RBR_RMII_MODE);
emilmont 4:d827a085afd9 249
emilmont 4:d827a085afd9 250 /* The link is not set active at this point, but will be detected
emilmont 4:d827a085afd9 251 later */
emilmont 4:d827a085afd9 252
emilmont 4:d827a085afd9 253 return ERR_OK;
emilmont 4:d827a085afd9 254 }
emilmont 4:d827a085afd9 255
emilmont 4:d827a085afd9 256 /* Phy status update state machine */
emilmont 4:d827a085afd9 257 s32_t lpc_phy_sts_sm(struct netif *netif)
emilmont 4:d827a085afd9 258 {
emilmont 4:d827a085afd9 259 s32_t changed = 0;
emilmont 4:d827a085afd9 260
emilmont 4:d827a085afd9 261 switch (phyustate) {
emilmont 4:d827a085afd9 262 default:
emilmont 4:d827a085afd9 263 case 0:
emilmont 4:d827a085afd9 264 /* Read BMSR to clear faults */
emilmont 4:d827a085afd9 265 lpc_mii_read_noblock(DP8_PHY_STAT_REG);
emilmont 4:d827a085afd9 266 phyustate = 1;
emilmont 4:d827a085afd9 267 break;
emilmont 4:d827a085afd9 268
emilmont 4:d827a085afd9 269 case 1:
emilmont 4:d827a085afd9 270 /* Wait for read status state */
emilmont 4:d827a085afd9 271 if (!lpc_mii_is_busy()) {
emilmont 4:d827a085afd9 272 /* Update PHY status */
emilmont 4:d827a085afd9 273 changed = lpc_update_phy_sts(netif, lpc_mii_read_data());
emilmont 4:d827a085afd9 274 phyustate = 0;
emilmont 4:d827a085afd9 275 }
emilmont 4:d827a085afd9 276 break;
emilmont 4:d827a085afd9 277 }
emilmont 4:d827a085afd9 278
emilmont 4:d827a085afd9 279 return changed;
emilmont 4:d827a085afd9 280 }
emilmont 4:d827a085afd9 281
emilmont 4:d827a085afd9 282 /**
emilmont 4:d827a085afd9 283 * @}
emilmont 4:d827a085afd9 284 */
emilmont 4:d827a085afd9 285
emilmont 4:d827a085afd9 286 /* --------------------------------- End Of File ------------------------------ */