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> |
Marek Vasut | fa8efac | 2020-03-15 14:42:23 +0100 | [diff] [blame] | 13 | #include <linux/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 15 | #include <linux/types.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 16 | |
Mike Frysinger | ad8e4f4 | 2009-02-23 10:29:47 -0500 | [diff] [blame] | 17 | #include "smc911x.h" |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 18 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 19 | struct chip_id { |
| 20 | u16 id; |
| 21 | char *name; |
| 22 | }; |
| 23 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 24 | struct smc911x_priv { |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 25 | #ifndef CONFIG_DM_ETH |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 26 | struct eth_device dev; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 27 | #endif |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 28 | phys_addr_t iobase; |
| 29 | const struct chip_id *chipid; |
| 30 | unsigned char enetaddr[6]; |
| 31 | }; |
| 32 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 33 | static const struct chip_id chip_ids[] = { |
| 34 | { CHIP_89218, "LAN89218" }, |
| 35 | { CHIP_9115, "LAN9115" }, |
| 36 | { CHIP_9116, "LAN9116" }, |
| 37 | { CHIP_9117, "LAN9117" }, |
| 38 | { CHIP_9118, "LAN9118" }, |
| 39 | { CHIP_9211, "LAN9211" }, |
| 40 | { CHIP_9215, "LAN9215" }, |
| 41 | { CHIP_9216, "LAN9216" }, |
| 42 | { CHIP_9217, "LAN9217" }, |
| 43 | { CHIP_9218, "LAN9218" }, |
| 44 | { CHIP_9220, "LAN9220" }, |
| 45 | { CHIP_9221, "LAN9221" }, |
| 46 | { 0, NULL }, |
| 47 | }; |
| 48 | |
| 49 | #define DRIVERNAME "smc911x" |
| 50 | |
| 51 | #if defined (CONFIG_SMC911X_32_BIT) && \ |
| 52 | defined (CONFIG_SMC911X_16_BIT) |
| 53 | #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \ |
| 54 | CONFIG_SMC911X_16_BIT shall be set" |
| 55 | #endif |
| 56 | |
| 57 | #if defined (CONFIG_SMC911X_32_BIT) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 58 | static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 59 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 60 | return readl(priv->iobase + offset); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 61 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 62 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 63 | static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 64 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 65 | writel(val, priv->iobase + offset); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 66 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 67 | #elif defined (CONFIG_SMC911X_16_BIT) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 68 | static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 69 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 70 | return (readw(priv->iobase + offset) & 0xffff) | |
| 71 | (readw(priv->iobase + offset + 2) << 16); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 72 | } |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 73 | static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 74 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 75 | writew(val & 0xffff, priv->iobase + offset); |
| 76 | writew(val >> 16, priv->iobase + offset + 2); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 77 | } |
| 78 | #else |
| 79 | #error "SMC911X: undefined bus width" |
| 80 | #endif /* CONFIG_SMC911X_16_BIT */ |
| 81 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 82 | static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 83 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 84 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 85 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 86 | smc911x_reg_write(priv, MAC_CSR_CMD, |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 87 | MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 88 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 89 | ; |
| 90 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 91 | return smc911x_reg_read(priv, MAC_CSR_DATA); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 94 | static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 95 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 96 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 97 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 98 | smc911x_reg_write(priv, MAC_CSR_DATA, data); |
| 99 | smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg); |
| 100 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 101 | ; |
| 102 | } |
| 103 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 104 | static int smc911x_detect_chip(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 105 | { |
| 106 | unsigned long val, i; |
| 107 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 108 | val = smc911x_reg_read(priv, BYTE_TEST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 109 | if (val == 0xffffffff) { |
| 110 | /* Special case -- no chip present */ |
| 111 | return -1; |
| 112 | } else if (val != 0x87654321) { |
| 113 | printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val); |
| 114 | return -1; |
| 115 | } |
| 116 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 117 | val = smc911x_reg_read(priv, ID_REV) >> 16; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 118 | for (i = 0; chip_ids[i].id != 0; i++) { |
| 119 | if (chip_ids[i].id == val) break; |
| 120 | } |
| 121 | if (!chip_ids[i].id) { |
| 122 | printf(DRIVERNAME ": Unknown chip ID %04lx\n", val); |
| 123 | return -1; |
| 124 | } |
| 125 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 126 | priv->chipid = &chip_ids[i]; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 131 | static void smc911x_reset(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 132 | { |
| 133 | int timeout; |
| 134 | |
| 135 | /* |
| 136 | * Take out of PM setting first |
| 137 | * Device is already wake up if PMT_CTRL_READY bit is set |
| 138 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 139 | if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) { |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 140 | /* Write to the bytetest will take out of powerdown */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 141 | smc911x_reg_write(priv, BYTE_TEST, 0x0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 142 | |
| 143 | timeout = 10; |
| 144 | |
| 145 | while (timeout-- && |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 146 | !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY)) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 147 | udelay(10); |
| 148 | if (timeout < 0) { |
| 149 | printf(DRIVERNAME |
| 150 | ": timeout waiting for PM restore\n"); |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* Disable interrupts */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 156 | smc911x_reg_write(priv, INT_EN, 0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 157 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 158 | smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 159 | |
| 160 | timeout = 1000; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 161 | while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 162 | udelay(10); |
| 163 | |
| 164 | if (timeout < 0) { |
| 165 | printf(DRIVERNAME ": reset timeout\n"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | /* Reset the FIFO level and flow control settings */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 170 | smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN); |
| 171 | smc911x_reg_write(priv, AFC_CFG, 0x0050287F); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 172 | |
| 173 | /* Set to LED outputs */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 174 | smc911x_reg_write(priv, GPIO_CFG, 0x70070000); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 177 | static void smc911x_handle_mac_address(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 178 | { |
| 179 | unsigned long addrh, addrl; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 180 | unsigned char *m = priv->enetaddr; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 181 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 182 | addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24); |
| 183 | addrh = m[4] | (m[5] << 8); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 184 | smc911x_set_mac_csr(priv, ADDRL, addrl); |
| 185 | smc911x_set_mac_csr(priv, ADDRH, addrh); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 186 | |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 187 | printf(DRIVERNAME ": MAC %pM\n", m); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 190 | static bool smc911x_read_mac_address(struct smc911x_priv *priv) |
| 191 | { |
| 192 | u32 addrh, addrl; |
| 193 | |
| 194 | /* address is obtained from optional eeprom */ |
| 195 | addrh = smc911x_get_mac_csr(priv, ADDRH); |
| 196 | addrl = smc911x_get_mac_csr(priv, ADDRL); |
| 197 | if (addrl == 0xffffffff && addrh == 0x0000ffff) |
| 198 | return false; |
| 199 | |
| 200 | priv->enetaddr[0] = addrl; |
| 201 | priv->enetaddr[1] = addrl >> 8; |
| 202 | priv->enetaddr[2] = addrl >> 16; |
| 203 | priv->enetaddr[3] = addrl >> 24; |
| 204 | priv->enetaddr[4] = addrh; |
| 205 | priv->enetaddr[5] = addrh >> 8; |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 210 | static int smc911x_eth_phy_read(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 211 | u8 phy, u8 reg, u16 *val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 212 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 213 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 214 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 215 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 216 | smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 217 | MII_ACC_MII_BUSY); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 218 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 219 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 220 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 221 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 222 | *val = smc911x_get_mac_csr(priv, MII_DATA); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 227 | static int smc911x_eth_phy_write(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 228 | u8 phy, u8 reg, u16 val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 229 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 230 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 231 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 232 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 233 | smc911x_set_mac_csr(priv, MII_DATA, val); |
| 234 | smc911x_set_mac_csr(priv, MII_ACC, |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 235 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 236 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 237 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 238 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 242 | static int smc911x_phy_reset(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 243 | { |
| 244 | u32 reg; |
| 245 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 246 | reg = smc911x_reg_read(priv, PMT_CTRL); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 247 | reg &= ~0xfffff030; |
| 248 | reg |= PMT_CTRL_PHY_RST; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 249 | smc911x_reg_write(priv, PMT_CTRL, reg); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 250 | |
| 251 | mdelay(100); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 256 | static void smc911x_phy_configure(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 257 | { |
| 258 | int timeout; |
| 259 | u16 status; |
| 260 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 261 | smc911x_phy_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 262 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 263 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 264 | mdelay(1); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 265 | smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1); |
| 266 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 267 | BMCR_ANRESTART); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 268 | |
| 269 | timeout = 5000; |
| 270 | do { |
| 271 | mdelay(1); |
| 272 | if ((timeout--) == 0) |
| 273 | goto err_out; |
| 274 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 275 | if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 276 | goto err_out; |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 277 | } while (!(status & BMSR_LSTATUS)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 278 | |
| 279 | printf(DRIVERNAME ": phy initialized\n"); |
| 280 | |
| 281 | return; |
| 282 | |
| 283 | err_out: |
| 284 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 285 | } |
| 286 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 287 | static void smc911x_enable(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 288 | { |
| 289 | /* Enable TX */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 290 | smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 291 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 292 | smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 293 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 294 | smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 295 | |
| 296 | /* no padding to start of packets */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 297 | smc911x_reg_write(priv, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 298 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 299 | smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 300 | MAC_CR_HBDIS); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 303 | static int smc911x_init_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 304 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 305 | const struct chip_id *id = priv->chipid; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 306 | |
Wolfgang Denk | d61fbcc | 2009-10-28 00:49:47 +0100 | [diff] [blame] | 307 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 308 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 309 | smc911x_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 310 | |
| 311 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 312 | smc911x_phy_configure(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 313 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 314 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 315 | |
| 316 | /* Turn on Tx + Rx */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 317 | smc911x_enable(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 318 | |
| 319 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 322 | static int smc911x_send_common(struct smc911x_priv *priv, |
| 323 | void *packet, int length) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 324 | { |
| 325 | u32 *data = (u32*)packet; |
| 326 | u32 tmplen; |
| 327 | u32 status; |
| 328 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 329 | smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 330 | TX_CMD_A_INT_LAST_SEG | length); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 331 | smc911x_reg_write(priv, TX_DATA_FIFO, length); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 332 | |
| 333 | tmplen = (length + 3) / 4; |
| 334 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 335 | while (tmplen--) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 336 | smc911x_reg_write(priv, TX_DATA_FIFO, *data++); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 337 | |
| 338 | /* wait for transmission */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 339 | while (!((smc911x_reg_read(priv, TX_FIFO_INF) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 340 | TX_FIFO_INF_TSUSED) >> 16)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 341 | |
| 342 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 343 | * full duplex operation |
| 344 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 345 | status = smc911x_reg_read(priv, TX_STATUS_FIFO) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 346 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 347 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 348 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 349 | if (!status) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 350 | return 0; |
| 351 | |
| 352 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 353 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 354 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 355 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 356 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 357 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 358 | |
| 359 | return -1; |
| 360 | } |
| 361 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 362 | static void smc911x_halt_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 363 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 364 | smc911x_reset(priv); |
| 365 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 368 | static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 369 | { |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 370 | u32 pktlen, tmplen; |
| 371 | u32 status; |
| 372 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 373 | status = smc911x_reg_read(priv, RX_FIFO_INF); |
| 374 | if (!(status & RX_FIFO_INF_RXSUSED)) |
| 375 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 376 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 377 | status = smc911x_reg_read(priv, RX_STATUS_FIFO); |
| 378 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 379 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 380 | smc911x_reg_write(priv, RX_CFG, 0); |
| 381 | |
| 382 | tmplen = (pktlen + 3) / 4; |
| 383 | while (tmplen--) |
| 384 | *data++ = smc911x_reg_read(priv, RX_DATA_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 385 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 386 | if (status & RX_STS_ES) { |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 387 | printf(DRIVERNAME |
| 388 | ": dropped bad packet. Status: 0x%08x\n", |
| 389 | status); |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 390 | return 0; |
| 391 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 392 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 393 | return pktlen; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 396 | #ifndef CONFIG_DM_ETH |
| 397 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 398 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 399 | /* wrapper for smc911x_eth_phy_read */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 400 | static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad, |
| 401 | int reg) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 402 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 403 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 404 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 405 | u16 val = 0; |
| 406 | int ret; |
| 407 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 408 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 409 | return -ENODEV; |
| 410 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 411 | ret = smc911x_eth_phy_read(priv, phy, reg, &val); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 412 | if (ret < 0) |
| 413 | return ret; |
| 414 | |
| 415 | return val; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 416 | } |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 417 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 418 | /* wrapper for smc911x_eth_phy_write */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 419 | static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad, |
| 420 | int reg, u16 val) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 421 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 422 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 423 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 424 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 425 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 426 | return -ENODEV; |
| 427 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 428 | return smc911x_eth_phy_write(priv, phy, reg, val); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 429 | } |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 430 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 431 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 432 | { |
| 433 | struct mii_dev *mdiodev = mdio_alloc(); |
| 434 | int ret; |
| 435 | |
| 436 | if (!mdiodev) |
| 437 | return -ENOMEM; |
| 438 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 439 | strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 440 | mdiodev->read = smc911x_miiphy_read; |
| 441 | mdiodev->write = smc911x_miiphy_write; |
| 442 | |
| 443 | ret = mdio_register(mdiodev); |
| 444 | if (ret < 0) { |
| 445 | mdio_free(mdiodev); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | #else |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 452 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 453 | { |
| 454 | return 0; |
| 455 | } |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 456 | #endif |
| 457 | |
Masahiro Yamada | 940a922 | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 458 | static int smc911x_init(struct eth_device *dev, struct bd_info *bd) |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 459 | { |
| 460 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 461 | |
| 462 | return smc911x_init_common(priv); |
| 463 | } |
| 464 | |
| 465 | static void smc911x_halt(struct eth_device *dev) |
| 466 | { |
| 467 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 468 | |
| 469 | smc911x_halt_common(priv); |
| 470 | } |
| 471 | |
| 472 | static int smc911x_send(struct eth_device *dev, void *packet, int length) |
| 473 | { |
| 474 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 475 | |
| 476 | return smc911x_send_common(priv, packet, length); |
| 477 | } |
| 478 | |
| 479 | static int smc911x_recv(struct eth_device *dev) |
| 480 | { |
| 481 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 482 | u32 *data = (u32 *)net_rx_packets[0]; |
| 483 | int ret; |
| 484 | |
| 485 | ret = smc911x_recv_common(priv, data); |
| 486 | if (ret) |
| 487 | net_process_received_packet(net_rx_packets[0], ret); |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 492 | int smc911x_initialize(u8 dev_num, int base_addr) |
| 493 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 494 | struct smc911x_priv *priv; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 495 | int ret; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 496 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 497 | priv = calloc(1, sizeof(*priv)); |
| 498 | if (!priv) |
Marek Vasut | cf61359 | 2020-03-15 15:14:18 +0100 | [diff] [blame] | 499 | return -ENOMEM; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 500 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 501 | priv->iobase = base_addr; |
| 502 | priv->dev.iobase = base_addr; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 503 | |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 504 | /* Try to detect chip. Will fail if not present. */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 505 | ret = smc911x_detect_chip(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 506 | if (ret) { |
| 507 | ret = 0; /* Card not detected is not an error */ |
| 508 | goto err_detect; |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 511 | if (smc911x_read_mac_address(priv)) |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 512 | memcpy(priv->dev.enetaddr, priv->enetaddr, 6); |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 513 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 514 | priv->dev.init = smc911x_init; |
| 515 | priv->dev.halt = smc911x_halt; |
| 516 | priv->dev.send = smc911x_send; |
| 517 | priv->dev.recv = smc911x_recv; |
| 518 | sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 519 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 520 | eth_register(&priv->dev); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 521 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 522 | ret = smc911x_initialize_mii(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 523 | if (ret) |
| 524 | goto err_mii; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 525 | |
Mike Rapoport | 907975a | 2009-11-12 15:35:08 +0200 | [diff] [blame] | 526 | return 1; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 527 | |
| 528 | err_mii: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 529 | eth_unregister(&priv->dev); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 530 | err_detect: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 531 | free(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 532 | return ret; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 533 | } |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 534 | |
| 535 | #else /* ifdef CONFIG_DM_ETH */ |
| 536 | |
| 537 | static int smc911x_start(struct udevice *dev) |
| 538 | { |
| 539 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 540 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 541 | |
| 542 | memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr)); |
| 543 | |
| 544 | return smc911x_init_common(priv); |
| 545 | } |
| 546 | |
| 547 | static void smc911x_stop(struct udevice *dev) |
| 548 | { |
| 549 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 550 | |
| 551 | smc911x_halt_common(priv); |
| 552 | } |
| 553 | |
| 554 | static int smc911x_send(struct udevice *dev, void *packet, int length) |
| 555 | { |
| 556 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 557 | int ret; |
| 558 | |
| 559 | ret = smc911x_send_common(priv, packet, length); |
| 560 | |
| 561 | return ret ? 0 : -ETIMEDOUT; |
| 562 | } |
| 563 | |
| 564 | static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp) |
| 565 | { |
| 566 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 567 | u32 *data = (u32 *)net_rx_packets[0]; |
| 568 | int ret; |
| 569 | |
| 570 | ret = smc911x_recv_common(priv, data); |
| 571 | if (ret) |
| 572 | *packetp = (void *)data; |
| 573 | |
| 574 | return ret ? ret : -EAGAIN; |
| 575 | } |
| 576 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 577 | static int smc911x_read_rom_hwaddr(struct udevice *dev) |
| 578 | { |
| 579 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 580 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 581 | |
| 582 | if (!smc911x_read_mac_address(priv)) |
| 583 | return -ENODEV; |
| 584 | |
| 585 | memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr)); |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 590 | static int smc911x_bind(struct udevice *dev) |
| 591 | { |
| 592 | return device_set_name(dev, dev->name); |
| 593 | } |
| 594 | |
| 595 | static int smc911x_probe(struct udevice *dev) |
| 596 | { |
| 597 | struct smc911x_priv *priv = dev_get_priv(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 598 | int ret; |
| 599 | |
| 600 | /* Try to detect chip. Will fail if not present. */ |
| 601 | ret = smc911x_detect_chip(priv); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 605 | smc911x_read_rom_hwaddr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int smc911x_ofdata_to_platdata(struct udevice *dev) |
| 611 | { |
| 612 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 613 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 614 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 615 | pdata->iobase = dev_read_addr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 616 | priv->iobase = pdata->iobase; |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static const struct eth_ops smc911x_ops = { |
| 622 | .start = smc911x_start, |
| 623 | .send = smc911x_send, |
| 624 | .recv = smc911x_recv, |
| 625 | .stop = smc911x_stop, |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 626 | .read_rom_hwaddr = smc911x_read_rom_hwaddr, |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | static const struct udevice_id smc911x_ids[] = { |
| 630 | { .compatible = "smsc,lan9115" }, |
| 631 | { } |
| 632 | }; |
| 633 | |
| 634 | U_BOOT_DRIVER(smc911x) = { |
| 635 | .name = "eth_smc911x", |
| 636 | .id = UCLASS_ETH, |
| 637 | .of_match = smc911x_ids, |
| 638 | .bind = smc911x_bind, |
| 639 | .ofdata_to_platdata = smc911x_ofdata_to_platdata, |
| 640 | .probe = smc911x_probe, |
| 641 | .ops = &smc911x_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 642 | .priv_auto = sizeof(struct smc911x_priv), |
| 643 | .platdata_auto = sizeof(struct eth_pdata), |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 644 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 645 | }; |
| 646 | #endif |