blob: 037e59ec6e3ccdfd3169de17ccb4d56e889525ba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskarb7a280d2009-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 Wadaskarb7a280d2009-06-14 22:33:46 +053012 */
13
14#include <common.h>
Chris Packham1de16f72018-07-09 21:34:00 +120015#include <dm.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053016#include <net.h>
17#include <malloc.h>
18#include <miiphy.h>
Chris Packhamf42e5b92018-06-09 20:46:16 +120019#include <wait_bit.h>
Lei Wen298ae912011-10-18 20:11:42 +053020#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053022#include <asm/types.h>
Lei Wen298ae912011-10-18 20:11:42 +053023#include <asm/system.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053024#include <asm/byteorder.h>
Anatolij Gustschinc8b222e2011-10-29 10:09:22 +000025#include <asm/arch/cpu.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020026
27#if defined(CONFIG_KIRKWOOD)
Stefan Roesec2437842014-10-22 12:13:06 +020028#include <asm/arch/soc.h>
Albert Aribaud8a995232010-07-12 22:24:29 +020029#elif defined(CONFIG_ORION5X)
30#include <asm/arch/orion5x.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020031#endif
32
Albert Aribaud0d027d92010-07-12 22:24:27 +020033#include "mvgbe.h"
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053034
Albert Aribauda7564072010-07-05 20:15:25 +020035DECLARE_GLOBAL_DATA_PTR;
36
Luka Perkov95acd992013-11-11 07:27:53 +010037#ifndef CONFIG_MVGBE_PORTS
38# define CONFIG_MVGBE_PORTS {0, 0}
39#endif
40
Albert Aribaude91d7d32010-07-12 22:24:28 +020041#define MV_PHY_ADR_REQUEST 0xee
42#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Simon Kagstromab9ca512009-08-20 10:12:28 +020043
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +010044#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packhamf42e5b92018-06-09 20:46:16 +120045static int smi_wait_ready(struct mvgbe_device *dmvgbe)
46{
47 int ret;
48
49 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
50 MVGBE_PHY_SMI_TIMEOUT_MS, false);
51 if (ret) {
52 printf("Error: SMI busy timeout\n");
53 return ret;
54 }
55
56 return 0;
57}
58
Chris Packham465f5cf2018-07-09 21:33:59 +120059static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
60 int devad, int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053061{
Albert Aribaude91d7d32010-07-12 22:24:28 +020062 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053063 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +020064 u32 timeout;
Chris Packham465f5cf2018-07-09 21:33:59 +120065 u16 data = 0;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053066
67 /* Phyadr read request */
Albert Aribaude91d7d32010-07-12 22:24:28 +020068 if (phy_adr == MV_PHY_ADR_REQUEST &&
69 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053070 /* */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050071 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
72 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053073 }
74 /* check parameters */
75 if (phy_adr > PHYADR_MASK) {
76 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050077 __func__, phy_adr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053078 return -EFAULT;
79 }
80 if (reg_ofs > PHYREG_MASK) {
81 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050082 __func__, reg_ofs);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053083 return -EFAULT;
84 }
85
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053086 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +120087 if (smi_wait_ready(dmvgbe) < 0)
88 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053089
90 /* fill the phy address and regiser offset and read opcode */
Albert Aribaude91d7d32010-07-12 22:24:28 +020091 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
92 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
93 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053094
95 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +020096 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053097
98 /*wait till read value is ready */
Albert Aribaude91d7d32010-07-12 22:24:28 +020099 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530100
101 do {
102 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200103 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530104 if (timeout-- == 0) {
105 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500106 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530107 return -EFAULT;
108 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200109 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530110
111 /* Wait for the data to update in the SMI register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200112 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
113 ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530114
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500115 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530116
Joe Hershberger9f09a362015-04-08 01:41:06 -0500117 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500118 data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530119
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500120 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530121}
122
123/*
Chris Packham465f5cf2018-07-09 21:33:59 +1200124 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530125 *
Chris Packham465f5cf2018-07-09 21:33:59 +1200126 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530127 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200128static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
129 int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530130{
Chris Packham1de16f72018-07-09 21:34:00 +1200131#ifdef CONFIG_DM_ETH
132 struct mvgbe_device *dmvgbe = bus->priv;
133#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500134 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200135 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200136#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200137
138 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
139}
140
141static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
142 int devad, int reg_ofs, u16 data)
143{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200144 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530145 u32 smi_reg;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530146
147 /* Phyadr write request*/
Albert Aribaude91d7d32010-07-12 22:24:28 +0200148 if (phy_adr == MV_PHY_ADR_REQUEST &&
149 reg_ofs == MV_PHY_ADR_REQUEST) {
150 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530151 return 0;
152 }
153
154 /* check parameters */
155 if (phy_adr > PHYADR_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500156 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530157 return -EINVAL;
158 }
159 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500160 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packhamf42e5b92018-06-09 20:46:16 +1200161 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530162 }
163
164 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +1200165 if (smi_wait_ready(dmvgbe) < 0)
166 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530167
168 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200169 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
170 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
171 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
172 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530173
174 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200175 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530176
177 return 0;
178}
Chris Packham465f5cf2018-07-09 21:33:59 +1200179
180/*
181 * smi_reg_write - miiphy_write callback function.
182 *
183 * Returns 0 if write succeed, -EFAULT on error
184 */
185static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
186 int reg_ofs, u16 data)
187{
Chris Packham1de16f72018-07-09 21:34:00 +1200188#ifdef CONFIG_DM_ETH
189 struct mvgbe_device *dmvgbe = bus->priv;
190#else
Chris Packham465f5cf2018-07-09 21:33:59 +1200191 struct eth_device *dev = eth_get_dev_by_name(bus->name);
192 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200193#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200194
195 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
196}
Stefan Bigler96455292012-03-26 00:02:13 +0000197#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530198
199/* Stop and checks all queues */
200static void stop_queue(u32 * qreg)
201{
202 u32 reg_data;
203
204 reg_data = readl(qreg);
205
206 if (reg_data & 0xFF) {
207 /* Issue stop command for active channels only */
208 writel((reg_data << 8), qreg);
209
210 /* Wait for all queue activity to terminate. */
211 do {
212 /*
213 * Check port cause register that all queues
214 * are stopped
215 */
216 reg_data = readl(qreg);
217 }
218 while (reg_data & 0xFF);
219 }
220}
221
222/*
223 * set_access_control - Config address decode parameters for Ethernet unit
224 *
225 * This function configures the address decode parameters for the Gigabit
226 * Ethernet Controller according the given parameters struct.
227 *
228 * @regs Register struct pointer.
229 * @param Address decode parameter struct.
230 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200231static void set_access_control(struct mvgbe_registers *regs,
232 struct mvgbe_winparam *param)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530233{
234 u32 access_prot_reg;
235
236 /* Set access control register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200237 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530238 /* clear window permission */
239 access_prot_reg &= (~(3 << (param->win * 2)));
240 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200241 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530242
243 /* Set window Size reg (SR) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200244 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530245 (((param->size / 0x10000) - 1) << 16));
246
247 /* Set window Base address reg (BA) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200248 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530249 (param->target | param->attrib | param->base_addr));
250 /* High address remap reg (HARR) */
251 if (param->win < 4)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200252 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530253
254 /* Base address enable reg (BARER) */
255 if (param->enable == 1)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200256 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530257 else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200258 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530259}
260
Albert Aribaude91d7d32010-07-12 22:24:28 +0200261static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530262{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200263 struct mvgbe_winparam win_param;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530264 int i;
265
266 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
267 /* Set access parameters for DRAM bank i */
268 win_param.win = i; /* Use Ethernet window i */
269 /* Window target - DDR */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200270 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530271 /* Enable full access */
272 win_param.access_ctrl = EWIN_ACCESS_FULL;
273 win_param.high_addr = 0;
Albert Aribauda7564072010-07-05 20:15:25 +0200274 /* Get bank base and size */
275 win_param.base_addr = gd->bd->bi_dram[i].start;
276 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530277 if (win_param.size == 0)
278 win_param.enable = 0;
279 else
280 win_param.enable = 1; /* Enable the access */
281
282 /* Enable DRAM bank */
283 switch (i) {
284 case 0:
285 win_param.attrib = EBAR_DRAM_CS0;
286 break;
287 case 1:
288 win_param.attrib = EBAR_DRAM_CS1;
289 break;
290 case 2:
291 win_param.attrib = EBAR_DRAM_CS2;
292 break;
293 case 3:
294 win_param.attrib = EBAR_DRAM_CS3;
295 break;
296 default:
Albert Aribauda7564072010-07-05 20:15:25 +0200297 /* invalid bank, disable access */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530298 win_param.enable = 0;
299 win_param.attrib = 0;
300 break;
301 }
302 /* Set the access control for address window(EPAPR) RD/WR */
303 set_access_control(regs, &win_param);
304 }
305}
306
307/*
308 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
309 *
310 * Go through all the DA filter tables (Unicast, Special Multicast & Other
311 * Multicast) and set each entry to 0.
312 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200313static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530314{
315 int table_index;
316
317 /* Clear DA filter unicast table (Ex_dFUT) */
318 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200319 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530320
321 for (table_index = 0; table_index < 64; ++table_index) {
322 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200323 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530324 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200325 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530326 }
327}
328
329/*
330 * port_uc_addr - This function Set the port unicast address table
331 *
332 * This function locates the proper entry in the Unicast table for the
333 * specified MAC nibble and sets its properties according to function
334 * parameters.
335 * This function add/removes MAC addresses from the port unicast address
336 * table.
337 *
338 * @uc_nibble Unicast MAC Address last nibble.
339 * @option 0 = Add, 1 = remove address.
340 *
341 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
342 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200343static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530344 int option)
345{
346 u32 unicast_reg;
347 u32 tbl_offset;
348 u32 reg_offset;
349
350 /* Locate the Unicast table entry */
351 uc_nibble = (0xf & uc_nibble);
352 /* Register offset from unicast table base */
353 tbl_offset = (uc_nibble / 4);
354 /* Entry offset within the above register */
355 reg_offset = uc_nibble % 4;
356
357 switch (option) {
358 case REJECT_MAC_ADDR:
359 /*
360 * Clear accepts frame bit at specified unicast
361 * DA table entry
362 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200363 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530364 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200365 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530366 break;
367 case ACCEPT_MAC_ADDR:
368 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200369 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530370 unicast_reg &= (0xFF << (8 * reg_offset));
371 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200372 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530373 break;
374 default:
375 return 0;
376 }
377 return 1;
378}
379
380/*
381 * port_uc_addr_set - This function Set the port Unicast address.
382 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200383static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530384{
Chris Packham465f5cf2018-07-09 21:33:59 +1200385 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530386 u32 mac_h;
387 u32 mac_l;
388
389 mac_l = (p_addr[4] << 8) | (p_addr[5]);
390 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
391 (p_addr[3] << 0);
392
Albert Aribaude91d7d32010-07-12 22:24:28 +0200393 MVGBE_REG_WR(regs->macal, mac_l);
394 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530395
396 /* Accept frames of this address */
397 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
398}
399
400/*
Albert Aribaude91d7d32010-07-12 22:24:28 +0200401 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530402 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200403static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530404{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200405 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530406 int i;
407
408 /* initialize the Rx descriptors ring */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200409 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530410 for (i = 0; i < RINGSZ; i++) {
411 p_rx_desc->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200412 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530413 p_rx_desc->buf_size = PKTSIZE_ALIGN;
414 p_rx_desc->byte_cnt = 0;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200415 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530416 if (i == (RINGSZ - 1))
Albert Aribaude91d7d32010-07-12 22:24:28 +0200417 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530418 else {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200419 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
420 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530421 p_rx_desc = p_rx_desc->nxtdesc_p;
422 }
423 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200424 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530425}
426
Chris Packham1de16f72018-07-09 21:34:00 +1200427static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
428 const char *name)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530429{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200430 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0984d642013-08-11 17:08:23 +0200431#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
432 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200433 !defined(CONFIG_DM_ETH) && \
Sascha Silbe0984d642013-08-11 17:08:23 +0200434 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200435 int i;
Prafulla Wadaskar9841dec2009-09-09 15:59:19 +0530436#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530437 /* setup RX rings */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200438 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530439
440 /* Clear the ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200441 MVGBE_REG_WR(regs->ic, 0);
442 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530443 /* Unmask RX buffer and TX end interrupt */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200444 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530445 /* Unmask phy and link status changes interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200446 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530447
448 set_dram_access(regs);
449 port_init_mac_tables(regs);
Chris Packham1de16f72018-07-09 21:34:00 +1200450 port_uc_addr_set(dmvgbe, enetaddr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530451
452 /* Assign port configuration and command. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200453 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
454 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
455 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530456
457 /* Assign port SDMA configuration */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200458 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
459 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
460 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
461 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530462 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200463 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530464
465 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200466 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
467 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530468
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530469 /* Enable port initially */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200470 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530471
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530472 /*
473 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
474 * disable the leaky bucket mechanism .
475 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200476 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530477
478 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200479 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200480 /* ensure previous write is done before enabling Rx DMA */
481 isb();
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530482 /* Enable port Rx. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200483 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530484
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100485#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
486 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200487 !defined(CONFIG_DM_ETH) && \
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100488 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200489 /* Wait up to 5s for the link status */
490 for (i = 0; i < 5; i++) {
491 u16 phyadr;
492
Chris Packham1de16f72018-07-09 21:34:00 +1200493 miiphy_read(name, MV_PHY_ADR_REQUEST,
Albert Aribaude91d7d32010-07-12 22:24:28 +0200494 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200495 /* Return if we get link up */
Chris Packham1de16f72018-07-09 21:34:00 +1200496 if (miiphy_link(name, phyadr))
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200497 return 0;
498 udelay(1000000);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530499 }
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200500
Chris Packham1de16f72018-07-09 21:34:00 +1200501 printf("No link on %s\n", name);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200502 return -1;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530503#endif
504 return 0;
505}
506
Chris Packham1de16f72018-07-09 21:34:00 +1200507#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200508static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530509{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200510 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200511
Chris Packham1de16f72018-07-09 21:34:00 +1200512 return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name);
Chris Packham465f5cf2018-07-09 21:33:59 +1200513}
Chris Packham1de16f72018-07-09 21:34:00 +1200514#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200515
516static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
517{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200518 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530519
520 /* Disable all gigE address decoder */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200521 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530522
523 stop_queue(&regs->tqc);
524 stop_queue(&regs->rqc);
525
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530526 /* Disable port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200527 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530528 /* Set port is not reset */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200529 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530530#ifdef CONFIG_SYS_MII_MODE
531 /* Set MMI interface up */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200532 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530533#endif
534 /* Disable & mask ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200535 MVGBE_REG_WR(regs->ic, 0);
536 MVGBE_REG_WR(regs->ice, 0);
537 MVGBE_REG_WR(regs->pim, 0);
538 MVGBE_REG_WR(regs->peim, 0);
Chris Packham465f5cf2018-07-09 21:33:59 +1200539}
540
Chris Packham1de16f72018-07-09 21:34:00 +1200541#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200542static int mvgbe_halt(struct eth_device *dev)
543{
544 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
545
546 __mvgbe_halt(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530547
548 return 0;
549}
Chris Packham1de16f72018-07-09 21:34:00 +1200550#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530551
Chris Packham1de16f72018-07-09 21:34:00 +1200552#ifdef CONFIG_DM_ETH
553static int mvgbe_write_hwaddr(struct udevice *dev)
554{
555 struct eth_pdata *pdata = dev_get_platdata(dev);
556
557 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
558
559 return 0;
560}
561#else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200562static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530563{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200564 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530565
566 /* Programs net device MAC address after initialization */
Chris Packham465f5cf2018-07-09 21:33:59 +1200567 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530568 return 0;
569}
Chris Packham1de16f72018-07-09 21:34:00 +1200570#endif
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530571
Chris Packham465f5cf2018-07-09 21:33:59 +1200572static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
573 int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530574{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200575 struct mvgbe_registers *regs = dmvgbe->regs;
576 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200577 void *p = (void *)dataptr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200578 u32 cmd_sts;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000579 u32 txuq0_reg_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530580
Simon Kagstrome9220b32009-08-20 10:14:11 +0200581 /* Copy buffer if it's misaligned */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530582 if ((u32) dataptr & 0x07) {
Simon Kagstrome9220b32009-08-20 10:14:11 +0200583 if (datasize > PKTSIZE_ALIGN) {
584 printf("Non-aligned data too large (%d)\n",
585 datasize);
586 return -1;
587 }
588
Albert Aribaude91d7d32010-07-12 22:24:28 +0200589 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
590 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530591 }
Simon Kagstrome9220b32009-08-20 10:14:11 +0200592
Albert Aribaude91d7d32010-07-12 22:24:28 +0200593 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
594 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
595 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
596 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200597 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530598 p_txdesc->byte_cnt = datasize;
599
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200600 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000601 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
602 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200603
604 /* ensure tx desc writes above are performed before we start Tx DMA */
605 isb();
606
607 /* Apply send command using zeroth TXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200608 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530609
610 /*
611 * wait for packet xmit completion
612 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200613 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200614 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530615 /* return fail if error is detected */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200616 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
617 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
618 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500619 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530620 return -1;
621 }
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200622 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530623 };
624 return 0;
625}
626
Chris Packham1de16f72018-07-09 21:34:00 +1200627#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200628static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530629{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200630 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200631
632 return __mvgbe_send(dmvgbe, dataptr, datasize);
633}
Chris Packham1de16f72018-07-09 21:34:00 +1200634#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200635
636static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
637{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200638 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200639 u32 cmd_sts;
640 u32 timeout = 0;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000641 u32 rxdesc_curr_addr;
Chris Packham465f5cf2018-07-09 21:33:59 +1200642 unsigned char *data;
643 int rx_bytes = 0;
644
645 *packetp = NULL;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530646
647 /* wait untill rx packet available or timeout */
648 do {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200649 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530650 timeout++;
651 else {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500652 debug("%s time out...\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530653 return -1;
654 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200655 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530656
657 if (p_rxdesc_curr->byte_cnt != 0) {
658 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500659 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530660 (u32) p_rxdesc_curr->buf_ptr,
661 (u32) p_rxdesc_curr->cmd_sts);
662 }
663
664 /*
665 * In case received a packet without first/last bits on
666 * OR the error summary bit is on,
667 * the packets needs to be dropeed.
668 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200669 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
670
671 if ((cmd_sts &
Albert Aribaude91d7d32010-07-12 22:24:28 +0200672 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
673 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530674
675 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500676 " multiple descriptors\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530677
Albert Aribaude91d7d32010-07-12 22:24:28 +0200678 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530679
680 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500681 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530682
683 } else {
684 /* !!! call higher layer processing */
685 debug("%s: Sending Received packet to"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500686 " upper layer (net_process_received_packet)\n",
687 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530688
Chris Packham465f5cf2018-07-09 21:33:59 +1200689 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
690 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
691 RX_BUF_OFFSET);
692
693 *packetp = data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530694 }
695 /*
696 * free these descriptors and point next in the ring
697 */
698 p_rxdesc_curr->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200699 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530700 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
701 p_rxdesc_curr->byte_cnt = 0;
702
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000703 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
704 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200705
Chris Packham465f5cf2018-07-09 21:33:59 +1200706 return rx_bytes;
707}
708
Chris Packham1de16f72018-07-09 21:34:00 +1200709#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200710static int mvgbe_recv(struct eth_device *dev)
711{
712 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
713 uchar *packet;
714 int ret;
715
716 ret = __mvgbe_recv(dmvgbe, &packet);
717 if (ret < 0)
718 return ret;
719
720 net_process_received_packet(packet, ret);
721
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530722 return 0;
723}
Chris Packham1de16f72018-07-09 21:34:00 +1200724#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530725
Chris Packham1de16f72018-07-09 21:34:00 +1200726#if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH)
727#if defined(CONFIG_DM_ETH)
728static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
729 struct mii_dev *bus,
730 phy_interface_t phy_interface,
731 int phyid)
732#else
733static struct phy_device *__mvgbe_phy_init(struct eth_device *dev,
734 struct mii_dev *bus,
735 phy_interface_t phy_interface,
736 int phyid)
737#endif
738{
739 struct phy_device *phydev;
740
741 /* Set phy address of the port */
742 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
743 phyid);
744
745 phydev = phy_connect(bus, phyid, dev, phy_interface);
746 if (!phydev) {
747 printf("phy_connect failed\n");
748 return NULL;
749 }
750
751 phy_config(phydev);
752 phy_startup(phydev);
753
754 return phydev;
755}
756#endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */
757
758#if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100759int mvgbe_phylib_init(struct eth_device *dev, int phyid)
760{
761 struct mii_dev *bus;
762 struct phy_device *phydev;
763 int ret;
764
765 bus = mdio_alloc();
766 if (!bus) {
767 printf("mdio_alloc failed\n");
768 return -ENOMEM;
769 }
Chris Packhamcee2fa32016-11-01 10:48:32 +1300770 bus->read = smi_reg_read;
771 bus->write = smi_reg_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000772 strcpy(bus->name, dev->name);
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100773
774 ret = mdio_register(bus);
775 if (ret) {
776 printf("mdio_register failed\n");
777 free(bus);
778 return -ENOMEM;
779 }
780
Chris Packham1de16f72018-07-09 21:34:00 +1200781 phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid);
782 if (!phydev)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100783 return -ENODEV;
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100784
785 return 0;
786}
787#endif
788
Chris Packham1de16f72018-07-09 21:34:00 +1200789static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
790{
791 dmvgbe->p_rxdesc = memalign(PKTALIGN,
792 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
793 if (!dmvgbe->p_rxdesc)
794 goto error1;
795
796 dmvgbe->p_rxbuf = memalign(PKTALIGN,
797 RINGSZ * PKTSIZE_ALIGN + 1);
798 if (!dmvgbe->p_rxbuf)
799 goto error2;
800
801 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
802 if (!dmvgbe->p_aligned_txbuf)
803 goto error3;
804
805 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
806 if (!dmvgbe->p_txdesc)
807 goto error4;
808
809 return 0;
810
811error4:
812 free(dmvgbe->p_aligned_txbuf);
813error3:
814 free(dmvgbe->p_rxbuf);
815error2:
816 free(dmvgbe->p_rxdesc);
817error1:
818 return -ENOMEM;
819}
820
821#ifndef CONFIG_DM_ETH
Albert Aribaude91d7d32010-07-12 22:24:28 +0200822int mvgbe_initialize(bd_t *bis)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530823{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200824 struct mvgbe_device *dmvgbe;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530825 struct eth_device *dev;
826 int devnum;
Chris Packham1de16f72018-07-09 21:34:00 +1200827 int ret;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200828 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530829
Albert Aribaude91d7d32010-07-12 22:24:28 +0200830 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530831 /*skip if port is configured not to use */
832 if (used_ports[devnum] == 0)
833 continue;
834
Albert Aribaude91d7d32010-07-12 22:24:28 +0200835 dmvgbe = malloc(sizeof(struct mvgbe_device));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200836 if (!dmvgbe)
Chris Packham1de16f72018-07-09 21:34:00 +1200837 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530838
Albert Aribaude91d7d32010-07-12 22:24:28 +0200839 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Chris Packham1de16f72018-07-09 21:34:00 +1200840 ret = mvgbe_alloc_buffers(dmvgbe);
841 if (ret) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530842 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500843 __func__);
Chris Packham1de16f72018-07-09 21:34:00 +1200844 free(dmvgbe);
845 return ret;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530846 }
847
Albert Aribaude91d7d32010-07-12 22:24:28 +0200848 dev = &dmvgbe->dev;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530849
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000850 /* must be less than sizeof(dev->name) */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530851 sprintf(dev->name, "egiga%d", devnum);
852
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530853 switch (devnum) {
854 case 0:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200855 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530856 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200857#if defined(MVGBE1_BASE)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530858 case 1:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200859 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530860 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200861#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530862 default: /* this should never happen */
863 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500864 __func__, devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530865 return -1;
866 }
867
Albert Aribaude91d7d32010-07-12 22:24:28 +0200868 dev->init = (void *)mvgbe_init;
869 dev->halt = (void *)mvgbe_halt;
870 dev->send = (void *)mvgbe_send;
871 dev->recv = (void *)mvgbe_recv;
872 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530873
874 eth_register(dev);
875
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100876#if defined(CONFIG_PHYLIB)
877 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
878#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500879 int retval;
880 struct mii_dev *mdiodev = mdio_alloc();
881 if (!mdiodev)
882 return -ENOMEM;
883 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
884 mdiodev->read = smi_reg_read;
885 mdiodev->write = smi_reg_write;
886
887 retval = mdio_register(mdiodev);
888 if (retval < 0)
889 return retval;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530890 /* Set phy address of the port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200891 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
892 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530893#endif
Chris Packham1de16f72018-07-09 21:34:00 +1200894 }
895 return 0;
896}
897#endif
898
899#ifdef CONFIG_DM_ETH
900static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
901{
902 return dmvgbe->phyaddr > PHY_MAX_ADDR;
903}
904
905static int mvgbe_start(struct udevice *dev)
906{
907 struct eth_pdata *pdata = dev_get_platdata(dev);
908 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
909 int ret;
910
911 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
912 if (ret)
913 return ret;
914
915 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
916 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
917 dmvgbe->phy_interface,
918 dmvgbe->phyaddr);
919 if (!dmvgbe->phydev)
920 return -ENODEV;
921 }
922
923 return 0;
924}
925
926static int mvgbe_send(struct udevice *dev, void *packet, int length)
927{
928 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
929
930 return __mvgbe_send(dmvgbe, packet, length);
931}
932
933static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
934{
935 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
936
937 return __mvgbe_recv(dmvgbe, packetp);
938}
939
940static void mvgbe_stop(struct udevice *dev)
941{
942 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
943
944 __mvgbe_halt(dmvgbe);
945}
946
947static int mvgbe_probe(struct udevice *dev)
948{
949 struct eth_pdata *pdata = dev_get_platdata(dev);
950 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
951 struct mii_dev *bus;
952 int ret;
953
954 ret = mvgbe_alloc_buffers(dmvgbe);
955 if (ret)
956 return ret;
957
958 dmvgbe->regs = (void __iomem *)pdata->iobase;
959
960 bus = mdio_alloc();
961 if (!bus) {
962 printf("Failed to allocate MDIO bus\n");
963 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530964 }
Chris Packham1de16f72018-07-09 21:34:00 +1200965
966 bus->read = smi_reg_read;
967 bus->write = smi_reg_write;
968 snprintf(bus->name, sizeof(bus->name), dev->name);
969 bus->priv = dmvgbe;
970 dmvgbe->bus = bus;
971
972 ret = mdio_register(bus);
973 if (ret < 0)
974 return ret;
975
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530976 return 0;
Prafulla Wadaskar12618ef2009-07-01 20:34:51 +0200977}
Chris Packham1de16f72018-07-09 21:34:00 +1200978
979static const struct eth_ops mvgbe_ops = {
980 .start = mvgbe_start,
981 .send = mvgbe_send,
982 .recv = mvgbe_recv,
983 .stop = mvgbe_stop,
984 .write_hwaddr = mvgbe_write_hwaddr,
985};
986
987static int mvgbe_ofdata_to_platdata(struct udevice *dev)
988{
989 struct eth_pdata *pdata = dev_get_platdata(dev);
990 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
991 void *blob = (void *)gd->fdt_blob;
992 int node = dev_of_offset(dev);
993 const char *phy_mode;
994 int fl_node;
995 int pnode;
996 unsigned long addr;
997
998 pdata->iobase = devfdt_get_addr(dev);
999 pdata->phy_interface = -1;
1000
1001 pnode = fdt_node_offset_by_compatible(blob, node,
1002 "marvell,kirkwood-eth-port");
1003
1004 /* Get phy-mode / phy_interface from DT */
1005 phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL);
1006 if (phy_mode)
1007 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
Chris Packham7ca5e692018-12-04 19:54:30 +13001008 else
1009 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
Chris Packham1de16f72018-07-09 21:34:00 +12001010
1011 dmvgbe->phy_interface = pdata->phy_interface;
1012
1013 /* fetch 'fixed-link' property */
1014 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
1015 if (fl_node != -FDT_ERR_NOTFOUND) {
1016 /* set phy_addr to invalid value for fixed link */
1017 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
1018 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1019 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1020 } else {
1021 /* Now read phyaddr from DT */
1022 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
1023 if (addr > 0)
1024 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1025 }
1026
1027 return 0;
1028}
1029
1030static const struct udevice_id mvgbe_ids[] = {
1031 { .compatible = "marvell,kirkwood-eth" },
1032 { }
1033};
1034
1035U_BOOT_DRIVER(mvgbe) = {
1036 .name = "mvgbe",
1037 .id = UCLASS_ETH,
1038 .of_match = mvgbe_ids,
1039 .ofdata_to_platdata = mvgbe_ofdata_to_platdata,
1040 .probe = mvgbe_probe,
1041 .ops = &mvgbe_ops,
1042 .priv_auto_alloc_size = sizeof(struct mvgbe_device),
1043 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1044};
1045#endif /* CONFIG_DM_ETH */