blob: 954bf86121a4988197fd965f81e4af6d477ef4d5 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Lei Wen298ae912011-10-18 20:11:42 +053022#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090024#include <linux/errno.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053025#include <asm/types.h>
Lei Wen298ae912011-10-18 20:11:42 +053026#include <asm/system.h>
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053027#include <asm/byteorder.h>
Anatolij Gustschinc8b222e2011-10-29 10:09:22 +000028#include <asm/arch/cpu.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020029
Trevor Woernerbb7ab072020-05-06 08:02:40 -040030#if defined(CONFIG_ARCH_KIRKWOOD)
Stefan Roesec2437842014-10-22 12:13:06 +020031#include <asm/arch/soc.h>
Trevor Woernerf9953752020-05-06 08:02:38 -040032#elif defined(CONFIG_ARCH_ORION5X)
Albert Aribaud8a995232010-07-12 22:24:29 +020033#include <asm/arch/orion5x.h>
Albert Aribaude91d7d32010-07-12 22:24:28 +020034#endif
35
Albert Aribaud0d027d92010-07-12 22:24:27 +020036#include "mvgbe.h"
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053037
Albert Aribauda7564072010-07-05 20:15:25 +020038DECLARE_GLOBAL_DATA_PTR;
39
Luka Perkov95acd992013-11-11 07:27:53 +010040#ifndef CONFIG_MVGBE_PORTS
41# define CONFIG_MVGBE_PORTS {0, 0}
42#endif
43
Albert Aribaude91d7d32010-07-12 22:24:28 +020044#define MV_PHY_ADR_REQUEST 0xee
45#define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
Simon Kagstromab9ca512009-08-20 10:12:28 +020046
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +010047#if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Chris Packhamf42e5b92018-06-09 20:46:16 +120048static int smi_wait_ready(struct mvgbe_device *dmvgbe)
49{
50 int ret;
51
52 ret = wait_for_bit_le32(&MVGBE_SMI_REG, MVGBE_PHY_SMI_BUSY_MASK, false,
53 MVGBE_PHY_SMI_TIMEOUT_MS, false);
54 if (ret) {
55 printf("Error: SMI busy timeout\n");
56 return ret;
57 }
58
59 return 0;
60}
61
Chris Packham465f5cf2018-07-09 21:33:59 +120062static int __mvgbe_mdio_read(struct mvgbe_device *dmvgbe, int phy_adr,
63 int devad, int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053064{
Albert Aribaude91d7d32010-07-12 22:24:28 +020065 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053066 u32 smi_reg;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +020067 u32 timeout;
Chris Packham465f5cf2018-07-09 21:33:59 +120068 u16 data = 0;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053069
70 /* Phyadr read request */
Albert Aribaude91d7d32010-07-12 22:24:28 +020071 if (phy_adr == MV_PHY_ADR_REQUEST &&
72 reg_ofs == MV_PHY_ADR_REQUEST) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053073 /* */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -050074 data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
75 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053076 }
77 /* check parameters */
78 if (phy_adr > PHYADR_MASK) {
79 printf("Err..(%s) Invalid PHY address %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050080 __func__, phy_adr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053081 return -EFAULT;
82 }
83 if (reg_ofs > PHYREG_MASK) {
84 printf("Err..(%s) Invalid register offset %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -050085 __func__, reg_ofs);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053086 return -EFAULT;
87 }
88
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053089 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +120090 if (smi_wait_ready(dmvgbe) < 0)
91 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053092
93 /* fill the phy address and regiser offset and read opcode */
Albert Aribaude91d7d32010-07-12 22:24:28 +020094 smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
95 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
96 | MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +053097
98 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +020099 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530100
101 /*wait till read value is ready */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200102 timeout = MVGBE_PHY_SMI_TIMEOUT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530103
104 do {
105 /* read smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200106 smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530107 if (timeout-- == 0) {
108 printf("Err..(%s) SMI read ready timeout\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500109 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530110 return -EFAULT;
111 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200112 } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530113
114 /* Wait for the data to update in the SMI register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200115 for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
116 ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530117
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500118 data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530119
Joe Hershberger9f09a362015-04-08 01:41:06 -0500120 debug("%s:(adr %d, off %d) value= %04x\n", __func__, phy_adr, reg_ofs,
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500121 data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530122
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500123 return data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530124}
125
126/*
Chris Packham465f5cf2018-07-09 21:33:59 +1200127 * smi_reg_read - miiphy_read callback function.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530128 *
Chris Packham465f5cf2018-07-09 21:33:59 +1200129 * Returns 16bit phy register value, or -EFAULT on error
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530130 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200131static int smi_reg_read(struct mii_dev *bus, int phy_adr, int devad,
132 int reg_ofs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530133{
Chris Packham1de16f72018-07-09 21:34:00 +1200134#ifdef CONFIG_DM_ETH
135 struct mvgbe_device *dmvgbe = bus->priv;
136#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500137 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200138 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200139#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200140
141 return __mvgbe_mdio_read(dmvgbe, phy_adr, devad, reg_ofs);
142}
143
144static int __mvgbe_mdio_write(struct mvgbe_device *dmvgbe, int phy_adr,
145 int devad, int reg_ofs, u16 data)
146{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200147 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530148 u32 smi_reg;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530149
150 /* Phyadr write request*/
Albert Aribaude91d7d32010-07-12 22:24:28 +0200151 if (phy_adr == MV_PHY_ADR_REQUEST &&
152 reg_ofs == MV_PHY_ADR_REQUEST) {
153 MVGBE_REG_WR(regs->phyadr, data);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530154 return 0;
155 }
156
157 /* check parameters */
158 if (phy_adr > PHYADR_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500159 printf("Err..(%s) Invalid phy address\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530160 return -EINVAL;
161 }
162 if (reg_ofs > PHYREG_MASK) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500163 printf("Err..(%s) Invalid register offset\n", __func__);
Chris Packhamf42e5b92018-06-09 20:46:16 +1200164 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530165 }
166
167 /* wait till the SMI is not busy */
Chris Packhamf42e5b92018-06-09 20:46:16 +1200168 if (smi_wait_ready(dmvgbe) < 0)
169 return -EFAULT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530170
171 /* fill the phy addr and reg offset and write opcode and data */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200172 smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
173 smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
174 | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
175 smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530176
177 /* write the smi register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200178 MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530179
180 return 0;
181}
Chris Packham465f5cf2018-07-09 21:33:59 +1200182
183/*
184 * smi_reg_write - miiphy_write callback function.
185 *
186 * Returns 0 if write succeed, -EFAULT on error
187 */
188static int smi_reg_write(struct mii_dev *bus, int phy_adr, int devad,
189 int reg_ofs, u16 data)
190{
Chris Packham1de16f72018-07-09 21:34:00 +1200191#ifdef CONFIG_DM_ETH
192 struct mvgbe_device *dmvgbe = bus->priv;
193#else
Chris Packham465f5cf2018-07-09 21:33:59 +1200194 struct eth_device *dev = eth_get_dev_by_name(bus->name);
195 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200196#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200197
198 return __mvgbe_mdio_write(dmvgbe, phy_adr, devad, reg_ofs, data);
199}
Stefan Bigler96455292012-03-26 00:02:13 +0000200#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530201
202/* Stop and checks all queues */
203static void stop_queue(u32 * qreg)
204{
205 u32 reg_data;
206
207 reg_data = readl(qreg);
208
209 if (reg_data & 0xFF) {
210 /* Issue stop command for active channels only */
211 writel((reg_data << 8), qreg);
212
213 /* Wait for all queue activity to terminate. */
214 do {
215 /*
216 * Check port cause register that all queues
217 * are stopped
218 */
219 reg_data = readl(qreg);
220 }
221 while (reg_data & 0xFF);
222 }
223}
224
225/*
226 * set_access_control - Config address decode parameters for Ethernet unit
227 *
228 * This function configures the address decode parameters for the Gigabit
229 * Ethernet Controller according the given parameters struct.
230 *
231 * @regs Register struct pointer.
232 * @param Address decode parameter struct.
233 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200234static void set_access_control(struct mvgbe_registers *regs,
235 struct mvgbe_winparam *param)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530236{
237 u32 access_prot_reg;
238
239 /* Set access control register */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200240 access_prot_reg = MVGBE_REG_RD(regs->epap);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530241 /* clear window permission */
242 access_prot_reg &= (~(3 << (param->win * 2)));
243 access_prot_reg |= (param->access_ctrl << (param->win * 2));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200244 MVGBE_REG_WR(regs->epap, access_prot_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530245
246 /* Set window Size reg (SR) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200247 MVGBE_REG_WR(regs->barsz[param->win].size,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530248 (((param->size / 0x10000) - 1) << 16));
249
250 /* Set window Base address reg (BA) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200251 MVGBE_REG_WR(regs->barsz[param->win].bar,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530252 (param->target | param->attrib | param->base_addr));
253 /* High address remap reg (HARR) */
254 if (param->win < 4)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200255 MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530256
257 /* Base address enable reg (BARER) */
258 if (param->enable == 1)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200259 MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530260 else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200261 MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530262}
263
Albert Aribaude91d7d32010-07-12 22:24:28 +0200264static void set_dram_access(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530265{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200266 struct mvgbe_winparam win_param;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530267 int i;
268
269 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
270 /* Set access parameters for DRAM bank i */
271 win_param.win = i; /* Use Ethernet window i */
272 /* Window target - DDR */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200273 win_param.target = MVGBE_TARGET_DRAM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530274 /* Enable full access */
275 win_param.access_ctrl = EWIN_ACCESS_FULL;
276 win_param.high_addr = 0;
Albert Aribauda7564072010-07-05 20:15:25 +0200277 /* Get bank base and size */
278 win_param.base_addr = gd->bd->bi_dram[i].start;
279 win_param.size = gd->bd->bi_dram[i].size;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530280 if (win_param.size == 0)
281 win_param.enable = 0;
282 else
283 win_param.enable = 1; /* Enable the access */
284
285 /* Enable DRAM bank */
286 switch (i) {
287 case 0:
288 win_param.attrib = EBAR_DRAM_CS0;
289 break;
290 case 1:
291 win_param.attrib = EBAR_DRAM_CS1;
292 break;
293 case 2:
294 win_param.attrib = EBAR_DRAM_CS2;
295 break;
296 case 3:
297 win_param.attrib = EBAR_DRAM_CS3;
298 break;
299 default:
Albert Aribauda7564072010-07-05 20:15:25 +0200300 /* invalid bank, disable access */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530301 win_param.enable = 0;
302 win_param.attrib = 0;
303 break;
304 }
305 /* Set the access control for address window(EPAPR) RD/WR */
306 set_access_control(regs, &win_param);
307 }
308}
309
310/*
311 * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
312 *
313 * Go through all the DA filter tables (Unicast, Special Multicast & Other
314 * Multicast) and set each entry to 0.
315 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200316static void port_init_mac_tables(struct mvgbe_registers *regs)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530317{
318 int table_index;
319
320 /* Clear DA filter unicast table (Ex_dFUT) */
321 for (table_index = 0; table_index < 4; ++table_index)
Albert Aribaude91d7d32010-07-12 22:24:28 +0200322 MVGBE_REG_WR(regs->dfut[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530323
324 for (table_index = 0; table_index < 64; ++table_index) {
325 /* Clear DA filter special multicast table (Ex_dFSMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200326 MVGBE_REG_WR(regs->dfsmt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530327 /* Clear DA filter other multicast table (Ex_dFOMT) */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200328 MVGBE_REG_WR(regs->dfomt[table_index], 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530329 }
330}
331
332/*
333 * port_uc_addr - This function Set the port unicast address table
334 *
335 * This function locates the proper entry in the Unicast table for the
336 * specified MAC nibble and sets its properties according to function
337 * parameters.
338 * This function add/removes MAC addresses from the port unicast address
339 * table.
340 *
341 * @uc_nibble Unicast MAC Address last nibble.
342 * @option 0 = Add, 1 = remove address.
343 *
344 * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
345 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200346static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530347 int option)
348{
349 u32 unicast_reg;
350 u32 tbl_offset;
351 u32 reg_offset;
352
353 /* Locate the Unicast table entry */
354 uc_nibble = (0xf & uc_nibble);
355 /* Register offset from unicast table base */
356 tbl_offset = (uc_nibble / 4);
357 /* Entry offset within the above register */
358 reg_offset = uc_nibble % 4;
359
360 switch (option) {
361 case REJECT_MAC_ADDR:
362 /*
363 * Clear accepts frame bit at specified unicast
364 * DA table entry
365 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200366 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530367 unicast_reg &= (0xFF << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200368 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530369 break;
370 case ACCEPT_MAC_ADDR:
371 /* Set accepts frame bit at unicast DA filter table entry */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200372 unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530373 unicast_reg &= (0xFF << (8 * reg_offset));
374 unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200375 MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530376 break;
377 default:
378 return 0;
379 }
380 return 1;
381}
382
383/*
384 * port_uc_addr_set - This function Set the port Unicast address.
385 */
Chris Packham465f5cf2018-07-09 21:33:59 +1200386static void port_uc_addr_set(struct mvgbe_device *dmvgbe, u8 *p_addr)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530387{
Chris Packham465f5cf2018-07-09 21:33:59 +1200388 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530389 u32 mac_h;
390 u32 mac_l;
391
392 mac_l = (p_addr[4] << 8) | (p_addr[5]);
393 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
394 (p_addr[3] << 0);
395
Albert Aribaude91d7d32010-07-12 22:24:28 +0200396 MVGBE_REG_WR(regs->macal, mac_l);
397 MVGBE_REG_WR(regs->macah, mac_h);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530398
399 /* Accept frames of this address */
400 port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
401}
402
403/*
Albert Aribaude91d7d32010-07-12 22:24:28 +0200404 * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530405 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200406static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530407{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200408 struct mvgbe_rxdesc *p_rx_desc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530409 int i;
410
411 /* initialize the Rx descriptors ring */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200412 p_rx_desc = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530413 for (i = 0; i < RINGSZ; i++) {
414 p_rx_desc->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200415 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530416 p_rx_desc->buf_size = PKTSIZE_ALIGN;
417 p_rx_desc->byte_cnt = 0;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200418 p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530419 if (i == (RINGSZ - 1))
Albert Aribaude91d7d32010-07-12 22:24:28 +0200420 p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530421 else {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200422 p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
423 ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530424 p_rx_desc = p_rx_desc->nxtdesc_p;
425 }
426 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200427 dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530428}
429
Chris Packham1de16f72018-07-09 21:34:00 +1200430static int __mvgbe_init(struct mvgbe_device *dmvgbe, u8 *enetaddr,
431 const char *name)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530432{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200433 struct mvgbe_registers *regs = dmvgbe->regs;
Sascha Silbe0984d642013-08-11 17:08:23 +0200434#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
435 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200436 !defined(CONFIG_DM_ETH) && \
Sascha Silbe0984d642013-08-11 17:08:23 +0200437 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200438 int i;
Prafulla Wadaskar9841dec2009-09-09 15:59:19 +0530439#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530440 /* setup RX rings */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200441 mvgbe_init_rx_desc_ring(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530442
443 /* Clear the ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200444 MVGBE_REG_WR(regs->ic, 0);
445 MVGBE_REG_WR(regs->ice, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530446 /* Unmask RX buffer and TX end interrupt */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200447 MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530448 /* Unmask phy and link status changes interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200449 MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530450
451 set_dram_access(regs);
452 port_init_mac_tables(regs);
Chris Packham1de16f72018-07-09 21:34:00 +1200453 port_uc_addr_set(dmvgbe, enetaddr);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530454
455 /* Assign port configuration and command. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200456 MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
457 MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
458 MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530459
460 /* Assign port SDMA configuration */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200461 MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
462 MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
463 MVGBE_REG_WR(regs->tqx[0].tqxtbc,
464 (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530465 /* Turn off the port/RXUQ bandwidth limitation */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200466 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530467
468 /* Set maximum receive buffer to 9700 bytes */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200469 MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
470 | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530471
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530472 /* Enable port initially */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200473 MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530474
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530475 /*
476 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
477 * disable the leaky bucket mechanism .
478 */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200479 MVGBE_REG_WR(regs->pmtu, 0);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530480
481 /* Assignment of Rx CRDB of given RXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200482 MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200483 /* ensure previous write is done before enabling Rx DMA */
484 isb();
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530485 /* Enable port Rx. */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200486 MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530487
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100488#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
489 !defined(CONFIG_PHYLIB) && \
Chris Packham1de16f72018-07-09 21:34:00 +1200490 !defined(CONFIG_DM_ETH) && \
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100491 defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200492 /* Wait up to 5s for the link status */
493 for (i = 0; i < 5; i++) {
494 u16 phyadr;
495
Chris Packham1de16f72018-07-09 21:34:00 +1200496 miiphy_read(name, MV_PHY_ADR_REQUEST,
Albert Aribaude91d7d32010-07-12 22:24:28 +0200497 MV_PHY_ADR_REQUEST, &phyadr);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200498 /* Return if we get link up */
Chris Packham1de16f72018-07-09 21:34:00 +1200499 if (miiphy_link(name, phyadr))
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200500 return 0;
501 udelay(1000000);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530502 }
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200503
Chris Packham1de16f72018-07-09 21:34:00 +1200504 printf("No link on %s\n", name);
Simon Kagstrom15cc5a62009-08-20 10:13:06 +0200505 return -1;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530506#endif
507 return 0;
508}
509
Chris Packham1de16f72018-07-09 21:34:00 +1200510#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200511static int mvgbe_init(struct eth_device *dev)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530512{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200513 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200514
Chris Packham1de16f72018-07-09 21:34:00 +1200515 return __mvgbe_init(dmvgbe, dmvgbe->dev.enetaddr, dmvgbe->dev.name);
Chris Packham465f5cf2018-07-09 21:33:59 +1200516}
Chris Packham1de16f72018-07-09 21:34:00 +1200517#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200518
519static void __mvgbe_halt(struct mvgbe_device *dmvgbe)
520{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200521 struct mvgbe_registers *regs = dmvgbe->regs;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530522
523 /* Disable all gigE address decoder */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200524 MVGBE_REG_WR(regs->bare, 0x3f);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530525
526 stop_queue(&regs->tqc);
527 stop_queue(&regs->rqc);
528
Prafulla Wadaskar60ee8a22010-04-06 21:33:08 +0530529 /* Disable port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200530 MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530531 /* Set port is not reset */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200532 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530533#ifdef CONFIG_SYS_MII_MODE
534 /* Set MMI interface up */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200535 MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530536#endif
537 /* Disable & mask ethernet port interrupts */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200538 MVGBE_REG_WR(regs->ic, 0);
539 MVGBE_REG_WR(regs->ice, 0);
540 MVGBE_REG_WR(regs->pim, 0);
541 MVGBE_REG_WR(regs->peim, 0);
Chris Packham465f5cf2018-07-09 21:33:59 +1200542}
543
Chris Packham1de16f72018-07-09 21:34:00 +1200544#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200545static int mvgbe_halt(struct eth_device *dev)
546{
547 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
548
549 __mvgbe_halt(dmvgbe);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530550
551 return 0;
552}
Chris Packham1de16f72018-07-09 21:34:00 +1200553#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530554
Chris Packham1de16f72018-07-09 21:34:00 +1200555#ifdef CONFIG_DM_ETH
556static int mvgbe_write_hwaddr(struct udevice *dev)
557{
Simon Glassfa20e932020-12-03 16:55:20 -0700558 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200559
560 port_uc_addr_set(dev_get_priv(dev), pdata->enetaddr);
561
562 return 0;
563}
564#else
Albert Aribaude91d7d32010-07-12 22:24:28 +0200565static int mvgbe_write_hwaddr(struct eth_device *dev)
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530566{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200567 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530568
569 /* Programs net device MAC address after initialization */
Chris Packham465f5cf2018-07-09 21:33:59 +1200570 port_uc_addr_set(dmvgbe, dmvgbe->dev.enetaddr);
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530571 return 0;
572}
Chris Packham1de16f72018-07-09 21:34:00 +1200573#endif
Prafulla Wadaskar7dae2eb2010-04-06 22:21:33 +0530574
Chris Packham465f5cf2018-07-09 21:33:59 +1200575static int __mvgbe_send(struct mvgbe_device *dmvgbe, void *dataptr,
576 int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530577{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200578 struct mvgbe_registers *regs = dmvgbe->regs;
579 struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200580 void *p = (void *)dataptr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200581 u32 cmd_sts;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000582 u32 txuq0_reg_addr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530583
Simon Kagstrome9220b32009-08-20 10:14:11 +0200584 /* Copy buffer if it's misaligned */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530585 if ((u32) dataptr & 0x07) {
Simon Kagstrome9220b32009-08-20 10:14:11 +0200586 if (datasize > PKTSIZE_ALIGN) {
587 printf("Non-aligned data too large (%d)\n",
588 datasize);
589 return -1;
590 }
591
Albert Aribaude91d7d32010-07-12 22:24:28 +0200592 memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
593 p = dmvgbe->p_aligned_txbuf;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530594 }
Simon Kagstrome9220b32009-08-20 10:14:11 +0200595
Albert Aribaude91d7d32010-07-12 22:24:28 +0200596 p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
597 p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
598 p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
599 p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
Simon Kagstrome9220b32009-08-20 10:14:11 +0200600 p_txdesc->buf_ptr = (u8 *) p;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530601 p_txdesc->byte_cnt = datasize;
602
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200603 /* Set this tc desc as zeroth TXUQ */
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000604 txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
605 writel((u32) p_txdesc, txuq0_reg_addr);
Albert Aribaudcc2b8e32010-07-10 15:41:29 +0200606
607 /* ensure tx desc writes above are performed before we start Tx DMA */
608 isb();
609
610 /* Apply send command using zeroth TXUQ */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200611 MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530612
613 /*
614 * wait for packet xmit completion
615 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200616 cmd_sts = readl(&p_txdesc->cmd_sts);
Albert Aribaude91d7d32010-07-12 22:24:28 +0200617 while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530618 /* return fail if error is detected */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200619 if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
620 (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
621 cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500622 printf("Err..(%s) in xmit packet\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530623 return -1;
624 }
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200625 cmd_sts = readl(&p_txdesc->cmd_sts);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530626 };
627 return 0;
628}
629
Chris Packham1de16f72018-07-09 21:34:00 +1200630#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200631static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530632{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200633 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
Chris Packham465f5cf2018-07-09 21:33:59 +1200634
635 return __mvgbe_send(dmvgbe, dataptr, datasize);
636}
Chris Packham1de16f72018-07-09 21:34:00 +1200637#endif
Chris Packham465f5cf2018-07-09 21:33:59 +1200638
639static int __mvgbe_recv(struct mvgbe_device *dmvgbe, uchar **packetp)
640{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200641 struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200642 u32 cmd_sts;
643 u32 timeout = 0;
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000644 u32 rxdesc_curr_addr;
Chris Packham465f5cf2018-07-09 21:33:59 +1200645 unsigned char *data;
646 int rx_bytes = 0;
647
648 *packetp = NULL;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530649
650 /* wait untill rx packet available or timeout */
651 do {
Albert Aribaude91d7d32010-07-12 22:24:28 +0200652 if (timeout < MVGBE_PHY_SMI_TIMEOUT)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530653 timeout++;
654 else {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500655 debug("%s time out...\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530656 return -1;
657 }
Albert Aribaude91d7d32010-07-12 22:24:28 +0200658 } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530659
660 if (p_rxdesc_curr->byte_cnt != 0) {
661 debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500662 __func__, (u32) p_rxdesc_curr->byte_cnt,
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530663 (u32) p_rxdesc_curr->buf_ptr,
664 (u32) p_rxdesc_curr->cmd_sts);
665 }
666
667 /*
668 * In case received a packet without first/last bits on
669 * OR the error summary bit is on,
670 * the packets needs to be dropeed.
671 */
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200672 cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
673
674 if ((cmd_sts &
Albert Aribaude91d7d32010-07-12 22:24:28 +0200675 (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
676 != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530677
678 printf("Err..(%s) Dropping packet spread on"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500679 " multiple descriptors\n", __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530680
Albert Aribaude91d7d32010-07-12 22:24:28 +0200681 } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530682
683 printf("Err..(%s) Dropping packet with errors\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500684 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530685
686 } else {
687 /* !!! call higher layer processing */
688 debug("%s: Sending Received packet to"
Joe Hershberger9f09a362015-04-08 01:41:06 -0500689 " upper layer (net_process_received_packet)\n",
690 __func__);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530691
Chris Packham465f5cf2018-07-09 21:33:59 +1200692 data = (p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET);
693 rx_bytes = (int)(p_rxdesc_curr->byte_cnt -
694 RX_BUF_OFFSET);
695
696 *packetp = data;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530697 }
698 /*
699 * free these descriptors and point next in the ring
700 */
701 p_rxdesc_curr->cmd_sts =
Albert Aribaude91d7d32010-07-12 22:24:28 +0200702 MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530703 p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
704 p_rxdesc_curr->byte_cnt = 0;
705
Anatolij Gustschinda42f242011-11-19 08:59:36 +0000706 rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
707 writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
Simon Kagstrom4d0941c2009-07-08 13:03:18 +0200708
Chris Packham465f5cf2018-07-09 21:33:59 +1200709 return rx_bytes;
710}
711
Chris Packham1de16f72018-07-09 21:34:00 +1200712#ifndef CONFIG_DM_ETH
Chris Packham465f5cf2018-07-09 21:33:59 +1200713static int mvgbe_recv(struct eth_device *dev)
714{
715 struct mvgbe_device *dmvgbe = to_mvgbe(dev);
716 uchar *packet;
717 int ret;
718
719 ret = __mvgbe_recv(dmvgbe, &packet);
720 if (ret < 0)
721 return ret;
722
723 net_process_received_packet(packet, ret);
724
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530725 return 0;
726}
Chris Packham1de16f72018-07-09 21:34:00 +1200727#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530728
Chris Packham1de16f72018-07-09 21:34:00 +1200729#if defined(CONFIG_PHYLIB) || defined(CONFIG_DM_ETH)
730#if defined(CONFIG_DM_ETH)
731static struct phy_device *__mvgbe_phy_init(struct udevice *dev,
732 struct mii_dev *bus,
733 phy_interface_t phy_interface,
734 int phyid)
735#else
736static struct phy_device *__mvgbe_phy_init(struct eth_device *dev,
737 struct mii_dev *bus,
738 phy_interface_t phy_interface,
739 int phyid)
740#endif
741{
742 struct phy_device *phydev;
743
744 /* Set phy address of the port */
745 miiphy_write(dev->name, MV_PHY_ADR_REQUEST, MV_PHY_ADR_REQUEST,
746 phyid);
747
748 phydev = phy_connect(bus, phyid, dev, phy_interface);
749 if (!phydev) {
750 printf("phy_connect failed\n");
751 return NULL;
752 }
753
754 phy_config(phydev);
755 phy_startup(phydev);
756
757 return phydev;
758}
759#endif /* CONFIG_PHYLIB || CONFIG_DM_ETH */
760
761#if defined(CONFIG_PHYLIB) && !defined(CONFIG_DM_ETH)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100762int mvgbe_phylib_init(struct eth_device *dev, int phyid)
763{
764 struct mii_dev *bus;
765 struct phy_device *phydev;
766 int ret;
767
768 bus = mdio_alloc();
769 if (!bus) {
770 printf("mdio_alloc failed\n");
771 return -ENOMEM;
772 }
Chris Packhamcee2fa32016-11-01 10:48:32 +1300773 bus->read = smi_reg_read;
774 bus->write = smi_reg_write;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000775 strcpy(bus->name, dev->name);
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100776
777 ret = mdio_register(bus);
778 if (ret) {
779 printf("mdio_register failed\n");
780 free(bus);
781 return -ENOMEM;
782 }
783
Chris Packham1de16f72018-07-09 21:34:00 +1200784 phydev = __mvgbe_phy_init(dev, bus, PHY_INTERFACE_MODE_RGMII, phyid);
785 if (!phydev)
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100786 return -ENODEV;
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100787
788 return 0;
789}
790#endif
791
Chris Packham1de16f72018-07-09 21:34:00 +1200792static int mvgbe_alloc_buffers(struct mvgbe_device *dmvgbe)
793{
794 dmvgbe->p_rxdesc = memalign(PKTALIGN,
795 MV_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
796 if (!dmvgbe->p_rxdesc)
797 goto error1;
798
799 dmvgbe->p_rxbuf = memalign(PKTALIGN,
800 RINGSZ * PKTSIZE_ALIGN + 1);
801 if (!dmvgbe->p_rxbuf)
802 goto error2;
803
804 dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
805 if (!dmvgbe->p_aligned_txbuf)
806 goto error3;
807
808 dmvgbe->p_txdesc = memalign(PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
809 if (!dmvgbe->p_txdesc)
810 goto error4;
811
812 return 0;
813
814error4:
815 free(dmvgbe->p_aligned_txbuf);
816error3:
817 free(dmvgbe->p_rxbuf);
818error2:
819 free(dmvgbe->p_rxdesc);
820error1:
821 return -ENOMEM;
822}
823
824#ifndef CONFIG_DM_ETH
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900825int mvgbe_initialize(struct bd_info *bis)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530826{
Albert Aribaude91d7d32010-07-12 22:24:28 +0200827 struct mvgbe_device *dmvgbe;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530828 struct eth_device *dev;
829 int devnum;
Chris Packham1de16f72018-07-09 21:34:00 +1200830 int ret;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200831 u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530832
Albert Aribaude91d7d32010-07-12 22:24:28 +0200833 for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530834 /*skip if port is configured not to use */
835 if (used_ports[devnum] == 0)
836 continue;
837
Albert Aribaude91d7d32010-07-12 22:24:28 +0200838 dmvgbe = malloc(sizeof(struct mvgbe_device));
Albert Aribaude91d7d32010-07-12 22:24:28 +0200839 if (!dmvgbe)
Chris Packham1de16f72018-07-09 21:34:00 +1200840 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530841
Albert Aribaude91d7d32010-07-12 22:24:28 +0200842 memset(dmvgbe, 0, sizeof(struct mvgbe_device));
Chris Packham1de16f72018-07-09 21:34:00 +1200843 ret = mvgbe_alloc_buffers(dmvgbe);
844 if (ret) {
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530845 printf("Err.. %s Failed to allocate memory\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500846 __func__);
Chris Packham1de16f72018-07-09 21:34:00 +1200847 free(dmvgbe);
848 return ret;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530849 }
850
Albert Aribaude91d7d32010-07-12 22:24:28 +0200851 dev = &dmvgbe->dev;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530852
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000853 /* must be less than sizeof(dev->name) */
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530854 sprintf(dev->name, "egiga%d", devnum);
855
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530856 switch (devnum) {
857 case 0:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200858 dmvgbe->regs = (void *)MVGBE0_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530859 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200860#if defined(MVGBE1_BASE)
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530861 case 1:
Albert Aribaude91d7d32010-07-12 22:24:28 +0200862 dmvgbe->regs = (void *)MVGBE1_BASE;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530863 break;
Albert Aribaude91d7d32010-07-12 22:24:28 +0200864#endif
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530865 default: /* this should never happen */
866 printf("Err..(%s) Invalid device number %d\n",
Joe Hershberger9f09a362015-04-08 01:41:06 -0500867 __func__, devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530868 return -1;
869 }
870
Albert Aribaude91d7d32010-07-12 22:24:28 +0200871 dev->init = (void *)mvgbe_init;
872 dev->halt = (void *)mvgbe_halt;
873 dev->send = (void *)mvgbe_send;
874 dev->recv = (void *)mvgbe_recv;
875 dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530876
877 eth_register(dev);
878
Sebastian Hesselbarth94a483c2012-12-04 09:32:00 +0100879#if defined(CONFIG_PHYLIB)
880 mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
881#elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500882 int retval;
883 struct mii_dev *mdiodev = mdio_alloc();
884 if (!mdiodev)
885 return -ENOMEM;
Vladimir Oltean208cf4a2021-09-27 14:21:55 +0300886 strlcpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500887 mdiodev->read = smi_reg_read;
888 mdiodev->write = smi_reg_write;
889
890 retval = mdio_register(mdiodev);
891 if (retval < 0)
892 return retval;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530893 /* Set phy address of the port */
Albert Aribaude91d7d32010-07-12 22:24:28 +0200894 miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
895 MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530896#endif
Chris Packham1de16f72018-07-09 21:34:00 +1200897 }
898 return 0;
899}
900#endif
901
902#ifdef CONFIG_DM_ETH
903static int mvgbe_port_is_fixed_link(struct mvgbe_device *dmvgbe)
904{
905 return dmvgbe->phyaddr > PHY_MAX_ADDR;
906}
907
908static int mvgbe_start(struct udevice *dev)
909{
Simon Glassfa20e932020-12-03 16:55:20 -0700910 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200911 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
912 int ret;
913
914 ret = __mvgbe_init(dmvgbe, pdata->enetaddr, dev->name);
915 if (ret)
916 return ret;
917
918 if (!mvgbe_port_is_fixed_link(dmvgbe)) {
919 dmvgbe->phydev = __mvgbe_phy_init(dev, dmvgbe->bus,
920 dmvgbe->phy_interface,
921 dmvgbe->phyaddr);
922 if (!dmvgbe->phydev)
923 return -ENODEV;
924 }
925
926 return 0;
927}
928
929static int mvgbe_send(struct udevice *dev, void *packet, int length)
930{
931 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
932
933 return __mvgbe_send(dmvgbe, packet, length);
934}
935
936static int mvgbe_recv(struct udevice *dev, int flags, uchar **packetp)
937{
938 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
939
940 return __mvgbe_recv(dmvgbe, packetp);
941}
942
943static void mvgbe_stop(struct udevice *dev)
944{
945 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
946
947 __mvgbe_halt(dmvgbe);
948}
949
950static int mvgbe_probe(struct udevice *dev)
951{
Simon Glassfa20e932020-12-03 16:55:20 -0700952 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200953 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
954 struct mii_dev *bus;
955 int ret;
956
957 ret = mvgbe_alloc_buffers(dmvgbe);
958 if (ret)
959 return ret;
960
961 dmvgbe->regs = (void __iomem *)pdata->iobase;
962
963 bus = mdio_alloc();
964 if (!bus) {
965 printf("Failed to allocate MDIO bus\n");
966 return -ENOMEM;
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530967 }
Chris Packham1de16f72018-07-09 21:34:00 +1200968
969 bus->read = smi_reg_read;
970 bus->write = smi_reg_write;
971 snprintf(bus->name, sizeof(bus->name), dev->name);
972 bus->priv = dmvgbe;
973 dmvgbe->bus = bus;
974
975 ret = mdio_register(bus);
976 if (ret < 0)
977 return ret;
978
Prafulla Wadaskarb7a280d2009-06-14 22:33:46 +0530979 return 0;
Prafulla Wadaskar12618ef2009-07-01 20:34:51 +0200980}
Chris Packham1de16f72018-07-09 21:34:00 +1200981
982static const struct eth_ops mvgbe_ops = {
983 .start = mvgbe_start,
984 .send = mvgbe_send,
985 .recv = mvgbe_recv,
986 .stop = mvgbe_stop,
987 .write_hwaddr = mvgbe_write_hwaddr,
988};
989
Simon Glassaad29ae2020-12-03 16:55:21 -0700990static int mvgbe_of_to_plat(struct udevice *dev)
Chris Packham1de16f72018-07-09 21:34:00 +1200991{
Simon Glassfa20e932020-12-03 16:55:20 -0700992 struct eth_pdata *pdata = dev_get_plat(dev);
Chris Packham1de16f72018-07-09 21:34:00 +1200993 struct mvgbe_device *dmvgbe = dev_get_priv(dev);
994 void *blob = (void *)gd->fdt_blob;
995 int node = dev_of_offset(dev);
996 const char *phy_mode;
997 int fl_node;
998 int pnode;
999 unsigned long addr;
1000
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001001 pdata->iobase = dev_read_addr(dev);
Chris Packham1de16f72018-07-09 21:34:00 +12001002 pdata->phy_interface = -1;
1003
1004 pnode = fdt_node_offset_by_compatible(blob, node,
1005 "marvell,kirkwood-eth-port");
1006
1007 /* Get phy-mode / phy_interface from DT */
1008 phy_mode = fdt_getprop(gd->fdt_blob, pnode, "phy-mode", NULL);
1009 if (phy_mode)
1010 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
Chris Packham7ca5e692018-12-04 19:54:30 +13001011 else
1012 pdata->phy_interface = PHY_INTERFACE_MODE_GMII;
Chris Packham1de16f72018-07-09 21:34:00 +12001013
1014 dmvgbe->phy_interface = pdata->phy_interface;
1015
1016 /* fetch 'fixed-link' property */
1017 fl_node = fdt_subnode_offset(blob, pnode, "fixed-link");
1018 if (fl_node != -FDT_ERR_NOTFOUND) {
1019 /* set phy_addr to invalid value for fixed link */
1020 dmvgbe->phyaddr = PHY_MAX_ADDR + 1;
1021 dmvgbe->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1022 dmvgbe->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1023 } else {
1024 /* Now read phyaddr from DT */
1025 addr = fdtdec_lookup_phandle(blob, pnode, "phy-handle");
1026 if (addr > 0)
1027 dmvgbe->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1028 }
1029
1030 return 0;
1031}
1032
1033static const struct udevice_id mvgbe_ids[] = {
1034 { .compatible = "marvell,kirkwood-eth" },
1035 { }
1036};
1037
1038U_BOOT_DRIVER(mvgbe) = {
1039 .name = "mvgbe",
1040 .id = UCLASS_ETH,
1041 .of_match = mvgbe_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001042 .of_to_plat = mvgbe_of_to_plat,
Chris Packham1de16f72018-07-09 21:34:00 +12001043 .probe = mvgbe_probe,
1044 .ops = &mvgbe_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001045 .priv_auto = sizeof(struct mvgbe_device),
Simon Glass71fa5b42020-12-03 16:55:18 -07001046 .plat_auto = sizeof(struct eth_pdata),
Chris Packham1de16f72018-07-09 21:34:00 +12001047};
1048#endif /* CONFIG_DM_ETH */