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