Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SMSC LAN9[12]1[567] Network driver |
| 3 | * |
Stelian Pop | 5049752 | 2008-05-08 22:52:09 +0200 | [diff] [blame] | 4 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 26 | #include <command.h> |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 27 | #include <malloc.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 28 | #include <net.h> |
| 29 | #include <miiphy.h> |
| 30 | |
Mike Frysinger | ad8e4f4 | 2009-02-23 10:29:47 -0500 | [diff] [blame] | 31 | #include "smc911x.h" |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 32 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 33 | u32 pkt_data_pull(struct eth_device *dev, u32 addr) \ |
Stefan Roese | 62f3ed6 | 2008-11-12 13:31:02 +0100 | [diff] [blame] | 34 | __attribute__ ((weak, alias ("smc911x_reg_read"))); |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 35 | void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \ |
Stefan Roese | 62f3ed6 | 2008-11-12 13:31:02 +0100 | [diff] [blame] | 36 | __attribute__ ((weak, alias ("smc911x_reg_write"))); |
Nobuhiro Iwamatsu | 0af6a43 | 2008-08-28 13:40:44 +0900 | [diff] [blame] | 37 | |
Mike Rapoport | 0b3cc38 | 2009-11-11 10:03:09 +0200 | [diff] [blame] | 38 | static void smc911x_handle_mac_address(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 39 | { |
| 40 | unsigned long addrh, addrl; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 41 | uchar *m = dev->enetaddr; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 42 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 43 | addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24); |
| 44 | addrh = m[4] | (m[5] << 8); |
| 45 | smc911x_set_mac_csr(dev, ADDRL, addrl); |
| 46 | smc911x_set_mac_csr(dev, ADDRH, addrh); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 47 | |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 48 | printf(DRIVERNAME ": MAC %pM\n", m); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 51 | static int smc911x_eth_phy_read(struct eth_device *dev, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 52 | u8 phy, u8 reg, u16 *val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 53 | { |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 54 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 55 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 56 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 57 | smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 | |
| 58 | MII_ACC_MII_BUSY); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 59 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 60 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 61 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 62 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 63 | *val = smc911x_get_mac_csr(dev, MII_DATA); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 68 | static int smc911x_eth_phy_write(struct eth_device *dev, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 69 | u8 phy, u8 reg, u16 val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 70 | { |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 71 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 72 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 73 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 74 | smc911x_set_mac_csr(dev, MII_DATA, val); |
| 75 | smc911x_set_mac_csr(dev, MII_ACC, |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 76 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 77 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 78 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 79 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 83 | static int smc911x_phy_reset(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 84 | { |
| 85 | u32 reg; |
| 86 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 87 | reg = smc911x_reg_read(dev, PMT_CTRL); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 88 | reg &= ~0xfffff030; |
| 89 | reg |= PMT_CTRL_PHY_RST; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 90 | smc911x_reg_write(dev, PMT_CTRL, reg); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 91 | |
| 92 | mdelay(100); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 97 | static void smc911x_phy_configure(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 98 | { |
| 99 | int timeout; |
| 100 | u16 status; |
| 101 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 102 | smc911x_phy_reset(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 103 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 104 | smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 105 | mdelay(1); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 106 | smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1); |
| 107 | smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 108 | BMCR_ANRESTART); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 109 | |
| 110 | timeout = 5000; |
| 111 | do { |
| 112 | mdelay(1); |
| 113 | if ((timeout--) == 0) |
| 114 | goto err_out; |
| 115 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 116 | if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 117 | goto err_out; |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 118 | } while (!(status & BMSR_LSTATUS)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 119 | |
| 120 | printf(DRIVERNAME ": phy initialized\n"); |
| 121 | |
| 122 | return; |
| 123 | |
| 124 | err_out: |
| 125 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 126 | } |
| 127 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 128 | static void smc911x_enable(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 129 | { |
| 130 | /* Enable TX */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 131 | smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 132 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 133 | smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 134 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 135 | smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 136 | |
| 137 | /* no padding to start of packets */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 138 | smc911x_reg_write(dev, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 139 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 140 | smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | |
| 141 | MAC_CR_HBDIS); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 142 | |
| 143 | } |
| 144 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 145 | static int smc911x_init(struct eth_device *dev, bd_t * bd) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 146 | { |
Olof Johansson | 64aabc2 | 2009-09-29 10:21:29 -0400 | [diff] [blame] | 147 | struct chip_id *id = dev->priv; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 148 | |
Wolfgang Denk | d61fbcc | 2009-10-28 00:49:47 +0100 | [diff] [blame] | 149 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 150 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 151 | smc911x_reset(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 152 | |
| 153 | /* Configure the PHY, initialize the link state */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 154 | smc911x_phy_configure(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 155 | |
Mike Rapoport | 0b3cc38 | 2009-11-11 10:03:09 +0200 | [diff] [blame] | 156 | smc911x_handle_mac_address(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 157 | |
| 158 | /* Turn on Tx + Rx */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 159 | smc911x_enable(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 160 | |
| 161 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 164 | static int smc911x_send(struct eth_device *dev, |
| 165 | volatile void *packet, int length) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 166 | { |
| 167 | u32 *data = (u32*)packet; |
| 168 | u32 tmplen; |
| 169 | u32 status; |
| 170 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 171 | smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | |
| 172 | TX_CMD_A_INT_LAST_SEG | length); |
| 173 | smc911x_reg_write(dev, TX_DATA_FIFO, length); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 174 | |
| 175 | tmplen = (length + 3) / 4; |
| 176 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 177 | while (tmplen--) |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 178 | pkt_data_push(dev, TX_DATA_FIFO, *data++); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 179 | |
| 180 | /* wait for transmission */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 181 | while (!((smc911x_reg_read(dev, TX_FIFO_INF) & |
| 182 | TX_FIFO_INF_TSUSED) >> 16)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 183 | |
| 184 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 185 | * full duplex operation |
| 186 | */ |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 187 | status = smc911x_reg_read(dev, TX_STATUS_FIFO) & |
| 188 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 189 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 190 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 191 | if (!status) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 192 | return 0; |
| 193 | |
| 194 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 195 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 196 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 197 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 198 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 199 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 200 | |
| 201 | return -1; |
| 202 | } |
| 203 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 204 | static void smc911x_halt(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 205 | { |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 206 | smc911x_reset(dev); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 209 | static int smc911x_rx(struct eth_device *dev) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 210 | { |
| 211 | u32 *data = (u32 *)NetRxPackets[0]; |
| 212 | u32 pktlen, tmplen; |
| 213 | u32 status; |
| 214 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 215 | if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) { |
| 216 | status = smc911x_reg_read(dev, RX_STATUS_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 217 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
| 218 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 219 | smc911x_reg_write(dev, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 220 | |
Valentin Yakovenkov | 8cb0801 | 2010-04-23 09:40:23 +0400 | [diff] [blame] | 221 | tmplen = (pktlen + 3) / 4; |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 222 | while (tmplen--) |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 223 | *data++ = pkt_data_pull(dev, RX_DATA_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 224 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 225 | if (status & RX_STS_ES) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 226 | printf(DRIVERNAME |
| 227 | ": dropped bad packet. Status: 0x%08x\n", |
| 228 | status); |
| 229 | else |
| 230 | NetReceive(NetRxPackets[0], pktlen); |
| 231 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 236 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 237 | /* wrapper for smc911x_eth_phy_read */ |
Wolfgang Denk | d1f10ba | 2011-09-08 01:04:27 +0000 | [diff] [blame] | 238 | static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 239 | { |
| 240 | struct eth_device *dev = eth_get_dev_by_name(devname); |
| 241 | if (dev) |
| 242 | return smc911x_eth_phy_read(dev, phy, reg, val); |
| 243 | return -1; |
| 244 | } |
| 245 | /* wrapper for smc911x_eth_phy_write */ |
Wolfgang Denk | d1f10ba | 2011-09-08 01:04:27 +0000 | [diff] [blame] | 246 | static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 247 | { |
| 248 | struct eth_device *dev = eth_get_dev_by_name(devname); |
| 249 | if (dev) |
| 250 | return smc911x_eth_phy_write(dev, phy, reg, val); |
| 251 | return -1; |
| 252 | } |
| 253 | #endif |
| 254 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 255 | int smc911x_initialize(u8 dev_num, int base_addr) |
| 256 | { |
| 257 | unsigned long addrl, addrh; |
| 258 | struct eth_device *dev; |
| 259 | |
| 260 | dev = malloc(sizeof(*dev)); |
| 261 | if (!dev) { |
Mike Rapoport | 907975a | 2009-11-12 15:35:08 +0200 | [diff] [blame] | 262 | return -1; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 263 | } |
| 264 | memset(dev, 0, sizeof(*dev)); |
| 265 | |
| 266 | dev->iobase = base_addr; |
| 267 | |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 268 | /* Try to detect chip. Will fail if not present. */ |
| 269 | if (smc911x_detect_chip(dev)) { |
| 270 | free(dev); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 274 | addrh = smc911x_get_mac_csr(dev, ADDRH); |
| 275 | addrl = smc911x_get_mac_csr(dev, ADDRL); |
Seunghyeon Rhee | a8572f1 | 2009-11-02 00:00:00 -0800 | [diff] [blame] | 276 | if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) { |
| 277 | /* address is obtained from optional eeprom */ |
| 278 | dev->enetaddr[0] = addrl; |
| 279 | dev->enetaddr[1] = addrl >> 8; |
| 280 | dev->enetaddr[2] = addrl >> 16; |
| 281 | dev->enetaddr[3] = addrl >> 24; |
| 282 | dev->enetaddr[4] = addrh; |
| 283 | dev->enetaddr[5] = addrh >> 8; |
| 284 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 285 | |
| 286 | dev->init = smc911x_init; |
| 287 | dev->halt = smc911x_halt; |
| 288 | dev->send = smc911x_send; |
| 289 | dev->recv = smc911x_rx; |
| 290 | sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 291 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 292 | eth_register(dev); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 293 | |
| 294 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 295 | miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write); |
| 296 | #endif |
| 297 | |
Mike Rapoport | 907975a | 2009-11-12 15:35:08 +0200 | [diff] [blame] | 298 | return 1; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 299 | } |