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