📄 ether.c
字号:
/* * Ethernet driver for TI TMS320DM644x (DaVinci) chips. * * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> * * Parts shamelessly stolen from TI's dm644x_emac.c. Original copyright * follows: * * ---------------------------------------------------------------------------- * * dm644x_emac.c * * TI DaVinci (DM644X) EMAC peripheral driver source for DV-EVM * * Copyright (C) 2005 Texas Instruments. * * ---------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * ---------------------------------------------------------------------------- * Modifications: * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot. * ver 1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors * */#include <common.h>#include <command.h>#include <net.h>#include <miiphy.h>#include <asm/arch/emac_defs.h>#ifdef CONFIG_DRIVER_TI_EMAC#ifdef CONFIG_CMD_NETunsigned int emac_dbg = 0;#define debug_emac(fmt,args...) if (emac_dbg) printf(fmt,##args)/* Internal static functions */static int dm644x_eth_hw_init (void);static int dm644x_eth_open (void);static int dm644x_eth_close (void);static int dm644x_eth_send_packet (volatile void *packet, int length);static int dm644x_eth_rcv_packet (void);static void dm644x_eth_mdio_enable(void);static int gen_init_phy(int phy_addr);static int gen_is_phy_connected(int phy_addr);static int gen_get_link_speed(int phy_addr);static int gen_auto_negotiate(int phy_addr);/* Wrappers exported to the U-Boot proper */int eth_hw_init(void){ return(dm644x_eth_hw_init());}int eth_init(bd_t * bd){ return(dm644x_eth_open());}void eth_halt(void){ dm644x_eth_close();}int eth_send(volatile void *packet, int length){ return(dm644x_eth_send_packet(packet, length));}int eth_rx(void){ return(dm644x_eth_rcv_packet());}void eth_mdio_enable(void){ dm644x_eth_mdio_enable();}/* End of wrappers */static u_int8_t dm644x_eth_mac_addr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };/* * This function must be called before emac_open() if you want to override * the default mac address. */void dm644x_eth_set_mac_addr(const u_int8_t *addr){ int i; for (i = 0; i < sizeof (dm644x_eth_mac_addr); i++) { dm644x_eth_mac_addr[i] = addr[i]; }}/* EMAC Addresses */static volatile emac_regs *adap_emac = (emac_regs *)EMAC_BASE_ADDR;static volatile ewrap_regs *adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;static volatile mdio_regs *adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;/* EMAC descriptors */static volatile emac_desc *emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);static volatile emac_desc *emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);static volatile emac_desc *emac_rx_active_head = 0;static volatile emac_desc *emac_rx_active_tail = 0;static int emac_rx_queue_active = 0;/* Receive packet buffers */static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * (EMAC_MAX_ETHERNET_PKT_SIZE + EMAC_PKT_ALIGN)];/* PHY address for a discovered PHY (0xff - not found) */static volatile u_int8_t active_phy_addr = 0xff;phy_t phy;static void dm644x_eth_mdio_enable(void){ u_int32_t clkdiv; clkdiv = (EMAC_MDIO_BUS_FREQ / EMAC_MDIO_CLOCK_FREQ) - 1; adap_mdio->CONTROL = (clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT | MDIO_CONTROL_FAULT_ENABLE; while (adap_mdio->CONTROL & MDIO_CONTROL_IDLE) {;}}/* * Tries to find an active connected PHY. Returns 1 if address if found. * If no active PHY (or more than one PHY) found returns 0. * Sets active_phy_addr variable. */static int dm644x_eth_phy_detect(void){ u_int32_t phy_act_state; int i; active_phy_addr = 0xff; if ((phy_act_state = adap_mdio->ALIVE) == 0) return(0); /* No active PHYs */ debug_emac("dm644x_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state); for (i = 0; i < 32; i++) { if (phy_act_state & (1 << i)) { if (phy_act_state & ~(1 << i)) return(0); /* More than one PHY */ else { active_phy_addr = i; return(1); } } } return(0); /* Just to make GCC happy */}/* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */int dm644x_eth_phy_read(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t *data){ int tmp; while (adap_mdio->USERACCESS0 & MDIO_USERACCESS0_GO) {;} adap_mdio->USERACCESS0 = MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_READ | ((reg_num & 0x1f) << 21) | ((phy_addr & 0x1f) << 16); /* Wait for command to complete */ while ((tmp = adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO) {;} if (tmp & MDIO_USERACCESS0_ACK) { *data = tmp & 0xffff; return(1); } *data = -1; return(0);}/* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */int dm644x_eth_phy_write(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t data){ while (adap_mdio->USERACCESS0 & MDIO_USERACCESS0_GO) {;} adap_mdio->USERACCESS0 = MDIO_USERACCESS0_GO | MDIO_USERACCESS0_WRITE_WRITE | ((reg_num & 0x1f) << 21) | ((phy_addr & 0x1f) << 16) | (data & 0xffff); /* Wait for command to complete */ while (adap_mdio->USERACCESS0 & MDIO_USERACCESS0_GO) {;} return(1);}/* PHY functions for a generic PHY */static int gen_init_phy(int phy_addr){ int ret = 1; if (gen_get_link_speed(phy_addr)) { /* Try another time */ ret = gen_get_link_speed(phy_addr); } return(ret);}static int gen_is_phy_connected(int phy_addr){ u_int16_t dummy; return(dm644x_eth_phy_read(phy_addr, PHY_PHYIDR1, &dummy));}static int gen_get_link_speed(int phy_addr){ u_int16_t tmp; if (dm644x_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) && (tmp & 0x04)) return(1); return(0);}static int gen_auto_negotiate(int phy_addr){ u_int16_t tmp; if (!dm644x_eth_phy_read(phy_addr, PHY_BMCR, &tmp)) return(0); /* Restart Auto_negotiation */ tmp |= PHY_BMCR_AUTON; dm644x_eth_phy_write(phy_addr, PHY_BMCR, tmp); /*check AutoNegotiate complete */ udelay (10000); if (!dm644x_eth_phy_read(phy_addr, PHY_BMSR, &tmp)) return(0); if (!(tmp & PHY_BMSR_AUTN_COMP)) return(0); return(gen_get_link_speed(phy_addr));}/* End of generic PHY functions */#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)static int dm644x_mii_phy_read(char *devname, unsigned char addr, unsigned char reg, unsigned short *value){ return(dm644x_eth_phy_read(addr, reg, value) ? 0 : 1);}static int dm644x_mii_phy_write(char *devname, unsigned char addr, unsigned char reg, unsigned short value){ return(dm644x_eth_phy_write(addr, reg, value) ? 0 : 1);}int dm644x_eth_miiphy_initialize(bd_t *bis){ miiphy_register(phy.name, dm644x_mii_phy_read, dm644x_mii_phy_write); return(1);}#endif/* * This function initializes the emac hardware. It does NOT initialize * EMAC modules power or pin multiplexors, that is done by board_init() * much earlier in bootup process. Returns 1 on success, 0 otherwise. */static int dm644x_eth_hw_init(void){ u_int32_t phy_id; u_int16_t tmp; int i; dm644x_eth_mdio_enable(); for (i = 0; i < 256; i++) { if (adap_mdio->ALIVE) break; udelay(10); } if (i >= 256) { printf("No ETH PHY detected!!!\n"); return(0); } /* Find if a PHY is connected and get it's address */ if (!dm644x_eth_phy_detect()) return(0); /* Get PHY ID and initialize phy_ops for a detected PHY */ if (!dm644x_eth_phy_read(active_phy_addr, PHY_PHYIDR1, &tmp)) { active_phy_addr = 0xff; return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -