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