blob: 2f9464b961a9381399682ca0a7c0613e35be877e [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>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053017#include <net.h>
18#include <malloc.h>
19#include <miiphy.h>
Chris Packhamf42e5b92018-06-09 20:46:16 +120020#include <wait_bit.h>
Lei Wen298ae912011-10-18 20:11:42 +053021#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090023#include <linux/errno.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053024#include <asm/types.h>
Lei Wen298ae912011-10-18 20:11:42 +053025#include <asm/system.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053026#include <asm/byteorder.h>
Anatolij Gustschinc8b222e2011-10-29 10:09:22 +000027#include <asm/arch/cpu.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020028
Trevor Woernerbb7ab072020-05-06 08:02:40 -040029#if defined(CONFIG_ARCH_KIRKWOOD)
Stefan Roesec2437842014-10-22 12:13:06 +020030#include <asm/arch/soc.h>
Trevor Woernerf9953752020-05-06 08:02:38 -040031#elif defined(CONFIG_ARCH_ORION5X)
Albert Aribaud8a995232010-07-12 22:24:29 +020032#include <asm/arch/orion5x.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020033#endif
34
Albert Aribaud0d027d92010-07-12 22:24:27 +020035#include "mvgbe.h"
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053036
Albert Aribauda7564072010-07-05 20:15:25 +020037DECLARE_GLOBAL_DATA_PTR;
38
Luka Perkov95acd992013-11-11 07:27:53 +010039#ifndef CONFIG_MVGBE_PORTS
40# define CONFIG_MVGBE_PORTS {0, 0}
41#endif
42
Albert Aribaude91d7d32010-07-12 22:24:28 +020043#define MV_PHY_ADR_REQUEST 0xee
44#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Simon Kagstromab9ca512009-08-20 10:12:28 +020045
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +010046#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packhamf42e5b92018-06-09 20:46:16 +120047static int smi_wait_ready(struct mvgbe_device *dmvgbe)
48{
49 int ret;
50
51 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
52 MVGBE_PHY_SMI_TIMEOUT_MS, false);
53 if (ret) {
54 printf("Error: SMI busy timeout\n");
55 return ret;
56 }
57
58 return 0;
59}
60
Chris Packham465f5cf2018-07-09 21:33:59 +120061static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
62 int devad, int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053063{
Albert Aribaude91d7d32010-07-12 22:24:28 +020064 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053065 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +020066 u32 timeout;
Chris Packham465f5cf2018-07-09 21:33:59 +120067 u16 data = 0;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053068
69 /* Phyadr read request */
Albert Aribaude91d7d32010-07-12 22:24:28 +020070 if (phy_adr == MV_PHY_ADR_REQUEST &&
71 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053072 /* */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050073 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
74 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053075 }
76 /* check parameters */
77 if (phy_adr > PHYADR_MASK) {
78 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050079 __func__, phy_adr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053080 return -EFAULT;
81 }
82 if (reg_ofs > PHYREG_MASK) {
83 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050084 __func__, reg_ofs);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053085 return -EFAULT;
86 }
87
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053088 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +120089 if (smi_wait_ready(dmvgbe) < 0)
90 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053091
92 /* fill the phy address and regiser offset and read opcode */
Albert Aribaude91d7d32010-07-12 22:24:28 +020093 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
94 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
95 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053096
97 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +020098 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053099
100 /*wait till read value is ready */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200101 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530102
103 do {
104 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200105 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530106 if (timeout-- == 0) {
107 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500108 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530109 return -EFAULT;
110 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200111 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530112
113 /* Wait for the data to update in the SMI register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200114 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
115 ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530116
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500117 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530118
Joe Hershberger9f09a362015-04-08 01:41:06 -0500119 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500120 data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530121
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500122 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530123}
124
125/*
Chris Packham465f5cf2018-07-09 21:33:59 +1200126 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530127 *
Chris Packham465f5cf2018-07-09 21:33:59 +1200128 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530129 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200130static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
131 int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530132{
Chris Packham1de16f72018-07-09 21:34:00 +1200133#ifdef CONFIG_DM_ETH
134 struct mvgbe_device *dmvgbe = bus->priv;
135#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500136 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200137 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200138#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200139
140 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
141}
142
143static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
144 int devad, int reg_ofs, u16 data)
145{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200146 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530147 u32 smi_reg;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530148
149 /* Phyadr write request*/
Albert Aribaude91d7d32010-07-12 22:24:28 +0200150 if (phy_adr == MV_PHY_ADR_REQUEST &&
151 reg_ofs == MV_PHY_ADR_REQUEST) {
152 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530153 return 0;
154 }
155
156 /* check parameters */
157 if (phy_adr > PHYADR_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500158 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530159 return -EINVAL;
160 }
161 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500162 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packhamf42e5b92018-06-09 20:46:16 +1200163 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530164 }
165
166 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +1200167 if (smi_wait_ready(dmvgbe) < 0)
168 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530169
170 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200171 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
172 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
173 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
174 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530175
176 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200177 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530178
179 return 0;
180}
Chris Packham465f5cf2018-07-09 21:33:59 +1200181
182/*
183 * smi_reg_write - miiphy_write callback function.
184 *
185 * Returns 0 if write succeed, -EFAULT on error
186 */
187static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
188 int reg_ofs, u16 data)
189{
Chris Packham1de16f72018-07-09 21:34:00 +1200190#ifdef CONFIG_DM_ETH
191 struct mvgbe_device *dmvgbe = bus->priv;
192#else
Chris Packham465f5cf2018-07-09 21:33:59 +1200193 struct eth_device *dev = eth_get_dev_by_name(bus->name);
194 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200195#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200196
197 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
198}
Stefan Bigler96455292012-03-26 00:02:13 +0000199#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530200
201/* Stop and checks all queues */
202static void stop_queue(u32 * qreg)
203{
204 u32 reg_data;
205
206 reg_data = readl(qreg);
207
208 if (reg_data & 0xFF) {
209 /* Issue stop command for active channels only */
210 writel((reg_data << 8), qreg);
211
212 /* Wait for all queue activity to terminate. */
213 do {
214 /*
215 * Check port cause register that all queues
216 * are stopped
217 */
218 reg_data = readl(qreg);
219 }
220 while (reg_data & 0xFF);
221 }
222}
223
224/*
225 * set_access_control - Config address decode parameters for Ethernet unit
226 *
227 * This function configures the address decode parameters for the Gigabit
228 * Ethernet Controller according the given parameters struct.
229 *
230 * @regs Register struct pointer.
231 * @param Address decode parameter struct.
232 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200233static void set_access_control(struct mvgbe_registers *regs,
234 struct mvgbe_winparam *param)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530235{
236 u32 access_prot_reg;
237
238 /* Set access control register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200239 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530240 /* clear window permission */
241 access_prot_reg &= (~(3 << (param->win * 2)));
242 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200243 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530244
245 /* Set window Size reg (SR) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200246 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530247 (((param->size / 0x10000) - 1) << 16));
248
249 /* Set window Base address reg (BA) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200250 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530251 (param->target | param->attrib | param->base_addr));
252 /* High address remap reg (HARR) */
253 if (param->win < 4)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200254 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530255
256 /* Base address enable reg (BARER) */
257 if (param->enable == 1)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200258 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530259 else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200260 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530261}
262
Albert Aribaude91d7d32010-07-12 22:24:28 +0200263static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530264{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200265 struct mvgbe_winparam win_param;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530266 int i;
267
268 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
269 /* Set access parameters for DRAM bank i */
270 win_param.win = i; /* Use Ethernet window i */
271 /* Window target - DDR */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200272 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530273 /* Enable full access */
274 win_param.access_ctrl = EWIN_ACCESS_FULL;
275 win_param.high_addr = 0;
Albert Aribauda7564072010-07-05 20:15:25 +0200276 /* Get bank base and size */
277 win_param.base_addr = gd->bd->bi_dram[i].start;
278 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530279 if (win_param.size == 0)
280 win_param.enable = 0;
281 else
282 win_param.enable = 1; /* Enable the access */
283
284 /* Enable DRAM bank */
285 switch (i) {
286 case 0:
287 win_param.attrib = EBAR_DRAM_CS0;
288 break;
289 case 1:
290 win_param.attrib = EBAR_DRAM_CS1;
291 break;
292 case 2:
293 win_param.attrib = EBAR_DRAM_CS2;
294 break;
295 case 3:
296 win_param.attrib = EBAR_DRAM_CS3;
297 break;
298 default:
Albert Aribauda7564072010-07-05 20:15:25 +0200299 /* invalid bank, disable access */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530300 win_param.enable = 0;
301 win_param.attrib = 0;
302 break;
303 }
304 /* Set the access control for address window(EPAPR) RD/WR */
305 set_access_control(regs, &win_param);
306 }
307}
308
309/*
310 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
311 *
312 * Go through all the DA filter tables (Unicast, Special Multicast & Other
313 * Multicast) and set each entry to 0.
314 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200315static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530316{
317 int table_index;
318
319 /* Clear DA filter unicast table (Ex_dFUT) */
320 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200321 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530322
323 for (table_index = 0; table_index < 64; ++table_index) {
324 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200325 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530326 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200327 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530328 }
329}
330
331/*
332 * port_uc_addr - This function Set the port unicast address table
333 *
334 * This function locates the proper entry in the Unicast table for the
335 * specified MAC nibble and sets its properties according to function
336 * parameters.
337 * This function add/removes MAC addresses from the port unicast address
338 * table.
339 *
340 * @uc_nibble Unicast MAC Address last nibble.
341 * @option 0 = Add, 1 = remove address.
342 *
343 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
344 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200345static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530346 int option)
347{
348 u32 unicast_reg;
349 u32 tbl_offset;
350 u32 reg_offset;
351
352 /* Locate the Unicast table entry */
353 uc_nibble = (0xf & uc_nibble);
354 /* Register offset from unicast table base */
355 tbl_offset = (uc_nibble / 4);
356 /* Entry offset within the above register */
357 reg_offset = uc_nibble % 4;
358
359 switch (option) {
360 case REJECT_MAC_ADDR:
361 /*
362 * Clear accepts frame bit at specified unicast
363 * DA table entry
364 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200365 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530366 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200367 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530368 break;
369 case ACCEPT_MAC_ADDR:
370 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200371 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530372 unicast_reg &= (0xFF << (8 * reg_offset));
373 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200374 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530375 break;
376 default:
377 return 0;
378 }
379 return 1;
380}
381
382/*
383 * port_uc_addr_set - This function Set the port Unicast address.
384 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200385static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530386{
Chris Packham465f5cf2018-07-09 21:33:59 +1200387 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530388 u32 mac_h;
389 u32 mac_l;
390
391 mac_l = (p_addr[4] << 8) | (p_addr[5]);
392 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
393 (p_addr[3] << 0);
394
Albert Aribaude91d7d32010-07-12 22:24:28 +0200395 MVGBE_REG_WR(regs->macal, mac_l);
396 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530397
398 /* Accept frames of this address */
399 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
400}
401
402/*
Albert Aribaude91d7d32010-07-12 22:24:28 +0200403 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530404 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200405static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530406{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200407 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530408 int i;
409
410 /* initialize the Rx descriptors ring */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200411 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530412 for (i = 0; i < RINGSZ; i++) {
413 p_rx_desc->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200414 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530415 p_rx_desc->buf_size = PKTSIZE_ALIGN;
416 p_rx_desc->byte_cnt = 0;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200417 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530418 if (i == (RINGSZ - 1))
Albert Aribaude91d7d32010-07-12 22:24:28 +0200419 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530420 else {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200421 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
422 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530423 p_rx_desc = p_rx_desc->nxtdesc_p;
424 }
425 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200426 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530427}
428
Chris Packham1de16f72018-07-09 21:34:00 +1200429static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
430 const char *name)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530431{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200432 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0984d642013-08-11 17:08:23 +0200433#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
434 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200435 !defined(CONFIG_DM_ETH) && \
Sascha Silbe0984d642013-08-11 17:08:23 +0200436 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200437 int i;
Prafulla Wadaskar9841dec2009-09-09 15:59:19 +0530438#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530439 /* setup RX rings */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200440 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530441
442 /* Clear the ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200443 MVGBE_REG_WR(regs->ic, 0);
444 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530445 /* Unmask RX buffer and TX end interrupt */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200446 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530447 /* Unmask phy and link status changes interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200448 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530449
450 set_dram_access(regs);
451 port_init_mac_tables(regs);
Chris Packham1de16f72018-07-09 21:34:00 +1200452 port_uc_addr_set(dmvgbe, enetaddr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530453
454 /* Assign port configuration and command. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200455 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
456 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
457 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530458
459 /* Assign port SDMA configuration */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200460 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
461 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
462 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
463 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530464 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200465 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530466
467 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200468 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
469 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530470
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530471 /* Enable port initially */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200472 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530473
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530474 /*
475 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
476 * disable the leaky bucket mechanism .
477 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200478 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530479
480 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200481 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200482 /* ensure previous write is done before enabling Rx DMA */
483 isb();
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530484 /* Enable port Rx. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200485 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530486
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100487#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
488 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200489 !defined(CONFIG_DM_ETH) && \
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100490 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200491 /* Wait up to 5s for the link status */
492 for (i = 0; i < 5; i++) {
493 u16 phyadr;
494
Chris Packham1de16f72018-07-09 21:34:00 +1200495 miiphy_read(name, MV_PHY_ADR_REQUEST,
Albert Aribaude91d7d32010-07-12 22:24:28 +0200496 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200497 /* Return if we get link up */
Chris Packham1de16f72018-07-09 21:34:00 +1200498 if (miiphy_link(name, phyadr))
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200499 return 0;
500 udelay(1000000);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530501 }
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200502
Chris Packham1de16f72018-07-09 21:34:00 +1200503 printf("No link on %s\n", name);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200504 return -1;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530505#endif
506 return 0;
507}
508
Chris Packham1de16f72018-07-09 21:34:00 +1200509#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200510static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530511{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200512 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200513
Chris Packham1de16f72018-07-09 21:34:00 +1200514 return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name);
Chris Packham465f5cf2018-07-09 21:33:59 +1200515}
Chris Packham1de16f72018-07-09 21:34:00 +1200516#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200517
518static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
519{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200520 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530521
522 /* Disable all gigE address decoder */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200523 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530524
525 stop_queue(&regs->tqc);
526 stop_queue(&regs->rqc);
527
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530528 /* Disable port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200529 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530530 /* Set port is not reset */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200531 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530532#ifdef CONFIG_SYS_MII_MODE
533 /* Set MMI interface up */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200534 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530535#endif
536 /* Disable & mask ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200537 MVGBE_REG_WR(regs->ic, 0);
538 MVGBE_REG_WR(regs->ice, 0);
539 MVGBE_REG_WR(regs->pim, 0);
540 MVGBE_REG_WR(regs->peim, 0);
Chris Packham465f5cf2018-07-09 21:33:59 +1200541}
542
Chris Packham1de16f72018-07-09 21:34:00 +1200543#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200544static int mvgbe_halt(struct eth_device *dev)
545{
546 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
547
548 __mvgbe_halt(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530549
550 return 0;
551}
Chris Packham1de16f72018-07-09 21:34:00 +1200552#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530553
Chris Packham1de16f72018-07-09 21:34:00 +1200554#ifdef CONFIG_DM_ETH
555static int mvgbe_write_hwaddr(struct udevice *dev)
556{
557 struct eth_pdata *pdata = dev_get_platdata(dev);
558
559 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
560
561 return 0;
562}
563#else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200564static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530565{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200566 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530567
568 /* Programs net device MAC address after initialization */
Chris Packham465f5cf2018-07-09 21:33:59 +1200569 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530570 return 0;
571}
Chris Packham1de16f72018-07-09 21:34:00 +1200572#endif
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530573
Chris Packham465f5cf2018-07-09 21:33:59 +1200574static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
575 int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530576{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200577 struct mvgbe_registers *regs = dmvgbe->regs;
578 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200579 void *p = (void *)dataptr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200580 u32 cmd_sts;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000581 u32 txuq0_reg_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530582
Simon Kagstrome9220b32009-08-20 10:14:11 +0200583 /* Copy buffer if it's misaligned */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530584 if ((u32) dataptr & 0x07) {
Simon Kagstrome9220b32009-08-20 10:14:11 +0200585 if (datasize > PKTSIZE_ALIGN) {
586 printf("Non-aligned data too large (%d)\n",
587 datasize);
588 return -1;
589 }
590
Albert Aribaude91d7d32010-07-12 22:24:28 +0200591 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
592 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530593 }
Simon Kagstrome9220b32009-08-20 10:14:11 +0200594
Albert Aribaude91d7d32010-07-12 22:24:28 +0200595 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
596 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
597 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
598 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200599 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530600 p_txdesc->byte_cnt = datasize;
601
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200602 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000603 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
604 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200605
606 /* ensure tx desc writes above are performed before we start Tx DMA */
607 isb();
608
609 /* Apply send command using zeroth TXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200610 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530611
612 /*
613 * wait for packet xmit completion
614 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200615 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200616 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530617 /* return fail if error is detected */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200618 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
619 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
620 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500621 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530622 return -1;
623 }
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200624 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530625 };
626 return 0;
627}
628
Chris Packham1de16f72018-07-09 21:34:00 +1200629#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200630static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530631{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200632 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200633
634 return __mvgbe_send(dmvgbe, dataptr, datasize);
635}
Chris Packham1de16f72018-07-09 21:34:00 +1200636#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200637
638static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
639{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200640 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200641 u32 cmd_sts;
642 u32 timeout = 0;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000643 u32 rxdesc_curr_addr;
Chris Packham465f5cf2018-07-09 21:33:59 +1200644 unsigned char *data;
645 int rx_bytes = 0;
646
647 *packetp = NULL;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530648
649 /* wait untill rx packet available or timeout */
650 do {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200651 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530652 timeout++;
653 else {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500654 debug("%s time out...\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530655 return -1;
656 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200657 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530658
659 if (p_rxdesc_curr->byte_cnt != 0) {
660 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500661 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530662 (u32) p_rxdesc_curr->buf_ptr,
663 (u32) p_rxdesc_curr->cmd_sts);
664 }
665
666 /*
667 * In case received a packet without first/last bits on
668 * OR the error summary bit is on,
669 * the packets needs to be dropeed.
670 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200671 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
672
673 if ((cmd_sts &
Albert Aribaude91d7d32010-07-12 22:24:28 +0200674 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
675 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530676
677 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500678 " multiple descriptors\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530679
Albert Aribaude91d7d32010-07-12 22:24:28 +0200680 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530681
682 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500683 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530684
685 } else {
686 /* !!! call higher layer processing */
687 debug("%s: Sending Received packet to"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500688 " upper layer (net_process_received_packet)\n",
689 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530690
Chris Packham465f5cf2018-07-09 21:33:59 +1200691 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
692 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
693 RX_BUF_OFFSET);
694
695 *packetp = data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530696 }
697 /*
698 * free these descriptors and point next in the ring
699 */
700 p_rxdesc_curr->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200701 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530702 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
703 p_rxdesc_curr->byte_cnt = 0;
704
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000705 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
706 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200707
Chris Packham465f5cf2018-07-09 21:33:59 +1200708 return rx_bytes;
709}
710
Chris Packham1de16f72018-07-09 21:34:00 +1200711#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200712static int mvgbe_recv(struct eth_device *dev)
713{
714 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
715 uchar *packet;
716 int ret;
717
718 ret = __mvgbe_recv(dmvgbe, &packet);
719 if (ret < 0)
720 return ret;
721
722 net_process_received_packet(packet, ret);
723
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530724 return 0;
725}
Chris Packham1de16f72018-07-09 21:34:00 +1200726#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530727
Chris Packham1de16f72018-07-09 21:34:00 +1200728#if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH)
729#if defined(CONFIG_DM_ETH)
730static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
731 struct mii_dev *bus,
732 phy_interface_t phy_interface,
733 int phyid)
734#else
735static struct phy_device *__mvgbe_phy_init(struct eth_device *dev,
736 struct mii_dev *bus,
737 phy_interface_t phy_interface,
738 int phyid)
739#endif
740{
741 struct phy_device *phydev;
742
743 /* Set phy address of the port */
744 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
745 phyid);
746
747 phydev = phy_connect(bus, phyid, dev, phy_interface);
748 if (!phydev) {
749 printf("phy_connect failed\n");
750 return NULL;
751 }
752
753 phy_config(phydev);
754 phy_startup(phydev);
755
756 return phydev;
757}
758#endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */
759
760#if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100761int mvgbe_phylib_init(struct eth_device *dev, int phyid)
762{
763 struct mii_dev *bus;
764 struct phy_device *phydev;
765 int ret;
766
767 bus = mdio_alloc();
768 if (!bus) {
769 printf("mdio_alloc failed\n");
770 return -ENOMEM;
771 }
Chris Packhamcee2fa32016-11-01 10:48:32 +1300772 bus->read = smi_reg_read;
773 bus->write = smi_reg_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000774 strcpy(bus->name, dev->name);
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100775
776 ret = mdio_register(bus);
777 if (ret) {
778 printf("mdio_register failed\n");
779 free(bus);
780 return -ENOMEM;
781 }
782
Chris Packham1de16f72018-07-09 21:34:00 +1200783 phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid);
784 if (!phydev)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100785 return -ENODEV;
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100786
787 return 0;
788}
789#endif
790
Chris Packham1de16f72018-07-09 21:34:00 +1200791static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
792{
793 dmvgbe->p_rxdesc = memalign(PKTALIGN,
794 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
795 if (!dmvgbe->p_rxdesc)
796 goto error1;
797
798 dmvgbe->p_rxbuf = memalign(PKTALIGN,
799 RINGSZ * PKTSIZE_ALIGN + 1);
800 if (!dmvgbe->p_rxbuf)
801 goto error2;
802
803 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
804 if (!dmvgbe->p_aligned_txbuf)
805 goto error3;
806
807 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
808 if (!dmvgbe->p_txdesc)
809 goto error4;
810
811 return 0;
812
813error4:
814 free(dmvgbe->p_aligned_txbuf);
815error3:
816 free(dmvgbe->p_rxbuf);
817error2:
818 free(dmvgbe->p_rxdesc);
819error1:
820 return -ENOMEM;
821}
822
823#ifndef CONFIG_DM_ETH
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900824int mvgbe_initialize(struct bd_info *bis)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530825{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200826 struct mvgbe_device *dmvgbe;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530827 struct eth_device *dev;
828 int devnum;
Chris Packham1de16f72018-07-09 21:34:00 +1200829 int ret;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200830 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530831
Albert Aribaude91d7d32010-07-12 22:24:28 +0200832 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530833 /*skip if port is configured not to use */
834 if (used_ports[devnum] == 0)
835 continue;
836
Albert Aribaude91d7d32010-07-12 22:24:28 +0200837 dmvgbe = malloc(sizeof(struct mvgbe_device));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200838 if (!dmvgbe)
Chris Packham1de16f72018-07-09 21:34:00 +1200839 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530840
Albert Aribaude91d7d32010-07-12 22:24:28 +0200841 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Chris Packham1de16f72018-07-09 21:34:00 +1200842 ret = mvgbe_alloc_buffers(dmvgbe);
843 if (ret) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530844 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500845 __func__);
Chris Packham1de16f72018-07-09 21:34:00 +1200846 free(dmvgbe);
847 return ret;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530848 }
849
Albert Aribaude91d7d32010-07-12 22:24:28 +0200850 dev = &dmvgbe->dev;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530851
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000852 /* must be less than sizeof(dev->name) */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530853 sprintf(dev->name, "egiga%d", devnum);
854
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530855 switch (devnum) {
856 case 0:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200857 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530858 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200859#if defined(MVGBE1_BASE)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530860 case 1:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200861 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530862 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200863#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530864 default: /* this should never happen */
865 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500866 __func__, devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530867 return -1;
868 }
869
Albert Aribaude91d7d32010-07-12 22:24:28 +0200870 dev->init = (void *)mvgbe_init;
871 dev->halt = (void *)mvgbe_halt;
872 dev->send = (void *)mvgbe_send;
873 dev->recv = (void *)mvgbe_recv;
874 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530875
876 eth_register(dev);
877
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100878#if defined(CONFIG_PHYLIB)
879 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
880#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500881 int retval;
882 struct mii_dev *mdiodev = mdio_alloc();
883 if (!mdiodev)
884 return -ENOMEM;
885 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
886 mdiodev->read = smi_reg_read;
887 mdiodev->write = smi_reg_write;
888
889 retval = mdio_register(mdiodev);
890 if (retval < 0)
891 return retval;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530892 /* Set phy address of the port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200893 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
894 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530895#endif
Chris Packham1de16f72018-07-09 21:34:00 +1200896 }
897 return 0;
898}
899#endif
900
901#ifdef CONFIG_DM_ETH
902static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
903{
904 return dmvgbe->phyaddr > PHY_MAX_ADDR;
905}
906
907static int mvgbe_start(struct udevice *dev)
908{
909 struct eth_pdata *pdata = dev_get_platdata(dev);
910 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
911 int ret;
912
913 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
914 if (ret)
915 return ret;
916
917 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
918 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
919 dmvgbe->phy_interface,
920 dmvgbe->phyaddr);
921 if (!dmvgbe->phydev)
922 return -ENODEV;
923 }
924
925 return 0;
926}
927
928static int mvgbe_send(struct udevice *dev, void *packet, int length)
929{
930 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
931
932 return __mvgbe_send(dmvgbe, packet, length);
933}
934
935static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
936{
937 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
938
939 return __mvgbe_recv(dmvgbe, packetp);
940}
941
942static void mvgbe_stop(struct udevice *dev)
943{
944 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
945
946 __mvgbe_halt(dmvgbe);
947}
948
949static int mvgbe_probe(struct udevice *dev)
950{
951 struct eth_pdata *pdata = dev_get_platdata(dev);
952 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
953 struct mii_dev *bus;
954 int ret;
955
956 ret = mvgbe_alloc_buffers(dmvgbe);
957 if (ret)
958 return ret;
959
960 dmvgbe->regs = (void __iomem *)pdata->iobase;
961
962 bus = mdio_alloc();
963 if (!bus) {
964 printf("Failed to allocate MDIO bus\n");
965 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530966 }
Chris Packham1de16f72018-07-09 21:34:00 +1200967
968 bus->read = smi_reg_read;
969 bus->write = smi_reg_write;
970 snprintf(bus->name, sizeof(bus->name), dev->name);
971 bus->priv = dmvgbe;
972 dmvgbe->bus = bus;
973
974 ret = mdio_register(bus);
975 if (ret < 0)
976 return ret;
977
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530978 return 0;
Prafulla Wadaskar12618ef2009-07-01 20:34:51 +0200979}
Chris Packham1de16f72018-07-09 21:34:00 +1200980
981static const struct eth_ops mvgbe_ops = {
982 .start = mvgbe_start,
983 .send = mvgbe_send,
984 .recv = mvgbe_recv,
985 .stop = mvgbe_stop,
986 .write_hwaddr = mvgbe_write_hwaddr,
987};
988
989static int mvgbe_ofdata_to_platdata(struct udevice *dev)
990{
991 struct eth_pdata *pdata = dev_get_platdata(dev);
992 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
993 void *blob = (void *)gd->fdt_blob;
994 int node = dev_of_offset(dev);
995 const char *phy_mode;
996 int fl_node;
997 int pnode;
998 unsigned long addr;
999
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001000 pdata->iobase = dev_read_addr(dev);
Chris Packham1de16f72018-07-09 21:34:00 +12001001 pdata->phy_interface = -1;
1002
1003 pnode = fdt_node_offset_by_compatible(blob, node,
1004 "marvell,kirkwood-eth-port");
1005
1006 /* Get phy-mode / phy_interface from DT */
1007 phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL);
1008 if (phy_mode)
1009 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
Chris Packham7ca5e692018-12-04 19:54:30 +13001010 else
1011 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
Chris Packham1de16f72018-07-09 21:34:00 +12001012
1013 dmvgbe->phy_interface = pdata->phy_interface;
1014
1015 /* fetch 'fixed-link' property */
1016 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
1017 if (fl_node != -FDT_ERR_NOTFOUND) {
1018 /* set phy_addr to invalid value for fixed link */
1019 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
1020 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1021 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1022 } else {
1023 /* Now read phyaddr from DT */
1024 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
1025 if (addr > 0)
1026 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1027 }
1028
1029 return 0;
1030}
1031
1032static const struct udevice_id mvgbe_ids[] = {
1033 { .compatible = "marvell,kirkwood-eth" },
1034 { }
1035};
1036
1037U_BOOT_DRIVER(mvgbe) = {
1038 .name = "mvgbe",
1039 .id = UCLASS_ETH,
1040 .of_match = mvgbe_ids,
1041 .ofdata_to_platdata = mvgbe_ofdata_to_platdata,
1042 .probe = mvgbe_probe,
1043 .ops = &mvgbe_ops,
1044 .priv_auto_alloc_size = sizeof(struct mvgbe_device),
1045 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1046};
1047#endif /* CONFIG_DM_ETH */