Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 14b4c70 | 2009-09-07 09:08:02 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007-2009 Michal Simek |
| 4 | * (C) Copyright 2003 Xilinx Inc. |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 5 | * |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 6 | * Michal SIMEK <monstr@monstr.eu> |
Michal Simek | 14b4c70 | 2009-09-07 09:08:02 +0200 | [diff] [blame] | 7 | */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 8 | |
| 9 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 11 | #include <net.h> |
| 12 | #include <config.h> |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 13 | #include <dm.h> |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 14 | #include <console.h> |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 18 | #include <phy.h> |
| 19 | #include <miiphy.h> |
Michal Simek | bb8b27b | 2012-06-28 21:37:57 +0000 | [diff] [blame] | 20 | #include <fdtdec.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 22 | #include <linux/errno.h> |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Zubair Lutfullah Kakakhel | d23bf84 | 2016-07-27 12:25:07 +0100 | [diff] [blame] | 24 | #include <asm/io.h> |
T Karthik Reddy | cff1084 | 2022-05-10 13:26:10 +0200 | [diff] [blame] | 25 | #include <eth_phy.h> |
Michal Simek | bb8b27b | 2012-06-28 21:37:57 +0000 | [diff] [blame] | 26 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 28 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 29 | #define ENET_ADDR_LENGTH 6 |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 30 | #define ETH_FCS_LEN 4 /* Octets in the FCS */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 31 | |
| 32 | /* Xmit complete */ |
| 33 | #define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL |
| 34 | /* Xmit interrupt enable bit */ |
| 35 | #define XEL_TSR_XMIT_IE_MASK 0x00000008UL |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 36 | /* Program the MAC address */ |
| 37 | #define XEL_TSR_PROGRAM_MASK 0x00000002UL |
| 38 | /* define for programming the MAC address into the EMAC Lite */ |
| 39 | #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK) |
| 40 | |
| 41 | /* Transmit packet length upper byte */ |
| 42 | #define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL |
| 43 | /* Transmit packet length lower byte */ |
| 44 | #define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL |
| 45 | |
| 46 | /* Recv complete */ |
| 47 | #define XEL_RSR_RECV_DONE_MASK 0x00000001UL |
| 48 | /* Recv interrupt enable bit */ |
| 49 | #define XEL_RSR_RECV_IE_MASK 0x00000008UL |
| 50 | |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 51 | /* MDIO Address Register Bit Masks */ |
| 52 | #define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */ |
| 53 | #define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */ |
| 54 | #define XEL_MDIOADDR_PHYADR_SHIFT 5 |
| 55 | #define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */ |
| 56 | |
| 57 | /* MDIO Write Data Register Bit Masks */ |
| 58 | #define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */ |
| 59 | |
| 60 | /* MDIO Read Data Register Bit Masks */ |
| 61 | #define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */ |
| 62 | |
| 63 | /* MDIO Control Register Bit Masks */ |
| 64 | #define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */ |
| 65 | #define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */ |
| 66 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 67 | struct emaclite_regs { |
| 68 | u32 tx_ping; /* 0x0 - TX Ping buffer */ |
| 69 | u32 reserved1[504]; |
| 70 | u32 mdioaddr; /* 0x7e4 - MDIO Address Register */ |
| 71 | u32 mdiowr; /* 0x7e8 - MDIO Write Data Register */ |
| 72 | u32 mdiord;/* 0x7ec - MDIO Read Data Register */ |
| 73 | u32 mdioctrl; /* 0x7f0 - MDIO Control Register */ |
| 74 | u32 tx_ping_tplr; /* 0x7f4 - Tx packet length */ |
| 75 | u32 global_interrupt; /* 0x7f8 - Global interrupt enable */ |
| 76 | u32 tx_ping_tsr; /* 0x7fc - Tx status */ |
| 77 | u32 tx_pong; /* 0x800 - TX Pong buffer */ |
| 78 | u32 reserved2[508]; |
| 79 | u32 tx_pong_tplr; /* 0xff4 - Tx packet length */ |
| 80 | u32 reserved3; /* 0xff8 */ |
| 81 | u32 tx_pong_tsr; /* 0xffc - Tx status */ |
| 82 | u32 rx_ping; /* 0x1000 - Receive Buffer */ |
| 83 | u32 reserved4[510]; |
| 84 | u32 rx_ping_rsr; /* 0x17fc - Rx status */ |
| 85 | u32 rx_pong; /* 0x1800 - Receive Buffer */ |
| 86 | u32 reserved5[510]; |
| 87 | u32 rx_pong_rsr; /* 0x1ffc - Rx status */ |
| 88 | }; |
| 89 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 90 | struct xemaclite { |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 91 | bool use_rx_pong_buffer_next; /* Next RX buffer to read from */ |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 92 | u32 txpp; /* TX ping pong buffer */ |
| 93 | u32 rxpp; /* RX ping pong buffer */ |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 94 | int phyaddr; |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 95 | struct emaclite_regs *regs; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 96 | struct phy_device *phydev; |
| 97 | struct mii_dev *bus; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 98 | }; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 99 | |
Michal Simek | 641ade0 | 2015-12-16 10:52:39 +0100 | [diff] [blame] | 100 | static uchar etherrxbuff[PKTSIZE_ALIGN]; /* Receive buffer */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 101 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 102 | static void xemaclite_alignedread(u32 *srcptr, void *destptr, u32 bytecount) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 103 | { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 104 | u32 i; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 105 | u32 alignbuffer; |
| 106 | u32 *to32ptr; |
| 107 | u32 *from32ptr; |
| 108 | u8 *to8ptr; |
| 109 | u8 *from8ptr; |
| 110 | |
| 111 | from32ptr = (u32 *) srcptr; |
| 112 | |
| 113 | /* Word aligned buffer, no correction needed. */ |
| 114 | to32ptr = (u32 *) destptr; |
| 115 | while (bytecount > 3) { |
| 116 | *to32ptr++ = *from32ptr++; |
| 117 | bytecount -= 4; |
| 118 | } |
| 119 | to8ptr = (u8 *) to32ptr; |
| 120 | |
| 121 | alignbuffer = *from32ptr++; |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 122 | from8ptr = (u8 *) &alignbuffer; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 123 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 124 | for (i = 0; i < bytecount; i++) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 125 | *to8ptr++ = *from8ptr++; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 128 | static void xemaclite_alignedwrite(void *srcptr, u32 *destptr, u32 bytecount) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 129 | { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 130 | u32 i; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 131 | u32 alignbuffer; |
| 132 | u32 *to32ptr = (u32 *) destptr; |
| 133 | u32 *from32ptr; |
| 134 | u8 *to8ptr; |
| 135 | u8 *from8ptr; |
| 136 | |
| 137 | from32ptr = (u32 *) srcptr; |
| 138 | while (bytecount > 3) { |
| 139 | |
| 140 | *to32ptr++ = *from32ptr++; |
| 141 | bytecount -= 4; |
| 142 | } |
| 143 | |
| 144 | alignbuffer = 0; |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 145 | to8ptr = (u8 *) &alignbuffer; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 146 | from8ptr = (u8 *) from32ptr; |
| 147 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 148 | for (i = 0; i < bytecount; i++) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 149 | *to8ptr++ = *from8ptr++; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 150 | |
| 151 | *to32ptr++ = alignbuffer; |
| 152 | } |
| 153 | |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 154 | static int wait_for_bit(const char *func, u32 *reg, const u32 mask, |
| 155 | bool set, unsigned int timeout) |
| 156 | { |
| 157 | u32 val; |
| 158 | unsigned long start = get_timer(0); |
| 159 | |
| 160 | while (1) { |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 161 | val = __raw_readl(reg); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 162 | |
| 163 | if (!set) |
| 164 | val = ~val; |
| 165 | |
| 166 | if ((val & mask) == mask) |
| 167 | return 0; |
| 168 | |
| 169 | if (get_timer(start) > timeout) |
| 170 | break; |
| 171 | |
| 172 | if (ctrlc()) { |
| 173 | puts("Abort\n"); |
| 174 | return -EINTR; |
| 175 | } |
| 176 | |
| 177 | udelay(1); |
| 178 | } |
| 179 | |
| 180 | debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", |
| 181 | func, reg, mask, set); |
| 182 | |
| 183 | return -ETIMEDOUT; |
| 184 | } |
| 185 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 186 | static int mdio_wait(struct emaclite_regs *regs) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 187 | { |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 188 | return wait_for_bit(__func__, ®s->mdioctrl, |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 189 | XEL_MDIOCTRL_MDIOSTS_MASK, false, 2000); |
| 190 | } |
| 191 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 192 | static u32 phyread(struct xemaclite *emaclite, u32 phyaddress, u32 registernum, |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 193 | u16 *data) |
| 194 | { |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 195 | struct emaclite_regs *regs = emaclite->regs; |
| 196 | |
| 197 | if (mdio_wait(regs)) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 198 | return 1; |
| 199 | |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 200 | u32 ctrl_reg = __raw_readl(®s->mdioctrl); |
| 201 | __raw_writel(XEL_MDIOADDR_OP_MASK |
| 202 | | ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) |
| 203 | | registernum), ®s->mdioaddr); |
| 204 | __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, ®s->mdioctrl); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 205 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 206 | if (mdio_wait(regs)) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | |
| 209 | /* Read data */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 210 | *data = __raw_readl(®s->mdiord); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 214 | static u32 phywrite(struct xemaclite *emaclite, u32 phyaddress, u32 registernum, |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 215 | u16 data) |
| 216 | { |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 217 | struct emaclite_regs *regs = emaclite->regs; |
| 218 | |
| 219 | if (mdio_wait(regs)) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 220 | return 1; |
| 221 | |
| 222 | /* |
| 223 | * Write the PHY address, register number and clear the OP bit in the |
| 224 | * MDIO Address register and then write the value into the MDIO Write |
| 225 | * Data register. Finally, set the Status bit in the MDIO Control |
| 226 | * register to start a MDIO write transaction. |
| 227 | */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 228 | u32 ctrl_reg = __raw_readl(®s->mdioctrl); |
| 229 | __raw_writel(~XEL_MDIOADDR_OP_MASK |
| 230 | & ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) |
| 231 | | registernum), ®s->mdioaddr); |
| 232 | __raw_writel(data, ®s->mdiowr); |
| 233 | __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, ®s->mdioctrl); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 234 | |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 235 | if (mdio_wait(regs)) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 236 | return 1; |
| 237 | |
| 238 | return 0; |
| 239 | } |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 240 | |
Michal Simek | feebc8a | 2015-12-16 10:40:05 +0100 | [diff] [blame] | 241 | static void emaclite_stop(struct udevice *dev) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 242 | { |
Michal Simek | feebc8a | 2015-12-16 10:40:05 +0100 | [diff] [blame] | 243 | debug("eth_stop\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 244 | } |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 245 | |
| 246 | /* Use MII register 1 (MII status register) to detect PHY */ |
| 247 | #define PHY_DETECT_REG 1 |
| 248 | |
| 249 | /* Mask used to verify certain PHY features (or register contents) |
| 250 | * in the register above: |
| 251 | * 0x1000: 10Mbps full duplex support |
| 252 | * 0x0800: 10Mbps half duplex support |
| 253 | * 0x0008: Auto-negotiation support |
| 254 | */ |
| 255 | #define PHY_DETECT_MASK 0x1808 |
| 256 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 257 | static int setup_phy(struct udevice *dev) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 258 | { |
Michal Simek | dbc0cfc | 2016-05-18 12:37:22 +0200 | [diff] [blame] | 259 | int i, ret; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 260 | u16 phyreg; |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 261 | struct xemaclite *emaclite = dev_get_priv(dev); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 262 | struct phy_device *phydev; |
| 263 | |
| 264 | u32 supported = SUPPORTED_10baseT_Half | |
| 265 | SUPPORTED_10baseT_Full | |
| 266 | SUPPORTED_100baseT_Half | |
| 267 | SUPPORTED_100baseT_Full; |
| 268 | |
| 269 | if (emaclite->phyaddr != -1) { |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 270 | phyread(emaclite, emaclite->phyaddr, PHY_DETECT_REG, &phyreg); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 271 | if ((phyreg != 0xFFFF) && |
| 272 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 273 | /* Found a valid PHY address */ |
| 274 | debug("Default phy address %d is valid\n", |
| 275 | emaclite->phyaddr); |
| 276 | } else { |
| 277 | debug("PHY address is not setup correctly %d\n", |
| 278 | emaclite->phyaddr); |
| 279 | emaclite->phyaddr = -1; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (emaclite->phyaddr == -1) { |
| 284 | /* detect the PHY address */ |
| 285 | for (i = 31; i >= 0; i--) { |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 286 | phyread(emaclite, i, PHY_DETECT_REG, &phyreg); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 287 | if ((phyreg != 0xFFFF) && |
| 288 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 289 | /* Found a valid PHY address */ |
| 290 | emaclite->phyaddr = i; |
| 291 | debug("emaclite: Found valid phy address, %d\n", |
| 292 | i); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* interface - look at tsec */ |
| 299 | phydev = phy_connect(emaclite->bus, emaclite->phyaddr, dev, |
| 300 | PHY_INTERFACE_MODE_MII); |
| 301 | /* |
| 302 | * Phy can support 1000baseT but device NOT that's why phydev->supported |
| 303 | * must be setup for 1000baseT. phydev->advertising setups what speeds |
| 304 | * will be used for autonegotiation where 1000baseT must be disabled. |
| 305 | */ |
| 306 | phydev->supported = supported | SUPPORTED_1000baseT_Half | |
| 307 | SUPPORTED_1000baseT_Full; |
| 308 | phydev->advertising = supported; |
| 309 | emaclite->phydev = phydev; |
| 310 | phy_config(phydev); |
Michal Simek | dbc0cfc | 2016-05-18 12:37:22 +0200 | [diff] [blame] | 311 | ret = phy_startup(phydev); |
| 312 | if (ret) |
| 313 | return ret; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 314 | |
| 315 | if (!phydev->link) { |
| 316 | printf("%s: No link.\n", phydev->dev->name); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* Do not setup anything */ |
| 321 | return 1; |
| 322 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 323 | |
Michal Simek | feebc8a | 2015-12-16 10:40:05 +0100 | [diff] [blame] | 324 | static int emaclite_start(struct udevice *dev) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 325 | { |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 326 | struct xemaclite *emaclite = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 327 | struct eth_pdata *pdata = dev_get_plat(dev); |
Michal Simek | 905f098 | 2015-12-10 14:18:15 +0100 | [diff] [blame] | 328 | struct emaclite_regs *regs = emaclite->regs; |
| 329 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 330 | debug("EmacLite Initialization Started\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * TX - TX_PING & TX_PONG initialization |
| 334 | */ |
| 335 | /* Restart PING TX */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 336 | __raw_writel(0, ®s->tx_ping_tsr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 337 | /* Copy MAC address */ |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 338 | xemaclite_alignedwrite(pdata->enetaddr, ®s->tx_ping, |
Michal Simek | 34240c4 | 2015-12-10 15:22:21 +0100 | [diff] [blame] | 339 | ENET_ADDR_LENGTH); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 340 | /* Set the length */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 341 | __raw_writel(ENET_ADDR_LENGTH, ®s->tx_ping_tplr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 342 | /* Update the MAC address in the EMAC Lite */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 343 | __raw_writel(XEL_TSR_PROG_MAC_ADDR, ®s->tx_ping_tsr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 344 | /* Wait for EMAC Lite to finish with the MAC address update */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 345 | while ((__raw_readl(®s->tx_ping_tsr) & |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 346 | XEL_TSR_PROG_MAC_ADDR) != 0) |
| 347 | ; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 348 | |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 349 | if (emaclite->txpp) { |
| 350 | /* The same operation with PONG TX */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 351 | __raw_writel(0, ®s->tx_pong_tsr); |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 352 | xemaclite_alignedwrite(pdata->enetaddr, ®s->tx_pong, |
Michal Simek | 34240c4 | 2015-12-10 15:22:21 +0100 | [diff] [blame] | 353 | ENET_ADDR_LENGTH); |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 354 | __raw_writel(ENET_ADDR_LENGTH, ®s->tx_pong_tplr); |
| 355 | __raw_writel(XEL_TSR_PROG_MAC_ADDR, ®s->tx_pong_tsr); |
| 356 | while ((__raw_readl(®s->tx_pong_tsr) & |
Michal Simek | 34240c4 | 2015-12-10 15:22:21 +0100 | [diff] [blame] | 357 | XEL_TSR_PROG_MAC_ADDR) != 0) |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 358 | ; |
| 359 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * RX - RX_PING & RX_PONG initialization |
| 363 | */ |
| 364 | /* Write out the value to flush the RX buffer */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 365 | __raw_writel(XEL_RSR_RECV_IE_MASK, ®s->rx_ping_rsr); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 366 | |
| 367 | if (emaclite->rxpp) |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 368 | __raw_writel(XEL_RSR_RECV_IE_MASK, ®s->rx_pong_rsr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 369 | |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 370 | __raw_writel(XEL_MDIOCTRL_MDIOEN_MASK, ®s->mdioctrl); |
| 371 | if (__raw_readl(®s->mdioctrl) & XEL_MDIOCTRL_MDIOEN_MASK) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 372 | if (!setup_phy(dev)) |
| 373 | return -1; |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 374 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 375 | debug("EmacLite Initialization complete\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
Michal Simek | 1edc657 | 2015-12-10 15:42:01 +0100 | [diff] [blame] | 379 | static int xemaclite_txbufferavailable(struct xemaclite *emaclite) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 380 | { |
Michal Simek | 1edc657 | 2015-12-10 15:42:01 +0100 | [diff] [blame] | 381 | u32 tmp; |
| 382 | struct emaclite_regs *regs = emaclite->regs; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 383 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 384 | /* |
| 385 | * Read the other buffer register |
| 386 | * and determine if the other buffer is available |
| 387 | */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 388 | tmp = ~__raw_readl(®s->tx_ping_tsr); |
Michal Simek | 1edc657 | 2015-12-10 15:42:01 +0100 | [diff] [blame] | 389 | if (emaclite->txpp) |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 390 | tmp |= ~__raw_readl(®s->tx_pong_tsr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 391 | |
Michal Simek | 1edc657 | 2015-12-10 15:42:01 +0100 | [diff] [blame] | 392 | return !(tmp & XEL_TSR_XMIT_BUSY_MASK); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 395 | static int emaclite_send(struct udevice *dev, void *ptr, int len) |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 396 | { |
| 397 | u32 reg; |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 398 | struct xemaclite *emaclite = dev_get_priv(dev); |
Michal Simek | 9b9423b | 2015-12-10 15:32:11 +0100 | [diff] [blame] | 399 | struct emaclite_regs *regs = emaclite->regs; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 400 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 401 | u32 maxtry = 1000; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 402 | |
Michal Simek | 3aa96f8 | 2011-09-12 21:10:04 +0000 | [diff] [blame] | 403 | if (len > PKTSIZE) |
| 404 | len = PKTSIZE; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 405 | |
Michal Simek | 1edc657 | 2015-12-10 15:42:01 +0100 | [diff] [blame] | 406 | while (xemaclite_txbufferavailable(emaclite) && maxtry) { |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 407 | udelay(10); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 408 | maxtry--; |
| 409 | } |
| 410 | |
| 411 | if (!maxtry) { |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 412 | printf("Error: Timeout waiting for ethernet TX buffer\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 413 | /* Restart PING TX */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 414 | __raw_writel(0, ®s->tx_ping_tsr); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 415 | if (emaclite->txpp) { |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 416 | __raw_writel(0, ®s->tx_pong_tsr); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 417 | } |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 418 | return -1; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 421 | /* Determine if the expected buffer address is empty */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 422 | reg = __raw_readl(®s->tx_ping_tsr); |
Michal Simek | d92cef4 | 2015-12-10 16:06:07 +0100 | [diff] [blame] | 423 | if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) { |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 424 | debug("Send packet from tx_ping buffer\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 425 | /* Write the frame to the buffer */ |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 426 | xemaclite_alignedwrite(ptr, ®s->tx_ping, len); |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 427 | __raw_writel(len |
| 428 | & (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO), |
| 429 | ®s->tx_ping_tplr); |
| 430 | reg = __raw_readl(®s->tx_ping_tsr); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 431 | reg |= XEL_TSR_XMIT_BUSY_MASK; |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 432 | __raw_writel(reg, ®s->tx_ping_tsr); |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 433 | return 0; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 434 | } |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 435 | |
| 436 | if (emaclite->txpp) { |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 437 | /* Determine if the expected buffer address is empty */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 438 | reg = __raw_readl(®s->tx_pong_tsr); |
Michal Simek | d92cef4 | 2015-12-10 16:06:07 +0100 | [diff] [blame] | 439 | if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) { |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 440 | debug("Send packet from tx_pong buffer\n"); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 441 | /* Write the frame to the buffer */ |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 442 | xemaclite_alignedwrite(ptr, ®s->tx_pong, len); |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 443 | __raw_writel(len & |
Michal Simek | 90e89bf | 2015-12-10 16:01:50 +0100 | [diff] [blame] | 444 | (XEL_TPLR_LENGTH_MASK_HI | |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 445 | XEL_TPLR_LENGTH_MASK_LO), |
| 446 | ®s->tx_pong_tplr); |
| 447 | reg = __raw_readl(®s->tx_pong_tsr); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 448 | reg |= XEL_TSR_XMIT_BUSY_MASK; |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 449 | __raw_writel(reg, ®s->tx_pong_tsr); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 450 | return 0; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 451 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 452 | } |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 453 | |
Michal Simek | 5d1cf6c | 2011-09-12 21:10:05 +0000 | [diff] [blame] | 454 | puts("Error while sending frame\n"); |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 455 | return -1; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 458 | static int emaclite_recv(struct udevice *dev, int flags, uchar **packetp) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 459 | { |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 460 | u32 length, first_read, reg, attempt = 0; |
| 461 | void *addr, *ack; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 462 | struct xemaclite *emaclite = dev_get_priv(dev); |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 463 | struct emaclite_regs *regs = emaclite->regs; |
| 464 | struct ethernet_hdr *eth; |
| 465 | struct ip_udp_hdr *ip; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 466 | |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 467 | try_again: |
| 468 | if (!emaclite->use_rx_pong_buffer_next) { |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 469 | reg = __raw_readl(®s->rx_ping_rsr); |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 470 | debug("Testing data at rx_ping\n"); |
| 471 | if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) { |
| 472 | debug("Data found in rx_ping buffer\n"); |
| 473 | addr = ®s->rx_ping; |
| 474 | ack = ®s->rx_ping_rsr; |
| 475 | } else { |
| 476 | debug("Data not found in rx_ping buffer\n"); |
| 477 | /* Pong buffer is not available - return immediately */ |
| 478 | if (!emaclite->rxpp) |
| 479 | return -1; |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 480 | |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 481 | /* Try pong buffer if this is first attempt */ |
| 482 | if (attempt++) |
| 483 | return -1; |
| 484 | emaclite->use_rx_pong_buffer_next = |
| 485 | !emaclite->use_rx_pong_buffer_next; |
| 486 | goto try_again; |
| 487 | } |
| 488 | } else { |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 489 | reg = __raw_readl(®s->rx_pong_rsr); |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 490 | debug("Testing data at rx_pong\n"); |
| 491 | if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) { |
| 492 | debug("Data found in rx_pong buffer\n"); |
| 493 | addr = ®s->rx_pong; |
| 494 | ack = ®s->rx_pong_rsr; |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 495 | } else { |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 496 | debug("Data not found in rx_pong buffer\n"); |
| 497 | /* Try ping buffer if this is first attempt */ |
| 498 | if (attempt++) |
| 499 | return -1; |
| 500 | emaclite->use_rx_pong_buffer_next = |
| 501 | !emaclite->use_rx_pong_buffer_next; |
| 502 | goto try_again; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 503 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 504 | } |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 505 | |
| 506 | /* Read all bytes for ARP packet with 32bit alignment - 48bytes */ |
| 507 | first_read = ALIGN(ETHER_HDR_SIZE + ARP_HDR_SIZE + ETH_FCS_LEN, 4); |
| 508 | xemaclite_alignedread(addr, etherrxbuff, first_read); |
| 509 | |
| 510 | /* Detect real packet size */ |
| 511 | eth = (struct ethernet_hdr *)etherrxbuff; |
| 512 | switch (ntohs(eth->et_protlen)) { |
| 513 | case PROT_ARP: |
| 514 | length = first_read; |
| 515 | debug("ARP Packet %x\n", length); |
| 516 | break; |
| 517 | case PROT_IP: |
| 518 | ip = (struct ip_udp_hdr *)(etherrxbuff + ETHER_HDR_SIZE); |
| 519 | length = ntohs(ip->ip_len); |
| 520 | length += ETHER_HDR_SIZE + ETH_FCS_LEN; |
| 521 | debug("IP Packet %x\n", length); |
| 522 | break; |
| 523 | default: |
| 524 | debug("Other Packet\n"); |
| 525 | length = PKTSIZE; |
| 526 | break; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 529 | /* Read the rest of the packet which is longer then first read */ |
| 530 | if (length != first_read) |
| 531 | xemaclite_alignedread(addr + first_read, |
| 532 | etherrxbuff + first_read, |
| 533 | length - first_read); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 534 | |
| 535 | /* Acknowledge the frame */ |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 536 | reg = __raw_readl(ack); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 537 | reg &= ~XEL_RSR_RECV_DONE_MASK; |
Zubair Lutfullah Kakakhel | 9166459 | 2016-07-27 12:25:08 +0100 | [diff] [blame] | 538 | __raw_writel(reg, ack); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 539 | |
Michal Simek | 36f7a41 | 2015-12-10 16:31:38 +0100 | [diff] [blame] | 540 | debug("Packet receive from 0x%p, length %dB\n", addr, length); |
Michal Simek | 641ade0 | 2015-12-16 10:52:39 +0100 | [diff] [blame] | 541 | *packetp = etherrxbuff; |
| 542 | return length; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 543 | } |
| 544 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 545 | static int emaclite_miiphy_read(struct mii_dev *bus, int addr, |
| 546 | int devad, int reg) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 547 | { |
| 548 | u32 ret; |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 549 | u16 val = 0; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 550 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 551 | ret = phyread(bus->priv, addr, reg, &val); |
| 552 | debug("emaclite: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg, val, ret); |
| 553 | return val; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 554 | } |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 555 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 556 | static int emaclite_miiphy_write(struct mii_dev *bus, int addr, int devad, |
| 557 | int reg, u16 value) |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 558 | { |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 559 | debug("emaclite: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value); |
| 560 | return phywrite(bus->priv, addr, reg, value); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 561 | } |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 562 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 563 | static int emaclite_probe(struct udevice *dev) |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 564 | { |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 565 | struct xemaclite *emaclite = dev_get_priv(dev); |
| 566 | int ret; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 567 | |
T Karthik Reddy | cff1084 | 2022-05-10 13:26:10 +0200 | [diff] [blame] | 568 | if (IS_ENABLED(CONFIG_DM_ETH_PHY)) |
| 569 | emaclite->bus = eth_phy_get_mdio_bus(dev); |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 570 | |
T Karthik Reddy | cff1084 | 2022-05-10 13:26:10 +0200 | [diff] [blame] | 571 | if (!emaclite->bus) { |
| 572 | emaclite->bus = mdio_alloc(); |
| 573 | emaclite->bus->read = emaclite_miiphy_read; |
| 574 | emaclite->bus->write = emaclite_miiphy_write; |
| 575 | emaclite->bus->priv = emaclite; |
| 576 | |
| 577 | ret = mdio_register_seq(emaclite->bus, dev_seq(dev)); |
| 578 | if (ret) |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | if (IS_ENABLED(CONFIG_DM_ETH_PHY)) { |
| 583 | eth_phy_set_mdio_bus(dev, emaclite->bus); |
| 584 | emaclite->phyaddr = eth_phy_get_addr(dev); |
| 585 | } |
| 586 | |
| 587 | printf("EMACLITE: %lx, phyaddr %d, %d/%d\n", (ulong)emaclite->regs, |
| 588 | emaclite->phyaddr, emaclite->txpp, emaclite->rxpp); |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 589 | |
| 590 | return 0; |
| 591 | } |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 592 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 593 | static int emaclite_remove(struct udevice *dev) |
| 594 | { |
| 595 | struct xemaclite *emaclite = dev_get_priv(dev); |
| 596 | |
| 597 | free(emaclite->phydev); |
| 598 | mdio_unregister(emaclite->bus); |
| 599 | mdio_free(emaclite->bus); |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 600 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 601 | return 0; |
| 602 | } |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 603 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 604 | static const struct eth_ops emaclite_ops = { |
Michal Simek | feebc8a | 2015-12-16 10:40:05 +0100 | [diff] [blame] | 605 | .start = emaclite_start, |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 606 | .send = emaclite_send, |
| 607 | .recv = emaclite_recv, |
Michal Simek | feebc8a | 2015-12-16 10:40:05 +0100 | [diff] [blame] | 608 | .stop = emaclite_stop, |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 609 | }; |
| 610 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 611 | static int emaclite_of_to_plat(struct udevice *dev) |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 612 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 613 | struct eth_pdata *pdata = dev_get_plat(dev); |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 614 | struct xemaclite *emaclite = dev_get_priv(dev); |
| 615 | int offset = 0; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 616 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 617 | pdata->iobase = dev_read_addr(dev); |
Zubair Lutfullah Kakakhel | d23bf84 | 2016-07-27 12:25:07 +0100 | [diff] [blame] | 618 | emaclite->regs = (struct emaclite_regs *)ioremap_nocache(pdata->iobase, |
| 619 | 0x10000); |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 620 | |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 621 | emaclite->phyaddr = -1; |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 622 | |
T Karthik Reddy | cff1084 | 2022-05-10 13:26:10 +0200 | [diff] [blame] | 623 | if (!(IS_ENABLED(CONFIG_DM_ETH_PHY))) { |
| 624 | offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), |
| 625 | "phy-handle"); |
| 626 | if (offset > 0) |
| 627 | emaclite->phyaddr = fdtdec_get_int(gd->fdt_blob, |
| 628 | offset, "reg", -1); |
| 629 | } |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 630 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 631 | emaclite->txpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 632 | "xlnx,tx-ping-pong", 0); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 633 | emaclite->rxpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 634 | "xlnx,rx-ping-pong", 0); |
Michal Simek | 912145b | 2015-12-10 13:33:20 +0100 | [diff] [blame] | 635 | |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 636 | return 0; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 637 | } |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 638 | |
| 639 | static const struct udevice_id emaclite_ids[] = { |
| 640 | { .compatible = "xlnx,xps-ethernetlite-1.00.a" }, |
| 641 | { } |
| 642 | }; |
| 643 | |
| 644 | U_BOOT_DRIVER(emaclite) = { |
| 645 | .name = "emaclite", |
| 646 | .id = UCLASS_ETH, |
| 647 | .of_match = emaclite_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 648 | .of_to_plat = emaclite_of_to_plat, |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 649 | .probe = emaclite_probe, |
| 650 | .remove = emaclite_remove, |
| 651 | .ops = &emaclite_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 652 | .priv_auto = sizeof(struct xemaclite), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 653 | .plat_auto = sizeof(struct eth_pdata), |
Michal Simek | f7cba78 | 2015-12-10 17:15:52 +0100 | [diff] [blame] | 654 | }; |