blob: aad1d279915f7dc1988ee9790f13cc65f8df62b0 [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
Chris Packhame9bf75c2018-07-09 21:33:59 +120058static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
59 int devad, int reg_ofs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +053060{
Albert Aribaudd44265a2010-07-12 22:24:28 +020061 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053062 u32 smi_reg;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +020063 u32 timeout;
Chris Packhame9bf75c2018-07-09 21:33:59 +120064 u16 data = 0;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053065
66 /* Phyadr read request */
Albert Aribaudd44265a2010-07-12 22:24:28 +020067 if (phy_adr == MV_PHY_ADR_REQUEST &&
68 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +053069 /* */
Joe Hershberger5a49f172016-08-08 11:28:38 -050070 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
71 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053072 }
73 /* check parameters */
74 if (phy_adr > PHYADR_MASK) {
75 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050076 __func__, phy_adr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053077 return -EFAULT;
78 }
79 if (reg_ofs > PHYREG_MASK) {
80 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -050081 __func__, reg_ofs);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053082 return -EFAULT;
83 }
84
Prafulla Wadaskar91315892009-06-14 22:33:46 +053085 /* wait till the SMI is not busy */
Chris Packham5194ed72018-06-09 20:46:16 +120086 if (smi_wait_ready(dmvgbe) < 0)
87 return -EFAULT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053088
89 /* fill the phy address and regiser offset and read opcode */
Albert Aribaudd44265a2010-07-12 22:24:28 +020090 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
91 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
92 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053093
94 /* write the smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +020095 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +053096
97 /*wait till read value is ready */
Albert Aribaudd44265a2010-07-12 22:24:28 +020098 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +053099
100 do {
101 /* read smi register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200102 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530103 if (timeout-- == 0) {
104 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500105 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530106 return -EFAULT;
107 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200108 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530109
110 /* Wait for the data to update in the SMI register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200111 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
112 ;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530113
Joe Hershberger5a49f172016-08-08 11:28:38 -0500114 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530115
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500116 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger5a49f172016-08-08 11:28:38 -0500117 data);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530118
Joe Hershberger5a49f172016-08-08 11:28:38 -0500119 return data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530120}
121
122/*
Chris Packhame9bf75c2018-07-09 21:33:59 +1200123 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530124 *
Chris Packhame9bf75c2018-07-09 21:33:59 +1200125 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530126 */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200127static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
128 int reg_ofs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530129{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500130 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200131 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200132
133 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
134}
135
136static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
137 int devad, int reg_ofs, u16 data)
138{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200139 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}
Chris Packhame9bf75c2018-07-09 21:33:59 +1200174
175/*
176 * smi_reg_write - miiphy_write callback function.
177 *
178 * Returns 0 if write succeed, -EFAULT on error
179 */
180static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
181 int reg_ofs, u16 data)
182{
183 struct eth_device *dev = eth_get_dev_by_name(bus->name);
184 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
185
186 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
187}
Stefan Biglercc796972012-03-26 00:02:13 +0000188#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530189
190/* Stop and checks all queues */
191static void stop_queue(u32 * qreg)
192{
193 u32 reg_data;
194
195 reg_data = readl(qreg);
196
197 if (reg_data & 0xFF) {
198 /* Issue stop command for active channels only */
199 writel((reg_data << 8), qreg);
200
201 /* Wait for all queue activity to terminate. */
202 do {
203 /*
204 * Check port cause register that all queues
205 * are stopped
206 */
207 reg_data = readl(qreg);
208 }
209 while (reg_data & 0xFF);
210 }
211}
212
213/*
214 * set_access_control - Config address decode parameters for Ethernet unit
215 *
216 * This function configures the address decode parameters for the Gigabit
217 * Ethernet Controller according the given parameters struct.
218 *
219 * @regs Register struct pointer.
220 * @param Address decode parameter struct.
221 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200222static void set_access_control(struct mvgbe_registers *regs,
223 struct mvgbe_winparam *param)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530224{
225 u32 access_prot_reg;
226
227 /* Set access control register */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200228 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530229 /* clear window permission */
230 access_prot_reg &= (~(3 << (param->win * 2)));
231 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200232 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530233
234 /* Set window Size reg (SR) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200235 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530236 (((param->size / 0x10000) - 1) << 16));
237
238 /* Set window Base address reg (BA) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200239 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530240 (param->target | param->attrib | param->base_addr));
241 /* High address remap reg (HARR) */
242 if (param->win < 4)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200243 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530244
245 /* Base address enable reg (BARER) */
246 if (param->enable == 1)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200247 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530248 else
Albert Aribaudd44265a2010-07-12 22:24:28 +0200249 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530250}
251
Albert Aribaudd44265a2010-07-12 22:24:28 +0200252static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530253{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200254 struct mvgbe_winparam win_param;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530255 int i;
256
257 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
258 /* Set access parameters for DRAM bank i */
259 win_param.win = i; /* Use Ethernet window i */
260 /* Window target - DDR */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200261 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530262 /* Enable full access */
263 win_param.access_ctrl = EWIN_ACCESS_FULL;
264 win_param.high_addr = 0;
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200265 /* Get bank base and size */
266 win_param.base_addr = gd->bd->bi_dram[i].start;
267 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530268 if (win_param.size == 0)
269 win_param.enable = 0;
270 else
271 win_param.enable = 1; /* Enable the access */
272
273 /* Enable DRAM bank */
274 switch (i) {
275 case 0:
276 win_param.attrib = EBAR_DRAM_CS0;
277 break;
278 case 1:
279 win_param.attrib = EBAR_DRAM_CS1;
280 break;
281 case 2:
282 win_param.attrib = EBAR_DRAM_CS2;
283 break;
284 case 3:
285 win_param.attrib = EBAR_DRAM_CS3;
286 break;
287 default:
Albert Aribaud49fa6ed2010-07-05 20:15:25 +0200288 /* invalid bank, disable access */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530289 win_param.enable = 0;
290 win_param.attrib = 0;
291 break;
292 }
293 /* Set the access control for address window(EPAPR) RD/WR */
294 set_access_control(regs, &win_param);
295 }
296}
297
298/*
299 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
300 *
301 * Go through all the DA filter tables (Unicast, Special Multicast & Other
302 * Multicast) and set each entry to 0.
303 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200304static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530305{
306 int table_index;
307
308 /* Clear DA filter unicast table (Ex_dFUT) */
309 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaudd44265a2010-07-12 22:24:28 +0200310 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530311
312 for (table_index = 0; table_index < 64; ++table_index) {
313 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200314 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530315 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200316 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530317 }
318}
319
320/*
321 * port_uc_addr - This function Set the port unicast address table
322 *
323 * This function locates the proper entry in the Unicast table for the
324 * specified MAC nibble and sets its properties according to function
325 * parameters.
326 * This function add/removes MAC addresses from the port unicast address
327 * table.
328 *
329 * @uc_nibble Unicast MAC Address last nibble.
330 * @option 0 = Add, 1 = remove address.
331 *
332 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
333 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200334static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530335 int option)
336{
337 u32 unicast_reg;
338 u32 tbl_offset;
339 u32 reg_offset;
340
341 /* Locate the Unicast table entry */
342 uc_nibble = (0xf & uc_nibble);
343 /* Register offset from unicast table base */
344 tbl_offset = (uc_nibble / 4);
345 /* Entry offset within the above register */
346 reg_offset = uc_nibble % 4;
347
348 switch (option) {
349 case REJECT_MAC_ADDR:
350 /*
351 * Clear accepts frame bit at specified unicast
352 * DA table entry
353 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200354 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530355 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200356 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530357 break;
358 case ACCEPT_MAC_ADDR:
359 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200360 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530361 unicast_reg &= (0xFF << (8 * reg_offset));
362 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaudd44265a2010-07-12 22:24:28 +0200363 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530364 break;
365 default:
366 return 0;
367 }
368 return 1;
369}
370
371/*
372 * port_uc_addr_set - This function Set the port Unicast address.
373 */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200374static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530375{
Chris Packhame9bf75c2018-07-09 21:33:59 +1200376 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530377 u32 mac_h;
378 u32 mac_l;
379
380 mac_l = (p_addr[4] << 8) | (p_addr[5]);
381 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
382 (p_addr[3] << 0);
383
Albert Aribaudd44265a2010-07-12 22:24:28 +0200384 MVGBE_REG_WR(regs->macal, mac_l);
385 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530386
387 /* Accept frames of this address */
388 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
389}
390
391/*
Albert Aribaudd44265a2010-07-12 22:24:28 +0200392 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530393 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200394static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530395{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200396 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530397 int i;
398
399 /* initialize the Rx descriptors ring */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200400 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530401 for (i = 0; i < RINGSZ; i++) {
402 p_rx_desc->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200403 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530404 p_rx_desc->buf_size = PKTSIZE_ALIGN;
405 p_rx_desc->byte_cnt = 0;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200406 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530407 if (i == (RINGSZ - 1))
Albert Aribaudd44265a2010-07-12 22:24:28 +0200408 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530409 else {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200410 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
411 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530412 p_rx_desc = p_rx_desc->nxtdesc_p;
413 }
414 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200415 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530416}
417
Chris Packhame9bf75c2018-07-09 21:33:59 +1200418static int __mvgbe_init(struct mvgbe_device *dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530419{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200420 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0611c602013-08-11 17:08:23 +0200421#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
422 !defined(CONFIG_PHYLIB) && \
423 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200424 int i;
Prafulla Wadaskaraba82372009-09-09 15:59:19 +0530425#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530426 /* setup RX rings */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200427 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530428
429 /* Clear the ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200430 MVGBE_REG_WR(regs->ic, 0);
431 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530432 /* Unmask RX buffer and TX end interrupt */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200433 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530434 /* Unmask phy and link status changes interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200435 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530436
437 set_dram_access(regs);
438 port_init_mac_tables(regs);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200439 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530440
441 /* Assign port configuration and command. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200442 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
443 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
444 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530445
446 /* Assign port SDMA configuration */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200447 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
448 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
449 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
450 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530451 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200452 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530453
454 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200455 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
456 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530457
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530458 /* Enable port initially */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200459 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530460
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530461 /*
462 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
463 * disable the leaky bucket mechanism .
464 */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200465 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530466
467 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200468 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200469 /* ensure previous write is done before enabling Rx DMA */
470 isb();
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530471 /* Enable port Rx. */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200472 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530473
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100474#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
475 !defined(CONFIG_PHYLIB) && \
476 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstromcad713b2009-08-20 10:13:06 +0200477 /* Wait up to 5s for the link status */
478 for (i = 0; i < 5; i++) {
479 u16 phyadr;
480
Chris Packhame9bf75c2018-07-09 21:33:59 +1200481 miiphy_read(dmvgbe->dev.name, MV_PHY_ADR_REQUEST,
Albert Aribaudd44265a2010-07-12 22:24:28 +0200482 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstromcad713b2009-08-20 10:13:06 +0200483 /* Return if we get link up */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200484 if (miiphy_link(dmvgbe->dev.name, phyadr))
Simon Kagstromcad713b2009-08-20 10:13:06 +0200485 return 0;
486 udelay(1000000);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530487 }
Simon Kagstromcad713b2009-08-20 10:13:06 +0200488
Chris Packhame9bf75c2018-07-09 21:33:59 +1200489 printf("No link on %s\n", dmvgbe->dev.name);
Simon Kagstromcad713b2009-08-20 10:13:06 +0200490 return -1;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530491#endif
492 return 0;
493}
494
Chris Packhame9bf75c2018-07-09 21:33:59 +1200495static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530496{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200497 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200498
499 return __mvgbe_init(dmvgbe);
500}
501
502static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
503{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200504 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530505
506 /* Disable all gigE address decoder */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200507 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530508
509 stop_queue(&regs->tqc);
510 stop_queue(&regs->rqc);
511
Prafulla Wadaskarf0588fd2010-04-06 21:33:08 +0530512 /* Disable port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200513 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530514 /* Set port is not reset */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200515 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530516#ifdef CONFIG_SYS_MII_MODE
517 /* Set MMI interface up */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200518 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530519#endif
520 /* Disable & mask ethernet port interrupts */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200521 MVGBE_REG_WR(regs->ic, 0);
522 MVGBE_REG_WR(regs->ice, 0);
523 MVGBE_REG_WR(regs->pim, 0);
524 MVGBE_REG_WR(regs->peim, 0);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200525}
526
527static int mvgbe_halt(struct eth_device *dev)
528{
529 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
530
531 __mvgbe_halt(dmvgbe);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530532
533 return 0;
534}
535
Albert Aribaudd44265a2010-07-12 22:24:28 +0200536static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530537{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200538 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530539
540 /* Programs net device MAC address after initialization */
Chris Packhame9bf75c2018-07-09 21:33:59 +1200541 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskarb5ce63e2010-04-06 22:21:33 +0530542 return 0;
543}
544
Chris Packhame9bf75c2018-07-09 21:33:59 +1200545static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
546 int datasize)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530547{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200548 struct mvgbe_registers *regs = dmvgbe->regs;
549 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200550 void *p = (void *)dataptr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200551 u32 cmd_sts;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000552 u32 txuq0_reg_addr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530553
Simon Kagstrom477fa632009-08-20 10:14:11 +0200554 /* Copy buffer if it's misaligned */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530555 if ((u32) dataptr & 0x07) {
Simon Kagstrom477fa632009-08-20 10:14:11 +0200556 if (datasize > PKTSIZE_ALIGN) {
557 printf("Non-aligned data too large (%d)\n",
558 datasize);
559 return -1;
560 }
561
Albert Aribaudd44265a2010-07-12 22:24:28 +0200562 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
563 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530564 }
Simon Kagstrom477fa632009-08-20 10:14:11 +0200565
Albert Aribaudd44265a2010-07-12 22:24:28 +0200566 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
567 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
568 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
569 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrom477fa632009-08-20 10:14:11 +0200570 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530571 p_txdesc->byte_cnt = datasize;
572
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200573 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000574 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
575 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudc19a20d2010-07-10 15:41:29 +0200576
577 /* ensure tx desc writes above are performed before we start Tx DMA */
578 isb();
579
580 /* Apply send command using zeroth TXUQ */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200581 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530582
583 /*
584 * wait for packet xmit completion
585 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200586 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaudd44265a2010-07-12 22:24:28 +0200587 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530588 /* return fail if error is detected */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200589 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
590 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
591 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500592 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530593 return -1;
594 }
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200595 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530596 };
597 return 0;
598}
599
Chris Packhame9bf75c2018-07-09 21:33:59 +1200600static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530601{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200602 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packhame9bf75c2018-07-09 21:33:59 +1200603
604 return __mvgbe_send(dmvgbe, dataptr, datasize);
605}
606
607static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
608{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200609 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200610 u32 cmd_sts;
611 u32 timeout = 0;
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000612 u32 rxdesc_curr_addr;
Chris Packhame9bf75c2018-07-09 21:33:59 +1200613 unsigned char *data;
614 int rx_bytes = 0;
615
616 *packetp = NULL;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530617
618 /* wait untill rx packet available or timeout */
619 do {
Albert Aribaudd44265a2010-07-12 22:24:28 +0200620 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530621 timeout++;
622 else {
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500623 debug("%s time out...\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530624 return -1;
625 }
Albert Aribaudd44265a2010-07-12 22:24:28 +0200626 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530627
628 if (p_rxdesc_curr->byte_cnt != 0) {
629 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500630 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530631 (u32) p_rxdesc_curr->buf_ptr,
632 (u32) p_rxdesc_curr->cmd_sts);
633 }
634
635 /*
636 * In case received a packet without first/last bits on
637 * OR the error summary bit is on,
638 * the packets needs to be dropeed.
639 */
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200640 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
641
642 if ((cmd_sts &
Albert Aribaudd44265a2010-07-12 22:24:28 +0200643 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
644 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530645
646 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500647 " multiple descriptors\n", __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530648
Albert Aribaudd44265a2010-07-12 22:24:28 +0200649 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530650
651 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500652 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530653
654 } else {
655 /* !!! call higher layer processing */
656 debug("%s: Sending Received packet to"
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500657 " upper layer (net_process_received_packet)\n",
658 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530659
Chris Packhame9bf75c2018-07-09 21:33:59 +1200660 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
661 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
662 RX_BUF_OFFSET);
663
664 *packetp = data;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530665 }
666 /*
667 * free these descriptors and point next in the ring
668 */
669 p_rxdesc_curr->cmd_sts =
Albert Aribaudd44265a2010-07-12 22:24:28 +0200670 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530671 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
672 p_rxdesc_curr->byte_cnt = 0;
673
Anatolij Gustschine6e556c2011-11-19 08:59:36 +0000674 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
675 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom7b05f5e2009-07-08 13:03:18 +0200676
Chris Packhame9bf75c2018-07-09 21:33:59 +1200677 return rx_bytes;
678}
679
680static int mvgbe_recv(struct eth_device *dev)
681{
682 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
683 uchar *packet;
684 int ret;
685
686 ret = __mvgbe_recv(dmvgbe, &packet);
687 if (ret < 0)
688 return ret;
689
690 net_process_received_packet(packet, ret);
691
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530692 return 0;
693}
694
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100695#if defined(CONFIG_PHYLIB)
696int mvgbe_phylib_init(struct eth_device *dev, int phyid)
697{
698 struct mii_dev *bus;
699 struct phy_device *phydev;
700 int ret;
701
702 bus = mdio_alloc();
703 if (!bus) {
704 printf("mdio_alloc failed\n");
705 return -ENOMEM;
706 }
Chris Packham6ecf9e22016-11-01 10:48:32 +1300707 bus->read = smi_reg_read;
708 bus->write = smi_reg_write;
Ben Whitten192bc692015-12-30 13:05:58 +0000709 strcpy(bus->name, dev->name);
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100710
711 ret = mdio_register(bus);
712 if (ret) {
713 printf("mdio_register failed\n");
714 free(bus);
715 return -ENOMEM;
716 }
717
718 /* Set phy address of the port */
Chris Packham6ecf9e22016-11-01 10:48:32 +1300719 smi_reg_write(bus, MV_PHY_ADR_REQUEST, 0, MV_PHY_ADR_REQUEST, phyid);
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100720
721 phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_RGMII);
722 if (!phydev) {
723 printf("phy_connect failed\n");
724 return -ENODEV;
725 }
726
727 phy_config(phydev);
728 phy_startup(phydev);
729
730 return 0;
731}
732#endif
733
Albert Aribaudd44265a2010-07-12 22:24:28 +0200734int mvgbe_initialize(bd_t *bis)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530735{
Albert Aribaudd44265a2010-07-12 22:24:28 +0200736 struct mvgbe_device *dmvgbe;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530737 struct eth_device *dev;
738 int devnum;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200739 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530740
Albert Aribaudd44265a2010-07-12 22:24:28 +0200741 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530742 /*skip if port is configured not to use */
743 if (used_ports[devnum] == 0)
744 continue;
745
Albert Aribaudd44265a2010-07-12 22:24:28 +0200746 dmvgbe = malloc(sizeof(struct mvgbe_device));
747
748 if (!dmvgbe)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530749 goto error1;
750
Albert Aribaudd44265a2010-07-12 22:24:28 +0200751 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530752
Albert Aribaudd44265a2010-07-12 22:24:28 +0200753 dmvgbe->p_rxdesc =
754 (struct mvgbe_rxdesc *)memalign(PKTALIGN,
755 MV_RXQ_DESC_ALIGNED_SIZE*RINGSZ + 1);
756
757 if (!dmvgbe->p_rxdesc)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530758 goto error2;
759
Albert Aribaudd44265a2010-07-12 22:24:28 +0200760 dmvgbe->p_rxbuf = (u8 *) memalign(PKTALIGN,
761 RINGSZ*PKTSIZE_ALIGN + 1);
762
763 if (!dmvgbe->p_rxbuf)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530764 goto error3;
765
Albert Aribaudd44265a2010-07-12 22:24:28 +0200766 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
767
768 if (!dmvgbe->p_aligned_txbuf)
Simon Kagstrom477fa632009-08-20 10:14:11 +0200769 goto error4;
770
Albert Aribaudd44265a2010-07-12 22:24:28 +0200771 dmvgbe->p_txdesc = (struct mvgbe_txdesc *) memalign(
772 PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
773
774 if (!dmvgbe->p_txdesc) {
775 free(dmvgbe->p_aligned_txbuf);
776error4:
777 free(dmvgbe->p_rxbuf);
778error3:
779 free(dmvgbe->p_rxdesc);
780error2:
781 free(dmvgbe);
782error1:
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530783 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500784 __func__);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530785 return -1;
786 }
787
Albert Aribaudd44265a2010-07-12 22:24:28 +0200788 dev = &dmvgbe->dev;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530789
Mike Frysingerf6add132011-11-10 14:11:04 +0000790 /* must be less than sizeof(dev->name) */
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530791 sprintf(dev->name, "egiga%d", devnum);
792
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530793 switch (devnum) {
794 case 0:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200795 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530796 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200797#if defined(MVGBE1_BASE)
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530798 case 1:
Albert Aribaudd44265a2010-07-12 22:24:28 +0200799 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530800 break;
Albert Aribaudd44265a2010-07-12 22:24:28 +0200801#endif
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530802 default: /* this should never happen */
803 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500804 __func__, devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530805 return -1;
806 }
807
Albert Aribaudd44265a2010-07-12 22:24:28 +0200808 dev->init = (void *)mvgbe_init;
809 dev->halt = (void *)mvgbe_halt;
810 dev->send = (void *)mvgbe_send;
811 dev->recv = (void *)mvgbe_recv;
812 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530813
814 eth_register(dev);
815
Sebastian Hesselbarthcd3ca3f2012-12-04 09:32:00 +0100816#if defined(CONFIG_PHYLIB)
817 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
818#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500819 int retval;
820 struct mii_dev *mdiodev = mdio_alloc();
821 if (!mdiodev)
822 return -ENOMEM;
823 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
824 mdiodev->read = smi_reg_read;
825 mdiodev->write = smi_reg_write;
826
827 retval = mdio_register(mdiodev);
828 if (retval < 0)
829 return retval;
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530830 /* Set phy address of the port */
Albert Aribaudd44265a2010-07-12 22:24:28 +0200831 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
832 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskar91315892009-06-14 22:33:46 +0530833#endif
834 }
835 return 0;
Prafulla Wadaskar0b785dd2009-07-01 20:34:51 +0200836}