Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 |
| 4 | * Marvell Semiconductor <www.marvell.com> |
| 5 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 6 | * |
| 7 | * (C) Copyright 2003 |
| 8 | * Ingo Assmus <ingo.assmus@keymile.com> |
| 9 | * |
| 10 | * based on - Driver for MV64360X ethernet ports |
| 11 | * Copyright (C) 2002 rabeeh@galileo.co.il |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 17 | #include <net.h> |
| 18 | #include <malloc.h> |
| 19 | #include <miiphy.h> |
Chris Packham | 5194ed7 | 2018-06-09 20:46:16 +1200 | [diff] [blame] | 20 | #include <wait_bit.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Lei Wen | a7efd71 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 22 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 24 | #include <linux/errno.h> |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 25 | #include <asm/types.h> |
Lei Wen | a7efd71 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 26 | #include <asm/system.h> |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 27 | #include <asm/byteorder.h> |
Anatolij Gustschin | 36aaa91 | 2011-10-29 10:09:22 +0000 | [diff] [blame] | 28 | #include <asm/arch/cpu.h> |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 29 | |
Trevor Woerner | bb0fb4c | 2020-05-06 08:02:40 -0400 | [diff] [blame] | 30 | #if defined(CONFIG_ARCH_KIRKWOOD) |
Stefan Roese | 3dc23f7 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 31 | #include <asm/arch/soc.h> |
Trevor Woerner | b16a331 | 2020-05-06 08:02:38 -0400 | [diff] [blame] | 32 | #elif defined(CONFIG_ARCH_ORION5X) |
Albert Aribaud | d3c9ffd | 2010-07-12 22:24:29 +0200 | [diff] [blame] | 33 | #include <asm/arch/orion5x.h> |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
Albert Aribaud | 9b6bcdc | 2010-07-12 22:24:27 +0200 | [diff] [blame] | 36 | #include "mvgbe.h" |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 37 | |
Albert Aribaud | 49fa6ed | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
Luka Perkov | 5aa2297 | 2013-11-11 07:27:53 +0100 | [diff] [blame] | 40 | #ifndef CONFIG_MVGBE_PORTS |
| 41 | # define CONFIG_MVGBE_PORTS {0, 0} |
| 42 | #endif |
| 43 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 44 | #define MV_PHY_ADR_REQUEST 0xee |
| 45 | #define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi) |
Tony Dinh | f0f9875 | 2022-04-12 13:18:19 -0700 | [diff] [blame] | 46 | #define MVGBE_PGADR_REG 22 |
Simon Kagstrom | bb1ca3b | 2009-08-20 10:12:28 +0200 | [diff] [blame] | 47 | |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 48 | #if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Chris Packham | 5194ed7 | 2018-06-09 20:46:16 +1200 | [diff] [blame] | 49 | static int smi_wait_ready(struct mvgbe_device *dmvgbe) |
| 50 | { |
| 51 | int ret; |
| 52 | |
| 53 | ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false, |
| 54 | MVGBE_PHY_SMI_TIMEOUT_MS, false); |
| 55 | if (ret) { |
| 56 | printf("Error: SMI busy timeout\n"); |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 63 | static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr, |
| 64 | int devad, int reg_ofs) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 65 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 66 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 67 | u32 smi_reg; |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 68 | u32 timeout; |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 69 | u16 data = 0; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 70 | |
| 71 | /* Phyadr read request */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 72 | if (phy_adr == MV_PHY_ADR_REQUEST && |
| 73 | reg_ofs == MV_PHY_ADR_REQUEST) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 74 | /* */ |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 75 | data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK); |
| 76 | return data; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 77 | } |
| 78 | /* check parameters */ |
| 79 | if (phy_adr > PHYADR_MASK) { |
| 80 | printf("Err..(%s) Invalid PHY address %d\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 81 | __func__, phy_adr); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 82 | return -EFAULT; |
| 83 | } |
| 84 | if (reg_ofs > PHYREG_MASK) { |
| 85 | printf("Err..(%s) Invalid register offset %d\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 86 | __func__, reg_ofs); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 87 | return -EFAULT; |
| 88 | } |
| 89 | |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 90 | /* wait till the SMI is not busy */ |
Chris Packham | 5194ed7 | 2018-06-09 20:46:16 +1200 | [diff] [blame] | 91 | if (smi_wait_ready(dmvgbe) < 0) |
| 92 | return -EFAULT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 93 | |
| 94 | /* fill the phy address and regiser offset and read opcode */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 95 | smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) |
| 96 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS) |
| 97 | | MVGBE_PHY_SMI_OPCODE_READ; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 98 | |
| 99 | /* write the smi register */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 100 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 101 | |
| 102 | /*wait till read value is ready */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 103 | timeout = MVGBE_PHY_SMI_TIMEOUT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 104 | |
| 105 | do { |
| 106 | /* read smi register */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 107 | smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 108 | if (timeout-- == 0) { |
| 109 | printf("Err..(%s) SMI read ready timeout\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 110 | __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 111 | return -EFAULT; |
| 112 | } |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 113 | } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 114 | |
| 115 | /* Wait for the data to update in the SMI register */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 116 | for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++) |
| 117 | ; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 118 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 119 | data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 120 | |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 121 | debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs, |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 122 | data); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 123 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 124 | return data; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 128 | * smi_reg_read - miiphy_read callback function. |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 129 | * |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 130 | * Returns 16bit phy register value, or -EFAULT on error |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 131 | */ |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 132 | static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad, |
| 133 | int reg_ofs) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 134 | { |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 135 | #ifdef CONFIG_DM_ETH |
| 136 | struct mvgbe_device *dmvgbe = bus->priv; |
| 137 | #else |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 138 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 139 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 140 | #endif |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 141 | |
| 142 | return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs); |
| 143 | } |
| 144 | |
| 145 | static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr, |
| 146 | int devad, int reg_ofs, u16 data) |
| 147 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 148 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 149 | u32 smi_reg; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 150 | |
| 151 | /* Phyadr write request*/ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 152 | if (phy_adr == MV_PHY_ADR_REQUEST && |
| 153 | reg_ofs == MV_PHY_ADR_REQUEST) { |
| 154 | MVGBE_REG_WR(regs->phyadr, data); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* check parameters */ |
| 159 | if (phy_adr > PHYADR_MASK) { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 160 | printf("Err..(%s) Invalid phy address\n", __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 161 | return -EINVAL; |
| 162 | } |
| 163 | if (reg_ofs > PHYREG_MASK) { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 164 | printf("Err..(%s) Invalid register offset\n", __func__); |
Chris Packham | 5194ed7 | 2018-06-09 20:46:16 +1200 | [diff] [blame] | 165 | return -EFAULT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* wait till the SMI is not busy */ |
Chris Packham | 5194ed7 | 2018-06-09 20:46:16 +1200 | [diff] [blame] | 169 | if (smi_wait_ready(dmvgbe) < 0) |
| 170 | return -EFAULT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 171 | |
| 172 | /* fill the phy addr and reg offset and write opcode and data */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 173 | smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS); |
| 174 | smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) |
| 175 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS); |
| 176 | smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 177 | |
| 178 | /* write the smi register */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 179 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * smi_reg_write - miiphy_write callback function. |
| 186 | * |
| 187 | * Returns 0 if write succeed, -EFAULT on error |
| 188 | */ |
| 189 | static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad, |
| 190 | int reg_ofs, u16 data) |
| 191 | { |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 192 | #ifdef CONFIG_DM_ETH |
| 193 | struct mvgbe_device *dmvgbe = bus->priv; |
| 194 | #else |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 195 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
| 196 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 197 | #endif |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 198 | |
| 199 | return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data); |
| 200 | } |
Stefan Bigler | cc79697 | 2012-03-26 00:02:13 +0000 | [diff] [blame] | 201 | #endif |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 202 | |
| 203 | /* Stop and checks all queues */ |
| 204 | static void stop_queue(u32 * qreg) |
| 205 | { |
| 206 | u32 reg_data; |
| 207 | |
| 208 | reg_data = readl(qreg); |
| 209 | |
| 210 | if (reg_data & 0xFF) { |
| 211 | /* Issue stop command for active channels only */ |
| 212 | writel((reg_data << 8), qreg); |
| 213 | |
| 214 | /* Wait for all queue activity to terminate. */ |
| 215 | do { |
| 216 | /* |
| 217 | * Check port cause register that all queues |
| 218 | * are stopped |
| 219 | */ |
| 220 | reg_data = readl(qreg); |
| 221 | } |
| 222 | while (reg_data & 0xFF); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * set_access_control - Config address decode parameters for Ethernet unit |
| 228 | * |
| 229 | * This function configures the address decode parameters for the Gigabit |
| 230 | * Ethernet Controller according the given parameters struct. |
| 231 | * |
| 232 | * @regs Register struct pointer. |
| 233 | * @param Address decode parameter struct. |
| 234 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 235 | static void set_access_control(struct mvgbe_registers *regs, |
| 236 | struct mvgbe_winparam *param) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 237 | { |
| 238 | u32 access_prot_reg; |
| 239 | |
| 240 | /* Set access control register */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 241 | access_prot_reg = MVGBE_REG_RD(regs->epap); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 242 | /* clear window permission */ |
| 243 | access_prot_reg &= (~(3 << (param->win * 2))); |
| 244 | access_prot_reg |= (param->access_ctrl << (param->win * 2)); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 245 | MVGBE_REG_WR(regs->epap, access_prot_reg); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 246 | |
| 247 | /* Set window Size reg (SR) */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 248 | MVGBE_REG_WR(regs->barsz[param->win].size, |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 249 | (((param->size / 0x10000) - 1) << 16)); |
| 250 | |
| 251 | /* Set window Base address reg (BA) */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 252 | MVGBE_REG_WR(regs->barsz[param->win].bar, |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 253 | (param->target | param->attrib | param->base_addr)); |
| 254 | /* High address remap reg (HARR) */ |
| 255 | if (param->win < 4) |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 256 | MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 257 | |
| 258 | /* Base address enable reg (BARER) */ |
| 259 | if (param->enable == 1) |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 260 | MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 261 | else |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 262 | MVGBE_REG_BITS_SET(regs->bare, (1 << param->win)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 263 | } |
| 264 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 265 | static void set_dram_access(struct mvgbe_registers *regs) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 266 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 267 | struct mvgbe_winparam win_param; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 268 | int i; |
| 269 | |
| 270 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 271 | /* Set access parameters for DRAM bank i */ |
| 272 | win_param.win = i; /* Use Ethernet window i */ |
| 273 | /* Window target - DDR */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 274 | win_param.target = MVGBE_TARGET_DRAM; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 275 | /* Enable full access */ |
| 276 | win_param.access_ctrl = EWIN_ACCESS_FULL; |
| 277 | win_param.high_addr = 0; |
Albert Aribaud | 49fa6ed | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 278 | /* Get bank base and size */ |
| 279 | win_param.base_addr = gd->bd->bi_dram[i].start; |
| 280 | win_param.size = gd->bd->bi_dram[i].size; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 281 | if (win_param.size == 0) |
| 282 | win_param.enable = 0; |
| 283 | else |
| 284 | win_param.enable = 1; /* Enable the access */ |
| 285 | |
| 286 | /* Enable DRAM bank */ |
| 287 | switch (i) { |
| 288 | case 0: |
| 289 | win_param.attrib = EBAR_DRAM_CS0; |
| 290 | break; |
| 291 | case 1: |
| 292 | win_param.attrib = EBAR_DRAM_CS1; |
| 293 | break; |
| 294 | case 2: |
| 295 | win_param.attrib = EBAR_DRAM_CS2; |
| 296 | break; |
| 297 | case 3: |
| 298 | win_param.attrib = EBAR_DRAM_CS3; |
| 299 | break; |
| 300 | default: |
Albert Aribaud | 49fa6ed | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 301 | /* invalid bank, disable access */ |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 302 | win_param.enable = 0; |
| 303 | win_param.attrib = 0; |
| 304 | break; |
| 305 | } |
| 306 | /* Set the access control for address window(EPAPR) RD/WR */ |
| 307 | set_access_control(regs, &win_param); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables |
| 313 | * |
| 314 | * Go through all the DA filter tables (Unicast, Special Multicast & Other |
| 315 | * Multicast) and set each entry to 0. |
| 316 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 317 | static void port_init_mac_tables(struct mvgbe_registers *regs) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 318 | { |
| 319 | int table_index; |
| 320 | |
| 321 | /* Clear DA filter unicast table (Ex_dFUT) */ |
| 322 | for (table_index = 0; table_index < 4; ++table_index) |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 323 | MVGBE_REG_WR(regs->dfut[table_index], 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 324 | |
| 325 | for (table_index = 0; table_index < 64; ++table_index) { |
| 326 | /* Clear DA filter special multicast table (Ex_dFSMT) */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 327 | MVGBE_REG_WR(regs->dfsmt[table_index], 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 328 | /* Clear DA filter other multicast table (Ex_dFOMT) */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 329 | MVGBE_REG_WR(regs->dfomt[table_index], 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * port_uc_addr - This function Set the port unicast address table |
| 335 | * |
| 336 | * This function locates the proper entry in the Unicast table for the |
| 337 | * specified MAC nibble and sets its properties according to function |
| 338 | * parameters. |
| 339 | * This function add/removes MAC addresses from the port unicast address |
| 340 | * table. |
| 341 | * |
| 342 | * @uc_nibble Unicast MAC Address last nibble. |
| 343 | * @option 0 = Add, 1 = remove address. |
| 344 | * |
| 345 | * RETURN: 1 if output succeeded. 0 if option parameter is invalid. |
| 346 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 347 | static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble, |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 348 | int option) |
| 349 | { |
| 350 | u32 unicast_reg; |
| 351 | u32 tbl_offset; |
| 352 | u32 reg_offset; |
| 353 | |
| 354 | /* Locate the Unicast table entry */ |
| 355 | uc_nibble = (0xf & uc_nibble); |
| 356 | /* Register offset from unicast table base */ |
| 357 | tbl_offset = (uc_nibble / 4); |
| 358 | /* Entry offset within the above register */ |
| 359 | reg_offset = uc_nibble % 4; |
| 360 | |
| 361 | switch (option) { |
| 362 | case REJECT_MAC_ADDR: |
| 363 | /* |
| 364 | * Clear accepts frame bit at specified unicast |
| 365 | * DA table entry |
| 366 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 367 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 368 | unicast_reg &= (0xFF << (8 * reg_offset)); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 369 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 370 | break; |
| 371 | case ACCEPT_MAC_ADDR: |
| 372 | /* Set accepts frame bit at unicast DA filter table entry */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 373 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 374 | unicast_reg &= (0xFF << (8 * reg_offset)); |
| 375 | unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset)); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 376 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 377 | break; |
| 378 | default: |
| 379 | return 0; |
| 380 | } |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * port_uc_addr_set - This function Set the port Unicast address. |
| 386 | */ |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 387 | static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 388 | { |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 389 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 390 | u32 mac_h; |
| 391 | u32 mac_l; |
| 392 | |
| 393 | mac_l = (p_addr[4] << 8) | (p_addr[5]); |
| 394 | mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) | |
| 395 | (p_addr[3] << 0); |
| 396 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 397 | MVGBE_REG_WR(regs->macal, mac_l); |
| 398 | MVGBE_REG_WR(regs->macah, mac_h); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 399 | |
| 400 | /* Accept frames of this address */ |
| 401 | port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR); |
| 402 | } |
| 403 | |
| 404 | /* |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 405 | * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory. |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 406 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 407 | static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 408 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 409 | struct mvgbe_rxdesc *p_rx_desc; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 410 | int i; |
| 411 | |
| 412 | /* initialize the Rx descriptors ring */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 413 | p_rx_desc = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 414 | for (i = 0; i < RINGSZ; i++) { |
| 415 | p_rx_desc->cmd_sts = |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 416 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 417 | p_rx_desc->buf_size = PKTSIZE_ALIGN; |
| 418 | p_rx_desc->byte_cnt = 0; |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 419 | p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 420 | if (i == (RINGSZ - 1)) |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 421 | p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 422 | else { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 423 | p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *) |
| 424 | ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 425 | p_rx_desc = p_rx_desc->nxtdesc_p; |
| 426 | } |
| 427 | } |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 428 | dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 429 | } |
| 430 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 431 | static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr, |
| 432 | const char *name) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 433 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 434 | struct mvgbe_registers *regs = dmvgbe->regs; |
Sascha Silbe | 0611c60 | 2013-08-11 17:08:23 +0200 | [diff] [blame] | 435 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \ |
| 436 | !defined(CONFIG_PHYLIB) && \ |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 437 | !defined(CONFIG_DM_ETH) && \ |
Sascha Silbe | 0611c60 | 2013-08-11 17:08:23 +0200 | [diff] [blame] | 438 | defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 439 | int i; |
Prafulla Wadaskar | aba8237 | 2009-09-09 15:59:19 +0530 | [diff] [blame] | 440 | #endif |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 441 | /* setup RX rings */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 442 | mvgbe_init_rx_desc_ring(dmvgbe); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 443 | |
| 444 | /* Clear the ethernet port interrupts */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 445 | MVGBE_REG_WR(regs->ic, 0); |
| 446 | MVGBE_REG_WR(regs->ice, 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 447 | /* Unmask RX buffer and TX end interrupt */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 448 | MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 449 | /* Unmask phy and link status changes interrupts */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 450 | MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 451 | |
| 452 | set_dram_access(regs); |
| 453 | port_init_mac_tables(regs); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 454 | port_uc_addr_set(dmvgbe, enetaddr); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 455 | |
| 456 | /* Assign port configuration and command. */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 457 | MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL); |
| 458 | MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE); |
| 459 | MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 460 | |
| 461 | /* Assign port SDMA configuration */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 462 | MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE); |
| 463 | MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL); |
| 464 | MVGBE_REG_WR(regs->tqx[0].tqxtbc, |
| 465 | (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 466 | /* Turn off the port/RXUQ bandwidth limitation */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 467 | MVGBE_REG_WR(regs->pmtu, 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 468 | |
| 469 | /* Set maximum receive buffer to 9700 bytes */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 470 | MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE |
| 471 | | (MVGBE_REG_RD(regs->psc0) & MRU_MASK)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 472 | |
Prafulla Wadaskar | f0588fd | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 473 | /* Enable port initially */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 474 | MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
Prafulla Wadaskar | f0588fd | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 475 | |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 476 | /* |
| 477 | * Set ethernet MTU for leaky bucket mechanism to 0 - this will |
| 478 | * disable the leaky bucket mechanism . |
| 479 | */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 480 | MVGBE_REG_WR(regs->pmtu, 0); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 481 | |
| 482 | /* Assignment of Rx CRDB of given RXUQ */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 483 | MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr); |
Albert Aribaud | c19a20d | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 484 | /* ensure previous write is done before enabling Rx DMA */ |
| 485 | isb(); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 486 | /* Enable port Rx. */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 487 | MVGBE_REG_WR(regs->rqc, (1 << RXUQ)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 488 | |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 489 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \ |
| 490 | !defined(CONFIG_PHYLIB) && \ |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 491 | !defined(CONFIG_DM_ETH) && \ |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 492 | defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 493 | /* Wait up to 5s for the link status */ |
| 494 | for (i = 0; i < 5; i++) { |
| 495 | u16 phyadr; |
| 496 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 497 | miiphy_read(name, MV_PHY_ADR_REQUEST, |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 498 | MV_PHY_ADR_REQUEST, &phyadr); |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 499 | /* Return if we get link up */ |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 500 | if (miiphy_link(name, phyadr)) |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 501 | return 0; |
| 502 | udelay(1000000); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 503 | } |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 504 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 505 | printf("No link on %s\n", name); |
Simon Kagstrom | cad713b | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 506 | return -1; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 507 | #endif |
| 508 | return 0; |
| 509 | } |
| 510 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 511 | #ifndef CONFIG_DM_ETH |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 512 | static int mvgbe_init(struct eth_device *dev) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 513 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 514 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 515 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 516 | return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name); |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 517 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 518 | #endif |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 519 | |
| 520 | static void __mvgbe_halt(struct mvgbe_device *dmvgbe) |
| 521 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 522 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 523 | |
| 524 | /* Disable all gigE address decoder */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 525 | MVGBE_REG_WR(regs->bare, 0x3f); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 526 | |
| 527 | stop_queue(®s->tqc); |
| 528 | stop_queue(®s->rqc); |
| 529 | |
Prafulla Wadaskar | f0588fd | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 530 | /* Disable port */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 531 | MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 532 | /* Set port is not reset */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 533 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 534 | #ifdef CONFIG_SYS_MII_MODE |
| 535 | /* Set MMI interface up */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 536 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 537 | #endif |
| 538 | /* Disable & mask ethernet port interrupts */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 539 | MVGBE_REG_WR(regs->ic, 0); |
| 540 | MVGBE_REG_WR(regs->ice, 0); |
| 541 | MVGBE_REG_WR(regs->pim, 0); |
| 542 | MVGBE_REG_WR(regs->peim, 0); |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 543 | } |
| 544 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 545 | #ifndef CONFIG_DM_ETH |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 546 | static int mvgbe_halt(struct eth_device *dev) |
| 547 | { |
| 548 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 549 | |
| 550 | __mvgbe_halt(dmvgbe); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 554 | #endif |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 555 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 556 | #ifdef CONFIG_DM_ETH |
| 557 | static int mvgbe_write_hwaddr(struct udevice *dev) |
| 558 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 559 | struct eth_pdata *pdata = dev_get_plat(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 560 | |
| 561 | port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr); |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | #else |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 566 | static int mvgbe_write_hwaddr(struct eth_device *dev) |
Prafulla Wadaskar | b5ce63e | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 567 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 568 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
Prafulla Wadaskar | b5ce63e | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 569 | |
| 570 | /* Programs net device MAC address after initialization */ |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 571 | port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr); |
Prafulla Wadaskar | b5ce63e | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 572 | return 0; |
| 573 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 574 | #endif |
Prafulla Wadaskar | b5ce63e | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 575 | |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 576 | static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr, |
| 577 | int datasize) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 578 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 579 | struct mvgbe_registers *regs = dmvgbe->regs; |
| 580 | struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc; |
Simon Kagstrom | 477fa63 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 581 | void *p = (void *)dataptr; |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 582 | u32 cmd_sts; |
Anatolij Gustschin | e6e556c | 2011-11-19 08:59:36 +0000 | [diff] [blame] | 583 | u32 txuq0_reg_addr; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 584 | |
Simon Kagstrom | 477fa63 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 585 | /* Copy buffer if it's misaligned */ |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 586 | if ((u32) dataptr & 0x07) { |
Simon Kagstrom | 477fa63 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 587 | if (datasize > PKTSIZE_ALIGN) { |
| 588 | printf("Non-aligned data too large (%d)\n", |
| 589 | datasize); |
| 590 | return -1; |
| 591 | } |
| 592 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 593 | memcpy(dmvgbe->p_aligned_txbuf, p, datasize); |
| 594 | p = dmvgbe->p_aligned_txbuf; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 595 | } |
Simon Kagstrom | 477fa63 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 596 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 597 | p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC; |
| 598 | p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC; |
| 599 | p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA; |
| 600 | p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT; |
Simon Kagstrom | 477fa63 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 601 | p_txdesc->buf_ptr = (u8 *) p; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 602 | p_txdesc->byte_cnt = datasize; |
| 603 | |
Albert Aribaud | c19a20d | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 604 | /* Set this tc desc as zeroth TXUQ */ |
Anatolij Gustschin | e6e556c | 2011-11-19 08:59:36 +0000 | [diff] [blame] | 605 | txuq0_reg_addr = (u32)®s->tcqdp[TXUQ]; |
| 606 | writel((u32) p_txdesc, txuq0_reg_addr); |
Albert Aribaud | c19a20d | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 607 | |
| 608 | /* ensure tx desc writes above are performed before we start Tx DMA */ |
| 609 | isb(); |
| 610 | |
| 611 | /* Apply send command using zeroth TXUQ */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 612 | MVGBE_REG_WR(regs->tqc, (1 << TXUQ)); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * wait for packet xmit completion |
| 616 | */ |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 617 | cmd_sts = readl(&p_txdesc->cmd_sts); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 618 | while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 619 | /* return fail if error is detected */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 620 | if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) == |
| 621 | (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) && |
| 622 | cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 623 | printf("Err..(%s) in xmit packet\n", __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 624 | return -1; |
| 625 | } |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 626 | cmd_sts = readl(&p_txdesc->cmd_sts); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 627 | }; |
| 628 | return 0; |
| 629 | } |
| 630 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 631 | #ifndef CONFIG_DM_ETH |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 632 | static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 633 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 634 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 635 | |
| 636 | return __mvgbe_send(dmvgbe, dataptr, datasize); |
| 637 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 638 | #endif |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 639 | |
| 640 | static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp) |
| 641 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 642 | struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr; |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 643 | u32 cmd_sts; |
| 644 | u32 timeout = 0; |
Anatolij Gustschin | e6e556c | 2011-11-19 08:59:36 +0000 | [diff] [blame] | 645 | u32 rxdesc_curr_addr; |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 646 | unsigned char *data; |
| 647 | int rx_bytes = 0; |
| 648 | |
| 649 | *packetp = NULL; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 650 | |
| 651 | /* wait untill rx packet available or timeout */ |
| 652 | do { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 653 | if (timeout < MVGBE_PHY_SMI_TIMEOUT) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 654 | timeout++; |
| 655 | else { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 656 | debug("%s time out...\n", __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 657 | return -1; |
| 658 | } |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 659 | } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 660 | |
| 661 | if (p_rxdesc_curr->byte_cnt != 0) { |
| 662 | debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 663 | __func__, (u32) p_rxdesc_curr->byte_cnt, |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 664 | (u32) p_rxdesc_curr->buf_ptr, |
| 665 | (u32) p_rxdesc_curr->cmd_sts); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * In case received a packet without first/last bits on |
| 670 | * OR the error summary bit is on, |
| 671 | * the packets needs to be dropeed. |
| 672 | */ |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 673 | cmd_sts = readl(&p_rxdesc_curr->cmd_sts); |
| 674 | |
| 675 | if ((cmd_sts & |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 676 | (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) |
| 677 | != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 678 | |
| 679 | printf("Err..(%s) Dropping packet spread on" |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 680 | " multiple descriptors\n", __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 681 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 682 | } else if (cmd_sts & MVGBE_ERROR_SUMMARY) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 683 | |
| 684 | printf("Err..(%s) Dropping packet with errors\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 685 | __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 686 | |
| 687 | } else { |
| 688 | /* !!! call higher layer processing */ |
| 689 | debug("%s: Sending Received packet to" |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 690 | " upper layer (net_process_received_packet)\n", |
| 691 | __func__); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 692 | |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 693 | data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET); |
| 694 | rx_bytes = (int)(p_rxdesc_curr->byte_cnt - |
| 695 | RX_BUF_OFFSET); |
| 696 | |
| 697 | *packetp = data; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 698 | } |
| 699 | /* |
| 700 | * free these descriptors and point next in the ring |
| 701 | */ |
| 702 | p_rxdesc_curr->cmd_sts = |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 703 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 704 | p_rxdesc_curr->buf_size = PKTSIZE_ALIGN; |
| 705 | p_rxdesc_curr->byte_cnt = 0; |
| 706 | |
Anatolij Gustschin | e6e556c | 2011-11-19 08:59:36 +0000 | [diff] [blame] | 707 | rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr; |
| 708 | writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr); |
Simon Kagstrom | 7b05f5e | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 709 | |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 710 | return rx_bytes; |
| 711 | } |
| 712 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 713 | #ifndef CONFIG_DM_ETH |
Chris Packham | e9bf75c | 2018-07-09 21:33:59 +1200 | [diff] [blame] | 714 | static int mvgbe_recv(struct eth_device *dev) |
| 715 | { |
| 716 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 717 | uchar *packet; |
| 718 | int ret; |
| 719 | |
| 720 | ret = __mvgbe_recv(dmvgbe, &packet); |
| 721 | if (ret < 0) |
| 722 | return ret; |
| 723 | |
| 724 | net_process_received_packet(packet, ret); |
| 725 | |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 726 | return 0; |
| 727 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 728 | #endif |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 729 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 730 | #if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH) |
| 731 | #if defined(CONFIG_DM_ETH) |
| 732 | static struct phy_device *__mvgbe_phy_init(struct udevice *dev, |
| 733 | struct mii_dev *bus, |
| 734 | phy_interface_t phy_interface, |
| 735 | int phyid) |
| 736 | #else |
| 737 | static struct phy_device *__mvgbe_phy_init(struct eth_device *dev, |
| 738 | struct mii_dev *bus, |
| 739 | phy_interface_t phy_interface, |
| 740 | int phyid) |
| 741 | #endif |
| 742 | { |
| 743 | struct phy_device *phydev; |
| 744 | |
| 745 | /* Set phy address of the port */ |
| 746 | miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST, |
| 747 | phyid); |
| 748 | |
Tony Dinh | f0f9875 | 2022-04-12 13:18:19 -0700 | [diff] [blame] | 749 | /* Make sure the selected PHY page is 0 before connecting */ |
| 750 | miiphy_write(dev->name, phyid, MVGBE_PGADR_REG, 0); |
| 751 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 752 | phydev = phy_connect(bus, phyid, dev, phy_interface); |
| 753 | if (!phydev) { |
| 754 | printf("phy_connect failed\n"); |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | phy_config(phydev); |
| 759 | phy_startup(phydev); |
| 760 | |
| 761 | return phydev; |
| 762 | } |
| 763 | #endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */ |
| 764 | |
| 765 | #if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH) |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 766 | int mvgbe_phylib_init(struct eth_device *dev, int phyid) |
| 767 | { |
| 768 | struct mii_dev *bus; |
| 769 | struct phy_device *phydev; |
| 770 | int ret; |
| 771 | |
| 772 | bus = mdio_alloc(); |
| 773 | if (!bus) { |
| 774 | printf("mdio_alloc failed\n"); |
| 775 | return -ENOMEM; |
| 776 | } |
Chris Packham | 6ecf9e2 | 2016-11-01 10:48:32 +1300 | [diff] [blame] | 777 | bus->read = smi_reg_read; |
| 778 | bus->write = smi_reg_write; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 779 | strcpy(bus->name, dev->name); |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 780 | |
| 781 | ret = mdio_register(bus); |
| 782 | if (ret) { |
| 783 | printf("mdio_register failed\n"); |
| 784 | free(bus); |
| 785 | return -ENOMEM; |
| 786 | } |
| 787 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 788 | phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid); |
| 789 | if (!phydev) |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 790 | return -ENODEV; |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | #endif |
| 795 | |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 796 | static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe) |
| 797 | { |
| 798 | dmvgbe->p_rxdesc = memalign(PKTALIGN, |
| 799 | MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1); |
| 800 | if (!dmvgbe->p_rxdesc) |
| 801 | goto error1; |
| 802 | |
| 803 | dmvgbe->p_rxbuf = memalign(PKTALIGN, |
| 804 | RINGSZ * PKTSIZE_ALIGN + 1); |
| 805 | if (!dmvgbe->p_rxbuf) |
| 806 | goto error2; |
| 807 | |
| 808 | dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN); |
| 809 | if (!dmvgbe->p_aligned_txbuf) |
| 810 | goto error3; |
| 811 | |
| 812 | dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1); |
| 813 | if (!dmvgbe->p_txdesc) |
| 814 | goto error4; |
| 815 | |
| 816 | return 0; |
| 817 | |
| 818 | error4: |
| 819 | free(dmvgbe->p_aligned_txbuf); |
| 820 | error3: |
| 821 | free(dmvgbe->p_rxbuf); |
| 822 | error2: |
| 823 | free(dmvgbe->p_rxdesc); |
| 824 | error1: |
| 825 | return -ENOMEM; |
| 826 | } |
| 827 | |
| 828 | #ifndef CONFIG_DM_ETH |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 829 | int mvgbe_initialize(struct bd_info *bis) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 830 | { |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 831 | struct mvgbe_device *dmvgbe; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 832 | struct eth_device *dev; |
| 833 | int devnum; |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 834 | int ret; |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 835 | u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 836 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 837 | for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 838 | /*skip if port is configured not to use */ |
| 839 | if (used_ports[devnum] == 0) |
| 840 | continue; |
| 841 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 842 | dmvgbe = malloc(sizeof(struct mvgbe_device)); |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 843 | if (!dmvgbe) |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 844 | return -ENOMEM; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 845 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 846 | memset(dmvgbe, 0, sizeof(struct mvgbe_device)); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 847 | ret = mvgbe_alloc_buffers(dmvgbe); |
| 848 | if (ret) { |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 849 | printf("Err.. %s Failed to allocate memory\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 850 | __func__); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 851 | free(dmvgbe); |
| 852 | return ret; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 853 | } |
| 854 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 855 | dev = &dmvgbe->dev; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 856 | |
Mike Frysinger | f6add13 | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 857 | /* must be less than sizeof(dev->name) */ |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 858 | sprintf(dev->name, "egiga%d", devnum); |
| 859 | |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 860 | switch (devnum) { |
| 861 | case 0: |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 862 | dmvgbe->regs = (void *)MVGBE0_BASE; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 863 | break; |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 864 | #if defined(MVGBE1_BASE) |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 865 | case 1: |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 866 | dmvgbe->regs = (void *)MVGBE1_BASE; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 867 | break; |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 868 | #endif |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 869 | default: /* this should never happen */ |
| 870 | printf("Err..(%s) Invalid device number %d\n", |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 871 | __func__, devnum); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 872 | return -1; |
| 873 | } |
| 874 | |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 875 | dev->init = (void *)mvgbe_init; |
| 876 | dev->halt = (void *)mvgbe_halt; |
| 877 | dev->send = (void *)mvgbe_send; |
| 878 | dev->recv = (void *)mvgbe_recv; |
| 879 | dev->write_hwaddr = (void *)mvgbe_write_hwaddr; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 880 | |
| 881 | eth_register(dev); |
| 882 | |
Sebastian Hesselbarth | cd3ca3f | 2012-12-04 09:32:00 +0100 | [diff] [blame] | 883 | #if defined(CONFIG_PHYLIB) |
| 884 | mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum); |
| 885 | #elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 886 | int retval; |
| 887 | struct mii_dev *mdiodev = mdio_alloc(); |
| 888 | if (!mdiodev) |
| 889 | return -ENOMEM; |
Vladimir Oltean | 77003e5 | 2021-09-27 14:21:55 +0300 | [diff] [blame] | 890 | strlcpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 891 | mdiodev->read = smi_reg_read; |
| 892 | mdiodev->write = smi_reg_write; |
| 893 | |
| 894 | retval = mdio_register(mdiodev); |
| 895 | if (retval < 0) |
| 896 | return retval; |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 897 | /* Set phy address of the port */ |
Albert Aribaud | d44265a | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 898 | miiphy_write(dev->name, MV_PHY_ADR_REQUEST, |
| 899 | MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum); |
Prafulla Wadaskar | 9131589 | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 900 | #endif |
| 901 | } |
| 902 | return 0; |
Prafulla Wadaskar | 0b785dd | 2009-07-01 20:34:51 +0200 | [diff] [blame] | 903 | } |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 904 | #endif |
| 905 | |
| 906 | #ifdef CONFIG_DM_ETH |
| 907 | static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe) |
| 908 | { |
| 909 | return dmvgbe->phyaddr > PHY_MAX_ADDR; |
| 910 | } |
| 911 | |
| 912 | static int mvgbe_start(struct udevice *dev) |
| 913 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 914 | struct eth_pdata *pdata = dev_get_plat(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 915 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 916 | int ret; |
| 917 | |
| 918 | ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name); |
| 919 | if (ret) |
| 920 | return ret; |
| 921 | |
| 922 | if (!mvgbe_port_is_fixed_link(dmvgbe)) { |
| 923 | dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus, |
| 924 | dmvgbe->phy_interface, |
| 925 | dmvgbe->phyaddr); |
| 926 | if (!dmvgbe->phydev) |
| 927 | return -ENODEV; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static int mvgbe_send(struct udevice *dev, void *packet, int length) |
| 934 | { |
| 935 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 936 | |
| 937 | return __mvgbe_send(dmvgbe, packet, length); |
| 938 | } |
| 939 | |
| 940 | static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp) |
| 941 | { |
| 942 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 943 | |
| 944 | return __mvgbe_recv(dmvgbe, packetp); |
| 945 | } |
| 946 | |
| 947 | static void mvgbe_stop(struct udevice *dev) |
| 948 | { |
| 949 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 950 | |
| 951 | __mvgbe_halt(dmvgbe); |
| 952 | } |
| 953 | |
| 954 | static int mvgbe_probe(struct udevice *dev) |
| 955 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 956 | struct eth_pdata *pdata = dev_get_plat(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 957 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 958 | struct mii_dev *bus; |
| 959 | int ret; |
| 960 | |
| 961 | ret = mvgbe_alloc_buffers(dmvgbe); |
| 962 | if (ret) |
| 963 | return ret; |
| 964 | |
| 965 | dmvgbe->regs = (void __iomem *)pdata->iobase; |
| 966 | |
| 967 | bus = mdio_alloc(); |
| 968 | if (!bus) { |
| 969 | printf("Failed to allocate MDIO bus\n"); |
| 970 | return -ENOMEM; |
| 971 | } |
| 972 | |
| 973 | bus->read = smi_reg_read; |
| 974 | bus->write = smi_reg_write; |
| 975 | snprintf(bus->name, sizeof(bus->name), dev->name); |
| 976 | bus->priv = dmvgbe; |
| 977 | dmvgbe->bus = bus; |
| 978 | |
| 979 | ret = mdio_register(bus); |
| 980 | if (ret < 0) |
| 981 | return ret; |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | static const struct eth_ops mvgbe_ops = { |
| 987 | .start = mvgbe_start, |
| 988 | .send = mvgbe_send, |
| 989 | .recv = mvgbe_recv, |
| 990 | .stop = mvgbe_stop, |
| 991 | .write_hwaddr = mvgbe_write_hwaddr, |
| 992 | }; |
| 993 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 994 | static int mvgbe_of_to_plat(struct udevice *dev) |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 995 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 996 | struct eth_pdata *pdata = dev_get_plat(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 997 | struct mvgbe_device *dmvgbe = dev_get_priv(dev); |
| 998 | void *blob = (void *)gd->fdt_blob; |
| 999 | int node = dev_of_offset(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 1000 | int fl_node; |
| 1001 | int pnode; |
| 1002 | unsigned long addr; |
| 1003 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 1004 | pdata->iobase = dev_read_addr(dev); |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 1005 | pdata->phy_interface = -1; |
| 1006 | |
| 1007 | pnode = fdt_node_offset_by_compatible(blob, node, |
| 1008 | "marvell,kirkwood-eth-port"); |
| 1009 | |
| 1010 | /* Get phy-mode / phy_interface from DT */ |
Marek BehĂșn | 123ca11 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 1011 | pdata->phy_interface = dev_read_phy_mode(dev); |
Marek BehĂșn | ffb0f6f | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 1012 | if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) |
Chris Packham | 92f129f | 2018-12-04 19:54:30 +1300 | [diff] [blame] | 1013 | pdata->phy_interface = PHY_INTERFACE_MODE_GMII; |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 1014 | |
| 1015 | dmvgbe->phy_interface = pdata->phy_interface; |
| 1016 | |
| 1017 | /* fetch 'fixed-link' property */ |
| 1018 | fl_node = fdt_subnode_offset(blob, pnode, "fixed-link"); |
| 1019 | if (fl_node != -FDT_ERR_NOTFOUND) { |
| 1020 | /* set phy_addr to invalid value for fixed link */ |
| 1021 | dmvgbe->phyaddr = PHY_MAX_ADDR + 1; |
| 1022 | dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex"); |
| 1023 | dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0); |
| 1024 | } else { |
| 1025 | /* Now read phyaddr from DT */ |
| 1026 | addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle"); |
| 1027 | if (addr > 0) |
| 1028 | dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0); |
| 1029 | } |
| 1030 | |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | static const struct udevice_id mvgbe_ids[] = { |
| 1035 | { .compatible = "marvell,kirkwood-eth" }, |
| 1036 | { } |
| 1037 | }; |
| 1038 | |
| 1039 | U_BOOT_DRIVER(mvgbe) = { |
| 1040 | .name = "mvgbe", |
| 1041 | .id = UCLASS_ETH, |
| 1042 | .of_match = mvgbe_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1043 | .of_to_plat = mvgbe_of_to_plat, |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 1044 | .probe = mvgbe_probe, |
| 1045 | .ops = &mvgbe_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1046 | .priv_auto = sizeof(struct mvgbe_device), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1047 | .plat_auto = sizeof(struct eth_pdata), |
Chris Packham | fb73107 | 2018-07-09 21:34:00 +1200 | [diff] [blame] | 1048 | }; |
| 1049 | #endif /* CONFIG_DM_ETH */ |