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