blob: 319fe8aba9718710984735f15471983f04a71db3 [file] [log] [blame]
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +05301/*
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 Wen298ae912011-10-18 20:11:42 +053035#include <asm/io.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053036#include <asm/errno.h>
37#include <asm/types.h>
Lei Wen298ae912011-10-18 20:11:42 +053038#include <asm/system.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053039#include <asm/byteorder.h>
Anatolij Gustschinc8b222e2011-10-29 10:09:22 +000040#include <asm/arch/cpu.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020041
42#if defined(CONFIG_KIRKWOOD)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053043#include <asm/arch/kirkwood.h>
Albert Aribaud8a995232010-07-12 22:24:29 +020044#elif defined(CONFIG_ORION5X)
45#include <asm/arch/orion5x.h>
Sebastian Hesselbartha533a0c2012-12-04 09:32:01 +010046#elif defined(CONFIG_DOVE)
47#include <asm/arch/dove.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020048#endif
49
Albert Aribaud0d027d92010-07-12 22:24:27 +020050#include "mvgbe.h"
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053051
Albert Aribauda7564072010-07-05 20:15:25 +020052DECLARE_GLOBAL_DATA_PTR;
53
Albert Aribaude91d7d32010-07-12 22:24:28 +020054#define MV_PHY_ADR_REQUEST 0xee
55#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Simon Kagstromab9ca512009-08-20 10:12:28 +020056
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +010057#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053058/*
59 * smi_reg_read - miiphy_read callback function.
60 *
61 * Returns 16bit phy register value, or 0xffff on error
62 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -040063static int smi_reg_read(const char *devname, u8 phy_adr, u8 reg_ofs, u16 * data)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053064{
65 struct eth_device *dev = eth_get_dev_by_name(devname);
Albert Aribaude91d7d32010-07-12 22:24:28 +020066 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
67 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053068 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +020069 u32 timeout;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053070
71 /* Phyadr read request */
Albert Aribaude91d7d32010-07-12 22:24:28 +020072 if (phy_adr == MV_PHY_ADR_REQUEST &&
73 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053074 /* */
Albert Aribaude91d7d32010-07-12 22:24:28 +020075 *data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053076 return 0;
77 }
78 /* check parameters */
79 if (phy_adr > PHYADR_MASK) {
80 printf("Err..(%s) Invalid PHY address %d\n",
81 __FUNCTION__, phy_adr);
82 return -EFAULT;
83 }
84 if (reg_ofs > PHYREG_MASK) {
85 printf("Err..(%s) Invalid register offset %d\n",
86 __FUNCTION__, reg_ofs);
87 return -EFAULT;
88 }
89
Albert Aribaude91d7d32010-07-12 22:24:28 +020090 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053091 /* wait till the SMI is not busy */
92 do {
93 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +020094 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053095 if (timeout-- == 0) {
96 printf("Err..(%s) SMI busy timeout\n", __FUNCTION__);
97 return -EFAULT;
98 }
Albert Aribaude91d7d32010-07-12 22:24:28 +020099 } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530100
101 /* fill the phy address and regiser offset and read opcode */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200102 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
103 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
104 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530105
106 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200107 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530108
109 /*wait till read value is ready */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200110 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530111
112 do {
113 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200114 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530115 if (timeout-- == 0) {
116 printf("Err..(%s) SMI read ready timeout\n",
117 __FUNCTION__);
118 return -EFAULT;
119 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200120 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530121
122 /* Wait for the data to update in the SMI register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200123 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
124 ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530125
Albert Aribaude91d7d32010-07-12 22:24:28 +0200126 *data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530127
128 debug("%s:(adr %d, off %d) value= %04x\n", __FUNCTION__, phy_adr,
129 reg_ofs, *data);
130
131 return 0;
132}
133
134/*
135 * smi_reg_write - imiiphy_write callback function.
136 *
137 * Returns 0 if write succeed, -EINVAL on bad parameters
138 * -ETIME on timeout
139 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400140static int smi_reg_write(const char *devname, u8 phy_adr, u8 reg_ofs, u16 data)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530141{
142 struct eth_device *dev = eth_get_dev_by_name(devname);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200143 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
144 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530145 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200146 u32 timeout;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530147
148 /* Phyadr write request*/
Albert Aribaude91d7d32010-07-12 22:24:28 +0200149 if (phy_adr == MV_PHY_ADR_REQUEST &&
150 reg_ofs == MV_PHY_ADR_REQUEST) {
151 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530152 return 0;
153 }
154
155 /* check parameters */
156 if (phy_adr > PHYADR_MASK) {
157 printf("Err..(%s) Invalid phy address\n", __FUNCTION__);
158 return -EINVAL;
159 }
160 if (reg_ofs > PHYREG_MASK) {
161 printf("Err..(%s) Invalid register offset\n", __FUNCTION__);
162 return -EINVAL;
163 }
164
165 /* wait till the SMI is not busy */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200166 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530167 do {
168 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200169 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530170 if (timeout-- == 0) {
171 printf("Err..(%s) SMI busy timeout\n", __FUNCTION__);
172 return -ETIME;
173 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200174 } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530175
176 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200177 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
178 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
179 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
180 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530181
182 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200183 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530184
185 return 0;
186}
Stefan Bigler96455292012-03-26 00:02:13 +0000187#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530188
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100189#if defined(CONFIG_PHYLIB)
190int mvgbe_phy_read(struct mii_dev *bus, int phy_addr, int dev_addr,
191 int reg_addr)
192{
193 u16 data;
194 int ret;
195 ret = smi_reg_read(bus->name, phy_addr, reg_addr, &data);
196 if (ret)
197 return ret;
198 return data;
199}
200
201int mvgbe_phy_write(struct mii_dev *bus, int phy_addr, int dev_addr,
202 int reg_addr, u16 data)
203{
204 return smi_reg_write(bus->name, phy_addr, reg_addr, data);
205}
206#endif
207
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530208/* Stop and checks all queues */
209static void stop_queue(u32 * qreg)
210{
211 u32 reg_data;
212
213 reg_data = readl(qreg);
214
215 if (reg_data & 0xFF) {
216 /* Issue stop command for active channels only */
217 writel((reg_data << 8), qreg);
218
219 /* Wait for all queue activity to terminate. */
220 do {
221 /*
222 * Check port cause register that all queues
223 * are stopped
224 */
225 reg_data = readl(qreg);
226 }
227 while (reg_data & 0xFF);
228 }
229}
230
231/*
232 * set_access_control - Config address decode parameters for Ethernet unit
233 *
234 * This function configures the address decode parameters for the Gigabit
235 * Ethernet Controller according the given parameters struct.
236 *
237 * @regs Register struct pointer.
238 * @param Address decode parameter struct.
239 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200240static void set_access_control(struct mvgbe_registers *regs,
241 struct mvgbe_winparam *param)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530242{
243 u32 access_prot_reg;
244
245 /* Set access control register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200246 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530247 /* clear window permission */
248 access_prot_reg &= (~(3 << (param->win * 2)));
249 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200250 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530251
252 /* Set window Size reg (SR) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200253 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530254 (((param->size / 0x10000) - 1) << 16));
255
256 /* Set window Base address reg (BA) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200257 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530258 (param->target | param->attrib | param->base_addr));
259 /* High address remap reg (HARR) */
260 if (param->win < 4)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200261 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530262
263 /* Base address enable reg (BARER) */
264 if (param->enable == 1)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200265 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530266 else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200267 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530268}
269
Albert Aribaude91d7d32010-07-12 22:24:28 +0200270static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530271{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200272 struct mvgbe_winparam win_param;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530273 int i;
274
275 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
276 /* Set access parameters for DRAM bank i */
277 win_param.win = i; /* Use Ethernet window i */
278 /* Window target - DDR */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200279 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530280 /* Enable full access */
281 win_param.access_ctrl = EWIN_ACCESS_FULL;
282 win_param.high_addr = 0;
Albert Aribauda7564072010-07-05 20:15:25 +0200283 /* Get bank base and size */
284 win_param.base_addr = gd->bd->bi_dram[i].start;
285 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530286 if (win_param.size == 0)
287 win_param.enable = 0;
288 else
289 win_param.enable = 1; /* Enable the access */
290
291 /* Enable DRAM bank */
292 switch (i) {
293 case 0:
294 win_param.attrib = EBAR_DRAM_CS0;
295 break;
296 case 1:
297 win_param.attrib = EBAR_DRAM_CS1;
298 break;
299 case 2:
300 win_param.attrib = EBAR_DRAM_CS2;
301 break;
302 case 3:
303 win_param.attrib = EBAR_DRAM_CS3;
304 break;
305 default:
Albert Aribauda7564072010-07-05 20:15:25 +0200306 /* invalid bank, disable access */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530307 win_param.enable = 0;
308 win_param.attrib = 0;
309 break;
310 }
311 /* Set the access control for address window(EPAPR) RD/WR */
312 set_access_control(regs, &win_param);
313 }
314}
315
316/*
317 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
318 *
319 * Go through all the DA filter tables (Unicast, Special Multicast & Other
320 * Multicast) and set each entry to 0.
321 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200322static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530323{
324 int table_index;
325
326 /* Clear DA filter unicast table (Ex_dFUT) */
327 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200328 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530329
330 for (table_index = 0; table_index < 64; ++table_index) {
331 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200332 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530333 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200334 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530335 }
336}
337
338/*
339 * port_uc_addr - This function Set the port unicast address table
340 *
341 * This function locates the proper entry in the Unicast table for the
342 * specified MAC nibble and sets its properties according to function
343 * parameters.
344 * This function add/removes MAC addresses from the port unicast address
345 * table.
346 *
347 * @uc_nibble Unicast MAC Address last nibble.
348 * @option 0 = Add, 1 = remove address.
349 *
350 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
351 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200352static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530353 int option)
354{
355 u32 unicast_reg;
356 u32 tbl_offset;
357 u32 reg_offset;
358
359 /* Locate the Unicast table entry */
360 uc_nibble = (0xf & uc_nibble);
361 /* Register offset from unicast table base */
362 tbl_offset = (uc_nibble / 4);
363 /* Entry offset within the above register */
364 reg_offset = uc_nibble % 4;
365
366 switch (option) {
367 case REJECT_MAC_ADDR:
368 /*
369 * Clear accepts frame bit at specified unicast
370 * DA table entry
371 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200372 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530373 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200374 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530375 break;
376 case ACCEPT_MAC_ADDR:
377 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200378 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530379 unicast_reg &= (0xFF << (8 * reg_offset));
380 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200381 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530382 break;
383 default:
384 return 0;
385 }
386 return 1;
387}
388
389/*
390 * port_uc_addr_set - This function Set the port Unicast address.
391 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200392static void port_uc_addr_set(struct mvgbe_registers *regs, u8 * p_addr)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530393{
394 u32 mac_h;
395 u32 mac_l;
396
397 mac_l = (p_addr[4] << 8) | (p_addr[5]);
398 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
399 (p_addr[3] << 0);
400
Albert Aribaude91d7d32010-07-12 22:24:28 +0200401 MVGBE_REG_WR(regs->macal, mac_l);
402 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530403
404 /* Accept frames of this address */
405 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
406}
407
408/*
Albert Aribaude91d7d32010-07-12 22:24:28 +0200409 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530410 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200411static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530412{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200413 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530414 int i;
415
416 /* initialize the Rx descriptors ring */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200417 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530418 for (i = 0; i < RINGSZ; i++) {
419 p_rx_desc->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200420 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530421 p_rx_desc->buf_size = PKTSIZE_ALIGN;
422 p_rx_desc->byte_cnt = 0;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200423 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530424 if (i == (RINGSZ - 1))
Albert Aribaude91d7d32010-07-12 22:24:28 +0200425 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530426 else {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200427 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
428 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530429 p_rx_desc = p_rx_desc->nxtdesc_p;
430 }
431 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200432 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530433}
434
Albert Aribaude91d7d32010-07-12 22:24:28 +0200435static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530436{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200437 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
438 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar9841dec2009-09-09 15:59:19 +0530439#if (defined (CONFIG_MII) || defined (CONFIG_CMD_MII)) \
440 && defined (CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200441 int i;
Prafulla Wadaskar9841dec2009-09-09 15:59:19 +0530442#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530443 /* setup RX rings */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200444 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530445
446 /* Clear the ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200447 MVGBE_REG_WR(regs->ic, 0);
448 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530449 /* Unmask RX buffer and TX end interrupt */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200450 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530451 /* Unmask phy and link status changes interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200452 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530453
454 set_dram_access(regs);
455 port_init_mac_tables(regs);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200456 port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530457
458 /* Assign port configuration and command. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200459 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
460 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
461 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530462
463 /* Assign port SDMA configuration */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200464 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
465 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
466 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
467 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530468 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200469 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530470
471 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200472 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
473 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530474
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530475 /* Enable port initially */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200476 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530477
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530478 /*
479 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
480 * disable the leaky bucket mechanism .
481 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200482 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530483
484 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200485 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200486 /* ensure previous write is done before enabling Rx DMA */
487 isb();
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530488 /* Enable port Rx. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200489 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530490
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100491#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
492 !defined(CONFIG_PHYLIB) && \
493 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200494 /* Wait up to 5s for the link status */
495 for (i = 0; i < 5; i++) {
496 u16 phyadr;
497
Albert Aribaude91d7d32010-07-12 22:24:28 +0200498 miiphy_read(dev->name, MV_PHY_ADR_REQUEST,
499 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200500 /* Return if we get link up */
501 if (miiphy_link(dev->name, phyadr))
502 return 0;
503 udelay(1000000);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530504 }
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200505
506 printf("No link on %s\n", dev->name);
507 return -1;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530508#endif
509 return 0;
510}
511
Albert Aribaude91d7d32010-07-12 22:24:28 +0200512static int mvgbe_halt(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530513{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200514 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
515 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530516
517 /* Disable all gigE address decoder */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200518 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530519
520 stop_queue(&regs->tqc);
521 stop_queue(&regs->rqc);
522
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530523 /* Disable port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200524 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530525 /* Set port is not reset */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200526 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530527#ifdef CONFIG_SYS_MII_MODE
528 /* Set MMI interface up */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200529 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530530#endif
531 /* Disable & mask ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200532 MVGBE_REG_WR(regs->ic, 0);
533 MVGBE_REG_WR(regs->ice, 0);
534 MVGBE_REG_WR(regs->pim, 0);
535 MVGBE_REG_WR(regs->peim, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530536
537 return 0;
538}
539
Albert Aribaude91d7d32010-07-12 22:24:28 +0200540static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530541{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200542 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
543 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530544
545 /* Programs net device MAC address after initialization */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200546 port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530547 return 0;
548}
549
Joe Hershbergere4e04882012-05-22 18:36:19 +0000550static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530551{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200552 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
553 struct mvgbe_registers *regs = dmvgbe->regs;
554 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200555 void *p = (void *)dataptr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200556 u32 cmd_sts;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000557 u32 txuq0_reg_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530558
Simon Kagstrome9220b32009-08-20 10:14:11 +0200559 /* Copy buffer if it's misaligned */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530560 if ((u32) dataptr & 0x07) {
Simon Kagstrome9220b32009-08-20 10:14:11 +0200561 if (datasize > PKTSIZE_ALIGN) {
562 printf("Non-aligned data too large (%d)\n",
563 datasize);
564 return -1;
565 }
566
Albert Aribaude91d7d32010-07-12 22:24:28 +0200567 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
568 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530569 }
Simon Kagstrome9220b32009-08-20 10:14:11 +0200570
Albert Aribaude91d7d32010-07-12 22:24:28 +0200571 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
572 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
573 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
574 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200575 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530576 p_txdesc->byte_cnt = datasize;
577
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200578 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000579 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
580 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200581
582 /* ensure tx desc writes above are performed before we start Tx DMA */
583 isb();
584
585 /* Apply send command using zeroth TXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200586 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530587
588 /*
589 * wait for packet xmit completion
590 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200591 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200592 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530593 /* return fail if error is detected */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200594 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
595 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
596 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530597 printf("Err..(%s) in xmit packet\n", __FUNCTION__);
598 return -1;
599 }
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200600 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530601 };
602 return 0;
603}
604
Albert Aribaude91d7d32010-07-12 22:24:28 +0200605static int mvgbe_recv(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530606{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200607 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
608 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200609 u32 cmd_sts;
610 u32 timeout = 0;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000611 u32 rxdesc_curr_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530612
613 /* wait untill rx packet available or timeout */
614 do {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200615 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530616 timeout++;
617 else {
618 debug("%s time out...\n", __FUNCTION__);
619 return -1;
620 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200621 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530622
623 if (p_rxdesc_curr->byte_cnt != 0) {
624 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
625 __FUNCTION__, (u32) p_rxdesc_curr->byte_cnt,
626 (u32) p_rxdesc_curr->buf_ptr,
627 (u32) p_rxdesc_curr->cmd_sts);
628 }
629
630 /*
631 * In case received a packet without first/last bits on
632 * OR the error summary bit is on,
633 * the packets needs to be dropeed.
634 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200635 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
636
637 if ((cmd_sts &
Albert Aribaude91d7d32010-07-12 22:24:28 +0200638 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
639 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530640
641 printf("Err..(%s) Dropping packet spread on"
642 " multiple descriptors\n", __FUNCTION__);
643
Albert Aribaude91d7d32010-07-12 22:24:28 +0200644 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530645
646 printf("Err..(%s) Dropping packet with errors\n",
647 __FUNCTION__);
648
649 } else {
650 /* !!! call higher layer processing */
651 debug("%s: Sending Received packet to"
652 " upper layer (NetReceive)\n", __FUNCTION__);
653
654 /* let the upper layer handle the packet */
655 NetReceive((p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET),
656 (int)(p_rxdesc_curr->byte_cnt - RX_BUF_OFFSET));
657 }
658 /*
659 * free these descriptors and point next in the ring
660 */
661 p_rxdesc_curr->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200662 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530663 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
664 p_rxdesc_curr->byte_cnt = 0;
665
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000666 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
667 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200668
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530669 return 0;
670}
671
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100672#if defined(CONFIG_PHYLIB)
673int mvgbe_phylib_init(struct eth_device *dev, int phyid)
674{
675 struct mii_dev *bus;
676 struct phy_device *phydev;
677 int ret;
678
679 bus = mdio_alloc();
680 if (!bus) {
681 printf("mdio_alloc failed\n");
682 return -ENOMEM;
683 }
684 bus->read = mvgbe_phy_read;
685 bus->write = mvgbe_phy_write;
686 sprintf(bus->name, dev->name);
687
688 ret = mdio_register(bus);
689 if (ret) {
690 printf("mdio_register failed\n");
691 free(bus);
692 return -ENOMEM;
693 }
694
695 /* Set phy address of the port */
696 mvgbe_phy_write(bus, MV_PHY_ADR_REQUEST, 0, MV_PHY_ADR_REQUEST, phyid);
697
698 phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_RGMII);
699 if (!phydev) {
700 printf("phy_connect failed\n");
701 return -ENODEV;
702 }
703
704 phy_config(phydev);
705 phy_startup(phydev);
706
707 return 0;
708}
709#endif
710
Albert Aribaude91d7d32010-07-12 22:24:28 +0200711int mvgbe_initialize(bd_t *bis)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530712{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200713 struct mvgbe_device *dmvgbe;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530714 struct eth_device *dev;
715 int devnum;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200716 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530717
Albert Aribaude91d7d32010-07-12 22:24:28 +0200718 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530719 /*skip if port is configured not to use */
720 if (used_ports[devnum] == 0)
721 continue;
722
Albert Aribaude91d7d32010-07-12 22:24:28 +0200723 dmvgbe = malloc(sizeof(struct mvgbe_device));
724
725 if (!dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530726 goto error1;
727
Albert Aribaude91d7d32010-07-12 22:24:28 +0200728 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530729
Albert Aribaude91d7d32010-07-12 22:24:28 +0200730 dmvgbe->p_rxdesc =
731 (struct mvgbe_rxdesc *)memalign(PKTALIGN,
732 MV_RXQ_DESC_ALIGNED_SIZE*RINGSZ + 1);
733
734 if (!dmvgbe->p_rxdesc)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530735 goto error2;
736
Albert Aribaude91d7d32010-07-12 22:24:28 +0200737 dmvgbe->p_rxbuf = (u8 *) memalign(PKTALIGN,
738 RINGSZ*PKTSIZE_ALIGN + 1);
739
740 if (!dmvgbe->p_rxbuf)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530741 goto error3;
742
Albert Aribaude91d7d32010-07-12 22:24:28 +0200743 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
744
745 if (!dmvgbe->p_aligned_txbuf)
Simon Kagstrome9220b32009-08-20 10:14:11 +0200746 goto error4;
747
Albert Aribaude91d7d32010-07-12 22:24:28 +0200748 dmvgbe->p_txdesc = (struct mvgbe_txdesc *) memalign(
749 PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
750
751 if (!dmvgbe->p_txdesc) {
752 free(dmvgbe->p_aligned_txbuf);
753error4:
754 free(dmvgbe->p_rxbuf);
755error3:
756 free(dmvgbe->p_rxdesc);
757error2:
758 free(dmvgbe);
759error1:
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530760 printf("Err.. %s Failed to allocate memory\n",
761 __FUNCTION__);
762 return -1;
763 }
764
Albert Aribaude91d7d32010-07-12 22:24:28 +0200765 dev = &dmvgbe->dev;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530766
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000767 /* must be less than sizeof(dev->name) */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530768 sprintf(dev->name, "egiga%d", devnum);
769
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530770 switch (devnum) {
771 case 0:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200772 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530773 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200774#if defined(MVGBE1_BASE)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530775 case 1:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200776 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530777 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200778#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530779 default: /* this should never happen */
780 printf("Err..(%s) Invalid device number %d\n",
781 __FUNCTION__, devnum);
782 return -1;
783 }
784
Albert Aribaude91d7d32010-07-12 22:24:28 +0200785 dev->init = (void *)mvgbe_init;
786 dev->halt = (void *)mvgbe_halt;
787 dev->send = (void *)mvgbe_send;
788 dev->recv = (void *)mvgbe_recv;
789 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530790
791 eth_register(dev);
792
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100793#if defined(CONFIG_PHYLIB)
794 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
795#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530796 miiphy_register(dev->name, smi_reg_read, smi_reg_write);
797 /* Set phy address of the port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200798 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
799 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530800#endif
801 }
802 return 0;
Prafulla Wadaskar12618ef2009-07-01 20:34:51 +0200803}