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