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