blob: e6585ef8b3e72643d45bdfbaa87bd3c959f15721 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar91315892009-06-14 22:33:46 +05302/*
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 Wadaskar91315892009-06-14 22:33:46 +053012 */
13
14#include <common.h>
15#include <net.h>
16#include <malloc.h>
17#include <miiphy.h>
Chris Packham5194ed72018-06-09 20:46:16 +120018#include <wait_bit.h>
Lei Wena7efd712011-10-18 20:11:42 +053019#include <asm/io.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090020#include <linux/errno.h>
Prafulla Wadaskar91315892009-06-14 22:33:46 +053021#include <asm/types.h>
Lei Wena7efd712011-10-18 20:11:42 +053022#include <asm/system.h>
Prafulla Wadaskar91315892009-06-14 22:33:46 +053023#include <asm/byteorder.h>
Anatolij Gustschin36aaa912011-10-29 10:09:22 +000024#include <asm/arch/cpu.h>
Albert Aribaudd44265a2010-07-12 22:24:28 +020025
26#if defined(CONFIG_KIRKWOOD)
Stefan Roese3dc23f72014-10-22 12:13:06 +020027#include <asm/arch/soc.h>
Albert Aribaudd3c9ffd2010-07-12 22:24:29 +020028#elif defined(CONFIG_ORION5X)
29#include <asm/arch/orion5x.h>
Albert Aribaudd44265a2010-07-12 22:24:28 +020030#endif
31
Albert Aribaud9b6bcdc2010-07-12 22:24:27 +020032#include "mvgbe.h"
Prafulla Wadaskar91315892009-06-14 22:33:46 +053033
Albert Aribaud49fa6ed2010-07-05 20:15:25 +020034DECLARE_GLOBAL_DATA_PTR;
35
Luka Perkov5aa22972013-11-11 07:27:53 +010036#ifndef CONFIG_MVGBE_PORTS
37# define CONFIG_MVGBE_PORTS {0, 0}
38#endif
39
Albert Aribaudd44265a2010-07-12 22:24:28 +020040#define MV_PHY_ADR_REQUEST 0xee
41#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Simon Kagstrombb1ca3b2009-08-20 10:12:28 +020042
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +010043#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packham5194ed72018-06-09 20:46:16 +120044static int smi_wait_ready(struct mvgbe_device *dmvgbe)
45{
46 int ret;
47
48 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
49 MVGBE_PHY_SMI_TIMEOUT_MS, false);
50 if (ret) {
51 printf("Error: SMI busy timeout\n");
52 return ret;
53 }
54
55 return 0;
56}
57
Prafulla Wadaskar91315892009-06-14 22:33:46 +053058/*
59 * smi_reg_read - miiphy_read callback function.
60 *
Chris Packham5194ed72018-06-09 20:46:16 +120061 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskar91315892009-06-14 22:33:46 +053062 */
Joe Hershberger5a49f172016-08-08 11:28:38 -050063static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
64 int reg_ofs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +053065{
Joe Hershberger5a49f172016-08-08 11:28:38 -050066 u16 data = 0;
67 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaudd44265a2010-07-12 22:24:28 +020068 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
69 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053070 u32 smi_reg;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +020071 u32 timeout;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053072
73 /* Phyadr read request */
Albert Aribaudd44265a2010-07-12 22:24:28 +020074 if (phy_adr == MV_PHY_ADR_REQUEST &&
75 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +053076 /* */
Joe Hershberger5a49f172016-08-08 11:28:38 -050077 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
78 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053079 }
80 /* check parameters */
81 if (phy_adr > PHYADR_MASK) {
82 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050083 __func__, phy_adr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053084 return -EFAULT;
85 }
86 if (reg_ofs > PHYREG_MASK) {
87 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050088 __func__, reg_ofs);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053089 return -EFAULT;
90 }
91
Prafulla Wadaskar91315892009-06-14 22:33:46 +053092 /* wait till the SMI is not busy */
Chris Packham5194ed72018-06-09 20:46:16 +120093 if (smi_wait_ready(dmvgbe) < 0)
94 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053095
96 /* fill the phy address and regiser offset and read opcode */
Albert Aribaudd44265a2010-07-12 22:24:28 +020097 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
98 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
99 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530100
101 /* write the smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200102 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530103
104 /*wait till read value is ready */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200105 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530106
107 do {
108 /* read smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200109 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530110 if (timeout-- == 0) {
111 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500112 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530113 return -EFAULT;
114 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200115 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530116
117 /* Wait for the data to update in the SMI register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200118 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
119 ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530120
Joe Hershberger5a49f172016-08-08 11:28:38 -0500121 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530122
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500123 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger5a49f172016-08-08 11:28:38 -0500124 data);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530125
Joe Hershberger5a49f172016-08-08 11:28:38 -0500126 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530127}
128
129/*
Chris Packham5194ed72018-06-09 20:46:16 +1200130 * smi_reg_write - miiphy_write callback function.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530131 *
Chris Packham5194ed72018-06-09 20:46:16 +1200132 * Returns 0 if write succeed, -EFAULT on error
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530133 */
Joe Hershberger5a49f172016-08-08 11:28:38 -0500134static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
135 int reg_ofs, u16 data)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530136{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500137 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200138 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
139 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530140 u32 smi_reg;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530141
142 /* Phyadr write request*/
Albert Aribaudd44265a2010-07-12 22:24:28 +0200143 if (phy_adr == MV_PHY_ADR_REQUEST &&
144 reg_ofs == MV_PHY_ADR_REQUEST) {
145 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530146 return 0;
147 }
148
149 /* check parameters */
150 if (phy_adr > PHYADR_MASK) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500151 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530152 return -EINVAL;
153 }
154 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500155 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packham5194ed72018-06-09 20:46:16 +1200156 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530157 }
158
159 /* wait till the SMI is not busy */
Chris Packham5194ed72018-06-09 20:46:16 +1200160 if (smi_wait_ready(dmvgbe) < 0)
161 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530162
163 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200164 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
165 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
166 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
167 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530168
169 /* write the smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200170 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530171
172 return 0;
173}
Stefan Biglercc796972012-03-26 00:02:13 +0000174#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530175
176/* Stop and checks all queues */
177static void stop_queue(u32 * qreg)
178{
179 u32 reg_data;
180
181 reg_data = readl(qreg);
182
183 if (reg_data & 0xFF) {
184 /* Issue stop command for active channels only */
185 writel((reg_data << 8), qreg);
186
187 /* Wait for all queue activity to terminate. */
188 do {
189 /*
190 * Check port cause register that all queues
191 * are stopped
192 */
193 reg_data = readl(qreg);
194 }
195 while (reg_data & 0xFF);
196 }
197}
198
199/*
200 * set_access_control - Config address decode parameters for Ethernet unit
201 *
202 * This function configures the address decode parameters for the Gigabit
203 * Ethernet Controller according the given parameters struct.
204 *
205 * @regs Register struct pointer.
206 * @param Address decode parameter struct.
207 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200208static void set_access_control(struct mvgbe_registers *regs,
209 struct mvgbe_winparam *param)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530210{
211 u32 access_prot_reg;
212
213 /* Set access control register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200214 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530215 /* clear window permission */
216 access_prot_reg &= (~(3 << (param->win * 2)));
217 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200218 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530219
220 /* Set window Size reg (SR) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200221 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530222 (((param->size / 0x10000) - 1) << 16));
223
224 /* Set window Base address reg (BA) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200225 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530226 (param->target | param->attrib | param->base_addr));
227 /* High address remap reg (HARR) */
228 if (param->win < 4)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200229 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530230
231 /* Base address enable reg (BARER) */
232 if (param->enable == 1)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200233 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530234 else
Albert Aribaudd44265a2010-07-12 22:24:28 +0200235 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530236}
237
Albert Aribaudd44265a2010-07-12 22:24:28 +0200238static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530239{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200240 struct mvgbe_winparam win_param;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530241 int i;
242
243 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
244 /* Set access parameters for DRAM bank i */
245 win_param.win = i; /* Use Ethernet window i */
246 /* Window target - DDR */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200247 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530248 /* Enable full access */
249 win_param.access_ctrl = EWIN_ACCESS_FULL;
250 win_param.high_addr = 0;
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200251 /* Get bank base and size */
252 win_param.base_addr = gd->bd->bi_dram[i].start;
253 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530254 if (win_param.size == 0)
255 win_param.enable = 0;
256 else
257 win_param.enable = 1; /* Enable the access */
258
259 /* Enable DRAM bank */
260 switch (i) {
261 case 0:
262 win_param.attrib = EBAR_DRAM_CS0;
263 break;
264 case 1:
265 win_param.attrib = EBAR_DRAM_CS1;
266 break;
267 case 2:
268 win_param.attrib = EBAR_DRAM_CS2;
269 break;
270 case 3:
271 win_param.attrib = EBAR_DRAM_CS3;
272 break;
273 default:
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200274 /* invalid bank, disable access */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530275 win_param.enable = 0;
276 win_param.attrib = 0;
277 break;
278 }
279 /* Set the access control for address window(EPAPR) RD/WR */
280 set_access_control(regs, &win_param);
281 }
282}
283
284/*
285 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
286 *
287 * Go through all the DA filter tables (Unicast, Special Multicast & Other
288 * Multicast) and set each entry to 0.
289 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200290static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530291{
292 int table_index;
293
294 /* Clear DA filter unicast table (Ex_dFUT) */
295 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200296 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530297
298 for (table_index = 0; table_index < 64; ++table_index) {
299 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200300 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530301 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200302 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530303 }
304}
305
306/*
307 * port_uc_addr - This function Set the port unicast address table
308 *
309 * This function locates the proper entry in the Unicast table for the
310 * specified MAC nibble and sets its properties according to function
311 * parameters.
312 * This function add/removes MAC addresses from the port unicast address
313 * table.
314 *
315 * @uc_nibble Unicast MAC Address last nibble.
316 * @option 0 = Add, 1 = remove address.
317 *
318 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
319 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200320static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530321 int option)
322{
323 u32 unicast_reg;
324 u32 tbl_offset;
325 u32 reg_offset;
326
327 /* Locate the Unicast table entry */
328 uc_nibble = (0xf & uc_nibble);
329 /* Register offset from unicast table base */
330 tbl_offset = (uc_nibble / 4);
331 /* Entry offset within the above register */
332 reg_offset = uc_nibble % 4;
333
334 switch (option) {
335 case REJECT_MAC_ADDR:
336 /*
337 * Clear accepts frame bit at specified unicast
338 * DA table entry
339 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200340 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530341 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200342 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530343 break;
344 case ACCEPT_MAC_ADDR:
345 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200346 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530347 unicast_reg &= (0xFF << (8 * reg_offset));
348 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200349 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530350 break;
351 default:
352 return 0;
353 }
354 return 1;
355}
356
357/*
358 * port_uc_addr_set - This function Set the port Unicast address.
359 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200360static void port_uc_addr_set(struct mvgbe_registers *regs, u8 * p_addr)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530361{
362 u32 mac_h;
363 u32 mac_l;
364
365 mac_l = (p_addr[4] << 8) | (p_addr[5]);
366 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
367 (p_addr[3] << 0);
368
Albert Aribaudd44265a2010-07-12 22:24:28 +0200369 MVGBE_REG_WR(regs->macal, mac_l);
370 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530371
372 /* Accept frames of this address */
373 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
374}
375
376/*
Albert Aribaudd44265a2010-07-12 22:24:28 +0200377 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530378 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200379static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530380{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200381 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530382 int i;
383
384 /* initialize the Rx descriptors ring */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200385 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530386 for (i = 0; i < RINGSZ; i++) {
387 p_rx_desc->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200388 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530389 p_rx_desc->buf_size = PKTSIZE_ALIGN;
390 p_rx_desc->byte_cnt = 0;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200391 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530392 if (i == (RINGSZ - 1))
Albert Aribaudd44265a2010-07-12 22:24:28 +0200393 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530394 else {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200395 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
396 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530397 p_rx_desc = p_rx_desc->nxtdesc_p;
398 }
399 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200400 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530401}
402
Albert Aribaudd44265a2010-07-12 22:24:28 +0200403static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530404{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200405 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
406 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0611c602013-08-11 17:08:23 +0200407#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
408 !defined(CONFIG_PHYLIB) && \
409 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200410 int i;
Prafulla Wadaskaraba82372009-09-09 15:59:19 +0530411#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530412 /* setup RX rings */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200413 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530414
415 /* Clear the ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200416 MVGBE_REG_WR(regs->ic, 0);
417 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530418 /* Unmask RX buffer and TX end interrupt */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200419 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530420 /* Unmask phy and link status changes interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200421 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530422
423 set_dram_access(regs);
424 port_init_mac_tables(regs);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200425 port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530426
427 /* Assign port configuration and command. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200428 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
429 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
430 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530431
432 /* Assign port SDMA configuration */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200433 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
434 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
435 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
436 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530437 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200438 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530439
440 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200441 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
442 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530443
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530444 /* Enable port initially */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200445 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530446
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530447 /*
448 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
449 * disable the leaky bucket mechanism .
450 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200451 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530452
453 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200454 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200455 /* ensure previous write is done before enabling Rx DMA */
456 isb();
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530457 /* Enable port Rx. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200458 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530459
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100460#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
461 !defined(CONFIG_PHYLIB) && \
462 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200463 /* Wait up to 5s for the link status */
464 for (i = 0; i < 5; i++) {
465 u16 phyadr;
466
Albert Aribaudd44265a2010-07-12 22:24:28 +0200467 miiphy_read(dev->name, MV_PHY_ADR_REQUEST,
468 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstromcad713b2009-08-20 10:13:06 +0200469 /* Return if we get link up */
470 if (miiphy_link(dev->name, phyadr))
471 return 0;
472 udelay(1000000);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530473 }
Simon Kagstromcad713b2009-08-20 10:13:06 +0200474
475 printf("No link on %s\n", dev->name);
476 return -1;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530477#endif
478 return 0;
479}
480
Albert Aribaudd44265a2010-07-12 22:24:28 +0200481static int mvgbe_halt(struct eth_device *dev)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530482{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200483 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
484 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530485
486 /* Disable all gigE address decoder */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200487 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530488
489 stop_queue(&regs->tqc);
490 stop_queue(&regs->rqc);
491
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530492 /* Disable port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200493 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530494 /* Set port is not reset */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200495 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530496#ifdef CONFIG_SYS_MII_MODE
497 /* Set MMI interface up */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200498 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530499#endif
500 /* Disable & mask ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200501 MVGBE_REG_WR(regs->ic, 0);
502 MVGBE_REG_WR(regs->ice, 0);
503 MVGBE_REG_WR(regs->pim, 0);
504 MVGBE_REG_WR(regs->peim, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530505
506 return 0;
507}
508
Albert Aribaudd44265a2010-07-12 22:24:28 +0200509static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530510{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200511 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
512 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530513
514 /* Programs net device MAC address after initialization */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200515 port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530516 return 0;
517}
518
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000519static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530520{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200521 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
522 struct mvgbe_registers *regs = dmvgbe->regs;
523 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200524 void *p = (void *)dataptr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200525 u32 cmd_sts;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000526 u32 txuq0_reg_addr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530527
Simon Kagstrom477fa632009-08-20 10:14:11 +0200528 /* Copy buffer if it's misaligned */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530529 if ((u32) dataptr & 0x07) {
Simon Kagstrom477fa632009-08-20 10:14:11 +0200530 if (datasize > PKTSIZE_ALIGN) {
531 printf("Non-aligned data too large (%d)\n",
532 datasize);
533 return -1;
534 }
535
Albert Aribaudd44265a2010-07-12 22:24:28 +0200536 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
537 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530538 }
Simon Kagstrom477fa632009-08-20 10:14:11 +0200539
Albert Aribaudd44265a2010-07-12 22:24:28 +0200540 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
541 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
542 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
543 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200544 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530545 p_txdesc->byte_cnt = datasize;
546
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200547 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000548 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
549 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200550
551 /* ensure tx desc writes above are performed before we start Tx DMA */
552 isb();
553
554 /* Apply send command using zeroth TXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200555 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530556
557 /*
558 * wait for packet xmit completion
559 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200560 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200561 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530562 /* return fail if error is detected */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200563 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
564 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
565 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500566 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530567 return -1;
568 }
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200569 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530570 };
571 return 0;
572}
573
Albert Aribaudd44265a2010-07-12 22:24:28 +0200574static int mvgbe_recv(struct eth_device *dev)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530575{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200576 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
577 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200578 u32 cmd_sts;
579 u32 timeout = 0;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000580 u32 rxdesc_curr_addr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530581
582 /* wait untill rx packet available or timeout */
583 do {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200584 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530585 timeout++;
586 else {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500587 debug("%s time out...\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530588 return -1;
589 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200590 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530591
592 if (p_rxdesc_curr->byte_cnt != 0) {
593 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500594 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530595 (u32) p_rxdesc_curr->buf_ptr,
596 (u32) p_rxdesc_curr->cmd_sts);
597 }
598
599 /*
600 * In case received a packet without first/last bits on
601 * OR the error summary bit is on,
602 * the packets needs to be dropeed.
603 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200604 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
605
606 if ((cmd_sts &
Albert Aribaudd44265a2010-07-12 22:24:28 +0200607 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
608 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530609
610 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500611 " multiple descriptors\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530612
Albert Aribaudd44265a2010-07-12 22:24:28 +0200613 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530614
615 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500616 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530617
618 } else {
619 /* !!! call higher layer processing */
620 debug("%s: Sending Received packet to"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500621 " upper layer (net_process_received_packet)\n",
622 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530623
624 /* let the upper layer handle the packet */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500625 net_process_received_packet((p_rxdesc_curr->buf_ptr +
626 RX_BUF_OFFSET),
627 (int)(p_rxdesc_curr->byte_cnt -
628 RX_BUF_OFFSET));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530629 }
630 /*
631 * free these descriptors and point next in the ring
632 */
633 p_rxdesc_curr->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200634 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530635 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
636 p_rxdesc_curr->byte_cnt = 0;
637
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000638 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
639 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200640
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530641 return 0;
642}
643
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100644#if defined(CONFIG_PHYLIB)
645int mvgbe_phylib_init(struct eth_device *dev, int phyid)
646{
647 struct mii_dev *bus;
648 struct phy_device *phydev;
649 int ret;
650
651 bus = mdio_alloc();
652 if (!bus) {
653 printf("mdio_alloc failed\n");
654 return -ENOMEM;
655 }
Chris Packham6ecf9e22016-11-01 10:48:32 +1300656 bus->read = smi_reg_read;
657 bus->write = smi_reg_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000658 strcpy(bus->name, dev->name);
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100659
660 ret = mdio_register(bus);
661 if (ret) {
662 printf("mdio_register failed\n");
663 free(bus);
664 return -ENOMEM;
665 }
666
667 /* Set phy address of the port */
Chris Packham6ecf9e22016-11-01 10:48:32 +1300668 smi_reg_write(bus, MV_PHY_ADR_REQUEST, 0, MV_PHY_ADR_REQUEST, phyid);
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100669
670 phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_RGMII);
671 if (!phydev) {
672 printf("phy_connect failed\n");
673 return -ENODEV;
674 }
675
676 phy_config(phydev);
677 phy_startup(phydev);
678
679 return 0;
680}
681#endif
682
Albert Aribaudd44265a2010-07-12 22:24:28 +0200683int mvgbe_initialize(bd_t *bis)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530684{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200685 struct mvgbe_device *dmvgbe;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530686 struct eth_device *dev;
687 int devnum;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200688 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530689
Albert Aribaudd44265a2010-07-12 22:24:28 +0200690 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530691 /*skip if port is configured not to use */
692 if (used_ports[devnum] == 0)
693 continue;
694
Albert Aribaudd44265a2010-07-12 22:24:28 +0200695 dmvgbe = malloc(sizeof(struct mvgbe_device));
696
697 if (!dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530698 goto error1;
699
Albert Aribaudd44265a2010-07-12 22:24:28 +0200700 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530701
Albert Aribaudd44265a2010-07-12 22:24:28 +0200702 dmvgbe->p_rxdesc =
703 (struct mvgbe_rxdesc *)memalign(PKTALIGN,
704 MV_RXQ_DESC_ALIGNED_SIZE*RINGSZ + 1);
705
706 if (!dmvgbe->p_rxdesc)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530707 goto error2;
708
Albert Aribaudd44265a2010-07-12 22:24:28 +0200709 dmvgbe->p_rxbuf = (u8 *) memalign(PKTALIGN,
710 RINGSZ*PKTSIZE_ALIGN + 1);
711
712 if (!dmvgbe->p_rxbuf)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530713 goto error3;
714
Albert Aribaudd44265a2010-07-12 22:24:28 +0200715 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
716
717 if (!dmvgbe->p_aligned_txbuf)
Simon Kagstrom477fa632009-08-20 10:14:11 +0200718 goto error4;
719
Albert Aribaudd44265a2010-07-12 22:24:28 +0200720 dmvgbe->p_txdesc = (struct mvgbe_txdesc *) memalign(
721 PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
722
723 if (!dmvgbe->p_txdesc) {
724 free(dmvgbe->p_aligned_txbuf);
725error4:
726 free(dmvgbe->p_rxbuf);
727error3:
728 free(dmvgbe->p_rxdesc);
729error2:
730 free(dmvgbe);
731error1:
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530732 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500733 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530734 return -1;
735 }
736
Albert Aribaudd44265a2010-07-12 22:24:28 +0200737 dev = &dmvgbe->dev;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530738
Mike Frysingerf6add132011-11-10 14:11:04 +0000739 /* must be less than sizeof(dev->name) */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530740 sprintf(dev->name, "egiga%d", devnum);
741
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530742 switch (devnum) {
743 case 0:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200744 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530745 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200746#if defined(MVGBE1_BASE)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530747 case 1:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200748 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530749 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200750#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530751 default: /* this should never happen */
752 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500753 __func__, devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530754 return -1;
755 }
756
Albert Aribaudd44265a2010-07-12 22:24:28 +0200757 dev->init = (void *)mvgbe_init;
758 dev->halt = (void *)mvgbe_halt;
759 dev->send = (void *)mvgbe_send;
760 dev->recv = (void *)mvgbe_recv;
761 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530762
763 eth_register(dev);
764
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100765#if defined(CONFIG_PHYLIB)
766 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
767#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500768 int retval;
769 struct mii_dev *mdiodev = mdio_alloc();
770 if (!mdiodev)
771 return -ENOMEM;
772 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
773 mdiodev->read = smi_reg_read;
774 mdiodev->write = smi_reg_write;
775
776 retval = mdio_register(mdiodev);
777 if (retval < 0)
778 return retval;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530779 /* Set phy address of the port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200780 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
781 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530782#endif
783 }
784 return 0;
Prafulla Wadaskar0b785dd2009-07-01 20:34:51 +0200785}