Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 |
| 3 | * Marvell Semiconductor <www.marvell.com> |
| 4 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 5 | * |
| 6 | * (C) Copyright 2003 |
| 7 | * Ingo Assmus <ingo.assmus@keymile.com> |
| 8 | * |
| 9 | * based on - Driver for MV64360X ethernet ports |
| 10 | * Copyright (C) 2002 rabeeh@galileo.co.il |
| 11 | * |
| 12 | * See file CREDITS for list of people who contributed to this |
| 13 | * project. |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License as |
| 17 | * published by the Free Software Foundation; either version 2 of |
| 18 | * the License, or (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 28 | * MA 02110-1301 USA |
| 29 | */ |
| 30 | |
| 31 | #include <common.h> |
| 32 | #include <net.h> |
| 33 | #include <malloc.h> |
| 34 | #include <miiphy.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 35 | #include <asm/io.h> |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 36 | #include <asm/errno.h> |
| 37 | #include <asm/types.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 38 | #include <asm/system.h> |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 39 | #include <asm/byteorder.h> |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 40 | |
| 41 | #if defined(CONFIG_KIRKWOOD) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 42 | #include <asm/arch/kirkwood.h> |
Albert Aribaud | 8a99523 | 2010-07-12 22:24:29 +0200 | [diff] [blame] | 43 | #elif defined(CONFIG_ORION5X) |
| 44 | #include <asm/arch/orion5x.h> |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Albert Aribaud | 0d027d9 | 2010-07-12 22:24:27 +0200 | [diff] [blame] | 47 | #include "mvgbe.h" |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 48 | |
Albert Aribaud | a756407 | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
| 50 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 51 | #define MV_PHY_ADR_REQUEST 0xee |
| 52 | #define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi) |
Simon Kagstrom | ab9ca51 | 2009-08-20 10:12:28 +0200 | [diff] [blame] | 53 | |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 54 | /* |
| 55 | * smi_reg_read - miiphy_read callback function. |
| 56 | * |
| 57 | * Returns 16bit phy register value, or 0xffff on error |
| 58 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 59 | static int smi_reg_read(const char *devname, u8 phy_adr, u8 reg_ofs, u16 * data) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 60 | { |
| 61 | struct eth_device *dev = eth_get_dev_by_name(devname); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 62 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 63 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 64 | u32 smi_reg; |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 65 | u32 timeout; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 66 | |
| 67 | /* Phyadr read request */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 68 | if (phy_adr == MV_PHY_ADR_REQUEST && |
| 69 | reg_ofs == MV_PHY_ADR_REQUEST) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 70 | /* */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 71 | *data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | /* check parameters */ |
| 75 | if (phy_adr > PHYADR_MASK) { |
| 76 | printf("Err..(%s) Invalid PHY address %d\n", |
| 77 | __FUNCTION__, phy_adr); |
| 78 | return -EFAULT; |
| 79 | } |
| 80 | if (reg_ofs > PHYREG_MASK) { |
| 81 | printf("Err..(%s) Invalid register offset %d\n", |
| 82 | __FUNCTION__, reg_ofs); |
| 83 | return -EFAULT; |
| 84 | } |
| 85 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 86 | timeout = MVGBE_PHY_SMI_TIMEOUT; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 87 | /* wait till the SMI is not busy */ |
| 88 | do { |
| 89 | /* read smi register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 90 | smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 91 | if (timeout-- == 0) { |
| 92 | printf("Err..(%s) SMI busy timeout\n", __FUNCTION__); |
| 93 | return -EFAULT; |
| 94 | } |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 95 | } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 96 | |
| 97 | /* fill the phy address and regiser offset and read opcode */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 98 | smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) |
| 99 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS) |
| 100 | | MVGBE_PHY_SMI_OPCODE_READ; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 101 | |
| 102 | /* write the smi register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 103 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 104 | |
| 105 | /*wait till read value is ready */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 106 | timeout = MVGBE_PHY_SMI_TIMEOUT; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 107 | |
| 108 | do { |
| 109 | /* read smi register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 110 | smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 111 | if (timeout-- == 0) { |
| 112 | printf("Err..(%s) SMI read ready timeout\n", |
| 113 | __FUNCTION__); |
| 114 | return -EFAULT; |
| 115 | } |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 116 | } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 117 | |
| 118 | /* Wait for the data to update in the SMI register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 119 | for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++) |
| 120 | ; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 121 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 122 | *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] | 123 | |
| 124 | debug("%s:(adr %d, off %d) value= %04x\n", __FUNCTION__, phy_adr, |
| 125 | reg_ofs, *data); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * smi_reg_write - imiiphy_write callback function. |
| 132 | * |
| 133 | * Returns 0 if write succeed, -EINVAL on bad parameters |
| 134 | * -ETIME on timeout |
| 135 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 136 | static int smi_reg_write(const char *devname, u8 phy_adr, u8 reg_ofs, u16 data) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 137 | { |
| 138 | struct eth_device *dev = eth_get_dev_by_name(devname); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 139 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 140 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 141 | u32 smi_reg; |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 142 | u32 timeout; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 143 | |
| 144 | /* Phyadr write request*/ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 145 | if (phy_adr == MV_PHY_ADR_REQUEST && |
| 146 | reg_ofs == MV_PHY_ADR_REQUEST) { |
| 147 | MVGBE_REG_WR(regs->phyadr, data); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* check parameters */ |
| 152 | if (phy_adr > PHYADR_MASK) { |
| 153 | printf("Err..(%s) Invalid phy address\n", __FUNCTION__); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | if (reg_ofs > PHYREG_MASK) { |
| 157 | printf("Err..(%s) Invalid register offset\n", __FUNCTION__); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | /* wait till the SMI is not busy */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 162 | timeout = MVGBE_PHY_SMI_TIMEOUT; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 163 | do { |
| 164 | /* read smi register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 165 | smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 166 | if (timeout-- == 0) { |
| 167 | printf("Err..(%s) SMI busy timeout\n", __FUNCTION__); |
| 168 | return -ETIME; |
| 169 | } |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 170 | } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 171 | |
| 172 | /* fill the phy addr and reg offset and write opcode and data */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 173 | smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS); |
| 174 | smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS) |
| 175 | | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS); |
| 176 | smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 177 | |
| 178 | /* write the smi register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 179 | MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* Stop and checks all queues */ |
| 185 | static void stop_queue(u32 * qreg) |
| 186 | { |
| 187 | u32 reg_data; |
| 188 | |
| 189 | reg_data = readl(qreg); |
| 190 | |
| 191 | if (reg_data & 0xFF) { |
| 192 | /* Issue stop command for active channels only */ |
| 193 | writel((reg_data << 8), qreg); |
| 194 | |
| 195 | /* Wait for all queue activity to terminate. */ |
| 196 | do { |
| 197 | /* |
| 198 | * Check port cause register that all queues |
| 199 | * are stopped |
| 200 | */ |
| 201 | reg_data = readl(qreg); |
| 202 | } |
| 203 | while (reg_data & 0xFF); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * set_access_control - Config address decode parameters for Ethernet unit |
| 209 | * |
| 210 | * This function configures the address decode parameters for the Gigabit |
| 211 | * Ethernet Controller according the given parameters struct. |
| 212 | * |
| 213 | * @regs Register struct pointer. |
| 214 | * @param Address decode parameter struct. |
| 215 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 216 | static void set_access_control(struct mvgbe_registers *regs, |
| 217 | struct mvgbe_winparam *param) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 218 | { |
| 219 | u32 access_prot_reg; |
| 220 | |
| 221 | /* Set access control register */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 222 | access_prot_reg = MVGBE_REG_RD(regs->epap); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 223 | /* clear window permission */ |
| 224 | access_prot_reg &= (~(3 << (param->win * 2))); |
| 225 | access_prot_reg |= (param->access_ctrl << (param->win * 2)); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 226 | MVGBE_REG_WR(regs->epap, access_prot_reg); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 227 | |
| 228 | /* Set window Size reg (SR) */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 229 | MVGBE_REG_WR(regs->barsz[param->win].size, |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 230 | (((param->size / 0x10000) - 1) << 16)); |
| 231 | |
| 232 | /* Set window Base address reg (BA) */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 233 | MVGBE_REG_WR(regs->barsz[param->win].bar, |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 234 | (param->target | param->attrib | param->base_addr)); |
| 235 | /* High address remap reg (HARR) */ |
| 236 | if (param->win < 4) |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 237 | MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 238 | |
| 239 | /* Base address enable reg (BARER) */ |
| 240 | if (param->enable == 1) |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 241 | MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 242 | else |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 243 | MVGBE_REG_BITS_SET(regs->bare, (1 << param->win)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 244 | } |
| 245 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 246 | static void set_dram_access(struct mvgbe_registers *regs) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 247 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 248 | struct mvgbe_winparam win_param; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 249 | int i; |
| 250 | |
| 251 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 252 | /* Set access parameters for DRAM bank i */ |
| 253 | win_param.win = i; /* Use Ethernet window i */ |
| 254 | /* Window target - DDR */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 255 | win_param.target = MVGBE_TARGET_DRAM; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 256 | /* Enable full access */ |
| 257 | win_param.access_ctrl = EWIN_ACCESS_FULL; |
| 258 | win_param.high_addr = 0; |
Albert Aribaud | a756407 | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 259 | /* Get bank base and size */ |
| 260 | win_param.base_addr = gd->bd->bi_dram[i].start; |
| 261 | win_param.size = gd->bd->bi_dram[i].size; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 262 | if (win_param.size == 0) |
| 263 | win_param.enable = 0; |
| 264 | else |
| 265 | win_param.enable = 1; /* Enable the access */ |
| 266 | |
| 267 | /* Enable DRAM bank */ |
| 268 | switch (i) { |
| 269 | case 0: |
| 270 | win_param.attrib = EBAR_DRAM_CS0; |
| 271 | break; |
| 272 | case 1: |
| 273 | win_param.attrib = EBAR_DRAM_CS1; |
| 274 | break; |
| 275 | case 2: |
| 276 | win_param.attrib = EBAR_DRAM_CS2; |
| 277 | break; |
| 278 | case 3: |
| 279 | win_param.attrib = EBAR_DRAM_CS3; |
| 280 | break; |
| 281 | default: |
Albert Aribaud | a756407 | 2010-07-05 20:15:25 +0200 | [diff] [blame] | 282 | /* invalid bank, disable access */ |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 283 | win_param.enable = 0; |
| 284 | win_param.attrib = 0; |
| 285 | break; |
| 286 | } |
| 287 | /* Set the access control for address window(EPAPR) RD/WR */ |
| 288 | set_access_control(regs, &win_param); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables |
| 294 | * |
| 295 | * Go through all the DA filter tables (Unicast, Special Multicast & Other |
| 296 | * Multicast) and set each entry to 0. |
| 297 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 298 | static void port_init_mac_tables(struct mvgbe_registers *regs) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 299 | { |
| 300 | int table_index; |
| 301 | |
| 302 | /* Clear DA filter unicast table (Ex_dFUT) */ |
| 303 | for (table_index = 0; table_index < 4; ++table_index) |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 304 | MVGBE_REG_WR(regs->dfut[table_index], 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 305 | |
| 306 | for (table_index = 0; table_index < 64; ++table_index) { |
| 307 | /* Clear DA filter special multicast table (Ex_dFSMT) */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 308 | MVGBE_REG_WR(regs->dfsmt[table_index], 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 309 | /* Clear DA filter other multicast table (Ex_dFOMT) */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 310 | MVGBE_REG_WR(regs->dfomt[table_index], 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * port_uc_addr - This function Set the port unicast address table |
| 316 | * |
| 317 | * This function locates the proper entry in the Unicast table for the |
| 318 | * specified MAC nibble and sets its properties according to function |
| 319 | * parameters. |
| 320 | * This function add/removes MAC addresses from the port unicast address |
| 321 | * table. |
| 322 | * |
| 323 | * @uc_nibble Unicast MAC Address last nibble. |
| 324 | * @option 0 = Add, 1 = remove address. |
| 325 | * |
| 326 | * RETURN: 1 if output succeeded. 0 if option parameter is invalid. |
| 327 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 328 | static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble, |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 329 | int option) |
| 330 | { |
| 331 | u32 unicast_reg; |
| 332 | u32 tbl_offset; |
| 333 | u32 reg_offset; |
| 334 | |
| 335 | /* Locate the Unicast table entry */ |
| 336 | uc_nibble = (0xf & uc_nibble); |
| 337 | /* Register offset from unicast table base */ |
| 338 | tbl_offset = (uc_nibble / 4); |
| 339 | /* Entry offset within the above register */ |
| 340 | reg_offset = uc_nibble % 4; |
| 341 | |
| 342 | switch (option) { |
| 343 | case REJECT_MAC_ADDR: |
| 344 | /* |
| 345 | * Clear accepts frame bit at specified unicast |
| 346 | * DA table entry |
| 347 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 348 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 349 | unicast_reg &= (0xFF << (8 * reg_offset)); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 350 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 351 | break; |
| 352 | case ACCEPT_MAC_ADDR: |
| 353 | /* Set accepts frame bit at unicast DA filter table entry */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 354 | unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 355 | unicast_reg &= (0xFF << (8 * reg_offset)); |
| 356 | unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset)); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 357 | MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 358 | break; |
| 359 | default: |
| 360 | return 0; |
| 361 | } |
| 362 | return 1; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * port_uc_addr_set - This function Set the port Unicast address. |
| 367 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 368 | static void port_uc_addr_set(struct mvgbe_registers *regs, u8 * p_addr) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 369 | { |
| 370 | u32 mac_h; |
| 371 | u32 mac_l; |
| 372 | |
| 373 | mac_l = (p_addr[4] << 8) | (p_addr[5]); |
| 374 | mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) | |
| 375 | (p_addr[3] << 0); |
| 376 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 377 | MVGBE_REG_WR(regs->macal, mac_l); |
| 378 | MVGBE_REG_WR(regs->macah, mac_h); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 379 | |
| 380 | /* Accept frames of this address */ |
| 381 | port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR); |
| 382 | } |
| 383 | |
| 384 | /* |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 385 | * 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] | 386 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 387 | static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 388 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 389 | struct mvgbe_rxdesc *p_rx_desc; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 390 | int i; |
| 391 | |
| 392 | /* initialize the Rx descriptors ring */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 393 | p_rx_desc = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 394 | for (i = 0; i < RINGSZ; i++) { |
| 395 | p_rx_desc->cmd_sts = |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 396 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 397 | p_rx_desc->buf_size = PKTSIZE_ALIGN; |
| 398 | p_rx_desc->byte_cnt = 0; |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 399 | p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 400 | if (i == (RINGSZ - 1)) |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 401 | p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 402 | else { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 403 | p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *) |
| 404 | ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 405 | p_rx_desc = p_rx_desc->nxtdesc_p; |
| 406 | } |
| 407 | } |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 408 | dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 409 | } |
| 410 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 411 | static int mvgbe_init(struct eth_device *dev) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 412 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 413 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 414 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 9841dec | 2009-09-09 15:59:19 +0530 | [diff] [blame] | 415 | #if (defined (CONFIG_MII) || defined (CONFIG_CMD_MII)) \ |
| 416 | && defined (CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
Simon Kagstrom | 15cc5a6 | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 417 | int i; |
Prafulla Wadaskar | 9841dec | 2009-09-09 15:59:19 +0530 | [diff] [blame] | 418 | #endif |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 419 | /* setup RX rings */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 420 | mvgbe_init_rx_desc_ring(dmvgbe); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 421 | |
| 422 | /* Clear the ethernet port interrupts */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 423 | MVGBE_REG_WR(regs->ic, 0); |
| 424 | MVGBE_REG_WR(regs->ice, 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 425 | /* Unmask RX buffer and TX end interrupt */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 426 | MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 427 | /* Unmask phy and link status changes interrupts */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 428 | MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 429 | |
| 430 | set_dram_access(regs); |
| 431 | port_init_mac_tables(regs); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 432 | port_uc_addr_set(regs, dmvgbe->dev.enetaddr); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 433 | |
| 434 | /* Assign port configuration and command. */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 435 | MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL); |
| 436 | MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE); |
| 437 | MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 438 | |
| 439 | /* Assign port SDMA configuration */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 440 | MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE); |
| 441 | MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL); |
| 442 | MVGBE_REG_WR(regs->tqx[0].tqxtbc, |
| 443 | (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 444 | /* Turn off the port/RXUQ bandwidth limitation */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 445 | MVGBE_REG_WR(regs->pmtu, 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 446 | |
| 447 | /* Set maximum receive buffer to 9700 bytes */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 448 | MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE |
| 449 | | (MVGBE_REG_RD(regs->psc0) & MRU_MASK)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 450 | |
Prafulla Wadaskar | 60ee8a2 | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 451 | /* Enable port initially */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 452 | MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
Prafulla Wadaskar | 60ee8a2 | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 453 | |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 454 | /* |
| 455 | * Set ethernet MTU for leaky bucket mechanism to 0 - this will |
| 456 | * disable the leaky bucket mechanism . |
| 457 | */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 458 | MVGBE_REG_WR(regs->pmtu, 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 459 | |
| 460 | /* Assignment of Rx CRDB of given RXUQ */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 461 | MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr); |
Albert Aribaud | cc2b8e3 | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 462 | /* ensure previous write is done before enabling Rx DMA */ |
| 463 | isb(); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 464 | /* Enable port Rx. */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 465 | MVGBE_REG_WR(regs->rqc, (1 << RXUQ)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 466 | |
| 467 | #if (defined (CONFIG_MII) || defined (CONFIG_CMD_MII)) \ |
| 468 | && defined (CONFIG_SYS_FAULT_ECHO_LINK_DOWN) |
Simon Kagstrom | 15cc5a6 | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 469 | /* Wait up to 5s for the link status */ |
| 470 | for (i = 0; i < 5; i++) { |
| 471 | u16 phyadr; |
| 472 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 473 | miiphy_read(dev->name, MV_PHY_ADR_REQUEST, |
| 474 | MV_PHY_ADR_REQUEST, &phyadr); |
Simon Kagstrom | 15cc5a6 | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 475 | /* Return if we get link up */ |
| 476 | if (miiphy_link(dev->name, phyadr)) |
| 477 | return 0; |
| 478 | udelay(1000000); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 479 | } |
Simon Kagstrom | 15cc5a6 | 2009-08-20 10:13:06 +0200 | [diff] [blame] | 480 | |
| 481 | printf("No link on %s\n", dev->name); |
| 482 | return -1; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 483 | #endif |
| 484 | return 0; |
| 485 | } |
| 486 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 487 | static int mvgbe_halt(struct eth_device *dev) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 488 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 489 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 490 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 491 | |
| 492 | /* Disable all gigE address decoder */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 493 | MVGBE_REG_WR(regs->bare, 0x3f); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 494 | |
| 495 | stop_queue(®s->tqc); |
| 496 | stop_queue(®s->rqc); |
| 497 | |
Prafulla Wadaskar | 60ee8a2 | 2010-04-06 21:33:08 +0530 | [diff] [blame] | 498 | /* Disable port */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 499 | MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 500 | /* Set port is not reset */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 501 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 502 | #ifdef CONFIG_SYS_MII_MODE |
| 503 | /* Set MMI interface up */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 504 | MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 505 | #endif |
| 506 | /* Disable & mask ethernet port interrupts */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 507 | MVGBE_REG_WR(regs->ic, 0); |
| 508 | MVGBE_REG_WR(regs->ice, 0); |
| 509 | MVGBE_REG_WR(regs->pim, 0); |
| 510 | MVGBE_REG_WR(regs->peim, 0); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 515 | static int mvgbe_write_hwaddr(struct eth_device *dev) |
Prafulla Wadaskar | 7dae2eb | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 516 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 517 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 518 | struct mvgbe_registers *regs = dmvgbe->regs; |
Prafulla Wadaskar | 7dae2eb | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 519 | |
| 520 | /* Programs net device MAC address after initialization */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 521 | port_uc_addr_set(regs, dmvgbe->dev.enetaddr); |
Prafulla Wadaskar | 7dae2eb | 2010-04-06 22:21:33 +0530 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 525 | static int mvgbe_send(struct eth_device *dev, void *dataptr, |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 526 | int datasize) |
| 527 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 528 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 529 | struct mvgbe_registers *regs = dmvgbe->regs; |
| 530 | struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc; |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 531 | void *p = (void *)dataptr; |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 532 | u32 cmd_sts; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 533 | |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 534 | /* Copy buffer if it's misaligned */ |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 535 | if ((u32) dataptr & 0x07) { |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 536 | if (datasize > PKTSIZE_ALIGN) { |
| 537 | printf("Non-aligned data too large (%d)\n", |
| 538 | datasize); |
| 539 | return -1; |
| 540 | } |
| 541 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 542 | memcpy(dmvgbe->p_aligned_txbuf, p, datasize); |
| 543 | p = dmvgbe->p_aligned_txbuf; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 544 | } |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 545 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 546 | p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC; |
| 547 | p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC; |
| 548 | p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA; |
| 549 | p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT; |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 550 | p_txdesc->buf_ptr = (u8 *) p; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 551 | p_txdesc->byte_cnt = datasize; |
| 552 | |
Albert Aribaud | cc2b8e3 | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 553 | /* Set this tc desc as zeroth TXUQ */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 554 | MVGBE_REG_WR(regs->tcqdp[TXUQ], (u32) p_txdesc); |
Albert Aribaud | cc2b8e3 | 2010-07-10 15:41:29 +0200 | [diff] [blame] | 555 | |
| 556 | /* ensure tx desc writes above are performed before we start Tx DMA */ |
| 557 | isb(); |
| 558 | |
| 559 | /* Apply send command using zeroth TXUQ */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 560 | MVGBE_REG_WR(regs->tqc, (1 << TXUQ)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * wait for packet xmit completion |
| 564 | */ |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 565 | cmd_sts = readl(&p_txdesc->cmd_sts); |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 566 | while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 567 | /* return fail if error is detected */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 568 | if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) == |
| 569 | (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) && |
| 570 | cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 571 | printf("Err..(%s) in xmit packet\n", __FUNCTION__); |
| 572 | return -1; |
| 573 | } |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 574 | cmd_sts = readl(&p_txdesc->cmd_sts); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 575 | }; |
| 576 | return 0; |
| 577 | } |
| 578 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 579 | static int mvgbe_recv(struct eth_device *dev) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 580 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 581 | struct mvgbe_device *dmvgbe = to_mvgbe(dev); |
| 582 | struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr; |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 583 | u32 cmd_sts; |
| 584 | u32 timeout = 0; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 585 | |
| 586 | /* wait untill rx packet available or timeout */ |
| 587 | do { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 588 | if (timeout < MVGBE_PHY_SMI_TIMEOUT) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 589 | timeout++; |
| 590 | else { |
| 591 | debug("%s time out...\n", __FUNCTION__); |
| 592 | return -1; |
| 593 | } |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 594 | } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 595 | |
| 596 | if (p_rxdesc_curr->byte_cnt != 0) { |
| 597 | debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n", |
| 598 | __FUNCTION__, (u32) p_rxdesc_curr->byte_cnt, |
| 599 | (u32) p_rxdesc_curr->buf_ptr, |
| 600 | (u32) p_rxdesc_curr->cmd_sts); |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * In case received a packet without first/last bits on |
| 605 | * OR the error summary bit is on, |
| 606 | * the packets needs to be dropeed. |
| 607 | */ |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 608 | cmd_sts = readl(&p_rxdesc_curr->cmd_sts); |
| 609 | |
| 610 | if ((cmd_sts & |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 611 | (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) |
| 612 | != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 613 | |
| 614 | printf("Err..(%s) Dropping packet spread on" |
| 615 | " multiple descriptors\n", __FUNCTION__); |
| 616 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 617 | } else if (cmd_sts & MVGBE_ERROR_SUMMARY) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 618 | |
| 619 | printf("Err..(%s) Dropping packet with errors\n", |
| 620 | __FUNCTION__); |
| 621 | |
| 622 | } else { |
| 623 | /* !!! call higher layer processing */ |
| 624 | debug("%s: Sending Received packet to" |
| 625 | " upper layer (NetReceive)\n", __FUNCTION__); |
| 626 | |
| 627 | /* let the upper layer handle the packet */ |
| 628 | NetReceive((p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET), |
| 629 | (int)(p_rxdesc_curr->byte_cnt - RX_BUF_OFFSET)); |
| 630 | } |
| 631 | /* |
| 632 | * free these descriptors and point next in the ring |
| 633 | */ |
| 634 | p_rxdesc_curr->cmd_sts = |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 635 | MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 636 | p_rxdesc_curr->buf_size = PKTSIZE_ALIGN; |
| 637 | p_rxdesc_curr->byte_cnt = 0; |
| 638 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 639 | writel((unsigned)p_rxdesc_curr->nxtdesc_p, |
| 640 | (u32) &dmvgbe->p_rxdesc_curr); |
Simon Kagstrom | 4d0941c | 2009-07-08 13:03:18 +0200 | [diff] [blame] | 641 | |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 645 | int mvgbe_initialize(bd_t *bis) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 646 | { |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 647 | struct mvgbe_device *dmvgbe; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 648 | struct eth_device *dev; |
| 649 | int devnum; |
Prafulla Wadaskar | c034316 | 2009-08-10 19:43:06 +0530 | [diff] [blame] | 650 | char *s; |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 651 | u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 652 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 653 | for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) { |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 654 | /*skip if port is configured not to use */ |
| 655 | if (used_ports[devnum] == 0) |
| 656 | continue; |
| 657 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 658 | dmvgbe = malloc(sizeof(struct mvgbe_device)); |
| 659 | |
| 660 | if (!dmvgbe) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 661 | goto error1; |
| 662 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 663 | memset(dmvgbe, 0, sizeof(struct mvgbe_device)); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 664 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 665 | dmvgbe->p_rxdesc = |
| 666 | (struct mvgbe_rxdesc *)memalign(PKTALIGN, |
| 667 | MV_RXQ_DESC_ALIGNED_SIZE*RINGSZ + 1); |
| 668 | |
| 669 | if (!dmvgbe->p_rxdesc) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 670 | goto error2; |
| 671 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 672 | dmvgbe->p_rxbuf = (u8 *) memalign(PKTALIGN, |
| 673 | RINGSZ*PKTSIZE_ALIGN + 1); |
| 674 | |
| 675 | if (!dmvgbe->p_rxbuf) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 676 | goto error3; |
| 677 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 678 | dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN); |
| 679 | |
| 680 | if (!dmvgbe->p_aligned_txbuf) |
Simon Kagstrom | e9220b3 | 2009-08-20 10:14:11 +0200 | [diff] [blame] | 681 | goto error4; |
| 682 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 683 | dmvgbe->p_txdesc = (struct mvgbe_txdesc *) memalign( |
| 684 | PKTALIGN, sizeof(struct mvgbe_txdesc) + 1); |
| 685 | |
| 686 | if (!dmvgbe->p_txdesc) { |
| 687 | free(dmvgbe->p_aligned_txbuf); |
| 688 | error4: |
| 689 | free(dmvgbe->p_rxbuf); |
| 690 | error3: |
| 691 | free(dmvgbe->p_rxdesc); |
| 692 | error2: |
| 693 | free(dmvgbe); |
| 694 | error1: |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 695 | printf("Err.. %s Failed to allocate memory\n", |
| 696 | __FUNCTION__); |
| 697 | return -1; |
| 698 | } |
| 699 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 700 | dev = &dmvgbe->dev; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 701 | |
| 702 | /* must be less than NAMESIZE (16) */ |
| 703 | sprintf(dev->name, "egiga%d", devnum); |
| 704 | |
| 705 | /* Extract the MAC address from the environment */ |
| 706 | switch (devnum) { |
| 707 | case 0: |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 708 | dmvgbe->regs = (void *)MVGBE0_BASE; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 709 | s = "ethaddr"; |
| 710 | break; |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 711 | #if defined(MVGBE1_BASE) |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 712 | case 1: |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 713 | dmvgbe->regs = (void *)MVGBE1_BASE; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 714 | s = "eth1addr"; |
| 715 | break; |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 716 | #endif |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 717 | default: /* this should never happen */ |
| 718 | printf("Err..(%s) Invalid device number %d\n", |
| 719 | __FUNCTION__, devnum); |
| 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | while (!eth_getenv_enetaddr(s, dev->enetaddr)) { |
Albert Aribaud | 6d596a4 | 2010-07-12 11:03:33 +0200 | [diff] [blame] | 724 | /* Generate Private MAC addr if not set */ |
Prafulla Wadaskar | c034316 | 2009-08-10 19:43:06 +0530 | [diff] [blame] | 725 | dev->enetaddr[0] = 0x02; |
| 726 | dev->enetaddr[1] = 0x50; |
| 727 | dev->enetaddr[2] = 0x43; |
Albert Aribaud | 6d596a4 | 2010-07-12 11:03:33 +0200 | [diff] [blame] | 728 | #if defined (CONFIG_SKIP_LOCAL_MAC_RANDOMIZATION) |
| 729 | /* Generate fixed lower MAC half using devnum */ |
| 730 | dev->enetaddr[3] = 0; |
| 731 | dev->enetaddr[4] = 0; |
| 732 | dev->enetaddr[5] = devnum; |
| 733 | #else |
| 734 | /* Generate random lower MAC half */ |
Prafulla Wadaskar | c034316 | 2009-08-10 19:43:06 +0530 | [diff] [blame] | 735 | dev->enetaddr[3] = get_random_hex(); |
| 736 | dev->enetaddr[4] = get_random_hex(); |
| 737 | dev->enetaddr[5] = get_random_hex(); |
Albert Aribaud | 6d596a4 | 2010-07-12 11:03:33 +0200 | [diff] [blame] | 738 | #endif |
Prafulla Wadaskar | c034316 | 2009-08-10 19:43:06 +0530 | [diff] [blame] | 739 | eth_setenv_enetaddr(s, dev->enetaddr); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 740 | } |
| 741 | |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 742 | dev->init = (void *)mvgbe_init; |
| 743 | dev->halt = (void *)mvgbe_halt; |
| 744 | dev->send = (void *)mvgbe_send; |
| 745 | dev->recv = (void *)mvgbe_recv; |
| 746 | dev->write_hwaddr = (void *)mvgbe_write_hwaddr; |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 747 | |
| 748 | eth_register(dev); |
| 749 | |
| 750 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 751 | miiphy_register(dev->name, smi_reg_read, smi_reg_write); |
| 752 | /* Set phy address of the port */ |
Albert Aribaud | e91d7d3 | 2010-07-12 22:24:28 +0200 | [diff] [blame] | 753 | miiphy_write(dev->name, MV_PHY_ADR_REQUEST, |
| 754 | MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum); |
Prafulla Wadaskar | b7a280d | 2009-06-14 22:33:46 +0530 | [diff] [blame] | 755 | #endif |
| 756 | } |
| 757 | return 0; |
Prafulla Wadaskar | 12618ef | 2009-07-01 20:34:51 +0200 | [diff] [blame] | 758 | } |