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 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 190 | static int smc911x_eth_phy_read(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 191 | u8 phy, u8 reg, u16 *val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 192 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 193 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 194 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 195 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 196 | smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 197 | MII_ACC_MII_BUSY); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 198 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 199 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 200 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 201 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 202 | *val = smc911x_get_mac_csr(priv, MII_DATA); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 207 | static int smc911x_eth_phy_write(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 208 | u8 phy, u8 reg, u16 val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 209 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 210 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 211 | ; |
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 | smc911x_set_mac_csr(priv, MII_DATA, val); |
| 214 | smc911x_set_mac_csr(priv, MII_ACC, |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 215 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 216 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 217 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 218 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 222 | static int smc911x_phy_reset(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 223 | { |
| 224 | u32 reg; |
| 225 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 226 | reg = smc911x_reg_read(priv, PMT_CTRL); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 227 | reg &= ~0xfffff030; |
| 228 | reg |= PMT_CTRL_PHY_RST; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 229 | smc911x_reg_write(priv, PMT_CTRL, reg); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 230 | |
| 231 | mdelay(100); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 236 | static void smc911x_phy_configure(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 237 | { |
| 238 | int timeout; |
| 239 | u16 status; |
| 240 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 241 | smc911x_phy_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 242 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 243 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 244 | mdelay(1); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 245 | smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1); |
| 246 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 247 | BMCR_ANRESTART); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 248 | |
| 249 | timeout = 5000; |
| 250 | do { |
| 251 | mdelay(1); |
| 252 | if ((timeout--) == 0) |
| 253 | goto err_out; |
| 254 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 255 | if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 256 | goto err_out; |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 257 | } while (!(status & BMSR_LSTATUS)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 258 | |
| 259 | printf(DRIVERNAME ": phy initialized\n"); |
| 260 | |
| 261 | return; |
| 262 | |
| 263 | err_out: |
| 264 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 265 | } |
| 266 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 267 | static void smc911x_enable(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 268 | { |
| 269 | /* Enable TX */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 270 | smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 271 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 272 | smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 273 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 274 | smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 275 | |
| 276 | /* no padding to start of packets */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 277 | smc911x_reg_write(priv, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 278 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 279 | 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] | 280 | MAC_CR_HBDIS); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 283 | static int smc911x_init_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 284 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 285 | const struct chip_id *id = priv->chipid; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 286 | |
Wolfgang Denk | d61fbcc | 2009-10-28 00:49:47 +0100 | [diff] [blame] | 287 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 288 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 289 | smc911x_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 290 | |
| 291 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 292 | smc911x_phy_configure(priv); |
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_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 295 | |
| 296 | /* Turn on Tx + Rx */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 297 | smc911x_enable(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 298 | |
| 299 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 302 | static int smc911x_send_common(struct smc911x_priv *priv, |
| 303 | void *packet, int length) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 304 | { |
| 305 | u32 *data = (u32*)packet; |
| 306 | u32 tmplen; |
| 307 | u32 status; |
| 308 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 309 | 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] | 310 | TX_CMD_A_INT_LAST_SEG | length); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 311 | smc911x_reg_write(priv, TX_DATA_FIFO, length); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 312 | |
| 313 | tmplen = (length + 3) / 4; |
| 314 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 315 | while (tmplen--) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 316 | smc911x_reg_write(priv, TX_DATA_FIFO, *data++); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 317 | |
| 318 | /* wait for transmission */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 319 | while (!((smc911x_reg_read(priv, TX_FIFO_INF) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 320 | TX_FIFO_INF_TSUSED) >> 16)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 321 | |
| 322 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 323 | * full duplex operation |
| 324 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 325 | status = smc911x_reg_read(priv, TX_STATUS_FIFO) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 326 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 327 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 328 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 329 | if (!status) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 330 | return 0; |
| 331 | |
| 332 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 333 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 334 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 335 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 336 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 337 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 338 | |
| 339 | return -1; |
| 340 | } |
| 341 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 342 | static void smc911x_halt_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 343 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 344 | smc911x_reset(priv); |
| 345 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 348 | static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 349 | { |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 350 | u32 pktlen, tmplen; |
| 351 | u32 status; |
| 352 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 353 | status = smc911x_reg_read(priv, RX_FIFO_INF); |
| 354 | if (!(status & RX_FIFO_INF_RXSUSED)) |
| 355 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 356 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 357 | status = smc911x_reg_read(priv, RX_STATUS_FIFO); |
| 358 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 359 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 360 | smc911x_reg_write(priv, RX_CFG, 0); |
| 361 | |
| 362 | tmplen = (pktlen + 3) / 4; |
| 363 | while (tmplen--) |
| 364 | *data++ = smc911x_reg_read(priv, RX_DATA_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 365 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 366 | if (status & RX_STS_ES) { |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 367 | printf(DRIVERNAME |
| 368 | ": dropped bad packet. Status: 0x%08x\n", |
| 369 | status); |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 370 | return 0; |
| 371 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 372 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 373 | return pktlen; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 376 | #ifndef CONFIG_DM_ETH |
| 377 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 378 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 379 | /* wrapper for smc911x_eth_phy_read */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 380 | static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad, |
| 381 | int reg) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 382 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 383 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 384 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 385 | u16 val = 0; |
| 386 | int ret; |
| 387 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 388 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 389 | return -ENODEV; |
| 390 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 391 | ret = smc911x_eth_phy_read(priv, phy, reg, &val); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 392 | if (ret < 0) |
| 393 | return ret; |
| 394 | |
| 395 | return val; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 396 | } |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 397 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 398 | /* wrapper for smc911x_eth_phy_write */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 399 | static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad, |
| 400 | int reg, u16 val) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 401 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 402 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 403 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 404 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 405 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 406 | return -ENODEV; |
| 407 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 408 | return smc911x_eth_phy_write(priv, phy, reg, val); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 409 | } |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 410 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 411 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 412 | { |
| 413 | struct mii_dev *mdiodev = mdio_alloc(); |
| 414 | int ret; |
| 415 | |
| 416 | if (!mdiodev) |
| 417 | return -ENOMEM; |
| 418 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 419 | strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 420 | mdiodev->read = smc911x_miiphy_read; |
| 421 | mdiodev->write = smc911x_miiphy_write; |
| 422 | |
| 423 | ret = mdio_register(mdiodev); |
| 424 | if (ret < 0) { |
| 425 | mdio_free(mdiodev); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | #else |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 432 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 433 | { |
| 434 | return 0; |
| 435 | } |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 436 | #endif |
| 437 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 438 | static int smc911x_init(struct eth_device *dev, bd_t *bd) |
| 439 | { |
| 440 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 441 | |
| 442 | return smc911x_init_common(priv); |
| 443 | } |
| 444 | |
| 445 | static void smc911x_halt(struct eth_device *dev) |
| 446 | { |
| 447 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 448 | |
| 449 | smc911x_halt_common(priv); |
| 450 | } |
| 451 | |
| 452 | static int smc911x_send(struct eth_device *dev, void *packet, int length) |
| 453 | { |
| 454 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 455 | |
| 456 | return smc911x_send_common(priv, packet, length); |
| 457 | } |
| 458 | |
| 459 | static int smc911x_recv(struct eth_device *dev) |
| 460 | { |
| 461 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 462 | u32 *data = (u32 *)net_rx_packets[0]; |
| 463 | int ret; |
| 464 | |
| 465 | ret = smc911x_recv_common(priv, data); |
| 466 | if (ret) |
| 467 | net_process_received_packet(net_rx_packets[0], ret); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 472 | int smc911x_initialize(u8 dev_num, int base_addr) |
| 473 | { |
| 474 | unsigned long addrl, addrh; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 475 | struct smc911x_priv *priv; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 476 | int ret; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 477 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 478 | priv = calloc(1, sizeof(*priv)); |
| 479 | if (!priv) |
Marek Vasut | cf61359 | 2020-03-15 15:14:18 +0100 | [diff] [blame] | 480 | return -ENOMEM; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 481 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 482 | priv->iobase = base_addr; |
| 483 | priv->dev.iobase = base_addr; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 484 | |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 485 | /* Try to detect chip. Will fail if not present. */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 486 | ret = smc911x_detect_chip(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 487 | if (ret) { |
| 488 | ret = 0; /* Card not detected is not an error */ |
| 489 | goto err_detect; |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 492 | addrh = smc911x_get_mac_csr(priv, ADDRH); |
| 493 | addrl = smc911x_get_mac_csr(priv, ADDRL); |
Seunghyeon Rhee | a8572f1 | 2009-11-02 00:00:00 -0800 | [diff] [blame] | 494 | if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) { |
| 495 | /* address is obtained from optional eeprom */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 496 | priv->enetaddr[0] = addrl; |
| 497 | priv->enetaddr[1] = addrl >> 8; |
| 498 | priv->enetaddr[2] = addrl >> 16; |
| 499 | priv->enetaddr[3] = addrl >> 24; |
| 500 | priv->enetaddr[4] = addrh; |
| 501 | priv->enetaddr[5] = addrh >> 8; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 502 | memcpy(priv->dev.enetaddr, priv->enetaddr, 6); |
Seunghyeon Rhee | a8572f1 | 2009-11-02 00:00:00 -0800 | [diff] [blame] | 503 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 504 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 505 | priv->dev.init = smc911x_init; |
| 506 | priv->dev.halt = smc911x_halt; |
| 507 | priv->dev.send = smc911x_send; |
| 508 | priv->dev.recv = smc911x_recv; |
| 509 | sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 510 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 511 | eth_register(&priv->dev); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 512 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 513 | ret = smc911x_initialize_mii(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 514 | if (ret) |
| 515 | goto err_mii; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 516 | |
Mike Rapoport | 907975a | 2009-11-12 15:35:08 +0200 | [diff] [blame] | 517 | return 1; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 518 | |
| 519 | err_mii: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 520 | eth_unregister(&priv->dev); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 521 | err_detect: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 522 | free(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 523 | return ret; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 524 | } |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 525 | |
| 526 | #else /* ifdef CONFIG_DM_ETH */ |
| 527 | |
| 528 | static int smc911x_start(struct udevice *dev) |
| 529 | { |
| 530 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 531 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 532 | |
| 533 | memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr)); |
| 534 | |
| 535 | return smc911x_init_common(priv); |
| 536 | } |
| 537 | |
| 538 | static void smc911x_stop(struct udevice *dev) |
| 539 | { |
| 540 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 541 | |
| 542 | smc911x_halt_common(priv); |
| 543 | } |
| 544 | |
| 545 | static int smc911x_send(struct udevice *dev, void *packet, int length) |
| 546 | { |
| 547 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 548 | int ret; |
| 549 | |
| 550 | ret = smc911x_send_common(priv, packet, length); |
| 551 | |
| 552 | return ret ? 0 : -ETIMEDOUT; |
| 553 | } |
| 554 | |
| 555 | static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp) |
| 556 | { |
| 557 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 558 | u32 *data = (u32 *)net_rx_packets[0]; |
| 559 | int ret; |
| 560 | |
| 561 | ret = smc911x_recv_common(priv, data); |
| 562 | if (ret) |
| 563 | *packetp = (void *)data; |
| 564 | |
| 565 | return ret ? ret : -EAGAIN; |
| 566 | } |
| 567 | |
| 568 | static int smc911x_bind(struct udevice *dev) |
| 569 | { |
| 570 | return device_set_name(dev, dev->name); |
| 571 | } |
| 572 | |
| 573 | static int smc911x_probe(struct udevice *dev) |
| 574 | { |
| 575 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 576 | unsigned long addrh, addrl; |
| 577 | int ret; |
| 578 | |
| 579 | /* Try to detect chip. Will fail if not present. */ |
| 580 | ret = smc911x_detect_chip(priv); |
| 581 | if (ret) |
| 582 | return ret; |
| 583 | |
| 584 | addrh = smc911x_get_mac_csr(priv, ADDRH); |
| 585 | addrl = smc911x_get_mac_csr(priv, ADDRL); |
| 586 | if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) { |
| 587 | /* address is obtained from optional eeprom */ |
| 588 | priv->enetaddr[0] = addrl; |
| 589 | priv->enetaddr[1] = addrl >> 8; |
| 590 | priv->enetaddr[2] = addrl >> 16; |
| 591 | priv->enetaddr[3] = addrl >> 24; |
| 592 | priv->enetaddr[4] = addrh; |
| 593 | priv->enetaddr[5] = addrh >> 8; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int smc911x_ofdata_to_platdata(struct udevice *dev) |
| 600 | { |
| 601 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 602 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 603 | |
| 604 | pdata->iobase = devfdt_get_addr(dev); |
| 605 | priv->iobase = pdata->iobase; |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static const struct eth_ops smc911x_ops = { |
| 611 | .start = smc911x_start, |
| 612 | .send = smc911x_send, |
| 613 | .recv = smc911x_recv, |
| 614 | .stop = smc911x_stop, |
| 615 | }; |
| 616 | |
| 617 | static const struct udevice_id smc911x_ids[] = { |
| 618 | { .compatible = "smsc,lan9115" }, |
| 619 | { } |
| 620 | }; |
| 621 | |
| 622 | U_BOOT_DRIVER(smc911x) = { |
| 623 | .name = "eth_smc911x", |
| 624 | .id = UCLASS_ETH, |
| 625 | .of_match = smc911x_ids, |
| 626 | .bind = smc911x_bind, |
| 627 | .ofdata_to_platdata = smc911x_ofdata_to_platdata, |
| 628 | .probe = smc911x_probe, |
| 629 | .ops = &smc911x_ops, |
| 630 | .priv_auto_alloc_size = sizeof(struct smc911x_priv), |
| 631 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 632 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 633 | }; |
| 634 | #endif |