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