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