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